Simplified cursor resetting

This commit is contained in:
Bert 2011-09-03 17:01:39 +02:00
parent 9fa0bbca17
commit a7a849761f
4 changed files with 48 additions and 44 deletions

View file

@ -1,6 +1,6 @@
all: sxiv all: sxiv
VERSION = git-20110902 VERSION = git-20110903
CC = gcc CC = gcc
DESTDIR = DESTDIR =

View file

@ -33,7 +33,7 @@ void cleanup();
void remove_file(int, unsigned char); void remove_file(int, unsigned char);
void load_image(int); void load_image(int);
void redraw(); void redraw();
void hide_cursor(); void reset_cursor();
void animate(); void animate();
void set_timeout(timeout_f, int, int); void set_timeout(timeout_f, int, int);
void reset_timeout(timeout_f); void reset_timeout(timeout_f);
@ -56,8 +56,7 @@ int it_switch_mode(arg_t a) {
if (!tns.thumbs) if (!tns.thumbs)
tns_init(&tns, filecnt); tns_init(&tns, filecnt);
img_close(&img, 0); img_close(&img, 0);
win_set_cursor(&win, CURSOR_ARROW); reset_timeout(reset_cursor);
reset_timeout(hide_cursor);
tns.sel = fileidx; tns.sel = fileidx;
tns.dirty = 1; tns.dirty = 1;
mode = MODE_THUMB; mode = MODE_THUMB;
@ -81,11 +80,14 @@ int it_toggle_fullscreen(arg_t a) {
int it_reload_image(arg_t a) { int it_reload_image(arg_t a) {
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
load_image(fileidx); load_image(fileidx);
} else if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) { } else {
remove_file(tns.sel, 0); win_set_cursor(&win, CURSOR_WATCH);
tns.dirty = 1; if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) {
if (tns.sel >= tns.cnt) remove_file(tns.sel, 0);
tns.sel = tns.cnt - 1; tns.dirty = 1;
if (tns.sel >= tns.cnt)
tns.sel = tns.cnt - 1;
}
} }
return 1; return 1;
} }
@ -250,7 +252,7 @@ int i_drag(arg_t a) {
} }
win_set_cursor(&win, CURSOR_ARROW); win_set_cursor(&win, CURSOR_ARROW);
set_timeout(hide_cursor, TO_CURSOR_HIDE, 1); set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
reset_timeout(redraw); reset_timeout(redraw);
return 0; return 0;
@ -369,17 +371,17 @@ int it_shell_cmd(arg_t a) {
return 0; return 0;
} }
win_set_cursor(&win, CURSOR_WATCH);
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL); execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
warn("could not exec: /bin/sh"); warn("could not exec: /bin/sh");
exit(1); exit(1);
} else if (pid < 0) { } else if (pid < 0) {
warn("could not fork. command line was: %s", cmdline); warn("could not fork. command line was: %s", cmdline);
goto end; return 0;
} }
win_set_cursor(&win, CURSOR_WATCH);
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
warn("child exited with non-zero return value: %d. command line was: %s", warn("child exited with non-zero return value: %d. command line was: %s",
@ -398,10 +400,5 @@ int it_shell_cmd(arg_t a) {
tns.sel = tns.cnt - 1; tns.sel = tns.cnt - 1;
} }
end:
if (mode == MODE_THUMB)
win_set_cursor(&win, CURSOR_ARROW);
/* else: cursor gets reset in redraw() */
return 1; return 1;
} }

57
main.c
View file

