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.

405 lines
9.5 KiB

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