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.

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