Configuration file for DWM on MacBook Air
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.

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