From f1737ddd06141afbe99f37af3b5c2d0f1df5fe7a Mon Sep 17 00:00:00 2001 From: Hannes Koerber Date: Fri, 1 Apr 2016 17:02:03 +0200 Subject: Add -x switch to exit imv when reaching end of file list. --- src/main.c | 17 +++++++++++++---- src/navigator.c | 21 ++++++++++++++++----- src/navigator.h | 7 +++++-- 3 files changed, 34 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 426a12e..1e57f1f 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,7 @@ struct { int solid_bg; int list; unsigned long delay; + int cycle; unsigned char bg_r; unsigned char bg_g; unsigned char bg_b; @@ -71,6 +72,7 @@ struct { .solid_bg = 1, .list = 0, .delay = 0, + .cycle = 1, .bg_r = 0, .bg_g = 0, .bg_b = 0, @@ -105,7 +107,7 @@ static void parse_args(int argc, char** argv) char *argp, o; - while((o = getopt(argc, argv, "firasSudhln:b:e:t:")) != -1) { + while((o = getopt(argc, argv, "firasSudxhln:b:e:t:")) != -1) { switch(o) { case 'f': g_options.fullscreen = 1; break; case 'i': @@ -118,6 +120,7 @@ static void parse_args(int argc, char** argv) case 'S': g_options.scaling = FULL; break; case 'u': g_options.nearest_neighbour = 1; break; case 'd': g_options.overlay = 1; break; + case 'x': g_options.cycle = 0; break; case 'h': print_usage(); exit(0); break; case 'l': g_options.list = 1; break; case 'n': @@ -345,13 +348,17 @@ int main(int argc, char** argv) break; case SDLK_LEFTBRACKET: case SDLK_LEFT: - imv_navigator_select_rel(&nav, -1); + if(!imv_navigator_select_rel(&nav, -1, g_options.cycle)) { + quit = 1; + } /* reset slideshow delay */ delay_msec = 0; break; case SDLK_RIGHTBRACKET: case SDLK_RIGHT: - imv_navigator_select_rel(&nav, 1); + if(!imv_navigator_select_rel(&nav, 1, g_options.cycle)) { + quit = 1; + } /* reset slideshow delay */ delay_msec = 0; break; @@ -523,7 +530,9 @@ int main(int argc, char** argv) delay_msec += dt; need_redraw = 1; if(delay_msec >= g_options.delay) { - imv_navigator_select_rel(&nav, 1); + if(!imv_navigator_select_rel(&nav, 1, g_options.cycle)) { + quit = 1; + } delay_msec = 0; } } diff --git a/src/navigator.c b/src/navigator.c index 650240d..22b5d81 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -126,11 +126,12 @@ const char *imv_navigator_selection(struct imv_navigator *nav) return nav->paths[nav->cur_path]; } -void imv_navigator_select_rel(struct imv_navigator *nav, int direction) +int imv_navigator_select_rel(struct imv_navigator *nav, int direction, + const int cycle) { int prev_path = nav->cur_path; if(nav->num_paths == 0) { - return; + return 0; } if(direction > 1) { @@ -138,17 +139,26 @@ void imv_navigator_select_rel(struct imv_navigator *nav, int direction) } else if(direction < -1) { direction = -1; } else if(direction == 0) { - return; + return 0; } nav->cur_path += direction; if(nav->cur_path == nav->num_paths) { + /* end of list reached */ + if(!cycle) { + return 0; + } nav->cur_path = 0; } else if(nav->cur_path < 0) { + /* going backwards at the beginning of the list */ + if(!cycle) { + return 0; + } nav->cur_path = nav->num_paths - 1; } nav->last_move_direction = direction; nav->changed = prev_path != nav->cur_path; + return 1; } void imv_navigator_remove(struct imv_navigator *nav, const char *path) @@ -175,8 +185,9 @@ void imv_navigator_remove(struct imv_navigator *nav, const char *path) if(nav->cur_path == removed) { /* We just removed the current path */ if(nav->last_move_direction < 0) { - /* Move left */ - imv_navigator_select_rel(nav, -1); + /* Move left + * the cycle parameter does not matter here */ + imv_navigator_select_rel(nav, -1, 0); } else { /* Try to stay where we are, unless we ran out of room */ if(nav->cur_path == nav->num_paths) { diff --git a/src/navigator.h b/src/navigator.h index 749f656..fc4d5f9 100644 --- a/src/navigator.h +++ b/src/navigator.h @@ -47,8 +47,11 @@ void imv_navigator_add(struct imv_navigator *nav, const char *path, * guaranteed to be valid until the next call to an imv_navigator method. */ const char *imv_navigator_selection(struct imv_navigator *nav); -/* Change the currently selected path. dir = -1 for previous, 1 for next */ -void imv_navigator_select_rel(struct imv_navigator *nav, int dir); +/* Change the currently selected path. dir = -1 for previous, 1 for next + * cycle = 1 to go to the beginning of the file list if end is reached + * cycle = 0 to return error instead + * Returns 1 on success, 0 otherwise */ +int imv_navigator_select_rel(struct imv_navigator *nav, int dir, int cycle); /* Removes the given path. The current selection is updated if necessary, * based on the last direction the selection moved. */ -- cgit v1.2.3