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.

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