Krolyxon
Commit 675db4b

Branches

Refs →

nsxiv675db4b

Make statusbar optional (#95)

libXft and libfontconfig are now optional dependencies which can be
disabled via `HAVE_LIBFONTS=0`. Disabling them means disabling the
statusbar. This also does not search for freetype2 header if disabled.
Author
Arthur Williams
Date
2021-09-19 01:49 -0700
Committer
NRK · 2021-10-10 02:17 +0600
Parent
6ce94e3
Commit
675db4bbb646ff51cc3ecc8ba7d587670872580a

+63 −34

6 files changed

  1. M Makefile +10 −4
  2. M README.md +2 −3
  3. M image.c +1 −1
  4. M nsxiv.h +7 −3
  5. M thumbs.c +3 −3
  6. M window.c +40 −20
M Makefile View
@@ -13,6 +13,7 @@ OPT_DEP_DEFAULT ?= 1
1313 HAVE_INOTIFY ?= $(OPT_DEP_DEFAULT)
1414
1515 # optional dependencies, see README for more info
16+HAVE_LIBFONTS ?= $(OPT_DEP_DEFAULT)
1617 HAVE_LIBGIF ?= $(OPT_DEP_DEFAULT)
1718 HAVE_LIBEXIF ?= $(OPT_DEP_DEFAULT)
1819 HAVE_LIBWEBP ?= $(OPT_DEP_DEFAULT)
@@ -23,11 +24,16 @@ CFLAGS ?= -std=c99 -Wall -pedantic
2324 # icons that will be installed via `make icon`
2425 ICONS = 16x16.png 32x32.png 48x48.png 64x64.png 128x128.png
2526
27+inc_fonts_0 =
28+inc_fonts_1 = -I/usr/include/freetype2 -I$(PREFIX)/include/freetype2
29+
2630 CPPFLAGS = -D_XOPEN_SOURCE=700 \
2731 -DHAVE_LIBGIF=$(HAVE_LIBGIF) -DHAVE_LIBEXIF=$(HAVE_LIBEXIF) \
28− -DHAVE_LIBWEBP=$(HAVE_LIBWEBP) \
29− -I/usr/include/freetype2 -I$(PREFIX)/include/freetype2
32+ -DHAVE_LIBWEBP=$(HAVE_LIBWEBP) -DHAVE_LIBFONTS=$(HAVE_LIBFONTS) \
33+ $(inc_fonts_$(HAVE_LIBFONTS))
3034
35+lib_fonts_0 =
36+lib_fonts_1 = -lXft -lfontconfig
3137 lib_exif_0 =
3238 lib_exif_1 = -lexif
3339 lib_gif_0 =
@@ -37,9 +43,9 @@ lib_webp_1 = -lwebpdemux -lwebp
3743 autoreload_0 = nop
3844 autoreload_1 = inotify
3945 # using += because certain *BSD distros may need to add additional flags
40−LDLIBS += -lImlib2 -lX11 -lXft -lfontconfig \
46+LDLIBS += -lImlib2 -lX11 \
4147 $(lib_exif_$(HAVE_LIBEXIF)) $(lib_gif_$(HAVE_LIBGIF)) \
42− $(lib_webp_$(HAVE_LIBWEBP))
48+ $(lib_webp_$(HAVE_LIBWEBP)) $(lib_fonts_$(HAVE_LIBFONTS))
4349
4450 OBJS = autoreload_$(autoreload_$(HAVE_INOTIFY)).o commands.o image.o main.o options.o \
4551 thumbs.o util.o window.o
M README.md View
@@ -45,14 +45,13 @@ nsxiv requires the following software to be installed:
4545
4646 * Imlib2
4747 * X11
48− * Xft
49− * freetype2
50− * fontconfig
5148
5249 The following dependencies are optional.
5350
5451 * inotify : Used for auto-reloading images on change.
5552 Disabled via `HAVE_INOTIFY=0`
53+ * libXft, freetype2, fontconfig : Used for the status bar.
54+ Disabled via `HAVE_LIBFONTS=0`
5655 * giflib : Used for animated gif playback.
5756 Disabled via `HAVE_LIBGIF=0`.
5857 * libexif : Used for auto-orientation and exif thumbnails.
M image.c View
@@ -643,7 +643,7 @@ void img_render(img_t *img)
643643 }
644644 imlib_image_put_back_data(data);
645645 } else {
646− c = win->win_bg.pixel;
646+ c = win->win_bg;
647647 imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
648648 imlib_image_fill_rectangle(0, 0, dw, dh);
649649 }
M nsxiv.h View
@@ -383,7 +383,9 @@ int r_mkdir(char*);
383383 /* window.c */
384384
385385 #include <X11/Xutil.h>
386+#if HAVE_LIBFONTS
386387 #include <X11/Xft/Xft.h>
388+#endif
387389
388390 enum {
389391 BAR_L_LEN = 512,
@@ -420,11 +422,13 @@ struct win {
420422 Window xwin;
421423 win_env_t env;
422424
423− XftColor win_bg;
424− XftColor win_fg;
425+ unsigned long win_bg;
426+ unsigned long win_fg;
427+ unsigned long mrk_fg;
428+#if HAVE_LIBFONTS
425429 XftColor bar_bg;
426430 XftColor bar_fg;
427− XftColor mrk_fg;
431+#endif
428432
429433 int x;
430434 int y;
M thumbs.c View
@@ -471,14 +471,14 @@ void tns_mark(tns_t *tns, int n, bool mark)
471471 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
472472 win_t *win = tns->win;
473473 thumb_t *t = &tns->thumbs[n];
474− unsigned long col = win->win_bg.pixel;
474+ unsigned long col = win->win_bg;
475475 int x = t->x + t->w, y = t->y + t->h;
476476
477477 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
478478 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
479479
480480 if (mark)
481− col = win->mrk_fg.pixel;
481+ col = win->mrk_fg;
482482
483483 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
484484
@@ -492,7 +492,7 @@ void tns_highlight(tns_t *tns, int n, bool hl)
492492 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
493493 win_t *win = tns->win;
494494 thumb_t *t = &tns->thumbs[n];
495− unsigned long col = hl ? win->win_fg.pixel : win->win_bg.pixel;
495+ unsigned long col = hl ? win->win_fg : win->win_bg;
496496 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
497497
498498 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
M window.c View
@@ -20,7 +20,6 @@
2020 #define _WINDOW_CONFIG
2121 #include "config.h"
2222 #include "icon/data.h"
23−#include "utf8.h"
2423
2524 #include <stdlib.h>
2625 #include <string.h>
@@ -30,6 +29,12 @@
3029 #include <X11/Xatom.h>
3130 #include <X11/Xresource.h>
3231
32+#if HAVE_LIBFONTS
33+#include "utf8.h"
34+static XftFont *font;
35+static double fontsize;
36+#endif
37+
3338 #define RES_CLASS "Nsxiv"
3439
3540 enum {
@@ -47,15 +52,14 @@ static struct {
4752
4853 static GC gc;
4954
50−static XftFont *font;
51−static int fontheight;
52−static double fontsize;
5355 static int barheight;
5456
5557 Atom atoms[ATOM_COUNT];
5658
59+#if HAVE_LIBFONTS
5760 void win_init_font(const win_env_t *e, const char *fontstr)
5861 {
62+ int fontheight = 0;
5963 if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
6064 error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
6165 fontheight = font->ascent + font->descent;
@@ -64,13 +68,19 @@ void win_init_font(const win_env_t *e, const char *fontstr)
6468 XftFontClose(e->dpy, font);
6569 }
6670
67−void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
71+void xft_alloc_color(const win_env_t *e, const char *name, XftColor *col)
6872 {
69− if (!XftColorAllocName(e->dpy, e->vis,
70− e->cmap, name, col))
71− {
73+ if (!XftColorAllocName(e->dpy, e->vis, e->cmap, name, col))
7274 error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
73− }
75+}
76+#endif /* HAVE_LIBFONTS */
77+
78+void win_alloc_color(const win_env_t *e, const char *name, unsigned long *pixel)
79+{
80+ XColor screen, exact;
81+ if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, &exact))
82+ error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
83+ *pixel = exact.pixel;
7484 }
7585
7686 const char* win_res(XrmDatabase db, const char *name, const char *def)
@@ -94,7 +104,10 @@ const char* win_res(XrmDatabase db, const char *name, const char *def)
94104 void win_init(win_t *win)
95105 {
96106 win_env_t *e;
97− const char *win_bg, *win_fg, *bar_bg, *bar_fg, *mrk_fg, *f;
107+ const char *win_bg, *win_fg, *mrk_fg;
108+#if HAVE_LIBFONTS
109+ const char *bar_fg, *bar_bg, *f;
110+#endif
98111 char *res_man;
99112 XrmDatabase db;
100113 XVisualInfo vis;
@@ -131,20 +144,22 @@ void win_init(win_t *win)
131144 res_man = XResourceManagerString(e->dpy);
132145 db = res_man != NULL ? XrmGetStringDatabase(res_man) : None;
133146
134− f = win_res(db, RES_CLASS ".bar.font", "monospace-8");
135− win_init_font(e, f);
136−
137147 win_bg = win_res(db, RES_CLASS ".window.background", "white");
138148 win_fg = win_res(db, RES_CLASS ".window.foreground", "black");
139− bar_bg = win_res(db, RES_CLASS ".bar.background", win_bg);
140− bar_fg = win_res(db, RES_CLASS ".bar.foreground", win_fg);
141149 mrk_fg = win_res(db, RES_CLASS ".mark.foreground", win_fg);
142150 win_alloc_color(e, win_bg, &win->win_bg);
143151 win_alloc_color(e, win_fg, &win->win_fg);
144− win_alloc_color(e, bar_bg, &win->bar_bg);
145− win_alloc_color(e, bar_fg, &win->bar_fg);
146152 win_alloc_color(e, mrk_fg, &win->mrk_fg);
147153
154+#if HAVE_LIBFONTS
155+ bar_bg = win_res(db, RES_CLASS ".bar.background", win_bg);
156+ bar_fg = win_res(db, RES_CLASS ".bar.foreground", win_fg);
157+ xft_alloc_color(e, bar_bg, &win->bar_bg);
158+ xft_alloc_color(e, bar_fg, &win->bar_fg);
159+
160+ f = win_res(db, RES_CLASS ".bar.font", "monospace-8");
161+ win_init_font(e, f);
162+
148163 win->bar.l.size = BAR_L_LEN;
149164 win->bar.r.size = BAR_R_LEN;
150165 /* 3 padding bytes needed by utf8_decode */
@@ -153,6 +168,7 @@ void win_init(win_t *win)
153168 win->bar.r.buf = emalloc(win->bar.r.size + 3);
154169 win->bar.r.buf[0] = '\0';
155170 win->bar.h = options->hide_bar ? 0 : barheight;
171+#endif /* HAVE_LIBFONTS */
156172
157173 INIT_ATOM_(WM_DELETE_WINDOW);
158174 INIT_ATOM_(_NET_WM_NAME);
@@ -306,7 +322,7 @@ void win_open(win_t *win)
306322 win->buf.h = e->scrh;
307323 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
308324 win->buf.w, win->buf.h, e->depth);
309− XSetForeground(e->dpy, gc, win->win_bg.pixel);
325+ XSetForeground(e->dpy, gc, win->win_bg);
310326 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
311327 XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
312328
@@ -388,10 +404,11 @@ void win_clear(win_t *win)
388404 win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
389405 win->buf.w, win->buf.h, e->depth);
390406 }
391− XSetForeground(e->dpy, gc, win->win_bg.pixel);
407+ XSetForeground(e->dpy, gc, win->win_bg);
392408 XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
393409 }
394410
411+#if HAVE_LIBFONTS
395412 #define TEXTWIDTH(win, text, len) \
396413 win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
397414
@@ -448,7 +465,7 @@ void win_draw_bar(win_t *win)
448465 XSetForeground(e->dpy, gc, win->bar_bg.pixel);
449466 XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
450467
451− XSetForeground(e->dpy, gc, win->win_bg.pixel);
468+ XSetForeground(e->dpy, gc, win->win_bg);
452469 XSetBackground(e->dpy, gc, win->bar_bg.pixel);
453470
454471 if ((len = strlen(r->buf)) > 0) {
@@ -465,6 +482,9 @@ void win_draw_bar(win_t *win)
465482 }
466483 XftDrawDestroy(d);
467484 }
485+#else
486+void win_draw_bar(win_t *win){}
487+#endif /* HAVE_LIBFONTS */
468488
469489 void win_draw(win_t *win)
470490 {