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.

413 lines
8.8 KiB

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 int
  51. xerrordummy(Display *dsply, XErrorEvent *ee) {
  52. return 0;
  53. }
  54. /* extern functions */
  55. void
  56. ban(Client *c) {
  57. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  58. XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
  59. }
  60. void
  61. configure(Client *c) {
  62. XEvent synev;
  63. synev.type = ConfigureNotify;
  64. synev.xconfigure.display = dpy;
  65. synev.xconfigure.event = c->win;
  66. synev.xconfigure.window = c->win;
  67. synev.xconfigure.x = c->x;
  68. synev.xconfigure.y = c->y;
  69. synev.xconfigure.width = c->w;
  70. synev.xconfigure.height = c->h;
  71. synev.xconfigure.border_width = c->border;
  72. synev.xconfigure.above = None;
  73. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  74. }
  75. void
  76. focus(Client *c) {
  77. Client *old;
  78. if(!issel || (c && !isvisible(c)))
  79. return;
  80. if(!sel)
  81. sel = c;
  82. else if(sel != c) {
  83. old = sel;
  84. sel = c;
  85. if(old) {
  86. grabbuttons(old, False);
  87. drawtitle(old);
  88. }
  89. }
  90. if(c) {
  91. detachstack(c);
  92. c->snext = stack;
  93. stack = c;
  94. grabbuttons(c, True);
  95. drawtitle(c);
  96. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  97. }
  98. else
  99. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  100. }
  101. Client *
  102. getclient(Window w) {
  103. Client *c;
  104. for(c = clients; c; c = c->next)
  105. if(c->win == w)
  106. return c;
  107. return NULL;
  108. }
  109. Client *
  110. getctitle(Window w) {
  111. Client *c;
  112. for(c = clients; c; c = c->next)
  113. if(c->twin == w)
  114. return c;
  115. return NULL;
  116. }
  117. void
  118. gravitate(Client *c, Bool invert) {
  119. int dx = 0, dy = 0;
  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->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. c->border = 0;
  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
  215. || (c->maxw && c->minw &&
  216. c->maxw == c->minw && c->maxh == c->minh);
  217. resizetitle(c);
  218. if(clients)
  219. clients->prev = c;
  220. c->next = clients;
  221. c->snext = stack;
  222. stack = clients = 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 < sx)
  255. c->x = sx;
  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 = BORDERPX;
  271. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  272. configure(c);
  273. XSync(dpy, False);
  274. }
  275. void
  276. resizetitle(Client *c) {
  277. c->tw = textw(c->name);
  278. if(c->tw > c->w)
  279. c->tw = c->w + 2 * BORDERPX;
  280. c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
  281. c->ty = c->y;
  282. if(isvisible(c))
  283. XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
  284. else
  285. XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
  286. }
  287. void
  288. updatesize(Client *c) {
  289. long msize;
  290. XSizeHints size;
  291. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  292. size.flags = PSize;
  293. c->flags = size.flags;
  294. if(c->flags & PBaseSize) {
  295. c->basew = size.base_width;
  296. c->baseh = size.base_height;
  297. }
  298. else
  299. c->basew = c->baseh = 0;
  300. if(c->flags & PResizeInc) {
  301. c->incw = size.width_inc;
  302. c->inch = size.height_inc;
  303. }
  304. else
  305. c->incw = c->inch = 0;
  306. if(c->flags & PMaxSize) {
  307. c->maxw = size.max_width;
  308. c->maxh = size.max_height;
  309. }
  310. else
  311. c->maxw = c->maxh = 0;
  312. if(c->flags & PMinSize) {
  313. c->minw = size.min_width;
  314. c->minh = size.min_height;
  315. }
  316. else
  317. c->minw = c->minh = 0;
  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(NULL);
  367. }