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.
This commit is contained in:
NRK 2022-06-01 16:47:17 +06:00 committed by N-R-K
parent 810a9651a3
commit e9a0096d6d
3 changed files with 29 additions and 34 deletions

43
main.c
View File

@ -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);

View File

@ -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);

View File

@ -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);
}
}