Initial read from stdin implementation
Anirudh Oppiliappan x@icyphox.sh
Sat, 06 Nov 2021 14:10:55 +0530
1 files changed,
69 insertions(+),
21 deletions(-)
jump to
M
barito.c
→
barito.c
@@ -2,6 +2,9 @@ #include <err.h>
#include <stdio.h> #include <stdlib.h> #include <string.h> +#include <fcntl.h> +#include <poll.h> +#include <unistd.h> #include <X11/Xlib.h> #include <X11/Xatom.h> #include <X11/Xutil.h>@@ -90,7 +93,8 @@ errx(1, "cannot allocate color");
} /* draw text on pixmap */ -static void draw(Drawable pix, XftFont *font, XftColor *color, const char *text) +static void draw_text(Drawable pix, XftFont *font, XftColor *color, + const char *text) { XGlyphInfo ext; XftDraw *draw;@@ -111,20 +115,31 @@ XftDrawDestroy(draw);
} /* enter the event loop */ -static void run(GC gc, Pixmap pix) -{ - XEvent ev; +// static void run(GC gc, Pixmap pix) +// { +// XEvent ev; +// +// while (XNextEvent(dpy, &ev) == 0) { +// switch (e.type) { +// case Expose: +// printf("%d %d", e.xexpose.x, e.xexpose.y); +// XCopyArea(dpy, pix, e.xexpose.window, gc, e.xexpose.x, +// e.xexpose.y, e.xexpose.width, +// e.xexpose.height, e.xexpose.x, +// e.xexpose.y); +// break; +// } +// } - while (XNextEvent(dpy, &ev) == 0) { - switch (ev.type) { - case Expose: - XCopyArea(dpy, pix, ev.xexpose.window, gc, ev.xexpose.x, - ev.xexpose.y, ev.xexpose.width, - ev.xexpose.height, ev.xexpose.x, - ev.xexpose.y); - break; - } - } +// } + +void draw_bar(Window win, GC gc, Pixmap pix, XftFont *font, XftColor *color, + const char *text, XExposeEvent xexpose) +{ + draw_text(pix, font, color, text); + XMapWindow(dpy, win); + XCopyArea(dpy, pix, win, gc, xexpose.x, xexpose.y, xexpose.width, + xexpose.height, xexpose.x, xexpose.y); } int main(int argc, char *argv[])@@ -134,11 +149,22 @@ XftFont *font;
Pixmap pix; Window win; GC gc; + XExposeEvent xexpose; - if (argc != 2) { - fprintf(stderr, "usage: %s STRING\n", argv[0]); - return 1; - } + /* read line from stdin */ + char buffer[BUFSIZ]; + int reading = 1; + int flags; /* stdin flags */ + + struct pollfd pfd; + pfd.fd = STDIN_FILENO; + pfd.events = POLLIN; + + /* make stdin non-blocking */ + if ((flags = fcntl(STDIN_FILENO, F_GETFL)) == -1) + errx(1, "cannot get status flags"); + if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) == -1) + errx(1, "cannot set flags on stdin"); if ((dpy = XOpenDisplay(NULL)) == NULL) errx(1, "could not open display");@@ -156,9 +182,31 @@ xft_alloc_color(fg, &color);
init_atoms(win); - draw(pix, font, &color, argv[1]); - XMapWindow(dpy, win); - run(gc, pix); + /* wait for expose event to draw our bar */ + while (1) { + XEvent e; + XNextEvent(dpy, &e); + if (e.type == Expose) { + xexpose = e.xexpose; + break; + } + } + + while (reading) { + if (pfd.revents & POLLHUP) { + pfd.fd = -1; + reading = 0; + } + if (poll(&pfd, 1, -1)) { + if (pfd.revents & POLLIN) { + if (fgets(buffer, sizeof buffer, stdin) == NULL) + break; + + draw_bar(win, gc, pix, font, &color, buffer, + xexpose); + } + } + } /* housekeeping */ XUnmapWindow(dpy, win);