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.

285 lines
4.4 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. higher(sel);
  50. focus(sel);
  51. }
  52. else
  53. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  54. drawall();
  55. }
  56. void
  57. dotile(Arg *arg)
  58. {
  59. int n, i, w, h;
  60. Client *c;
  61. w = sw - mw;
  62. for(n = 0, c = clients; c; c = c->next)
  63. if(isvisible(c) && !c->isfloat)
  64. n++;
  65. if(n > 1)
  66. h = (sh - bh) / (n - 1);
  67. else
  68. h = sh - bh;
  69. for(i = 0, c = clients; c; c = c->next) {
  70. c->ismax = False;
  71. if(isvisible(c)) {
  72. if(c->isfloat) {
  73. higher(c);
  74. resize(c, True, TopLeft);
  75. continue;
  76. }
  77. if(n == 1) {
  78. c->x = sx;
  79. c->y = sy + bh;
  80. c->w = sw - 2;
  81. c->h = sh - 2 - bh;
  82. }
  83. else if(i == 0) {
  84. c->x = sx;
  85. c->y = sy + bh;
  86. c->w = mw - 2;
  87. c->h = sh - 2 - bh;
  88. }
  89. else if(h > bh) {
  90. c->x = sx + mw;
  91. c->y = sy + (i - 1) * h + bh;
  92. c->w = w - 2;
  93. if(i + 1 == n)
  94. c->h = sh - c->y - 2;
  95. else
  96. c->h = h - 2;
  97. }
  98. else { /* fallback if h < bh */
  99. c->x = sx + mw;
  100. c->y = sy + bh;
  101. c->w = w - 2;
  102. c->h = sh - 2 - bh;
  103. }
  104. resize(c, False, TopLeft);
  105. i++;
  106. }
  107. else
  108. ban(c);
  109. }
  110. if((sel = getnext(clients))) {
  111. higher(sel);
  112. focus(sel);
  113. }
  114. else
  115. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  116. drawall();
  117. }
  118. Client *
  119. getnext(Client *c)
  120. {
  121. for(; c && !isvisible(c); c = c->next);
  122. return c;
  123. }
  124. Client *
  125. getprev(Client *c)
  126. {
  127. for(; c && !isvisible(c); c = c->prev);
  128. return c;
  129. }
  130. void
  131. initrregs()
  132. {
  133. unsigned int i;
  134. regex_t *reg;
  135. if(rreg)
  136. return;
  137. len = sizeof(rule) / sizeof(rule[0]);
  138. rreg = emallocz(len * sizeof(RReg));
  139. for(i = 0; i < len; i++) {
  140. if(rule[i].clpattern) {
  141. reg = emallocz(sizeof(regex_t));
  142. if(regcomp(reg, rule[i].clpattern, 0))
  143. free(reg);
  144. else
  145. rreg[i].clregex = reg;
  146. }
  147. if(rule[i].tpattern) {
  148. reg = emallocz(sizeof(regex_t));
  149. if(regcomp(reg, rule[i].tpattern, 0))
  150. free(reg);
  151. else
  152. rreg[i].tregex = reg;
  153. }
  154. }
  155. }
  156. Bool
  157. isvisible(Client *c)
  158. {
  159. unsigned int i;
  160. for(i = 0; i < ntags; i++)
  161. if(c->tags[i] && seltag[i])
  162. return True;
  163. return False;
  164. }
  165. void
  166. replacetag(Arg *arg)
  167. {
  168. int i;
  169. if(!sel)
  170. return;
  171. for(i = 0; i < ntags; i++)
  172. sel->tags[i] = False;
  173. appendtag(arg);
  174. }
  175. void
  176. settags(Client *c)
  177. {
  178. char classinst[256];
  179. unsigned int i, j;
  180. regmatch_t tmp;
  181. Bool matched = False;
  182. XClassHint ch;
  183. if(XGetClassHint(dpy, c->win, &ch)) {
  184. snprintf(classinst, sizeof(classinst), "%s:%s",
  185. ch.res_class ? ch.res_class : "",
  186. ch.res_name ? ch.res_name : "");
  187. for(i = 0; !matched && i < len; i++)
  188. if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
  189. c->isfloat = rule[i].isfloat;
  190. for(j = 0; rreg[i].tregex && j < ntags; j++) {
  191. if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
  192. matched = True;
  193. c->tags[j] = True;
  194. }
  195. }
  196. }
  197. if(ch.res_class)
  198. XFree(ch.res_class);
  199. if(ch.res_name)
  200. XFree(ch.res_name);
  201. }
  202. if(!matched)
  203. for(i = 0; i < ntags; i++)
  204. c->tags[i] = seltag[i];
  205. }
  206. void
  207. togglemode(Arg *arg)
  208. {
  209. arrange = arrange == dofloat ? dotile : dofloat;
  210. arrange(NULL);
  211. }
  212. void
  213. view(Arg *arg)
  214. {
  215. unsigned int i;
  216. for(i = 0; i < ntags; i++)
  217. seltag[i] = False;
  218. seltag[arg->i] = True;
  219. arrange(NULL);
  220. drawall();
  221. }
  222. void
  223. toggleview(Arg *arg)
  224. {
  225. unsigned int i;
  226. seltag[arg->i] = !seltag[arg->i];
  227. for(i = 0; !seltag[i] && i < ntags; i++);
  228. if(i == ntags)
  229. seltag[arg->i] = True; /* cannot toggle last view */
  230. arrange(NULL);
  231. drawall();
  232. }
  233. void
  234. viewnext(Arg *arg)
  235. {
  236. unsigned int i;
  237. for(i = 0; !seltag[i]; i++);
  238. arg->i = (i < ntags-1) ? i+1 : 0;
  239. view(arg);
  240. }
  241. void
  242. viewprev(Arg *arg)
  243. {
  244. unsigned int i;
  245. for(i = 0; !seltag[i]; i++);
  246. arg->i = (i > 0) ? i-1 : ntags-1;
  247. view(arg);
  248. }