aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2015-12-08 15:43:08 +0000
committerHarry Jeffery <harry@exec64.co.uk>2015-12-08 15:43:08 +0000
commitad1aca588a29516896e0088c3c207127d732e3ce (patch)
tree8248780fe39bbfb6606160904b231ee178888ea9 /src
parent9f569a394b9c8dbc7175ef9c7405aa8800f54d67 (diff)
downloadimv-ad1aca588a29516896e0088c3c207127d732e3ce.tar.gz
Refactor imv_navigator interface
Diffstat (limited to 'src')
-rw-r--r--src/main.c88
-rw-r--r--src/navigator.c97
-rw-r--r--src/navigator.h43
3 files changed, 96 insertions, 132 deletions
diff --git a/src/main.c b/src/main.c
index 870e33a..fdbc036 100644
--- a/src/main.c
+++ b/src/main.c
@@ -153,7 +153,7 @@ int main(int argc, char** argv)
}
struct imv_navigator nav;
- imv_init_navigator(&nav);
+ imv_navigator_init(&nav);
/* parse any command line options given */
parse_args(argc, argv);
@@ -166,11 +166,7 @@ int main(int argc, char** argv)
continue;
}
/* add the given path to the list to load */
- if(g_options.recursive) {
- imv_navigator_add_path_recursive(&nav, argv[i]);
- } else {
- imv_navigator_add_path(&nav, argv[i]);
- }
+ imv_navigator_add(&nav, argv[i], g_options.recursive);
}
/* if the user asked us to load paths from stdin, now is the time */
@@ -182,17 +178,13 @@ int main(int argc, char** argv)
buf[--len] = 0;
}
if(len > 0) {
- if(g_options.recursive) {
- imv_navigator_add_path_recursive(&nav, buf);
- } else {
- imv_navigator_add_path(&nav, buf);
- }
+ imv_navigator_add(&nav, buf, g_options.recursive);
}
}
}
/* if we weren't given any paths we have nothing to view. exit */
- if(!imv_navigator_get_current_path(&nav)) {
+ if(!imv_navigator_selection(&nav)) {
fprintf(stderr, "No input files. Exiting.\n");
exit(1);
}
@@ -203,7 +195,7 @@ int main(int argc, char** argv)
if(start_index < 0) {
start_index = strtol(g_options.start_at,NULL,10) - 1;
}
- imv_navigator_set_path(&nav, start_index);
+ imv_navigator_select_str(&nav, start_index);
}
/* we've got something to display, so create an SDL window */
@@ -285,11 +277,17 @@ int main(int argc, char** argv)
break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym) {
- case SDLK_q: quit = 1; break;
+ case SDLK_q:
+ quit = 1;
+ break;
case SDLK_LEFTBRACKET:
- case SDLK_LEFT: imv_navigator_prev_path(&nav); break;
+ case SDLK_LEFT:
+ imv_navigator_select_rel(&nav, -1);
+ break;
case SDLK_RIGHTBRACKET:
- case SDLK_RIGHT: imv_navigator_next_path(&nav); break;
+ case SDLK_RIGHT:
+ imv_navigator_select_rel(&nav, 1);
+ break;
case SDLK_EQUALS:
case SDLK_i:
case SDLK_UP:
@@ -300,18 +298,42 @@ int main(int argc, char** argv)
case SDLK_DOWN:
imv_viewport_zoom(&view, &tex, IMV_ZOOM_KEYBOARD, -1);
break;
- case SDLK_a: imv_viewport_scale_to_actual(&view, &tex);break;
- case SDLK_r: imv_viewport_scale_to_window(&view, &tex);break;
- case SDLK_c: imv_viewport_center(&view, &tex); break;
- case SDLK_j: imv_viewport_move(&view, 0, -50); break;
- case SDLK_k: imv_viewport_move(&view, 0, 50); break;
- case SDLK_h: imv_viewport_move(&view, 50, 0); break;
- case SDLK_l: imv_viewport_move(&view, -50, 0); break;
- case SDLK_x: imv_navigator_remove_current_path(&nav); break;
- case SDLK_f: imv_viewport_toggle_fullscreen(&view); break;
- case SDLK_PERIOD: imv_loader_load_next_frame(&ldr); break;
- case SDLK_SPACE: imv_viewport_toggle_playing(&view); ;break;
- case SDLK_p: puts(imv_navigator_get_current_path(&nav));break;
+ case SDLK_a:
+ imv_viewport_scale_to_actual(&view, &tex);
+ break;
+ case SDLK_r:
+ imv_viewport_scale_to_window(&view, &tex);
+ break;
+ case SDLK_c:
+ imv_viewport_center(&view, &tex);
+ break;
+ case SDLK_j:
+ imv_viewport_move(&view, 0, -50);
+ break;
+ case SDLK_k:
+ imv_viewport_move(&view, 0, 50);
+ break;
+ case SDLK_h:
+ imv_viewport_move(&view, 50, 0);
+ break;
+ case SDLK_l:
+ imv_viewport_move(&view, -50, 0);
+ break;
+ case SDLK_x:
+ imv_navigator_remove(&nav, imv_navigator_selection(&nav));
+ break;
+ case SDLK_f:
+ imv_viewport_toggle_fullscreen(&view);
+ break;
+ case SDLK_PERIOD:
+ imv_loader_load_next_frame(&ldr);
+ break;
+ case SDLK_SPACE:
+ imv_viewport_toggle_playing(&view);
+ break;
+ case SDLK_p:
+ puts(imv_navigator_selection(&nav));
+ break;
case SDLK_d:
g_options.overlay = !g_options.overlay;
imv_viewport_set_redraw(&view);
@@ -340,13 +362,13 @@ int main(int argc, char** argv)
/* check if an image failed to load, if so, remove it from our image list */
char *err_path = imv_loader_get_error(&ldr);
if(err_path) {
- imv_navigator_remove_path(&nav, err_path);
+ imv_navigator_remove(&nav, err_path);
free(err_path);
}
/* if the user has changed image, start loading the new one */
- if(imv_navigator_has_changed(&nav)) {
- const char *current_path = imv_navigator_get_current_path(&nav);
+ if(imv_navigator_poll_changed(&nav)) {
+ const char *current_path = imv_navigator_selection(&nav);
if(!current_path) {
fprintf(stderr, "No input files left. Exiting.\n");
exit(1);
@@ -372,7 +394,7 @@ int main(int argc, char** argv)
if(is_new_image) {
is_new_image = 0;
view.playing = 1;
- const char *current_path = imv_navigator_get_current_path(&nav);
+ const char *current_path = imv_navigator_selection(&nav);
char title[256];
snprintf(&title[0], sizeof(title), "imv - [%i/%i] [%ix%i] %s",
nav.cur_path + 1, nav.num_paths,
@@ -471,7 +493,7 @@ int main(int argc, char** argv)
/* clean up our resources now that we're exiting */
imv_destroy_loader(&ldr);
imv_destroy_texture(&tex);
- imv_destroy_navigator(&nav);
+ imv_navigator_destroy(&nav);
imv_destroy_viewport(&view);
if(font) {
diff --git a/src/navigator.c b/src/navigator.c
index a4aa4eb..ac3eac6 100644
--- a/src/navigator.c
+++ b/src/navigator.c
@@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <string.h>
#include <stdio.h>
-void imv_init_navigator(struct imv_navigator *nav)
+void imv_navigator_init(struct imv_navigator *nav)
{
nav->buf_size = 512;
nav->paths = malloc(sizeof(char*) * nav->buf_size);
@@ -33,7 +33,7 @@ void imv_init_navigator(struct imv_navigator *nav)
nav->changed = 0;
}
-void imv_destroy_navigator(struct imv_navigator *nav)
+void imv_navigator_destroy(struct imv_navigator *nav)
{
if(nav->buf_size > 0) {
free(nav->paths);
@@ -60,7 +60,8 @@ static void add_item(struct imv_navigator *nav, const char *path)
}
}
-void imv_navigator_add_path(struct imv_navigator *nav, const char *path)
+void imv_navigator_add(struct imv_navigator *nav, const char *path,
+ int recursive)
{
char path_buf[512];
struct stat path_info;
@@ -75,31 +76,11 @@ void imv_navigator_add_path(struct imv_navigator *nav, const char *path)
continue;
}
snprintf(path_buf, sizeof(path_buf), "%s/%s", path, dir->d_name);
- add_item(nav, path_buf);
- }
- closedir(d);
- }
- } else {
- add_item(nav, path);
- }
-}
-
-void imv_navigator_add_path_recursive(struct imv_navigator *nav, const char *path)
-{
- char path_buf[512];
- struct stat path_info;
- stat(path, &path_info);
- if(S_ISDIR(path_info.st_mode)) {
- DIR *d = opendir(path);
- if(d) {
- struct dirent *dir;
- while((dir = readdir(d)) != NULL) {
- if(strcmp(dir->d_name, "..") == 0 ||
- strcmp(dir->d_name, ".") == 0) {
- continue;
+ if(recursive) {
+ imv_navigator_add(nav, path_buf, recursive);
+ } else {
+ add_item(nav, path_buf);
}
- snprintf(path_buf, sizeof(path_buf), "%s/%s", path, dir->d_name);
- imv_navigator_add_path_recursive(nav, path_buf);
}
closedir(d);
}
@@ -108,7 +89,7 @@ void imv_navigator_add_path_recursive(struct imv_navigator *nav, const char *pat
}
}
-const char *imv_navigator_get_current_path(struct imv_navigator *nav)
+const char *imv_navigator_selection(struct imv_navigator *nav)
{
if(nav->num_paths == 0) {
return NULL;
@@ -116,35 +97,32 @@ const char *imv_navigator_get_current_path(struct imv_navigator *nav)
return nav->paths[nav->cur_path];
}
-void imv_navigator_next_path(struct imv_navigator *nav)
+void imv_navigator_select_rel(struct imv_navigator *nav, int direction)
{
int prev_path = nav->cur_path;
if(nav->num_paths == 0) {
return;
}
- nav->cur_path += 1;
- if(nav->cur_path == nav->num_paths) {
- nav->cur_path = 0;
- }
- nav->last_move_direction = 1;
- nav->changed = prev_path != nav->cur_path;
-}
-void imv_navigator_prev_path(struct imv_navigator *nav)
-{
- int prev_path = nav->cur_path;
- if(nav->num_paths == 0) {
+ if(direction > 1) {
+ direction = 1;
+ } else if(direction < -1) {
+ direction = -1;
+ } else if(direction == 0) {
return;
}
- nav->cur_path -= 1;
- if(nav->cur_path < 0) {
+
+ nav->cur_path += direction;
+ if(nav->cur_path == nav->num_paths) {
+ nav->cur_path = 0;
+ } else if(nav->cur_path < 0) {
nav->cur_path = nav->num_paths - 1;
}
- nav->last_move_direction = -1;
+ nav->last_move_direction = direction;
nav->changed = prev_path != nav->cur_path;
}
-void imv_navigator_remove_path(struct imv_navigator *nav, const char *path)
+void imv_navigator_remove(struct imv_navigator *nav, const char *path)
{
int removed = -1;
for(int i = 0; i < nav->num_paths; ++i) {
@@ -169,7 +147,7 @@ void imv_navigator_remove_path(struct imv_navigator *nav, const char *path)
/* We just removed the current path */
if(nav->last_move_direction < 0) {
/* Move left */
- imv_navigator_prev_path(nav);
+ 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) {
@@ -180,32 +158,7 @@ void imv_navigator_remove_path(struct imv_navigator *nav, const char *path)
nav->changed = 1;
}
-void imv_navigator_remove_current_path(struct imv_navigator *nav)
-{
- if(nav->num_paths == 0) {
- return;
- }
-
- free(nav->paths[nav->cur_path]);
- for(int i = nav->cur_path; i < nav->num_paths - 1; ++i) {
- nav->paths[i] = nav->paths[i+1];
- }
- nav->num_paths -= 1;
-
- if(nav->last_move_direction < 0) {
- /* Move left */
- imv_navigator_prev_path(nav);
- } 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->changed = 1;
-}
-
-void imv_navigator_set_path(struct imv_navigator *nav, const int path)
+void imv_navigator_select_str(struct imv_navigator *nav, const int path)
{
if(path <= 0 || path >= nav->num_paths) {
return;
@@ -236,7 +189,7 @@ int imv_navigator_find_path(struct imv_navigator *nav, const char *path)
return -1;
}
-int imv_navigator_has_changed(struct imv_navigator *nav)
+int imv_navigator_poll_changed(struct imv_navigator *nav)
{
if(nav->changed) {
nav->changed = 0;
diff --git a/src/navigator.h b/src/navigator.h
index 4ed2523..5b1d4a1 100644
--- a/src/navigator.h
+++ b/src/navigator.h
@@ -28,47 +28,36 @@ struct imv_navigator {
};
/* Initialises an instance of imv_navigator */
-void imv_init_navigator(struct imv_navigator *nav);
+void imv_navigator_init(struct imv_navigator *nav);
/* Cleans up all resources owned by a imv_navigator instance */
-void imv_destroy_navigator(struct imv_navigator *nav);
+void imv_navigator_destroy(struct imv_navigator *nav);
/* Adds the given path to the navigator's internal list.
* If a directory is given, all files within that directory are added.
- * An internal copy of path is made. */
-void imv_navigator_add_path(struct imv_navigator *nav, const char *path);
-
-/* Adds the given path to the navigator's internal list.
- * If a directory is given, all files within that directory are added.
- * Subdirectories are recursed into. An internal copy of path is made. */
-void imv_navigator_add_path_recursive(struct imv_navigator *nav, const char *path);
+ * An internal copy of path is made.
+ * If recursive is non-zero then subdirectories are recursed into. */
+void imv_navigator_add(struct imv_navigator *nav, const char *path,
+ int recursive);
/* Returns a read-only reference to the current path. The pointer is only
- * guaranteed to be valid until the next call to and imv_navigator method.
- */
-const char *imv_navigator_get_current_path(struct imv_navigator *nav);
-
-/* Changes to the next path */
-void imv_navigator_next_path(struct imv_navigator *nav);
-
-/* Changes to the previous path */
-void imv_navigator_prev_path(struct imv_navigator *nav);
+ * guaranteed to be valid until the next call to an imv_navigator method. */
+const char *imv_navigator_selection(struct imv_navigator *nav);
-/* Removes the currently selected path. The current selection is updated if
- * necessary, based on the last direction the selection moved. */
-void imv_navigator_remove_current_path(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);
-/* Removes the given path if it exists. The current selection is updated if
- * necessary, based on the last direction the selection moved. */
-void imv_navigator_remove_path(struct imv_navigator *nav, const char *path);
+/* Removes the given path. The current selection is updated if necessary,
+ * based on the last direction the selection moved. */
+void imv_navigator_remove(struct imv_navigator *nav, const char *path);
-/* Set the current path to the given path if it exists. */
-void imv_navigator_set_path(struct imv_navigator *nav, const int path);
+/* Select the given path if it exists. */
+void imv_navigator_select_str(struct imv_navigator *nav, const int path);
/* Return the index of the path given. Returns -1 if not found. */
int imv_navigator_find_path(struct imv_navigator *nav, const char *path);
/* Returns 1 if the currently selected path has changed since last called */
-int imv_navigator_has_changed(struct imv_navigator *nav);
+int imv_navigator_poll_changed(struct imv_navigator *nav);
#endif