Krolyxon
Commit 447bc1c

Branches

Refs →

nsxiv447bc1c

Merge branch 'asyncinfo'

Author
Bert Münnich
Date
2013-03-19 20:40 +0100
Committer
Bert Münnich · 2013-03-19 21:14 +0100
Parents
9ee3447 825c52c
Commit
447bc1c784cc358078f4ffbbac0c63ee74a069c8

+158 −93

5 files changed

  1. M commands.c +6 −2
  2. M image-info +4 −2
  3. M main.c +106 −55
  4. M window.c +33 −31
  5. M window.h +9 −3
M commands.c View
@@ -33,6 +33,7 @@
3333 void cleanup(void);
3434 void remove_file(int, bool);
3535 void load_image(int);
36+void open_info(void);
3637 void redraw(void);
3738 void reset_cursor(void);
3839 void animate(void);
@@ -92,10 +93,13 @@ bool it_toggle_fullscreen(arg_t a)
9293 bool it_toggle_bar(arg_t a)
9394 {
9495 win_toggle_bar(&win);
95− if (mode == MODE_IMAGE)
96+ if (mode == MODE_IMAGE) {
9697 img.checkpan = img.dirty = true;
97− else
98+ if (win.bar.h > 0)
99+ open_info();
100+ } else {
98101 tns.dirty = true;
102+ }
99103 return true;
100104 }
101105
M image-info View
@@ -5,13 +5,15 @@
55 # with the name of the image file as its first argument.
66 # The output is displayed in sxiv's status bar.
77
8+s=" | " # field separator
9+
810 filename=$(basename "$1")
911 filesize=$(du -h "$1" | cut -f 1)
1012
1113 geometry=$(identify -format '%wx%h' "$1[0]")
1214
1315 tags=$(exiv2 -q pr -pi "$1" | awk '$1~"Keywords" { printf("%s,", $4); }')
14−tags=${tags:+|}${tags%,}
16+tags=${tags%,}
1517
16−echo "[$filesize|$geometry$tags] $filename"
18+echo "${filesize}${s}${geometry}${tags:+$s}${tags}${s}${filename}"
1719
M main.c View
@@ -22,10 +22,14 @@
2222 #include <stdlib.h>
2323 #include <stdio.h>
2424 #include <string.h>
25+#include <fcntl.h>
2526 #include <unistd.h>
27+#include <errno.h>
28+#include <signal.h>
2629 #include <sys/select.h>
2730 #include <sys/stat.h>
2831 #include <sys/time.h>
32+#include <sys/wait.h>
2933 #include <X11/keysym.h>
3034
3135 #include "types.h"
@@ -38,8 +42,6 @@
3842 #include "config.h"
3943
4044 enum {
41− BAR_L_LEN = 512,
42− BAR_R_LEN = 64,
4345 FILENAME_CNT = 1024,
4446 TITLE_LEN = 256
4547 };
@@ -70,12 +72,12 @@ int prefix;
7072 bool resized = false;
7173
7274 const char * const INFO_SCRIPT = ".sxiv/exec/image-info";
73−char *info_script;
74−
7575 struct {
76− char l[BAR_L_LEN];
77− char r[BAR_R_LEN];
78−} bar;
76+ char *script;
77+ int fd;
78+ unsigned int i, lastsep;
79+ bool open;
80+} info;
7981
8082 timeout_t timeouts[] = {
8183 { { 0, 0 }, false, redraw },
@@ -212,35 +214,69 @@ bool check_timeouts(struct timeval *t)
212214 return tmin > 0;
213215 }
214216
217+void open_info(void)
218+{
219+ static pid_t pid;
220+ int pfd[2];
221+
222+ if (info.script == NULL || info.open || win.bar.h == 0)
223+ return;
224+ if (info.fd != -1) {
225+ close(info.fd);
226+ kill(pid, SIGTERM);
227+ info.fd = -1;
228+ }
229+ win.bar.l[0] = '\0';
230+
231+ if (pipe(pfd) < 0)
232+ return;
233+ pid = fork();
234+ if (pid > 0) {
235+ close(pfd[1]);
236+ fcntl(pfd[0], F_SETFL, O_NONBLOCK);
237+ info.fd = pfd[0];
238+ info.i = info.lastsep = 0;
239+ info.open = true;
240+ } else if (pid == 0) {
241+ close(pfd[0]);
242+ dup2(pfd[1], 1);
243+ execl(info.script, info.script, files[fileidx].name, NULL);
244+ warn("could not exec: %s", info.script);
245+ exit(EXIT_FAILURE);
246+ }
247+}
248+
215249 void read_info(void)
216250 {
217− char cmd[4096];
218− FILE *outp;
219− int c, i = 0, n = sizeof(bar.l) - 1;
220− bool lastsep = false;
221−
222− if (info_script != NULL) {
223− snprintf(cmd, sizeof(cmd), "%s \"%s\"", info_script, files[fileidx].name);
224− outp = popen(cmd, "r");
225− if (outp == NULL)
251+ ssize_t i, n;
252+ char buf[BAR_L_LEN];
253+
254+ while (true) {
255+ n = read(info.fd, buf, sizeof(buf));
256+ if (n < 0 && errno == EAGAIN)
257+ return;
258+ else if (n == 0)
226259 goto end;
227− while (i < n && (c = fgetc(outp)) != EOF) {
228− if (c == '\n') {
229− if (!lastsep) {
230− bar.l[i++] = ' ';
231− lastsep = true;
260+ for (i = 0; i < n; i++) {
261+ if (buf[i] == '\n') {
262+ if (info.lastsep == 0) {
263+ win.bar.l[info.i++] = ' ';
264+ info.lastsep = 1;
232265 }
233266 } else {
234− bar.l[i++] = c;
235− lastsep = false;
267+ win.bar.l[info.i++] = buf[i];
268+ info.lastsep = 0;
236269 }
270+ if (info.i + 1 == sizeof(win.bar.l))
271+ goto end;
237272 }
238− pclose(outp);
239273 }
240274 end:
241− if (lastsep)
242− i--;
243− bar.l[i] = '\0';
275+ info.i -= info.lastsep;
276+ win.bar.l[info.i] = '\0';
277+ win_update_bar(&win);
278+ info.fd = -1;
279+ while (waitpid(-1, NULL, WNOHANG) > 0);
244280 }
245281
246282 void load_image(int new)
@@ -261,7 +297,8 @@ void load_image(int new)
261297 alternate = fileidx;
262298 fileidx = new;
263299
264− read_info();
300+ info.open = false;
301+ open_info();
265302
266303 if (img.multi.cnt > 0 && img.multi.animate)
267304 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
@@ -271,51 +308,56 @@ void load_image(int new)
271308
272309 void update_info(void)
273310 {
274− unsigned int i, fn, fw, n, len = sizeof(bar.r);
275311 int sel;
276− char *t = bar.r, title[TITLE_LEN];
312+ unsigned int i, fn, fw, n;
313+ unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
314+ char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
277315 bool ow_info;
278316
279317 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
280318 sel = mode == MODE_IMAGE ? fileidx : tns.sel;
281319
320+ /* update window title */
282321 if (mode == MODE_THUMB) {
283322 win_set_title(&win, "sxiv");
323+ } else {
324+ snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
325+ win_set_title(&win, title);
326+ }
284327
328+ /* update bar contents */
329+ if (win.bar.h == 0)
330+ return;
331+ if (mode == MODE_THUMB) {
285332 if (tns.cnt == filecnt) {
286− n = snprintf(t, len, "%0*d/%d", fw, sel + 1, filecnt);
333+ n = snprintf(rt, rlen, "%0*d/%d", fw, sel + 1, filecnt);
287334 ow_info = true;
288335 } else {
289− snprintf(bar.l, sizeof(bar.l), "Loading... %0*d/%d",
290− fw, tns.cnt, filecnt);
291− bar.r[0] = '\0';
336+ snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.cnt, filecnt);
337+ rt[0] = '\0';
292338 ow_info = false;
293339 }
294340 } else {
295− snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
296− win_set_title(&win, title);
297−
298− n = snprintf(t, len, "%3d%% ", (int) (img.zoom * 100.0));
341+ n = snprintf(rt, rlen, "%3d%% | ", (int) (img.zoom * 100.0));
299342 if (img.multi.cnt > 0) {
300343 for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
301− n += snprintf(t + n, len - n, "(%0*d/%d) ",
344+ n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
302345 fn, img.multi.sel + 1, img.multi.cnt);
303346 }
304− n += snprintf(t + n, len - n, "%0*d/%d", fw, sel + 1, filecnt);
305− ow_info = bar.l[0] == '\0';
347+ n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
348+ ow_info = info.script == NULL;
306349 }
307350 if (ow_info) {
308351 fn = strlen(files[sel].name);
309− if (fn < sizeof(bar.l) &&
352+ if (fn < llen &&
310353 win_textwidth(files[sel].name, fn, true) +
311− win_textwidth(bar.r, n, true) < win.w)
354+ win_textwidth(rt, n, true) < win.w)
312355 {
313− strncpy(bar.l, files[sel].name, sizeof(bar.l));
356+ strncpy(lt, files[sel].name, llen);
314357 } else {
315− strncpy(bar.l, files[sel].base, sizeof(bar.l));
358+ strncpy(lt, files[sel].base, llen);
316359 }
317360 }
318− win_set_bar_info(&win, bar.l, bar.r);
319361 }
320362
321363 void redraw(void)
@@ -458,8 +500,8 @@ void run(void)
458500 int xfd;
459501 fd_set fds;
460502 struct timeval timeout;
503+ bool discard, to_set;
461504 XEvent ev, nextev;
462− bool discard;
463505
464506 redraw();
465507
@@ -482,12 +524,20 @@ void run(void)
482524 check_timeouts(NULL);
483525 }
484526
485− while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
486− /* wait for timeouts */
527+ while (XPending(win.env.dpy) == 0
528+ && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
529+ {
530+ /* check for timeouts & input */
487531 xfd = ConnectionNumber(win.env.dpy);
488532 FD_ZERO(&fds);
489533 FD_SET(xfd, &fds);
490− select(xfd + 1, &fds, 0, 0, &timeout);
534+ if (info.fd != -1) {
535+ FD_SET(info.fd, &fds);
536+ xfd = MAX(xfd, info.fd);
537+ }
538+ select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
539+ if (FD_ISSET(info.fd, &fds))
540+ read_info();
491541 }
492542
493543 do {
@@ -641,13 +691,14 @@ int main(int argc, char **argv)
641691 warn("could not locate home directory");
642692 } else {
643693 len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
644− info_script = (char*) s_malloc(len);
645− snprintf(info_script, len, "%s/%s", homedir, INFO_SCRIPT);
646− if (access(info_script, X_OK) != 0) {
647− free(info_script);
648− info_script = NULL;
694+ info.script = (char*) s_malloc(len);
695+ snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT);
696+ if (access(info.script, X_OK) != 0) {
697+ free(info.script);
698+ info.script = NULL;
649699 }
650700 }
701+ info.fd = -1;
651702
652703 if (options->thumb_mode) {
653704 mode = MODE_THUMB;
M window.c View
@@ -119,12 +119,15 @@ void win_init(win_t *win)
119119 e->cmap = DefaultColormap(e->dpy, e->scr);
120120 e->depth = DefaultDepth(e->dpy, e->scr);
121121
122+ win_init_font(e->dpy, BAR_FONT);
123+
122124 win->white = WhitePixel(e->dpy, e->scr);
123125 win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
124126 win->fscol = win_alloc_color(win, WIN_FS_COLOR);
125127 win->selcol = win_alloc_color(win, SEL_COLOR);
126128 win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
127129 win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
130+ win->bar.h = options->hide_bar ? 0 : barheight;
128131
129132 win->sizehints.flags = PWinGravity;
130133 win->sizehints.win_gravity = NorthWestGravity;
@@ -135,8 +138,6 @@ void win_init(win_t *win)
135138 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
136139 warn("no locale support");
137140
138− win_init_font(e->dpy, BAR_FONT);
139−
140141 wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
141142 }
142143
@@ -414,31 +415,28 @@ void win_draw_bar(win_t *win)
414415 XSetForeground(e->dpy, gc, win->bar.fgcol);
415416 XSetBackground(e->dpy, gc, win->bar.bgcol);
416417
417− if (win->bar.r != NULL) {
418− len = strlen(win->bar.r);
419− if (len > 0) {
420− if ((tw = win_textwidth(win->bar.r, len, true)) > w)
421− return;
422− x = win->w - tw + H_TEXT_PAD;
423− w -= tw;
424− if (font.set)
425− XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
426− else
427− XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
428− }
418+ if ((len = strlen(win->bar.r)) > 0) {
419+ if ((tw = win_textwidth(win->bar.r, len, true)) > w)
420+ return;
421+ x = win->w - tw + H_TEXT_PAD;
422+ w -= tw;
423+ if (font.set)
424+ XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
425+ else
426+ XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
429427 }
430− if (win->bar.l != NULL) {
431− olen = len = strlen(win->bar.l);
428+ if ((len = strlen(win->bar.l)) > 0) {
429+ olen = len;
432430 while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
433431 len--;
434432 if (len > 0) {
435− if (len != olen) {
436− w = strlen(dots);
437− if (len <= w)
438− return;
439− memcpy(rest, win->bar.l + len - w, w);
440− memcpy(win->bar.l + len - w, dots, w);
441− }
433+ if (len != olen) {
434+ w = strlen(dots);
435+ if (len <= w)
436+ return;
437+ memcpy(rest, win->bar.l + len - w, w);
438+ memcpy(win->bar.l + len - w, dots, w);
439+ }
442440 x = H_TEXT_PAD;
443441 if (font.set)
444442 XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
@@ -480,6 +478,18 @@ void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
480478 XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
481479 }
482480
481+void win_update_bar(win_t *win)
482+{
483+ if (win == NULL || win->xwin == None || win->pm == None)
484+ return;
485+
486+ if (win->bar.h > 0) {
487+ win_draw_bar(win);
488+ XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
489+ 0, win->h, win->w, win->bar.h, 0, win->h);
490+ }
491+}
492+
483493 int win_textwidth(const char *text, unsigned int len, bool with_padding)
484494 {
485495 XRectangle r;
@@ -514,14 +524,6 @@ void win_set_title(win_t *win, const char *title)
514524 PropModeReplace, (unsigned char *) title, strlen(title));
515525 }
516526
517−void win_set_bar_info(win_t *win, char *linfo, char *rinfo)
518−{
519− if (win != NULL) {
520− win->bar.l = linfo;
521− win->bar.r = rinfo;
522− }
523−}
524−
525527 void win_set_cursor(win_t *win, cursor_t cursor)
526528 {
527529 if (win == NULL || win->xwin == None)
M window.h View
@@ -24,6 +24,11 @@
2424
2525 #include "types.h"
2626
27+enum {
28+ BAR_L_LEN = 512,
29+ BAR_R_LEN = 64
30+};
31+
2732 typedef struct {
2833 Display *dpy;
2934 int scr;
@@ -55,8 +60,8 @@ typedef struct {
5560
5661 struct {
5762 unsigned int h;
58− char *l;
59− char *r;
63+ char l[BAR_L_LEN];
64+ char r[BAR_R_LEN];
6065 unsigned long bgcol;
6166 unsigned long fgcol;
6267 } bar;
@@ -80,10 +85,11 @@ void win_draw(win_t*);
8085 void win_draw_rect(win_t*, Pixmap, int, int, int, int, bool, int,
8186 unsigned long);
8287
88+void win_update_bar(win_t*);
89+
8390 int win_textwidth(const char*, unsigned int, bool);
8491
8592 void win_set_title(win_t*, const char*);
86−void win_set_bar_info(win_t*, char*, char*);
8793 void win_set_cursor(win_t*, cursor_t);
8894
8995 #endif /* WINDOW_H */