st-xresources-20200604-9ba7ecf.diff 4.7 KB

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