Simplified cursor resetting

master
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
VERSION = git-20110902
VERSION = git-20110903
CC = gcc
DESTDIR =

View File

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

57
main.c
View File

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

View File

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