diff options
Diffstat (limited to 'src/navigator.c')
-rw-r--r-- | src/navigator.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/navigator.c b/src/navigator.c index 97772cd..ae7bccd 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -24,13 +24,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include <string.h> #include <stdio.h> -void imv_navigator_init(struct imv_navigator *nav) +struct imv_navigator *imv_navigator_create(void) { + struct imv_navigator *nav = malloc(sizeof(struct imv_navigator)); memset(nav, 0, sizeof(struct imv_navigator)); nav->last_move_direction = 1; + return nav; } -void imv_navigator_destroy(struct imv_navigator *nav) +void imv_navigator_free(struct imv_navigator *nav) { if(nav->paths) { for(int i = 0; i < nav->num_paths; ++i) { @@ -45,7 +47,11 @@ void imv_navigator_destroy(struct imv_navigator *nav) free(nav->mtimes); } - memset(nav, 0, sizeof(struct imv_navigator)); + if(nav->ctimes) { + free(nav->ctimes); + } + + free(nav); } static int add_item(struct imv_navigator *nav, const char *path, @@ -54,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; @@ -216,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; @@ -227,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; |