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.

452 lines
8.1 KiB

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 <math.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. #include "dwm.h"
  11. static void floating(void);
  12. static void tiling(void);
  13. static void (*arrange)(void) = floating;
  14. void
  15. max(void *aux)
  16. {
  17. if(!stack)
  18. return;
  19. stack->x = sx;
  20. stack->y = sy;
  21. stack->w = sw - 2 * stack->border;
  22. stack->h = sh - 2 * stack->border;
  23. resize(stack);
  24. discard_events(EnterWindowMask);
  25. }
  26. static void
  27. floating(void)
  28. {
  29. Client *c;
  30. for(c = stack; c; c = c->snext)
  31. resize(c);
  32. discard_events(EnterWindowMask);
  33. }
  34. static void
  35. tiling(void)
  36. {
  37. Client *c;
  38. int n, cols, rows, gw, gh, i, j;
  39. float rt, fd;
  40. if(!clients)
  41. return;
  42. for(n = 0, c = clients; c; c = c->next, n++);
  43. rt = sqrt(n);
  44. if(modff(rt, &fd) < 0.5)
  45. rows = floor(rt);
  46. else
  47. rows = ceil(rt);
  48. if(rows * rows < n)
  49. cols = rows + 1;
  50. else
  51. cols = rows;
  52. gw = (sw - 2) / cols;
  53. gh = (sh - 2) / rows;
  54. for(i = j = 0, c = clients; c; c = c->next) {
  55. c->x = i * gw;
  56. c->y = j * gh;
  57. c->w = gw;
  58. c->h = gh;
  59. resize(c);
  60. if(++i == cols) {
  61. j++;
  62. i = 0;
  63. }
  64. }
  65. discard_events(EnterWindowMask);
  66. }
  67. void
  68. toggle(void *aux)
  69. {
  70. if(arrange == floating)
  71. arrange = tiling;
  72. else
  73. arrange = floating;
  74. arrange();
  75. }
  76. void
  77. sel(void *aux)
  78. {
  79. const char *arg = aux;
  80. Client *c = NULL;
  81. if(!arg || !stack)
  82. return;
  83. if(!strncmp(arg, "next", 5))
  84. c = stack->snext ? stack->snext : stack;
  85. else if(!strncmp(arg, "prev", 5))
  86. for(c = stack; c && c->snext; c = c->snext);
  87. if(!c)
  88. c = stack;
  89. craise(c);
  90. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
  91. focus(c);
  92. }
  93. void
  94. ckill(void *aux)
  95. {
  96. Client *c = stack;
  97. if(!c)
  98. return;
  99. if(c->proto & WM_PROTOCOL_DELWIN)
  100. send_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
  101. else
  102. XKillClient(dpy, c->win);
  103. }
  104. static void
  105. resize_title(Client *c)
  106. {
  107. int i;
  108. c->tw = 0;
  109. for(i = 0; i < TLast; i++)
  110. if(c->tags[i])
  111. c->tw += textw(c->tags[i]) + dc.font.height;
  112. c->tw += textw(c->name) + dc.font.height;
  113. if(c->tw > c->w)
  114. c->tw = c->w + 2;
  115. c->tx = c->x + c->w - c->tw + 2;
  116. c->ty = c->y;
  117. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  118. }
  119. void
  120. update_name(Client *c)
  121. {
  122. XTextProperty name;
  123. int n;
  124. char **list = NULL;
  125. name.nitems = 0;
  126. c->name[0] = 0;
  127. XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
  128. if(!name.nitems)
  129. XGetWMName(dpy, c->win, &name);
  130. if(!name.nitems)
  131. return;
  132. if(name.encoding == XA_STRING)
  133. strncpy(c->name, (char *)name.value, sizeof(c->name));
  134. else {
  135. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  136. && n > 0 && *list)
  137. {
  138. strncpy(c->name, *list, sizeof(c->name));
  139. XFreeStringList(list);
  140. }
  141. }
  142. XFree(name.value);
  143. resize_title(c);
  144. }
  145. void
  146. update_size(Client *c)
  147. {
  148. XSizeHints size;
  149. long msize;
  150. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  151. size.flags = PSize;
  152. c->flags = size.flags;
  153. if(c->flags & PBaseSize) {
  154. c->basew = size.base_width;
  155. c->baseh = size.base_height;
  156. }
  157. else
  158. c->basew = c->baseh = 0;
  159. if(c->flags & PResizeInc) {
  160. c->incw = size.width_inc;
  161. c->inch = size.height_inc;
  162. }
  163. else
  164. c->incw = c->inch = 0;
  165. if(c->flags & PMaxSize) {
  166. c->maxw = size.max_width;
  167. c->maxh = size.max_height;
  168. }
  169. else
  170. c->maxw = c->maxh = 0;
  171. if(c->flags & PMinSize) {
  172. c->minw = size.min_width;
  173. c->minh = size.min_height;
  174. }
  175. else
  176. c->minw = c->minh = 0;
  177. if(c->flags & PWinGravity)
  178. c->grav = size.win_gravity;
  179. else
  180. c->grav = NorthWestGravity;
  181. }
  182. void
  183. craise(Client *c)
  184. {
  185. XRaiseWindow(dpy, c->win);
  186. XRaiseWindow(dpy, c->title);
  187. }
  188. void
  189. lower(Client *c)
  190. {
  191. XLowerWindow(dpy, c->title);
  192. XLowerWindow(dpy, c->win);
  193. }
  194. void
  195. focus(Client *c)
  196. {
  197. Client **l, *old;
  198. old = stack;
  199. for(l = &stack; *l && *l != c; l = &(*l)->snext);
  200. if(*l)
  201. *l = c->snext;
  202. c->snext = stack;
  203. stack = c;
  204. if(old && old != c) {
  205. XSetWindowBorder(dpy, old->win, dc.bg);
  206. XMapWindow(dpy, old->title);
  207. draw_client(old);
  208. }
  209. XUnmapWindow(dpy, c->title);
  210. XSetWindowBorder(dpy, c->win, dc.fg);
  211. draw_client(c);
  212. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  213. XFlush(dpy);
  214. discard_events(EnterWindowMask);
  215. }
  216. void
  217. manage(Window w, XWindowAttributes *wa)
  218. {
  219. Client *c, **l;
  220. XSetWindowAttributes twa;
  221. c = emallocz(sizeof(Client));
  222. c->win = w;
  223. c->tx = c->x = wa->x;
  224. c->ty = c->y = wa->y;
  225. c->tw = c->w = wa->width;
  226. c->h = wa->height;
  227. c->th = th;
  228. c->border = 1;
  229. update_size(c);
  230. XSelectInput(dpy, c->win,
  231. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  232. XGetTransientForHint(dpy, c->win, &c->trans);
  233. twa.override_redirect = 1;
  234. twa.background_pixmap = ParentRelative;
  235. twa.event_mask = ExposureMask;
  236. c->tags[tsel] = tags[tsel];
  237. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  238. 0, DefaultDepth(dpy, screen), CopyFromParent,
  239. DefaultVisual(dpy, screen),
  240. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  241. update_name(c);
  242. for(l=&clients; *l; l=&(*l)->next);
  243. c->next = *l; /* *l == nil */
  244. *l = c;
  245. XSetWindowBorderWidth(dpy, c->win, 1);
  246. XMapRaised(dpy, c->win);
  247. XMapRaised(dpy, c->title);
  248. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  249. GrabModeAsync, GrabModeSync, None, None);
  250. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  251. GrabModeAsync, GrabModeSync, None, None);
  252. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  253. GrabModeAsync, GrabModeSync, None, None);
  254. arrange();
  255. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
  256. focus(c);
  257. }
  258. void
  259. gravitate(Client *c, Bool invert)
  260. {
  261. int dx = 0, dy = 0;
  262. switch(c->grav) {
  263. case StaticGravity:
  264. case NorthWestGravity:
  265. case NorthGravity:
  266. case NorthEastGravity:
  267. dy = c->border;
  268. break;
  269. case EastGravity:
  270. case CenterGravity:
  271. case WestGravity:
  272. dy = -(c->h / 2) + c->border;
  273. break;
  274. case SouthEastGravity:
  275. case SouthGravity:
  276. case SouthWestGravity:
  277. dy = -c->h;
  278. break;
  279. default:
  280. break;
  281. }
  282. switch (c->grav) {
  283. case StaticGravity:
  284. case NorthWestGravity:
  285. case WestGravity:
  286. case SouthWestGravity:
  287. dx = c->border;
  288. break;
  289. case NorthGravity:
  290. case CenterGravity:
  291. case SouthGravity:
  292. dx = -(c->w / 2) + c->border;
  293. break;
  294. case NorthEastGravity:
  295. case EastGravity:
  296. case SouthEastGravity:
  297. dx = -(c->w + c->border);
  298. break;
  299. default:
  300. break;
  301. }
  302. if(invert) {
  303. dx = -dx;
  304. dy = -dy;
  305. }
  306. c->x += dx;
  307. c->y += dy;
  308. }
  309. void
  310. resize(Client *c)
  311. {
  312. XConfigureEvent e;
  313. if(c->incw)
  314. c->w -= (c->w - c->basew) % c->incw;
  315. if(c->inch)
  316. c->h -= (c->h - c->baseh) % c->inch;
  317. if(c->minw && c->w < c->minw)
  318. c->w = c->minw;
  319. if(c->minh && c->h < c->minh)
  320. c->h = c->minh;
  321. if(c->maxw && c->w > c->maxw)
  322. c->w = c->maxw;
  323. if(c->maxh && c->h > c->maxh)
  324. c->h = c->maxh;
  325. resize_title(c);
  326. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  327. e.type = ConfigureNotify;
  328. e.event = c->win;
  329. e.window = c->win;
  330. e.x = c->x;
  331. e.y = c->y;
  332. e.width = c->w;
  333. e.height = c->h;
  334. e.border_width = c->border;
  335. e.above = None;
  336. e.override_redirect = False;
  337. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  338. XFlush(dpy);
  339. }
  340. static int
  341. dummy_error_handler(Display *dsply, XErrorEvent *err)
  342. {
  343. return 0;
  344. }
  345. void
  346. unmanage(Client *c)
  347. {
  348. Client **l;
  349. XGrabServer(dpy);
  350. XSetErrorHandler(dummy_error_handler);
  351. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  352. XDestroyWindow(dpy, c->title);
  353. for(l=&clients; *l && *l != c; l=&(*l)->next);
  354. *l = c->next;
  355. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  356. *l = c->snext;
  357. free(c);
  358. XFlush(dpy);
  359. XSetErrorHandler(error_handler);
  360. XUngrabServer(dpy);
  361. arrange();
  362. if(stack)
  363. focus(stack);
  364. }
  365. Client *
  366. gettitle(Window w)
  367. {
  368. Client *c;
  369. for(c = clients; c; c = c->next)
  370. if(c->title == w)
  371. return c;
  372. return NULL;
  373. }
  374. Client *
  375. getclient(Window w)
  376. {
  377. Client *c;
  378. for(c = clients; c; c = c->next)
  379. if(c->win == w)
  380. return c;
  381. return NULL;
  382. }
  383. void
  384. draw_client(Client *c)
  385. {
  386. int i;
  387. if(c == stack)
  388. return;
  389. dc.x = dc.y = 0;
  390. dc.h = c->th;
  391. dc.w = 0;
  392. for(i = 0; i < TLast; i++) {
  393. if(c->tags[i]) {
  394. dc.x += dc.w;
  395. dc.w = textw(c->tags[i]) + dc.font.height;
  396. draw(True, c->tags[i]);
  397. }
  398. }
  399. dc.x += dc.w;
  400. dc.w = textw(c->name) + dc.font.height;
  401. draw(True, c->name);
  402. XCopyArea(dpy, dc.drawable, c->title, dc.gc,
  403. 0, 0, c->tw, c->th, 0, 0);
  404. XFlush(dpy);
  405. }