nsxiv156a537
set title based on prefix and suffix (#23)
+74 −7
7 files changed
- M config.def.h +15 −0
- M main.c +2 −0
- M options.c +16 −3
- M sxiv.1 +15 −0
- M sxiv.h +8 −0
- M thumbs.c +2 −0
- M window.c +16 −4
| @@ -12,6 +12,21 @@ enum { | |||
| 12 | 12 | */ | |
| 13 | 13 | ||
| 14 | 14 | #endif | |
| 15 | + | ||
| 16 | + | #ifdef _TITLE_CONFIG | |
| 17 | + | ||
| 18 | + | /* default title prefix */ | |
| 19 | + | static const char *TITLE_PREFIX = "sxiv - "; | |
| 20 | + | ||
| 21 | + | /* default title suffixmode, available options are: | |
| 22 | + | * SUFFIX_EMPTY | |
| 23 | + | * SUFFIX_BASENAME | |
| 24 | + | * SUFFIX_FULLPATH | |
| 25 | + | */ | |
| 26 | + | static const suffixmode_t TITLE_SUFFIXMODE = SUFFIX_BASENAME; | |
| 27 | + | ||
| 28 | + | #endif | |
| 29 | + | ||
| 15 | 30 | #ifdef _IMAGE_CONFIG | |
| 16 | 31 | ||
| 17 | 32 | /* levels (in percent) to use when zooming via '-' and '+': | |
| @@ -314,6 +314,7 @@ void load_image(int new) | |||
| 314 | 314 | close_info(); | |
| 315 | 315 | open_info(); | |
| 316 | 316 | arl_setup(&arl, files[fileidx].path); | |
| 317 | + | win_set_title(&win, files[fileidx].path); | |
| 317 | 318 | ||
| 318 | 319 | if (img.multi.cnt > 0 && img.multi.animate) | |
| 319 | 320 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); | |
| @@ -939,6 +940,7 @@ int main(int argc, char **argv) | |||
| 939 | 940 | load_image(fileidx); | |
| 940 | 941 | } | |
| 941 | 942 | win_open(&win); | |
| 943 | + | win_set_title(&win, files[fileidx].path); | |
| 942 | 944 | win_set_cursor(&win, CURSOR_WATCH); | |
| 943 | 945 | ||
| 944 | 946 | atexit(cleanup); | |
| @@ -18,6 +18,7 @@ | |||
| 18 | 18 | ||
| 19 | 19 | #include "sxiv.h" | |
| 20 | 20 | #define _IMAGE_CONFIG | |
| 21 | + | #define _TITLE_CONFIG | |
| 21 | 22 | #include "config.h" | |
| 22 | 23 | #include "version.h" | |
| 23 | 24 | ||
| @@ -31,8 +32,8 @@ const opt_t *options = (const opt_t*) &_options; | |||
| 31 | 32 | void print_usage(void) | |
| 32 | 33 | { | |
| 33 | 34 | printf("usage: sxiv [-abcfhiopqrtvZ] [-A FRAMERATE] [-e WID] [-G GAMMA] " | |
| 34 | − | "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] " | |
| 35 | − | "FILES...\n"); | |
| 35 | + | "[-g GEOMETRY] [-N NAME] [-T TITLE] [-n NUM] [-S DELAY] [-s MODE] " | |
| 36 | + | "[-z ZOOM] FILES...\n"); | |
| 36 | 37 | } | |
| 37 | 38 | ||
| 38 | 39 | void print_version(void) | |
| @@ -66,13 +67,15 @@ void parse_options(int argc, char **argv) | |||
| 66 | 67 | _options.hide_bar = false; | |
| 67 | 68 | _options.geometry = NULL; | |
| 68 | 69 | _options.res_name = NULL; | |
| 70 | + | _options.title_prefix = TITLE_PREFIX; | |
| 71 | + | _options.title_suffixmode = TITLE_SUFFIXMODE; | |
| 69 | 72 | ||
| 70 | 73 | _options.quiet = false; | |
| 71 | 74 | _options.thumb_mode = false; | |
| 72 | 75 | _options.clean_cache = false; | |
| 73 | 76 | _options.private_mode = false; | |
| 74 | 77 | ||
| 75 | − | while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:tvZz:")) != -1) { | |
| 78 | + | while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:T:tvZz:")) != -1) { | |
| 76 | 79 | switch (opt) { | |
| 77 | 80 | case '?': | |
| 78 | 81 | print_usage(); | |
| @@ -149,6 +152,16 @@ void parse_options(int argc, char **argv) | |||
| 149 | 152 | error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg); | |
| 150 | 153 | _options.scalemode = s - scalemodes; | |
| 151 | 154 | break; | |
| 155 | + | case 'T': | |
| 156 | + | if ((s = strrchr(optarg, ':')) != NULL) { | |
| 157 | + | *s = '\0'; | |
| 158 | + | n = strtol(++s, &end, 0); | |
| 159 | + | if (*end != '\0' || n < SUFFIX_EMPTY || n > SUFFIX_FULLPATH) | |
| 160 | + | error(EXIT_FAILURE, 0, "Invalid argument for option -T suffixmode: %s", s); | |
| 161 | + | _options.title_suffixmode = n; | |
| 162 | + | } | |
| 163 | + | _options.title_prefix = optarg; | |
| 164 | + | break; | |
| 152 | 165 | case 't': | |
| 153 | 166 | _options.thumb_mode = true; | |
| 154 | 167 | break; | |
| @@ -14,6 +14,8 @@ sxiv \- Simple X Image Viewer | |||
| 14 | 14 | .IR GEOMETRY ] | |
| 15 | 15 | .RB [ \-N | |
| 16 | 16 | .IR NAME ] | |
| 17 | + | .RB [ \-T | |
| 18 | + | .IR TITLE ] | |
| 17 | 19 | .RB [ \-n | |
| 18 | 20 | .IR NUM ] | |
| 19 | 21 | .RB [ \-S | |
| @@ -64,6 +66,19 @@ more information on GEOMETRY argument. | |||
| 64 | 66 | .BI "\-N " NAME | |
| 65 | 67 | Set the resource name of sxiv's X window to NAME. | |
| 66 | 68 | .TP | |
| 69 | + | .BI "\-T " TITLE | |
| 70 | + | Set the window title to TITLE. Use the format `prefix:suffixmode'. Any string | |
| 71 | + | literal is accepted for prefix, and the format of suffixmode is: | |
| 72 | + | ||
| 73 | + | .EX | |
| 74 | + | Value Format | |
| 75 | + | 0 Empty | |
| 76 | + | 1 Basename of file | |
| 77 | + | 2 Full path to file | |
| 78 | + | .EE | |
| 79 | + | ||
| 80 | + | By defualt, prefix is set to "sxiv - " and suffixmode is set to 1 (basename). | |
| 81 | + | .TP | |
| 67 | 82 | .BI "\-n " NUM | |
| 68 | 83 | Start at picture number NUM. | |
| 69 | 84 | .TP | |
| @@ -116,6 +116,12 @@ typedef enum { | |||
| 116 | 116 | FF_TN_INIT = 4 | |
| 117 | 117 | } fileflags_t; | |
| 118 | 118 | ||
| 119 | + | typedef enum { | |
| 120 | + | SUFFIX_EMPTY, | |
| 121 | + | SUFFIX_BASENAME, | |
| 122 | + | SUFFIX_FULLPATH, | |
| 123 | + | } suffixmode_t; | |
| 124 | + | ||
| 119 | 125 | typedef struct { | |
| 120 | 126 | const char *name; /* as given by user */ | |
| 121 | 127 | const char *path; /* always absolute */ | |
| @@ -280,6 +286,8 @@ struct opt { | |||
| 280 | 286 | long embed; | |
| 281 | 287 | char *geometry; | |
| 282 | 288 | char *res_name; | |
| 289 | + | const char *title_prefix; | |
| 290 | + | suffixmode_t title_suffixmode; | |
| 283 | 291 | ||
| 284 | 292 | /* misc flags: */ | |
| 285 | 293 | bool quiet; | |
| @@ -35,6 +35,7 @@ void exif_auto_orientate(const fileinfo_t*); | |||
| 35 | 35 | Imlib_Image img_open(const fileinfo_t*); | |
| 36 | 36 | ||
| 37 | 37 | static char *cache_dir; | |
| 38 | + | extern const int fileidx; | |
| 38 | 39 | ||
| 39 | 40 | char* tns_cache_filepath(const char *filepath) | |
| 40 | 41 | { | |
| @@ -531,6 +532,7 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt) | |||
| 531 | 532 | if (!tns->dirty) | |
| 532 | 533 | tns_highlight(tns, *tns->sel, true); | |
| 533 | 534 | } | |
| 535 | + | win_set_title(tns->win, tns->files[fileidx].path); | |
| 534 | 536 | return *tns->sel != old; | |
| 535 | 537 | } | |
| 536 | 538 | ||
| @@ -276,7 +276,9 @@ void win_open(win_t *win) | |||
| 276 | 276 | } | |
| 277 | 277 | free(icon_data); | |
| 278 | 278 | ||
| 279 | − | win_set_title(win, "sxiv"); | |
| 279 | + | /* These two atoms won't change and thus only need to be set once. */ | |
| 280 | + | XStoreName(win->env.dpy, win->xwin, "sxiv"); | |
| 281 | + | XSetIconName(win->env.dpy, win->xwin, "sxiv"); | |
| 280 | 282 | ||
| 281 | 283 | classhint.res_class = RES_CLASS; | |
| 282 | 284 | classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv"; | |
| @@ -486,10 +488,20 @@ void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw, | |||
| 486 | 488 | XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h); | |
| 487 | 489 | } | |
| 488 | 490 | ||
| 489 | − | void win_set_title(win_t *win, const char *title) | |
| 491 | + | void win_set_title(win_t *win, const char *path) | |
| 490 | 492 | { | |
| 491 | − | XStoreName(win->env.dpy, win->xwin, title); | |
| 492 | − | XSetIconName(win->env.dpy, win->xwin, title); | |
| 493 | + | const unsigned int title_max = strlen(path) + strlen(options->title_prefix) + 1; | |
| 494 | + | char title[title_max]; | |
| 495 | + | const char *basename = strrchr(path, '/') + 1; | |
| 496 | + | ||
| 497 | + | /* Return if window is not ready yet */ | |
| 498 | + | if (win->xwin == None) | |
| 499 | + | return; | |
| 500 | + | ||
| 501 | + | snprintf(title, title_max, "%s%s", options->title_prefix, | |
| 502 | + | (options->title_suffixmode == SUFFIX_BASENAME) ? basename : path); | |
| 503 | + | if (options->title_suffixmode == SUFFIX_EMPTY) | |
| 504 | + | *(title+strlen(options->title_prefix)) = '\0'; | |
| 493 | 505 | ||
| 494 | 506 | XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME], | |
| 495 | 507 | XInternAtom(win->env.dpy, "UTF8_STRING", False), 8, | |