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.

342 lines
8.2 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. XEvent synev;
  63. synev.type = ConfigureNotify;
  64. synev.xconfigure.display = dpy;
  65. synev.xconfigure.event = c->win;
  66. synev.xconfigure.window = c->win;
  67. synev.xconfigure.x = c->x;
  68. synev.xconfigure.y = c->y;
  69. synev.xconfigure.width = c->w;
  70. synev.xconfigure.height = c->h;
  71. synev.xconfigure.border_width = c->border;
  72. synev.xconfigure.above = None;
  73. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  74. }
  75. void
  76. focus(Client *c) {
  77. if(c && !isvisible(c))
  78. return;
  79. if(sel && sel != c) {
  80. grabbuttons(sel, False);
  81. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  82. }
  83. if(c) {
  84. detachstack(c);
  85. c->snext = stack;
  86. stack = c;
  87. grabbuttons(c, True);
  88. }
  89. sel = c;
  90. drawstatus();
  91. if(!selscreen)
  92. return;
  93. if(c) {
  94. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  95. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  96. }
  97. else
  98. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  99. }
  100. Client *
  101. getclient(Window w) {
  102. Client *c;
  103. for(c = clients; c; c = c->next)
  104. if(c->win == w)
  105. return c;
  106. return NULL;
  107. }
  108. void
  109. killclient(Arg *arg) {
  110. if(!sel)
  111. return;
  112. if(sel->proto & PROTODELWIN)
  113. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  114. else
  115. XKillClient(dpy, sel->win);
  116. }
  117. void
  118. manage(Window w, XWindowAttributes *wa) {
  119. Client *c;
  120. Window trans;
  121. c = emallocz(sizeof(Client));
  122. c->tags = emallocz(ntags * sizeof(Bool));
  123. c->win = w;
  124. c->x = wa->x;
  125. c->y = wa->y;
  126. c->w = wa->width;
  127. c->h = wa->height;
  128. if(c->w == sw && c->h == sh) {
  129. c->border = 0;
  130. c->x = sx;
  131. c->y = sy;
  132. }
  133. else {
  134. c->border = BORDERPX;
  135. if(c->x + c->w + 2 * c->border > wax + waw)
  136. c->x = wax + waw - c->w - 2 * c->border;
  137. if(c->y + c->h + 2 * c->border > way + wah)
  138. c->y = way + wah - c->h - 2 * c->border;
  139. if(c->x < wax)
  140. c->x = wax;
  141. if(c->y < way)
  142. c->y = way;
  143. }
  144. updatesizehints(c);
  145. c->proto = getproto(c->win);
  146. XSelectInput(dpy, c->win,
  147. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  148. XGetTransientForHint(dpy, c->win, &trans);
  149. grabbuttons(c, False);
  150. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  151. updatetitle(c);
  152. settags(c, getclient(trans));
  153. if(!c->isfloat)
  154. c->isfloat = trans || c->isfixed;
  155. if(clients)
  156. clients->prev = c;
  157. c->next = clients;
  158. c->snext = stack;
  159. stack = clients = c;
  160. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  161. XMapWindow(dpy, c->win);
  162. setclientstate(c, NormalState);
  163. if(isvisible(c))
  164. focus(c);
  165. arrange();
  166. }
  167. void
  168. resize(Client *c, Bool sizehints) {
  169. float actual, dx, dy, max, min, u;
  170. XWindowChanges wc;
  171. if(c->w <= 0 || c->h <= 0)
  172. return;
  173. if(sizehints) {
  174. if(c->minw && c->w < c->minw)
  175. c->w = c->minw;
  176. if(c->minh && c->h < c->minh)
  177. c->h = c->minh;
  178. if(c->maxw && c->w > c->maxw)
  179. c->w = c->maxw;
  180. if(c->maxh && c->h > c->maxh)
  181. c->h = c->maxh;
  182. /* inspired by algorithm from fluxbox */
  183. if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
  184. dx = (float)(c->w - c->basew);
  185. dy = (float)(c->h - c->baseh);
  186. min = (float)(c->minax) / (float)(c->minay);
  187. max = (float)(c->maxax) / (float)(c->maxay);
  188. actual = dx / dy;
  189. if(max > 0 && min > 0 && actual > 0) {
  190. if(actual < min) {
  191. dy = (dx * min + dy) / (min * min + 1);
  192. dx = dy * min;
  193. c->w = (int)dx + c->basew;
  194. c->h = (int)dy + c->baseh;
  195. }
  196. else if(actual > max) {
  197. dy = (dx * min + dy) / (max * max + 1);
  198. dx = dy * min;
  199. c->w = (int)dx + c->basew;
  200. c->h = (int)dy + c->baseh;
  201. }
  202. }
  203. }
  204. if(c->incw)
  205. c->w -= (c->w - c->basew) % c->incw;
  206. if(c->inch)
  207. c->h -= (c->h - c->baseh) % c->inch;
  208. }
  209. if(c->w == sw && c->h == sh)
  210. c->border = 0;
  211. else
  212. c->border = BORDERPX;
  213. /* offscreen appearance fixes */
  214. if(c->x > sw)
  215. c->x = sw - c->w - 2 * c->border;
  216. if(c->y > sh)
  217. c->y = sh - c->h - 2 * c->border;
  218. if(c->x + c->w + 2 * c->border < sx)
  219. c->x = sx;
  220. if(c->y + c->h + 2 * c->border < sy)
  221. c->y = sy;
  222. wc.x = c->x;
  223. wc.y = c->y;
  224. wc.width = c->w;
  225. wc.height = c->h;
  226. wc.border_width = c->border;
  227. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  228. configure(c);
  229. XSync(dpy, False);
  230. }
  231. void
  232. updatesizehints(Client *c) {
  233. long msize;
  234. XSizeHints size;
  235. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  236. size.flags = PSize;
  237. c->flags = size.flags;
  238. if(c->flags & PBaseSize) {
  239. c->basew = size.base_width;
  240. c->baseh = size.base_height;
  241. }
  242. else
  243. c->basew = c->baseh = 0;
  244. if(c->flags & PResizeInc) {
  245. c->incw = size.width_inc;
  246. c->inch = size.height_inc;
  247. }
  248. else
  249. c->incw = c->inch = 0;
  250. if(c->flags & PMaxSize) {
  251. c->maxw = size.max_width;
  252. c->maxh = size.max_height;
  253. }
  254. else
  255. c->maxw = c->maxh = 0;
  256. if(c->flags & PMinSize) {
  257. c->minw = size.min_width;
  258. c->minh = size.min_height;
  259. }
  260. else
  261. c->minw = c->minh = 0;
  262. if(c->flags & PAspect) {
  263. c->minax = size.min_aspect.x;
  264. c->minay = size.min_aspect.y;
  265. c->maxax = size.max_aspect.x;
  266. c->maxay = size.max_aspect.y;
  267. }
  268. else
  269. c->minax = c->minay = c->maxax = c->maxay = 0;
  270. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
  271. c->maxw == c->minw && c->maxh == c->minh);
  272. }
  273. void
  274. updatetitle(Client *c) {
  275. char **list = NULL;
  276. int n;
  277. XTextProperty name;
  278. name.nitems = 0;
  279. c->name[0] = 0;
  280. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  281. if(!name.nitems)
  282. XGetWMName(dpy, c->win, &name);
  283. if(!name.nitems)
  284. return;
  285. if(name.encoding == XA_STRING)
  286. strncpy(c->name, (char *)name.value, sizeof c->name);
  287. else {
  288. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  289. && n > 0 && *list)
  290. {
  291. strncpy(c->name, *list, sizeof c->name);
  292. XFreeStringList(list);
  293. }
  294. }
  295. XFree(name.value);
  296. }
  297. void
  298. unmanage(Client *c) {
  299. Client *nc;
  300. /* The server grab construct avoids race conditions. */
  301. XGrabServer(dpy);
  302. XSetErrorHandler(xerrordummy);
  303. detach(c);
  304. detachstack(c);
  305. if(sel == c) {
  306. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  307. focus(nc);
  308. }
  309. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  310. setclientstate(c, WithdrawnState);
  311. free(c->tags);
  312. free(c);
  313. XSync(dpy, False);
  314. XSetErrorHandler(xerror);
  315. XUngrabServer(dpy);
  316. arrange();
  317. }