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.

465 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. Client *
  180. getclient(Window w) {
  181. Client *c;
  182. for(c = clients; c; c = c->next)
  183. if(c->win == w)
  184. return c;
  185. return NULL;
  186. }
  187. void
  188. killclient(Arg *arg) {
  189. if(!sel)
  190. return;
  191. if(isprotodel(sel))
  192. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  193. else
  194. XKillClient(dpy, sel->win);
  195. }
  196. void
  197. manage(Window w, XWindowAttributes *wa) {
  198. Client *c, *t;
  199. Window trans;
  200. XWindowChanges wc;
  201. c = emallocz(sizeof(Client));
  202. c->tags = emallocz(ntags * sizeof(Bool));
  203. c->win = w;
  204. c->x = wa->x;
  205. c->y = wa->y;
  206. c->w = wa->width;
  207. c->h = wa->height;
  208. if(c->w == sw && c->h == sh) {
  209. c->border = 0;
  210. c->x = sx;
  211. c->y = sy;
  212. }
  213. else {
  214. c->border = BORDERPX;
  215. if(c->x + c->w + 2 * c->border > wax + waw)
  216. c->x = wax + waw - c->w - 2 * c->border;
  217. if(c->y + c->h + 2 * c->border > way + wah)
  218. c->y = way + wah - c->h - 2 * c->border;
  219. if(c->x < wax)
  220. c->x = wax;
  221. if(c->y < way)
  222. c->y = way;
  223. }
  224. updatesizehints(c);
  225. XSelectInput(dpy, c->win,
  226. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  227. XGetTransientForHint(dpy, c->win, &trans);
  228. grabbuttons(c, False);
  229. wc.border_width = c->border;
  230. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
  231. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  232. configure(c); /* propagates border_width, if size doesn't change */
  233. updatetitle(c);
  234. t = getclient(trans);
  235. settags(c, t);
  236. if(!c->isfloat)
  237. c->isfloat = (t != 0) || c->isfixed;
  238. attach(c);
  239. attachstack(c);
  240. c->isbanned = True;
  241. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  242. XMapWindow(dpy, c->win);
  243. setclientstate(c, NormalState);
  244. if(isvisible(c))
  245. focus(c);
  246. arrange();
  247. }
  248. Client *
  249. nexttiled(Client *c) {
  250. for(; c && (c->isfloat || !isvisible(c)); c = c->next);
  251. return c;
  252. }
  253. void
  254. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  255. float actual, dx, dy, max, min;
  256. XWindowChanges wc;
  257. if(w <= 0 || h <= 0)
  258. return;
  259. if(sizehints) {
  260. if(c->minw && w < c->minw)
  261. w = c->minw;
  262. if(c->minh && h < c->minh)
  263. h = c->minh;
  264. if(c->maxw && w > c->maxw)
  265. w = c->maxw;
  266. if(c->maxh && h > c->maxh)
  267. h = c->maxh;
  268. /* inspired by algorithm from fluxbox */
  269. if(c->minay > 0 && c->maxay && (h - c->baseh) > 0) {
  270. dx = (float)(w - c->basew);
  271. dy = (float)(h - c->baseh);
  272. min = (float)(c->minax) / (float)(c->minay);
  273. max = (float)(c->maxax) / (float)(c->maxay);
  274. actual = dx / dy;
  275. if(max > 0 && min > 0 && actual > 0) {
  276. if(actual < min) {
  277. dy = (dx * min + dy) / (min * min + 1);
  278. dx = dy * min;
  279. w = (int)dx + c->basew;
  280. h = (int)dy + c->baseh;
  281. }
  282. else if(actual > max) {
  283. dy = (dx * min + dy) / (max * max + 1);
  284. dx = dy * min;
  285. w = (int)dx + c->basew;
  286. h = (int)dy + c->baseh;
  287. }
  288. }
  289. }
  290. if(c->incw)
  291. w -= (w - c->basew) % c->incw;
  292. if(c->inch)
  293. h -= (h - c->baseh) % c->inch;
  294. }
  295. if(w == sw && h == sh)
  296. c->border = 0;
  297. else
  298. c->border = BORDERPX;
  299. /* offscreen appearance fixes */
  300. if(x > sw)
  301. x = sw - w - 2 * c->border;
  302. if(y > sh)
  303. y = sh - h - 2 * c->border;
  304. if(x + w + 2 * c->border < sx)
  305. x = sx;
  306. if(y + h + 2 * c->border < sy)
  307. y = sy;
  308. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  309. c->x = wc.x = x;
  310. c->y = wc.y = y;
  311. c->w = wc.width = w;
  312. c->h = wc.height = h;
  313. wc.border_width = c->border;
  314. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  315. configure(c);
  316. XSync(dpy, False);
  317. }
  318. }
  319. void
  320. updatesizehints(Client *c) {
  321. long msize;
  322. XSizeHints size;
  323. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  324. size.flags = PSize;
  325. c->flags = size.flags;
  326. if(c->flags & PBaseSize) {
  327. c->basew = size.base_width;
  328. c->baseh = size.base_height;
  329. }
  330. else
  331. c->basew = c->baseh = 0;
  332. if(c->flags & PResizeInc) {
  333. c->incw = size.width_inc;
  334. c->inch = size.height_inc;
  335. }
  336. else
  337. c->incw = c->inch = 0;
  338. if(c->flags & PMaxSize) {
  339. c->maxw = size.max_width;
  340. c->maxh = size.max_height;
  341. }
  342. else
  343. c->maxw = c->maxh = 0;
  344. if(c->flags & PMinSize) {
  345. c->minw = size.min_width;
  346. c->minh = size.min_height;
  347. }
  348. else
  349. c->minw = c->minh = 0;
  350. if(c->flags & PAspect) {
  351. c->minax = size.min_aspect.x;
  352. c->minay = size.min_aspect.y;
  353. c->maxax = size.max_aspect.x;
  354. c->maxay = size.max_aspect.y;
  355. }
  356. else
  357. c->minax = c->minay = c->maxax = c->maxay = 0;
  358. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  359. && c->maxw == c->minw && c->maxh == c->minh);
  360. }
  361. void
  362. updatetitle(Client *c) {
  363. char **list = NULL;
  364. int n;
  365. XTextProperty name;
  366. name.nitems = 0;
  367. c->name[0] = 0;
  368. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  369. if(!name.nitems)
  370. XGetWMName(dpy, c->win, &name);
  371. if(!name.nitems)
  372. return;
  373. if(name.encoding == XA_STRING)
  374. strncpy(c->name, (char *)name.value, sizeof c->name);
  375. else {
  376. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  377. && n > 0 && *list)
  378. {
  379. strncpy(c->name, *list, sizeof c->name);
  380. XFreeStringList(list);
  381. }
  382. }
  383. XFree(name.value);
  384. }
  385. void
  386. unmanage(Client *c) {
  387. Client *nc;
  388. /* The server grab construct avoids race conditions. */
  389. XGrabServer(dpy);
  390. XSetErrorHandler(xerrordummy);
  391. detach(c);
  392. detachstack(c);
  393. if(sel == c) {
  394. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  395. focus(nc);
  396. }
  397. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  398. setclientstate(c, WithdrawnState);
  399. free(c->tags);
  400. free(c);
  401. XSync(dpy, False);
  402. XSetErrorHandler(xerror);
  403. XUngrabServer(dpy);
  404. arrange();
  405. }
  406. void
  407. zoom(Arg *arg) {
  408. unsigned int n;
  409. Client *c;
  410. if(!sel)
  411. return;
  412. if(sel->isfloat || (arrange == dofloat)) {
  413. togglemax(sel);
  414. return;
  415. }
  416. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  417. n++;
  418. if((c = sel) == nexttiled(clients))
  419. if(!(c = nexttiled(c->next)))
  420. return;
  421. detach(c);
  422. attach(c);
  423. focus(c);
  424. arrange();
  425. }