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