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.

469 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 < 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(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->tags = emallocz(ntags * sizeof(Bool));
  185. c->win = w;
  186. c->x = c->tx = wa->x;
  187. c->y = c->ty = wa->y;
  188. c->w = c->tw = wa->width;
  189. c->h = wa->height;
  190. c->th = bh;
  191. c->border = 0;
  192. setsize(c);
  193. if(c->h != sh && c->y < bh)
  194. c->y = c->ty = bh;
  195. c->proto = getproto(c->win);
  196. XSelectInput(dpy, c->win,
  197. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  198. XGetTransientForHint(dpy, c->win, &trans);
  199. twa.override_redirect = 1;
  200. twa.background_pixmap = ParentRelative;
  201. twa.event_mask = ExposureMask | EnterWindowMask;
  202. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  203. 0, DefaultDepth(dpy, screen), CopyFromParent,
  204. DefaultVisual(dpy, screen),
  205. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  206. if(clients)
  207. clients->prev = c;
  208. c->next = clients;
  209. clients = c;
  210. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  211. GrabModeAsync, GrabModeSync, None, None);
  212. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  213. GrabModeAsync, GrabModeSync, None, None);
  214. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  215. GrabModeAsync, GrabModeSync, None, None);
  216. settags(c);
  217. if(!c->isfloat)
  218. c->isfloat = trans
  219. || (c->maxw && c->minw &&
  220. c->maxw == c->minw && c->maxh == c->minh);
  221. settitle(c);
  222. arrange(NULL);
  223. /* mapping the window now prevents flicker */
  224. XMapRaised(dpy, c->win);
  225. XMapRaised(dpy, c->title);
  226. if(c->tags[tsel])
  227. focus(c);
  228. }
  229. void
  230. resize(Client *c, Bool sizehints, Corner sticky)
  231. {
  232. int bottom = c->y + c->h;
  233. int right = c->x + c->w;
  234. /*XConfigureEvent e;*/
  235. XWindowChanges wc;
  236. if(sizehints) {
  237. if(c->incw)
  238. c->w -= (c->w - c->basew) % c->incw;
  239. if(c->inch)
  240. c->h -= (c->h - c->baseh) % c->inch;
  241. if(c->minw && c->w < c->minw)
  242. c->w = c->minw;
  243. if(c->minh && c->h < c->minh)
  244. c->h = c->minh;
  245. if(c->maxw && c->w > c->maxw)
  246. c->w = c->maxw;
  247. if(c->maxh && c->h > c->maxh)
  248. c->h = c->maxh;
  249. }
  250. if(c->x > right) /* might happen on restart */
  251. c->x = right - c->w;
  252. if(c->y > bottom)
  253. c->y = bottom - c->h;
  254. if(sticky == TopRight || sticky == BotRight)
  255. c->x = right - c->w;
  256. if(sticky == BotLeft || sticky == BotRight)
  257. c->y = bottom - c->h;
  258. resizetitle(c);
  259. wc.x = c->x;
  260. wc.y = c->y;
  261. wc.width = c->w;
  262. wc.height = c->h;
  263. if(c->w == sw && c->h == sh)
  264. wc.border_width = 0;
  265. else
  266. wc.border_width = 1;
  267. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  268. XSync(dpy, False);
  269. }
  270. void
  271. setsize(Client *c)
  272. {
  273. long msize;
  274. XSizeHints size;
  275. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  276. size.flags = PSize;
  277. c->flags = size.flags;
  278. if(c->flags & PBaseSize) {
  279. c->basew = size.base_width;
  280. c->baseh = size.base_height;
  281. }
  282. else
  283. c->basew = c->baseh = 0;
  284. if(c->flags & PResizeInc) {
  285. c->incw = size.width_inc;
  286. c->inch = size.height_inc;
  287. }
  288. else
  289. c->incw = c->inch = 0;
  290. if(c->flags & PMaxSize) {
  291. c->maxw = size.max_width;
  292. c->maxh = size.max_height;
  293. }
  294. else
  295. c->maxw = c->maxh = 0;
  296. if(c->flags & PMinSize) {
  297. c->minw = size.min_width;
  298. c->minh = size.min_height;
  299. }
  300. else
  301. c->minw = c->minh = 0;
  302. if(c->flags & PWinGravity)
  303. c->grav = size.win_gravity;
  304. else
  305. c->grav = NorthWestGravity;
  306. }
  307. void
  308. settitle(Client *c)
  309. {
  310. char **list = NULL;
  311. int n;
  312. XTextProperty name;
  313. name.nitems = 0;
  314. c->name[0] = 0;
  315. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  316. if(!name.nitems)
  317. XGetWMName(dpy, c->win, &name);
  318. if(!name.nitems)
  319. return;
  320. if(name.encoding == XA_STRING)
  321. strncpy(c->name, (char *)name.value, sizeof(c->name));
  322. else {
  323. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  324. && n > 0 && *list)
  325. {
  326. strncpy(c->name, *list, sizeof(c->name));
  327. XFreeStringList(list);
  328. }
  329. }
  330. XFree(name.value);
  331. resizetitle(c);
  332. }
  333. void
  334. togglemax(Arg *arg)
  335. {
  336. int ox, oy, ow, oh;
  337. XEvent ev;
  338. if(!sel)
  339. return;
  340. if((sel->ismax = !sel->ismax)) {
  341. ox = sel->x;
  342. oy = sel->y;
  343. ow = sel->w;
  344. oh = sel->h;
  345. sel->x = sx;
  346. sel->y = sy + bh;
  347. sel->w = sw - 2;
  348. sel->h = sh - 2 - bh;
  349. higher(sel);
  350. resize(sel, False, TopLeft);
  351. sel->x = ox;
  352. sel->y = oy;
  353. sel->w = ow;
  354. sel->h = oh;
  355. }
  356. else
  357. resize(sel, False, TopLeft);
  358. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  359. }
  360. void
  361. unmanage(Client *c)
  362. {
  363. XGrabServer(dpy);
  364. XSetErrorHandler(xerrordummy);
  365. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  366. XDestroyWindow(dpy, c->title);
  367. if(c->prev)
  368. c->prev->next = c->next;
  369. if(c->next)
  370. c->next->prev = c->prev;
  371. if(c == clients)
  372. clients = c->next;
  373. if(sel == c) {
  374. sel = getnext(c->next);
  375. if(!sel)
  376. sel = getprev(c->prev);
  377. if(!sel)
  378. sel = clients;
  379. }
  380. free(c->tags);
  381. free(c);
  382. XSync(dpy, False);
  383. XSetErrorHandler(xerror);
  384. XUngrabServer(dpy);
  385. arrange(NULL);
  386. if(sel)
  387. focus(sel);
  388. }
  389. void
  390. zoom(Arg *arg)
  391. {
  392. Client *c;
  393. if(!sel)
  394. return;
  395. if(sel == getnext(clients) && sel->next) {
  396. if((c = getnext(sel->next)))
  397. sel = c;
  398. }
  399. /* pop */
  400. if(sel->prev)
  401. sel->prev->next = sel->next;
  402. if(sel->next)
  403. sel->next->prev = sel->prev;
  404. sel->prev = NULL;
  405. if(clients)
  406. clients->prev = sel;
  407. sel->next = clients;
  408. clients = sel;
  409. arrange(NULL);
  410. focus(sel);
  411. }