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.

470 lines
8.9 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. if(isvisible(c))
  228. sel = c;
  229. arrange(NULL);
  230. XMapWindow(dpy, c->win);
  231. XMapWindow(dpy, c->title);
  232. if(isvisible(c))
  233. focus(c);
  234. }
  235. void
  236. resize(Client *c, Bool sizehints, Corner sticky)
  237. {
  238. int bottom = c->y + c->h;
  239. int right = c->x + c->w;
  240. /*XConfigureEvent e;*/
  241. XWindowChanges wc;
  242. if(sizehints) {
  243. if(c->incw)
  244. c->w -= (c->w - c->basew) % c->incw;
  245. if(c->inch)
  246. c->h -= (c->h - c->baseh) % c->inch;
  247. if(c->minw && c->w < c->minw)
  248. c->w = c->minw;
  249. if(c->minh && c->h < c->minh)
  250. c->h = c->minh;
  251. if(c->maxw && c->w > c->maxw)
  252. c->w = c->maxw;
  253. if(c->maxh && c->h > c->maxh)
  254. c->h = c->maxh;
  255. }
  256. if(c->x > right) /* might happen on restart */
  257. c->x = right - c->w;
  258. if(c->y > bottom)
  259. c->y = bottom - c->h;
  260. if(sticky == TopRight || sticky == BotRight)
  261. c->x = right - c->w;
  262. if(sticky == BotLeft || sticky == BotRight)
  263. c->y = bottom - c->h;
  264. resizetitle(c);
  265. wc.x = c->x;
  266. wc.y = c->y;
  267. wc.width = c->w;
  268. wc.height = c->h;
  269. if(c->w == sw && c->h == sh)
  270. wc.border_width = 0;
  271. else
  272. wc.border_width = 1;
  273. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  274. XSync(dpy, False);
  275. }
  276. void
  277. setsize(Client *c)
  278. {
  279. long msize;
  280. XSizeHints size;
  281. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  282. size.flags = PSize;
  283. c->flags = size.flags;
  284. if(c->flags & PBaseSize) {
  285. c->basew = size.base_width;
  286. c->baseh = size.base_height;
  287. }
  288. else
  289. c->basew = c->baseh = 0;
  290. if(c->flags & PResizeInc) {
  291. c->incw = size.width_inc;
  292. c->inch = size.height_inc;
  293. }
  294. else
  295. c->incw = c->inch = 0;
  296. if(c->flags & PMaxSize) {
  297. c->maxw = size.max_width;
  298. c->maxh = size.max_height;
  299. }
  300. else
  301. c->maxw = c->maxh = 0;
  302. if(c->flags & PMinSize) {
  303. c->minw = size.min_width;
  304. c->minh = size.min_height;
  305. }
  306. else
  307. c->minw = c->minh = 0;
  308. if(c->flags & PWinGravity)
  309. c->grav = size.win_gravity;
  310. else
  311. c->grav = NorthWestGravity;
  312. }
  313. void
  314. settitle(Client *c)
  315. {
  316. char **list = NULL;
  317. int n;
  318. XTextProperty name;
  319. name.nitems = 0;
  320. c->name[0] = 0;
  321. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  322. if(!name.nitems)
  323. XGetWMName(dpy, c->win, &name);
  324. if(!name.nitems)
  325. return;
  326. if(name.encoding == XA_STRING)
  327. strncpy(c->name, (char *)name.value, sizeof(c->name));
  328. else {
  329. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  330. && n > 0 && *list)
  331. {
  332. strncpy(c->name, *list, sizeof(c->name));
  333. XFreeStringList(list);
  334. }
  335. }
  336. XFree(name.value);
  337. resizetitle(c);
  338. }
  339. void
  340. togglemax(Arg *arg)
  341. {
  342. int ox, oy, ow, oh;
  343. XEvent ev;
  344. if(!sel)
  345. return;
  346. if((sel->ismax = !sel->ismax)) {
  347. ox = sel->x;
  348. oy = sel->y;
  349. ow = sel->w;
  350. oh = sel->h;
  351. sel->x = sx;
  352. sel->y = sy + bh;
  353. sel->w = sw - 2;
  354. sel->h = sh - 2 - bh;
  355. restack();
  356. resize(sel, arrange == dofloat, TopLeft);
  357. sel->x = ox;
  358. sel->y = oy;
  359. sel->w = ow;
  360. sel->h = oh;
  361. }
  362. else
  363. resize(sel, False, TopLeft);
  364. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  365. }
  366. void
  367. unmanage(Client *c)
  368. {
  369. XGrabServer(dpy);
  370. XSetErrorHandler(xerrordummy);
  371. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  372. XDestroyWindow(dpy, c->title);
  373. if(c->prev)
  374. c->prev->next = c->next;
  375. if(c->next)
  376. c->next->prev = c->prev;
  377. if(c == clients)
  378. clients = c->next;
  379. if(sel == c)
  380. sel = getnext(clients);
  381. free(c->tags);
  382. free(c);
  383. XSync(dpy, False);
  384. XSetErrorHandler(xerror);
  385. XUngrabServer(dpy);
  386. if(sel)
  387. focus(sel);
  388. arrange(NULL);
  389. }
  390. void
  391. zoom(Arg *arg)
  392. {
  393. Client *c;
  394. if(!sel || (arrange != dotile) || sel->isfloat || sel->ismax)
  395. return;
  396. if(sel == getnext(clients)) {
  397. if((c = getnext(sel->next)))
  398. sel = c;
  399. else
  400. return;
  401. }
  402. /* pop */
  403. sel->prev->next = sel->next;
  404. if(sel->next)
  405. sel->next->prev = sel->prev;
  406. sel->prev = NULL;
  407. clients->prev = sel;
  408. sel->next = clients;
  409. clients = sel;
  410. focus(sel);
  411. arrange(NULL);
  412. }