From 5d0679b855bb42aaaf3435a3182ac5ddb4412e1f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bert=20M=C3=BCnnich?= <ber.t@posteo.de>
Date: Sun, 15 Jun 2014 14:15:48 +0200
Subject: [PATCH] Show key handler status in bar while it is running

---
 Makefile |  2 +-
 main.c   | 24 +++++++++++++++++-------
 window.c |  2 ++
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile
index 35a19ae..7a2d9e3 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-VERSION = git-20140610
+VERSION = git-20140615
 
 PREFIX    = /usr/local
 MANPREFIX = $(PREFIX)/share/man
diff --git a/main.c b/main.c
index c734edf..ee84719 100644
--- a/main.c
+++ b/main.c
@@ -465,7 +465,8 @@ void run_key_handler(const char *key, unsigned int mask)
 {
 	pid_t pid;
 	int retval, status, n = mode == MODE_IMAGE ? fileidx : tns.sel;
-	char kstr[32];
+	char kstr[32], oldbar[sizeof(win.bar.l)];
+	bool restore_bar = mode == MODE_IMAGE && info.cmd != NULL;
 	struct stat oldst, newst;
 
 	if (keyhandler.cmd == NULL) {
@@ -483,6 +484,11 @@ void run_key_handler(const char *key, unsigned int mask)
 	         mask & Mod1Mask    ? "M-" : "",
 	         mask & ShiftMask   ? "S-" : "", key);
 
+	if (restore_bar)
+		memcpy(oldbar, win.bar.l, sizeof(win.bar.l));
+	strncpy(win.bar.l, "Running key handler...", sizeof(win.bar.l));
+	win_update_bar(&win);
+	win_set_cursor(&win, CURSOR_WATCH);
 	stat(files[n].path, &oldst);
 
 	if ((pid = fork()) == 0) {
@@ -491,10 +497,8 @@ void run_key_handler(const char *key, unsigned int mask)
 		exit(EXIT_FAILURE);
 	} else if (pid < 0) {
 		warn("could not fork key handler");
-		return;
+		goto end;
 	}
-	win_set_cursor(&win, CURSOR_WATCH);
-
 	waitpid(pid, &status, 0);
 	retval = WEXITSTATUS(status);
 	if (WIFEXITED(status) == 0 || retval != 0)
@@ -504,10 +508,12 @@ void run_key_handler(const char *key, unsigned int mask)
 	    memcmp(&oldst.st_mtime, &newst.st_mtime, sizeof(oldst.st_mtime)) == 0)
 	{
 		/* file has not changed */
-		win_set_cursor(&win, CURSOR_ARROW);
-		set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
-		return;
+		goto end;
 	}
+	restore_bar = false;
+	strncpy(win.bar.l, "Reloading image...", sizeof(win.bar.l));
+	win_update_bar(&win);
+
 	if (mode == MODE_IMAGE) {
 		img_close(&img, true);
 		load_image(fileidx);
@@ -520,6 +526,10 @@ void run_key_handler(const char *key, unsigned int mask)
 		if (tns.sel >= tns.cnt)
 			tns.sel = tns.cnt - 1;
 	}
+end:
+	if (restore_bar)
+		memcpy(win.bar.l, oldbar, sizeof(win.bar.l));
+	set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
 	redraw();
 }
 
diff --git a/window.c b/window.c
index 3cf4379..a0a8d80 100644
--- a/window.c
+++ b/window.c
@@ -481,6 +481,7 @@ void win_draw(win_t *win)
 
 	XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
 	          0, 0, win->w, win->h + win->bar.h, 0, 0);
+	XFlush(win->env.dpy);
 }
 
 void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
@@ -510,6 +511,7 @@ void win_update_bar(win_t *win)
 		win_draw_bar(win);
 		XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
 		          0, win->h, win->w, win->bar.h, 0, win->h);
+		XFlush(win->env.dpy);
 	}
 }