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