diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2017-04-14 16:44:07 +0100 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2017-04-14 16:44:07 +0100 |
commit | 50759fb279b38c5db7cacf88206188b827f564b2 (patch) | |
tree | 06d773eb726db3f3b3a89383af4d891897fadee3 | |
parent | 93f68f6b4c2212a80492274b9aea9a9fb43d7ec4 (diff) | |
download | imv-50759fb279b38c5db7cacf88206188b827f564b2.tar.gz |
Let's not have imv_commands use hidden globals
-rw-r--r-- | src/commands.c | 55 | ||||
-rw-r--r-- | src/commands.h | 14 | ||||
-rw-r--r-- | src/main.c | 55 |
3 files changed, 71 insertions, 53 deletions
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 @@ -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); |