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.

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