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.

341 lines
7.9 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 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
  1. /* (C)opyright MMVI 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 functions */
  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 int
  50. xerrordummy(Display *dsply, XErrorEvent *ee) {
  51. return 0;
  52. }
  53. /* extern functions */
  54. void
  55. ban(Client *c) {
  56. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  57. XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
  58. }
  59. void
  60. configure(Client *c) {
  61. XEvent synev;
  62. synev.type = ConfigureNotify;
  63. synev.xconfigure.display = dpy;
  64. synev.xconfigure.event = c->win;
  65. synev.xconfigure.window = c->win;
  66. synev.xconfigure.x = c->x;
  67. synev.xconfigure.y = c->y;
  68. synev.xconfigure.width = c->w;
  69. synev.xconfigure.height = c->h;
  70. synev.xconfigure.border_width = c->border;
  71. synev.xconfigure.above = None;
  72. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  73. }
  74. void
  75. focus(Client *c) {
  76. Client *old;
  77. if(!issel || (c && !isvisible(c)))
  78. return;
  79. if(!sel)
  80. sel = c;
  81. else if(sel != c) {
  82. old = sel;
  83. sel = c;
  84. if(old) {
  85. grabbuttons(old, False);
  86. drawtitle(old);
  87. }
  88. }
  89. if(c) {
  90. detachstack(c);
  91. c->snext = stack;
  92. stack = c;
  93. grabbuttons(c, True);
  94. drawtitle(c);
  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. Client *
  109. getctitle(Window w) {
  110. Client *c;
  111. for(c = clients; c; c = c->next)
  112. if(c->twin == w)
  113. return c;
  114. return NULL;
  115. }
  116. void
  117. killclient(Arg *arg) {
  118. if(!sel)
  119. return;
  120. if(sel->proto & PROTODELWIN)
  121. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  122. else
  123. XKillClient(dpy, sel->win);
  124. }
  125. void
  126. manage(Window w, XWindowAttributes *wa) {
  127. Client *c;
  128. Window trans;
  129. XSetWindowAttributes twa;
  130. c = emallocz(sizeof(Client));
  131. c->tags = emallocz(ntags * sizeof(Bool));
  132. c->win = w;
  133. c->border = 0;
  134. c->x = c->tx = wa->x;
  135. c->y = c->ty = wa->y;
  136. c->w = c->tw = wa->width;
  137. c->h = wa->height;
  138. c->th = bh;
  139. updatesize(c);
  140. if(c->x + c->w + 2 * BORDERPX > sw)
  141. c->x = sw - c->w - 2 * BORDERPX;
  142. if(c->x < sx)
  143. c->x = sx;
  144. if(c->y + c->h + 2 * BORDERPX > sh)
  145. c->y = sh - c->h - 2 * BORDERPX;
  146. if(c->h != sh && c->y < bh)
  147. c->y = bh;
  148. c->proto = getproto(c->win);
  149. XSelectInput(dpy, c->win,
  150. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  151. XGetTransientForHint(dpy, c->win, &trans);
  152. twa.override_redirect = 1;
  153. twa.background_pixmap = ParentRelative;
  154. twa.event_mask = ExposureMask | EnterWindowMask;
  155. c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  156. 0, DefaultDepth(dpy, screen), CopyFromParent,
  157. DefaultVisual(dpy, screen),
  158. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  159. grabbuttons(c, False);
  160. updatetitle(c);
  161. settags(c, getclient(trans));
  162. if(!c->isfloat)
  163. c->isfloat = trans || c->isfixed;
  164. resizetitle(c);
  165. if(clients)
  166. clients->prev = c;
  167. c->next = clients;
  168. c->snext = stack;
  169. stack = clients = c;
  170. ban(c);
  171. XMapWindow(dpy, c->win);
  172. XMapWindow(dpy, c->twin);
  173. if(isvisible(c))
  174. focus(c);
  175. arrange();
  176. }
  177. void
  178. resize(Client *c, Bool sizehints, Corner sticky) {
  179. int bottom = c->y + c->h;
  180. int right = c->x + c->w;
  181. XWindowChanges wc;
  182. if(sizehints) {
  183. if(c->incw)
  184. c->w -= (c->w - c->basew) % c->incw;
  185. if(c->inch)
  186. c->h -= (c->h - c->baseh) % c->inch;
  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. }
  196. if(sticky == TopRight || sticky == BotRight)
  197. c->x = right - c->w;
  198. if(sticky == BotLeft || sticky == BotRight)
  199. c->y = bottom - c->h;
  200. /* offscreen appearance fixes */
  201. if(c->x + c->w < sx)
  202. c->x = sx;
  203. if(c->y + c->h < bh)
  204. c->y = bh;
  205. if(c->x > sw)
  206. c->x = sw - c->w;
  207. if(c->y > sh)
  208. c->y = sh - c->h;
  209. resizetitle(c);
  210. wc.x = c->x;
  211. wc.y = c->y;
  212. wc.width = c->w;
  213. wc.height = c->h;
  214. if(c->w == sw && c->h == sh)
  215. wc.border_width = 0;
  216. else
  217. wc.border_width = BORDERPX;
  218. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  219. configure(c);
  220. XSync(dpy, False);
  221. }
  222. void
  223. resizetitle(Client *c) {
  224. c->tw = textw(c->name);
  225. if(c->tw > c->w)
  226. c->tw = c->w + 2 * BORDERPX;
  227. c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
  228. c->ty = c->y;
  229. if(isvisible(c))
  230. XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
  231. else
  232. XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
  233. }
  234. void
  235. updatesize(Client *c) {
  236. long msize;
  237. XSizeHints size;
  238. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  239. size.flags = PSize;
  240. c->flags = size.flags;
  241. if(c->flags & PBaseSize) {
  242. c->basew = size.base_width;
  243. c->baseh = size.base_height;
  244. }
  245. else
  246. c->basew = c->baseh = 0;
  247. if(c->flags & PResizeInc) {
  248. c->incw = size.width_inc;
  249. c->inch = size.height_inc;
  250. }
  251. else
  252. c->incw = c->inch = 0;
  253. if(c->flags & PMaxSize) {
  254. c->maxw = size.max_width;
  255. c->maxh = size.max_height;
  256. }
  257. else
  258. c->maxw = c->maxh = 0;
  259. if(c->flags & PMinSize) {
  260. c->minw = size.min_width;
  261. c->minh = size.min_height;
  262. }
  263. else
  264. c->minw = c->minh = 0;
  265. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
  266. c->maxw == c->minw && c->maxh == c->minh);
  267. }
  268. void
  269. updatetitle(Client *c) {
  270. char **list = NULL;
  271. int n;
  272. XTextProperty name;
  273. name.nitems = 0;
  274. c->name[0] = 0;
  275. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  276. if(!name.nitems)
  277. XGetWMName(dpy, c->win, &name);
  278. if(!name.nitems)
  279. return;
  280. if(name.encoding == XA_STRING)
  281. strncpy(c->name, (char *)name.value, sizeof c->name);
  282. else {
  283. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  284. && n > 0 && *list)
  285. {
  286. strncpy(c->name, *list, sizeof c->name);
  287. XFreeStringList(list);
  288. }
  289. }
  290. XFree(name.value);
  291. }
  292. void
  293. unmanage(Client *c) {
  294. Client *nc;
  295. /* The server grab construct avoids race conditions. */
  296. XGrabServer(dpy);
  297. XSetErrorHandler(xerrordummy);
  298. detach(c);
  299. detachstack(c);
  300. if(sel == c) {
  301. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  302. focus(nc);
  303. }
  304. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  305. XDestroyWindow(dpy, c->twin);
  306. free(c->tags);
  307. free(c);
  308. XSync(dpy, False);
  309. XSetErrorHandler(xerror);
  310. XUngrabServer(dpy);
  311. arrange();
  312. }