Krolyxon
Commit 6578e6e

Branches

Refs →

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/
Author
NRK
Date
2022-08-16 10:53 +0200
Parent
216f312
Commit
6578e6eb6533a6651bd88ecae7720aced88c8c2f

+22 −34

3 files changed

  1. M main.c +16 −20
  2. M util.c +1 −7
  3. M window.c +5 −7
M main.c View
@@ -50,12 +50,6 @@
5050 (tv)->tv_usec += (t) % 1000 * 1000; \
5151 }
5252
53−typedef struct {
54− struct timeval when;
55− bool active;
56− timeout_f handler;
57−} timeout_t;
58−
5953 typedef struct {
6054 int err;
6155 char *cmd;
@@ -98,12 +92,16 @@ static struct {
9892
9993 bool title_dirty;
10094
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 },
107105 };
108106
109107 /**************************
@@ -611,14 +609,12 @@ static bool run_key_handler(const char *key, unsigned int mask)
611609 /* drop user input events that occurred while running the key handler */
612610 while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
613611
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 {
621616 open_info();
617+ }
622618 free(oldst);
623619 reset_cursor();
624620 return true;
@@ -633,7 +629,7 @@ static bool process_bindings(const keymap_t *bindings, unsigned int len, KeySym
633629 for (i = 0; i < len; i++) {
634630 if (bindings[i].ksym_or_button == ksym_or_button &&
635631 MODMASK(bindings[i].mask | implicit_mod) == MODMASK(state) &&
636− bindings[i].cmd.func &&
632+ bindings[i].cmd.func != NULL &&
637633 (bindings[i].cmd.mode == MODE_ALL || bindings[i].cmd.mode == mode))
638634 {
639635 if (bindings[i].cmd.func(bindings[i].arg))
M util.c View
@@ -60,14 +60,8 @@ void* erealloc(void *ptr, size_t size)
6060
6161 char* estrdup(const char *s)
6262 {
63− char *d;
6463 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);
7165 }
7266
7367 void error(int eval, int err, const char* fmt, ...)
M window.c View
@@ -33,6 +33,7 @@
3333
3434 #if HAVE_LIBFONTS
3535 #include "utf8.h"
36+#define UTF8_PADDING 4 /* utf8_decode requires 4 bytes of zero padding */
3637 static XftFont *font;
3738 static double fontsize;
3839 #define TEXTWIDTH(win, text, len) \
@@ -112,7 +113,7 @@ void win_init(win_t *win)
112113 #if HAVE_LIBFONTS
113114 const char *bar_fg, *bar_bg, *f;
114115
115− static char lbuf[512 + 3], rbuf[64 + 3];
116+ static char lbuf[512 + UTF8_PADDING], rbuf[64 + UTF8_PADDING];
116117 #endif
117118
118119 memset(win, 0, sizeof(win_t));
@@ -153,9 +154,8 @@ void win_init(win_t *win)
153154
154155 win->bar.l.buf = lbuf;
155156 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;
159159 win->bar.h = options->hide_bar ? 0 : barheight;
160160 win->bar.top = TOP_STATUSBAR;
161161 #endif /* HAVE_LIBFONTS */
@@ -388,9 +388,7 @@ void win_toggle_bar(win_t *win)
388388
389389 void win_clear(win_t *win)
390390 {
391− win_env_t *e;
392−
393− e = &win->env;
391+ win_env_t *e = &win->env;
394392
395393 if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
396394 XFreePixmap(e->dpy, win->buf.pm);