@ -49,7 +49,7 @@ typedef struct {
/* timeout handler functions: */ /* timeout handler functions: */
void redraw(); void redraw();
void hide_cursor(); void reset_cursor();
void animate(); void animate();
appmode_t mode; appmode_t mode;
@ -65,7 +65,7 @@ char win_title[TITLE_LEN];
timeout_t timeouts[] = { timeout_t timeouts[] = {
{ { 0, 0 }, False, redraw }, { { 0, 0 }, False, redraw },
{ { 0, 0 }, False, hide_cursor }, { { 0, 0 }, False, reset_cursor },
{ { 0, 0 }, False, animate } { { 0, 0 }, False, animate }
}; };
@ -189,10 +189,9 @@ void load_image(int new) {
if (new < 0 || new >= filecnt) if (new < 0 || new >= filecnt)
return; return;
/* cursor gets reset in redraw() */
win_set_cursor(&win, CURSOR_WATCH); win_set_cursor(&win, CURSOR_WATCH);
img_close(&img, 0); img_close(&img, 0);
while (!img_load(&img, &files[new])) { while (!img_load(&img, &files[new])) {
remove_file(new, 0); remove_file(new, 0);
if (new >= filecnt) if (new >= filecnt)
@ -247,23 +246,34 @@ void update_title() {
} }
void redraw() { void redraw() {
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE)
img_render(&img, &win); img_render(&img, &win);
if (img.multi.animate) { else
win_set_cursor(&win, CURSOR_NONE);
} else {
win_set_cursor(&win, CURSOR_ARROW);
set_timeout(hide_cursor, TO_CURSOR_HIDE, 1);
}
} else {
tns_render(&tns, &win); tns_render(&tns, &win);
}
update_title(); update_title();
reset_timeout(redraw); reset_timeout(redraw);
reset_cursor();
} }
void hide_cursor() { void reset_cursor() {
win_set_cursor(&win, CURSOR_NONE); int i;
cursor_t cursor = CURSOR_NONE;
if (mode == MODE_IMAGE) {
for (i = 0; i < LEN(timeouts); i++) {
if (timeouts[i].handler == reset_cursor) {
if (timeouts[i].active)
cursor = CURSOR_ARROW;
break;
}
}
} else {
if (tns.cnt != filecnt)
cursor = CURSOR_WATCH;
else
cursor = CURSOR_ARROW;
}
win_set_cursor(&win, cursor);
} }
void animate() { void animate() {
@ -312,7 +322,7 @@ void on_buttonpress(XButtonEvent *bev) {
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
win_set_cursor(&win, CURSOR_ARROW); win_set_cursor(&win, CURSOR_ARROW);
set_timeout(hide_cursor, TO_CURSOR_HIDE, 1); set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
for (i = 0; i < LEN(buttons); i++) { for (i = 0; i < LEN(buttons); i++) {
if (buttons[i].button == bev->button && if (buttons[i].button == bev->button &&
@ -329,15 +339,15 @@ void on_buttonpress(XButtonEvent *bev) {
case Button1: case Button1:
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
if (sel == tns.sel) { if (sel == tns.sel) {
load_image(tns.sel);
mode = MODE_IMAGE; mode = MODE_IMAGE;
set_timeout(hide_cursor, TO_CURSOR_HIDE, 1); set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
load_image(tns.sel);
redraw();
} else { } else {
tns_highlight(&tns, &win, tns.sel, False); tns_highlight(&tns, &win, tns.sel, False);
tns_highlight(&tns, &win, sel, True); tns_highlight(&tns, &win, sel, True);
tns.sel = sel; tns.sel = sel;
} }
redraw();
break; break;
} }
break; break;
@ -363,18 +373,15 @@ void run() {
!XPending(win.env.dpy)) !XPending(win.env.dpy))
{ {
/* load thumbnails */ /* load thumbnails */
win_set_cursor(&win, CURSOR_WATCH);
set_timeout(redraw, TO_REDRAW_THUMBS, 0); set_timeout(redraw, TO_REDRAW_THUMBS, 0);
if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False)) if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False))
tns.cnt++; tns.cnt++;
else else
remove_file(tns.cnt, 0); remove_file(tns.cnt, 0);
if (tns.cnt == filecnt) { if (tns.cnt == filecnt)
redraw(); redraw();
win_set_cursor(&win, CURSOR_ARROW); else
} else {
check_timeouts(NULL); check_timeouts(NULL);
}
} }
while (!XPending(win.env.dpy) && check_timeouts(&timeout)) { while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
@ -410,7 +417,7 @@ void run() {
case MotionNotify: case MotionNotify:
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
win_set_cursor(&win, CURSOR_ARROW); win_set_cursor(&win, CURSOR_ARROW);
set_timeout(hide_cursor, TO_CURSOR_HIDE, 1); set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
} }
break; break;
} }

View file

@ -35,7 +35,7 @@ typedef struct {
enum { enum {
TO_REDRAW_RESIZE = 75, TO_REDRAW_RESIZE = 75,
TO_REDRAW_THUMBS = 200, TO_REDRAW_THUMBS = 200,
TO_CURSOR_HIDE = 1500 TO_CURSOR_HIDE = 1200
}; };
typedef void (*timeout_f)(void); typedef void (*timeout_f)(void);