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