Krolyxon
Commit e9a0096

Branches

Refs →

nsxive9a0096

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.
Author
NRK
Date
2022-06-01 16:47 +0600
Committer
N-R-K · 2022-06-02 08:09 +0000
Parent
810a965
Commit
e9a0096d6df602aefac5ce63b4bda2e1c85ed846

+30 −35

3 files changed

  1. M main.c +21 −22
  2. M nsxiv.h +1 −2
  3. M window.c +8 −11
M main.c View
@@ -233,35 +233,30 @@ static bool check_timeouts(int *t)
233233 return tmin > 0;
234234 }
235235
236−size_t get_win_title(unsigned char *buf, int len, bool init)
236+static size_t get_win_title(char *buf, size_t len)
237237 {
238238 char *argv[8];
239239 spawn_t pfd;
240240 char w[12] = "", h[12] = "", z[12] = "", fidx[12], fcnt[12];
241241 ssize_t n = -1;
242242
243− if (buf == NULL || len <= 0)
243+ if (wintitle.f.err || buf == NULL || len == 0)
244244 return 0;
245245
246− if (init) {
247− n = snprintf((char *)buf, len, "%s", options->res_name != NULL ?
248− options->res_name : "nsxiv");
249− } else if (!wintitle.f.err) {
250− if (mode == MODE_IMAGE) {
251− snprintf(w, ARRLEN(w), "%d", img.w);
252− snprintf(h, ARRLEN(h), "%d", img.h);
253− snprintf(z, ARRLEN(z), "%d", (int)(img.zoom * 100));
254− }
255− snprintf(fidx, ARRLEN(fidx), "%d", fileidx+1);
256− snprintf(fcnt, ARRLEN(fcnt), "%d", filecnt);
257− construct_argv(argv, ARRLEN(argv), wintitle.f.cmd, files[fileidx].path,
258− fidx, fcnt, w, h, z, NULL);
259− pfd = spawn(wintitle.f.cmd, argv, X_READ);
260− if (pfd.readfd >= 0) {
261− if ((n = read(pfd.readfd, buf, len-1)) > 0)
262− buf[n] = '\0';
263− close(pfd.readfd);
264− }
246+ if (mode == MODE_IMAGE) {
247+ snprintf(w, ARRLEN(w), "%d", img.w);
248+ snprintf(h, ARRLEN(h), "%d", img.h);
249+ snprintf(z, ARRLEN(z), "%d", (int)(img.zoom * 100));
250+ }
251+ snprintf(fidx, ARRLEN(fidx), "%d", fileidx+1);
252+ snprintf(fcnt, ARRLEN(fcnt), "%d", filecnt);
253+ construct_argv(argv, ARRLEN(argv), wintitle.f.cmd, files[fileidx].path,
254+ fidx, fcnt, w, h, z, NULL);
255+ pfd = spawn(wintitle.f.cmd, argv, X_READ);
256+ if (pfd.readfd >= 0) {
257+ if ((n = read(pfd.readfd, buf, len-1)) > 0)
258+ buf[n] = '\0';
259+ close(pfd.readfd);
265260 }
266261
267262 return MAX(0, n);
@@ -455,7 +450,11 @@ void redraw(void)
455450 }
456451 update_info();
457452 if (title_dirty) {
458− win_set_title(&win, false);
453+ size_t n;
454+ char buf[512];
455+
456+ if ((n = get_win_title(buf, sizeof(buf))) > 0)
457+ win_set_title(&win, buf, n);
459458 title_dirty = false;
460459 }
461460 win_draw(&win);
M nsxiv.h View
@@ -438,7 +438,7 @@ void win_toggle_bar(win_t*);
438438 void win_clear(win_t*);
439439 void win_draw(win_t*);
440440 void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
441−void win_set_title(win_t*, bool);
441+void win_set_title(win_t*, const char*, size_t);
442442 void win_set_cursor(win_t*, cursor_t);
443443 void win_cursor_pos(win_t*, int*, int*);
444444
@@ -454,7 +454,6 @@ void clear_resize(void);
454454 void remove_file(int, bool);
455455 void set_timeout(timeout_f, int, bool);
456456 void reset_timeout(timeout_f);
457−size_t get_win_title(unsigned char*, int, bool);
458457 void close_info(void);
459458 void open_info(void);
460459 void load_image(int);
M window.c View
@@ -288,7 +288,7 @@ void win_open(win_t *win)
288288 }
289289 free(icon_data);
290290
291− win_set_title(win, true);
291+ win_set_title(win, res_name, strlen(res_name));
292292 classhint.res_class = res_class;
293293 classhint.res_name = options->res_name != NULL ? options->res_name : res_name;
294294 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,
504504 XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
505505 }
506506
507−void win_set_title(win_t *win, bool init)
507+void win_set_title(win_t *win, const char *title, size_t len)
508508 {
509− size_t len, i;
510− unsigned char title[512];
511− int targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
512−
513− if ((len = get_win_title(title, ARRLEN(title), init)) > 0) {
514− for (i = 0; i < ARRLEN(targets); ++i) {
515− XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
516− atoms[ATOM_UTF8_STRING], 8, PropModeReplace, title, len);
517− }
509+ int i, targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
510+
511+ for (i = 0; i < ARRLEN(targets); ++i) {
512+ XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
513+ atoms[ATOM_UTF8_STRING], 8, PropModeReplace,
514+ (unsigned char *)title, len);
518515 }
519516 }
520517