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.

376 lines
6.3 KiB

18 years ago
  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. togglebar(const char *arg) {
  239. if(bpos == BarOff)
  240. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  241. else
  242. bpos = BarOff;
  243. updatebarpos();
  244. arrange();
  245. }
  246. void
  247. togglefloating(const char *arg) {
  248. if(!sel)
  249. return;
  250. sel->isfloating = !sel->isfloating;
  251. if(sel->isfloating)
  252. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  253. arrange();
  254. }
  255. void
  256. togglemax(const char *arg) {
  257. XEvent ev;
  258. if(!sel || (!isfloating() && !sel->isfloating) || sel->isfixed)
  259. return;
  260. if((sel->ismax = !sel->ismax)) {
  261. sel->rx = sel->x;
  262. sel->ry = sel->y;
  263. sel->rw = sel->w;
  264. sel->rh = sel->h;
  265. resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
  266. }
  267. else
  268. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  269. drawbar();
  270. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  271. }
  272. void
  273. toggletag(const char *arg) {
  274. unsigned int i, j;
  275. if(!sel)
  276. return;
  277. i = idxoftag(arg);
  278. sel->tags[i] = !sel->tags[i];
  279. for(j = 0; j < ntags && !sel->tags[j]; j++);
  280. if(j == ntags)
  281. sel->tags[i] = True;
  282. arrange();
  283. }
  284. void
  285. toggleview(const char *arg) {
  286. unsigned int i, j;
  287. i = idxoftag(arg);
  288. seltags[i] = !seltags[i];
  289. for(j = 0; j < ntags && !seltags[j]; j++);
  290. if(j == ntags)
  291. seltags[i] = True; /* cannot toggle last view */
  292. arrange();
  293. }
  294. void
  295. updatebarpos(void) {
  296. XEvent ev;
  297. wax = sx;
  298. way = sy;
  299. wah = sh;
  300. waw = sw;
  301. switch(bpos) {
  302. default:
  303. wah -= bh;
  304. way += bh;
  305. XMoveWindow(dpy, barwin, sx, sy);
  306. break;
  307. case BarBot:
  308. wah -= bh;
  309. XMoveWindow(dpy, barwin, sx, sy + wah);
  310. break;
  311. case BarOff:
  312. XMoveWindow(dpy, barwin, sx, sy - bh);
  313. break;
  314. }
  315. XSync(dpy, False);
  316. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  317. }
  318. void
  319. view(const char *arg) {
  320. unsigned int i;
  321. for(i = 0; i < ntags; i++)
  322. seltags[i] = arg == NULL;
  323. i = idxoftag(arg);
  324. if(i >= 0 && i < ntags)
  325. seltags[i] = True;
  326. arrange();
  327. }