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.

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