From 6578e6eb6533a6651bd88ecae7720aced88c8c2f Mon Sep 17 00:00:00 2001 From: NRK Date: Tue, 16 Aug 2022 10:53:05 +0200 Subject: code-style: various cleanups (#356) * run_key_handler: make the logic easier to follow * remove timeout_t the typedef is not needed. inline the declaration similar to the other static structs. * simplify estrdup reuse emalloc, instead of calling malloc and null-checking. * win_clear: initialize `e` right away * process_bindings: explicitly check against NULL most pointer checks in the codebase do explicit check. * use a named constant instead of magic number also changes the padding from 3 to 4 bytes according to [0]. but i couldn't find any situtation where this mattered, so perhaps the current padding is enough. but doesn't hurt adding one more byte. [0]: https://nullprogram.com/blog/2017/10/06/ Reviewed-on: https://codeberg.org/nsxiv/nsxiv/pulls/356 Reviewed-by: explosion-mental --- main.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 12cf404..02128a7 100644 --- a/main.c +++ b/main.c @@ -50,12 +50,6 @@ (tv)->tv_usec += (t) % 1000 * 1000; \ } -typedef struct { - struct timeval when; - bool active; - timeout_f handler; -} timeout_t; - typedef struct { int err; char *cmd; @@ -98,12 +92,16 @@ static struct { bool title_dirty; -static timeout_t timeouts[] = { - { { 0, 0 }, false, redraw }, - { { 0, 0 }, false, reset_cursor }, - { { 0, 0 }, false, slideshow }, - { { 0, 0 }, false, animate }, - { { 0, 0 }, false, clear_resize }, +static struct { + timeout_f handler; + struct timeval when; + bool active; +} timeouts[] = { + { redraw }, + { reset_cursor }, + { slideshow }, + { animate }, + { clear_resize }, }; /************************** @@ -611,14 +609,12 @@ static bool run_key_handler(const char *key, unsigned int mask) /* drop user input events that occurred while running the key handler */ while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL)); - if (mode == MODE_IMAGE) { - if (changed) { - img_close(&img, true); - load_image(fileidx); - } - } - if (mode == MODE_THUMB || !changed) + if (mode == MODE_IMAGE && changed) { + img_close(&img, true); + load_image(fileidx); + } else { open_info(); + } free(oldst); reset_cursor(); return true; @@ -633,7 +629,7 @@ static bool process_bindings(const keymap_t *bindings, unsigned int len, KeySym for (i = 0; i < len; i++) { if (bindings[i].ksym_or_button == ksym_or_button && MODMASK(bindings[i].mask | implicit_mod) == MODMASK(state) && - bindings[i].cmd.func && + bindings[i].cmd.func != NULL && (bindings[i].cmd.mode == MODE_ALL || bindings[i].cmd.mode == mode)) { if (bindings[i].cmd.func(bindings[i].arg)) -- cgit v1.2.3-70-g09d2