nsxiv4cfb8a2
Correct timeout handling, more complex
+52 −41
1 file changed
- M main.c +52 −41
| @@ -254,24 +254,23 @@ void read_dir_rec(const char *dirname) { | |||
| 254 | 254 | ||
| 255 | 255 | /* event handling */ | |
| 256 | 256 | ||
| 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; | |
| 259 | 263 | ||
| 260 | − | int mox, moy; | |
| 261 | 264 | unsigned char drag; | |
| 265 | + | int mox, moy; | |
| 262 | 266 | ||
| 263 | 267 | 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) | |
| 269 | 269 | img_render(&img, &win); | |
| 270 | − | } else { | |
| 270 | + | else | |
| 271 | 271 | tns_render(&tns, &win); | |
| 272 | − | } | |
| 273 | 272 | update_title(); | |
| 274 | − | timeout = 0; | |
| 273 | + | timo_redraw = 0; | |
| 275 | 274 | } | |
| 276 | 275 | ||
| 277 | 276 | void on_keypress(XKeyEvent *kev) { | |
| @@ -391,7 +390,7 @@ void on_keypress(XKeyEvent *kev) { | |||
| 391 | 390 | tns_init(&tns, filecnt); | |
| 392 | 391 | mode = MODE_THUMBS; | |
| 393 | 392 | win_set_cursor(&win, CURSOR_ARROW); | |
| 394 | − | hidecur = 0; | |
| 393 | + | timo_cursor = 0; | |
| 395 | 394 | tns.sel = fileidx; | |
| 396 | 395 | changed = tns.dirty = 1; | |
| 397 | 396 | break; | |
| @@ -489,7 +488,7 @@ void on_buttonpress(XButtonEvent *bev) { | |||
| 489 | 488 | mox = bev->x; | |
| 490 | 489 | moy = bev->y; | |
| 491 | 490 | win_set_cursor(&win, CURSOR_HAND); | |
| 492 | − | hidecur = 0; | |
| 491 | + | timo_cursor = 0; | |
| 493 | 492 | drag = 1; | |
| 494 | 493 | break; | |
| 495 | 494 | case Button3: | |
| @@ -530,7 +529,7 @@ void on_buttonpress(XButtonEvent *bev) { | |||
| 530 | 529 | fileidx = tns.sel; | |
| 531 | 530 | load_image(); | |
| 532 | 531 | mode = MODE_NORMAL; | |
| 533 | − | win_set_cursor(&win, CURSOR_NONE); | |
| 532 | + | timo_cursor = TO_CURSOR_HIDE; | |
| 534 | 533 | } else { | |
| 535 | 534 | tns_highlight(&tns, &win, tns.sel, False); | |
| 536 | 535 | tns_highlight(&tns, &win, sel, True); | |
| @@ -559,7 +558,7 @@ void on_motionnotify(XMotionEvent *mev) { | |||
| 559 | 558 | ||
| 560 | 559 | if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) { | |
| 561 | 560 | if (img_move(&img, &win, mev->x - mox, mev->y - moy)) | |
| 562 | − | timeout = 1000; | |
| 561 | + | timo_redraw = TO_IMAGE_DRAG; | |
| 563 | 562 | ||
| 564 | 563 | mox = mev->x; | |
| 565 | 564 | moy = mev->y; | |
| @@ -567,17 +566,16 @@ void on_motionnotify(XMotionEvent *mev) { | |||
| 567 | 566 | } | |
| 568 | 567 | ||
| 569 | 568 | void run() { | |
| 570 | − | int xfd; | |
| 569 | + | int xfd, timeout; | |
| 571 | 570 | fd_set fds; | |
| 572 | − | struct timeval t, t0; | |
| 571 | + | struct timeval tt, t0, t1; | |
| 573 | 572 | XEvent ev; | |
| 574 | 573 | ||
| 575 | − | drag = timeout = 0; | |
| 574 | + | timo_cursor = timo_redraw = 0; | |
| 575 | + | drag = 0; | |
| 576 | 576 | ||
| 577 | − | if (mode == MODE_NORMAL) { | |
| 578 | − | hidecur = 1; | |
| 579 | − | timeout = 1500000; | |
| 580 | − | } | |
| 577 | + | if (mode == MODE_NORMAL) | |
| 578 | + | timo_cursor = TO_CURSOR_HIDE; | |
| 581 | 579 | ||
| 582 | 580 | while (1) { | |
| 583 | 581 | if (mode == MODE_THUMBS && tns.cnt < filecnt) { | |
| @@ -586,8 +584,8 @@ void run() { | |||
| 586 | 584 | ||
| 587 | 585 | while (!XPending(win.env.dpy) && tns.cnt < filecnt) { | |
| 588 | 586 | 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) | |
| 591 | 589 | break; | |
| 592 | 590 | } | |
| 593 | 591 | if (tns.cnt == filecnt) | |
| @@ -596,24 +594,40 @@ void run() { | |||
| 596 | 594 | redraw(); | |
| 597 | 595 | continue; | |
| 598 | 596 | } else { | |
| 599 | − | timeout = 75000; | |
| 597 | + | timo_redraw = TO_THUMBS_LOAD; | |
| 600 | 598 | } | |
| 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; | |
| 604 | 609 | xfd = ConnectionNumber(win.env.dpy); | |
| 605 | 610 | FD_ZERO(&fds); | |
| 606 | 611 | FD_SET(xfd, &fds); | |
| 607 | 612 | ||
| 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) | |
| 611 | 622 | 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) | |
| 614 | 627 | redraw(); | |
| 615 | − | } | |
| 616 | 628 | } | |
| 629 | + | if (!XPending(win.env.dpy) && (timo_cursor || timo_redraw)) | |
| 630 | + | continue; | |
| 617 | 631 | } | |
| 618 | 632 | ||
| 619 | 633 | if (!XNextEvent(win.env.dpy, &ev)) { | |
| @@ -629,8 +643,7 @@ void run() { | |||
| 629 | 643 | drag = 0; | |
| 630 | 644 | if (mode == MODE_NORMAL) { | |
| 631 | 645 | win_set_cursor(&win, CURSOR_ARROW); | |
| 632 | − | hidecur = 1; | |
| 633 | − | timeout = 1500000; | |
| 646 | + | timo_cursor = TO_CURSOR_HIDE; | |
| 634 | 647 | } | |
| 635 | 648 | } | |
| 636 | 649 | break; | |
| @@ -638,16 +651,14 @@ void run() { | |||
| 638 | 651 | if (drag) { | |
| 639 | 652 | on_motionnotify(&ev.xmotion); | |
| 640 | 653 | } else if (mode == MODE_NORMAL) { | |
| 641 | − | if (!hidecur) { | |
| 654 | + | if (!timo_cursor) | |
| 642 | 655 | win_set_cursor(&win, CURSOR_ARROW); | |
| 643 | − | hidecur = 1; | |
| 644 | − | } | |
| 645 | − | timeout = 1500000; | |
| 656 | + | timo_cursor = TO_CURSOR_HIDE; | |
| 646 | 657 | } | |
| 647 | 658 | break; | |
| 648 | 659 | case ConfigureNotify: | |
| 649 | 660 | if (win_configure(&win, &ev.xconfigure)) { | |
| 650 | − | timeout = 75000; | |
| 661 | + | timo_redraw = TO_WIN_RESIZE; | |
| 651 | 662 | if (mode == MODE_NORMAL) | |
| 652 | 663 | img.checkpan = 1; | |
| 653 | 664 | else | |