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.

365 lines
8.6 KiB

18 years ago
19 years ago
18 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
18 years ago
18 years ago
19 years ago
19 years ago
18 years ago
18 years ago
19 years ago
  1. /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <X11/Xatom.h>
  8. #include <X11/Xutil.h>
  9. /* static */
  10. static void
  11. detachstack(Client *c) {
  12. Client **tc;
  13. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  14. *tc = c->snext;
  15. }
  16. static void
  17. grabbuttons(Client *c, Bool focused) {
  18. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  19. if(focused) {
  20. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  21. GrabModeAsync, GrabModeSync, None, None);
  22. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  23. GrabModeAsync, GrabModeSync, None, None);
  24. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  25. GrabModeAsync, GrabModeSync, None, None);
  26. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  27. GrabModeAsync, GrabModeSync, None, None);
  28. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  29. GrabModeAsync, GrabModeSync, None, None);
  30. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  31. GrabModeAsync, GrabModeSync, None, None);
  32. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  33. GrabModeAsync, GrabModeSync, None, None);
  34. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  35. GrabModeAsync, GrabModeSync, None, None);
  36. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  37. GrabModeAsync, GrabModeSync, None, None);
  38. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  39. GrabModeAsync, GrabModeSync, None, None);
  40. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  41. GrabModeAsync, GrabModeSync, None, None);
  42. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  43. GrabModeAsync, GrabModeSync, None, None);
  44. }
  45. else
  46. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  47. GrabModeAsync, GrabModeSync, None, None);
  48. }
  49. static void
  50. setclientstate(Client *c, long state) {
  51. long data[] = {state, None};
  52. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  53. PropModeReplace, (unsigned char *)data, 2);
  54. }
  55. static int
  56. xerrordummy(Display *dsply, XErrorEvent *ee) {
  57. return 0;
  58. }
  59. /* extern */
  60. void
  61. configure(Client *c) {
  62. XConfigureEvent ce;
  63. ce.type = ConfigureNotify;
  64. ce.display = dpy;
  65. ce.event = c->win;
  66. ce.window = c->win;
  67. ce.x = c->x;
  68. ce.y = c->y;
  69. ce.width = c->w;
  70. ce.height = c->h;
  71. ce.border_width = c->border;
  72. ce.above = None;
  73. ce.override_redirect = False;
  74. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  75. }
  76. void
  77. focus(Client *c) {
  78. if(c && !isvisible(c))
  79. return;
  80. if(sel && sel != c) {
  81. grabbuttons(sel, False);
  82. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  83. }
  84. if(c) {
  85. detachstack(c);
  86. c->snext = stack;
  87. stack = c;
  88. grabbuttons(c, True);
  89. }
  90. sel = c;
  91. drawstatus();
  92. if(!selscreen)
  93. return;
  94. if(c) {
  95. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  96. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  97. }
  98. else
  99. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  100. }
  101. Client *
  102. getclient(Window w) {
  103. Client *c;
  104. for(c = clients; c; c = c->next)
  105. if(c->win == w)
  106. return c;
  107. return NULL;
  108. }
  109. Bool
  110. isprotodel(Client *c) {
  111. int i, n;
  112. Atom *protocols;
  113. Bool ret = False;
  114. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  115. for(i = 0; !ret && i < n; i++)
  116. if(protocols[i] == wmatom[WMDelete])
  117. ret = True;
  118. XFree(protocols);
  119. }
  120. return ret;
  121. }
  122. void
  123. killclient(Arg *arg) {
  124. if(!sel)
  125. return;
  126. if(isprotodel(sel))
  127. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  128. else
  129. XKillClient(dpy, sel->win);
  130. }
  131. void
  132. manage(Window w, XWindowAttributes *wa) {
  133. Client *c, *t;
  134. Window trans;
  135. XWindowChanges wc;
  136. c = emallocz(sizeof(Client));
  137. c->tags = emallocz(ntags * sizeof(Bool));
  138. c->win = w;
  139. c->x = wa->x;
  140. c->y = wa->y;
  141. c->w = wa->width;
  142. c->h = wa->height;
  143. if(c->w == sw && c->h == sh) {
  144. c->border = 0;
  145. c->x = sx;
  146. c->y = sy;
  147. }
  148. else {
  149. c->border = BORDERPX;
  150. if(c->x + c->w + 2 * c->border > wax + waw)
  151. c->x = wax + waw - c->w - 2 * c->border;
  152. if(c->y + c->h + 2 * c->border > way + wah)
  153. c->y = way + wah - c->h - 2 * c->border;
  154. if(c->x < wax)
  155. c->x = wax;
  156. if(c->y < way)
  157. c->y = way;
  158. }
  159. updatesizehints(c);
  160. XSelectInput(dpy, c->win,
  161. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  162. XGetTransientForHint(dpy, c->win, &trans);
  163. grabbuttons(c, False);
  164. wc.border_width = c->border;
  165. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
  166. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  167. configure(c); /* propagates border_width, if size doesn't change */
  168. updatetitle(c);
  169. t = getclient(trans);
  170. settags(c, t);
  171. if(!c->isfloat)
  172. c->isfloat = (t != 0) || c->isfixed;
  173. if(clients)
  174. clients->prev = c;
  175. c->next = clients;
  176. c->snext = stack;
  177. stack = clients = c;
  178. c->isbanned = True;
  179. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  180. XMapWindow(dpy, c->win);
  181. setclientstate(c, NormalState);
  182. if(isvisible(c))
  183. focus(c);
  184. arrange();
  185. }
  186. void
  187. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  188. float actual, dx, dy, max, min;
  189. XWindowChanges wc;
  190. if(w <= 0 || h <= 0)
  191. return;
  192. if(sizehints) {
  193. if(c->minw && w < c->minw)
  194. w = c->minw;
  195. if(c->minh && h < c->minh)
  196. h = c->minh;
  197. if(c->maxw && w > c->maxw)
  198. w = c->maxw;
  199. if(c->maxh && h > c->maxh)
  200. h = c->maxh;
  201. /* inspired by algorithm from fluxbox */
  202. if(c->minay > 0 && c->maxay && (h - c->baseh) > 0) {
  203. dx = (float)(w - c->basew);
  204. dy = (float)(h - c->baseh);
  205. min = (float)(c->minax) / (float)(c->minay);
  206. max = (float)(c->maxax) / (float)(c->maxay);
  207. actual = dx / dy;
  208. if(max > 0 && min > 0 && actual > 0) {
  209. if(actual < min) {
  210. dy = (dx * min + dy) / (min * min + 1);
  211. dx = dy * min;
  212. w = (int)dx + c->basew;
  213. h = (int)dy + c->baseh;
  214. }
  215. else if(actual > max) {
  216. dy = (dx * min + dy) / (max * max + 1);
  217. dx = dy * min;
  218. w = (int)dx + c->basew;
  219. h = (int)dy + c->baseh;
  220. }
  221. }
  222. }
  223. if(c->incw)
  224. w -= (w - c->basew) % c->incw;
  225. if(c->inch)
  226. h -= (h - c->baseh) % c->inch;
  227. }
  228. if(w == sw && h == sh)
  229. c->border = 0;
  230. else
  231. c->border = BORDERPX;
  232. /* offscreen appearance fixes */
  233. if(x > sw)
  234. x = sw - w - 2 * c->border;
  235. if(y > sh)
  236. y = sh - h - 2 * c->border;
  237. if(x + w + 2 * c->border < sx)
  238. x = sx;
  239. if(y + h + 2 * c->border < sy)
  240. y = sy;
  241. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  242. c->x = wc.x = x;
  243. c->y = wc.y = y;
  244. c->w = wc.width = w;
  245. c->h = wc.height = h;
  246. wc.border_width = c->border;
  247. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  248. configure(c);
  249. XSync(dpy, False);
  250. }
  251. }
  252. void
  253. updatesizehints(Client *c) {
  254. long msize;
  255. XSizeHints size;
  256. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  257. size.flags = PSize;
  258. c->flags = size.flags;
  259. if(c->flags & PBaseSize) {
  260. c->basew = size.base_width;
  261. c->baseh = size.base_height;
  262. }
  263. else
  264. c->basew = c->baseh = 0;
  265. if(c->flags & PResizeInc) {
  266. c->incw = size.width_inc;
  267. c->inch = size.height_inc;
  268. }
  269. else
  270. c->incw = c->inch = 0;
  271. if(c->flags & PMaxSize) {
  272. c->maxw = size.max_width;
  273. c->maxh = size.max_height;
  274. }
  275. else
  276. c->maxw = c->maxh = 0;
  277. if(c->flags & PMinSize) {
  278. c->minw = size.min_width;
  279. c->minh = size.min_height;
  280. }
  281. else
  282. c->minw = c->minh = 0;
  283. if(c->flags & PAspect) {
  284. c->minax = size.min_aspect.x;
  285. c->minay = size.min_aspect.y;
  286. c->maxax = size.max_aspect.x;
  287. c->maxay = size.max_aspect.y;
  288. }
  289. else
  290. c->minax = c->minay = c->maxax = c->maxay = 0;
  291. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  292. && c->maxw == c->minw && c->maxh == c->minh);
  293. }
  294. void
  295. updatetitle(Client *c) {
  296. char **list = NULL;
  297. int n;
  298. XTextProperty name;
  299. name.nitems = 0;
  300. c->name[0] = 0;
  301. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  302. if(!name.nitems)
  303. XGetWMName(dpy, c->win, &name);
  304. if(!name.nitems)
  305. return;
  306. if(name.encoding == XA_STRING)
  307. strncpy(c->name, (char *)name.value, sizeof c->name);
  308. else {
  309. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  310. && n > 0 && *list)
  311. {
  312. strncpy(c->name, *list, sizeof c->name);
  313. XFreeStringList(list);
  314. }
  315. }
  316. XFree(name.value);
  317. }
  318. void
  319. unmanage(Client *c) {
  320. Client *nc;
  321. /* The server grab construct avoids race conditions. */
  322. XGrabServer(dpy);
  323. XSetErrorHandler(xerrordummy);
  324. detach(c);
  325. detachstack(c);
  326. if(sel == c) {
  327. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  328. focus(nc);
  329. }
  330. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  331. setclientstate(c, WithdrawnState);
  332. free(c->tags);
  333. free(c);
  334. XSync(dpy, False);
  335. XSetErrorHandler(xerror);
  336. XUngrabServer(dpy);
  337. arrange();
  338. }