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.

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