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.

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