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
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. configure(Client *c) {
  74. XEvent synev;
  75. synev.type = ConfigureNotify;
  76. synev.xconfigure.display = dpy;
  77. synev.xconfigure.event = c->win;
  78. synev.xconfigure.window = c->win;
  79. synev.xconfigure.x = c->x;
  80. synev.xconfigure.y = c->y;
  81. synev.xconfigure.width = c->w;
  82. synev.xconfigure.height = c->h;
  83. synev.xconfigure.border_width = c->border;
  84. synev.xconfigure.above = None;
  85. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  86. }
  87. void
  88. focus(Client *c) {
  89. Client *old;
  90. if(!issel || (c && !isvisible(c)))
  91. return;
  92. if(!sel)
  93. sel = c;
  94. else if(sel != c) {
  95. old = sel;
  96. sel = c;
  97. if(old) {
  98. grabbuttons(old, False);
  99. drawtitle(old);
  100. }
  101. }
  102. if(c) {
  103. detachstack(c);
  104. c->snext = stack;
  105. stack = c;
  106. grabbuttons(c, True);
  107. drawtitle(c);
  108. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  109. }
  110. else
  111. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  112. }
  113. Client *
  114. getclient(Window w) {
  115. Client *c;
  116. for(c = clients; c; c = c->next)
  117. if(c->win == w)
  118. return c;
  119. return NULL;
  120. }
  121. Client *
  122. getctitle(Window w) {
  123. Client *c;
  124. for(c = clients; c; c = c->next)
  125. if(c->twin == w)
  126. return c;
  127. return NULL;
  128. }
  129. void
  130. gravitate(Client *c, Bool invert) {
  131. int dx = 0, dy = 0;
  132. switch(c->grav) {
  133. default:
  134. break;
  135. case StaticGravity:
  136. case NorthWestGravity:
  137. case NorthGravity:
  138. case NorthEastGravity:
  139. dy = c->border;
  140. break;
  141. case EastGravity:
  142. case CenterGravity:
  143. case WestGravity:
  144. dy = -(c->h / 2) + c->border;
  145. break;
  146. case SouthEastGravity:
  147. case SouthGravity:
  148. case SouthWestGravity:
  149. dy = -(c->h);
  150. break;
  151. }
  152. switch (c->grav) {
  153. default:
  154. break;
  155. case StaticGravity:
  156. case NorthWestGravity:
  157. case WestGravity:
  158. case SouthWestGravity:
  159. dx = c->border;
  160. break;
  161. case NorthGravity:
  162. case CenterGravity:
  163. case SouthGravity:
  164. dx = -(c->w / 2) + c->border;
  165. break;
  166. case NorthEastGravity:
  167. case EastGravity:
  168. case SouthEastGravity:
  169. dx = -(c->w + c->border);
  170. break;
  171. }
  172. if(invert) {
  173. dx = -dx;
  174. dy = -dy;
  175. }
  176. c->x += dx;
  177. c->y += dy;
  178. }
  179. void
  180. killclient(Arg *arg) {
  181. if(!sel)
  182. return;
  183. if(sel->proto & PROTODELWIN)
  184. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  185. else
  186. XKillClient(dpy, sel->win);
  187. }
  188. void
  189. manage(Window w, XWindowAttributes *wa) {
  190. Client *c;
  191. Window trans;
  192. XSetWindowAttributes twa;
  193. c = emallocz(sizeof(Client));
  194. c->tags = emallocz(ntags * sizeof(Bool));
  195. c->win = w;
  196. c->x = c->tx = wa->x;
  197. c->y = c->ty = wa->y;
  198. c->w = c->tw = wa->width;
  199. c->h = wa->height;
  200. c->th = bh;
  201. c->border = 0;
  202. updatesize(c);
  203. if(c->x + c->w + 2 > sw)
  204. c->x = sw - c->w - 2;
  205. if(c->x < 0)
  206. c->x = 0;
  207. if(c->y + c->h + 2 > sh)
  208. c->y = sh - c->h - 2;
  209. if(c->h != sh && c->y < bh)
  210. c->y = bh;
  211. c->proto = getproto(c->win);
  212. XSelectInput(dpy, c->win,
  213. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  214. XGetTransientForHint(dpy, c->win, &trans);
  215. twa.override_redirect = 1;
  216. twa.background_pixmap = ParentRelative;
  217. twa.event_mask = ExposureMask | EnterWindowMask;
  218. c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  219. 0, DefaultDepth(dpy, screen), CopyFromParent,
  220. DefaultVisual(dpy, screen),
  221. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  222. grabbuttons(c, False);
  223. settags(c, getclient(trans));
  224. if(!c->isfloat)
  225. c->isfloat = trans
  226. || (c->maxw && c->minw &&
  227. c->maxw == c->minw && c->maxh == c->minh);
  228. if(clients)
  229. clients->prev = c;
  230. c->next = clients;
  231. c->snext = stack;
  232. stack = clients = c;
  233. updatetitle(c);
  234. ban(c);
  235. XMapWindow(dpy, c->win);
  236. XMapWindow(dpy, c->twin);
  237. if(isvisible(c))
  238. focus(c);
  239. arrange(NULL);
  240. }
  241. void
  242. resize(Client *c, Bool sizehints, Corner sticky) {
  243. int bottom = c->y + c->h;
  244. int right = c->x + c->w;
  245. XWindowChanges wc;
  246. if(sizehints) {
  247. if(c->incw)
  248. c->w -= (c->w - c->basew) % c->incw;
  249. if(c->inch)
  250. c->h -= (c->h - c->baseh) % c->inch;
  251. if(c->minw && c->w < c->minw)
  252. c->w = c->minw;
  253. if(c->minh && c->h < c->minh)
  254. c->h = c->minh;
  255. if(c->maxw && c->w > c->maxw)
  256. c->w = c->maxw;
  257. if(c->maxh && c->h > c->maxh)
  258. c->h = c->maxh;
  259. }
  260. if(sticky == TopRight || sticky == BotRight)
  261. c->x = right - c->w;
  262. if(sticky == BotLeft || sticky == BotRight)
  263. c->y = bottom - c->h;
  264. /* offscreen appearance fixes */
  265. if(c->x + c->w < 0)
  266. c->x = 0;
  267. if(c->y + c->h < bh)
  268. c->y = bh;
  269. if(c->x > sw)
  270. c->x = sw - c->w;
  271. if(c->y > sh)
  272. c->y = sh - c->h;
  273. resizetitle(c);
  274. wc.x = c->x;
  275. wc.y = c->y;
  276. wc.width = c->w;
  277. wc.height = c->h;
  278. if(c->w == sw && c->h == sh)
  279. wc.border_width = 0;
  280. else
  281. wc.border_width = 1;
  282. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  283. configure(c);
  284. XSync(dpy, False);
  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. resizetitle(c);
  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. }