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.

417 lines
9.6 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. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  88. c->isbanned = True;
  89. }
  90. void
  91. configure(Client *c) {
  92. XConfigureEvent ce;
  93. ce.type = ConfigureNotify;
  94. ce.display = dpy;
  95. ce.event = c->win;
  96. ce.window = c->win;
  97. ce.x = c->x;
  98. ce.y = c->y;
  99. ce.width = c->w;
  100. ce.height = c->h;
  101. ce.border_width = c->border;
  102. ce.above = None;
  103. ce.override_redirect = False;
  104. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  105. }
  106. void
  107. detach(Client *c) {
  108. if(c->prev)
  109. c->prev->next = c->next;
  110. if(c->next)
  111. c->next->prev = c->prev;
  112. if(c == clients)
  113. clients = c->next;
  114. c->next = c->prev = NULL;
  115. }
  116. void
  117. focus(Client *c) {
  118. if((!c && selscreen)|| (c && !isvisible(c)))
  119. for(c = stack; c && !isvisible(c); c = c->snext);
  120. if(sel && sel != c) {
  121. grabbuttons(sel, False);
  122. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  123. }
  124. if(c) {
  125. detachstack(c);
  126. attachstack(c);
  127. grabbuttons(c, True);
  128. }
  129. sel = c;
  130. drawstatus();
  131. if(!selscreen)
  132. return;
  133. if(c) {
  134. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  135. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  136. }
  137. else
  138. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  139. }
  140. void
  141. killclient(const char *arg) {
  142. XEvent ev;
  143. if(!sel)
  144. return;
  145. if(isprotodel(sel)) {
  146. ev.type = ClientMessage;
  147. ev.xclient.window = sel->win;
  148. ev.xclient.message_type = wmatom[WMProtocols];
  149. ev.xclient.format = 32;
  150. ev.xclient.data.l[0] = wmatom[WMDelete];
  151. ev.xclient.data.l[1] = CurrentTime;
  152. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  153. }
  154. else
  155. XKillClient(dpy, sel->win);
  156. }
  157. void
  158. manage(Window w, XWindowAttributes *wa) {
  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. settags(c, t);
  199. if(!c->isfloating)
  200. c->isfloating = (rettrans == Success) || c->isfixed;
  201. attach(c);
  202. attachstack(c);
  203. ban(c);
  204. XMapWindow(dpy, w);
  205. setclientstate(c, NormalState);
  206. focus(c);
  207. lt->arrange();
  208. }
  209. void
  210. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  211. float dx, dy, max, min, ratio;
  212. XWindowChanges wc;
  213. if(w <= 0 || h <= 0)
  214. return;
  215. if(sizehints) {
  216. if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0) {
  217. dx = (float)(w - c->basew);
  218. dy = (float)(h - c->baseh);
  219. min = (float)(c->minax) / (float)(c->minay);
  220. max = (float)(c->maxax) / (float)(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. togglefloating(const char *arg) {
  274. if(!sel || lt->arrange == floating)
  275. return;
  276. sel->isfloating = !sel->isfloating;
  277. if(sel->isfloating)
  278. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  279. lt->arrange();
  280. }
  281. void
  282. unban(Client *c) {
  283. if (!c->isbanned)
  284. return;
  285. XMoveWindow(dpy, c->win, c->x, c->y);
  286. c->isbanned = False;
  287. }
  288. void
  289. unmanage(Client *c) {
  290. XWindowChanges wc;
  291. wc.border_width = c->oldborder;
  292. /* The server grab construct avoids race conditions. */
  293. XGrabServer(dpy);
  294. XSetErrorHandler(xerrordummy);
  295. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  296. detach(c);
  297. detachstack(c);
  298. if(sel == c)
  299. focus(NULL);
  300. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  301. setclientstate(c, WithdrawnState);
  302. free(c->tags);
  303. free(c);
  304. XSync(dpy, False);
  305. XSetErrorHandler(xerror);
  306. XUngrabServer(dpy);
  307. lt->arrange();
  308. }
  309. void
  310. updatesizehints(Client *c) {
  311. long msize;
  312. XSizeHints size;
  313. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  314. size.flags = PSize;
  315. c->flags = size.flags;
  316. if(c->flags & PBaseSize) {
  317. c->basew = size.base_width;
  318. c->baseh = size.base_height;
  319. }
  320. else if(c->flags & PMinSize) {
  321. c->basew = size.min_width;
  322. c->baseh = size.min_height;
  323. }
  324. else
  325. c->basew = c->baseh = 0;
  326. if(c->flags & PResizeInc) {
  327. c->incw = size.width_inc;
  328. c->inch = size.height_inc;
  329. }
  330. else
  331. c->incw = c->inch = 0;
  332. if(c->flags & PMaxSize) {
  333. c->maxw = size.max_width;
  334. c->maxh = size.max_height;
  335. }
  336. else
  337. c->maxw = c->maxh = 0;
  338. if(c->flags & PMinSize) {
  339. c->minw = size.min_width;
  340. c->minh = size.min_height;
  341. }
  342. else if(c->flags & PBaseSize) {
  343. c->minw = size.base_width;
  344. c->minh = size.base_height;
  345. }
  346. else
  347. c->minw = c->minh = 0;
  348. if(c->flags & PAspect) {
  349. c->minax = size.min_aspect.x;
  350. c->maxax = size.max_aspect.x;
  351. c->minay = size.min_aspect.y;
  352. c->maxay = size.max_aspect.y;
  353. }
  354. else
  355. c->minax = c->maxax = c->minay = c->maxay = 0;
  356. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  357. && c->maxw == c->minw && c->maxh == c->minh);
  358. }
  359. void
  360. updatetitle(Client *c) {
  361. char **list = NULL;
  362. int n;
  363. XTextProperty name;
  364. name.nitems = 0;
  365. c->name[0] = 0;
  366. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  367. if(!name.nitems)
  368. XGetWMName(dpy, c->win, &name);
  369. if(!name.nitems)
  370. return;
  371. if(name.encoding == XA_STRING)
  372. strncpy(c->name, (char *)name.value, sizeof c->name - 1);
  373. else {
  374. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  375. && n > 0 && *list)
  376. {
  377. strncpy(c->name, *list, sizeof c->name - 1);
  378. XFreeStringList(list);
  379. }
  380. }
  381. c->name[sizeof c->name - 1] = '\0';
  382. XFree(name.value);
  383. }