use dedicated function to process key/button bindings (#166)

Co-authored-by: Arthur Williams <taaparthur@gmail.com>
This commit is contained in:
N-R-K 2021-11-19 11:49:42 +06:00 committed by GitHub
parent 8f6ba7b868
commit 4a282da692
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 29 deletions

44
main.c
View file

@ -596,9 +596,27 @@ end:
redraw(); 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; 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; unsigned int sh = 0;
KeySym ksym, shksym; KeySym ksym, shksym;
char dummy, key; char dummy, key;
@ -624,15 +642,8 @@ void on_keypress(XKeyEvent *kev)
/* number prefix for commands */ /* number prefix for commands */
prefix = prefix * 10 + (int) (key - '0'); prefix = prefix * 10 + (int) (key - '0');
return; return;
} else for (i = 0; i < ARRLEN(keys); i++) { } else {
if (keys[i].ksym == ksym && dirty = process_bindings(keys, ARRLEN(keys), ksym, kev->state & ~sh);
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;
}
} }
if (dirty) if (dirty)
redraw(); redraw();
@ -642,24 +653,13 @@ void on_keypress(XKeyEvent *kev)
void on_buttonpress(XButtonEvent *bev) void on_buttonpress(XButtonEvent *bev)
{ {
int sel; int sel;
unsigned int i;
bool dirty = false; bool dirty = false;
static Time firstclick; static Time firstclick;
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
set_timeout(reset_cursor, TO_CURSOR_HIDE, true); set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
reset_cursor(); reset_cursor();
dirty = process_bindings(buttons, ARRLEN(buttons), bev->button, bev->state);
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;
}
}
if (dirty) if (dirty)
redraw(); redraw();
} else { } else {

View file

@ -172,17 +172,12 @@ typedef struct {
typedef struct { typedef struct {
unsigned int mask; unsigned int mask;
KeySym ksym; KeySym ksym_or_button;
cmd_t cmd; cmd_t cmd;
arg_t arg; arg_t arg;
} keymap_t; } keymap_t;
typedef struct { typedef keymap_t button_t;
unsigned int mask;
unsigned int button;
cmd_t cmd;
arg_t arg;
} button_t;
/* image.c */ /* image.c */