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.

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