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.

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