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.

302 lines
7.1 KiB

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