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.

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