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
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(sizehints) {
  164. if(c->incw)
  165. c->w -= (c->w - c->basew) % c->incw;
  166. if(c->inch)
  167. c->h -= (c->h - c->baseh) % c->inch;
  168. if(c->minw && c->w < c->minw)
  169. c->w = c->minw;
  170. if(c->minh && c->h < c->minh)
  171. c->h = c->minh;
  172. if(c->maxw && c->w > c->maxw)
  173. c->w = c->maxw;
  174. if(c->maxh && c->h > c->maxh)
  175. c->h = c->maxh;
  176. }
  177. if(c->w == sw && c->h == sh)
  178. c->border = 0;
  179. else
  180. c->border = BORDERPX;
  181. /* offscreen appearance fixes */
  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. if(c->x + c->w + 2 * c->border < sx)
  187. c->x = sx;
  188. if(c->y + c->h + 2 * c->border < sy)
  189. c->y = sy;
  190. wc.x = c->x;
  191. wc.y = c->y;
  192. wc.width = c->w;
  193. wc.height = c->h;
  194. wc.border_width = c->border;
  195. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  196. configure(c);
  197. XSync(dpy, False);
  198. }
  199. void
  200. updatesizehints(Client *c) {
  201. long msize;
  202. XSizeHints size;
  203. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  204. size.flags = PSize;
  205. c->flags = size.flags;
  206. if(c->flags & PBaseSize) {
  207. c->basew = size.base_width;
  208. c->baseh = size.base_height;
  209. }
  210. else
  211. c->basew = c->baseh = 0;
  212. if(c->flags & PResizeInc) {
  213. c->incw = size.width_inc;
  214. c->inch = size.height_inc;
  215. }
  216. else
  217. c->incw = c->inch = 0;
  218. if(c->flags & PMaxSize) {
  219. c->maxw = size.max_width;
  220. c->maxh = size.max_height;
  221. }
  222. else
  223. c->maxw = c->maxh = 0;
  224. if(c->flags & PMinSize) {
  225. c->minw = size.min_width;
  226. c->minh = size.min_height;
  227. }
  228. else
  229. c->minw = c->minh = 0;
  230. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
  231. c->maxw == c->minw && c->maxh == c->minh);
  232. }
  233. void
  234. updatetitle(Client *c) {
  235. char **list = NULL;
  236. int n;
  237. XTextProperty name;
  238. name.nitems = 0;
  239. c->name[0] = 0;
  240. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  241. if(!name.nitems)
  242. XGetWMName(dpy, c->win, &name);
  243. if(!name.nitems)
  244. return;
  245. if(name.encoding == XA_STRING)
  246. strncpy(c->name, (char *)name.value, sizeof c->name);
  247. else {
  248. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  249. && n > 0 && *list)
  250. {
  251. strncpy(c->name, *list, sizeof c->name);
  252. XFreeStringList(list);
  253. }
  254. }
  255. XFree(name.value);
  256. }
  257. void
  258. unmanage(Client *c) {
  259. Client *nc;
  260. /* The server grab construct avoids race conditions. */
  261. XGrabServer(dpy);
  262. XSetErrorHandler(xerrordummy);
  263. detach(c);
  264. detachstack(c);
  265. if(sel == c) {
  266. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  267. focus(nc);
  268. }
  269. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  270. free(c->tags);
  271. free(c);
  272. XSync(dpy, False);
  273. XSetErrorHandler(xerror);
  274. XUngrabServer(dpy);
  275. arrange();
  276. }