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.

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