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.

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