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.

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