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.

399 lines
8.4 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->rx = sx;
  187. c->ry = bh;
  188. c->rw = sw;
  189. c->rh = sh - bh;
  190. c->border = 0;
  191. updatesize(c);
  192. if(c->x + c->w + 2 > sw)
  193. c->x = sw - c->w - 2;
  194. if(c->x < 0)
  195. c->x = 0;
  196. if(c->y + c->h + 2 > sh)
  197. c->y = sh - c->h - 2;
  198. if(c->h != sh && c->y < bh)
  199. c->y = bh;
  200. c->proto = getproto(c->win);
  201. XSelectInput(dpy, c->win,
  202. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  203. XGetTransientForHint(dpy, c->win, &trans);
  204. twa.override_redirect = 1;
  205. twa.background_pixmap = ParentRelative;
  206. twa.event_mask = ExposureMask | EnterWindowMask;
  207. c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  208. 0, DefaultDepth(dpy, screen), CopyFromParent,
  209. DefaultVisual(dpy, screen),
  210. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  211. grabbuttons(c, False);
  212. settags(c, getclient(trans));
  213. if(!c->isfloat)
  214. c->isfloat = trans
  215. || (c->maxw && c->minw &&
  216. c->maxw == c->minw && c->maxh == c->minh);
  217. if(clients)
  218. clients->prev = c;
  219. c->next = clients;
  220. c->snext = stack;
  221. stack = clients = c;
  222. updatetitle(c);
  223. ban(c);
  224. XMapWindow(dpy, c->win);
  225. XMapWindow(dpy, c->twin);
  226. if(isvisible(c))
  227. focus(c);
  228. arrange(NULL);
  229. }
  230. void
  231. resize(Client *c, Bool sizehints, Corner sticky) {
  232. int bottom = c->y + c->h;
  233. int right = c->x + c->w;
  234. XWindowChanges wc;
  235. if(sizehints) {
  236. if(c->incw)
  237. c->w -= (c->w - c->basew) % c->incw;
  238. if(c->inch)
  239. c->h -= (c->h - c->baseh) % c->inch;
  240. if(c->minw && c->w < c->minw)
  241. c->w = c->minw;
  242. if(c->minh && c->h < c->minh)
  243. c->h = c->minh;
  244. if(c->maxw && c->w > c->maxw)
  245. c->w = c->maxw;
  246. if(c->maxh && c->h > c->maxh)
  247. c->h = c->maxh;
  248. }
  249. if(sticky == TopRight || sticky == BotRight)
  250. c->x = right - c->w;
  251. if(sticky == BotLeft || sticky == BotRight)
  252. c->y = bottom - c->h;
  253. /* offscreen appearance fixes */
  254. if(c->x + c->w < 0)
  255. c->x = 0;
  256. if(c->y + c->h < bh)
  257. c->y = bh;
  258. if(c->x > sw)
  259. c->x = sw - c->w;
  260. if(c->y > sh)
  261. c->y = sh - c->h;
  262. resizetitle(c);
  263. wc.x = c->x;
  264. wc.y = c->y;
  265. wc.width = c->w;
  266. wc.height = c->h;
  267. if(c->w == sw && c->h == sh)
  268. wc.border_width = 0;
  269. else
  270. wc.border_width = 1;
  271. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  272. XSync(dpy, False);
  273. }
  274. void
  275. updatesize(Client *c) {
  276. long msize;
  277. XSizeHints size;
  278. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  279. size.flags = PSize;
  280. c->flags = size.flags;
  281. if(c->flags & PBaseSize) {
  282. c->basew = size.base_width;
  283. c->baseh = size.base_height;
  284. }
  285. else
  286. c->basew = c->baseh = 0;
  287. if(c->flags & PResizeInc) {
  288. c->incw = size.width_inc;
  289. c->inch = size.height_inc;
  290. }
  291. else
  292. c->incw = c->inch = 0;
  293. if(c->flags & PMaxSize) {
  294. c->maxw = size.max_width;
  295. c->maxh = size.max_height;
  296. }
  297. else
  298. c->maxw = c->maxh = 0;
  299. if(c->flags & PMinSize) {
  300. c->minw = size.min_width;
  301. c->minh = size.min_height;
  302. }
  303. else
  304. c->minw = c->minh = 0;
  305. if(c->flags & PWinGravity)
  306. c->grav = size.win_gravity;
  307. else
  308. c->grav = NorthWestGravity;
  309. }
  310. void
  311. updatetitle(Client *c) {
  312. char **list = NULL;
  313. int n;
  314. XTextProperty name;
  315. name.nitems = 0;
  316. c->name[0] = 0;
  317. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  318. if(!name.nitems)
  319. XGetWMName(dpy, c->win, &name);
  320. if(!name.nitems)
  321. return;
  322. if(name.encoding == XA_STRING)
  323. strncpy(c->name, (char *)name.value, sizeof(c->name));
  324. else {
  325. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  326. && n > 0 && *list)
  327. {
  328. strncpy(c->name, *list, sizeof(c->name));
  329. XFreeStringList(list);
  330. }
  331. }
  332. XFree(name.value);
  333. resizetitle(c);
  334. }
  335. void
  336. unmanage(Client *c) {
  337. Client *nc;
  338. /* The server grab construct avoids race conditions. */
  339. XGrabServer(dpy);
  340. XSetErrorHandler(xerrordummy);
  341. detach(c);
  342. detachstack(c);
  343. if(sel == c) {
  344. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  345. focus(nc);
  346. }
  347. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  348. XDestroyWindow(dpy, c->twin);
  349. free(c->tags);
  350. free(c);
  351. XSync(dpy, False);
  352. XSetErrorHandler(xerror);
  353. XUngrabServer(dpy);
  354. arrange(NULL);
  355. }