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.

423 lines
8.4 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 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. grabbuttons(Client *c, Bool focus)
  13. {
  14. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  15. if(focus) {
  16. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  17. GrabModeAsync, GrabModeSync, None, None);
  18. XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
  19. GrabModeAsync, GrabModeSync, None, None);
  20. XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  21. GrabModeAsync, GrabModeSync, None, None);
  22. XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  23. GrabModeAsync, GrabModeSync, None, None);
  24. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  25. GrabModeAsync, GrabModeSync, None, None);
  26. XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
  27. GrabModeAsync, GrabModeSync, None, None);
  28. XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  29. GrabModeAsync, GrabModeSync, None, None);
  30. XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  31. GrabModeAsync, GrabModeSync, None, None);
  32. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  33. GrabModeAsync, GrabModeSync, None, None);
  34. XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
  35. GrabModeAsync, GrabModeSync, None, None);
  36. XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
  37. GrabModeAsync, GrabModeSync, None, None);
  38. XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
  39. GrabModeAsync, GrabModeSync, None, None);
  40. }
  41. else
  42. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  43. GrabModeAsync, GrabModeSync, None, None);
  44. }
  45. static void
  46. resizetitle(Client *c)
  47. {
  48. c->tw = textw(c->name);
  49. if(c->tw > c->w)
  50. c->tw = c->w + 2;
  51. c->tx = c->x + c->w - c->tw + 2;
  52. c->ty = c->y;
  53. if(isvisible(c))
  54. XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
  55. else
  56. XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
  57. }
  58. static int
  59. xerrordummy(Display *dsply, XErrorEvent *ee)
  60. {
  61. return 0;
  62. }
  63. /* extern functions */
  64. void
  65. ban(Client *c)
  66. {
  67. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  68. XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
  69. }
  70. void
  71. focus(Client *c)
  72. {
  73. Client *old = sel;
  74. if(!issel)
  75. return;
  76. if(!sel)
  77. sel = c;
  78. else if(sel != c) {
  79. if(sel->ismax)
  80. togglemax(NULL);
  81. sel = c;
  82. grabbuttons(old, False);
  83. drawtitle(old);
  84. }
  85. grabbuttons(c, True);
  86. drawtitle(c);
  87. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  88. }
  89. Client *
  90. getclient(Window w)
  91. {
  92. Client *c;
  93. for(c = clients; c; c = c->next)
  94. if(c->win == w)
  95. return c;
  96. return NULL;
  97. }
  98. Client *
  99. getctitle(Window w)
  100. {
  101. Client *c;
  102. for(c = clients; c; c = c->next)
  103. if(c->twin == w)
  104. return c;
  105. return NULL;
  106. }
  107. void
  108. gravitate(Client *c, Bool invert)
  109. {
  110. int dx = 0, dy = 0;
  111. switch(c->grav) {
  112. default:
  113. break;
  114. case StaticGravity:
  115. case NorthWestGravity:
  116. case NorthGravity:
  117. case NorthEastGravity:
  118. dy = c->border;
  119. break;
  120. case EastGravity:
  121. case CenterGravity:
  122. case WestGravity:
  123. dy = -(c->h / 2) + c->border;
  124. break;
  125. case SouthEastGravity:
  126. case SouthGravity:
  127. case SouthWestGravity:
  128. dy = -(c->h);
  129. break;
  130. }
  131. switch (c->grav) {
  132. default:
  133. break;
  134. case StaticGravity:
  135. case NorthWestGravity:
  136. case WestGravity:
  137. case SouthWestGravity:
  138. dx = c->border;
  139. break;
  140. case NorthGravity:
  141. case CenterGravity:
  142. case SouthGravity:
  143. dx = -(c->w / 2) + c->border;
  144. break;
  145. case NorthEastGravity:
  146. case EastGravity:
  147. case SouthEastGravity:
  148. dx = -(c->w + c->border);
  149. break;
  150. }
  151. if(invert) {
  152. dx = -dx;
  153. dy = -dy;
  154. }
  155. c->x += dx;
  156. c->y += dy;
  157. }
  158. void
  159. killclient(Arg *arg)
  160. {
  161. if(!sel)
  162. return;
  163. if(sel->proto & PROTODELWIN)
  164. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  165. else
  166. XKillClient(dpy, sel->win);
  167. }
  168. void
  169. manage(Window w, XWindowAttributes *wa)
  170. {
  171. unsigned int i;
  172. Client *c, *tc;
  173. Window trans;
  174. XSetWindowAttributes twa;
  175. c = emallocz(sizeof(Client));
  176. c->tags = emallocz(ntags * sizeof(Bool));
  177. c->win = w;
  178. c->x = c->tx = wa->x;
  179. c->y = c->ty = wa->y;
  180. c->w = c->tw = wa->width;
  181. c->h = wa->height;
  182. c->th = bh;
  183. c->border = 0;
  184. setsize(c);
  185. if(c->x + c->w + 2 > sw)
  186. c->x = sw - c->w - 2;
  187. if(c->x < 0)
  188. c->x = 0;
  189. if(c->y + c->h + 2 > sh)
  190. c->y = sh - c->h - 2;
  191. if(c->h != sh && c->y < bh)
  192. c->y = bh;
  193. c->proto = getproto(c->win);
  194. XSelectInput(dpy, c->win,
  195. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  196. XGetTransientForHint(dpy, c->win, &trans);
  197. twa.override_redirect = 1;
  198. twa.background_pixmap = ParentRelative;
  199. twa.event_mask = ExposureMask | EnterWindowMask;
  200. c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  201. 0, DefaultDepth(dpy, screen), CopyFromParent,
  202. DefaultVisual(dpy, screen),
  203. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  204. grabbuttons(c, False);
  205. if((tc = getclient(trans))) /* inherit tags */
  206. for(i = 0; i < ntags; i++)
  207. c->tags[i] = tc->tags[i];
  208. else
  209. settags(c);
  210. if(!c->isfloat)
  211. c->isfloat = trans
  212. || (c->maxw && c->minw &&
  213. c->maxw == c->minw && c->maxh == c->minh);
  214. attach(c);
  215. settitle(c);
  216. if(isvisible(c))
  217. sel = c;
  218. arrange(NULL);
  219. XMapWindow(dpy, c->win);
  220. XMapWindow(dpy, c->twin);
  221. if(isvisible(c))
  222. focus(c);
  223. }
  224. void
  225. resize(Client *c, Bool sizehints, Corner sticky)
  226. {
  227. int bottom = c->y + c->h;
  228. int right = c->x + c->w;
  229. XWindowChanges wc;
  230. if(sizehints) {
  231. if(c->incw)
  232. c->w -= (c->w - c->basew) % c->incw;
  233. if(c->inch)
  234. c->h -= (c->h - c->baseh) % c->inch;
  235. if(c->minw && c->w < c->minw)
  236. c->w = c->minw;
  237. if(c->minh && c->h < c->minh)
  238. c->h = c->minh;
  239. if(c->maxw && c->w > c->maxw)
  240. c->w = c->maxw;
  241. if(c->maxh && c->h > c->maxh)
  242. c->h = c->maxh;
  243. }
  244. if(sticky == TopRight || sticky == BotRight)
  245. c->x = right - c->w;
  246. if(sticky == BotLeft || sticky == BotRight)
  247. c->y = bottom - c->h;
  248. resizetitle(c);
  249. wc.x = c->x;
  250. wc.y = c->y;
  251. wc.width = c->w;
  252. wc.height = c->h;
  253. if(c->w == sw && c->h == sh)
  254. wc.border_width = 0;
  255. else
  256. wc.border_width = 1;
  257. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  258. XSync(dpy, False);
  259. }
  260. void
  261. setsize(Client *c)
  262. {
  263. long msize;
  264. XSizeHints size;
  265. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  266. size.flags = PSize;
  267. c->flags = size.flags;
  268. if(c->flags & PBaseSize) {
  269. c->basew = size.base_width;
  270. c->baseh = size.base_height;
  271. }
  272. else
  273. c->basew = c->baseh = 0;
  274. if(c->flags & PResizeInc) {
  275. c->incw = size.width_inc;
  276. c->inch = size.height_inc;
  277. }
  278. else
  279. c->incw = c->inch = 0;
  280. if(c->flags & PMaxSize) {
  281. c->maxw = size.max_width;
  282. c->maxh = size.max_height;
  283. }
  284. else
  285. c->maxw = c->maxh = 0;
  286. if(c->flags & PMinSize) {
  287. c->minw = size.min_width;
  288. c->minh = size.min_height;
  289. }
  290. else
  291. c->minw = c->minh = 0;
  292. if(c->flags & PWinGravity)
  293. c->grav = size.win_gravity;
  294. else
  295. c->grav = NorthWestGravity;
  296. }
  297. void
  298. settitle(Client *c)
  299. {
  300. char **list = NULL;
  301. int n;
  302. XTextProperty name;
  303. name.nitems = 0;
  304. c->name[0] = 0;
  305. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  306. if(!name.nitems)
  307. XGetWMName(dpy, c->win, &name);
  308. if(!name.nitems)
  309. return;
  310. if(name.encoding == XA_STRING)
  311. strncpy(c->name, (char *)name.value, sizeof(c->name));
  312. else {
  313. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  314. && n > 0 && *list)
  315. {
  316. strncpy(c->name, *list, sizeof(c->name));
  317. XFreeStringList(list);
  318. }
  319. }
  320. XFree(name.value);
  321. resizetitle(c);
  322. }
  323. void
  324. togglemax(Arg *arg)
  325. {
  326. int ox, oy, ow, oh;
  327. XEvent ev;
  328. if(!sel)
  329. return;
  330. if((sel->ismax = !sel->ismax)) {
  331. ox = sel->x;
  332. oy = sel->y;
  333. ow = sel->w;
  334. oh = sel->h;
  335. sel->x = sx;
  336. sel->y = sy + bh;
  337. sel->w = sw - 2;
  338. sel->h = sh - 2 - bh;
  339. restack();
  340. resize(sel, arrange == dofloat, TopLeft);
  341. sel->x = ox;
  342. sel->y = oy;
  343. sel->w = ow;
  344. sel->h = oh;
  345. }
  346. else
  347. resize(sel, False, TopLeft);
  348. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  349. }
  350. void
  351. unmanage(Client *c)
  352. {
  353. Client *tc;
  354. Window trans;
  355. XGrabServer(dpy);
  356. XSetErrorHandler(xerrordummy);
  357. XGetTransientForHint(dpy, c->win, &trans);
  358. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  359. XDestroyWindow(dpy, c->twin);
  360. detach(c);
  361. if(sel == c) {
  362. if(trans && (tc = getclient(trans)) && isvisible(tc))
  363. sel = tc;
  364. else
  365. sel = getnext(clients);
  366. }
  367. free(c->tags);
  368. free(c);
  369. XSync(dpy, False);
  370. XSetErrorHandler(xerror);
  371. XUngrabServer(dpy);
  372. if(sel)
  373. focus(sel);
  374. arrange(NULL);
  375. }