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.

397 lines
8.8 KiB

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
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
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  1. /* (C)opyright MMVI 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. ban(Client *c) {
  56. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  57. XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
  58. }
  59. void
  60. configure(Client *c) {
  61. XEvent synev;
  62. synev.type = ConfigureNotify;
  63. synev.xconfigure.display = dpy;
  64. synev.xconfigure.event = c->win;
  65. synev.xconfigure.window = c->win;
  66. synev.xconfigure.x = c->x;
  67. synev.xconfigure.y = c->y;
  68. synev.xconfigure.width = c->w;
  69. synev.xconfigure.height = c->h;
  70. synev.xconfigure.border_width = c->border;
  71. synev.xconfigure.above = None;
  72. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  73. }
  74. void
  75. focus(Client *c) {
  76. Client *old;
  77. if(!issel || (c && !isvisible(c)))
  78. return;
  79. if(!sel)
  80. sel = c;
  81. else if(sel != c) {
  82. old = sel;
  83. sel = c;
  84. if(old) {
  85. grabbuttons(old, False);
  86. drawtitle(old);
  87. }
  88. }
  89. if(c) {
  90. detachstack(c);
  91. c->snext = stack;
  92. stack = c;
  93. grabbuttons(c, True);
  94. drawtitle(c);
  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. Client *
  109. getctitle(Window w) {
  110. Client *c;
  111. for(c = clients; c; c = c->next)
  112. if(c->twin == w)
  113. return c;
  114. return NULL;
  115. }
  116. void
  117. gravitate(Client *c, Bool invert) {
  118. int dx = 0, dy = 0;
  119. switch(c->grav) {
  120. default:
  121. break;
  122. case StaticGravity:
  123. case NorthWestGravity:
  124. case NorthGravity:
  125. case NorthEastGravity:
  126. dy = c->border;
  127. break;
  128. case EastGravity:
  129. case CenterGravity:
  130. case WestGravity:
  131. dy = -(c->h / 2) + c->border;
  132. break;
  133. case SouthEastGravity:
  134. case SouthGravity:
  135. case SouthWestGravity:
  136. dy = -(c->h);
  137. break;
  138. }
  139. switch (c->grav) {
  140. default:
  141. break;
  142. case StaticGravity:
  143. case NorthWestGravity:
  144. case WestGravity:
  145. case SouthWestGravity:
  146. dx = c->border;
  147. break;
  148. case NorthGravity:
  149. case CenterGravity:
  150. case SouthGravity:
  151. dx = -(c->w / 2) + c->border;
  152. break;
  153. case NorthEastGravity:
  154. case EastGravity:
  155. case SouthEastGravity:
  156. dx = -(c->w + c->border);
  157. break;
  158. }
  159. if(invert) {
  160. dx = -dx;
  161. dy = -dy;
  162. }
  163. c->x += dx;
  164. c->y += dy;
  165. }
  166. void
  167. killclient(Arg *arg) {
  168. if(!sel)
  169. return;
  170. if(sel->proto & PROTODELWIN)
  171. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  172. else
  173. XKillClient(dpy, sel->win);
  174. }
  175. void
  176. manage(Window w, XWindowAttributes *wa) {
  177. Client *c;
  178. Window trans;
  179. XSetWindowAttributes twa;
  180. c = emallocz(sizeof(Client));
  181. c->tags = emallocz(ntags * sizeof(Bool));
  182. c->win = w;
  183. c->x = c->tx = wa->x;
  184. c->y = c->ty = wa->y;
  185. c->w = c->tw = wa->width;
  186. c->h = wa->height;
  187. c->th = bh;
  188. c->border = 0;
  189. updatesize(c);
  190. if(c->x + c->w + 2 * BORDERPX > sw)
  191. c->x = sw - c->w - 2 * BORDERPX;
  192. if(c->x < sx)
  193. c->x = sx;
  194. if(c->y + c->h + 2 * BORDERPX > sh)
  195. c->y = sh - c->h - 2 * BORDERPX;
  196. if(c->h != sh && c->y < bh)
  197. c->y = bh;
  198. c->proto = getproto(c->win);
  199. XSelectInput(dpy, c->win,
  200. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  201. XGetTransientForHint(dpy, c->win, &trans);
  202. twa.override_redirect = 1;
  203. twa.background_pixmap = ParentRelative;
  204. twa.event_mask = ExposureMask | EnterWindowMask;
  205. c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  206. 0, DefaultDepth(dpy, screen), CopyFromParent,
  207. DefaultVisual(dpy, screen),
  208. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  209. grabbuttons(c, False);
  210. updatetitle(c);
  211. settags(c, getclient(trans));
  212. if(!c->isfloat)
  213. c->isfloat = trans
  214. || (c->maxw && c->minw &&
  215. c->maxw == c->minw && c->maxh == c->minh);
  216. resizetitle(c);
  217. if(clients)
  218. clients->prev = c;
  219. c->next = clients;
  220. c->snext = stack;
  221. stack = clients = c;
  222. ban(c);
  223. XMapWindow(dpy, c->win);
  224. XMapWindow(dpy, c->twin);
  225. if(isvisible(c))
  226. focus(c);
  227. arrange();
  228. }
  229. void
  230. resize(Client *c, Bool sizehints, Corner sticky) {
  231. int bottom = c->y + c->h;
  232. int right = c->x + c->w;
  233. XWindowChanges wc;
  234. if(sizehints) {
  235. if(c->incw)
  236. c->w -= (c->w - c->basew) % c->incw;
  237. if(c->inch)
  238. c->h -= (c->h - c->baseh) % c->inch;
  239. if(c->minw && c->w < c->minw)
  240. c->w = c->minw;
  241. if(c->minh && c->h < c->minh)
  242. c->h = c->minh;
  243. if(c->maxw && c->w > c->maxw)
  244. c->w = c->maxw;
  245. if(c->maxh && c->h > c->maxh)
  246. c->h = c->maxh;
  247. }
  248. if(sticky == TopRight || sticky == BotRight)
  249. c->x = right - c->w;
  250. if(sticky == BotLeft || sticky == BotRight)
  251. c->y = bottom - c->h;
  252. /* offscreen appearance fixes */
  253. if(c->x + c->w < sx)
  254. c->x = sx;
  255. if(c->y + c->h < bh)
  256. c->y = bh;
  257. if(c->x > sw)
  258. c->x = sw - c->w;
  259. if(c->y > sh)
  260. c->y = sh - c->h;
  261. resizetitle(c);
  262. wc.x = c->x;
  263. wc.y = c->y;
  264. wc.width = c->w;
  265. wc.height = c->h;
  266. if(c->w == sw && c->h == sh)
  267. wc.border_width = 0;
  268. else
  269. wc.border_width = BORDERPX;
  270. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  271. configure(c);
  272. XSync(dpy, False);
  273. }
  274. void
  275. resizetitle(Client *c) {
  276. c->tw = textw(c->name);
  277. if(c->tw > c->w)
  278. c->tw = c->w + 2 * BORDERPX;
  279. c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
  280. c->ty = c->y;
  281. if(isvisible(c))
  282. XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
  283. else
  284. XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
  285. }
  286. void
  287. updatesize(Client *c) {
  288. long msize;
  289. XSizeHints size;
  290. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  291. size.flags = PSize;
  292. c->flags = size.flags;
  293. if(c->flags & PBaseSize) {
  294. c->basew = size.base_width;
  295. c->baseh = size.base_height;
  296. }
  297. else
  298. c->basew = c->baseh = 0;
  299. if(c->flags & PResizeInc) {
  300. c->incw = size.width_inc;
  301. c->inch = size.height_inc;
  302. }
  303. else
  304. c->incw = c->inch = 0;
  305. if(c->flags & PMaxSize) {
  306. c->maxw = size.max_width;
  307. c->maxh = size.max_height;
  308. }
  309. else
  310. c->maxw = c->maxh = 0;
  311. if(c->flags & PMinSize) {
  312. c->minw = size.min_width;
  313. c->minh = size.min_height;
  314. }
  315. else
  316. c->minw = c->minh = 0;
  317. if(c->flags & PWinGravity)
  318. c->grav = size.win_gravity;
  319. else
  320. c->grav = NorthWestGravity;
  321. }
  322. void
  323. updatetitle(Client *c) {
  324. char **list = NULL;
  325. int n;
  326. XTextProperty name;
  327. name.nitems = 0;
  328. c->name[0] = 0;
  329. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  330. if(!name.nitems)
  331. XGetWMName(dpy, c->win, &name);
  332. if(!name.nitems)
  333. return;
  334. if(name.encoding == XA_STRING)
  335. strncpy(c->name, (char *)name.value, sizeof(c->name));
  336. else {
  337. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  338. && n > 0 && *list)
  339. {
  340. strncpy(c->name, *list, sizeof(c->name));
  341. XFreeStringList(list);
  342. }
  343. }
  344. XFree(name.value);
  345. }
  346. void
  347. unmanage(Client *c) {
  348. Client *nc;
  349. /* The server grab construct avoids race conditions. */
  350. XGrabServer(dpy);
  351. XSetErrorHandler(xerrordummy);
  352. detach(c);
  353. detachstack(c);
  354. if(sel == c) {
  355. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  356. focus(nc);
  357. }
  358. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  359. XDestroyWindow(dpy, c->twin);
  360. free(c->tags);
  361. free(c);
  362. XSync(dpy, False);
  363. XSetErrorHandler(xerror);
  364. XUngrabServer(dpy);
  365. arrange();
  366. }