Krolyxon
Commit a7a8497

Branches

Refs →

nsxiva7a8497

Simplified cursor resetting

Author
Bert
Date
2011-09-03 17:01 +0200
Parent
9fa0bbc
Commit
a7a849761fed7f829da3874ef6a5b5097a7b9e6a

+48 −44

4 files changed

  1. M Makefile +1 −1
  2. M commands.c +14 −17
  3. M main.c +32 −25
  4. M types.h +1 −1
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION = git-20110902
3+VERSION = git-20110903
44
55 CC = gcc
66 DESTDIR =
M commands.c View
@@ -33,7 +33,7 @@ void cleanup();
3333 void remove_file(int, unsigned char);
3434 void load_image(int);
3535 void redraw();
36−void hide_cursor();
36+void reset_cursor();
3737 void animate();
3838 void set_timeout(timeout_f, int, int);
3939 void reset_timeout(timeout_f);
@@ -56,8 +56,7 @@ int it_switch_mode(arg_t a) {
5656 if (!tns.thumbs)
5757 tns_init(&tns, filecnt);
5858 img_close(&img, 0);
59− win_set_cursor(&win, CURSOR_ARROW);
60− reset_timeout(hide_cursor);
59+ reset_timeout(reset_cursor);
6160 tns.sel = fileidx;
6261 tns.dirty = 1;
6362 mode = MODE_THUMB;
@@ -81,11 +80,14 @@ int it_toggle_fullscreen(arg_t a) {
8180 int it_reload_image(arg_t a) {
8281 if (mode == MODE_IMAGE) {
8382 load_image(fileidx);
84− } else if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) {
85− remove_file(tns.sel, 0);
86− tns.dirty = 1;
87− if (tns.sel >= tns.cnt)
88− tns.sel = tns.cnt - 1;
83+ } else {
84+ win_set_cursor(&win, CURSOR_WATCH);
85+ if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) {
86+ remove_file(tns.sel, 0);
87+ tns.dirty = 1;
88+ if (tns.sel >= tns.cnt)
89+ tns.sel = tns.cnt - 1;
90+ }
8991 }
9092 return 1;
9193 }
@@ -250,7 +252,7 @@ int i_drag(arg_t a) {
250252 }
251253
252254 win_set_cursor(&win, CURSOR_ARROW);
253− set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
255+ set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
254256 reset_timeout(redraw);
255257
256258 return 0;
@@ -369,17 +371,17 @@ int it_shell_cmd(arg_t a) {
369371 return 0;
370372 }
371373
372− win_set_cursor(&win, CURSOR_WATCH);
373−
374374 if ((pid = fork()) == 0) {
375375 execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
376376 warn("could not exec: /bin/sh");
377377 exit(1);
378378 } else if (pid < 0) {
379379 warn("could not fork. command line was: %s", cmdline);
380− goto end;
380+ return 0;
381381 }
382382
383+ win_set_cursor(&win, CURSOR_WATCH);
384+
383385 waitpid(pid, &status, 0);
384386 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
385387 warn("child exited with non-zero return value: %d. command line was: %s",
@@ -398,10 +400,5 @@ int it_shell_cmd(arg_t a) {
398400 tns.sel = tns.cnt - 1;
399401 }
400402
401−end:
402− if (mode == MODE_THUMB)
403− win_set_cursor(&win, CURSOR_ARROW);
404− /* else: cursor gets reset in redraw() */
405−
406403 return 1;
407404 }
M main.c View
@@ -49,7 +49,7 @@ typedef struct {
4949
5050 /* timeout handler functions: */
5151 void redraw();
52−void hide_cursor();
52+void reset_cursor();
5353 void animate();
5454
5555 appmode_t mode;
@@ -65,7 +65,7 @@ char win_title[TITLE_LEN];
6565
6666 timeout_t timeouts[] = {
6767 { { 0, 0 }, False, redraw },
68− { { 0, 0 }, False, hide_cursor },
68+ { { 0, 0 }, False, reset_cursor },
6969 { { 0, 0 }, False, animate }
7070 };
7171
@@ -189,10 +189,9 @@ void load_image(int new) {
189189 if (new < 0 || new >= filecnt)
190190 return;
191191
192− /* cursor gets reset in redraw() */
193192 win_set_cursor(&win, CURSOR_WATCH);
193+
194194 img_close(&img, 0);
195−
196195 while (!img_load(&img, &files[new])) {
197196 remove_file(new, 0);
198197 if (new >= filecnt)
@@ -247,23 +246,34 @@ void update_title() {
247246 }
248247
249248 void redraw() {
250− if (mode == MODE_IMAGE) {
249+ if (mode == MODE_IMAGE)
251250 img_render(&img, &win);
252− if (img.multi.animate) {
253− win_set_cursor(&win, CURSOR_NONE);
254− } else {
255− win_set_cursor(&win, CURSOR_ARROW);
256− set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
257− }
258− } else {
251+ else
259252 tns_render(&tns, &win);
260− }
261253 update_title();
262254 reset_timeout(redraw);
255+ reset_cursor();
263256 }
264257
265−void hide_cursor() {
266− win_set_cursor(&win, CURSOR_NONE);
258+void reset_cursor() {
259+ int i;
260+ cursor_t cursor = CURSOR_NONE;
261+
262+ if (mode == MODE_IMAGE) {
263+ for (i = 0; i < LEN(timeouts); i++) {
264+ if (timeouts[i].handler == reset_cursor) {
265+ if (timeouts[i].active)
266+ cursor = CURSOR_ARROW;
267+ break;
268+ }
269+ }
270+ } else {
271+ if (tns.cnt != filecnt)
272+ cursor = CURSOR_WATCH;
273+ else
274+ cursor = CURSOR_ARROW;
275+ }
276+ win_set_cursor(&win, cursor);
267277 }
268278
269279 void animate() {
@@ -312,7 +322,7 @@ void on_buttonpress(XButtonEvent *bev) {
312322
313323 if (mode == MODE_IMAGE) {
314324 win_set_cursor(&win, CURSOR_ARROW);
315− set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
325+ set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
316326
317327 for (i = 0; i < LEN(buttons); i++) {
318328 if (buttons[i].button == bev->button &&
@@ -329,15 +339,15 @@ void on_buttonpress(XButtonEvent *bev) {
329339 case Button1:
330340 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
331341 if (sel == tns.sel) {
332− load_image(tns.sel);
333342 mode = MODE_IMAGE;
334− set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
343+ set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
344+ load_image(tns.sel);
345+ redraw();
335346 } else {
336347 tns_highlight(&tns, &win, tns.sel, False);
337348 tns_highlight(&tns, &win, sel, True);
338349 tns.sel = sel;
339350 }
340− redraw();
341351 break;
342352 }
343353 break;
@@ -363,18 +373,15 @@ void run() {
363373 !XPending(win.env.dpy))
364374 {
365375 /* load thumbnails */
366− win_set_cursor(&win, CURSOR_WATCH);
367376 set_timeout(redraw, TO_REDRAW_THUMBS, 0);
368377 if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
369378 tns.cnt++;
370379 else
371380 remove_file(tns.cnt, 0);
372− if (tns.cnt == filecnt) {
381+ if (tns.cnt == filecnt)
373382 redraw();
374− win_set_cursor(&win, CURSOR_ARROW);
375− } else {
383+ else
376384 check_timeouts(NULL);
377− }
378385 }
379386
380387 while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
@@ -410,7 +417,7 @@ void run() {
410417 case MotionNotify:
411418 if (mode == MODE_IMAGE) {
412419 win_set_cursor(&win, CURSOR_ARROW);
413− set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
420+ set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
414421 }
415422 break;
416423 }
M types.h View
@@ -35,7 +35,7 @@ typedef struct {
3535 enum {
3636 TO_REDRAW_RESIZE = 75,
3737 TO_REDRAW_THUMBS = 200,
38− TO_CURSOR_HIDE = 1500
38+ TO_CURSOR_HIDE = 1200
3939 };
4040
4141 typedef void (*timeout_f)(void);