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.

420 lines
9.8 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
  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. ban(Client *c) {
  85. if(c->isbanned)
  86. return;
  87. XUnmapWindow(dpy, c->win);
  88. setclientstate(c, IconicState);
  89. c->isbanned = True;
  90. c->unmapped++;
  91. }
  92. void
  93. configure(Client *c) {
  94. XConfigureEvent ce;
  95. ce.type = ConfigureNotify;
  96. ce.display = dpy;
  97. ce.event = c->win;
  98. ce.window = c->win;
  99. ce.x = c->x;
  100. ce.y = c->y;
  101. ce.width = c->w;
  102. ce.height = c->h;
  103. ce.border_width = c->border;
  104. ce.above = None;
  105. ce.override_redirect = False;
  106. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  107. }
  108. void
  109. detach(Client *c) {
  110. if(c->prev)
  111. c->prev->next = c->next;
  112. if(c->next)
  113. c->next->prev = c->prev;
  114. if(c == clients)
  115. clients = c->next;
  116. c->next = c->prev = NULL;
  117. }
  118. void
  119. focus(Client *c) {
  120. if((!c && selscreen) || (c && !isvisible(c)))
  121. for(c = stack; c && !isvisible(c); c = c->snext);
  122. if(sel && sel != c) {
  123. grabbuttons(sel, False);
  124. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  125. }
  126. if(c) {
  127. detachstack(c);
  128. attachstack(c);
  129. grabbuttons(c, True);
  130. }
  131. sel = c;
  132. drawstatus();
  133. if(!selscreen)
  134. return;
  135. if(c) {
  136. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  137. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  138. }
  139. else
  140. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  141. }
  142. void
  143. killclient(const char *arg) {
  144. XEvent ev;
  145. if(!sel)
  146. return;
  147. if(isprotodel(sel)) {
  148. ev.type = ClientMessage;
  149. ev.xclient.window = sel->win;
  150. ev.xclient.message_type = wmatom[WMProtocols];
  151. ev.xclient.format = 32;
  152. ev.xclient.data.l[0] = wmatom[WMDelete];
  153. ev.xclient.data.l[1] = CurrentTime;
  154. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  155. }
  156. else
  157. XKillClient(dpy, sel->win);
  158. }
  159. void
  160. manage(Window w, XWindowAttributes *wa) {
  161. Client *c, *t = NULL;
  162. Window trans;
  163. Status rettrans;
  164. XWindowChanges wc;
  165. c = emallocz(sizeof(Client));
  166. c->tags = emallocz(ntags * sizeof(Bool));
  167. c->win = w;
  168. c->x = wa->x;
  169. c->y = wa->y;
  170. c->w = wa->width;
  171. c->h = wa->height;
  172. c->oldborder = wa->border_width;
  173. if(c->w == sw && c->h == sh) {
  174. c->x = sx;
  175. c->y = sy;
  176. c->border = wa->border_width;
  177. }
  178. else {
  179. if(c->x + c->w + 2 * c->border > wax + waw)
  180. c->x = wax + waw - c->w - 2 * c->border;
  181. if(c->y + c->h + 2 * c->border > way + wah)
  182. c->y = way + wah - c->h - 2 * c->border;
  183. if(c->x < wax)
  184. c->x = wax;
  185. if(c->y < way)
  186. c->y = way;
  187. c->border = BORDERPX;
  188. }
  189. wc.border_width = c->border;
  190. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  191. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  192. configure(c); /* propagates border_width, if size doesn't change */
  193. updatesizehints(c);
  194. XSelectInput(dpy, w,
  195. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  196. grabbuttons(c, False);
  197. updatetitle(c);
  198. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  199. for(t = clients; t && t->win != trans; t = t->next);
  200. settags(c, t);
  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. setclientstate(c, IconicState);
  207. c->isbanned = True;
  208. focus(c);
  209. lt->arrange(NULL);
  210. }
  211. void
  212. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  213. float dx, dy, max, min, ratio;
  214. XWindowChanges wc;
  215. if(w <= 0 || h <= 0)
  216. return;
  217. if(sizehints) {
  218. if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0) {
  219. dx = (float)(w - c->basew);
  220. dy = (float)(h - c->baseh);
  221. min = (float)(c->minax) / (float)(c->minay);
  222. max = (float)(c->maxax) / (float)(c->maxay);
  223. ratio = dx / dy;
  224. if(max > 0 && min > 0 && ratio > 0) {
  225. if(ratio < min) {
  226. dy = (dx * min + dy) / (min * min + 1);
  227. dx = dy * min;
  228. w = (int)dx + c->basew;
  229. h = (int)dy + c->baseh;
  230. }
  231. else if(ratio > max) {
  232. dy = (dx * min + dy) / (max * max + 1);
  233. dx = dy * min;
  234. w = (int)dx + c->basew;
  235. h = (int)dy + c->baseh;
  236. }
  237. }
  238. }
  239. if(c->minw && w < c->minw)
  240. w = c->minw;
  241. if(c->minh && h < c->minh)
  242. h = c->minh;
  243. if(c->maxw && w > c->maxw)
  244. w = c->maxw;
  245. if(c->maxh && h > c->maxh)
  246. h = c->maxh;
  247. if(c->incw)
  248. w -= (w - c->basew) % c->incw;
  249. if(c->inch)
  250. h -= (h - c->baseh) % c->inch;
  251. }
  252. if(w <= 0 || h <= 0)
  253. return;
  254. /* offscreen appearance fixes */
  255. if(x > sw)
  256. x = sw - w - 2 * c->border;
  257. if(y > sh)
  258. y = sh - h - 2 * c->border;
  259. if(x + w + 2 * c->border < sx)
  260. x = sx;
  261. if(y + h + 2 * c->border < sy)
  262. y = sy;
  263. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  264. c->x = wc.x = x;
  265. c->y = wc.y = y;
  266. c->w = wc.width = w;
  267. c->h = wc.height = h;
  268. wc.border_width = c->border;
  269. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  270. configure(c);
  271. XSync(dpy, False);
  272. }
  273. }
  274. void
  275. togglefloating(const char *arg) {
  276. if(!sel || lt->arrange == floating)
  277. return;
  278. sel->isfloating = !sel->isfloating;
  279. if(sel->isfloating)
  280. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  281. lt->arrange(NULL);
  282. }
  283. void
  284. unban(Client *c) {
  285. if(!c->isbanned)
  286. return;
  287. XMapWindow(dpy, c->win);
  288. setclientstate(c, NormalState);
  289. c->isbanned = False;
  290. }
  291. void
  292. unmanage(Client *c) {
  293. XWindowChanges wc;
  294. wc.border_width = c->oldborder;
  295. /* The server grab construct avoids race conditions. */
  296. XGrabServer(dpy);
  297. XSetErrorHandler(xerrordummy);
  298. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  299. detach(c);
  300. detachstack(c);
  301. if(sel == c)
  302. focus(NULL);
  303. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  304. setclientstate(c, WithdrawnState);
  305. free(c->tags);
  306. free(c);
  307. XSync(dpy, False);
  308. XSetErrorHandler(xerror);
  309. XUngrabServer(dpy);
  310. lt->arrange(NULL);
  311. }
  312. void
  313. updatesizehints(Client *c) {
  314. long msize;
  315. XSizeHints size;
  316. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  317. size.flags = PSize;
  318. c->flags = size.flags;
  319. if(c->flags & PBaseSize) {
  320. c->basew = size.base_width;
  321. c->baseh = size.base_height;
  322. }
  323. else if(c->flags & PMinSize) {
  324. c->basew = size.min_width;
  325. c->baseh = size.min_height;
  326. }
  327. else
  328. c->basew = c->baseh = 0;
  329. if(c->flags & PResizeInc) {
  330. c->incw = size.width_inc;
  331. c->inch = size.height_inc;
  332. }
  333. else
  334. c->incw = c->inch = 0;
  335. if(c->flags & PMaxSize) {
  336. c->maxw = size.max_width;
  337. c->maxh = size.max_height;
  338. }
  339. else
  340. c->maxw = c->maxh = 0;
  341. if(c->flags & PMinSize) {
  342. c->minw = size.min_width;
  343. c->minh = size.min_height;
  344. }
  345. else if(c->flags & PBaseSize) {
  346. c->minw = size.base_width;
  347. c->minh = size.base_height;
  348. }
  349. else
  350. c->minw = c->minh = 0;
  351. if(c->flags & PAspect) {
  352. c->minax = size.min_aspect.x;
  353. c->maxax = size.max_aspect.x;
  354. c->minay = size.min_aspect.y;
  355. c->maxay = size.max_aspect.y;
  356. }
  357. else
  358. c->minax = c->maxax = c->minay = c->maxay = 0;
  359. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  360. && c->maxw == c->minw && c->maxh == c->minh);
  361. }
  362. void
  363. updatetitle(Client *c) {
  364. char **list = NULL;
  365. int n;
  366. XTextProperty name;
  367. name.nitems = 0;
  368. c->name[0] = 0;
  369. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  370. if(!name.nitems)
  371. XGetWMName(dpy, c->win, &name);
  372. if(!name.nitems)
  373. return;
  374. if(name.encoding == XA_STRING)
  375. strncpy(c->name, (char *)name.value, sizeof c->name - 1);
  376. else {
  377. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  378. && n > 0 && *list)
  379. {
  380. strncpy(c->name, *list, sizeof c->name - 1);
  381. XFreeStringList(list);
  382. }
  383. }
  384. c->name[sizeof c->name - 1] = '\0';
  385. XFree(name.value);
  386. }