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.

299 lines
7.0 KiB

18 years ago
19 years ago
19 years ago
19 years ago
19 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-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 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. configure(Client *c) {
  56. XEvent synev;
  57. synev.type = ConfigureNotify;
  58. synev.xconfigure.display = dpy;
  59. synev.xconfigure.event = c->win;
  60. synev.xconfigure.window = c->win;
  61. synev.xconfigure.x = c->x;
  62. synev.xconfigure.y = c->y;
  63. synev.xconfigure.width = c->w;
  64. synev.xconfigure.height = c->h;
  65. synev.xconfigure.border_width = c->border;
  66. synev.xconfigure.above = None;
  67. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  68. }
  69. void
  70. focus(Client *c) {
  71. Client *old;
  72. if(!issel || (c && !isvisible(c)))
  73. return;
  74. if(!sel)
  75. sel = c;
  76. else if(sel != c) {
  77. old = sel;
  78. sel = c;
  79. if(old) {
  80. grabbuttons(old, False);
  81. drawclient(old);
  82. }
  83. }
  84. if(c) {
  85. detachstack(c);
  86. c->snext = stack;
  87. stack = c;
  88. grabbuttons(c, True);
  89. drawclient(c);
  90. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  91. }
  92. else
  93. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  94. }
  95. Client *
  96. getclient(Window w) {
  97. Client *c;
  98. for(c = clients; c; c = c->next)
  99. if(c->win == w)
  100. return c;
  101. return NULL;
  102. }
  103. void
  104. killclient(Arg *arg) {
  105. if(!sel)
  106. return;
  107. if(sel->proto & PROTODELWIN)
  108. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  109. else
  110. XKillClient(dpy, sel->win);
  111. }
  112. void
  113. manage(Window w, XWindowAttributes *wa) {
  114. Client *c;
  115. Window trans;
  116. c = emallocz(sizeof(Client));
  117. c->tags = emallocz(ntags * sizeof(Bool));
  118. c->win = w;
  119. c->border = 0;
  120. c->x = wa->x;
  121. c->y = wa->y;
  122. c->w = wa->width;
  123. c->h = wa->height;
  124. updatesizehints(c);
  125. if(c->x + c->w + 2 * BORDERPX > sw)
  126. c->x = sw - c->w - 2 * BORDERPX;
  127. if(c->x < sx)
  128. c->x = sx;
  129. if(c->y + c->h + 2 * BORDERPX > sh)
  130. c->y = sh - c->h - 2 * BORDERPX;
  131. if(c->h != sh && c->y < bh)
  132. c->y = bh;
  133. c->proto = getproto(c->win);
  134. XSelectInput(dpy, c->win,
  135. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  136. XGetTransientForHint(dpy, c->win, &trans);
  137. grabbuttons(c, False);
  138. updatetitle(c);
  139. settags(c, getclient(trans));
  140. if(!c->isfloat)
  141. c->isfloat = trans || c->isfixed;
  142. if(clients)
  143. clients->prev = c;
  144. c->next = clients;
  145. c->snext = stack;
  146. stack = clients = c;
  147. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  148. XMapWindow(dpy, c->win);
  149. if(isvisible(c))
  150. focus(c);
  151. arrange();
  152. }
  153. void
  154. resize(Client *c, Bool sizehints, Corner sticky) {
  155. int bottom = c->y + c->h;
  156. int right = c->x + c->w;
  157. XWindowChanges wc;
  158. if(sizehints) {
  159. if(c->incw)
  160. c->w -= (c->w - c->basew) % c->incw;
  161. if(c->inch)
  162. c->h -= (c->h - c->baseh) % c->inch;
  163. if(c->minw && c->w < c->minw)
  164. c->w = c->minw;
  165. if(c->minh && c->h < c->minh)
  166. c->h = c->minh;
  167. if(c->maxw && c->w > c->maxw)
  168. c->w = c->maxw;
  169. if(c->maxh && c->h > c->maxh)
  170. c->h = c->maxh;
  171. }
  172. if(sticky == TopRight || sticky == BotRight)
  173. c->x = right - c->w;
  174. if(sticky == BotLeft || sticky == BotRight)
  175. c->y = bottom - c->h;
  176. /* offscreen appearance fixes */
  177. if(c->x + c->w < sx)
  178. c->x = sx;
  179. if(c->y + c->h < bh)
  180. c->y = bh;
  181. if(c->x > sw)
  182. c->x = sw - c->w;
  183. if(c->y > sh)
  184. c->y = sh - c->h;
  185. wc.x = c->x;
  186. wc.y = c->y;
  187. wc.width = c->w;
  188. wc.height = c->h;
  189. if(c->w == sw && c->h == sh)
  190. wc.border_width = 0;
  191. else
  192. wc.border_width = BORDERPX;
  193. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  194. configure(c);
  195. XSync(dpy, False);
  196. }
  197. void
  198. updatesizehints(Client *c) {
  199. long msize;
  200. XSizeHints size;
  201. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  202. size.flags = PSize;
  203. c->flags = size.flags;
  204. if(c->flags & PBaseSize) {
  205. c->basew = size.base_width;
  206. c->baseh = size.base_height;
  207. }
  208. else
  209. c->basew = c->baseh = 0;
  210. if(c->flags & PResizeInc) {
  211. c->incw = size.width_inc;
  212. c->inch = size.height_inc;
  213. }
  214. else
  215. c->incw = c->inch = 0;
  216. if(c->flags & PMaxSize) {
  217. c->maxw = size.max_width;
  218. c->maxh = size.max_height;
  219. }
  220. else
  221. c->maxw = c->maxh = 0;
  222. if(c->flags & PMinSize) {
  223. c->minw = size.min_width;
  224. c->minh = size.min_height;
  225. }
  226. else
  227. c->minw = c->minh = 0;
  228. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
  229. c->maxw == c->minw && c->maxh == c->minh);
  230. }
  231. void
  232. updatetitle(Client *c) {
  233. char **list = NULL;
  234. int n;
  235. XTextProperty name;
  236. name.nitems = 0;
  237. c->name[0] = 0;
  238. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  239. if(!name.nitems)
  240. XGetWMName(dpy, c->win, &name);
  241. if(!name.nitems)
  242. return;
  243. if(name.encoding == XA_STRING)
  244. strncpy(c->name, (char *)name.value, sizeof c->name);
  245. else {
  246. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  247. && n > 0 && *list)
  248. {
  249. strncpy(c->name, *list, sizeof c->name);
  250. XFreeStringList(list);
  251. }
  252. }
  253. XFree(name.value);
  254. }
  255. void
  256. unmanage(Client *c) {
  257. Client *nc;
  258. /* The server grab construct avoids race conditions. */
  259. XGrabServer(dpy);
  260. XSetErrorHandler(xerrordummy);
  261. detach(c);
  262. detachstack(c);
  263. if(sel == c) {
  264. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  265. focus(nc);
  266. }
  267. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  268. free(c->tags);
  269. free(c);
  270. XSync(dpy, False);
  271. XSetErrorHandler(xerror);
  272. XUngrabServer(dpy);
  273. arrange();
  274. }