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.

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