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
6.0 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 versatile;
  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->versatile)
  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->versatile || lt->arrange == versatile)
  158. XRaiseWindow(dpy, sel->win);
  159. if(lt->arrange != versatile) {
  160. if(!sel->versatile)
  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->versatile = rule[i].versatile;
  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. tag(Arg *arg) {
  207. unsigned int i;
  208. if(!sel)
  209. return;
  210. for(i = 0; i < ntags; i++)
  211. sel->tags[i] = (arg->i == -1) ? True : False;
  212. if(arg->i >= 0 && arg->i < ntags)
  213. sel->tags[arg->i] = True;
  214. lt->arrange();
  215. }
  216. void
  217. toggletag(Arg *arg) {
  218. unsigned int i;
  219. if(!sel)
  220. return;
  221. sel->tags[arg->i] = !sel->tags[arg->i];
  222. for(i = 0; i < ntags && !sel->tags[i]; i++);
  223. if(i == ntags)
  224. sel->tags[arg->i] = True;
  225. lt->arrange();
  226. }
  227. void
  228. togglelayout(Arg *arg) {
  229. unsigned int i;
  230. for(i = 0; i < nlayouts && lt != &layout[i]; i++);
  231. if(i == nlayouts - 1)
  232. lt = &layout[0];
  233. else
  234. lt = &layout[++i];
  235. if(sel)
  236. lt->arrange();
  237. else
  238. drawstatus();
  239. }
  240. void
  241. toggleversatile(Arg *arg) {
  242. if(!sel || lt->arrange == versatile)
  243. return;
  244. sel->versatile = !sel->versatile;
  245. lt->arrange();
  246. }
  247. void
  248. toggleview(Arg *arg) {
  249. unsigned int i;
  250. seltag[arg->i] = !seltag[arg->i];
  251. for(i = 0; i < ntags && !seltag[i]; i++);
  252. if(i == ntags)
  253. seltag[arg->i] = True; /* cannot toggle last view */
  254. lt->arrange();
  255. }
  256. void
  257. versatile(void) {
  258. Client *c;
  259. for(c = clients; c; c = c->next) {
  260. if(isvisible(c)) {
  261. if(c->isbanned)
  262. XMoveWindow(dpy, c->win, c->x, c->y);
  263. c->isbanned = False;
  264. resize(c, c->x, c->y, c->w, c->h, True);
  265. }
  266. else {
  267. c->isbanned = True;
  268. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  269. }
  270. }
  271. if(!sel || !isvisible(sel)) {
  272. for(c = stack; c && !isvisible(c); c = c->snext);
  273. focus(c);
  274. }
  275. restack();
  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. }