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.

395 lines
8.3 KiB

18 years ago
18 years ago
18 years ago
18 years ago
19 years ago
19 years ago
19 years ago
18 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
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
18 years ago
19 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. /* static functions */
  11. static void
  12. detachstack(Client *c) {
  13. Client **tc;
  14. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  15. *tc = c->snext;
  16. }
  17. static void
  18. grabbuttons(Client *c, Bool focused) {
  19. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  20. if(focused) {
  21. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  22. GrabModeAsync, GrabModeSync, None, None);
  23. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  24. GrabModeAsync, GrabModeSync, None, None);
  25. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  26. GrabModeAsync, GrabModeSync, None, None);
  27. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  28. GrabModeAsync, GrabModeSync, None, None);
  29. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  30. GrabModeAsync, GrabModeSync, None, None);
  31. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  32. GrabModeAsync, GrabModeSync, None, None);
  33. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  34. GrabModeAsync, GrabModeSync, None, None);
  35. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  36. GrabModeAsync, GrabModeSync, None, None);
  37. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  38. GrabModeAsync, GrabModeSync, None, None);
  39. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  40. GrabModeAsync, GrabModeSync, None, None);
  41. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  42. GrabModeAsync, GrabModeSync, None, None);
  43. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  44. GrabModeAsync, GrabModeSync, None, None);
  45. }
  46. else
  47. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  48. GrabModeAsync, GrabModeSync, None, None);
  49. }
  50. static void
  51. resizetitle(Client *c) {
  52. c->tw = textw(c->name);
  53. if(c->tw > c->w)
  54. c->tw = c->w + 2;
  55. c->tx = c->x + c->w - c->tw + 2;
  56. c->ty = c->y;
  57. if(isvisible(c))
  58. XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
  59. else
  60. XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
  61. }
  62. static int
  63. xerrordummy(Display *dsply, XErrorEvent *ee) {
  64. return 0;
  65. }
  66. /* extern functions */
  67. void
  68. ban(Client *c) {
  69. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  70. XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
  71. }
  72. void
  73. focus(Client *c) {
  74. Client *old;
  75. if(!issel || (c && !isvisible(c)))
  76. return;
  77. if(!sel)
  78. sel = c;
  79. else if(sel != c) {
  80. old = sel;
  81. sel = c;
  82. if(old) {
  83. grabbuttons(old, False);
  84. drawtitle(old);
  85. }
  86. }
  87. if(c) {
  88. detachstack(c);
  89. c->snext = stack;
  90. stack = c;
  91. grabbuttons(c, True);
  92. drawtitle(c);
  93. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  94. }
  95. else
  96. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  97. }
  98. Client *
  99. getclient(Window w) {
  100. Client *c;
  101. for(c = clients; c; c = c->next)
  102. if(c->win == w)
  103. return c;
  104. return NULL;
  105. }
  106. Client *
  107. getctitle(Window w) {
  108. Client *c;
  109. for(c = clients; c; c = c->next)
  110. if(c->twin == w)
  111. return c;
  112. return NULL;
  113. }
  114. void
  115. gravitate(Client *c, Bool invert) {
  116. int dx = 0, dy = 0;
  117. switch(c->grav) {
  118. default:
  119. break;
  120. case StaticGravity:
  121. case NorthWestGravity:
  122. case NorthGravity:
  123. case NorthEastGravity:
  124. dy = c->border;
  125. break;
  126. case EastGravity:
  127. case CenterGravity:
  128. case WestGravity:
  129. dy = -(c->h / 2) + c->border;
  130. break;
  131. case SouthEastGravity:
  132. case SouthGravity:
  133. case SouthWestGravity:
  134. dy = -(c->h);
  135. break;
  136. }
  137. switch (c->grav) {
  138. default:
  139. break;
  140. case StaticGravity:
  141. case NorthWestGravity:
  142. case WestGravity:
  143. case SouthWestGravity:
  144. dx = c->border;
  145. break;
  146. case NorthGravity:
  147. case CenterGravity:
  148. case SouthGravity:
  149. dx = -(c->w / 2) + c->border;
  150. break;
  151. case NorthEastGravity:
  152. case EastGravity:
  153. case SouthEastGravity:
  154. dx = -(c->w + c->border);
  155. break;
  156. }
  157. if(invert) {
  158. dx = -dx;
  159. dy = -dy;
  160. }
  161. c->x += dx;
  162. c->y += dy;
  163. }
  164. void
  165. killclient(Arg *arg) {
  166. if(!sel)
  167. return;
  168. if(sel->proto & PROTODELWIN)
  169. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  170. else
  171. XKillClient(dpy, sel->win);
  172. }
  173. void
  174. manage(Window w, XWindowAttributes *wa) {
  175. Client *c;
  176. Window trans;
  177. XSetWindowAttributes twa;
  178. c = emallocz(sizeof(Client));
  179. c->tags = emallocz(ntags * sizeof(Bool));
  180. c->win = w;
  181. c->x = c->tx = wa->x;
  182. c->y = c->ty = wa->y;
  183. c->w = c->tw = wa->width;
  184. c->h = wa->height;
  185. c->th = bh;
  186. c->border = 0;
  187. updatesize(c);
  188. if(c->x + c->w + 2 > sw)
  189. c->x = sw - c->w - 2;
  190. if(c->x < 0)
  191. c->x = 0;
  192. if(c->y + c->h + 2 > sh)
  193. c->y = sh - c->h - 2;
  194. if(c->h != sh && c->y < bh)
  195. c->y = bh;
  196. c->proto = getproto(c->win);
  197. XSelectInput(dpy, c->win,
  198. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  199. XGetTransientForHint(dpy, c->win, &trans);
  200. twa.override_redirect = 1;
  201. twa.background_pixmap = ParentRelative;
  202. twa.event_mask = ExposureMask | EnterWindowMask;
  203. c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  204. 0, DefaultDepth(dpy, screen), CopyFromParent,
  205. DefaultVisual(dpy, screen),
  206. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  207. grabbuttons(c, False);
  208. settags(c, getclient(trans));
  209. if(!c->isfloat)
  210. c->isfloat = trans
  211. || (c->maxw && c->minw &&
  212. c->maxw == c->minw && c->maxh == c->minh);
  213. if(clients)
  214. clients->prev = c;
  215. c->next = clients;
  216. c->snext = stack;
  217. stack = clients = c;
  218. updatetitle(c);
  219. ban(c);
  220. XMapWindow(dpy, c->win);
  221. XMapWindow(dpy, c->twin);
  222. if(isvisible(c))
  223. focus(c);
  224. arrange(NULL);
  225. }
  226. void
  227. resize(Client *c, Bool sizehints, Corner sticky) {
  228. int bottom = c->y + c->h;
  229. int right = c->x + c->w;
  230. XWindowChanges wc;
  231. if(sizehints) {
  232. if(c->incw)
  233. c->w -= (c->w - c->basew) % c->incw;
  234. if(c->inch)
  235. c->h -= (c->h - c->baseh) % c->inch;
  236. if(c->minw && c->w < c->minw)
  237. c->w = c->minw;
  238. if(c->minh && c->h < c->minh)
  239. c->h = c->minh;
  240. if(c->maxw && c->w > c->maxw)
  241. c->w = c->maxw;
  242. if(c->maxh && c->h > c->maxh)
  243. c->h = c->maxh;
  244. }
  245. if(sticky == TopRight || sticky == BotRight)
  246. c->x = right - c->w;
  247. if(sticky == BotLeft || sticky == BotRight)
  248. c->y = bottom - c->h;
  249. /* offscreen appearance fixes */
  250. if(c->x + c->w < 0)
  251. c->x = 0;
  252. if(c->y + c->h < bh)
  253. c->y = bh;
  254. if(c->x > sw)
  255. c->x = sw - c->w;
  256. if(c->y > sh)
  257. c->y = sh - c->h;
  258. resizetitle(c);
  259. wc.x = c->x;
  260. wc.y = c->y;
  261. wc.width = c->w;
  262. wc.height = c->h;
  263. if(c->w == sw && c->h == sh)
  264. wc.border_width = 0;
  265. else
  266. wc.border_width = 1;
  267. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  268. XSync(dpy, False);
  269. }
  270. void
  271. updatesize(Client *c) {
  272. long msize;
  273. XSizeHints size;
  274. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  275. size.flags = PSize;
  276. c->flags = size.flags;
  277. if(c->flags & PBaseSize) {
  278. c->basew = size.base_width;
  279. c->baseh = size.base_height;
  280. }
  281. else
  282. c->basew = c->baseh = 0;
  283. if(c->flags & PResizeInc) {
  284. c->incw = size.width_inc;
  285. c->inch = size.height_inc;
  286. }
  287. else
  288. c->incw = c->inch = 0;
  289. if(c->flags & PMaxSize) {
  290. c->maxw = size.max_width;
  291. c->maxh = size.max_height;
  292. }
  293. else
  294. c->maxw = c->maxh = 0;
  295. if(c->flags & PMinSize) {
  296. c->minw = size.min_width;
  297. c->minh = size.min_height;
  298. }
  299. else
  300. c->minw = c->minh = 0;
  301. if(c->flags & PWinGravity)
  302. c->grav = size.win_gravity;
  303. else
  304. c->grav = NorthWestGravity;
  305. }
  306. void
  307. updatetitle(Client *c) {
  308. char **list = NULL;
  309. int n;
  310. XTextProperty name;
  311. name.nitems = 0;
  312. c->name[0] = 0;
  313. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  314. if(!name.nitems)
  315. XGetWMName(dpy, c->win, &name);
  316. if(!name.nitems)
  317. return;
  318. if(name.encoding == XA_STRING)
  319. strncpy(c->name, (char *)name.value, sizeof(c->name));
  320. else {
  321. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  322. && n > 0 && *list)
  323. {
  324. strncpy(c->name, *list, sizeof(c->name));
  325. XFreeStringList(list);
  326. }
  327. }
  328. XFree(name.value);
  329. resizetitle(c);
  330. }
  331. void
  332. unmanage(Client *c) {
  333. Client *nc;
  334. /* The server grab construct avoids race conditions. */
  335. XGrabServer(dpy);
  336. XSetErrorHandler(xerrordummy);
  337. detach(c);
  338. detachstack(c);
  339. if(sel == c) {
  340. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  341. focus(nc);
  342. }
  343. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  344. XDestroyWindow(dpy, c->twin);
  345. free(c->tags);
  346. free(c);
  347. XSync(dpy, False);
  348. XSetErrorHandler(xerror);
  349. XUngrabServer(dpy);
  350. arrange(NULL);
  351. }