summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorArthur Williams <taaparthur@gmail.com>2021-09-18 21:27:12 +0200
committerN-R-K <79544946+N-R-K@users.noreply.github.com>2021-10-13 02:05:00 +0200
commit12efa0e3b429675047cb2900d49e1f38afeb650b (patch)
tree31a5e934aeac55b14f26a89b3333921bb985e318 /main.c
parent5c6947c1c6df76ab8438ac27377559da79ab1eab (diff)
downloadnsxiv-12efa0e3b429675047cb2900d49e1f38afeb650b.tar.zst
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
Diffstat (limited to 'main.c')
-rw-r--r--main.c15
1 files changed, 8 insertions, 7 deletions
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);