From e9a0096d6df602aefac5ce63b4bda2e1c85ed846 Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 1 Jun 2022 16:47:17 +0600 Subject: [PATCH] code-style: simplify window title related code instead of dancing around with some `init` parameter, directly give `win_set_title()` what it needs. `get_win_title()` now also does *just* what the name says. this simplifies things quite a bit and the functions now do what their name implies more closely instead of doing some `init` dance internally. --- main.c | 43 +++++++++++++++++++++---------------------- nsxiv.h | 3 +-- window.c | 17 +++++++---------- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/main.c b/main.c index 7c7b436..f148250 100644 --- a/main.c +++ b/main.c @@ -233,35 +233,30 @@ static bool check_timeouts(int *t) return tmin > 0; } -size_t get_win_title(unsigned char *buf, int len, bool init) +static size_t get_win_title(char *buf, size_t len) { char *argv[8]; spawn_t pfd; char w[12] = "", h[12] = "", z[12] = "", fidx[12], fcnt[12]; ssize_t n = -1; - if (buf == NULL || len <= 0) + if (wintitle.f.err || buf == NULL || len == 0) return 0; - if (init) { - n = snprintf((char *)buf, len, "%s", options->res_name != NULL ? - options->res_name : "nsxiv"); - } else if (!wintitle.f.err) { - if (mode == MODE_IMAGE) { - snprintf(w, ARRLEN(w), "%d", img.w); - snprintf(h, ARRLEN(h), "%d", img.h); - snprintf(z, ARRLEN(z), "%d", (int)(img.zoom * 100)); - } - snprintf(fidx, ARRLEN(fidx), "%d", fileidx+1); - snprintf(fcnt, ARRLEN(fcnt), "%d", filecnt); - construct_argv(argv, ARRLEN(argv), wintitle.f.cmd, files[fileidx].path, - fidx, fcnt, w, h, z, NULL); - pfd = spawn(wintitle.f.cmd, argv, X_READ); - if (pfd.readfd >= 0) { - if ((n = read(pfd.readfd, buf, len-1)) > 0) - buf[n] = '\0'; - close(pfd.readfd); - } + if (mode == MODE_IMAGE) { + snprintf(w, ARRLEN(w), "%d", img.w); + snprintf(h, ARRLEN(h), "%d", img.h); + snprintf(z, ARRLEN(z), "%d", (int)(img.zoom * 100)); + } + snprintf(fidx, ARRLEN(fidx), "%d", fileidx+1); + snprintf(fcnt, ARRLEN(fcnt), "%d", filecnt); + construct_argv(argv, ARRLEN(argv), wintitle.f.cmd, files[fileidx].path, + fidx, fcnt, w, h, z, NULL); + pfd = spawn(wintitle.f.cmd, argv, X_READ); + if (pfd.readfd >= 0) { + if ((n = read(pfd.readfd, buf, len-1)) > 0) + buf[n] = '\0'; + close(pfd.readfd); } return MAX(0, n); @@ -455,7 +450,11 @@ void redraw(void) } update_info(); if (title_dirty) { - win_set_title(&win, false); + size_t n; + char buf[512]; + + if ((n = get_win_title(buf, sizeof(buf))) > 0) + win_set_title(&win, buf, n); title_dirty = false; } win_draw(&win); diff --git a/nsxiv.h b/nsxiv.h index c781aa5..38e1d7b 100644 --- a/nsxiv.h +++ b/nsxiv.h @@ -438,7 +438,7 @@ void win_toggle_bar(win_t*); void win_clear(win_t*); void win_draw(win_t*); void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long); -void win_set_title(win_t*, bool); +void win_set_title(win_t*, const char*, size_t); void win_set_cursor(win_t*, cursor_t); void win_cursor_pos(win_t*, int*, int*); @@ -454,7 +454,6 @@ void clear_resize(void); void remove_file(int, bool); void set_timeout(timeout_f, int, bool); void reset_timeout(timeout_f); -size_t get_win_title(unsigned char*, int, bool); void close_info(void); void open_info(void); void load_image(int); diff --git a/window.c b/window.c index 1e1ac59..e13dd83 100644 --- a/window.c +++ b/window.c @@ -288,7 +288,7 @@ void win_open(win_t *win) } free(icon_data); - win_set_title(win, true); + win_set_title(win, res_name, strlen(res_name)); classhint.res_class = res_class; classhint.res_name = options->res_name != NULL ? options->res_name : res_name; XSetClassHint(e->dpy, win->xwin, &classhint); @@ -504,17 +504,14 @@ void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw, XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h); } -void win_set_title(win_t *win, bool init) +void win_set_title(win_t *win, const char *title, size_t len) { - size_t len, i; - unsigned char title[512]; - int targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME }; + int i, targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME }; - if ((len = get_win_title(title, ARRLEN(title), init)) > 0) { - for (i = 0; i < ARRLEN(targets); ++i) { - XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]], - atoms[ATOM_UTF8_STRING], 8, PropModeReplace, title, len); - } + for (i = 0; i < ARRLEN(targets); ++i) { + XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]], + atoms[ATOM_UTF8_STRING], 8, PropModeReplace, + (unsigned char *)title, len); } }