From 12efa0e3b429675047cb2900d49e1f38afeb650b Mon Sep 17 00:00:00 2001 From: Arthur Williams Date: Sat, 18 Sep 2021 12:27:12 -0700 Subject: Add ability to bind arbitrary functions. Before all the predated commands where kept in an array and their indexes were used in bindings. This meant that users couldn't add their own functions from the config file. Now key/mouse bindings have been changed to to store the function ptr (wrapped in a cmd_t struct to also store the mode) directly instead. General cleanup done in this commit: Defined `MODE_ALL` instead of using magic number. For example, suppose one had bindings like: { 0, XK_q, g_quit, None }, { ShitMask, XK_q, {quit_err}, None } { ControlMask, XK_q, {quit_err, .mode=MODE_IMAGE}, None } The existing binding `q` has been left unchanged and is defined the same way. However, the new hypothetical binding `Shift-q` can be used to call the custom function quit_err in any mode (default). `Ctrl-q` on the other hand will be called only on image mode. Closes #50 --- main.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index a17e615..ffba285 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,7 @@ */ #include "nsxiv.h" +#include "commands.h" #define _MAPPINGS_CONFIG #include "config.h" @@ -613,10 +614,10 @@ void on_keypress(XKeyEvent *kev) } else for (i = 0; i < ARRLEN(keys); i++) { if (keys[i].ksym == ksym && MODMASK(keys[i].mask | sh) == MODMASK(kev->state) && - keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT && - (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode)) + keys[i].cmd.func && + (keys[i].cmd.mode == MODE_ALL || keys[i].cmd.mode == mode)) { - if (cmds[keys[i].cmd].func(keys[i].arg)) + if (keys[i].cmd.func(keys[i].arg)) dirty = true; } } @@ -638,10 +639,10 @@ void on_buttonpress(XButtonEvent *bev) for (i = 0; i < ARRLEN(buttons); i++) { if (buttons[i].button == bev->button && MODMASK(buttons[i].mask) == MODMASK(bev->state) && - buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT && - (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode)) + buttons[i].cmd.func && + (buttons[i].cmd.mode == MODE_ALL || buttons[i].cmd.mode == mode)) { - if (cmds[buttons[i].cmd].func(buttons[i].arg)) + if (buttons[i].cmd.func(buttons[i].arg)) dirty = true; } } @@ -778,7 +779,7 @@ void run(void) break; case ClientMessage: if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW]) - cmds[g_quit].func(0); + cg_quit(); break; case DestroyNotify: exit(EXIT_FAILURE); -- cgit v1.2.3