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.

421 lines
8.7 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
18 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
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)
  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. resizetitle(c);
  252. wc.x = c->x;
  253. wc.y = c->y;
  254. wc.width = c->w;
  255. wc.height = c->h;
  256. if(c->w == sw && c->h == sh)
  257. wc.border_width = 0;
  258. else
  259. wc.border_width = 1;
  260. XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
  261. XSync(dpy, False);
  262. }
  263. void
  264. updatesize(Client *c) {
  265. long msize;
  266. XSizeHints size;
  267. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  268. size.flags = PSize;
  269. c->flags = size.flags;
  270. if(c->flags & PBaseSize) {
  271. c->basew = size.base_width;
  272. c->baseh = size.base_height;
  273. }
  274. else
  275. c->basew = c->baseh = 0;
  276. if(c->flags & PResizeInc) {
  277. c->incw = size.width_inc;
  278. c->inch = size.height_inc;
  279. }
  280. else
  281. c->incw = c->inch = 0;
  282. if(c->flags & PMaxSize) {
  283. c->maxw = size.max_width;
  284. c->maxh = size.max_height;
  285. }
  286. else
  287. c->maxw = c->maxh = 0;
  288. if(c->flags & PMinSize) {
  289. c->minw = size.min_width;
  290. c->minh = size.min_height;
  291. }
  292. else
  293. c->minw = c->minh = 0;
  294. if(c->flags & PWinGravity)
  295. c->grav = size.win_gravity;
  296. else
  297. c->grav = NorthWestGravity;
  298. }
  299. void
  300. updatetitle(Client *c) {
  301. char **list = NULL;
  302. int n;
  303. XTextProperty name;
  304. name.nitems = 0;
  305. c->name[0] = 0;
  306. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  307. if(!name.nitems)
  308. XGetWMName(dpy, c->win, &name);
  309. if(!name.nitems)
  310. return;
  311. if(name.encoding == XA_STRING)
  312. strncpy(c->name, (char *)name.value, sizeof(c->name));
  313. else {
  314. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  315. && n > 0 && *list)
  316. {
  317. strncpy(c->name, *list, sizeof(c->name));
  318. XFreeStringList(list);
  319. }
  320. }
  321. XFree(name.value);
  322. resizetitle(c);
  323. }
  324. void
  325. togglemax(Arg *arg) {
  326. int ox, oy, ow, oh;
  327. Client *c;
  328. XEvent ev;
  329. if(!sel)
  330. return;
  331. if((maximized = !maximized)) {
  332. ox = sel->x;
  333. oy = sel->y;
  334. ow = sel->w;
  335. oh = sel->h;
  336. sel->x = sx;
  337. sel->y = sy + bh;
  338. sel->w = sw - 2;
  339. sel->h = sh - 2 - bh;
  340. restack();
  341. for(c = getnext(clients); c; c = getnext(c->next))
  342. if(c != sel)
  343. ban(c);
  344. resize(sel, arrange == dofloat, TopLeft);
  345. sel->x = ox;
  346. sel->y = oy;
  347. sel->w = ow;
  348. sel->h = oh;
  349. }
  350. else
  351. arrange(NULL);
  352. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  353. }
  354. void
  355. unmanage(Client *c) {
  356. Client *nc;
  357. XGrabServer(dpy);
  358. XSetErrorHandler(xerrordummy);
  359. detach(c);
  360. detachstack(c);
  361. if(sel == c) {
  362. for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
  363. focus(nc);
  364. }
  365. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  366. XDestroyWindow(dpy, c->twin);
  367. free(c->tags);
  368. free(c);
  369. XSync(dpy, False);
  370. XSetErrorHandler(xerror);
  371. XUngrabServer(dpy);
  372. arrange(NULL);
  373. }