From f3298400e6704844aeb1d3e0951e84b4236d2302 Mon Sep 17 00:00:00 2001 From: Bert Münnich Date: Mon, 11 Feb 2013 23:05:26 +0100 Subject: Spawn and read from info script without blocking --- window.c | 59 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'window.c') diff --git a/window.c b/window.c index 0b1ce65..a51499e 100644 --- a/window.c +++ b/window.c @@ -414,31 +414,28 @@ void win_draw_bar(win_t *win) XSetForeground(e->dpy, gc, win->bar.fgcol); XSetBackground(e->dpy, gc, win->bar.bgcol); - if (win->bar.r != NULL) { - len = strlen(win->bar.r); - if (len > 0) { - if ((tw = win_textwidth(win->bar.r, len, true)) > w) - return; - x = win->w - tw + H_TEXT_PAD; - w -= tw; - if (font.set) - XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len); - else - XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len); - } + if ((len = strlen(win->bar.r)) > 0) { + if ((tw = win_textwidth(win->bar.r, len, true)) > w) + return; + x = win->w - tw + H_TEXT_PAD; + w -= tw; + if (font.set) + XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len); + else + XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len); } - if (win->bar.l != NULL) { - olen = len = strlen(win->bar.l); + if ((len = strlen(win->bar.l)) > 0) { + olen = len; while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w) len--; if (len > 0) { - if (len != olen) { - w = strlen(dots); - if (len <= w) - return; - memcpy(rest, win->bar.l + len - w, w); - memcpy(win->bar.l + len - w, dots, w); - } + if (len != olen) { + w = strlen(dots); + if (len <= w) + return; + memcpy(rest, win->bar.l + len - w, w); + memcpy(win->bar.l + len - w, dots, w); + } x = H_TEXT_PAD; if (font.set) XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len); @@ -480,6 +477,18 @@ void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h, XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h); } +void win_update_bar(win_t *win) +{ + if (win == NULL || win->xwin == None || win->pm == None) + return; + + if (win->bar.h > 0) { + win_draw_bar(win); + XCopyArea(win->env.dpy, win->pm, win->xwin, gc, + 0, win->h, win->w, win->bar.h, 0, win->h); + } +} + int win_textwidth(const char *text, unsigned int len, bool with_padding) { XRectangle r; @@ -514,14 +523,6 @@ void win_set_title(win_t *win, const char *title) PropModeReplace, (unsigned char *) title, strlen(title)); } -void win_set_bar_info(win_t *win, char *linfo, char *rinfo) -{ - if (win != NULL) { - win->bar.l = linfo; - win->bar.r = rinfo; - } -} - void win_set_cursor(win_t *win, cursor_t cursor) { if (win == NULL || win->xwin == None) -- cgit v1.2.3-54-g00ecf From 30802cec0f233aa9977256684cb749df6c7e28c0 Mon Sep 17 00:00:00 2001 From: Bert Münnich Date: Tue, 12 Feb 2013 17:55:47 +0100 Subject: Spawn info script & update bar contents only when needed --- commands.c | 8 ++++++-- main.c | 24 +++++++++++++++++------- window.c | 5 +++-- 3 files changed, 26 insertions(+), 11 deletions(-) (limited to 'window.c') diff --git a/commands.c b/commands.c index f358ed3..3e108a4 100644 --- a/commands.c +++ b/commands.c @@ -33,6 +33,7 @@ void cleanup(void); void remove_file(int, bool); void load_image(int); +void open_info(void); void redraw(void); void reset_cursor(void); void animate(void); @@ -92,10 +93,13 @@ bool it_toggle_fullscreen(arg_t a) bool it_toggle_bar(arg_t a) { win_toggle_bar(&win); - if (mode == MODE_IMAGE) + if (mode == MODE_IMAGE) { img.checkpan = img.dirty = true; - else + if (win.bar.h > 0) + open_info(); + } else { tns.dirty = true; + } return true; } diff --git a/main.c b/main.c index f648dab..d6cdcd4 100644 --- a/main.c +++ b/main.c @@ -76,6 +76,7 @@ struct { char *script; int fd; unsigned int i, lastsep; + bool open; } info; timeout_t timeouts[] = { @@ -218,23 +219,25 @@ void open_info(void) static pid_t pid; int pfd[2]; - win.bar.l[0] = '\0'; - + if (info.script == NULL || info.open || win.bar.h == 0) + return; if (info.fd != -1) { close(info.fd); kill(pid, SIGTERM); while (waitpid(-1, NULL, WNOHANG) > 0); info.fd = -1; } - if (info.script == NULL || pipe(pfd) < 0) - return; + win.bar.l[0] = '\0'; + if (pipe(pfd) < 0) + return; pid = fork(); if (pid > 0) { close(pfd[1]); fcntl(pfd[0], F_SETFL, O_NONBLOCK); info.fd = pfd[0]; info.i = info.lastsep = 0; + info.open = true; } else if (pid == 0) { close(pfd[0]); dup2(pfd[1], 1); @@ -293,6 +296,7 @@ void load_image(int new) alternate = fileidx; fileidx = new; + info.open = false; open_info(); if (img.multi.cnt > 0 && img.multi.animate) @@ -312,9 +316,18 @@ void update_info(void) for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); sel = mode == MODE_IMAGE ? fileidx : tns.sel; + /* update window title */ if (mode == MODE_THUMB) { win_set_title(&win, "sxiv"); + } else { + snprintf(title, sizeof(title), "sxiv - %s", files[sel].name); + win_set_title(&win, title); + } + /* update bar contents */ + if (win.bar.h == 0) + return; + if (mode == MODE_THUMB) { if (tns.cnt == filecnt) { n = snprintf(rt, rlen, "%0*d/%d", fw, sel + 1, filecnt); ow_info = true; @@ -324,9 +337,6 @@ void update_info(void) ow_info = false; } } else { - snprintf(title, sizeof(title), "sxiv - %s", files[sel].name); - win_set_title(&win, title); - n = snprintf(rt, rlen, "%3d%% ", (int) (img.zoom * 100.0)); if (img.multi.cnt > 0) { for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10); diff --git a/window.c b/window.c index a51499e..30081de 100644 --- a/window.c +++ b/window.c @@ -119,12 +119,15 @@ void win_init(win_t *win) e->cmap = DefaultColormap(e->dpy, e->scr); e->depth = DefaultDepth(e->dpy, e->scr); + win_init_font(e->dpy, BAR_FONT); + win->white = WhitePixel(e->dpy, e->scr); win->bgcol = win_alloc_color(win, WIN_BG_COLOR); win->fscol = win_alloc_color(win, WIN_FS_COLOR); win->selcol = win_alloc_color(win, SEL_COLOR); win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR); win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR); + win->bar.h = options->hide_bar ? 0 : barheight; win->sizehints.flags = PWinGravity; win->sizehints.win_gravity = NorthWestGravity; @@ -135,8 +138,6 @@ void win_init(win_t *win) if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0) warn("no locale support"); - win_init_font(e->dpy, BAR_FONT); - wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False); } -- cgit v1.2.3-54-g00ecf