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 <explosion-mental@noreply.codeberg.org>
pull/365/head
NRK 2 years ago
parent 216f312578
commit 6578e6eb65

@ -50,12 +50,6 @@
(tv)->tv_usec += (t) % 1000 * 1000; \ (tv)->tv_usec += (t) % 1000 * 1000; \
} }
typedef struct {
struct timeval when;
bool active;
timeout_f handler;
} timeout_t;
typedef struct { typedef struct {
int err; int err;
char *cmd; char *cmd;
@ -98,12 +92,16 @@ static struct {
bool title_dirty; bool title_dirty;
static timeout_t timeouts[] = { static struct {
{ { 0, 0 }, false, redraw }, timeout_f handler;
{ { 0, 0 }, false, reset_cursor }, struct timeval when;
{ { 0, 0 }, false, slideshow }, bool active;
{ { 0, 0 }, false, animate }, } timeouts[] = {
{ { 0, 0 }, false, clear_resize }, { 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 */ /* drop user input events that occurred while running the key handler */
while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL)); while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE && changed) {
if (changed) { img_close(&img, true);
img_close(&img, true); load_image(fileidx);
load_image(fileidx); } else {
}
}
if (mode == MODE_THUMB || !changed)
open_info(); open_info();
}
free(oldst); free(oldst);
reset_cursor(); reset_cursor();
return true; return true;
@ -633,7 +629,7 @@ static bool process_bindings(const keymap_t *bindings, unsigned int len, KeySym
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (bindings[i].ksym_or_button == ksym_or_button && if (bindings[i].ksym_or_button == ksym_or_button &&
MODMASK(bindings[i].mask | implicit_mod) == MODMASK(state) && 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)) (bindings[i].cmd.mode == MODE_ALL || bindings[i].cmd.mode == mode))
{ {
if (bindings[i].cmd.func(bindings[i].arg)) if (bindings[i].cmd.func(bindings[i].arg))

@ -60,14 +60,8 @@ void* erealloc(void *ptr, size_t size)
char* estrdup(const char *s) char* estrdup(const char *s)
{ {
char *d;
size_t n = strlen(s) + 1; size_t n = strlen(s) + 1;
return memcpy(emalloc(n), s, n);
d = malloc(n);
if (d == NULL)
error(EXIT_FAILURE, errno, NULL);
memcpy(d, s, n);
return d;
} }
void error(int eval, int err, const char* fmt, ...) void error(int eval, int err, const char* fmt, ...)

@ -33,6 +33,7 @@
#if HAVE_LIBFONTS #if HAVE_LIBFONTS
#include "utf8.h" #include "utf8.h"
#define UTF8_PADDING 4 /* utf8_decode requires 4 bytes of zero padding */
static XftFont *font; static XftFont *font;
static double fontsize; static double fontsize;
#define TEXTWIDTH(win, text, len) \ #define TEXTWIDTH(win, text, len) \
@ -112,7 +113,7 @@ void win_init(win_t *win)
#if HAVE_LIBFONTS #if HAVE_LIBFONTS
const char *bar_fg, *bar_bg, *f; const char *bar_fg, *bar_bg, *f;
static char lbuf[512 + 3], rbuf[64 + 3]; static char lbuf[512 + UTF8_PADDING], rbuf[64 + UTF8_PADDING];
#endif #endif
memset(win, 0, sizeof(win_t)); memset(win, 0, sizeof(win_t));
@ -153,9 +154,8 @@ void win_init(win_t *win)
win->bar.l.buf = lbuf; win->bar.l.buf = lbuf;
win->bar.r.buf = rbuf; win->bar.r.buf = rbuf;
/* 3 padding bytes needed by utf8_decode */ win->bar.l.size = sizeof(lbuf) - UTF8_PADDING;
win->bar.l.size = sizeof(lbuf) - 3; win->bar.r.size = sizeof(rbuf) - UTF8_PADDING;
win->bar.r.size = sizeof(rbuf) - 3;
win->bar.h = options->hide_bar ? 0 : barheight; win->bar.h = options->hide_bar ? 0 : barheight;
win->bar.top = TOP_STATUSBAR; win->bar.top = TOP_STATUSBAR;
#endif /* HAVE_LIBFONTS */ #endif /* HAVE_LIBFONTS */
@ -388,9 +388,7 @@ void win_toggle_bar(win_t *win)
void win_clear(win_t *win) void win_clear(win_t *win)
{ {
win_env_t *e; win_env_t *e = &win->env;
e = &win->env;
if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) { if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
XFreePixmap(e->dpy, win->buf.pm); XFreePixmap(e->dpy, win->buf.pm);

Loading…
Cancel
Save