st-xresources-20180309-c5ba9c0.diff (4798B)
1 From 9ba79c106145759de6b436dac2e4b8ff5a12bc2e Mon Sep 17 00:00:00 2001 2 From: "Devin J. Pohly" <djpohly@gmail.com> 3 Date: Fri, 9 Mar 2018 14:56:34 -0600 4 Subject: [PATCH] Port Xresources patch by @dcat to st-git 5 6 --- 7 config.def.h | 35 +++++++++++++++++++++++++++ 8 x.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 9 2 files changed, 109 insertions(+), 4 deletions(-) 10 11 diff --git a/config.def.h b/config.def.h 12 index 82b1b09..471fe57 100644 13 --- a/config.def.h 14 +++ b/config.def.h 15 @@ -150,6 +150,41 @@ static unsigned int mousebg = 0; 16 */ 17 static unsigned int defaultattr = 11; 18 19 +/* 20 + * Xresources preferences to load at startup 21 + */ 22 +ResourcePref resources[] = { 23 + { "font", STRING, &font }, 24 + { "color0", STRING, &colorname[0] }, 25 + { "color1", STRING, &colorname[1] }, 26 + { "color2", STRING, &colorname[2] }, 27 + { "color3", STRING, &colorname[3] }, 28 + { "color4", STRING, &colorname[4] }, 29 + { "color5", STRING, &colorname[5] }, 30 + { "color6", STRING, &colorname[6] }, 31 + { "color7", STRING, &colorname[7] }, 32 + { "color8", STRING, &colorname[8] }, 33 + { "color9", STRING, &colorname[9] }, 34 + { "color10", STRING, &colorname[10] }, 35 + { "color11", STRING, &colorname[11] }, 36 + { "color12", STRING, &colorname[12] }, 37 + { "color13", STRING, &colorname[13] }, 38 + { "color14", STRING, &colorname[14] }, 39 + { "color15", STRING, &colorname[15] }, 40 + { "background", STRING, &colorname[256] }, 41 + { "foreground", STRING, &colorname[257] }, 42 + { "cursorColor", STRING, &colorname[258] }, 43 + { "termname", STRING, &termname }, 44 + { "shell", STRING, &shell }, 45 + { "xfps", INTEGER, &xfps }, 46 + { "actionfps", INTEGER, &actionfps }, 47 + { "blinktimeout", INTEGER, &blinktimeout }, 48 + { "bellvolume", INTEGER, &bellvolume }, 49 + { "tabspaces", INTEGER, &tabspaces }, 50 + { "cwscale", FLOAT, &cwscale }, 51 + { "chscale", FLOAT, &chscale }, 52 +}; 53 + 54 /* 55 * Internal mouse shortcuts. 56 * Beware that overloading Button1 will disable the selection. 57 diff --git a/x.c b/x.c 58 index d43a529..7f29cc2 100644 59 --- a/x.c 60 +++ b/x.c 61 @@ -14,6 +14,7 @@ 62 #include <X11/keysym.h> 63 #include <X11/Xft/Xft.h> 64 #include <X11/XKBlib.h> 65 +#include <X11/Xresource.h> 66 67 static char *argv0; 68 #include "arg.h" 69 @@ -43,6 +44,19 @@ typedef struct { 70 signed char appcursor; /* application cursor */ 71 } Key; 72 73 +/* Xresources preferences */ 74 +enum resource_type { 75 + STRING = 0, 76 + INTEGER = 1, 77 + FLOAT = 2 78 +}; 79 + 80 +typedef struct { 81 + char *name; 82 + enum resource_type type; 83 + void *dst; 84 +} ResourcePref; 85 + 86 /* X modifiers */ 87 #define XK_ANY_MOD UINT_MAX 88 #define XK_NO_MOD 0 89 @@ -778,8 +792,8 @@ xclear(int x1, int y1, int x2, int y2) 90 void 91 xhints(void) 92 { 93 - XClassHint class = {opt_name ? opt_name : termname, 94 - opt_class ? opt_class : termname}; 95 + XClassHint class = {opt_name ? opt_name : "st", 96 + opt_class ? opt_class : "St"}; 97 XWMHints wm = {.flags = InputHint, .input = 1}; 98 XSizeHints *sizeh; 99 100 @@ -999,8 +1013,6 @@ xinit(int cols, int rows) 101 pid_t thispid = getpid(); 102 XColor xmousefg, xmousebg; 103 104 - if (!(xw.dpy = XOpenDisplay(NULL))) 105 - die("Can't open display\n"); 106 xw.scr = XDefaultScreen(xw.dpy); 107 xw.vis = XDefaultVisual(xw.dpy, xw.scr); 108 109 @@ -1864,6 +1876,59 @@ run(void) 110 } 111 } 112 113 +int 114 +resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) 115 +{ 116 + char **sdst = dst; 117 + int *idst = dst; 118 + float *fdst = dst; 119 + 120 + char fullname[256]; 121 + char fullclass[256]; 122 + char *type; 123 + XrmValue ret; 124 + 125 + snprintf(fullname, sizeof(fullname), "%s.%s", 126 + opt_name ? opt_name : "st", name); 127 + snprintf(fullclass, sizeof(fullclass), "%s.%s", 128 + opt_class ? opt_class : "St", name); 129 + fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0'; 130 + 131 + XrmGetResource(db, fullname, fullclass, &type, &ret); 132 + if (ret.addr == NULL || strncmp("String", type, 64)) 133 + return 1; 134 + 135 + switch (rtype) { 136 + case STRING: 137 + *sdst = ret.addr; 138 + break; 139 + case INTEGER: 140 + *idst = strtoul(ret.addr, NULL, 10); 141 + break; 142 + case FLOAT: 143 + *fdst = strtof(ret.addr, NULL); 144 + break; 145 + } 146 + return 0; 147 +} 148 + 149 +void 150 +config_init(void) 151 +{ 152 + char *resm; 153 + XrmDatabase db; 154 + ResourcePref *p; 155 + 156 + XrmInitialize(); 157 + resm = XResourceManagerString(xw.dpy); 158 + if (!resm) 159 + return; 160 + 161 + db = XrmGetStringDatabase(resm); 162 + for (p = resources; p < resources + LEN(resources); p++) 163 + resource_load(db, p->name, p->type, p->dst); 164 +} 165 + 166 void 167 usage(void) 168 { 169 @@ -1937,6 +2002,11 @@ run: 170 } 171 setlocale(LC_CTYPE, ""); 172 XSetLocaleModifiers(""); 173 + 174 + if(!(xw.dpy = XOpenDisplay(NULL))) 175 + die("Can't open display\n"); 176 + 177 + config_init(); 178 cols = MAX(cols, 1); 179 rows = MAX(rows, 1); 180 tnew(cols, rows); 181 -- 182 2.16.2 183