Krolyxon
Commit 4cfb8a2

Branches

Refs →

nsxiv4cfb8a2

Correct timeout handling, more complex

Author
Bert
Date
2011-02-21 22:05 +0100
Parent
88b5c5f
Commit
4cfb8a204a2d56cb3b41681bf0fa3d68b89c6b31

+52 −41

1 file changed

  1. M main.c +52 −41
M main.c View
@@ -254,24 +254,23 @@ void read_dir_rec(const char *dirname) {
254254
255255 /* event handling */
256256
257−int timeout;
258−unsigned char hidecur;
257+#define TO_WIN_RESIZE 75000;
258+#define TO_IMAGE_DRAG 1000;
259+#define TO_CURSOR_HIDE 1500000;
260+#define TO_THUMBS_LOAD 75000;
261+int timo_cursor;
262+int timo_redraw;
259263
260−int mox, moy;
261264 unsigned char drag;
265+int mox, moy;
262266
263267 void redraw() {
264− if (mode == MODE_NORMAL) {
265− if (!drag && hidecur) {
266− win_set_cursor(&win, CURSOR_NONE);
267− hidecur = 0;
268− }
268+ if (mode == MODE_NORMAL)
269269 img_render(&img, &win);
270− } else {
270+ else
271271 tns_render(&tns, &win);
272− }
273272 update_title();
274− timeout = 0;
273+ timo_redraw = 0;
275274 }
276275
277276 void on_keypress(XKeyEvent *kev) {
@@ -391,7 +390,7 @@ void on_keypress(XKeyEvent *kev) {
391390 tns_init(&tns, filecnt);
392391 mode = MODE_THUMBS;
393392 win_set_cursor(&win, CURSOR_ARROW);
394− hidecur = 0;
393+ timo_cursor = 0;
395394 tns.sel = fileidx;
396395 changed = tns.dirty = 1;
397396 break;
@@ -489,7 +488,7 @@ void on_buttonpress(XButtonEvent *bev) {
489488 mox = bev->x;
490489 moy = bev->y;
491490 win_set_cursor(&win, CURSOR_HAND);
492− hidecur = 0;
491+ timo_cursor = 0;
493492 drag = 1;
494493 break;
495494 case Button3:
@@ -530,7 +529,7 @@ void on_buttonpress(XButtonEvent *bev) {
530529 fileidx = tns.sel;
531530 load_image();
532531 mode = MODE_NORMAL;
533− win_set_cursor(&win, CURSOR_NONE);
532+ timo_cursor = TO_CURSOR_HIDE;
534533 } else {
535534 tns_highlight(&tns, &win, tns.sel, False);
536535 tns_highlight(&tns, &win, sel, True);
@@ -559,7 +558,7 @@ void on_motionnotify(XMotionEvent *mev) {
559558
560559 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
561560 if (img_move(&img, &win, mev->x - mox, mev->y - moy))
562− timeout = 1000;
561+ timo_redraw = TO_IMAGE_DRAG;
563562
564563 mox = mev->x;
565564 moy = mev->y;
@@ -567,17 +566,16 @@ void on_motionnotify(XMotionEvent *mev) {
567566 }
568567
569568 void run() {
570− int xfd;
569+ int xfd, timeout;
571570 fd_set fds;
572− struct timeval t, t0;
571+ struct timeval tt, t0, t1;
573572 XEvent ev;
574573
575− drag = timeout = 0;
574+ timo_cursor = timo_redraw = 0;
575+ drag = 0;
576576
577− if (mode == MODE_NORMAL) {
578− hidecur = 1;
579− timeout = 1500000;
580− }
577+ if (mode == MODE_NORMAL)
578+ timo_cursor = TO_CURSOR_HIDE;
581579
582580 while (1) {
583581 if (mode == MODE_THUMBS && tns.cnt < filecnt) {
@@ -586,8 +584,8 @@ void run() {
586584
587585 while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
588586 tns_load(&tns, &win, filenames[tns.cnt]);
589− gettimeofday(&t, 0);
590− if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
587+ gettimeofday(&t1, 0);
588+ if (TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0) >= 0.25)
591589 break;
592590 }
593591 if (tns.cnt == filecnt)
@@ -596,24 +594,40 @@ void run() {
596594 redraw();
597595 continue;
598596 } else {
599− timeout = 75000;
597+ timo_redraw = TO_THUMBS_LOAD;
600598 }
601− } else if (timeout) {
602− t.tv_sec = timeout / 1000000;
603− t.tv_usec = timeout % 1000000;
599+ } else if (timo_cursor || timo_redraw) {
600+ gettimeofday(&t0, 0);
601+ if (timo_cursor && timo_redraw)
602+ timeout = MIN(timo_cursor, timo_redraw);
603+ else if (timo_cursor)
604+ timeout = timo_cursor;
605+ else
606+ timeout = timo_redraw;
607+ tt.tv_sec = timeout / 1000000;
608+ tt.tv_usec = timeout % 1000000;
604609 xfd = ConnectionNumber(win.env.dpy);
605610 FD_ZERO(&fds);
606611 FD_SET(xfd, &fds);
607612
608− if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
609− /* timeout fired */
610− if (hidecur) {
613+ if (!XPending(win.env.dpy))
614+ select(xfd + 1, &fds, 0, 0, &tt);
615+ gettimeofday(&t1, 0);
616+ timeout = MIN((TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0)) * 1000000, timeout);
617+
618+ /* timeouts fired? */
619+ if (timo_cursor) {
620+ timo_cursor = MAX(0, timo_cursor - timeout);
621+ if (!timo_cursor)
611622 win_set_cursor(&win, CURSOR_NONE);
612− hidecur = 0;
613− } else {
623+ }
624+ if (timo_redraw) {
625+ timo_redraw = MAX(0, timo_redraw - timeout);
626+ if (!timo_redraw)
614627 redraw();
615− }
616628 }
629+ if (!XPending(win.env.dpy) && (timo_cursor || timo_redraw))
630+ continue;
617631 }
618632
619633 if (!XNextEvent(win.env.dpy, &ev)) {
@@ -629,8 +643,7 @@ void run() {
629643 drag = 0;
630644 if (mode == MODE_NORMAL) {
631645 win_set_cursor(&win, CURSOR_ARROW);
632− hidecur = 1;
633− timeout = 1500000;
646+ timo_cursor = TO_CURSOR_HIDE;
634647 }
635648 }
636649 break;
@@ -638,16 +651,14 @@ void run() {
638651 if (drag) {
639652 on_motionnotify(&ev.xmotion);
640653 } else if (mode == MODE_NORMAL) {
641− if (!hidecur) {
654+ if (!timo_cursor)
642655 win_set_cursor(&win, CURSOR_ARROW);
643− hidecur = 1;
644− }
645− timeout = 1500000;
656+ timo_cursor = TO_CURSOR_HIDE;
646657 }
647658 break;
648659 case ConfigureNotify:
649660 if (win_configure(&win, &ev.xconfigure)) {
650− timeout = 75000;
661+ timo_redraw = TO_WIN_RESIZE;
651662 if (mode == MODE_NORMAL)
652663 img.checkpan = 1;
653664 else