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
17 years ago
17 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/Xatom.h>
  5. #include <X11/Xutil.h>
  6. /* static */
  7. static void
  8. attachstack(Client *c) {
  9. c->snext = stack;
  10. stack = c;
  11. }
  12. static void
  13. detachstack(Client *c) {
  14. Client **tc;
  15. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  16. *tc = c->snext;
  17. }
  18. static void
  19. grabbuttons(Client *c, Bool focused) {
  20. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  21. if(focused) {
  22. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  23. GrabModeAsync, GrabModeSync, None, None);
  24. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  25. GrabModeAsync, GrabModeSync, None, None);
  26. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  27. GrabModeAsync, GrabModeSync, None, None);
  28. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  29. GrabModeAsync, GrabModeSync, None, None);
  30. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  31. GrabModeAsync, GrabModeSync, None, None);
  32. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  33. GrabModeAsync, GrabModeSync, None, None);
  34. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  35. GrabModeAsync, GrabModeSync, None, None);
  36. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  37. GrabModeAsync, GrabModeSync, None, None);
  38. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  39. GrabModeAsync, GrabModeSync, None, None);
  40. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  41. GrabModeAsync, GrabModeSync, None, None);
  42. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  43. GrabModeAsync, GrabModeSync, None, None);
  44. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  45. GrabModeAsync, GrabModeSync, None, None);
  46. }
  47. else
  48. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  49. GrabModeAsync, GrabModeSync, None, None);
  50. }
  51. static Bool
  52. isprotodel(Client *c) {
  53. int i, n;
  54. Atom *protocols;
  55. Bool ret = False;
  56. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  57. for(i = 0; !ret && i < n; i++)
  58. if(protocols[i] == wmatom[WMDelete])
  59. ret = True;
  60. XFree(protocols);
  61. }
  62. return ret;
  63. }
  64. static void
  65. setclientstate(Client *c, long state) {
  66. long data[] = {state, None};
  67. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  68. PropModeReplace, (unsigned char *)data, 2);
  69. }
  70. static int
  71. xerrordummy(Display *dsply, XErrorEvent *ee) {
  72. return 0;
  73. }
  74. /* extern */
  75. void
  76. attach(Client *c) {
  77. if(clients)
  78. clients->prev = c;
  79. c->next = clients;
  80. clients = c;
  81. }
  82. void
  83. ban(Client *c) {
  84. if(c->isbanned)
  85. return;
  86. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  87. c->isbanned = True;
  88. }
  89. void
  90. configure(Client *c) {
  91. XConfigureEvent ce;
  92. ce.type = ConfigureNotify;
  93. ce.display = dpy;
  94. ce.event = c->win;
  95. ce.window = c->win;
  96. ce.x = c->x;
  97. ce.y = c->y;
  98. ce.width = c->w;
  99. ce.height = c->h;
  100. ce.border_width = c->border;
  101. ce.above = None;
  102. ce.override_redirect = False;
  103. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  104. }
  105. void
  106. detach(Client *c) {
  107. if(c->prev)
  108. c->prev->next = c->next;
  109. if(c->next)
  110. c->next->prev = c->prev;
  111. if(c == clients)
  112. clients = c->next;
  113. c->next = c->prev = NULL;
  114. }
  115. void
  116. focus(Client *c) {
  117. if((!c && selscreen) || (c && !isvisible(c)))
  118. for(c = stack; c && !isvisible(c); c = c->snext);
  119. if(sel && sel != c) {
  120. grabbuttons(sel, False);
  121. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  122. }
  123. if(c) {
  124. detachstack(c);
  125. attachstack(c);
  126. grabbuttons(c, True);
  127. }
  128. sel = c;
  129. drawstatus();
  130. if(!selscreen)
  131. return;
  132. if(c) {
  133. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  134. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  135. }
  136. else
  137. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  138. }
  139. void
  140. killclient(const char *arg) {
  141. XEvent ev;
  142. if(!sel)
  143. return;
  144. if(isprotodel(sel)) {
  145. ev.type = ClientMessage;
  146. ev.xclient.window = sel->win;
  147. ev.xclient.message_type = wmatom[WMProtocols];
  148. ev.xclient.format = 32;
  149. ev.xclient.data.l[0] = wmatom[WMDelete];
  150. ev.xclient.data.l[1] = CurrentTime;
  151. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  152. }
  153. else
  154. XKillClient(dpy, sel->win);
  155. }
  156. void
  157. manage(Window w, XWindowAttributes *wa) {
  158. unsigned int i;
  159. Client *c, *t = NULL;
  160. Window trans;
  161. Status rettrans;
  162. XWindowChanges wc;
  163. c = emallocz(sizeof(Client));
  164. c->tags = emallocz(ntags * sizeof(Bool));
  165. c->win = w;
  166. c->x = wa->x;
  167. c->y = wa->y;
  168. c->w = wa->width;
  169. c->h = wa->height;
  170. c->oldborder = wa->border_width;
  171. if(c->w == sw && c->h == sh) {
  172. c->x = sx;
  173. c->y = sy;
  174. c->border = wa->border_width;
  175. }
  176. else {
  177. if(c->x + c->w + 2 * c->border > wax + waw)
  178. c->x = wax + waw - c->w - 2 * c->border;
  179. if(c->y + c->h + 2 * c->border > way + wah)
  180. c->y = way + wah - c->h - 2 * c->border;
  181. if(c->x < wax)
  182. c->x = wax;
  183. if(c->y < way)
  184. c->y = way;
  185. c->border = BORDERPX;
  186. }
  187. wc.border_width = c->border;
  188. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  189. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  190. configure(c); /* propagates border_width, if size doesn't change */
  191. updatesizehints(c);
  192. XSelectInput(dpy, w,
  193. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  194. grabbuttons(c, False);
  195. updatetitle(c);
  196. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  197. for(t = clients; t && t->win != trans; t = t->next);
  198. if(t)
  199. for(i = 0; i < ntags; i++)
  200. c->tags[i] = t->tags[i];
  201. applyrules(c);
  202. if(!c->isfloating)
  203. c->isfloating = (rettrans == Success) || c->isfixed;
  204. attach(c);
  205. attachstack(c);
  206. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  207. ban(c);
  208. XMapWindow(dpy, c->win);
  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. }