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.

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