nsxiv6578e6e
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/
+22 −34
3 files changed
| @@ -50,12 +50,6 @@ | |||
| 50 | 50 | (tv)->tv_usec += (t) % 1000 * 1000; \ | |
| 51 | 51 | } | |
| 52 | 52 | ||
| 53 | − | typedef struct { | |
| 54 | − | struct timeval when; | |
| 55 | − | bool active; | |
| 56 | − | timeout_f handler; | |
| 57 | − | } timeout_t; | |
| 58 | − | ||
| 59 | 53 | typedef struct { | |
| 60 | 54 | int err; | |
| 61 | 55 | char *cmd; | |
| @@ -98,12 +92,16 @@ static struct { | |||
| 98 | 92 | ||
| 99 | 93 | bool title_dirty; | |
| 100 | 94 | ||
| 101 | − | static timeout_t timeouts[] = { | |
| 102 | − | { { 0, 0 }, false, redraw }, | |
| 103 | − | { { 0, 0 }, false, reset_cursor }, | |
| 104 | − | { { 0, 0 }, false, slideshow }, | |
| 105 | − | { { 0, 0 }, false, animate }, | |
| 106 | − | { { 0, 0 }, false, clear_resize }, | |
| 95 | + | static struct { | |
| 96 | + | timeout_f handler; | |
| 97 | + | struct timeval when; | |
| 98 | + | bool active; | |
| 99 | + | } timeouts[] = { | |
| 100 | + | { redraw }, | |
| 101 | + | { reset_cursor }, | |
| 102 | + | { slideshow }, | |
| 103 | + | { animate }, | |
| 104 | + | { clear_resize }, | |
| 107 | 105 | }; | |
| 108 | 106 | ||
| 109 | 107 | /************************** | |
| @@ -611,14 +609,12 @@ static bool run_key_handler(const char *key, unsigned int mask) | |||
| 611 | 609 | /* drop user input events that occurred while running the key handler */ | |
| 612 | 610 | while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL)); | |
| 613 | 611 | ||
| 614 | − | if (mode == MODE_IMAGE) { | |
| 615 | − | if (changed) { | |
| 616 | − | img_close(&img, true); | |
| 617 | − | load_image(fileidx); | |
| 618 | − | } | |
| 619 | − | } | |
| 620 | − | if (mode == MODE_THUMB || !changed) | |
| 612 | + | if (mode == MODE_IMAGE && changed) { | |
| 613 | + | img_close(&img, true); | |
| 614 | + | load_image(fileidx); | |
| 615 | + | } else { | |
| 621 | 616 | open_info(); | |
| 617 | + | } | |
| 622 | 618 | free(oldst); | |
| 623 | 619 | reset_cursor(); | |
| 624 | 620 | return true; | |
| @@ -633,7 +629,7 @@ static bool process_bindings(const keymap_t *bindings, unsigned int len, KeySym | |||
| 633 | 629 | for (i = 0; i < len; i++) { | |
| 634 | 630 | if (bindings[i].ksym_or_button == ksym_or_button && | |
| 635 | 631 | MODMASK(bindings[i].mask | implicit_mod) == MODMASK(state) && | |
| 636 | − | bindings[i].cmd.func && | |
| 632 | + | bindings[i].cmd.func != NULL && | |
| 637 | 633 | (bindings[i].cmd.mode == MODE_ALL || bindings[i].cmd.mode == mode)) | |
| 638 | 634 | { | |
| 639 | 635 | if (bindings[i].cmd.func(bindings[i].arg)) | |
| @@ -60,14 +60,8 @@ void* erealloc(void *ptr, size_t size) | |||
| 60 | 60 | ||
| 61 | 61 | char* estrdup(const char *s) | |
| 62 | 62 | { | |
| 63 | − | char *d; | |
| 64 | 63 | size_t n = strlen(s) + 1; | |
| 65 | − | ||
| 66 | − | d = malloc(n); | |
| 67 | − | if (d == NULL) | |
| 68 | − | error(EXIT_FAILURE, errno, NULL); | |
| 69 | − | memcpy(d, s, n); | |
| 70 | − | return d; | |
| 64 | + | return memcpy(emalloc(n), s, n); | |
| 71 | 65 | } | |
| 72 | 66 | ||
| 73 | 67 | void error(int eval, int err, const char* fmt, ...) | |
| @@ -33,6 +33,7 @@ | |||
| 33 | 33 | ||
| 34 | 34 | #if HAVE_LIBFONTS | |
| 35 | 35 | #include "utf8.h" | |
| 36 | + | #define UTF8_PADDING 4 /* utf8_decode requires 4 bytes of zero padding */ | |
| 36 | 37 | static XftFont *font; | |
| 37 | 38 | static double fontsize; | |
| 38 | 39 | #define TEXTWIDTH(win, text, len) \ | |
| @@ -112,7 +113,7 @@ void win_init(win_t *win) | |||
| 112 | 113 | #if HAVE_LIBFONTS | |
| 113 | 114 | const char *bar_fg, *bar_bg, *f; | |
| 114 | 115 | ||
| 115 | − | static char lbuf[512 + 3], rbuf[64 + 3]; | |
| 116 | + | static char lbuf[512 + UTF8_PADDING], rbuf[64 + UTF8_PADDING]; | |
| 116 | 117 | #endif | |
| 117 | 118 | ||
| 118 | 119 | memset(win, 0, sizeof(win_t)); | |
| @@ -153,9 +154,8 @@ void win_init(win_t *win) | |||
| 153 | 154 | ||
| 154 | 155 | win->bar.l.buf = lbuf; | |
| 155 | 156 | win->bar.r.buf = rbuf; | |
| 156 | − | /* 3 padding bytes needed by utf8_decode */ | |
| 157 | − | win->bar.l.size = sizeof(lbuf) - 3; | |
| 158 | − | win->bar.r.size = sizeof(rbuf) - 3; | |
| 157 | + | win->bar.l.size = sizeof(lbuf) - UTF8_PADDING; | |
| 158 | + | win->bar.r.size = sizeof(rbuf) - UTF8_PADDING; | |
| 159 | 159 | win->bar.h = options->hide_bar ? 0 : barheight; | |
| 160 | 160 | win->bar.top = TOP_STATUSBAR; | |
| 161 | 161 | #endif /* HAVE_LIBFONTS */ | |
| @@ -388,9 +388,7 @@ void win_toggle_bar(win_t *win) | |||
| 388 | 388 | ||
| 389 | 389 | void win_clear(win_t *win) | |
| 390 | 390 | { | |
| 391 | − | win_env_t *e; | |
| 392 | − | ||
| 393 | − | e = &win->env; | |
| 391 | + | win_env_t *e = &win->env; | |
| 394 | 392 | ||
| 395 | 393 | if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) { | |
| 396 | 394 | XFreePixmap(e->dpy, win->buf.pm); | |