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.
+30 −35
3 files changed
| @@ -233,35 +233,30 @@ static bool check_timeouts(int *t) | |||
| 233 | 233 | return tmin > 0; | |
| 234 | 234 | } | |
| 235 | 235 | ||
| 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) | |
| 237 | 237 | { | |
| 238 | 238 | char *argv[8]; | |
| 239 | 239 | spawn_t pfd; | |
| 240 | 240 | char w[12] = "", h[12] = "", z[12] = "", fidx[12], fcnt[12]; | |
| 241 | 241 | ssize_t n = -1; | |
| 242 | 242 | ||
| 243 | − | if (buf == NULL || len <= 0) | |
| 243 | + | if (wintitle.f.err || buf == NULL || len == 0) | |
| 244 | 244 | return 0; | |
| 245 | 245 | ||
| 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); | |
| 265 | 260 | } | |
| 266 | 261 | ||
| 267 | 262 | return MAX(0, n); | |
| @@ -455,7 +450,11 @@ void redraw(void) | |||
| 455 | 450 | } | |
| 456 | 451 | update_info(); | |
| 457 | 452 | 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); | |
| 459 | 458 | title_dirty = false; | |
| 460 | 459 | } | |
| 461 | 460 | win_draw(&win); | |
| @@ -438,7 +438,7 @@ void win_toggle_bar(win_t*); | |||
| 438 | 438 | void win_clear(win_t*); | |
| 439 | 439 | void win_draw(win_t*); | |
| 440 | 440 | 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); | |
| 442 | 442 | void win_set_cursor(win_t*, cursor_t); | |
| 443 | 443 | void win_cursor_pos(win_t*, int*, int*); | |
| 444 | 444 | ||
| @@ -454,7 +454,6 @@ void clear_resize(void); | |||
| 454 | 454 | void remove_file(int, bool); | |
| 455 | 455 | void set_timeout(timeout_f, int, bool); | |
| 456 | 456 | void reset_timeout(timeout_f); | |
| 457 | − | size_t get_win_title(unsigned char*, int, bool); | |
| 458 | 457 | void close_info(void); | |
| 459 | 458 | void open_info(void); | |
| 460 | 459 | void load_image(int); | |
| @@ -288,7 +288,7 @@ void win_open(win_t *win) | |||
| 288 | 288 | } | |
| 289 | 289 | free(icon_data); | |
| 290 | 290 | ||
| 291 | − | win_set_title(win, true); | |
| 291 | + | win_set_title(win, res_name, strlen(res_name)); | |
| 292 | 292 | classhint.res_class = res_class; | |
| 293 | 293 | classhint.res_name = options->res_name != NULL ? options->res_name : res_name; | |
| 294 | 294 | 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, | |||
| 504 | 504 | XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h); | |
| 505 | 505 | } | |
| 506 | 506 | ||
| 507 | − | void win_set_title(win_t *win, bool init) | |
| 507 | + | void win_set_title(win_t *win, const char *title, size_t len) | |
| 508 | 508 | { | |
| 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); | |
| 518 | 515 | } | |
| 519 | 516 | } | |
| 520 | 517 | ||