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.

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