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.

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