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.

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