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.

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