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.

321 lines
5.9 KiB

  1. /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <regex.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <X11/Xutil.h>
  11. unsigned int master = MASTER;
  12. unsigned int nmaster = NMASTER;
  13. unsigned int blw = 0;
  14. Layout *lt = NULL;
  15. /* static */
  16. typedef struct {
  17. const char *prop;
  18. const char *tags;
  19. Bool swimming;
  20. } Rule;
  21. typedef struct {
  22. regex_t *propregex;
  23. regex_t *tagregex;
  24. } Regs;
  25. TAGS
  26. RULES
  27. static Regs *regs = NULL;
  28. static unsigned int nrules = 0;
  29. static unsigned int nlayouts = 0;
  30. static void
  31. tile(void) {
  32. unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
  33. Client *c;
  34. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  35. n++;
  36. /* window geoms */
  37. mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
  38. mw = (n > nmaster) ? (waw * master) / 1000 : waw;
  39. th = (n > nmaster) ? wah / (n - nmaster) : 0;
  40. tw = waw - mw;
  41. for(i = 0, c = clients; c; c = c->next)
  42. if(isvisible(c)) {
  43. if(c->isbanned)
  44. XMoveWindow(dpy, c->win, c->x, c->y);
  45. c->isbanned = False;
  46. if(c->swimming)
  47. continue;
  48. c->ismax = False;
  49. nx = wax;
  50. ny = way;
  51. if(i < nmaster) {
  52. ny += i * mh;
  53. nw = mw - 2 * BORDERPX;
  54. nh = mh - 2 * BORDERPX;
  55. }
  56. else { /* tile window */
  57. nx += mw;
  58. nw = tw - 2 * BORDERPX;
  59. if(th > 2 * BORDERPX) {
  60. ny += (i - nmaster) * th;
  61. nh = th - 2 * BORDERPX;
  62. }
  63. else /* fallback if th <= 2 * BORDERPX */
  64. nh = wah - 2 * BORDERPX;
  65. }
  66. resize(c, nx, ny, nw, nh, False);
  67. i++;
  68. }
  69. else {
  70. c->isbanned = True;
  71. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  72. }
  73. if(!sel || !isvisible(sel)) {
  74. for(c = stack; c && !isvisible(c); c = c->snext);
  75. focus(c);
  76. }
  77. restack();
  78. }
  79. LAYOUTS
  80. /* extern */
  81. void
  82. compileregs(void) {
  83. unsigned int i;
  84. regex_t *reg;
  85. if(regs)
  86. return;
  87. nrules = sizeof rule / sizeof rule[0];
  88. regs = emallocz(nrules * sizeof(Regs));
  89. for(i = 0; i < nrules; i++) {
  90. if(rule[i].prop) {
  91. reg = emallocz(sizeof(regex_t));
  92. if(regcomp(reg, rule[i].prop, REG_EXTENDED))
  93. free(reg);
  94. else
  95. regs[i].propregex = reg;
  96. }
  97. if(rule[i].tags) {
  98. reg = emallocz(sizeof(regex_t));
  99. if(regcomp(reg, rule[i].tags, REG_EXTENDED))
  100. free(reg);
  101. else
  102. regs[i].tagregex = reg;
  103. }
  104. }
  105. }
  106. void
  107. incnmaster(Arg *arg) {
  108. if((lt->arrange != tile) || (nmaster + arg->i < 1)
  109. || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
  110. return;
  111. nmaster += arg->i;
  112. if(sel)
  113. lt->arrange();
  114. else
  115. drawstatus();
  116. }
  117. void
  118. initlayouts(void) {
  119. unsigned int i, w;
  120. lt = &layout[0];
  121. nlayouts = sizeof layout / sizeof layout[0];
  122. for(blw = i = 0; i < nlayouts; i++) {
  123. w = textw(layout[i].symbol);
  124. if(w > blw)
  125. blw = w;
  126. }
  127. }
  128. Bool
  129. isvisible(Client *c) {
  130. unsigned int i;
  131. for(i = 0; i < ntags; i++)
  132. if(c->tags[i] && seltag[i])
  133. return True;
  134. return False;
  135. }
  136. void
  137. resizemaster(Arg *arg) {
  138. if(lt->arrange != tile)
  139. return;
  140. if(arg->i == 0)
  141. master = MASTER;
  142. else {
  143. if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
  144. || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
  145. return;
  146. master += arg->i;
  147. }
  148. lt->arrange();
  149. }
  150. void
  151. restack(void) {
  152. Client *c;
  153. XEvent ev;
  154. drawstatus();
  155. if(!sel)
  156. return;
  157. if(sel->swimming || lt->arrange == swim)
  158. XRaiseWindow(dpy, sel->win);
  159. if(lt->arrange != swim) {
  160. if(!sel->swimming)
  161. XLowerWindow(dpy, sel->win);
  162. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  163. if(c == sel)
  164. continue;
  165. XLowerWindow(dpy, c->win);
  166. }
  167. }
  168. XSync(dpy, False);
  169. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  170. }
  171. void
  172. settags(Client *c, Client *trans) {
  173. char prop[512];
  174. unsigned int i, j;
  175. regmatch_t tmp;
  176. Bool matched = trans != NULL;
  177. XClassHint ch = { 0 };
  178. if(matched)
  179. for(i = 0; i < ntags; i++)
  180. c->tags[i] = trans->tags[i];
  181. else {
  182. XGetClassHint(dpy, c->win, &ch);
  183. snprintf(prop, sizeof prop, "%s:%s:%s",
  184. ch.res_class ? ch.res_class : "",
  185. ch.res_name ? ch.res_name : "", c->name);
  186. for(i = 0; i < nrules; i++)
  187. if(regs[i].propregex && !regexec(regs[i].propregex, prop, 1, &tmp, 0)) {
  188. c->swimming = rule[i].swimming;
  189. for(j = 0; regs[i].tagregex && j < ntags; j++) {
  190. if(!regexec(regs[i].tagregex, tags[j], 1, &tmp, 0)) {
  191. matched = True;
  192. c->tags[j] = True;
  193. }
  194. }
  195. }
  196. if(ch.res_class)
  197. XFree(ch.res_class);
  198. if(ch.res_name)
  199. XFree(ch.res_name);
  200. }
  201. if(!matched)
  202. for(i = 0; i < ntags; i++)
  203. c->tags[i] = seltag[i];
  204. }
  205. void
  206. swim(void) {
  207. Client *c;
  208. for(c = clients; c; c = c->next) {
  209. if(isvisible(c)) {
  210. if(c->isbanned)
  211. XMoveWindow(dpy, c->win, c->x, c->y);
  212. c->isbanned = False;
  213. resize(c, c->x, c->y, c->w, c->h, True);
  214. }
  215. else {
  216. c->isbanned = True;
  217. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  218. }
  219. }
  220. if(!sel || !isvisible(sel)) {
  221. for(c = stack; c && !isvisible(c); c = c->snext);
  222. focus(c);
  223. }
  224. restack();
  225. }
  226. void
  227. tag(Arg *arg) {
  228. unsigned int i;
  229. if(!sel)
  230. return;
  231. for(i = 0; i < ntags; i++)
  232. sel->tags[i] = (arg->i == -1) ? True : False;
  233. if(arg->i >= 0 && arg->i < ntags)
  234. sel->tags[arg->i] = True;
  235. lt->arrange();
  236. }
  237. void
  238. toggleswimming(Arg *arg) {
  239. if(!sel || lt->arrange == swim)
  240. return;
  241. sel->swimming = !sel->swimming;
  242. lt->arrange();
  243. }
  244. void
  245. toggletag(Arg *arg) {
  246. unsigned int i;
  247. if(!sel)
  248. return;
  249. sel->tags[arg->i] = !sel->tags[arg->i];
  250. for(i = 0; i < ntags && !sel->tags[i]; i++);
  251. if(i == ntags)
  252. sel->tags[arg->i] = True;
  253. lt->arrange();
  254. }
  255. void
  256. togglelayout(Arg *arg) {
  257. unsigned int i;
  258. for(i = 0; i < nlayouts && lt != &layout[i]; i++);
  259. if(i == nlayouts - 1)
  260. lt = &layout[0];
  261. else
  262. lt = &layout[++i];
  263. if(sel)
  264. lt->arrange();
  265. else
  266. drawstatus();
  267. }
  268. void
  269. toggleview(Arg *arg) {
  270. unsigned int i;
  271. seltag[arg->i] = !seltag[arg->i];
  272. for(i = 0; i < ntags && !seltag[i]; i++);
  273. if(i == ntags)
  274. seltag[arg->i] = True; /* cannot toggle last view */
  275. lt->arrange();
  276. }
  277. void
  278. view(Arg *arg) {
  279. unsigned int i;
  280. for(i = 0; i < ntags; i++)
  281. seltag[i] = (arg->i == -1) ? True : False;
  282. if(arg->i >= 0 && arg->i < ntags)
  283. seltag[arg->i] = True;
  284. lt->arrange();
  285. }