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.

311 lines
5.0 KiB

19 years ago
19 years ago
19 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. settitle(sel);
  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. if(!(n = 2 * (f + m))) {
  187. drawstatus();
  188. return;
  189. }
  190. if(nwins < n) {
  191. nwins = n;
  192. wins = erealloc(wins, nwins * sizeof(Window));
  193. }
  194. fi = 0;
  195. mi = 2 * f;
  196. if(sel->isfloat || arrange == dofloat) {
  197. wins[fi++] = sel->title;
  198. wins[fi++] = sel->win;
  199. }
  200. else {
  201. wins[mi++] = sel->title;
  202. wins[mi++] = sel->win;
  203. }
  204. for(c = clients; c; c = c->next)
  205. if(isvisible(c) && c != sel) {
  206. if(c->isfloat || arrange == dofloat) {
  207. wins[fi++] = c->title;
  208. wins[fi++] = c->win;
  209. }
  210. else {
  211. wins[mi++] = c->title;
  212. wins[mi++] = c->win;
  213. }
  214. }
  215. XRestackWindows(dpy, wins, n);
  216. drawall();
  217. XSync(dpy, False);
  218. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  219. }
  220. void
  221. settags(Client *c)
  222. {
  223. char classinst[256];
  224. unsigned int i, j;
  225. regmatch_t tmp;
  226. Bool matched = False;
  227. XClassHint ch;
  228. if(XGetClassHint(dpy, c->win, &ch)) {
  229. snprintf(classinst, sizeof(classinst), "%s:%s",
  230. ch.res_class ? ch.res_class : "",
  231. ch.res_name ? ch.res_name : "");
  232. for(i = 0; !matched && i < len; i++)
  233. if(rreg[i].clregex && !regexec(rreg[i].clregex, classinst, 1, &tmp, 0)) {
  234. c->isfloat = rule[i].isfloat;
  235. for(j = 0; rreg[i].tregex && j < ntags; j++) {
  236. if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) {
  237. matched = True;
  238. c->tags[j] = True;
  239. }
  240. }
  241. }
  242. if(ch.res_class)
  243. XFree(ch.res_class);
  244. if(ch.res_name)
  245. XFree(ch.res_name);
  246. }
  247. if(!matched)
  248. for(i = 0; i < ntags; i++)
  249. c->tags[i] = seltag[i];
  250. }
  251. void
  252. togglemode(Arg *arg)
  253. {
  254. arrange = arrange == dofloat ? dotile : dofloat;
  255. arrange(NULL);
  256. }
  257. void
  258. view(Arg *arg)
  259. {
  260. unsigned int i;
  261. for(i = 0; i < ntags; i++)
  262. seltag[i] = False;
  263. seltag[arg->i] = True;
  264. arrange(NULL);
  265. }
  266. void
  267. toggleview(Arg *arg)
  268. {
  269. unsigned int i;
  270. seltag[arg->i] = !seltag[arg->i];
  271. for(i = 0; !seltag[i] && i < ntags; i++);
  272. if(i == ntags)
  273. seltag[arg->i] = True; /* cannot toggle last view */
  274. arrange(NULL);
  275. }