From 4a282da692fa7b8471c1399b30010de80448bda9 Mon Sep 17 00:00:00 2001 From: N-R-K <79544946+N-R-K@users.noreply.github.com> Date: Fri, 19 Nov 2021 11:49:42 +0600 Subject: [PATCH] use dedicated function to process key/button bindings (#166) Co-authored-by: Arthur Williams --- main.c | 44 ++++++++++++++++++++++---------------------- nsxiv.h | 9 ++------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/main.c b/main.c index 2643cad..a4af92b 100644 --- a/main.c +++ b/main.c @@ -596,9 +596,27 @@ end: redraw(); } -void on_keypress(XKeyEvent *kev) +bool process_bindings(const keymap_t *keys, unsigned int len, + KeySym ksym_or_button, unsigned int state) { unsigned int i; + bool dirty = false; + + for (i = 0; i < len; i++) { + if (keys[i].ksym_or_button == ksym_or_button && + MODMASK(keys[i].mask) == MODMASK(state) && + keys[i].cmd.func && + (keys[i].cmd.mode == MODE_ALL || keys[i].cmd.mode == mode)) + { + if (keys[i].cmd.func(keys[i].arg)) + dirty = true; + } + } + return dirty; +} + +void on_keypress(XKeyEvent *kev) +{ unsigned int sh = 0; KeySym ksym, shksym; char dummy, key; @@ -624,15 +642,8 @@ void on_keypress(XKeyEvent *kev) /* number prefix for commands */ prefix = prefix * 10 + (int) (key - '0'); return; - } else for (i = 0; i < ARRLEN(keys); i++) { - if (keys[i].ksym == ksym && - MODMASK(keys[i].mask | sh) == MODMASK(kev->state) && - keys[i].cmd.func && - (keys[i].cmd.mode == MODE_ALL || keys[i].cmd.mode == mode)) - { - if (keys[i].cmd.func(keys[i].arg)) - dirty = true; - } + } else { + dirty = process_bindings(keys, ARRLEN(keys), ksym, kev->state & ~sh); } if (dirty) redraw(); @@ -642,24 +653,13 @@ void on_keypress(XKeyEvent *kev) void on_buttonpress(XButtonEvent *bev) { int sel; - unsigned int i; bool dirty = false; static Time firstclick; if (mode == MODE_IMAGE) { set_timeout(reset_cursor, TO_CURSOR_HIDE, true); reset_cursor(); - - for (i = 0; i < ARRLEN(buttons); i++) { - if (buttons[i].button == bev->button && - MODMASK(buttons[i].mask) == MODMASK(bev->state) && - buttons[i].cmd.func && - (buttons[i].cmd.mode == MODE_ALL || buttons[i].cmd.mode == mode)) - { - if (buttons[i].cmd.func(buttons[i].arg)) - dirty = true; - } - } + dirty = process_bindings(buttons, ARRLEN(buttons), bev->button, bev->state); if (dirty) redraw(); } else { diff --git a/nsxiv.h b/nsxiv.h index e38460a..6c1f5f9 100644 --- a/nsxiv.h +++ b/nsxiv.h @@ -172,17 +172,12 @@ typedef struct { typedef struct { unsigned int mask; - KeySym ksym; + KeySym ksym_or_button; cmd_t cmd; arg_t arg; } keymap_t; -typedef struct { - unsigned int mask; - unsigned int button; - cmd_t cmd; - arg_t arg; -} button_t; +typedef keymap_t button_t; /* image.c */