Skip to content

Commit

Permalink
Use a refreshable timeout rather than a fixed delay before autoreload…
Browse files Browse the repository at this point in the history
…ing image
  • Loading branch information
0ion9 committed Apr 1, 2020
1 parent fc5e146 commit 6ae2df6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
31 changes: 23 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef struct {
} timeout_t;

/* timeout handler functions: */
void autoreload(void);
void redraw(void);
void reset_cursor(void);
void animate(void);
Expand Down Expand Up @@ -88,6 +89,7 @@ struct {
} keyhandler;

timeout_t timeouts[] = {
{ { 0, 0 }, false, autoreload },
{ { 0, 0 }, false, redraw },
{ { 0, 0 }, false, reset_cursor },
{ { 0, 0 }, false, animate },
Expand Down Expand Up @@ -943,10 +945,12 @@ void load_image(int new)

if (win.xwin != None)
win_set_cursor(&win, CURSOR_WATCH);
reset_timeout(autoreload);
reset_timeout(slideshow);

if (new != current)
if (new != current) {
alternate = current;
img.pending_autoreload = false;
}

img_close(&img, false);

Expand Down Expand Up @@ -1092,6 +1096,21 @@ int ptr_third_x(void)
return MAX(0, MIN(2, (x / (win.w * 0.33))));
}

// kau

void autoreload(void)
{
if (img.pending_autoreload) {
img_close(&img, true);
/* loading an image also resets the autoreload timeout */
load_image(fileidx);
redraw();
} else {
/* shouldn't happen */
reset_timeout(autoreload);
}
}

void redraw(void)
{
int t;
Expand Down Expand Up @@ -1394,8 +1413,6 @@ void on_buttonpress(XButtonEvent *bev)
prefix = 0;
}

const struct timespec ten_ms = {0, 10000000};

void run(void)
{
int xfd;
Expand Down Expand Up @@ -1442,10 +1459,8 @@ void run(void)
if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
if (arl_handle(&arl)) {
/* when too fast, imlib2 can't load the image */
nanosleep(&ten_ms, NULL);
img_close(&img, true);
load_image(fileidx);
redraw();
img.pending_autoreload = true;
set_timeout(autoreload, TO_AUTORELOAD, true);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion sxiv.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ enum {
TO_REDRAW_RESIZE = 75,
TO_REDRAW_THUMBS = 200,
TO_CURSOR_HIDE = 1200,
TO_DOUBLE_CLICK = 300
TO_DOUBLE_CLICK = 300,
TO_AUTORELOAD = 500
};

typedef void (*timeout_f)(void);
Expand Down Expand Up @@ -257,6 +258,8 @@ struct img {
int delay;
} ss;

bool pending_autoreload;

multi_img_t multi;
};

Expand Down

0 comments on commit 6ae2df6

Please sign in to comment.