Configuration file for DWM on MacBook Air
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.

409 lines
7.0 KiB

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