all repos — barito @ 1339acc11caca25ef066f8149093fd6be6cfca66

an x11 bar

Refactor to update from stdin
Anirudh Oppiliappan x@icyphox.sh
Sun, 16 Jan 2022 11:35:35 +0530
commit

1339acc11caca25ef066f8149093fd6be6cfca66

parent

71126fd27598d983afa58672a599e6b21115856e

1 files changed, 38 insertions(+), 51 deletions(-)

jump to
M barito.cbarito.c

@@ -17,6 +17,7 @@ static Window root;

static Colormap colormap; static int depth; static int screen; +static int xfd; static unsigned long alloc_color(const char *s) {

@@ -36,9 +37,9 @@ swa.background_pixel = alloc_color(bg);

swa.override_redirect = 1; swa.event_mask = ExposureMask; win = XCreateWindow(dpy, root, x, y, w, h, 0, CopyFromParent, - CopyFromParent, CopyFromParent, - CWBackPixel | CWOverrideRedirect | CWEventMask, - &swa); + CopyFromParent, CopyFromParent, + CWBackPixel | CWOverrideRedirect | CWEventMask, + &swa); return win; }

@@ -94,7 +95,7 @@ }

/* draw text on pixmap */ static void draw_text(Drawable pix, XftFont *font, XftColor *color, - const char *text) + const char *text) { XGlyphInfo ext; XftDraw *draw;

@@ -114,34 +115,6 @@

XftDrawDestroy(draw); } -/* enter the event loop */ -// 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; -// } -// } - -// } - -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[]) { XftColor color;

@@ -156,9 +129,6 @@ 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)

@@ -179,35 +149,52 @@ gc = XCreateGC(dpy, root, 0, NULL);

pix = create_pixmap(gc, WIDTH, HEIGHT); font = XftFontOpenName(dpy, screen, fontname); xft_alloc_color(fg, &color); + xfd = XConnectionNumber(dpy); init_atoms(win); - /* wait for expose event to draw our bar */ - while (1) { - XEvent e; - XNextEvent(dpy, &e); - if (e.type == Expose) { - xexpose = e.xexpose; - break; - } - } + struct pollfd pfd[2]; + pfd[0].fd = STDIN_FILENO; + pfd[1].fd = xfd; + pfd[0].events = pfd[1].events = POLLIN; + + int redraw = 0; + XMapWindow(dpy, win); while (reading) { - if (pfd.revents & POLLHUP) { - pfd.fd = -1; + if (pfd[0].revents & POLLHUP) { + pfd[0].fd = -1; reading = 0; } - if (poll(&pfd, 1, -1)) { - if (pfd.revents & POLLIN) { + if (poll(pfd, 2, -1)) { + if (pfd[0].revents & POLLIN) { if (fgets(buffer, sizeof buffer, stdin) == NULL) break; + + if (redraw) { + XFreePixmap(dpy, pix); + pix = create_pixmap(gc, WIDTH, HEIGHT); + } - draw_bar(win, gc, pix, font, &color, buffer, - xexpose); + draw_text(pix, font, &color, buffer); + XClearArea(dpy, win, 0, 0, WIDTH, HEIGHT, 1); + XFlush(dpy); + } + if (pfd[1].revents & POLLIN) { + XEvent ev; + while (XPending(dpy) && !XNextEvent(dpy, &ev)) { + if (ev.type == Expose) { + xexpose = ev.xexpose; + XCopyArea(dpy, pix, win, gc, xexpose.x, xexpose.y, xexpose.width, + xexpose.height, xexpose.x, xexpose.y); + redraw = 1; + break; + } + } } } + XFlush(dpy); } - /* housekeeping */ XUnmapWindow(dpy, win); XDestroyWindow(dpy, win);