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.

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