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