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
5.3 KiB

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