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.

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