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.

431 lines
8.9 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
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. focus(Client *c) {
  74. Client *old;
  75. if(!issel || (c && !isvisible(c)))
  76. return;
  77. if(!sel)
  78. sel = c;
  79. else if(sel != c) {
  80. if(maximized)
  81. togglemax(NULL);
  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 > sw)
  191. c->x = sw - c->w - 2;
  192. if(c->x < 0)
  193. c->x = 0;
  194. if(c->y + c->h + 2 > sh)
  195. c->y = sh - c->h - 2;
  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. settags(c, getclient(trans));
  211. if(!c->isfloat)
  212. c->isfloat = trans
  213. || (c->maxw && c->minw &&
  214. c->maxw == c->minw && c->maxh == c->minh);
  215. if(clients)
  216. clients->prev = c;
  217. c->next = clients;
  218. c->snext = stack;
  219. stack = clients = c;
  220. updatetitle(c);
  221. ban(c);
  222. XMapWindow(dpy, c->win);
  223. XMapWindow(dpy, c->twin);
  224. if(isvisible(c))
  225. focus(c);
  226. arrange(NULL);
  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 < 0)
  253. c->x = 0;
  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 = 1;
  269. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  270. XSync(dpy, False);
  271. }
  272. void
  273. updatesize(Client *c) {
  274. long msize;
  275. XSizeHints size;
  276. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  277. size.flags = PSize;
  278. c->flags = size.flags;
  279. if(c->flags & PBaseSize) {
  280. c->basew = size.base_width;
  281. c->baseh = size.base_height;
  282. }
  283. else
  284. c->basew = c->baseh = 0;
  285. if(c->flags & PResizeInc) {
  286. c->incw = size.width_inc;
  287. c->inch = size.height_inc;
  288. }
  289. else
  290. c->incw = c->inch = 0;
  291. if(c->flags & PMaxSize) {
  292. c->maxw = size.max_width;
  293. c->maxh = size.max_height;
  294. }
  295. else
  296. c->maxw = c->maxh = 0;
  297. if(c->flags & PMinSize) {
  298. c->minw = size.min_width;
  299. c->minh = size.min_height;
  300. }
  301. else
  302. c->minw = c->minh = 0;
  303. if(c->flags & PWinGravity)
  304. c->grav = size.win_gravity;
  305. else
  306. c->grav = NorthWestGravity;
  307. }
  308. void
  309. updatetitle(Client *c) {
  310. char **list = NULL;
  311. int n;
  312. XTextProperty name;
  313. name.nitems = 0;
  314. c->name[0] = 0;
  315. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  316. if(!name.nitems)
  317. XGetWMName(dpy, c->win, &name);
  318. if(!name.nitems)
  319. return;
  320. if(name.encoding == XA_STRING)
  321. strncpy(c->name, (char *)name.value, sizeof(c->name));
  322. else {
  323. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  324. && n > 0 && *list)
  325. {
  326. strncpy(c->name, *list, sizeof(c->name));
  327. XFreeStringList(list);
  328. }
  329. }
  330. XFree(name.value);
  331. resizetitle(c);
  332. }
  333. void
  334. togglemax(Arg *arg) {
  335. int ox, oy, ow, oh;
  336. Client *c;
  337. XEvent ev;
  338. if(!sel)
  339. return;
  340. if((maximized = !maximized)) {
  341. ox = sel->x;
  342. oy = sel->y;
  343. ow = sel->w;
  344. oh = sel->h;
  345. sel->x = sx;
  346. sel->y = sy + bh;
  347. sel->w = sw - 2;
  348. sel->h = sh - 2 - bh;
  349. restack();
  350. for(c = getnext(clients); c; c = getnext(c->next))
  351. if(c != sel)
  352. ban(c);
  353. resize(sel, arrange == dofloat, TopLeft);
  354. sel->x = ox;
  355. sel->y = oy;
  356. sel->w = ow;
  357. sel->h = oh;
  358. }
  359. else
  360. arrange(NULL);
  361. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  362. }
  363. void
  364. unmanage(Client *c) {
  365. Client *nc;
  366. XGrabServer(dpy);
  367. XSetErrorHandler(xerrordummy);
  368. detach(c);
  369. detachstack(c);
  370. if(sel == c) {
  371. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  372. focus(nc);
  373. }
  374. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  375. XDestroyWindow(dpy, c->twin);
  376. free(c->tags);
  377. free(c);
  378. XSync(dpy, False);
  379. XSetErrorHandler(xerror);
  380. XUngrabServer(dpy);
  381. arrange(NULL);
  382. }