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.9 KiB

18 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
18 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
18 years ago
19 years ago
18 years ago
18 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->border = 0;
  184. c->x = c->tx = wa->x;
  185. c->y = c->ty = wa->y;
  186. c->w = c->tw = wa->width;
  187. c->h = wa->height;
  188. c->th = bh;
  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 || c->isfixed;
  214. resizetitle(c);
  215. if(clients)
  216. clients->prev = c;
  217. c->next = clients;
  218. c->snext = stack;
  219. stack = clients = c;
  220. ban(c);
  221. XMapWindow(dpy, c->win);
  222. XMapWindow(dpy, c->twin);
  223. if(isvisible(c))
  224. focus(c);
  225. arrange();
  226. }
  227. void
  228. resize(Client *c, Bool sizehints, Corner sticky) {
  229. int bottom = c->y + c->h;
  230. int right = c->x + c->w;
  231. XWindowChanges wc;
  232. if(sizehints) {
  233. if(c->incw)
  234. c->w -= (c->w - c->basew) % c->incw;
  235. if(c->inch)
  236. c->h -= (c->h - c->baseh) % c->inch;
  237. if(c->minw && c->w < c->minw)
  238. c->w = c->minw;
  239. if(c->minh && c->h < c->minh)
  240. c->h = c->minh;
  241. if(c->maxw && c->w > c->maxw)
  242. c->w = c->maxw;
  243. if(c->maxh && c->h > c->maxh)
  244. c->h = c->maxh;
  245. }
  246. if(sticky == TopRight || sticky == BotRight)
  247. c->x = right - c->w;
  248. if(sticky == BotLeft || sticky == BotRight)
  249. c->y = bottom - c->h;
  250. /* offscreen appearance fixes */
  251. if(c->x + c->w < sx)
  252. c->x = sx;
  253. if(c->y + c->h < bh)
  254. c->y = bh;
  255. if(c->x > sw)
  256. c->x = sw - c->w;
  257. if(c->y > sh)
  258. c->y = sh - c->h;
  259. resizetitle(c);
  260. wc.x = c->x;
  261. wc.y = c->y;
  262. wc.width = c->w;
  263. wc.height = c->h;
  264. if(c->w == sw && c->h == sh)
  265. wc.border_width = 0;
  266. else
  267. wc.border_width = BORDERPX;
  268. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  269. configure(c);
  270. XSync(dpy, False);
  271. }
  272. void
  273. resizetitle(Client *c) {
  274. c->tw = textw(c->name);
  275. if(c->tw > c->w)
  276. c->tw = c->w + 2 * BORDERPX;
  277. c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
  278. c->ty = c->y;
  279. if(isvisible(c))
  280. XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
  281. else
  282. XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
  283. }
  284. void
  285. updatesize(Client *c) {
  286. long msize;
  287. XSizeHints size;
  288. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  289. size.flags = PSize;
  290. c->flags = size.flags;
  291. if(c->flags & PBaseSize) {
  292. c->basew = size.base_width;
  293. c->baseh = size.base_height;
  294. }
  295. else
  296. c->basew = c->baseh = 0;
  297. if(c->flags & PResizeInc) {
  298. c->incw = size.width_inc;
  299. c->inch = size.height_inc;
  300. }
  301. else
  302. c->incw = c->inch = 0;
  303. if(c->flags & PMaxSize) {
  304. c->maxw = size.max_width;
  305. c->maxh = size.max_height;
  306. }
  307. else
  308. c->maxw = c->maxh = 0;
  309. if(c->flags & PMinSize) {
  310. c->minw = size.min_width;
  311. c->minh = size.min_height;
  312. }
  313. else
  314. c->minw = c->minh = 0;
  315. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
  316. c->maxw == c->minw && c->maxh == c->minh);
  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. }