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.

405 lines
9.6 KiB

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