diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2017-04-14 22:34:14 +0100 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2017-04-14 22:34:36 +0100 |
commit | f085f13f429d01a8f72e3b348a694124d5ba7533 (patch) | |
tree | 27da92ec779400ffd3732cc91a511e9abea601db | |
parent | cccb00ec0fa28d58bcebad8e9c8ff054657b725c (diff) | |
download | imv-f085f13f429d01a8f72e3b348a694124d5ba7533.tar.gz |
Refactor navigator poll rate limiting
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/navigator.c | 18 | ||||
-rw-r--r-- | src/navigator.h | 4 | ||||
-rw-r--r-- | test/navigator.c | 13 |
4 files changed, 27 insertions, 16 deletions
@@ -370,9 +370,6 @@ int main(int argc, char** argv) unsigned int last_time = SDL_GetTicks(); unsigned int current_time; - /* keep file change polling rate under control */ - static uint8_t poll_countdown = UINT8_MAX; - g_state.need_redraw = 1; g_state.need_rescale = 0; @@ -422,7 +419,7 @@ int main(int argc, char** argv) } /* if the user has changed image, start loading the new one */ - if(imv_navigator_poll_changed(g_state.nav, poll_countdown--)) { + if(imv_navigator_poll_changed(g_state.nav)) { const char *current_path = imv_navigator_selection(g_state.nav); if(!current_path) { if(g_options.stdin_list) { @@ -560,9 +557,6 @@ int main(int argc, char** argv) /* redraw complete, unset the flag */ g_state.need_redraw = 0; - /* reset poll countdown timer */ - poll_countdown = UINT8_MAX; - /* tell SDL to show the newly drawn frame */ SDL_RenderPresent(g_state.renderer); } diff --git a/src/navigator.c b/src/navigator.c index 6abbfb1..ae7bccd 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -47,6 +47,10 @@ void imv_navigator_free(struct imv_navigator *nav) free(nav->mtimes); } + if(nav->ctimes) { + free(nav->ctimes); + } + free(nav); } @@ -56,19 +60,23 @@ static int add_item(struct imv_navigator *nav, const char *path, if(nav->num_paths % BUFFER_SIZE == 0) { char **new_paths; time_t *new_mtimes; + time_t *new_ctimes; size_t new_size = nav->num_paths + BUFFER_SIZE; new_paths = realloc(nav->paths, sizeof(char*) * new_size); new_mtimes = realloc(nav->mtimes, sizeof(time_t) * new_size); - if (new_paths == NULL || new_mtimes == NULL) { + new_ctimes = realloc(nav->ctimes, sizeof(time_t) * new_size); + if (new_paths == NULL || new_mtimes == NULL || new_ctimes == NULL) { return 1; } nav->paths = new_paths; nav->mtimes = new_mtimes; + nav->ctimes = new_ctimes; } if((nav->paths[nav->num_paths] = strndup(path, PATH_MAX)) == NULL) { return 1; } nav->mtimes[nav->num_paths] = mtime; + nav->ctimes[nav->num_paths] = time(NULL); nav->num_paths += 1; if(nav->num_paths == 1) { nav->changed = 1; @@ -218,7 +226,7 @@ int imv_navigator_find_path(struct imv_navigator *nav, const char *path) return -1; } -int imv_navigator_poll_changed(struct imv_navigator *nav, const int nopoll) +int imv_navigator_poll_changed(struct imv_navigator *nav) { if(nav->changed) { nav->changed = 0; @@ -229,7 +237,11 @@ int imv_navigator_poll_changed(struct imv_navigator *nav, const int nopoll) return 0; }; - if(!nopoll) { + time_t cur_time = time(NULL); + /* limit polling to once per second */ + if(nav->ctimes[nav->cur_path] < cur_time - 1) { + nav->ctimes[nav->cur_path] = cur_time; + struct stat file_info; if(stat(nav->paths[nav->cur_path], &file_info) == -1) { return 0; diff --git a/src/navigator.h b/src/navigator.h index 7b7ccf8..0d56fa7 100644 --- a/src/navigator.h +++ b/src/navigator.h @@ -27,9 +27,11 @@ struct imv_navigator { int cur_path; char **paths; time_t *mtimes; + time_t *ctimes; int last_move_direction; int changed; int wrapped; + int poll_countdown; }; /* Creates an instance of imv_navigator */ @@ -65,7 +67,7 @@ int imv_navigator_find_path(struct imv_navigator *nav, const char *path); /* Returns 1 if either the currently selected path or underlying file has * changed since last called */ -int imv_navigator_poll_changed(struct imv_navigator *nav, const int nopoll); +int imv_navigator_poll_changed(struct imv_navigator *nav); /* Check whether navigator wrapped around paths list */ int imv_navigator_wrapped(struct imv_navigator *nav); diff --git a/test/navigator.c b/test/navigator.c index 2aba317..3f2c50f 100644 --- a/test/navigator.c +++ b/test/navigator.c @@ -22,7 +22,7 @@ static void test_navigator_add_remove(void **state) struct imv_navigator *nav = imv_navigator_create(); /* Check poll_changed */ - assert_false(imv_navigator_poll_changed(nav, 0)); + assert_false(imv_navigator_poll_changed(nav)); /* Add 6 paths, one non-existant should fail */ assert_false(imv_navigator_add(nav, FILENAME1, 0)); @@ -34,7 +34,7 @@ static void test_navigator_add_remove(void **state) assert_int_equal(nav->num_paths, 6); /* Check poll_changed */ - assert_true(imv_navigator_poll_changed(nav, 0)); + assert_true(imv_navigator_poll_changed(nav)); /* Make sure current selection is #1 */ assert_string_equal(imv_navigator_selection(nav), FILENAME1); @@ -87,8 +87,8 @@ static void test_navigator_file_changed(void **state) assert_false(futimens(fd, times) == -1); assert_false(imv_navigator_add(nav, FILENAME1, 0)); - assert_true(imv_navigator_poll_changed(nav, 0)); - assert_false(imv_navigator_poll_changed(nav, 0)); + assert_true(imv_navigator_poll_changed(nav)); + assert_false(imv_navigator_poll_changed(nav)); assert_false(sleep(1)); @@ -101,7 +101,10 @@ static void test_navigator_file_changed(void **state) times[1].tv_sec = UTIME_NOW; assert_false(futimens(fd, times) == -1); - assert_true(imv_navigator_poll_changed(nav, 0)); + /* sleep to ensure we don't hit the poll rate-limiting */ + sleep(1); + + assert_true(imv_navigator_poll_changed(nav)); (void)close(fd); (void)unlink(FILENAME1); |