You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
5.8 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include "dwm.h"
  3. #include <regex.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <X11/Xutil.h>
  8. /* static */
  9. typedef struct {
  10. const char *symbol;
  11. void (*arrange)(void);
  12. } Layout;
  13. typedef struct {
  14. const char *prop;
  15. const char *tags;
  16. Bool isfloating;
  17. } Rule;
  18. typedef struct {
  19. regex_t *propregex;
  20. regex_t *tagregex;
  21. } Regs;
  22. TAGS
  23. RULES
  24. static unsigned int nrules = 0;
  25. static unsigned int nlayouts = 0;
  26. static unsigned int ltidx = 0; /* default */
  27. static Regs *regs = NULL;
  28. static unsigned int
  29. idxoftag(const char *tag) {
  30. unsigned int i;
  31. for(i = 0; i < ntags; i++)
  32. if(tags[i] == tag)
  33. return i;
  34. return 0;
  35. }
  36. static void
  37. floating(void) { /* default floating layout */
  38. Client *c;
  39. for(c = clients; c; c = c->next)
  40. if(isvisible(c))
  41. resize(c, c->x, c->y, c->w, c->h, True);
  42. }
  43. LAYOUTS
  44. /* extern */
  45. unsigned int blw = 0;
  46. void
  47. applyrules(Client *c) {
  48. static char buf[512];
  49. unsigned int i, j;
  50. regmatch_t tmp;
  51. Bool matched = False;
  52. XClassHint ch = { 0 };
  53. /* rule matching */
  54. XGetClassHint(dpy, c->win, &ch);
  55. snprintf(buf, sizeof buf, "%s:%s:%s",
  56. ch.res_class ? ch.res_class : "",
  57. ch.res_name ? ch.res_name : "", c->name);
  58. for(i = 0; i < nrules; i++)
  59. if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
  60. c->isfloating = rules[i].isfloating;
  61. for(j = 0; regs[i].tagregex && j < ntags; j++) {
  62. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  63. matched = True;
  64. c->tags[j] = True;
  65. }
  66. }
  67. }
  68. if(ch.res_class)
  69. XFree(ch.res_class);
  70. if(ch.res_name)
  71. XFree(ch.res_name);
  72. if(!matched)
  73. for(i = 0; i < ntags; i++)
  74. c->tags[i] = seltags[i];
  75. }
  76. void
  77. arrange(void) {
  78. Client *c;
  79. for(c = clients; c; c = c->next)
  80. if(isvisible(c))
  81. unban(c);
  82. else
  83. ban(c);
  84. layouts[ltidx].arrange();
  85. focus(NULL);
  86. restack();
  87. }
  88. void
  89. compileregs(void) {
  90. unsigned int i;
  91. regex_t *reg;
  92. if(regs)
  93. return;
  94. nrules = sizeof rules / sizeof rules[0];
  95. regs = emallocz(nrules * sizeof(Regs));
  96. for(i = 0; i < nrules; i++) {
  97. if(rules[i].prop) {
  98. reg = emallocz(sizeof(regex_t));
  99. if(regcomp(reg, rules[i].prop, REG_EXTENDED))
  100. free(reg);
  101. else
  102. regs[i].propregex = reg;
  103. }
  104. if(rules[i].tags) {
  105. reg = emallocz(sizeof(regex_t));
  106. if(regcomp(reg, rules[i].tags, REG_EXTENDED))
  107. free(reg);
  108. else
  109. regs[i].tagregex = reg;
  110. }
  111. }
  112. }
  113. void
  114. focusnext(const char *arg) {
  115. Client *c;
  116. if(!sel)
  117. return;
  118. for(c = sel->next; c && !isvisible(c); c = c->next);
  119. if(!c)
  120. for(c = clients; c && !isvisible(c); c = c->next);
  121. if(c) {
  122. focus(c);
  123. restack();
  124. }
  125. }
  126. void
  127. focusprev(const char *arg) {
  128. Client *c;
  129. if(!sel)
  130. return;
  131. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  132. if(!c) {
  133. for(c = clients; c && c->next; c = c->next);
  134. for(; c && !isvisible(c); c = c->prev);
  135. }
  136. if(c) {
  137. focus(c);
  138. restack();
  139. }
  140. }
  141. const char *
  142. getsymbol(void)
  143. {
  144. return layouts[ltidx].symbol;
  145. }
  146. void
  147. initlayouts(void) {
  148. unsigned int i, w;
  149. nlayouts = sizeof layouts / sizeof layouts[0];
  150. for(blw = i = 0; i < nlayouts; i++) {
  151. w = textw(layouts[i].symbol);
  152. if(w > blw)
  153. blw = w;
  154. }
  155. }
  156. Bool
  157. isfloating(void) {
  158. return layouts[ltidx].arrange == floating;
  159. }
  160. Bool
  161. isarrange(void (*func)())
  162. {
  163. return func == layouts[ltidx].arrange;
  164. }
  165. Bool
  166. isvisible(Client *c) {
  167. unsigned int i;
  168. for(i = 0; i < ntags; i++)
  169. if(c->tags[i] && seltags[i])
  170. return True;
  171. return False;
  172. }
  173. Client *
  174. nexttiled(Client *c) {
  175. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  176. return c;
  177. }
  178. void
  179. restack(void) {
  180. Client *c;
  181. XEvent ev;
  182. XWindowChanges wc;
  183. drawbar();
  184. if(!sel)
  185. return;
  186. if(sel->isfloating || isfloating())
  187. XRaiseWindow(dpy, sel->win);
  188. if(!isfloating()) {
  189. wc.stack_mode = Below;
  190. wc.sibling = barwin;
  191. if(!sel->isfloating) {
  192. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  193. wc.sibling = sel->win;
  194. }
  195. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  196. if(c == sel)
  197. continue;
  198. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  199. wc.sibling = c->win;
  200. }
  201. }
  202. XSync(dpy, False);
  203. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  204. }
  205. void
  206. setlayout(const char *arg) {
  207. unsigned int i;
  208. if(!arg) {
  209. if(++ltidx == nlayouts)
  210. ltidx = 0;;
  211. }
  212. else {
  213. for(i = 0; i < nlayouts; i++)
  214. if(!strcmp(arg, layouts[i].symbol))
  215. break;
  216. if(i == nlayouts)
  217. return;
  218. ltidx = i;
  219. }
  220. if(sel)
  221. arrange();
  222. else
  223. drawbar();
  224. }
  225. void
  226. tag(const char *arg) {
  227. unsigned int i;
  228. if(!sel)
  229. return;
  230. for(i = 0; i < ntags; i++)
  231. sel->tags[i] = arg == NULL;
  232. i = idxoftag(arg);
  233. if(i >= 0 && i < ntags)
  234. sel->tags[i] = True;
  235. arrange();
  236. }
  237. void
  238. togglefloating(const char *arg) {
  239. if(!sel)
  240. return;
  241. sel->isfloating = !sel->isfloating;
  242. if(sel->isfloating)
  243. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  244. arrange();
  245. }
  246. void
  247. togglemax(const char *arg) {
  248. XEvent ev;
  249. if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed)
  250. return;
  251. if((sel->ismax = !sel->ismax)) {
  252. sel->rx = sel->x;
  253. sel->ry = sel->y;
  254. sel->rw = sel->w;
  255. sel->rh = sel->h;
  256. resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
  257. }
  258. else
  259. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  260. drawbar();
  261. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  262. }
  263. void
  264. toggletag(const char *arg) {
  265. unsigned int i, j;
  266. if(!sel)
  267. return;
  268. i = idxoftag(arg);
  269. sel->tags[i] = !sel->tags[i];
  270. for(j = 0; j < ntags && !sel->tags[j]; j++);
  271. if(j == ntags)
  272. sel->tags[i] = True;
  273. arrange();
  274. }
  275. void
  276. toggleview(const char *arg) {
  277. unsigned int i, j;
  278. i = idxoftag(arg);
  279. seltags[i] = !seltags[i];
  280. for(j = 0; j < ntags && !seltags[j]; j++);
  281. if(j == ntags)
  282. seltags[i] = True; /* cannot toggle last view */
  283. arrange();
  284. }
  285. void
  286. view(const char *arg) {
  287. unsigned int i;
  288. for(i = 0; i < ntags; i++)
  289. seltags[i] = arg == NULL;
  290. i = idxoftag(arg);
  291. if(i >= 0 && i < ntags)
  292. seltags[i] = True;
  293. arrange();
  294. }