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.

439 lines
8.6 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
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
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. /* static functions */
  11. static void
  12. detachstack(Client *c)
  13. {
  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 focus)
  20. {
  21. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  22. if(focus) {
  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 void
  53. resizetitle(Client *c)
  54. {
  55. c->tw = textw(c->name);
  56. if(c->tw > c->w)
  57. c->tw = c->w + 2;
  58. c->tx = c->x + c->w - c->tw + 2;
  59. c->ty = c->y;
  60. if(isvisible(c))
  61. XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
  62. else
  63. XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
  64. }
  65. static int
  66. xerrordummy(Display *dsply, XErrorEvent *ee)
  67. {
  68. return 0;
  69. }
  70. /* extern functions */
  71. void
  72. ban(Client *c)
  73. {
  74. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  75. XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
  76. }
  77. void
  78. focus(Client *c)
  79. {
  80. Client *old;
  81. if(!issel)
  82. return;
  83. if(!sel)
  84. sel = c;
  85. else if(sel != c) {
  86. if(maximized)
  87. togglemax(NULL);
  88. old = sel;
  89. sel = c;
  90. if(old) {
  91. grabbuttons(old, False);
  92. drawtitle(old);
  93. }
  94. }
  95. if(c) {
  96. detachstack(c);
  97. c->snext = stack;
  98. stack = c;
  99. grabbuttons(c, True);
  100. drawtitle(c);
  101. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  102. }
  103. else
  104. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  105. }
  106. Client *
  107. getclient(Window w)
  108. {
  109. Client *c;
  110. for(c = clients; c; c = c->next)
  111. if(c->win == w)
  112. return c;
  113. return NULL;
  114. }
  115. Client *
  116. getctitle(Window w)
  117. {
  118. Client *c;
  119. for(c = clients; c; c = c->next)
  120. if(c->twin == w)
  121. return c;
  122. return NULL;
  123. }
  124. void
  125. gravitate(Client *c, Bool invert)
  126. {
  127. int dx = 0, dy = 0;
  128. switch(c->grav) {
  129. default:
  130. break;
  131. case StaticGravity:
  132. case NorthWestGravity:
  133. case NorthGravity:
  134. case NorthEastGravity:
  135. dy = c->border;
  136. break;
  137. case EastGravity:
  138. case CenterGravity:
  139. case WestGravity:
  140. dy = -(c->h / 2) + c->border;
  141. break;
  142. case SouthEastGravity:
  143. case SouthGravity:
  144. case SouthWestGravity:
  145. dy = -(c->h);
  146. break;
  147. }
  148. switch (c->grav) {
  149. default:
  150. break;
  151. case StaticGravity:
  152. case NorthWestGravity:
  153. case WestGravity:
  154. case SouthWestGravity:
  155. dx = c->border;
  156. break;
  157. case NorthGravity:
  158. case CenterGravity:
  159. case SouthGravity:
  160. dx = -(c->w / 2) + c->border;
  161. break;
  162. case NorthEastGravity:
  163. case EastGravity:
  164. case SouthEastGravity:
  165. dx = -(c->w + c->border);
  166. break;
  167. }
  168. if(invert) {
  169. dx = -dx;
  170. dy = -dy;
  171. }
  172. c->x += dx;
  173. c->y += dy;
  174. }
  175. void
  176. killclient(Arg *arg)
  177. {
  178. if(!sel)
  179. return;
  180. if(sel->proto & PROTODELWIN)
  181. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  182. else
  183. XKillClient(dpy, sel->win);
  184. }
  185. void
  186. manage(Window w, XWindowAttributes *wa)
  187. {
  188. Client *c;
  189. Window trans;
  190. XSetWindowAttributes twa;
  191. c = emallocz(sizeof(Client));
  192. c->tags = emallocz(ntags * sizeof(Bool));
  193. c->win = w;
  194. c->x = c->tx = wa->x;
  195. c->y = c->ty = wa->y;
  196. c->w = c->tw = wa->width;
  197. c->h = wa->height;
  198. c->th = bh;
  199. c->border = 0;
  200. setsize(c);
  201. if(c->x + c->w + 2 > sw)
  202. c->x = sw - c->w - 2;
  203. if(c->x < 0)
  204. c->x = 0;
  205. if(c->y + c->h + 2 > sh)
  206. c->y = sh - c->h - 2;
  207. if(c->h != sh && c->y < bh)
  208. c->y = bh;
  209. c->proto = getproto(c->win);
  210. XSelectInput(dpy, c->win,
  211. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  212. XGetTransientForHint(dpy, c->win, &trans);
  213. twa.override_redirect = 1;
  214. twa.background_pixmap = ParentRelative;
  215. twa.event_mask = ExposureMask | EnterWindowMask;
  216. c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  217. 0, DefaultDepth(dpy, screen), CopyFromParent,
  218. DefaultVisual(dpy, screen),
  219. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  220. grabbuttons(c, False);
  221. settags(c, getclient(trans));
  222. if(!c->isfloat)
  223. c->isfloat = trans
  224. || (c->maxw && c->minw &&
  225. c->maxw == c->minw && c->maxh == c->minh);
  226. if(clients)
  227. clients->prev = c;
  228. c->next = clients;
  229. c->snext = stack;
  230. stack = clients = c;
  231. settitle(c);
  232. ban(c);
  233. XMapWindow(dpy, c->win);
  234. XMapWindow(dpy, c->twin);
  235. if(isvisible(c))
  236. focus(c);
  237. arrange(NULL);
  238. }
  239. void
  240. resize(Client *c, Bool sizehints, Corner sticky)
  241. {
  242. int bottom = c->y + c->h;
  243. int right = c->x + c->w;
  244. XWindowChanges wc;
  245. if(sizehints) {
  246. if(c->incw)
  247. c->w -= (c->w - c->basew) % c->incw;
  248. if(c->inch)
  249. c->h -= (c->h - c->baseh) % c->inch;
  250. if(c->minw && c->w < c->minw)
  251. c->w = c->minw;
  252. if(c->minh && c->h < c->minh)
  253. c->h = c->minh;
  254. if(c->maxw && c->w > c->maxw)
  255. c->w = c->maxw;
  256. if(c->maxh && c->h > c->maxh)
  257. c->h = c->maxh;
  258. }
  259. if(sticky == TopRight || sticky == BotRight)
  260. c->x = right - c->w;
  261. if(sticky == BotLeft || sticky == BotRight)
  262. c->y = bottom - c->h;
  263. resizetitle(c);
  264. wc.x = c->x;
  265. wc.y = c->y;
  266. wc.width = c->w;
  267. wc.height = c->h;
  268. if(c->w == sw && c->h == sh)
  269. wc.border_width = 0;
  270. else
  271. wc.border_width = 1;
  272. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  273. XSync(dpy, False);
  274. }
  275. void
  276. setsize(Client *c)
  277. {
  278. long msize;
  279. XSizeHints size;
  280. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  281. size.flags = PSize;
  282. c->flags = size.flags;
  283. if(c->flags & PBaseSize) {
  284. c->basew = size.base_width;
  285. c->baseh = size.base_height;
  286. }
  287. else
  288. c->basew = c->baseh = 0;
  289. if(c->flags & PResizeInc) {
  290. c->incw = size.width_inc;
  291. c->inch = size.height_inc;
  292. }
  293. else
  294. c->incw = c->inch = 0;
  295. if(c->flags & PMaxSize) {
  296. c->maxw = size.max_width;
  297. c->maxh = size.max_height;
  298. }
  299. else
  300. c->maxw = c->maxh = 0;
  301. if(c->flags & PMinSize) {
  302. c->minw = size.min_width;
  303. c->minh = size.min_height;
  304. }
  305. else
  306. c->minw = c->minh = 0;
  307. if(c->flags & PWinGravity)
  308. c->grav = size.win_gravity;
  309. else
  310. c->grav = NorthWestGravity;
  311. }
  312. void
  313. settitle(Client *c)
  314. {
  315. char **list = NULL;
  316. int n;
  317. XTextProperty name;
  318. name.nitems = 0;
  319. c->name[0] = 0;
  320. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  321. if(!name.nitems)
  322. XGetWMName(dpy, c->win, &name);
  323. if(!name.nitems)
  324. return;
  325. if(name.encoding == XA_STRING)
  326. strncpy(c->name, (char *)name.value, sizeof(c->name));
  327. else {
  328. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  329. && n > 0 && *list)
  330. {
  331. strncpy(c->name, *list, sizeof(c->name));
  332. XFreeStringList(list);
  333. }
  334. }
  335. XFree(name.value);
  336. resizetitle(c);
  337. }
  338. void
  339. togglemax(Arg *arg)
  340. {
  341. int ox, oy, ow, oh;
  342. Client *c;
  343. XEvent ev;
  344. if(!sel)
  345. return;
  346. if((maximized = !maximized)) {
  347. ox = sel->x;
  348. oy = sel->y;
  349. ow = sel->w;
  350. oh = sel->h;
  351. sel->x = sx;
  352. sel->y = sy + bh;
  353. sel->w = sw - 2;
  354. sel->h = sh - 2 - bh;
  355. restack();
  356. for(c = getnext(clients); c; c = getnext(c->next))
  357. if(c != sel)
  358. ban(c);
  359. resize(sel, arrange == dofloat, TopLeft);
  360. sel->x = ox;
  361. sel->y = oy;
  362. sel->w = ow;
  363. sel->h = oh;
  364. }
  365. else
  366. arrange(NULL);
  367. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  368. }
  369. void
  370. unmanage(Client *c)
  371. {
  372. Client *nc;
  373. XGrabServer(dpy);
  374. XSetErrorHandler(xerrordummy);
  375. detach(c);
  376. detachstack(c);
  377. if(sel == c) {
  378. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  379. focus(nc);
  380. }
  381. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  382. XDestroyWindow(dpy, c->twin);
  383. free(c->tags);
  384. free(c);
  385. XSync(dpy, False);
  386. XSetErrorHandler(xerror);
  387. XUngrabServer(dpy);
  388. arrange(NULL);
  389. }