Krolyxon
Commit cd710f5

Branches

Refs →

nsxivcd710f5

Fix behaviour when TrueColor / 24 bit depth is not available (#114)

* Fix regression introduced in c7ca547 which made nsxiv not start in
  non-TrueColor X server.
* Introduce a new fix for embedding into tabbed-alpha. 
* Fixes a visual glitch from original sxiv when drawing transparent images in 8
  bit depth. In 8 bit PseudoColor, `.pixel` is just an index into the 256
  defined colors and thus trying to extract rgb bits from it would result in
  visual glitch. The values `.color.red` on the other hand and so on are always
  integers between 0 and 0xFFFF representing the color as expected.
* Use XColor for win_bg/fg and mrk_fg
Author
miseran
Date
2021-10-12 01:46 +0200
Committer
GitHub · 2021-10-11 23:46 +0000
Parent
1449bfc
Commit
cd710f583f5ab8650fa2e41c08dc9a6332a5680f

+24 −33

4 files changed

  1. M image.c +2 −2
  2. M nsxiv.h +3 −3
  3. M thumbs.c +4 −4
  4. M window.c +15 −24
M image.c View
@@ -577,7 +577,7 @@ void img_render(img_t *img)
577577 int sx, sy, sw, sh;
578578 int dx, dy, dw, dh;
579579 Imlib_Image bg;
580− unsigned long c;
580+ XColor c;
581581
582582 win = img->win;
583583 img_fit(img);
@@ -646,7 +646,7 @@ void img_render(img_t *img)
646646 imlib_image_put_back_data(data);
647647 } else {
648648 c = win->win_bg;
649− imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
649+ imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
650650 imlib_image_fill_rectangle(0, 0, dw, dh);
651651 }
652652 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
M nsxiv.h View
@@ -416,9 +416,9 @@ struct win {
416416 Window xwin;
417417 win_env_t env;
418418
419− unsigned long win_bg;
420− unsigned long win_fg;
421− unsigned long mrk_fg;
419+ XColor win_bg;
420+ XColor win_fg;
421+ XColor mrk_fg;
422422 #if HAVE_LIBFONTS
423423 XftColor bar_bg;
424424 XftColor bar_fg;
M thumbs.c View
@@ -44,7 +44,7 @@ char* tns_cache_filepath(const char *filepath)
4444
4545 if (*filepath != '/')
4646 return NULL;
47−
47+
4848 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
4949 /* don't cache images inside the cache directory! */
5050 len = strlen(cache_dir) + strlen(filepath) + 2;
@@ -469,14 +469,14 @@ void tns_mark(tns_t *tns, int n, bool mark)
469469 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
470470 win_t *win = tns->win;
471471 thumb_t *t = &tns->thumbs[n];
472− unsigned long col = win->win_bg;
472+ unsigned long col = win->win_bg.pixel;
473473 int x = t->x + t->w, y = t->y + t->h;
474474
475475 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
476476 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
477477
478478 if (mark)
479− col = win->mrk_fg;
479+ col = win->mrk_fg.pixel;
480480
481481 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
482482
@@ -490,7 +490,7 @@ void tns_highlight(tns_t *tns, int n, bool hl)
490490 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
491491 win_t *win = tns->win;
492492 thumb_t *t = &tns->thumbs[n];
493− unsigned long col = hl ? win->win_fg : win->win_bg;
493+ unsigned long col = hl ? win->win_fg.pixel : win->win_bg.pixel;
494494 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
495495
496496 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
M window.c View
@@ -80,12 +80,11 @@ void xft_alloc_color(const win_env_t *e, const char *name, XftColor *col)
8080 }
8181 #endif /* HAVE_LIBFONTS */
8282
83−void win_alloc_color(const win_env_t *e, const char *name, unsigned long *pixel)
83+void win_alloc_color(const win_env_t *e, const char *name, XColor *col)
8484 {
85− XColor screen, exact;
86− if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, &exact))
85+ XColor screen;
86+ if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, col))
8787 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
88− *pixel = exact.pixel;
8988 }
9089
9190 const char* win_res(XrmDatabase db, const char *name, const char *def)
@@ -112,9 +111,6 @@ void win_init(win_t *win)
112111 #endif
113112 char *res_man;
114113 XrmDatabase db;
115− XVisualInfo vis;
116− XWindowAttributes attr;
117− Window parent;
118114
119115 memset(win, 0, sizeof(win_t));
120116
@@ -125,19 +121,9 @@ void win_init(win_t *win)
125121 e->scr = DefaultScreen(e->dpy);
126122 e->scrw = DisplayWidth(e->dpy, e->scr);
127123 e->scrh = DisplayHeight(e->dpy, e->scr);
128−
129− parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
130−
131− if (options->embed == 0) {
132− e->depth = DefaultDepth(e->dpy, e->scr);
133− } else {
134− XGetWindowAttributes(e->dpy, parent, &attr);
135− e->depth = attr.depth;
136− }
137−
138− XMatchVisualInfo(e->dpy, e->scr, e->depth, TrueColor, &vis);
139− e->vis = vis.visual;
140− e->cmap = XCreateColormap(e->dpy, parent, e->vis, None);
124+ e->depth = DefaultDepth(e->dpy, e->scr);
125+ e->vis = DefaultVisual(e->dpy, e->scr);
126+ e->cmap = DefaultColormap(e->dpy, e->scr);
141127
142128 if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
143129 error(0, 0, "No locale support");
@@ -197,6 +183,7 @@ void win_open(win_t *win)
197183 XWMHints hints;
198184 pid_t pid;
199185 char hostname[256];
186+ XSetWindowAttributes attrs;
200187
201188 e = &win->env;
202189 parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
@@ -238,8 +225,12 @@ void win_open(win_t *win)
238225 win->y = 0;
239226 }
240227
228+ attrs.colormap = e->cmap;
229+ attrs.border_pixel = 0;
230+
241231 win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0,
242− e->depth, InputOutput, e->vis, 0, NULL);
232+ e->depth, InputOutput, e->vis,
233+ CWColormap | CWBorderPixel, &attrs);
243234 if (win->xwin == None)
244235 error(EXIT_FAILURE, 0, "Error creating X window");
245236
@@ -317,7 +308,7 @@ void win_open(win_t *win)
317308 win->buf.h = e->scrh;
318309 win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
319310
320− XSetForeground(e->dpy, gc, win->win_bg);
311+ XSetForeground(e->dpy, gc, win->win_bg.pixel);
321312 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
322313 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
323314 XMapWindow(e->dpy, win->xwin);
@@ -398,7 +389,7 @@ void win_clear(win_t *win)
398389 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
399390 win->buf.w, win->buf.h, e->depth);
400391 }
401− XSetForeground(e->dpy, gc, win->win_bg);
392+ XSetForeground(e->dpy, gc, win->win_bg.pixel);
402393 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
403394 }
404395
@@ -455,7 +446,7 @@ void win_draw_bar(win_t *win)
455446 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
456447 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
457448
458− XSetForeground(e->dpy, gc, win->win_bg);
449+ XSetForeground(e->dpy, gc, win->win_bg.pixel);
459450 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
460451
461452 if ((len = strlen(r->buf)) > 0) {