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
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(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. if(issel) {
  80. detachstack(c);
  81. c->snext = stack;
  82. stack = c;
  83. grabbuttons(c, True);
  84. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  85. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  86. }
  87. else
  88. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  89. }
  90. else if(issel)
  91. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  92. sel = c;
  93. drawstatus();
  94. }
  95. Client *
  96. getclient(Window w) {
  97. Client *c;
  98. for(c = clients; c; c = c->next)
  99. if(c->win == w)
  100. return c;
  101. return NULL;
  102. }
  103. void
  104. killclient(Arg *arg) {
  105. if(!sel)
  106. return;
  107. if(sel->proto & PROTODELWIN)
  108. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  109. else
  110. XKillClient(dpy, sel->win);
  111. }
  112. void
  113. manage(Window w, XWindowAttributes *wa) {
  114. Client *c;
  115. Window trans;
  116. c = emallocz(sizeof(Client));
  117. c->tags = emallocz(ntags * sizeof(Bool));
  118. c->win = w;
  119. c->x = wa->x;
  120. c->y = wa->y;
  121. c->w = wa->width;
  122. c->h = wa->height;
  123. if(c->w == sw && c->h == sh) {
  124. c->border = 0;
  125. c->x = sx;
  126. c->y = sy;
  127. }
  128. else {
  129. c->border = BORDERPX;
  130. if(c->x < wax)
  131. c->x = wax;
  132. if(c->y < way)
  133. c->y = way;
  134. if(c->x + c->w + 2 * c->border > wax + waw)
  135. c->x = wax + waw - c->w - 2 * c->border;
  136. if(c->y + c->h + 2 * c->border > way + wah)
  137. c->y = way + wah - c->h - 2 * c->border;
  138. }
  139. updatesizehints(c);
  140. c->proto = getproto(c->win);
  141. XSelectInput(dpy, c->win,
  142. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  143. XGetTransientForHint(dpy, c->win, &trans);
  144. grabbuttons(c, False);
  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 + c->w + 2 * c->border < sx)
  183. c->x = sx;
  184. if(c->y + c->h + 2 * c->border < sy)
  185. c->y = sy;
  186. if(c->x > sw)
  187. c->x = sw - c->w - 2 * c->border;
  188. if(c->y > sh)
  189. c->y = sh - c->h - 2 * c->border;
  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. }