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.

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