From ad33be42c0192a491cce10c1771ed8e2ac1d0196 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Wed, 12 Apr 2017 22:16:41 +0100 Subject: Add basic command system --- src/commands.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/commands.h (limited to 'src/commands.h') diff --git a/src/commands.h b/src/commands.h new file mode 100644 index 0000000..1dfa3bf --- /dev/null +++ b/src/commands.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2017 imv authors + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef COMMANDS_H +#define COMMANDS_H + +void imv_command_register(const char *command, void (*handler)()); +int imv_command_exec(const char *command); + +#endif + +/* vim:set ts=2 sts=2 sw=2 et: */ -- cgit v1.2.3 From b46385618eb6c2e6ee468c672c6824259d97add2 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Fri, 14 Apr 2017 16:23:50 +0100 Subject: Add basic alias support --- src/commands.c | 23 +++++++++++++++++++++-- src/commands.h | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'src/commands.h') diff --git a/src/commands.c b/src/commands.c index 9b93933..f9a4ff3 100644 --- a/src/commands.c +++ b/src/commands.c @@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. struct imv_command { const char* command; void (*handler)(struct imv_list *args); + const char* alias; }; struct imv_list *g_commands = NULL; @@ -34,6 +35,20 @@ void imv_command_register(const char *command, void (*handler)()) struct imv_command *cmd = malloc(sizeof(struct imv_command)); cmd->command = strdup(command); cmd->handler = handler; + cmd->alias = NULL; + imv_list_append(g_commands, cmd); +} + +void imv_command_alias(const char *command, const char *alias) +{ + if(!g_commands) { + g_commands = imv_list_create(); + } + + struct imv_command *cmd = malloc(sizeof(struct imv_command)); + cmd->command = strdup(command); + cmd->handler = NULL; + cmd->alias = strdup(alias); imv_list_append(g_commands, cmd); } @@ -50,8 +65,12 @@ int imv_command_exec(const char *command) for(size_t i = 0; i < g_commands->len; ++i) { struct imv_command *cmd = g_commands->items[i]; if(!strcmp(cmd->command, args->items[0])) { - cmd->handler(args); - ret = 0; + if(cmd->handler) { + cmd->handler(args); + ret = 0; + } else if(cmd->alias) { + ret = imv_command_exec(cmd->alias); + } break; } } diff --git a/src/commands.h b/src/commands.h index 1dfa3bf..b3a78e6 100644 --- a/src/commands.h +++ b/src/commands.h @@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define COMMANDS_H void imv_command_register(const char *command, void (*handler)()); +void imv_command_alias(const char *command, const char *alias); int imv_command_exec(const char *command); #endif -- cgit v1.2.3 From 50759fb279b38c5db7cacf88206188b827f564b2 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Fri, 14 Apr 2017 16:44:07 +0100 Subject: Let's not have imv_commands use hidden globals --- src/commands.c | 55 +++++++++++++++++++++++++++++++------------------------ src/commands.h | 14 +++++++++++--- src/main.c | 55 +++++++++++++++++++++++++++++-------------------------- 3 files changed, 71 insertions(+), 53 deletions(-) (limited to 'src/commands.h') diff --git a/src/commands.c b/src/commands.c index f9a4ff3..a3a59b1 100644 --- a/src/commands.c +++ b/src/commands.c @@ -18,58 +18,65 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "commands.h" #include "list.h" -struct imv_command { - const char* command; +struct command { + char* command; void (*handler)(struct imv_list *args); - const char* alias; + char* alias; }; -struct imv_list *g_commands = NULL; +struct imv_commands *imv_commands_create(void) +{ + struct imv_commands *cmds = malloc(sizeof(struct imv_commands)); + cmds->command_list = imv_list_create(); + return cmds; +} -void imv_command_register(const char *command, void (*handler)()) +void imv_commands_free(struct imv_commands *cmds) { - if(!g_commands) { - g_commands = imv_list_create(); + for(size_t i = 0; i < cmds->command_list->len; ++i) { + struct command *cmd = cmds->command_list->items[i]; + free(cmd->command); + if(cmd->alias) { + free(cmd->alias); + } + free(cmd); } + imv_list_free(cmds->command_list); + free(cmds); +} - struct imv_command *cmd = malloc(sizeof(struct imv_command)); +void imv_command_register(struct imv_commands *cmds, const char *command, void (*handler)()) +{ + struct command *cmd = malloc(sizeof(struct command)); cmd->command = strdup(command); cmd->handler = handler; cmd->alias = NULL; - imv_list_append(g_commands, cmd); + imv_list_append(cmds->command_list, cmd); } -void imv_command_alias(const char *command, const char *alias) +void imv_command_alias(struct imv_commands *cmds, const char *command, const char *alias) { - if(!g_commands) { - g_commands = imv_list_create(); - } - - struct imv_command *cmd = malloc(sizeof(struct imv_command)); + struct command *cmd = malloc(sizeof(struct command)); cmd->command = strdup(command); cmd->handler = NULL; cmd->alias = strdup(alias); - imv_list_append(g_commands, cmd); + imv_list_append(cmds->command_list, cmd); } -int imv_command_exec(const char *command) +int imv_command_exec(struct imv_commands *cmds, const char *command) { - if(!g_commands) { - return 1; - } - struct imv_list *args = imv_split_string(command, ' '); int ret = 1; if(args->len > 0) { - for(size_t i = 0; i < g_commands->len; ++i) { - struct imv_command *cmd = g_commands->items[i]; + for(size_t i = 0; i < cmds->command_list->len; ++i) { + struct command *cmd = cmds->command_list->items[i]; if(!strcmp(cmd->command, args->items[0])) { if(cmd->handler) { cmd->handler(args); ret = 0; } else if(cmd->alias) { - ret = imv_command_exec(cmd->alias); + ret = imv_command_exec(cmds, cmd->alias); } break; } diff --git a/src/commands.h b/src/commands.h index b3a78e6..df8b43b 100644 --- a/src/commands.h +++ b/src/commands.h @@ -18,9 +18,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifndef COMMANDS_H #define COMMANDS_H -void imv_command_register(const char *command, void (*handler)()); -void imv_command_alias(const char *command, const char *alias); -int imv_command_exec(const char *command); +struct imv_list; + +struct imv_commands { + struct imv_list *command_list; +}; + +struct imv_commands *imv_commands_create(void); +void imv_commands_free(struct imv_commands *cmds); +void imv_command_register(struct imv_commands *cmds, const char *command, void (*handler)()); +void imv_command_alias(struct imv_commands *cmds, const char *command, const char *alias); +int imv_command_exec(struct imv_commands *cmds, const char *command); #endif diff --git a/src/main.c b/src/main.c index 9ad0531..56971c1 100644 --- a/src/main.c +++ b/src/main.c @@ -177,6 +177,7 @@ struct { struct imv_loader ldr; struct imv_texture tex; struct imv_viewport view; + struct imv_commands *cmds; SDL_Window *window; SDL_Renderer *renderer; int quit; @@ -203,20 +204,21 @@ void cmd_overlay(struct imv_list *args); int main(int argc, char** argv) { - imv_command_register("quit", &cmd_quit); - imv_command_register("pan", &cmd_pan); - imv_command_register("select_rel", &cmd_select_rel); - imv_command_register("select_abs", &cmd_select_abs); - imv_command_register("zoom", &cmd_zoom); - imv_command_register("remove", &cmd_remove); - imv_command_register("fullscreen", &cmd_fullscreen); - imv_command_register("overlay", &cmd_overlay); - - imv_command_alias("q", "quit"); - imv_command_alias("next", "select_rel 1"); - imv_command_alias("previous", "select_rel -1"); - imv_command_alias("n", "select_rel 1"); - imv_command_alias("p", "select_rel -1"); + g_state.cmds = imv_commands_create(); + imv_command_register(g_state.cmds, "quit", &cmd_quit); + imv_command_register(g_state.cmds, "pan", &cmd_pan); + imv_command_register(g_state.cmds, "select_rel", &cmd_select_rel); + imv_command_register(g_state.cmds, "select_abs", &cmd_select_abs); + imv_command_register(g_state.cmds, "zoom", &cmd_zoom); + imv_command_register(g_state.cmds, "remove", &cmd_remove); + imv_command_register(g_state.cmds, "fullscreen", &cmd_fullscreen); + imv_command_register(g_state.cmds, "overlay", &cmd_overlay); + + imv_command_alias(g_state.cmds, "q", "quit"); + imv_command_alias(g_state.cmds, "next", "select_rel 1"); + imv_command_alias(g_state.cmds, "previous", "select_rel -1"); + imv_command_alias(g_state.cmds, "n", "select_rel 1"); + imv_command_alias(g_state.cmds, "p", "select_rel -1"); imv_navigator_init(&g_state.nav); @@ -392,7 +394,7 @@ int main(int argc, char** argv) while(!g_state.quit && SDL_PollEvent(&e)) { switch(e.type) { case SDL_QUIT: - imv_command_exec("quit"); + imv_command_exec(g_state.cmds, "quit"); break; case SDL_KEYDOWN: @@ -405,7 +407,7 @@ int main(int argc, char** argv) g_state.command_buffer = NULL; g_state.need_redraw = 1; } else if(e.key.keysym.sym == SDLK_RETURN) { - imv_command_exec(g_state.command_buffer); + imv_command_exec(g_state.cmds, g_state.command_buffer); free(g_state.command_buffer); g_state.command_buffer = NULL; g_state.need_redraw = 1; @@ -437,15 +439,15 @@ int main(int argc, char** argv) } break; case SDLK_q: - imv_command_exec("quit"); + imv_command_exec(g_state.cmds, "quit"); break; case SDLK_LEFTBRACKET: case SDLK_LEFT: - imv_command_exec("select_rel -1"); + imv_command_exec(g_state.cmds, "select_rel -1"); break; case SDLK_RIGHTBRACKET: case SDLK_RIGHT: - imv_command_exec("select_rel 1"); + imv_command_exec(g_state.cmds, "select_rel 1"); break; case SDLK_EQUALS: case SDLK_PLUS: @@ -482,25 +484,25 @@ int main(int argc, char** argv) } break; case SDLK_j: - imv_command_exec("pan 0 -50"); + imv_command_exec(g_state.cmds, "pan 0 -50"); break; case SDLK_k: - imv_command_exec("pan 0 50"); + imv_command_exec(g_state.cmds, "pan 0 50"); break; case SDLK_h: - imv_command_exec("pan 50 0"); + imv_command_exec(g_state.cmds, "pan 50 0"); break; case SDLK_l: - imv_command_exec("pan -50 0"); + imv_command_exec(g_state.cmds, "pan -50 0"); break; case SDLK_x: if(!e.key.repeat) { - imv_command_exec("remove"); + imv_command_exec(g_state.cmds, "remove"); } break; case SDLK_f: if(!e.key.repeat) { - imv_command_exec("fullscreen"); + imv_command_exec(g_state.cmds, "fullscreen"); } break; case SDLK_PERIOD: @@ -518,7 +520,7 @@ int main(int argc, char** argv) break; case SDLK_d: if(!e.key.repeat) { - imv_command_exec("overlay"); + imv_command_exec(g_state.cmds, "overlay"); } break; case SDLK_t: @@ -772,6 +774,7 @@ int main(int argc, char** argv) imv_destroy_texture(&g_state.tex); imv_navigator_destroy(&g_state.nav); imv_destroy_viewport(&g_state.view); + imv_commands_free(g_state.cmds); if(font) { TTF_CloseFont(font); -- cgit v1.2.3 From a6bb8ad100348693a39ea13ba6af361f2dab5101 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sat, 15 Apr 2017 10:58:31 +0100 Subject: Let commands take an arbitrary pointer --- src/commands.c | 8 ++++---- src/commands.h | 4 ++-- src/main.c | 64 +++++++++++++++++++++++++++++++++------------------------- 3 files changed, 42 insertions(+), 34 deletions(-) (limited to 'src/commands.h') diff --git a/src/commands.c b/src/commands.c index a3a59b1..0f31bfc 100644 --- a/src/commands.c +++ b/src/commands.c @@ -20,7 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. struct command { char* command; - void (*handler)(struct imv_list *args); + void (*handler)(struct imv_list *args, void *data); char* alias; }; @@ -63,7 +63,7 @@ void imv_command_alias(struct imv_commands *cmds, const char *command, const cha imv_list_append(cmds->command_list, cmd); } -int imv_command_exec(struct imv_commands *cmds, const char *command) +int imv_command_exec(struct imv_commands *cmds, const char *command, void *data) { struct imv_list *args = imv_split_string(command, ' '); int ret = 1; @@ -73,10 +73,10 @@ int imv_command_exec(struct imv_commands *cmds, const char *command) struct command *cmd = cmds->command_list->items[i]; if(!strcmp(cmd->command, args->items[0])) { if(cmd->handler) { - cmd->handler(args); + cmd->handler(args, data); ret = 0; } else if(cmd->alias) { - ret = imv_command_exec(cmds, cmd->alias); + ret = imv_command_exec(cmds, cmd->alias, data); } break; } diff --git a/src/commands.h b/src/commands.h index df8b43b..89f94a2 100644 --- a/src/commands.h +++ b/src/commands.h @@ -24,11 +24,11 @@ struct imv_commands { struct imv_list *command_list; }; -struct imv_commands *imv_commands_create(void); +struct imv_commands *imv_commands_create(); void imv_commands_free(struct imv_commands *cmds); void imv_command_register(struct imv_commands *cmds, const char *command, void (*handler)()); void imv_command_alias(struct imv_commands *cmds, const char *command, const char *alias); -int imv_command_exec(struct imv_commands *cmds, const char *command); +int imv_command_exec(struct imv_commands *cmds, const char *command, void *data); #endif diff --git a/src/main.c b/src/main.c index d0cfb78..8cbd095 100644 --- a/src/main.c +++ b/src/main.c @@ -101,14 +101,14 @@ struct { char *command_buffer; } g_state; -void cmd_quit(struct imv_list *args); -void cmd_pan(struct imv_list *args); -void cmd_select_rel(struct imv_list *args); -void cmd_select_abs(struct imv_list *args); -void cmd_zoom(struct imv_list *args); -void cmd_remove(struct imv_list *args); -void cmd_fullscreen(struct imv_list *args); -void cmd_overlay(struct imv_list *args); +void cmd_quit(struct imv_list *args, void *data); +void cmd_pan(struct imv_list *args, void *data); +void cmd_select_rel(struct imv_list *args, void *data); +void cmd_select_abs(struct imv_list *args, void *data); +void cmd_zoom(struct imv_list *args, void *data); +void cmd_remove(struct imv_list *args, void *data); +void cmd_fullscreen(struct imv_list *args, void *data); +void cmd_overlay(struct imv_list *args, void *data); static void parse_args(int *argc, char ***argv); void handle_event(SDL_Event *event); @@ -495,14 +495,16 @@ int main(int argc, char** argv) return 0; } -void cmd_quit(struct imv_list *args) +void cmd_quit(struct imv_list *args, void *data) { (void)args; + (void)data; g_state.quit = 1; } -void cmd_pan(struct imv_list *args) +void cmd_pan(struct imv_list *args, void *data) { + (void)data; if(args->len != 3) { return; } @@ -513,8 +515,9 @@ void cmd_pan(struct imv_list *args) imv_viewport_move(g_state.view, x, y); } -void cmd_select_rel(struct imv_list *args) +void cmd_select_rel(struct imv_list *args, void *data) { + (void)data; if(args->len != 2) { return; } @@ -526,19 +529,22 @@ void cmd_select_rel(struct imv_list *args) g_state.delay_msec = 0; } -void cmd_select_abs(struct imv_list *args) +void cmd_select_abs(struct imv_list *args, void *data) { (void)args; + (void)data; } -void cmd_zoom(struct imv_list *args) +void cmd_zoom(struct imv_list *args, void *data) { (void)args; + (void)data; } -void cmd_remove(struct imv_list *args) +void cmd_remove(struct imv_list *args, void *data) { (void)args; + (void)data; char* path = strdup(imv_navigator_selection(g_state.nav)); imv_navigator_remove(g_state.nav, path); free(path); @@ -547,15 +553,17 @@ void cmd_remove(struct imv_list *args) g_state.delay_msec = 0; } -void cmd_fullscreen(struct imv_list *args) +void cmd_fullscreen(struct imv_list *args, void *data) { (void)args; + (void)data; imv_viewport_toggle_fullscreen(g_state.view); } -void cmd_overlay(struct imv_list *args) +void cmd_overlay(struct imv_list *args, void *data) { (void)args; + (void)data; g_options.overlay = !g_options.overlay; g_state.need_redraw = 1; } @@ -659,7 +667,7 @@ void handle_event(SDL_Event *event) const int command_buffer_len = 1024; switch(event->type) { case SDL_QUIT: - imv_command_exec(g_state.cmds, "quit"); + imv_command_exec(g_state.cmds, "quit", NULL); break; case SDL_TEXTINPUT: @@ -678,7 +686,7 @@ void handle_event(SDL_Event *event) g_state.command_buffer = NULL; g_state.need_redraw = 1; } else if(event->key.keysym.sym == SDLK_RETURN) { - imv_command_exec(g_state.cmds, g_state.command_buffer); + imv_command_exec(g_state.cmds, g_state.command_buffer, NULL); SDL_StopTextInput(); free(g_state.command_buffer); g_state.command_buffer = NULL; @@ -704,15 +712,15 @@ void handle_event(SDL_Event *event) } break; case SDLK_q: - imv_command_exec(g_state.cmds, "quit"); + imv_command_exec(g_state.cmds, "quit", NULL); break; case SDLK_LEFTBRACKET: case SDLK_LEFT: - imv_command_exec(g_state.cmds, "select_rel -1"); + imv_command_exec(g_state.cmds, "select_rel -1", NULL); break; case SDLK_RIGHTBRACKET: case SDLK_RIGHT: - imv_command_exec(g_state.cmds, "select_rel 1"); + imv_command_exec(g_state.cmds, "select_rel 1", NULL); break; case SDLK_EQUALS: case SDLK_PLUS: @@ -749,25 +757,25 @@ void handle_event(SDL_Event *event) } break; case SDLK_j: - imv_command_exec(g_state.cmds, "pan 0 -50"); + imv_command_exec(g_state.cmds, "pan 0 -50", NULL); break; case SDLK_k: - imv_command_exec(g_state.cmds, "pan 0 50"); + imv_command_exec(g_state.cmds, "pan 0 50", NULL); break; case SDLK_h: - imv_command_exec(g_state.cmds, "pan 50 0"); + imv_command_exec(g_state.cmds, "pan 50 0", NULL); break; case SDLK_l: - imv_command_exec(g_state.cmds, "pan -50 0"); + imv_command_exec(g_state.cmds, "pan -50 0", NULL); break; case SDLK_x: if(!event->key.repeat) { - imv_command_exec(g_state.cmds, "remove"); + imv_command_exec(g_state.cmds, "remove", NULL); } break; case SDLK_f: if(!event->key.repeat) { - imv_command_exec(g_state.cmds, "fullscreen"); + imv_command_exec(g_state.cmds, "fullscreen", NULL); } break; case SDLK_PERIOD: @@ -785,7 +793,7 @@ void handle_event(SDL_Event *event) break; case SDLK_d: if(!event->key.repeat) { - imv_command_exec(g_state.cmds, "overlay"); + imv_command_exec(g_state.cmds, "overlay", NULL); } break; case SDLK_t: -- cgit v1.2.3 From 0357d51086ffa79e964c4b22895e5d6d72771f37 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sat, 15 Apr 2017 16:40:53 +0100 Subject: Fix compiler warning --- src/commands.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/commands.h') diff --git a/src/commands.h b/src/commands.h index 89f94a2..2d106d9 100644 --- a/src/commands.h +++ b/src/commands.h @@ -24,7 +24,7 @@ struct imv_commands { struct imv_list *command_list; }; -struct imv_commands *imv_commands_create(); +struct imv_commands *imv_commands_create(void); void imv_commands_free(struct imv_commands *cmds); void imv_command_register(struct imv_commands *cmds, const char *command, void (*handler)()); void imv_command_alias(struct imv_commands *cmds, const char *command, const char *alias); -- cgit v1.2.3