nsxivaa6ccf4
Merge branch 'userinfo'
+218 −155
8 files changed
- M Makefile +6 −2
- M README.md +2 −2
- A image-info +17 −0
- M main.c +90 −55
- M sxiv.1 +16 −14
- M thumbs.c +7 −5
- M window.c +72 −69
- M window.h +8 −8
| @@ -1,4 +1,4 @@ | |||
| 1 | − | VERSION = git-20130112 | |
| 1 | + | VERSION = git-20130127 | |
| 2 | 2 | ||
| 3 | 3 | PREFIX = /usr/local | |
| 4 | 4 | MANPREFIX = $(PREFIX)/share/man | |
| @@ -32,9 +32,13 @@ install: all | |||
| 32 | 32 | cp sxiv $(DESTDIR)$(PREFIX)/bin/ | |
| 33 | 33 | chmod 755 $(DESTDIR)$(PREFIX)/bin/sxiv | |
| 34 | 34 | mkdir -p $(DESTDIR)$(MANPREFIX)/man1 | |
| 35 | − | sed "s/VERSION/$(VERSION)/g" sxiv.1 > $(DESTDIR)$(MANPREFIX)/man1/sxiv.1 | |
| 35 | + | sed "s!PREFIX!$(PREFIX)!g; s!VERSION!$(VERSION)!g" sxiv.1 > $(DESTDIR)$(MANPREFIX)/man1/sxiv.1 | |
| 36 | 36 | chmod 644 $(DESTDIR)$(MANPREFIX)/man1/sxiv.1 | |
| 37 | + | mkdir -p $(DESTDIR)$(PREFIX)/share/sxiv/exec | |
| 38 | + | cp image-info $(DESTDIR)$(PREFIX)/share/sxiv/exec/image-info | |
| 39 | + | chmod 755 $(DESTDIR)$(PREFIX)/share/sxiv/exec/image-info | |
| 37 | 40 | ||
| 38 | 41 | uninstall: | |
| 39 | 42 | rm -f $(DESTDIR)$(PREFIX)/bin/sxiv | |
| 40 | 43 | rm -f $(DESTDIR)$(MANPREFIX)/man1/sxiv.1 | |
| 44 | + | rm -rf $(DESTDIR)$(PREFIX)/share/sxiv | |
| @@ -1,7 +1,7 @@ | |||
| 1 | 1 | sxiv | |
| 2 | 2 | ==== | |
| 3 | 3 | ||
| 4 | − | **Simple (or small or suckless) X Image Viewer** | |
| 4 | + | **Simple X Image Viewer** | |
| 5 | 5 | ||
| 6 | 6 | sxiv is an alternative to feh and qiv. Its only dependencies besides xlib are | |
| 7 | 7 | imlib2 and giflib. The primary goal for writing sxiv is to create an image | |
| @@ -20,7 +20,7 @@ Features | |||
| 20 | 20 | * Ability to cache thumbnails for fast re-loading | |
| 21 | 21 | * Basic support for multi-frame images | |
| 22 | 22 | * Load all frames from GIF files and play GIF animations | |
| 23 | − | * Display image information in window title | |
| 23 | + | * Display image information in status bar | |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | 26 | Screenshots | |
| @@ -0,0 +1,17 @@ | |||
| 1 | + | #!/bin/sh | |
| 2 | + | ||
| 3 | + | # Example for ~/.sxiv/exec/image-info | |
| 4 | + | # Called by sxiv(1) whenever an image gets loaded, | |
| 5 | + | # with the name of the image file as its first argument. | |
| 6 | + | # The output is displayed in sxiv's status bar. | |
| 7 | + | ||
| 8 | + | filename=$(basename "$1") | |
| 9 | + | filesize=$(du -h "$1" | cut -f 1) | |
| 10 | + | ||
| 11 | + | geometry=$(identify -format '%wx%h' "$1") | |
| 12 | + | ||
| 13 | + | tags=$(exiv2 -q pr -pi "$1" | awk '$1~"Keywords" { printf("%s,", $4); }') | |
| 14 | + | tags=${tags:+|}${tags%,} | |
| 15 | + | ||
| 16 | + | echo "[$filesize|$geometry$tags] $filename" | |
| 17 | + | ||
| @@ -39,8 +39,10 @@ | |||
| 39 | 39 | #include "config.h" | |
| 40 | 40 | ||
| 41 | 41 | enum { | |
| 42 | − | INFO_STR_LEN = 256, | |
| 43 | − | FILENAME_CNT = 1024 | |
| 42 | + | BAR_L_LEN = 512, | |
| 43 | + | BAR_R_LEN = 64, | |
| 44 | + | FILENAME_CNT = 1024, | |
| 45 | + | TITLE_LEN = 256 | |
| 44 | 46 | }; | |
| 45 | 47 | ||
| 46 | 48 | typedef struct { | |
| @@ -63,15 +65,18 @@ win_t win; | |||
| 63 | 65 | fileinfo_t *files; | |
| 64 | 66 | int filecnt, fileidx; | |
| 65 | 67 | int alternate; | |
| 66 | − | size_t filesize; | |
| 67 | 68 | ||
| 68 | 69 | int prefix; | |
| 69 | 70 | ||
| 70 | 71 | bool resized = false; | |
| 71 | 72 | ||
| 72 | − | char win_bar_l[INFO_STR_LEN]; | |
| 73 | − | char win_bar_r[INFO_STR_LEN]; | |
| 74 | − | char win_title[INFO_STR_LEN]; | |
| 73 | + | const char * const INFO_SCRIPT = ".sxiv/exec/image-info"; | |
| 74 | + | char *info_script; | |
| 75 | + | ||
| 76 | + | struct { | |
| 77 | + | char l[BAR_L_LEN]; | |
| 78 | + | char r[BAR_R_LEN]; | |
| 79 | + | } bar; | |
| 75 | 80 | ||
| 76 | 81 | timeout_t timeouts[] = { | |
| 77 | 82 | { { 0, 0 }, false, redraw }, | |
| @@ -202,9 +207,37 @@ bool check_timeouts(struct timeval *t) { | |||
| 202 | 207 | return tmin > 0; | |
| 203 | 208 | } | |
| 204 | 209 | ||
| 205 | − | void load_image(int new) { | |
| 206 | − | struct stat fstats; | |
| 210 | + | void read_info(void) { | |
| 211 | + | char cmd[4096]; | |
| 212 | + | FILE *outp; | |
| 213 | + | int c, i = 0, n = sizeof(bar.l) - 1; | |
| 214 | + | bool lastsep = false; | |
| 215 | + | ||
| 216 | + | if (info_script != NULL) { | |
| 217 | + | snprintf(cmd, sizeof(cmd), "%s \"%s\"", info_script, files[fileidx].name); | |
| 218 | + | outp = popen(cmd, "r"); | |
| 219 | + | if (outp == NULL) | |
| 220 | + | goto end; | |
| 221 | + | while (i < n && (c = fgetc(outp)) != EOF) { | |
| 222 | + | if (c == '\n') { | |
| 223 | + | if (!lastsep) { | |
| 224 | + | bar.l[i++] = ' '; | |
| 225 | + | lastsep = true; | |
| 226 | + | } | |
| 227 | + | } else { | |
| 228 | + | bar.l[i++] = c; | |
| 229 | + | lastsep = false; | |
| 230 | + | } | |
| 231 | + | } | |
| 232 | + | pclose(outp); | |
| 233 | + | } | |
| 234 | + | end: | |
| 235 | + | if (lastsep) | |
| 236 | + | i--; | |
| 237 | + | bar.l[i] = '\0'; | |
| 238 | + | } | |
| 207 | 239 | ||
| 240 | + | void load_image(int new) { | |
| 208 | 241 | if (new < 0 || new >= filecnt) | |
| 209 | 242 | return; | |
| 210 | 243 | ||
| @@ -220,10 +253,8 @@ void load_image(int new) { | |||
| 220 | 253 | files[new].loaded = true; | |
| 221 | 254 | alternate = fileidx; | |
| 222 | 255 | fileidx = new; | |
| 223 | − | if (stat(files[new].path, &fstats) == 0) | |
| 224 | − | filesize = fstats.st_size; | |
| 225 | − | else | |
| 226 | − | filesize = 0; | |
| 256 | + | ||
| 257 | + | read_info(); | |
| 227 | 258 | ||
| 228 | 259 | if (img.multi.cnt > 0 && img.multi.animate) | |
| 229 | 260 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); | |
| @@ -232,60 +263,51 @@ void load_image(int new) { | |||
| 232 | 263 | } | |
| 233 | 264 | ||
| 234 | 265 | void update_info(void) { | |
| 235 | − | int i, fw, pw, fi, ln, rn; | |
| 236 | − | char frame_info[16]; | |
| 237 | − | const char *size_unit; | |
| 238 | − | float size = filesize; | |
| 266 | + | unsigned int i, fn, fw, n, len = sizeof(bar.r); | |
| 267 | + | int sel; | |
| 268 | + | char *t = bar.r, title[TITLE_LEN]; | |
| 269 | + | bool ow_info; | |
| 239 | 270 | ||
| 240 | − | pw = 0; | |
| 241 | − | for (i = filecnt; i > 0; i /= 10) | |
| 242 | − | pw++; | |
| 271 | + | for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); | |
| 272 | + | sel = mode == MODE_IMAGE ? fileidx : tns.sel; | |
| 243 | 273 | ||
| 244 | 274 | if (mode == MODE_THUMB) { | |
| 245 | − | if (tns.cnt != filecnt) { | |
| 246 | − | snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d", | |
| 247 | − | pw, tns.cnt, filecnt); | |
| 275 | + | win_set_title(&win, "sxiv"); | |
| 276 | + | ||
| 277 | + | if (tns.cnt == filecnt) { | |
| 278 | + | n = snprintf(t, len, "%0*d/%d", fw, sel + 1, filecnt); | |
| 279 | + | ow_info = true; | |
| 248 | 280 | } else { | |
| 249 | − | fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s", | |
| 250 | − | pw, tns.sel + 1, filecnt, BAR_SEPARATOR); | |
| 251 | − | ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s", | |
| 252 | − | files[tns.sel].name) + fi; | |
| 253 | − | if (win_textwidth(win_bar_l, ln, true) > win.w) | |
| 254 | − | snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s", | |
| 255 | − | files[tns.sel].base); | |
| 281 | + | snprintf(bar.l, sizeof(bar.l), "Loading... %0*d/%d", | |
| 282 | + | fw, tns.cnt, filecnt); | |
| 283 | + | bar.r[0] = '\0'; | |
| 284 | + | ow_info = false; | |
| 256 | 285 | } | |
| 257 | − | win_set_title(&win, "sxiv"); | |
| 258 | − | win_set_bar_info(&win, win_bar_l, NULL); | |
| 259 | 286 | } else { | |
| 260 | − | size_readable(&size, &size_unit); | |
| 287 | + | snprintf(title, sizeof(title), "sxiv - %s", files[sel].name); | |
| 288 | + | win_set_title(&win, title); | |
| 289 | + | ||
| 290 | + | n = snprintf(t, len, "%3d%% ", (int) (img.zoom * 100.0)); | |
| 261 | 291 | if (img.multi.cnt > 0) { | |
| 262 | − | fw = 0; | |
| 263 | − | for (i = img.multi.cnt; i > 0; i /= 10) | |
| 264 | − | fw++; | |
| 265 | − | snprintf(frame_info, sizeof frame_info, "%s%0*d/%d", | |
| 266 | − | BAR_SEPARATOR, fw, img.multi.sel+1, img.multi.cnt); | |
| 267 | − | } else { | |
| 268 | − | frame_info[0] = '\0'; | |
| 292 | + | for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10); | |
| 293 | + | n += snprintf(t + n, len - n, "(%0*d/%d) ", | |
| 294 | + | fn, img.multi.sel + 1, img.multi.cnt); | |
| 269 | 295 | } | |
| 270 | − | fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s", | |
| 271 | − | pw, fileidx + 1, filecnt, BAR_SEPARATOR); | |
| 272 | − | ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s", | |
| 273 | − | files[fileidx].name) + fi; | |
| 274 | − | rn = snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s", | |
| 275 | − | size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR, | |
| 276 | − | (int) (img.zoom * 100.0), frame_info); | |
| 277 | − | ||
| 278 | − | if (win_textwidth(win_bar_l, ln, true) + | |
| 279 | − | win_textwidth(win_bar_r, rn, true) > win.w) | |
| 296 | + | n += snprintf(t + n, len - n, "%0*d/%d", fw, sel + 1, filecnt); | |
| 297 | + | ow_info = bar.l[0] == '\0'; | |
| 298 | + | } | |
| 299 | + | if (ow_info) { | |
| 300 | + | fn = strlen(files[sel].name); | |
| 301 | + | if (fn < sizeof(bar.l) && | |
| 302 | + | win_textwidth(files[sel].name, fn, true) + | |
| 303 | + | win_textwidth(bar.r, n, true) < win.w) | |
| 280 | 304 | { | |
| 281 | − | snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s", | |
| 282 | − | files[fileidx].base); | |
| 305 | + | strncpy(bar.l, files[sel].name, sizeof(bar.l)); | |
| 306 | + | } else { | |
| 307 | + | strncpy(bar.l, files[sel].base, sizeof(bar.l)); | |
| 283 | 308 | } | |
| 284 | − | win_set_bar_info(&win, win_bar_l, win_bar_r); | |
| 285 | − | ||
| 286 | − | snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name); | |
| 287 | − | win_set_title(&win, win_title); | |
| 288 | 309 | } | |
| 310 | + | win_set_bar_info(&win, bar.l, bar.r); | |
| 289 | 311 | } | |
| 290 | 312 | ||
| 291 | 313 | void redraw(void) { | |
| @@ -519,6 +541,7 @@ int main(int argc, char **argv) { | |||
| 519 | 541 | size_t n; | |
| 520 | 542 | ssize_t len; | |
| 521 | 543 | char *filename; | |
| 544 | + | const char *homedir; | |
| 522 | 545 | struct stat fstats; | |
| 523 | 546 | r_dir_t dir; | |
| 524 | 547 | ||
| @@ -595,6 +618,18 @@ int main(int argc, char **argv) { | |||
| 595 | 618 | win_init(&win); | |
| 596 | 619 | img_init(&img, &win); | |
| 597 | 620 | ||
| 621 | + | if ((homedir = getenv("HOME")) == NULL) { | |
| 622 | + | warn("could not locate home directory"); | |
| 623 | + | } else { | |
| 624 | + | len = strlen(homedir) + strlen(INFO_SCRIPT) + 2; | |
| 625 | + | info_script = (char*) s_malloc(len); | |
| 626 | + | snprintf(info_script, len, "%s/%s", homedir, INFO_SCRIPT); | |
| 627 | + | if (access(info_script, X_OK) != 0) { | |
| 628 | + | free(info_script); | |
| 629 | + | info_script = NULL; | |
| 630 | + | } | |
| 631 | + | } | |
| 632 | + | ||
| 598 | 633 | if (options->thumb_mode) { | |
| 599 | 634 | mode = MODE_THUMB; | |
| 600 | 635 | tns_init(&tns, filecnt, &win); | |
| @@ -1,6 +1,6 @@ | |||
| 1 | 1 | .TH SXIV 1 sxiv\-VERSION | |
| 2 | 2 | .SH NAME | |
| 3 | − | sxiv \- Simple (or small or suckless) X Image Viewer | |
| 3 | + | sxiv \- Simple X Image Viewer | |
| 4 | 4 | .SH SYNOPSIS | |
| 5 | 5 | .B sxiv | |
| 6 | 6 | .RB [ \-bcdFfhpqrstvZ ] | |
| @@ -306,9 +306,18 @@ Pan image left. | |||
| 306 | 306 | .TP | |
| 307 | 307 | .B Shift+ScrollDown | |
| 308 | 308 | Pan image right. | |
| 309 | + | .SH STATUS BAR | |
| 310 | + | The information displayed on the left side of the status bar can be replaced | |
| 311 | + | with the output of a user-provided script, which is called by sxiv whenever an | |
| 312 | + | image gets loaded. The path of this script is | |
| 313 | + | .I ~/.sxiv/exec/image-info | |
| 314 | + | and the first argument to this script is the path of the loaded image. | |
| 315 | + | .P | |
| 316 | + | There is also an example script installed together with sxiv as | |
| 317 | + | .IR PREFIX/share/sxiv/exec/image-info . | |
| 309 | 318 | .SH THUMBNAIL CACHING | |
| 310 | 319 | To enable thumbnail caching, please make sure to create the directory | |
| 311 | − | .I ~/.sxiv/ | |
| 320 | + | .I ~/.sxiv/cache/ | |
| 312 | 321 | with write permissions. sxiv will then store all thumbnails inside this | |
| 313 | 322 | directory, but it will not create this directory by itself. It rather uses the | |
| 314 | 323 | existance of this directory as an affirmation, that the user wants thumbnails | |
| @@ -321,30 +330,23 @@ Additionally, run the following command afterwards inside the cache directory | |||
| 321 | 330 | to remove empty subdirectories: | |
| 322 | 331 | .P | |
| 323 | 332 | .RS | |
| 324 | − | find \-type d \-empty \-delete | |
| 325 | − | .RE | |
| 326 | − | .P | |
| 327 | − | If the version of | |
| 328 | − | .I find | |
| 329 | − | installed on your local system does not support the \-delete option, then you | |
| 330 | − | can also try the following command: | |
| 331 | − | .P | |
| 332 | − | .RS | |
| 333 | 333 | find . \-depth \-type d \-empty ! \-name '.' \-exec rmdir {} \\; | |
| 334 | 334 | .RE | |
| 335 | 335 | .SH AUTHOR | |
| 336 | 336 | .EX | |
| 337 | − | Bert Muennich <ber.t at gmx.com> | |
| 337 | + | Bert Muennich <be.muennich @ gmail.com> | |
| 338 | 338 | .EE | |
| 339 | 339 | .SH CONTRIBUTORS | |
| 340 | 340 | .EX | |
| 341 | 341 | Bastien Dejean <nihilhill at gmail.com> | |
| 342 | 342 | Dave Reisner <d at falconindy.com> | |
| 343 | 343 | Fung SzeTat <sthorde at gmail.com> | |
| 344 | − | .EX | |
| 344 | + | .EE | |
| 345 | 345 | .SH HOMEPAGE | |
| 346 | − | .TP | |
| 346 | + | .EX | |
| 347 | + | http://muennich.github.com/sxiv | |
| 347 | 348 | https://github.com/muennich/sxiv | |
| 349 | + | .EE | |
| 348 | 350 | .SH SEE ALSO | |
| 349 | 351 | .BR feh (1), | |
| 350 | 352 | .BR qiv (1) | |
| @@ -31,8 +31,10 @@ | |||
| 31 | 31 | #include "util.h" | |
| 32 | 32 | #include "config.h" | |
| 33 | 33 | ||
| 34 | − | const int thumb_dim = THUMB_SIZE + 10; | |
| 35 | − | char *cache_dir = NULL; | |
| 34 | + | static const int thumb_dim = THUMB_SIZE + 10; | |
| 35 | + | ||
| 36 | + | static const char * const CACHE_DIR = ".sxiv/cache"; | |
| 37 | + | static char *cache_dir = NULL; | |
| 36 | 38 | ||
| 37 | 39 | bool tns_cache_enabled(void) { | |
| 38 | 40 | struct stat stats; | |
| @@ -175,9 +177,9 @@ void tns_init(tns_t *tns, int cnt, win_t *win) { | |||
| 175 | 177 | if ((homedir = getenv("HOME")) != NULL) { | |
| 176 | 178 | if (cache_dir != NULL) | |
| 177 | 179 | free(cache_dir); | |
| 178 | − | len = strlen(homedir) + 10; | |
| 179 | − | cache_dir = (char*) s_malloc(len * sizeof(char)); | |
| 180 | − | snprintf(cache_dir, len, "%s/.sxiv", homedir); | |
| 180 | + | len = strlen(homedir) + strlen(CACHE_DIR) + 2; | |
| 181 | + | cache_dir = (char*) s_malloc(len); | |
| 182 | + | snprintf(cache_dir, len, "%s/%s", homedir, CACHE_DIR); | |
| 181 | 183 | } else { | |
| 182 | 184 | warn("could not locate thumbnail cache directory"); | |
| 183 | 185 | } | |
| @@ -42,13 +42,16 @@ static GC gc; | |||
| 42 | 42 | ||
| 43 | 43 | Atom wm_delete_win; | |
| 44 | 44 | ||
| 45 | − | struct { | |
| 45 | + | static struct { | |
| 46 | 46 | int ascent; | |
| 47 | 47 | int descent; | |
| 48 | 48 | XFontStruct *xfont; | |
| 49 | 49 | XFontSet set; | |
| 50 | 50 | } font; | |
| 51 | 51 | ||
| 52 | + | static int fontheight; | |
| 53 | + | static int barheight; | |
| 54 | + | ||
| 52 | 55 | void win_init_font(Display *dpy, const char *fontstr) { | |
| 53 | 56 | int n; | |
| 54 | 57 | char *def, **missing; | |
| @@ -77,6 +80,8 @@ void win_init_font(Display *dpy, const char *fontstr) { | |||
| 77 | 80 | font.ascent = font.xfont->ascent; | |
| 78 | 81 | font.descent = font.xfont->descent; | |
| 79 | 82 | } | |
| 83 | + | fontheight = font.ascent + font.descent; | |
| 84 | + | barheight = fontheight + 2 * V_TEXT_PAD; | |
| 80 | 85 | } | |
| 81 | 86 | ||
| 82 | 87 | unsigned long win_alloc_color(win_t *win, const char *name) { | |
| @@ -85,8 +90,8 @@ unsigned long win_alloc_color(win_t *win, const char *name) { | |||
| 85 | 90 | if (win == NULL) | |
| 86 | 91 | return 0UL; | |
| 87 | 92 | if (XAllocNamedColor(win->env.dpy, | |
| 88 | − | DefaultColormap(win->env.dpy, win->env.scr), | |
| 89 | − | name, &col, &col) == 0) | |
| 93 | + | DefaultColormap(win->env.dpy, win->env.scr), | |
| 94 | + | name, &col, &col) == 0) | |
| 90 | 95 | { | |
| 91 | 96 | die("could not allocate color: %s", name); | |
| 92 | 97 | } | |
| @@ -99,6 +104,8 @@ void win_init(win_t *win) { | |||
| 99 | 104 | if (win == NULL) | |
| 100 | 105 | return; | |
| 101 | 106 | ||
| 107 | + | memset(win, 0, sizeof(win_t)); | |
| 108 | + | ||
| 102 | 109 | e = &win->env; | |
| 103 | 110 | if ((e->dpy = XOpenDisplay(NULL)) == NULL) | |
| 104 | 111 | die("could not open display"); | |
| @@ -110,19 +117,12 @@ void win_init(win_t *win) { | |||
| 110 | 117 | e->cmap = DefaultColormap(e->dpy, e->scr); | |
| 111 | 118 | e->depth = DefaultDepth(e->dpy, e->scr); | |
| 112 | 119 | ||
| 113 | − | win->white = WhitePixel(e->dpy, e->scr); | |
| 114 | − | win->bgcol = win_alloc_color(win, WIN_BG_COLOR); | |
| 115 | − | win->fscol = win_alloc_color(win, WIN_FS_COLOR); | |
| 116 | − | win->selcol = win_alloc_color(win, SEL_COLOR); | |
| 117 | − | win->barbgcol = win_alloc_color(win, BAR_BG_COLOR); | |
| 118 | − | win->barfgcol = win_alloc_color(win, BAR_FG_COLOR); | |
| 119 | − | ||
| 120 | − | win->xwin = 0; | |
| 121 | − | win->pm = 0; | |
| 122 | − | win->fullscreen = false; | |
| 123 | − | win->barh = 0; | |
| 124 | − | win->lbar = NULL; | |
| 125 | − | win->rbar = NULL; | |
| 120 | + | win->white = WhitePixel(e->dpy, e->scr); | |
| 121 | + | win->bgcol = win_alloc_color(win, WIN_BG_COLOR); | |
| 122 | + | win->fscol = win_alloc_color(win, WIN_FS_COLOR); | |
| 123 | + | win->selcol = win_alloc_color(win, SEL_COLOR); | |
| 124 | + | win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR); | |
| 125 | + | win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR); | |
| 126 | 126 | ||
| 127 | 127 | if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0) | |
| 128 | 128 | warn("no locale support"); | |
| @@ -141,8 +141,8 @@ void win_set_sizehints(win_t *win) { | |||
| 141 | 141 | sizehints.flags = PMinSize | PMaxSize; | |
| 142 | 142 | sizehints.min_width = win->w; | |
| 143 | 143 | sizehints.max_width = win->w; | |
| 144 | − | sizehints.min_height = win->h + win->barh; | |
| 145 | − | sizehints.max_height = win->h + win->barh; | |
| 144 | + | sizehints.min_height = win->h + win->bar.h; | |
| 145 | + | sizehints.max_height = win->h + win->bar.h; | |
| 146 | 146 | XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints); | |
| 147 | 147 | } | |
| 148 | 148 | ||
| @@ -215,8 +215,8 @@ void win_open(win_t *win) { | |||
| 215 | 215 | XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1); | |
| 216 | 216 | ||
| 217 | 217 | if (!options->hide_bar) { | |
| 218 | − | win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD; | |
| 219 | − | win->h -= win->barh; | |
| 218 | + | win->bar.h = barheight; | |
| 219 | + | win->h -= win->bar.h; | |
| 220 | 220 | } | |
| 221 | 221 | ||
| 222 | 222 | if (options->fixed_win) | |
| @@ -249,8 +249,8 @@ bool win_configure(win_t *win, XConfigureEvent *c) { | |||
| 249 | 249 | ||
| 250 | 250 | if (win == NULL || c == NULL) | |
| 251 | 251 | return false; | |
| 252 | − | ||
| 253 | − | if ((changed = win->w != c->width || win->h + win->barh != c->height)) { | |
| 252 | + | ||
| 253 | + | if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) { | |
| 254 | 254 | if (win->pm != None) { | |
| 255 | 255 | XFreePixmap(win->env.dpy, win->pm); | |
| 256 | 256 | win->pm = None; | |
| @@ -260,7 +260,7 @@ bool win_configure(win_t *win, XConfigureEvent *c) { | |||
| 260 | 260 | win->x = c->x; | |
| 261 | 261 | win->y = c->y; | |
| 262 | 262 | win->w = c->width; | |
| 263 | − | win->h = c->height - win->barh; | |
| 263 | + | win->h = c->height - win->bar.h; | |
| 264 | 264 | win->bw = c->border_width; | |
| 265 | 265 | ||
| 266 | 266 | return changed; | |
| @@ -283,13 +283,13 @@ bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) { | |||
| 283 | 283 | w = MIN(w, win->env.scrw - 2 * win->bw); | |
| 284 | 284 | h = MIN(h, win->env.scrh - 2 * win->bw); | |
| 285 | 285 | ||
| 286 | − | if (win->x == x && win->y == y && win->w == w && win->h + win->barh == h) | |
| 286 | + | if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h) | |
| 287 | 287 | return false; | |
| 288 | 288 | ||
| 289 | 289 | win->x = x; | |
| 290 | 290 | win->y = y; | |
| 291 | 291 | win->w = w; | |
| 292 | − | win->h = h - win->barh; | |
| 292 | + | win->h = h - win->bar.h; | |
| 293 | 293 | ||
| 294 | 294 | if (options->fixed_win) | |
| 295 | 295 | win_set_sizehints(win); | |
| @@ -327,12 +327,12 @@ void win_toggle_bar(win_t *win) { | |||
| 327 | 327 | if (win == NULL || win->xwin == None) | |
| 328 | 328 | return; | |
| 329 | 329 | ||
| 330 | − | if (win->barh != 0) { | |
| 331 | − | win->h += win->barh; | |
| 332 | − | win->barh = 0; | |
| 330 | + | if (win->bar.h != 0) { | |
| 331 | + | win->h += win->bar.h; | |
| 332 | + | win->bar.h = 0; | |
| 333 | 333 | } else { | |
| 334 | − | win->barh = font.ascent + font.descent + 2 * V_TEXT_PAD; | |
| 335 | − | win->h -= win->barh; | |
| 334 | + | win->bar.h = barheight; | |
| 335 | + | win->h -= win->bar.h; | |
| 336 | 336 | } | |
| 337 | 337 | } | |
| 338 | 338 | ||
| @@ -343,7 +343,7 @@ void win_clear(win_t *win) { | |||
| 343 | 343 | if (win == NULL || win->xwin == None) | |
| 344 | 344 | return; | |
| 345 | 345 | ||
| 346 | − | h = win->h + win->barh; | |
| 346 | + | h = win->h + win->bar.h; | |
| 347 | 347 | e = &win->env; | |
| 348 | 348 | ||
| 349 | 349 | if (win->pm == None) | |
| @@ -354,53 +354,56 @@ void win_clear(win_t *win) { | |||
| 354 | 354 | } | |
| 355 | 355 | ||
| 356 | 356 | void win_draw_bar(win_t *win) { | |
| 357 | + | int len, olen, x, y, w, tw; | |
| 358 | + | char rest[3]; | |
| 359 | + | const char *dots = "..."; | |
| 357 | 360 | win_env_t *e; | |
| 358 | − | int len, x, y, w, tw = 0, seplen; | |
| 359 | − | const char *rt; | |
| 360 | 361 | ||
| 361 | 362 | if (win == NULL || win->xwin == None || win->pm == None) | |
| 362 | 363 | return; | |
| 363 | 364 | ||
| 364 | 365 | e = &win->env; | |
| 365 | − | x = H_TEXT_PAD; | |
| 366 | 366 | y = win->h + font.ascent + V_TEXT_PAD; | |
| 367 | − | w = win->w - 2 * H_TEXT_PAD; | |
| 367 | + | w = win->w; | |
| 368 | 368 | ||
| 369 | − | XSetForeground(e->dpy, gc, win->barbgcol); | |
| 370 | − | XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->barh); | |
| 369 | + | XSetForeground(e->dpy, gc, win->bar.bgcol); | |
| 370 | + | XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h); | |
| 371 | 371 | ||
| 372 | − | XSetForeground(e->dpy, gc, win->barfgcol); | |
| 373 | − | XSetBackground(e->dpy, gc, win->barbgcol); | |
| 372 | + | XSetForeground(e->dpy, gc, win->bar.fgcol); | |
| 373 | + | XSetBackground(e->dpy, gc, win->bar.bgcol); | |
| 374 | 374 | ||
| 375 | − | if (win->lbar != NULL) { | |
| 376 | − | len = strlen(win->lbar); | |
| 377 | − | while (len > 0 && (tw = win_textwidth(win->lbar, len, false)) > w) | |
| 378 | − | len--; | |
| 379 | − | w -= tw + 2 * H_TEXT_PAD; | |
| 380 | − | if (font.set) | |
| 381 | − | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->lbar, len); | |
| 382 | − | else | |
| 383 | − | XDrawString(e->dpy, win->pm, gc, x, y, win->lbar, len); | |
| 384 | − | } | |
| 385 | − | if (win->rbar != NULL) { | |
| 386 | − | len = strlen(win->rbar); | |
| 387 | − | seplen = strlen(BAR_SEPARATOR); | |
| 388 | − | rt = win->rbar; | |
| 389 | − | while (len > 0 && (tw = win_textwidth(rt, len, false)) > w) { | |
| 390 | − | rt = strstr(rt, BAR_SEPARATOR); | |
| 391 | − | if (rt != NULL) { | |
| 392 | − | rt += seplen; | |
| 393 | − | len = strlen(rt); | |
| 394 | − | } else { | |
| 395 | − | len = 0; | |
| 396 | − | } | |
| 375 | + | if (win->bar.r != NULL) { | |
| 376 | + | len = strlen(win->bar.r); | |
| 377 | + | if (len > 0) { | |
| 378 | + | if ((tw = win_textwidth(win->bar.r, len, true)) > w) | |
| 379 | + | return; | |
| 380 | + | x = win->w - tw + H_TEXT_PAD; | |
| 381 | + | w -= tw; | |
| 382 | + | if (font.set) | |
| 383 | + | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len); | |
| 384 | + | else | |
| 385 | + | XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len); | |
| 397 | 386 | } | |
| 387 | + | } | |
| 388 | + | if (win->bar.l != NULL) { | |
| 389 | + | olen = len = strlen(win->bar.l); | |
| 390 | + | while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w) | |
| 391 | + | len--; | |
| 398 | 392 | if (len > 0) { | |
| 399 | − | x = win->w - tw - H_TEXT_PAD; | |
| 393 | + | if (len != olen) { | |
| 394 | + | w = strlen(dots); | |
| 395 | + | if (len <= w) | |
| 396 | + | return; | |
| 397 | + | memcpy(rest, win->bar.l + len - w, w); | |
| 398 | + | memcpy(win->bar.l + len - w, dots, w); | |
| 399 | + | } | |
| 400 | + | x = H_TEXT_PAD; | |
| 400 | 401 | if (font.set) | |
| 401 | − | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, rt, len); | |
| 402 | + | XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len); | |
| 402 | 403 | else | |
| 403 | − | XDrawString(e->dpy, win->pm, gc, x, y, rt, len); | |
| 404 | + | XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len); | |
| 405 | + | if (len != olen) | |
| 406 | + | memcpy(win->bar.l + len - w, rest, w); | |
| 404 | 407 | } | |
| 405 | 408 | } | |
| 406 | 409 | } | |
| @@ -409,11 +412,11 @@ void win_draw(win_t *win) { | |||
| 409 | 412 | if (win == NULL || win->xwin == None || win->pm == None) | |
| 410 | 413 | return; | |
| 411 | 414 | ||
| 412 | − | if (win->barh > 0) | |
| 415 | + | if (win->bar.h > 0) | |
| 413 | 416 | win_draw_bar(win); | |
| 414 | 417 | ||
| 415 | 418 | XCopyArea(win->env.dpy, win->pm, win->xwin, gc, | |
| 416 | − | 0, 0, win->w, win->h + win->barh, 0, 0); | |
| 419 | + | 0, 0, win->w, win->h + win->bar.h, 0, 0); | |
| 417 | 420 | } | |
| 418 | 421 | ||
| 419 | 422 | void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h, | |
| @@ -466,10 +469,10 @@ void win_set_title(win_t *win, const char *title) { | |||
| 466 | 469 | PropModeReplace, (unsigned char *) title, strlen(title)); | |
| 467 | 470 | } | |
| 468 | 471 | ||
| 469 | − | void win_set_bar_info(win_t *win, const char *li, const char *ri) { | |
| 472 | + | void win_set_bar_info(win_t *win, char *linfo, char *rinfo) { | |
| 470 | 473 | if (win != NULL) { | |
| 471 | − | win->lbar = li; | |
| 472 | − | win->rbar = ri; | |
| 474 | + | win->bar.l = linfo; | |
| 475 | + | win->bar.r = rinfo; | |
| 473 | 476 | } | |
| 474 | 477 | } | |
| 475 | 478 | ||
| @@ -23,8 +23,6 @@ | |||
| 23 | 23 | ||
| 24 | 24 | #include "types.h" | |
| 25 | 25 | ||
| 26 | − | #define BAR_SEPARATOR " | " | |
| 27 | − | ||
| 28 | 26 | typedef struct { | |
| 29 | 27 | Display *dpy; | |
| 30 | 28 | int scr; | |
| @@ -42,21 +40,23 @@ typedef struct { | |||
| 42 | 40 | unsigned long bgcol; | |
| 43 | 41 | unsigned long fscol; | |
| 44 | 42 | unsigned long selcol; | |
| 45 | − | unsigned long barbgcol; | |
| 46 | − | unsigned long barfgcol; | |
| 47 | 43 | Pixmap pm; | |
| 48 | 44 | ||
| 49 | 45 | int x; | |
| 50 | 46 | int y; | |
| 51 | 47 | unsigned int w; | |
| 52 | 48 | unsigned int h; /* = win height - bar height */ | |
| 53 | − | unsigned int barh; | |
| 54 | 49 | unsigned int bw; | |
| 55 | 50 | ||
| 56 | 51 | bool fullscreen; | |
| 57 | 52 | ||
| 58 | − | const char *lbar; | |
| 59 | − | const char *rbar; | |
| 53 | + | struct { | |
| 54 | + | unsigned int h; | |
| 55 | + | char *l; | |
| 56 | + | char *r; | |
| 57 | + | unsigned long bgcol; | |
| 58 | + | unsigned long fgcol; | |
| 59 | + | } bar; | |
| 60 | 60 | } win_t; | |
| 61 | 61 | ||
| 62 | 62 | extern Atom wm_delete_win; | |
| @@ -80,7 +80,7 @@ void win_draw_rect(win_t*, Pixmap, int, int, int, int, bool, int, | |||
| 80 | 80 | int win_textwidth(const char*, unsigned int, bool); | |
| 81 | 81 | ||
| 82 | 82 | void win_set_title(win_t*, const char*); | |
| 83 | − | void win_set_bar_info(win_t*, const char*, const char*); | |
| 83 | + | void win_set_bar_info(win_t*, char*, char*); | |
| 84 | 84 | void win_set_cursor(win_t*, cursor_t); | |
| 85 | 85 | ||
| 86 | 86 | #endif /* WINDOW_H */ | |