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.

357 lines
8.4 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
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;
  134. Window trans;
  135. c = emallocz(sizeof(Client));
  136. c->tags = emallocz(ntags * sizeof(Bool));
  137. c->win = w;
  138. c->x = wa->x;
  139. c->y = wa->y;
  140. c->w = wa->width;
  141. c->h = wa->height;
  142. if(c->w == sw && c->h == sh) {
  143. c->border = 0;
  144. c->x = sx;
  145. c->y = sy;
  146. }
  147. else {
  148. c->border = BORDERPX;
  149. if(c->x + c->w + 2 * c->border > wax + waw)
  150. c->x = wax + waw - c->w - 2 * c->border;
  151. if(c->y + c->h + 2 * c->border > way + wah)
  152. c->y = way + wah - c->h - 2 * c->border;
  153. if(c->x < wax)
  154. c->x = wax;
  155. if(c->y < way)
  156. c->y = way;
  157. }
  158. updatesizehints(c);
  159. XSelectInput(dpy, c->win,
  160. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  161. XGetTransientForHint(dpy, c->win, &trans);
  162. grabbuttons(c, False);
  163. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  164. updatetitle(c);
  165. settags(c, getclient(trans));
  166. if(!c->isfloat)
  167. c->isfloat = trans || c->isfixed;
  168. if(clients)
  169. clients->prev = c;
  170. c->next = clients;
  171. c->snext = stack;
  172. stack = clients = c;
  173. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  174. XMapWindow(dpy, c->win);
  175. setclientstate(c, NormalState);
  176. if(isvisible(c))
  177. focus(c);
  178. arrange();
  179. }
  180. void
  181. resize(Client *c, Bool sizehints) {
  182. float actual, dx, dy, max, min;
  183. XWindowChanges wc;
  184. if(c->w <= 0 || c->h <= 0)
  185. return;
  186. if(sizehints) {
  187. if(c->minw && c->w < c->minw)
  188. c->w = c->minw;
  189. if(c->minh && c->h < c->minh)
  190. c->h = c->minh;
  191. if(c->maxw && c->w > c->maxw)
  192. c->w = c->maxw;
  193. if(c->maxh && c->h > c->maxh)
  194. c->h = c->maxh;
  195. /* inspired by algorithm from fluxbox */
  196. if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
  197. dx = (float)(c->w - c->basew);
  198. dy = (float)(c->h - c->baseh);
  199. min = (float)(c->minax) / (float)(c->minay);
  200. max = (float)(c->maxax) / (float)(c->maxay);
  201. actual = dx / dy;
  202. if(max > 0 && min > 0 && actual > 0) {
  203. if(actual < min) {
  204. dy = (dx * min + dy) / (min * min + 1);
  205. dx = dy * min;
  206. c->w = (int)dx + c->basew;
  207. c->h = (int)dy + c->baseh;
  208. }
  209. else if(actual > max) {
  210. dy = (dx * min + dy) / (max * max + 1);
  211. dx = dy * min;
  212. c->w = (int)dx + c->basew;
  213. c->h = (int)dy + c->baseh;
  214. }
  215. }
  216. }
  217. if(c->incw)
  218. c->w -= (c->w - c->basew) % c->incw;
  219. if(c->inch)
  220. c->h -= (c->h - c->baseh) % c->inch;
  221. }
  222. if(c->w == sw && c->h == sh)
  223. c->border = 0;
  224. else
  225. c->border = BORDERPX;
  226. /* offscreen appearance fixes */
  227. if(c->x > sw)
  228. c->x = sw - c->w - 2 * c->border;
  229. if(c->y > sh)
  230. c->y = sh - c->h - 2 * c->border;
  231. if(c->x + c->w + 2 * c->border < sx)
  232. c->x = sx;
  233. if(c->y + c->h + 2 * c->border < sy)
  234. c->y = sy;
  235. wc.x = c->x;
  236. wc.y = c->y;
  237. wc.width = c->w;
  238. wc.height = c->h;
  239. wc.border_width = c->border;
  240. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  241. configure(c);
  242. XSync(dpy, False);
  243. }
  244. void
  245. updatesizehints(Client *c) {
  246. long msize;
  247. XSizeHints size;
  248. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  249. size.flags = PSize;
  250. c->flags = size.flags;
  251. if(c->flags & PBaseSize) {
  252. c->basew = size.base_width;
  253. c->baseh = size.base_height;
  254. }
  255. else
  256. c->basew = c->baseh = 0;
  257. if(c->flags & PResizeInc) {
  258. c->incw = size.width_inc;
  259. c->inch = size.height_inc;
  260. }
  261. else
  262. c->incw = c->inch = 0;
  263. if(c->flags & PMaxSize) {
  264. c->maxw = size.max_width;
  265. c->maxh = size.max_height;
  266. }
  267. else
  268. c->maxw = c->maxh = 0;
  269. if(c->flags & PMinSize) {
  270. c->minw = size.min_width;
  271. c->minh = size.min_height;
  272. }
  273. else
  274. c->minw = c->minh = 0;
  275. if(c->flags & PAspect) {
  276. c->minax = size.min_aspect.x;
  277. c->minay = size.min_aspect.y;
  278. c->maxax = size.max_aspect.x;
  279. c->maxay = size.max_aspect.y;
  280. }
  281. else
  282. c->minax = c->minay = c->maxax = c->maxay = 0;
  283. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
  284. c->maxw == c->minw && c->maxh == c->minh);
  285. }
  286. void
  287. updatetitle(Client *c) {
  288. char **list = NULL;
  289. int n;
  290. XTextProperty name;
  291. name.nitems = 0;
  292. c->name[0] = 0;
  293. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  294. if(!name.nitems)
  295. XGetWMName(dpy, c->win, &name);
  296. if(!name.nitems)
  297. return;
  298. if(name.encoding == XA_STRING)
  299. strncpy(c->name, (char *)name.value, sizeof c->name);
  300. else {
  301. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  302. && n > 0 && *list)
  303. {
  304. strncpy(c->name, *list, sizeof c->name);
  305. XFreeStringList(list);
  306. }
  307. }
  308. XFree(name.value);
  309. }
  310. void
  311. unmanage(Client *c) {
  312. Client *nc;
  313. /* The server grab construct avoids race conditions. */
  314. XGrabServer(dpy);
  315. XSetErrorHandler(xerrordummy);
  316. detach(c);
  317. detachstack(c);
  318. if(sel == c) {
  319. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  320. focus(nc);
  321. }
  322. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  323. setclientstate(c, WithdrawnState);
  324. free(c->tags);
  325. free(c);
  326. XSync(dpy, False);
  327. XSetErrorHandler(xerror);
  328. XUngrabServer(dpy);
  329. arrange();
  330. }