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.

328 lines
6.1 KiB

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