Configuration file for DWM on MacBook Air
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.

455 lines
10 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  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 <stdlib.h>
  6. #include <string.h>
  7. #include <X11/Xatom.h>
  8. #include <X11/Xutil.h>
  9. /* static */
  10. static void
  11. attachstack(Client *c) {
  12. c->snext = stack;
  13. stack = c;
  14. }
  15. static void
  16. detachstack(Client *c) {
  17. Client **tc;
  18. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  19. *tc = c->snext;
  20. }
  21. static void
  22. grabbuttons(Client *c, Bool focused) {
  23. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  24. if(focused) {
  25. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  26. GrabModeAsync, GrabModeSync, None, None);
  27. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  28. GrabModeAsync, GrabModeSync, None, None);
  29. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  30. GrabModeAsync, GrabModeSync, None, None);
  31. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  32. GrabModeAsync, GrabModeSync, None, None);
  33. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  34. GrabModeAsync, GrabModeSync, None, None);
  35. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  36. GrabModeAsync, GrabModeSync, None, None);
  37. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  38. GrabModeAsync, GrabModeSync, None, None);
  39. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  40. GrabModeAsync, GrabModeSync, None, None);
  41. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  42. GrabModeAsync, GrabModeSync, None, None);
  43. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  44. GrabModeAsync, GrabModeSync, None, None);
  45. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  46. GrabModeAsync, GrabModeSync, None, None);
  47. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  48. GrabModeAsync, GrabModeSync, None, None);
  49. }
  50. else
  51. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  52. GrabModeAsync, GrabModeSync, None, None);
  53. }
  54. static Bool
  55. isprotodel(Client *c) {
  56. int i, n;
  57. Atom *protocols;
  58. Bool ret = False;
  59. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  60. for(i = 0; !ret && i < n; i++)
  61. if(protocols[i] == wmatom[WMDelete])
  62. ret = True;
  63. XFree(protocols);
  64. }
  65. return ret;
  66. }
  67. static void
  68. setclientstate(Client *c, long state) {
  69. long data[] = {state, None};
  70. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  71. PropModeReplace, (unsigned char *)data, 2);
  72. }
  73. static void
  74. togglemax(Client *c) {
  75. XEvent ev;
  76. if(c->isfixed)
  77. return;
  78. if((c->ismax = !c->ismax)) {
  79. c->rx = c->x;
  80. c->ry = c->y;
  81. c->rw = c->w;
  82. c->rh = c->h;
  83. resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
  84. }
  85. else
  86. resize(c, c->rx, c->ry, c->rw, c->rh, True);
  87. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  88. }
  89. static int
  90. xerrordummy(Display *dsply, XErrorEvent *ee) {
  91. return 0;
  92. }
  93. /* extern */
  94. void
  95. attach(Client *c) {
  96. if(clients)
  97. clients->prev = c;
  98. c->next = clients;
  99. clients = c;
  100. }
  101. void
  102. configure(Client *c) {
  103. XConfigureEvent ce;
  104. ce.type = ConfigureNotify;
  105. ce.display = dpy;
  106. ce.event = c->win;
  107. ce.window = c->win;
  108. ce.x = c->x;
  109. ce.y = c->y;
  110. ce.width = c->w;
  111. ce.height = c->h;
  112. ce.border_width = c->border;
  113. ce.above = None;
  114. ce.override_redirect = False;
  115. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  116. }
  117. void
  118. detach(Client *c) {
  119. if(c->prev)
  120. c->prev->next = c->next;
  121. if(c->next)
  122. c->next->prev = c->prev;
  123. if(c == clients)
  124. clients = c->next;
  125. c->next = c->prev = NULL;
  126. }
  127. void
  128. focus(Client *c) {
  129. if(c && !isvisible(c))
  130. return;
  131. if(sel && sel != c) {
  132. grabbuttons(sel, False);
  133. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  134. }
  135. if(c) {
  136. detachstack(c);
  137. attachstack(c);
  138. grabbuttons(c, True);
  139. }
  140. sel = c;
  141. drawstatus();
  142. if(!selscreen)
  143. return;
  144. if(c) {
  145. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  146. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  147. }
  148. else
  149. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  150. }
  151. void
  152. focusnext(Arg *arg) {
  153. Client *c;
  154. if(!sel)
  155. return;
  156. for(c = sel->next; c && !isvisible(c); c = c->next);
  157. if(!c)
  158. for(c = clients; c && !isvisible(c); c = c->next);
  159. if(c) {
  160. focus(c);
  161. restack();
  162. }
  163. }
  164. void
  165. focusprev(Arg *arg) {
  166. Client *c;
  167. if(!sel)
  168. return;
  169. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  170. if(!c) {
  171. for(c = clients; c && c->next; c = c->next);
  172. for(; c && !isvisible(c); c = c->prev);
  173. }
  174. if(c) {
  175. focus(c);
  176. restack();
  177. }
  178. }
  179. void
  180. killclient(Arg *arg) {
  181. if(!sel)
  182. return;
  183. if(isprotodel(sel))
  184. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  185. else
  186. XKillClient(dpy, sel->win);
  187. }
  188. void
  189. manage(Window w, XWindowAttributes *wa) {
  190. Client *c, *t;
  191. Window trans;
  192. XWindowChanges wc;
  193. c = emallocz(sizeof(Client));
  194. c->tags = emallocz(ntags * sizeof(Bool));
  195. c->win = w;
  196. c->x = wa->x;
  197. c->y = wa->y;
  198. c->w = wa->width;
  199. c->h = wa->height;
  200. if(c->w == sw && c->h == sh) {
  201. c->border = 0;
  202. c->x = sx;
  203. c->y = sy;
  204. }
  205. else {
  206. c->border = BORDERPX;
  207. if(c->x + c->w + 2 * c->border > wax + waw)
  208. c->x = wax + waw - c->w - 2 * c->border;
  209. if(c->y + c->h + 2 * c->border > way + wah)
  210. c->y = way + wah - c->h - 2 * c->border;
  211. if(c->x < wax)
  212. c->x = wax;
  213. if(c->y < way)
  214. c->y = way;
  215. }
  216. updatesizehints(c);
  217. XSelectInput(dpy, c->win,
  218. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  219. XGetTransientForHint(dpy, c->win, &trans);
  220. grabbuttons(c, False);
  221. wc.border_width = c->border;
  222. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
  223. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  224. configure(c); /* propagates border_width, if size doesn't change */
  225. updatetitle(c);
  226. for(t = clients; t && t->win != c->win; t = t->next);
  227. settags(c, t);
  228. if(!c->isfloat)
  229. c->isfloat = (t != 0) || c->isfixed;
  230. attach(c);
  231. attachstack(c);
  232. c->isbanned = True;
  233. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  234. XMapWindow(dpy, c->win);
  235. setclientstate(c, NormalState);
  236. if(isvisible(c))
  237. focus(c);
  238. arrange();
  239. }
  240. Client *
  241. nexttiled(Client *c) {
  242. for(; c && (c->isfloat || !isvisible(c)); c = c->next);
  243. return c;
  244. }
  245. void
  246. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  247. float actual, dx, dy, max, min;
  248. XWindowChanges wc;
  249. if(w <= 0 || h <= 0)
  250. return;
  251. if(sizehints) {
  252. if(c->minw && w < c->minw)
  253. w = c->minw;
  254. if(c->minh && h < c->minh)
  255. h = c->minh;
  256. if(c->maxw && w > c->maxw)
  257. w = c->maxw;
  258. if(c->maxh && h > c->maxh)
  259. h = c->maxh;
  260. /* inspired by algorithm from fluxbox */
  261. if(c->minay > 0 && c->maxay && (h - c->baseh) > 0) {
  262. dx = (float)(w - c->basew);
  263. dy = (float)(h - c->baseh);
  264. min = (float)(c->minax) / (float)(c->minay);
  265. max = (float)(c->maxax) / (float)(c->maxay);
  266. actual = dx / dy;
  267. if(max > 0 && min > 0 && actual > 0) {
  268. if(actual < min) {
  269. dy = (dx * min + dy) / (min * min + 1);
  270. dx = dy * min;
  271. w = (int)dx + c->basew;
  272. h = (int)dy + c->baseh;
  273. }
  274. else if(actual > max) {
  275. dy = (dx * min + dy) / (max * max + 1);
  276. dx = dy * min;
  277. w = (int)dx + c->basew;
  278. h = (int)dy + c->baseh;
  279. }
  280. }
  281. }
  282. if(c->incw)
  283. w -= (w - c->basew) % c->incw;
  284. if(c->inch)
  285. h -= (h - c->baseh) % c->inch;
  286. }
  287. if(w == sw && h == sh)
  288. c->border = 0;
  289. else
  290. c->border = BORDERPX;
  291. /* offscreen appearance fixes */
  292. if(x > sw)
  293. x = sw - w - 2 * c->border;
  294. if(y > sh)
  295. y = sh - h - 2 * c->border;
  296. if(x + w + 2 * c->border < sx)
  297. x = sx;
  298. if(y + h + 2 * c->border < sy)
  299. y = sy;
  300. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  301. c->x = wc.x = x;
  302. c->y = wc.y = y;
  303. c->w = wc.width = w;
  304. c->h = wc.height = h;
  305. wc.border_width = c->border;
  306. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  307. configure(c);
  308. XSync(dpy, False);
  309. }
  310. }
  311. void
  312. updatesizehints(Client *c) {
  313. long msize;
  314. XSizeHints size;
  315. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  316. size.flags = PSize;
  317. c->flags = size.flags;
  318. if(c->flags & PBaseSize) {
  319. c->basew = size.base_width;
  320. c->baseh = size.base_height;
  321. }
  322. else
  323. c->basew = c->baseh = 0;
  324. if(c->flags & PResizeInc) {
  325. c->incw = size.width_inc;
  326. c->inch = size.height_inc;
  327. }
  328. else
  329. c->incw = c->inch = 0;
  330. if(c->flags & PMaxSize) {
  331. c->maxw = size.max_width;
  332. c->maxh = size.max_height;
  333. }
  334. else
  335. c->maxw = c->maxh = 0;
  336. if(c->flags & PMinSize) {
  337. c->minw = size.min_width;
  338. c->minh = size.min_height;
  339. }
  340. else
  341. c->minw = c->minh = 0;
  342. if(c->flags & PAspect) {
  343. c->minax = size.min_aspect.x;
  344. c->minay = size.min_aspect.y;
  345. c->maxax = size.max_aspect.x;
  346. c->maxay = size.max_aspect.y;
  347. }
  348. else
  349. c->minax = c->minay = c->maxax = c->maxay = 0;
  350. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  351. && c->maxw == c->minw && c->maxh == c->minh);
  352. }
  353. void
  354. updatetitle(Client *c) {
  355. char **list = NULL;
  356. int n;
  357. XTextProperty name;
  358. name.nitems = 0;
  359. c->name[0] = 0;
  360. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  361. if(!name.nitems)
  362. XGetWMName(dpy, c->win, &name);
  363. if(!name.nitems)
  364. return;
  365. if(name.encoding == XA_STRING)
  366. strncpy(c->name, (char *)name.value, sizeof c->name);
  367. else {
  368. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  369. && n > 0 && *list)
  370. {
  371. strncpy(c->name, *list, sizeof c->name);
  372. XFreeStringList(list);
  373. }
  374. }
  375. XFree(name.value);
  376. }
  377. void
  378. unmanage(Client *c) {
  379. Client *nc;
  380. /* The server grab construct avoids race conditions. */
  381. XGrabServer(dpy);
  382. XSetErrorHandler(xerrordummy);
  383. detach(c);
  384. detachstack(c);
  385. if(sel == c) {
  386. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  387. focus(nc);
  388. }
  389. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  390. setclientstate(c, WithdrawnState);
  391. free(c->tags);
  392. free(c);
  393. XSync(dpy, False);
  394. XSetErrorHandler(xerror);
  395. XUngrabServer(dpy);
  396. arrange();
  397. }
  398. void
  399. zoom(Arg *arg) {
  400. unsigned int n;
  401. Client *c;
  402. if(!sel)
  403. return;
  404. if(sel->isfloat || (arrange == dofloat)) {
  405. togglemax(sel);
  406. return;
  407. }
  408. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  409. n++;
  410. if((c = sel) == nexttiled(clients))
  411. if(!(c = nexttiled(c->next)))
  412. return;
  413. detach(c);
  414. attach(c);
  415. focus(c);
  416. arrange();
  417. }