Configuration of dwm for Mac Computers
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.

418 lines
7.4 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. void (*arrange)(void) = DEFMODE;
  12. unsigned int master = MASTER;
  13. unsigned int nmaster = NMASTER;
  14. /* static */
  15. typedef struct {
  16. const char *prop;
  17. const char *tags;
  18. Bool isfloat;
  19. } Rule;
  20. typedef struct {
  21. regex_t *propregex;
  22. regex_t *tagregex;
  23. } Regexps;
  24. TAGS
  25. RULES
  26. static Regexps *regexps = NULL;
  27. static unsigned int len = 0;
  28. static Client *
  29. nextmanaged(Client *c) {
  30. for(; c && (c->isfloat || !isvisible(c)); c = c->next);
  31. return c;
  32. }
  33. static void
  34. togglemax(Client *c) {
  35. XEvent ev;
  36. if(c->isfixed)
  37. return;
  38. if((c->ismax = !c->ismax)) {
  39. c->rx = c->x;
  40. c->ry = c->y;
  41. c->rw = c->w;
  42. c->rh = c->h;
  43. resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
  44. }
  45. else
  46. resize(c, c->rx, c->ry, c->rw, c->rh, True);
  47. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  48. }
  49. /* extern */
  50. void
  51. attach(Client *c) {
  52. if(clients)
  53. clients->prev = c;
  54. c->next = clients;
  55. clients = c;
  56. }
  57. void
  58. attachstack(Client *c) {
  59. c->snext = stack;
  60. stack = c;
  61. }
  62. void
  63. compileregexps(void) {
  64. unsigned int i;
  65. regex_t *reg;
  66. if(regexps)
  67. return;
  68. len = sizeof rule / sizeof rule[0];
  69. regexps = emallocz(len * sizeof(Regexps));
  70. for(i = 0; i < len; i++) {
  71. if(rule[i].prop) {
  72. reg = emallocz(sizeof(regex_t));
  73. if(regcomp(reg, rule[i].prop, REG_EXTENDED))
  74. free(reg);
  75. else
  76. regexps[i].propregex = reg;
  77. }
  78. if(rule[i].tags) {
  79. reg = emallocz(sizeof(regex_t));
  80. if(regcomp(reg, rule[i].tags, REG_EXTENDED))
  81. free(reg);
  82. else
  83. regexps[i].tagregex = reg;
  84. }
  85. }
  86. }
  87. void
  88. detach(Client *c) {
  89. if(c->prev)
  90. c->prev->next = c->next;
  91. if(c->next)
  92. c->next->prev = c->prev;
  93. if(c == clients)
  94. clients = c->next;
  95. c->next = c->prev = NULL;
  96. }
  97. void
  98. detachstack(Client *c) {
  99. Client **tc;
  100. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  101. *tc = c->snext;
  102. }
  103. void
  104. dofloat(void) {
  105. Client *c;
  106. for(c = clients; c; c = c->next) {
  107. if(isvisible(c)) {
  108. if(c->isbanned)
  109. XMoveWindow(dpy, c->win, c->x, c->y);
  110. c->isbanned = False;
  111. resize(c, c->x, c->y, c->w, c->h, True);
  112. }
  113. else {
  114. c->isbanned = True;
  115. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  116. }
  117. }
  118. if(!sel || !isvisible(sel)) {
  119. for(c = stack; c && !isvisible(c); c = c->snext);
  120. focus(c);
  121. }
  122. restack();
  123. }
  124. void
  125. dotile(void) {
  126. unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
  127. Client *c;
  128. for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
  129. n++;
  130. /* window geoms */
  131. mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
  132. mw = (n > nmaster) ? (waw * master) / 1000 : waw;
  133. th = (n > nmaster) ? wah / (n - nmaster) : 0;
  134. tw = waw - mw;
  135. for(i = 0, c = clients; c; c = c->next)
  136. if(isvisible(c)) {
  137. if(c->isbanned)
  138. XMoveWindow(dpy, c->win, c->x, c->y);
  139. c->isbanned = False;
  140. if(c->isfloat)
  141. continue;
  142. c->ismax = False;
  143. nx = wax;
  144. ny = way;
  145. if(i < nmaster) {
  146. ny += i * mh;
  147. nw = mw - 2 * BORDERPX;
  148. nh = mh - 2 * BORDERPX;
  149. }
  150. else { /* tile window */
  151. nx += mw;
  152. nw = tw - 2 * BORDERPX;
  153. if(th > 2 * BORDERPX) {
  154. ny += (i - nmaster) * th;
  155. nh = th - 2 * BORDERPX;
  156. }
  157. else /* fallback if th <= 2 * BORDERPX */
  158. nh = wah - 2 * BORDERPX;
  159. }
  160. resize(c, nx, ny, nw, nh, False);
  161. i++;
  162. }
  163. else {
  164. c->isbanned = True;
  165. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  166. }
  167. if(!sel || !isvisible(sel)) {
  168. for(c = stack; c && !isvisible(c); c = c->snext);
  169. focus(c);
  170. }
  171. restack();
  172. }
  173. void
  174. focusnext(Arg *arg) {
  175. Client *c;
  176. if(!sel)
  177. return;
  178. for(c = sel->next; c && !isvisible(c); c = c->next);
  179. if(!c)
  180. for(c = clients; c && !isvisible(c); c = c->next);
  181. if(c) {
  182. focus(c);
  183. restack();
  184. }
  185. }
  186. void
  187. focusprev(Arg *arg) {
  188. Client *c;
  189. if(!sel)
  190. return;
  191. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  192. if(!c) {
  193. for(c = clients; c && c->next; c = c->next);
  194. for(; c && !isvisible(c); c = c->prev);
  195. }
  196. if(c) {
  197. focus(c);
  198. restack();
  199. }
  200. }
  201. Client *
  202. getclient(Window w) {
  203. Client *c;
  204. for(c = clients; c; c = c->next)
  205. if(c->win == w)
  206. return c;
  207. return NULL;
  208. }
  209. void
  210. incnmaster(Arg *arg) {
  211. if((arrange == dofloat) || (nmaster + arg->i < 1)
  212. || (wah / (nmaster + arg->i) <= 2 * BORDERPX))
  213. return;
  214. nmaster += arg->i;
  215. if(sel)
  216. arrange();
  217. else
  218. drawstatus();
  219. }
  220. Bool
  221. isvisible(Client *c) {
  222. unsigned int i;
  223. for(i = 0; i < ntags; i++)
  224. if(c->tags[i] && seltag[i])
  225. return True;
  226. return False;
  227. }
  228. void
  229. resizemaster(Arg *arg) {
  230. if(arrange != dotile)
  231. return;
  232. if(arg->i == 0)
  233. master = MASTER;
  234. else {
  235. if(waw * (master + arg->i) / 1000 >= waw - 2 * BORDERPX
  236. || waw * (master + arg->i) / 1000 <= 2 * BORDERPX)
  237. return;
  238. master += arg->i;
  239. }
  240. arrange();
  241. }
  242. void
  243. restack(void) {
  244. Client *c;
  245. XEvent ev;
  246. drawstatus();
  247. if(!sel)
  248. return;
  249. if(sel->isfloat || arrange == dofloat)
  250. XRaiseWindow(dpy, sel->win);
  251. if(arrange != dofloat) {
  252. if(!sel->isfloat)
  253. XLowerWindow(dpy, sel->win);
  254. for(c = nextmanaged(clients); c; c = nextmanaged(c->next)) {
  255. if(c == sel)
  256. continue;
  257. XLowerWindow(dpy, c->win);
  258. }
  259. }
  260. XSync(dpy, False);
  261. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  262. }
  263. void
  264. settags(Client *c, Client *trans) {
  265. char prop[512];
  266. unsigned int i, j;
  267. regmatch_t tmp;
  268. Bool matched = trans != NULL;
  269. XClassHint ch = { 0 };
  270. if(matched)
  271. for(i = 0; i < ntags; i++)
  272. c->tags[i] = trans->tags[i];
  273. else {
  274. XGetClassHint(dpy, c->win, &ch);
  275. snprintf(prop, sizeof prop, "%s:%s:%s",
  276. ch.res_class ? ch.res_class : "",
  277. ch.res_name ? ch.res_name : "", c->name);
  278. for(i = 0; i < len; i++)
  279. if(regexps[i].propregex && !regexec(regexps[i].propregex, prop, 1, &tmp, 0)) {
  280. c->isfloat = rule[i].isfloat;
  281. for(j = 0; regexps[i].tagregex && j < ntags; j++) {
  282. if(!regexec(regexps[i].tagregex, tags[j], 1, &tmp, 0)) {
  283. matched = True;
  284. c->tags[j] = True;
  285. }
  286. }
  287. }
  288. if(ch.res_class)
  289. XFree(ch.res_class);
  290. if(ch.res_name)
  291. XFree(ch.res_name);
  292. }
  293. if(!matched)
  294. for(i = 0; i < ntags; i++)
  295. c->tags[i] = seltag[i];
  296. }
  297. void
  298. tag(Arg *arg) {
  299. unsigned int i;
  300. if(!sel)
  301. return;
  302. for(i = 0; i < ntags; i++)
  303. sel->tags[i] = (arg->i == -1) ? True : False;
  304. if(arg->i >= 0 && arg->i < ntags)
  305. sel->tags[arg->i] = True;
  306. arrange();
  307. }
  308. void
  309. togglefloat(Arg *arg) {
  310. if(!sel || arrange == dofloat)
  311. return;
  312. sel->isfloat = !sel->isfloat;
  313. arrange();
  314. }
  315. void
  316. toggletag(Arg *arg) {
  317. unsigned int i;
  318. if(!sel)
  319. return;
  320. sel->tags[arg->i] = !sel->tags[arg->i];
  321. for(i = 0; i < ntags && !sel->tags[i]; i++);
  322. if(i == ntags)
  323. sel->tags[arg->i] = True;
  324. arrange();
  325. }
  326. void
  327. togglemode(Arg *arg) {
  328. arrange = (arrange == dofloat) ? dotile : dofloat;
  329. if(sel)
  330. arrange();
  331. else
  332. drawstatus();
  333. }
  334. void
  335. toggleview(Arg *arg) {
  336. unsigned int i;
  337. seltag[arg->i] = !seltag[arg->i];
  338. for(i = 0; i < ntags && !seltag[i]; i++);
  339. if(i == ntags)
  340. seltag[arg->i] = True; /* cannot toggle last view */
  341. arrange();
  342. }
  343. void
  344. view(Arg *arg) {
  345. unsigned int i;
  346. for(i = 0; i < ntags; i++)
  347. seltag[i] = (arg->i == -1) ? True : False;
  348. if(arg->i >= 0 && arg->i < ntags)
  349. seltag[arg->i] = True;
  350. arrange();
  351. }
  352. void
  353. zoom(Arg *arg) {
  354. unsigned int n;
  355. Client *c;
  356. if(!sel)
  357. return;
  358. if(sel->isfloat || (arrange == dofloat)) {
  359. togglemax(sel);
  360. return;
  361. }
  362. for(n = 0, c = nextmanaged(clients); c; c = nextmanaged(c->next))
  363. n++;
  364. if((c = sel) == nextmanaged(clients))
  365. if(!(c = nextmanaged(c->next)))
  366. return;
  367. detach(c);
  368. attach(c);
  369. focus(c);
  370. arrange();
  371. }