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.

401 lines
9.4 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
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. ban(c);
  197. XMapWindow(dpy, w);
  198. setclientstate(c, NormalState);
  199. focus(c);
  200. lt->arrange();
  201. }
  202. void
  203. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  204. float dx, dy, max, min, ratio;
  205. XWindowChanges wc;
  206. if(w <= 0 || h <= 0)
  207. return;
  208. if(sizehints) {
  209. if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0) {
  210. dx = (float)(w - c->basew);
  211. dy = (float)(h - c->baseh);
  212. min = (float)(c->minax) / (float)(c->minay);
  213. max = (float)(c->maxax) / (float)(c->maxay);
  214. ratio = dx / dy;
  215. if(max > 0 && min > 0 && ratio > 0) {
  216. if(ratio < min) {
  217. dy = (dx * min + dy) / (min * min + 1);
  218. dx = dy * min;
  219. w = (int)dx + c->basew;
  220. h = (int)dy + c->baseh;
  221. }
  222. else if(ratio > max) {
  223. dy = (dx * min + dy) / (max * max + 1);
  224. dx = dy * min;
  225. w = (int)dx + c->basew;
  226. h = (int)dy + c->baseh;
  227. }
  228. }
  229. }
  230. if(c->minw && w < c->minw)
  231. w = c->minw;
  232. if(c->minh && h < c->minh)
  233. h = c->minh;
  234. if(c->maxw && w > c->maxw)
  235. w = c->maxw;
  236. if(c->maxh && h > c->maxh)
  237. h = c->maxh;
  238. if(c->incw)
  239. w -= (w - c->basew) % c->incw;
  240. if(c->inch)
  241. h -= (h - c->baseh) % c->inch;
  242. }
  243. if(w <= 0 || h <= 0)
  244. return;
  245. /* offscreen appearance fixes */
  246. if(x > sw)
  247. x = sw - w - 2 * c->border;
  248. if(y > sh)
  249. y = sh - h - 2 * c->border;
  250. if(x + w + 2 * c->border < sx)
  251. x = sx;
  252. if(y + h + 2 * c->border < sy)
  253. y = sy;
  254. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  255. c->x = wc.x = x;
  256. c->y = wc.y = y;
  257. c->w = wc.width = w;
  258. c->h = wc.height = h;
  259. wc.border_width = c->border;
  260. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  261. configure(c);
  262. XSync(dpy, False);
  263. }
  264. }
  265. void
  266. togglefloating(const char *arg) {
  267. if(!sel || lt->arrange == floating)
  268. return;
  269. sel->isfloating = !sel->isfloating;
  270. if(sel->isfloating)
  271. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  272. lt->arrange();
  273. }
  274. void
  275. updatesizehints(Client *c) {
  276. long msize;
  277. XSizeHints size;
  278. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  279. size.flags = PSize;
  280. c->flags = size.flags;
  281. if(c->flags & PBaseSize) {
  282. c->basew = size.base_width;
  283. c->baseh = size.base_height;
  284. }
  285. else if(c->flags & PMinSize) {
  286. c->basew = size.min_width;
  287. c->baseh = size.min_height;
  288. }
  289. else
  290. c->basew = c->baseh = 0;
  291. if(c->flags & PResizeInc) {
  292. c->incw = size.width_inc;
  293. c->inch = size.height_inc;
  294. }
  295. else
  296. c->incw = c->inch = 0;
  297. if(c->flags & PMaxSize) {
  298. c->maxw = size.max_width;
  299. c->maxh = size.max_height;
  300. }
  301. else
  302. c->maxw = c->maxh = 0;
  303. if(c->flags & PMinSize) {
  304. c->minw = size.min_width;
  305. c->minh = size.min_height;
  306. }
  307. else if(c->flags & PBaseSize) {
  308. c->minw = size.base_width;
  309. c->minh = size.base_height;
  310. }
  311. else
  312. c->minw = c->minh = 0;
  313. if(c->flags & PAspect) {
  314. c->minax = size.min_aspect.x;
  315. c->maxax = size.max_aspect.x;
  316. c->minay = size.min_aspect.y;
  317. c->maxay = size.max_aspect.y;
  318. }
  319. else
  320. c->minax = c->maxax = c->minay = c->maxay = 0;
  321. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  322. && c->maxw == c->minw && c->maxh == c->minh);
  323. }
  324. void
  325. updatetitle(Client *c) {
  326. char **list = NULL;
  327. int n;
  328. XTextProperty name;
  329. name.nitems = 0;
  330. c->name[0] = 0;
  331. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  332. if(!name.nitems)
  333. XGetWMName(dpy, c->win, &name);
  334. if(!name.nitems)
  335. return;
  336. if(name.encoding == XA_STRING)
  337. strncpy(c->name, (char *)name.value, sizeof c->name - 1);
  338. else {
  339. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  340. && n > 0 && *list)
  341. {
  342. strncpy(c->name, *list, sizeof c->name - 1);
  343. XFreeStringList(list);
  344. }
  345. }
  346. c->name[sizeof c->name - 1] = '\0';
  347. XFree(name.value);
  348. }
  349. void
  350. unmanage(Client *c) {
  351. XWindowChanges wc;
  352. wc.border_width = c->oldborder;
  353. /* The server grab construct avoids race conditions. */
  354. XGrabServer(dpy);
  355. XSetErrorHandler(xerrordummy);
  356. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  357. detach(c);
  358. detachstack(c);
  359. if(sel == c)
  360. focus(NULL);
  361. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  362. setclientstate(c, WithdrawnState);
  363. free(c->tags);
  364. free(c);
  365. XSync(dpy, False);
  366. XSetErrorHandler(xerror);
  367. XUngrabServer(dpy);
  368. lt->arrange();
  369. }