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.

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