diff --git a/.github/workflows/make.yml b/.github/workflows/make.yml index ccf3acc..4f2e45c 100644 --- a/.github/workflows/make.yml +++ b/.github/workflows/make.yml @@ -16,10 +16,10 @@ jobs: - name: make run: make - name: make install - run: sudo env PREFIX=/usr/ make install + run: sudo make PREFIX=/usr/ install - name: test deps run: sudo apt-get install -y xvfb - name: smoke test build run: xvfb-run ./xst bash -c exit - name: smoke test install - run: xvfb-run xst bash -c exit + run: sync ; sleep 0.1 ; xvfb-run xst bash -c exit diff --git a/x.c b/x.c index 798880e..29dafe9 100644 --- a/x.c +++ b/x.c @@ -75,7 +75,10 @@ static void ttysend(const Arg *); #include "config.h" /* Calculate count of spare fonts */ -int fonts_count; +static int fonts_count = 0; + +// Declare fallback_fonts as a dynamic array +static char **fallback_fonts = NULL; /* XEMBED messages */ #define XEMBED_FOCUS_IN 4 @@ -174,6 +177,7 @@ static int xloadcolor(int, const char *, Color *); static int xloadfont(Font *, FcPattern *); static void xloadfonts(const char *, double); static int xloadsparefont(FcPattern *, int); +static void xinitsparefonts(void); static void xloadsparefonts(void); static void xunloadfont(Font *); static void xunloadfonts(void); @@ -333,9 +337,7 @@ zoomabs(const Arg *arg) { xunloadfonts(); xloadfonts(getusedfont(), arg->f); - fonts_count--; xloadsparefonts(); - fonts_count++; cresize(0, 0); redraw(); xhints(); @@ -1149,6 +1151,28 @@ xloadsparefont(FcPattern *pattern, int flags) return 0; } +void +xinitsparefonts(void) +{ + // Ignore if spare fonts already loaded: + if (fonts_count) return; + + // Allocate memory for initial fonts + int initial_count = sizeof(font2) / sizeof(font2[0]); + fallback_fonts = malloc((initial_count + 1) * sizeof(char *)); + if (!fallback_fonts) { + die("Error initializing fallback fonts!\n"); + } + + // Copy the initial fonts to fallback_fonts + for (int i = 0; i < initial_count; i++) { + fallback_fonts[i] = font2[i]; + } + + fonts_count = initial_count; + fallback_fonts[fonts_count] = NULL; // Null-terminate the array +} + void xloadsparefonts(void) { @@ -1168,8 +1192,8 @@ xloadsparefonts(void) frc = xrealloc(frc, frccap * sizeof(Fontcache)); } - for (fp = font2; fp - font2 < fonts_count; ++fp) { - + for (fp = fallback_fonts; fp < fallback_fonts + fonts_count && *fp != NULL; ++fp) { + if (**fp == '-') pattern = XftXlfdParse(*fp, False, False); else @@ -1330,6 +1354,7 @@ xinit(int cols, int rows) xloadfonts(getusedfont(), 0); /* spare fonts */ + xinitsparefonts(); xloadsparefonts(); /* colors */ diff --git a/xst.c b/xst.c index 89c3fe7..4ad9031 100644 --- a/xst.c +++ b/xst.c @@ -63,20 +63,38 @@ xrdb_load(void) } XRESOURCE_LOAD_META("font_fallback") { - int count = 0, endchar = fonts_count = sizeof(font2) / sizeof(*font2); - for (int i = 0; ret.addr[i]; i++) if (ret.addr[i] == ',') count++; - if (count > 0) - { - for (int i = 0; i <= count; i++) - { - if (i == 0) font2[endchar + i] = strtok(ret.addr, ","); - else font2[endchar + i] = strtok(NULL, ","); - fonts_count++; + int count = 0; + for (int i = 0; ret.addr[i]; i++) { + if (ret.addr[i] == ',') count++; + } + + if (count > 0) { + // Reallocate fallback_fonts to fit additional fonts from Xresources + fallback_fonts = realloc(fallback_fonts, (fonts_count + count + 2) * sizeof(char *)); + if (!fallback_fonts) { + printf("ERROR: can't load fonts from 'st.font_fallback' !\n"); + return; + } + + for (int i = 0; i <= count; i++) { + if (i == 0) + fallback_fonts[fonts_count + i] = strtok(ret.addr, ","); + else + fallback_fonts[fonts_count + i] = strtok(NULL, ","); + printf(" :: XRDB: adding fallback font: %s \n", fallback_fonts[fonts_count + i]); } - font2[endchar + count + 1] = '\0'; + + fonts_count += count + 1; + fallback_fonts[fonts_count] = NULL; // Null-terminate } else if (ret.addr) { - font2[endchar] = ret.addr; - fonts_count++; + fallback_fonts = realloc(fallback_fonts, (fonts_count + 2) * sizeof(char *)); + if (!fallback_fonts) { + printf("ERROR: can't load fonts from 'st.font_fallback' !\n"); + return; + } + + fallback_fonts[fonts_count++] = ret.addr; + fallback_fonts[fonts_count] = NULL; // Null-terminate } } @@ -139,6 +157,7 @@ reload(int sig) if (sig == -1) { return; } + printf(" :: XST:: reloading config...\n"); xrdb_load(); @@ -146,11 +165,14 @@ reload(int sig) xloadcols(); xunloadfonts(); xloadfonts(getusedfont(), 0); + xinitsparefonts(); + xloadsparefonts(); xsetcursor(cursorshape); /* pretend the window just got resized */ cresize(win.w, win.h); redraw(); + xhints(); /* triggers re-render if we're visible. */ ttywrite("\033[O", 3, 1); }