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.

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