aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitrij D. Czarkoff <czarkoff@gmail.com>2016-04-26 22:07:02 +0200
committerDmitrij D. Czarkoff <czarkoff@gmail.com>2016-04-26 22:07:02 +0200
commitfe10d9d38f02dbfe229e40f96f9640c3568a7f69 (patch)
treed8c06b524331784d004b8a549004295c12067373
parent4d2f36a98be483e021845e4e8fc17722a8aa800c (diff)
downloadimv-fe10d9d38f02dbfe229e40f96f9640c3568a7f69.tar.gz
Reimplement '-x' flag
This change effectively backs out f1737ddd06141afbe99f37af3b5c2d0f1df5fe7a, implementing the same functionality in a simpler and more correct way. Implementation details: * imv_navigator.wrapped - a new field that has value "0" by default; once navigator wraps around the list, this field recieves value "1". * imv_navigator_wrapped(&nav) - a new function that returns non-zero value iff navigator wrapped around the list. Currently just returns value of imv_navigator.wrapped. While at it, expanded "test_navigator_remove" and merged it with "test_navigator_add". Resulting test is called "test_navigator_add_remove". Fixes #94.
-rw-r--r--src/main.c17
-rw-r--r--src/navigator.c37
-rw-r--r--src/navigator.h11
-rw-r--r--test/navigator.c60
4 files changed, 67 insertions, 58 deletions
diff --git a/src/main.c b/src/main.c
index a342a6c..2bb2729 100644
--- a/src/main.c
+++ b/src/main.c
@@ -348,17 +348,13 @@ int main(int argc, char** argv)
break;
case SDLK_LEFTBRACKET:
case SDLK_LEFT:
- if(!imv_navigator_select_rel(&nav, -1, g_options.cycle)) {
- quit = 1;
- }
+ imv_navigator_select_rel(&nav, -1);
/* reset slideshow delay */
delay_msec = 0;
break;
case SDLK_RIGHTBRACKET:
case SDLK_RIGHT:
- if(!imv_navigator_select_rel(&nav, 1, g_options.cycle)) {
- quit = 1;
- }
+ imv_navigator_select_rel(&nav, 1);
/* reset slideshow delay */
delay_msec = 0;
break;
@@ -486,6 +482,11 @@ int main(int argc, char** argv)
free(err_path);
}
+ /* Check if navigator wrapped around paths lists */
+ if(!g_options.cycle && imv_navigator_wrapped(&nav)) {
+ break;
+ }
+
/* if the user has changed image, start loading the new one */
if(imv_navigator_poll_changed(&nav, poll_countdown--)) {
const char *current_path = imv_navigator_selection(&nav);
@@ -549,9 +550,7 @@ int main(int argc, char** argv)
delay_msec += dt;
need_redraw = 1;
if(delay_msec >= g_options.delay) {
- if(!imv_navigator_select_rel(&nav, 1, g_options.cycle)) {
- quit = 1;
- }
+ imv_navigator_select_rel(&nav, 1);
delay_msec = 0;
}
}
diff --git a/src/navigator.c b/src/navigator.c
index b4e65fb..2756134 100644
--- a/src/navigator.c
+++ b/src/navigator.c
@@ -42,6 +42,7 @@ void imv_navigator_init(struct imv_navigator *nav)
nav->cur_path = 0;
nav->last_move_direction = 1;
nav->changed = 0;
+ nav->wrapped = 0;
}
void imv_navigator_destroy(struct imv_navigator *nav)
@@ -126,12 +127,11 @@ const char *imv_navigator_selection(struct imv_navigator *nav)
return nav->paths[nav->cur_path];
}
-int imv_navigator_select_rel(struct imv_navigator *nav, int direction,
- const int cycle)
+void imv_navigator_select_rel(struct imv_navigator *nav, int direction)
{
int prev_path = nav->cur_path;
if(nav->num_paths == 0) {
- return 0;
+ return;
}
if(direction > 1) {
@@ -139,30 +139,22 @@ int imv_navigator_select_rel(struct imv_navigator *nav, int direction,
} else if(direction < -1) {
direction = -1;
} else if(direction == 0) {
- return 0;
+ return;
}
nav->cur_path += direction;
if(nav->cur_path == nav->num_paths) {
- /* end of list reached */
- if(!cycle) {
- /* undo move */
- nav->cur_path -= direction;
- return 0;
- }
+ /* Wrap after the end of the list */
nav->cur_path = 0;
+ nav->wrapped = 1;
} else if(nav->cur_path < 0) {
- /* going backwards at the beginning of the list */
- if(!cycle) {
- /* undo move */
- nav->cur_path -= direction;
- return 0;
- }
+ /* Wrap before the start of the list */
nav->cur_path = nav->num_paths - 1;
+ nav->wrapped = 1;
}
nav->last_move_direction = direction;
nav->changed = prev_path != nav->cur_path;
- return 1;
+ return;
}
void imv_navigator_remove(struct imv_navigator *nav, const char *path)
@@ -189,13 +181,13 @@ 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
- * the cycle parameter does not matter here */
- imv_navigator_select_rel(nav, -1, 0);
+ /* Move left */
+ imv_navigator_select_rel(nav, -1);
} else {
/* Try to stay where we are, unless we ran out of room */
if(nav->cur_path == nav->num_paths) {
nav->cur_path = 0;
+ nav->wrapped = 1;
}
}
}
@@ -253,5 +245,10 @@ int imv_navigator_poll_changed(struct imv_navigator *nav, const int nopoll)
return 0;
}
+int imv_navigator_wrapped(struct imv_navigator *nav)
+{
+ return nav->wrapped;
+}
+
/* vim:set ts=2 sts=2 sw=2 et: */
diff --git a/src/navigator.h b/src/navigator.h
index 1645cae..b539e8b 100644
--- a/src/navigator.h
+++ b/src/navigator.h
@@ -28,6 +28,7 @@ struct imv_navigator {
time_t *mtimes;
int last_move_direction;
int changed;
+ int wrapped;
};
/* Initialises an instance of imv_navigator */
@@ -47,11 +48,8 @@ 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
- * 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);
+/* Change the currently selected path. dir = -1 for previous, 1 for next. */
+void imv_navigator_select_rel(struct imv_navigator *nav, int dir);
/* Removes the given path. The current selection is updated if necessary,
* based on the last direction the selection moved. */
@@ -67,6 +65,9 @@ int imv_navigator_find_path(struct imv_navigator *nav, const char *path);
* changed since last called */
int imv_navigator_poll_changed(struct imv_navigator *nav, const int nopoll);
+/* Check whether navigator wrapped around paths list */
+int imv_navigator_wrapped(struct imv_navigator *nav);
+
#endif
diff --git a/test/navigator.c b/test/navigator.c
index 9b756e9..2a8a7bb 100644
--- a/test/navigator.c
+++ b/test/navigator.c
@@ -12,48 +12,61 @@
#define FILENAME1 "example.file.1"
#define FILENAME2 "example.file.2"
#define FILENAME3 "example.file.3"
+#define FILENAME4 "example.file.4"
+#define FILENAME5 "example.file.5"
+#define FILENAME6 "example.file.6"
-static void test_navigator_add(void **state)
+static void test_navigator_add_remove(void **state)
{
(void)state;
struct imv_navigator nav;
imv_navigator_init(&nav);
+ /* Check poll_changed */
assert_false(imv_navigator_poll_changed(&nav, 0));
- imv_navigator_add(&nav, FILENAME1, 0);
- assert_true(imv_navigator_poll_changed(&nav, 0));
- assert_string_equal(imv_navigator_selection(&nav), FILENAME1);
-
- imv_navigator_destroy(&nav);
-}
-
-static void test_navigator_remove(void **state)
-{
- (void)state;
- struct imv_navigator nav;
- imv_navigator_init(&nav);
/* Add 3 paths */
imv_navigator_add(&nav, FILENAME1, 0);
imv_navigator_add(&nav, FILENAME2, 0);
imv_navigator_add(&nav, FILENAME3, 0);
- assert_int_equal(nav.num_paths, 3);
+ imv_navigator_add(&nav, FILENAME4, 0);
+ imv_navigator_add(&nav, FILENAME5, 0);
+ imv_navigator_add(&nav, FILENAME6, 0);
+ assert_int_equal(nav.num_paths, 6);
+
+ /* Check poll_changed */
+ assert_true(imv_navigator_poll_changed(&nav, 0));
+ /* Make sure current selection is #1 */
assert_string_equal(imv_navigator_selection(&nav), FILENAME1);
- assert_true(imv_navigator_select_rel(&nav, 1, 0));
+
+ /* Move right and remove current file (#2); should get to #3 */
+ imv_navigator_select_rel(&nav, 1);
assert_string_equal(imv_navigator_selection(&nav), FILENAME2);
imv_navigator_remove(&nav, FILENAME2);
- assert_int_equal(nav.num_paths, 2);
+ assert_int_equal(nav.num_paths, 5);
assert_string_equal(imv_navigator_selection(&nav), FILENAME3);
- assert_false(imv_navigator_select_rel(&nav, 1, 0));
- assert_string_equal(imv_navigator_selection(&nav), FILENAME3);
- imv_navigator_remove(&nav, FILENAME3);
- assert_int_equal(nav.num_paths, 1);
+ /* Move left and remove current file (#1); should get to #6 */
+ imv_navigator_select_rel(&nav, -1);
assert_string_equal(imv_navigator_selection(&nav), FILENAME1);
imv_navigator_remove(&nav, FILENAME1);
- assert_int_equal(nav.num_paths, 0);
- assert_false(imv_navigator_selection(&nav));
+ assert_string_equal(imv_navigator_selection(&nav), FILENAME6);
+
+ /* Move left, right, remove current file (#6); should get to #3 */
+ imv_navigator_select_rel(&nav, -1);
+ imv_navigator_select_rel(&nav, 1);
+ assert_string_equal(imv_navigator_selection(&nav), FILENAME6);
+ imv_navigator_remove(&nav, FILENAME6);
+ assert_string_equal(imv_navigator_selection(&nav), FILENAME3);
+
+ /* Remove #4; should not move */
+ imv_navigator_remove(&nav, FILENAME4);
+ assert_string_equal(imv_navigator_selection(&nav), FILENAME3);
+
+ /* Verify that #4 is removed by moving left; should get to #5 */
+ imv_navigator_select_rel(&nav, 1);
+ assert_string_equal(imv_navigator_selection(&nav), FILENAME5);
imv_navigator_destroy(&nav);
}
@@ -100,8 +113,7 @@ static void test_navigator_file_changed(void **state)
int main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_navigator_add),
- cmocka_unit_test(test_navigator_remove),
+ cmocka_unit_test(test_navigator_add_remove),
cmocka_unit_test(test_navigator_file_changed),
};