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.

309 lines
4.9 KiB

18 years ago
18 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <regex.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <X11/Xutil.h>
  12. typedef struct {
  13. const char *clpattern;
  14. const char *tpattern;
  15. Bool isfloat;
  16. } Rule;
  17. typedef struct {
  18. regex_t *clregex;
  19. regex_t *tregex;
  20. } RReg;
  21. /* static */
  22. TAGS
  23. RULES
  24. static RReg *rreg = NULL;
  25. static unsigned int len = 0;
  26. void (*arrange)(Arg *) = DEFMODE;
  27. /* extern */
  28. void
  29. appendtag(Arg *arg)
  30. {
  31. if(!sel)
  32. return;
  33. sel->tags[arg->i] = True;
  34. arrange(NULL);
  35. }
  36. void
  37. dofloat(Arg *arg)
  38. {
  39. Client *c;
  40. for(c = clients; c; c = c->next) {
  41. c->ismax = False;
  42. if(isvisible(c)) {
  43. resize(c, True, TopLeft);
  44. }
  45. else
  46. ban(c);
  47. }
  48. if((sel = getnext(clients))) {
  49. focus(sel);
  50. restack();
  51. }
  52. else
  53. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  54. }
  55. void
  56. dotile(Arg *arg)
  57. {
  58. int h, i, n, w;
  59. Client *c;
  60. w = sw - mw;
  61. for(n = 0, c = clients; c; c = c->next)
  62. if(isvisible(c) && !c->isfloat)
  63. n++;
  64. if(n > 1)
  65. h = (sh - bh) / (n - 1);
  66. else
  67. h = sh - bh;
  68. for(i = 0, c = clients; c; c = c->next) {
  69. c->ismax = False;
  70. if(isvisible(c)) {
  71. if(c->isfloat) {
  72. resize(c, True, TopLeft);
  73. continue;
  74. }
  75. if(n == 1) {
  76. c->x = sx;
  77. c->y = sy + bh;
  78. c->w = sw - 2;
  79. c->h = sh - 2 - bh;
  80. }
  81. else if(i == 0) {
  82. c->x = sx;
  83. c->y = sy + bh;
  84. c->w = mw - 2;
  85. c->h = sh - 2 - bh;
  86. }
  87. else if(h > bh) {
  88. c->x = sx + mw;
  89. c->y = sy + (i - 1) * h + bh;
  90. c->w = w - 2;
  91. if(i + 1 == n)
  92. c->h = sh - c->y - 2;
  93. else
  94. c->h = h - 2;
  95. }
  96. else { /* fallback if h < bh */
  97. c->x = sx + mw;
  98. c->y = sy + bh;
  99. c->w = w - 2;
  100. c->h = sh - 2 - bh;
  101. }
  102. resize(c, False, TopLeft);
  103. i++;
  104. }
  105. else
  106. ban(c);
  107. }
  108. if((sel = getnext(clients)))
  109. focus(sel);
  110. else
  111. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  112. restack();
  113. }
  114. Client *
  115. getnext(Client *c)
  116. {
  117. for(; c && !isvisible(c); c = c->next);
  118. return c;
  119. }
  120. Client *
  121. getprev(Client *c)
  122. {
  123. for(; c && !isvisible(c); c = c->prev);
  124. return c;
  125. }
  126. void
  127. initrregs()
  128. {
  129. unsigned int i;
  130. regex_t *reg;
  131. if(rreg)
  132. return;
  133. len = sizeof(rule) / sizeof(rule[0]);
  134. rreg = emallocz(len * sizeof(RReg));
  135. for(i = 0; i < len; i++) {
  136. if(rule[i].clpattern) {
  137. reg = emallocz(sizeof(regex_t));
  138. if(regcomp(reg, rule[i].clpattern, 0))
  139. free(reg);
  140. else
  141. rreg[i].clregex = reg;
  142. }
  143. if(rule[i].tpattern) {
  144. reg = emallocz(sizeof(regex_t));
  145. if(regcomp(reg, rule[i].tpattern, 0))
  146. free(reg);
  147. else
  148. rreg[i].tregex = reg;
  149. }
  150. }
  151. }
  152. Bool
  153. isvisible(Client *c)
  154. {
  155. unsigned int i;
  156. for(i = 0; i < ntags; i++)
  157. if(c->tags[i] && seltag[i])
  158. return True;
  159. return False;
  160. }
  161. void
  162. replacetag(Arg *arg)
  163. {
  164. int i;
  165. if(!sel)
  166. return;
  167. for(i = 0; i < ntags; i++)
  168. sel->tags[i] = False;
  169. appendtag(arg);
  170. }
  171. void
  172. restack()
  173. {
  174. static unsigned int nwins = 0;
  175. static Window *wins = NULL;
  176. unsigned int f, fi, m, mi, n;
  177. Client *c;
  178. XEvent ev;
  179. for(f = 0, m = 0, c = clients; c; c = c->next)
  180. if(isvisible(c)) {
  181. if(c->isfloat || arrange == dofloat)
  182. f++;
  183. else
  184. m++;
  185. }
  186. n = 2 * (f + m);
  187. if(nwins < n) {
  188. nwins = n;
  189. wins = erealloc(wins, nwins * sizeof(Window));
  190. }
  191. fi = 0;
  192. mi = 2 * f;
  193. if(sel->isfloat || arrange == dofloat) {
  194. wins[fi++] = sel->title;
  195. wins[fi++] = sel->win;
  196. }
  197. else {
  198. wins[mi++] = sel->title;
  199. wins[mi++] = sel->win;
  200. }
  201. for(c = clients; c; c = c->next)
  202. if(isvisible(c) && c != sel) {
  203. if(c->isfloat || arrange == dofloat) {
  204. wins[fi++] = c->title;
  205. wins[fi++] = c->win;
  206. }
  207. else {
  208. wins[mi++] = c->title;
  209. wins[mi++] = c->win;
  210. }
  211. }
  212. XRestackWindows(dpy, wins, n);
  213. drawall();
  214. XSync(dpy, False);
  215. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  216. }
  217. void
  218. settags(Client *c)
  219. {
  220. char classinst[256];
  221. unsigned int i, j;
  222. regmatch_t tmp;
  223. Bool matched = False;
  224. XClassHint ch;
  225. if(XGetClassHint(dpy, c->win, &ch)) {
  226. snprintf(classinst, sizeof(classinst), "%s:%s",
  227. ch.res_class ? ch.res_class : "",
  228. ch.res_name ? ch.res_name : "");
  229. for(i = 0; !matched && i < len; i++)
  230. if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
  231. c->isfloat = rule[i].isfloat;
  232. for(j = 0; rreg[i].tregex && j < ntags; j++) {
  233. if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
  234. matched = True;
  235. c->tags[j] = True;
  236. }
  237. }
  238. }
  239. if(ch.res_class)
  240. XFree(ch.res_class);
  241. if(ch.res_name)
  242. XFree(ch.res_name);
  243. }
  244. if(!matched)
  245. for(i = 0; i < ntags; i++)
  246. c->tags[i] = seltag[i];
  247. }
  248. void
  249. togglemode(Arg *arg)
  250. {
  251. arrange = arrange == dofloat ? dotile : dofloat;
  252. arrange(NULL);
  253. }
  254. void
  255. view(Arg *arg)
  256. {
  257. unsigned int i;
  258. for(i = 0; i < ntags; i++)
  259. seltag[i] = False;
  260. seltag[arg->i] = True;
  261. arrange(NULL);
  262. }
  263. void
  264. toggleview(Arg *arg)
  265. {
  266. unsigned int i;
  267. seltag[arg->i] = !seltag[arg->i];
  268. for(i = 0; !seltag[i] && i < ntags; i++);
  269. if(i == ntags)
  270. seltag[arg->i] = True; /* cannot toggle last view */
  271. arrange(NULL);
  272. }