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.

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