st-xresources-signal-reloading-20220407-ef05519.diff 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. From b2a9c96cc3c9152c4e8188f341606c914741cb50 Mon Sep 17 00:00:00 2001
  2. From: wael <40663@protonmail.com>
  3. Date: Thu, 7 Apr 2022 17:14:02 +0300
  4. Subject: [PATCH] fix xresources with signal reloading removing arg.h and st.h
  5. & remove unneccesary xresources variables(?)
  6. ---
  7. x.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  8. 1 file changed, 115 insertions(+)
  9. diff --git a/x.c b/x.c
  10. index 2a3bd38..e8fe7ad 100644
  11. --- a/x.c
  12. +++ b/x.c
  13. @@ -14,6 +14,7 @@
  14. #include <X11/keysym.h>
  15. #include <X11/Xft/Xft.h>
  16. #include <X11/XKBlib.h>
  17. +#include <X11/Xresource.h>
  18. char *argv0;
  19. #include "arg.h"
  20. @@ -2011,6 +2012,118 @@ run(void)
  21. }
  22. }
  23. +
  24. +#define XRESOURCE_LOAD_META(NAME) \
  25. + if(!XrmGetResource(xrdb, "st." NAME, "st." NAME, &type, &ret)) \
  26. + XrmGetResource(xrdb, "*." NAME, "*." NAME, &type, &ret); \
  27. + if (ret.addr != NULL && !strncmp("String", type, 64))
  28. +
  29. +#define XRESOURCE_LOAD_STRING(NAME, DST) \
  30. + XRESOURCE_LOAD_META(NAME) \
  31. + DST = ret.addr;
  32. +
  33. +#define XRESOURCE_LOAD_CHAR(NAME, DST) \
  34. + XRESOURCE_LOAD_META(NAME) \
  35. + DST = ret.addr[0];
  36. +
  37. +#define XRESOURCE_LOAD_INTEGER(NAME, DST) \
  38. + XRESOURCE_LOAD_META(NAME) \
  39. + DST = strtoul(ret.addr, NULL, 10);
  40. +
  41. +#define XRESOURCE_LOAD_FLOAT(NAME, DST) \
  42. + XRESOURCE_LOAD_META(NAME) \
  43. + DST = strtof(ret.addr, NULL);
  44. +
  45. +void
  46. +xrdb_load(void)
  47. +{
  48. + /* XXX */
  49. + char *xrm;
  50. + char *type;
  51. + XrmDatabase xrdb;
  52. + XrmValue ret;
  53. + Display *dpy;
  54. +
  55. + if(!(dpy = XOpenDisplay(NULL)))
  56. + die("Can't open display\n");
  57. +
  58. + XrmInitialize();
  59. + xrm = XResourceManagerString(dpy);
  60. +
  61. + if (xrm != NULL) {
  62. + xrdb = XrmGetStringDatabase(xrm);
  63. +
  64. + /* handling colors here without macros to do via loop. */
  65. + int i = 0;
  66. + char loadValue[12] = "";
  67. + for (i = 0; i < 256; i++)
  68. + {
  69. + sprintf(loadValue, "%s%d", "st.color", i);
  70. +
  71. + if(!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret))
  72. + {
  73. + sprintf(loadValue, "%s%d", "*.color", i);
  74. + if (!XrmGetResource(xrdb, loadValue, loadValue, &type, &ret))
  75. + /* reset if not found (unless in range for defaults). */
  76. + if (i > 15)
  77. + colorname[i] = NULL;
  78. + }
  79. +
  80. + if (ret.addr != NULL && !strncmp("String", type, 64))
  81. + colorname[i] = ret.addr;
  82. + }
  83. +
  84. + XRESOURCE_LOAD_STRING("foreground", colorname[defaultfg]);
  85. + XRESOURCE_LOAD_STRING("background", colorname[defaultbg]);
  86. + XRESOURCE_LOAD_STRING("cursorColor", colorname[defaultcs])
  87. + else {
  88. + // this looks confusing because we are chaining off of the if
  89. + // in the macro. probably we should be wrapping everything blocks
  90. + // so this isn't possible...
  91. + defaultcs = defaultfg;
  92. + }
  93. + XRESOURCE_LOAD_STRING("reverse-cursor", colorname[defaultrcs])
  94. + else {
  95. + // see above.
  96. + defaultrcs = defaultbg;
  97. + }
  98. +
  99. + XRESOURCE_LOAD_STRING("font", font);
  100. + XRESOURCE_LOAD_STRING("termname", termname);
  101. +
  102. + XRESOURCE_LOAD_INTEGER("blinktimeout", blinktimeout);
  103. + XRESOURCE_LOAD_INTEGER("bellvolume", bellvolume);
  104. + XRESOURCE_LOAD_INTEGER("borderpx", borderpx);
  105. + XRESOURCE_LOAD_INTEGER("cursorshape", cursorshape);
  106. +
  107. + XRESOURCE_LOAD_FLOAT("cwscale", cwscale);
  108. + XRESOURCE_LOAD_FLOAT("chscale", chscale);
  109. + }
  110. + XFlush(dpy);
  111. +}
  112. +
  113. +void
  114. +reload(int sig)
  115. +{
  116. + xrdb_load();
  117. +
  118. + /* colors, fonts */
  119. + xloadcols();
  120. + xunloadfonts();
  121. + xloadfonts(font, 0);
  122. +
  123. + /* pretend the window just got resized */
  124. + cresize(win.w, win.h);
  125. +
  126. + redraw();
  127. +
  128. + /* triggers re-render if we're visible. */
  129. + ttywrite("\033[O", 3, 1);
  130. +
  131. + signal(SIGUSR1, reload);
  132. +}
  133. +
  134. +
  135. void
  136. usage(void)
  137. {
  138. @@ -2084,6 +2197,8 @@ run:
  139. setlocale(LC_CTYPE, "");
  140. XSetLocaleModifiers("");
  141. + xrdb_load();
  142. + signal(SIGUSR1, reload);
  143. cols = MAX(cols, 1);
  144. rows = MAX(rows, 1);
  145. tnew(cols, rows);
  146. --
  147. 2.35.1