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.

294 lines
5.1 KiB

18 years ago
18 years ago
18 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include "dwm.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. unsigned int blw = 0;
  6. Layout *lt = NULL;
  7. /* static */
  8. static double hratio = HRATIO;
  9. static double vratio = VRATIO;
  10. static unsigned int nlayouts = 0;
  11. static unsigned int nmaster = NMASTER;
  12. static double /* simple pow() */
  13. spow(double x, double y)
  14. {
  15. if(y == 0)
  16. return 1;
  17. while(--y)
  18. x *= x;
  19. return x;
  20. }
  21. static void
  22. tile(void) {
  23. double mscale = 0, tscale = 0, sum = 0;
  24. unsigned int i, n, nx, ny, nw, nh, mw, tw;
  25. Client *c;
  26. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  27. n++;
  28. mw = (n <= nmaster) ? waw : waw / (1 + hratio);
  29. tw = waw - mw;
  30. if(n > 0) {
  31. if(n < nmaster) {
  32. for(i = 0; i < n; i++)
  33. sum += spow(vratio, i);
  34. mscale = wah / sum;
  35. }
  36. else {
  37. for(i = 0; i < nmaster; i++)
  38. sum += spow(vratio, i);
  39. mscale = wah / sum;
  40. for(sum = 0, i = 0; i < (n - nmaster); i++)
  41. sum += spow(vratio, i);
  42. tscale = wah / sum;
  43. }
  44. }
  45. nx = wax;
  46. ny = way;
  47. for(i = 0, c = clients; c; c = c->next)
  48. if(isvisible(c)) {
  49. unban(c);
  50. if(c->isfloating)
  51. continue;
  52. c->ismax = False;
  53. if(i < nmaster) { /* master window */
  54. nw = mw - 2 * c->border;
  55. if(i + 1 == n || i + 1 == nmaster)
  56. nh = (way + wah) - ny - (2 * c->border);
  57. else
  58. nh = (mscale * spow(vratio, i)) - (2 * c->border);
  59. }
  60. else { /* tile window */
  61. if(i == nmaster) {
  62. ny = way;
  63. nx = wax + mw;
  64. }
  65. nw = tw - 2 * c->border;
  66. if(i + 1 == n)
  67. nh = (way + wah) - ny - (2 * c->border);
  68. else
  69. nh = (tscale * spow(vratio, i - nmaster)) - (2 * c->border);
  70. }
  71. if(nh < bh) {
  72. nh = bh;
  73. ny = way + wah - nh;
  74. }
  75. resize(c, nx, ny, nw, nh, False);
  76. ny += nh;
  77. i++;
  78. }
  79. else
  80. ban(c);
  81. focus(NULL);
  82. restack();
  83. }
  84. LAYOUTS
  85. static void
  86. incratio(const char *arg, double *ratio, double def) {
  87. double delta;
  88. if(lt->arrange != tile)
  89. return;
  90. if(!arg)
  91. *ratio = def;
  92. else {
  93. if(1 == sscanf(arg, "%lf", &delta)) {
  94. if(delta + (*ratio) < .1 || delta + (*ratio) > 1.9)
  95. return;
  96. *ratio += delta;
  97. }
  98. }
  99. lt->arrange();
  100. }
  101. /* extern */
  102. void
  103. floating(void) {
  104. Client *c;
  105. for(c = clients; c; c = c->next)
  106. if(isvisible(c)) {
  107. unban(c);
  108. resize(c, c->x, c->y, c->w, c->h, True);
  109. }
  110. else
  111. ban(c);
  112. focus(NULL);
  113. restack();
  114. }
  115. void
  116. focusclient(const char *arg) {
  117. Client *c;
  118. if(!sel || !arg)
  119. return;
  120. if(atoi(arg) < 0) {
  121. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  122. if(!c) {
  123. for(c = clients; c && c->next; c = c->next);
  124. for(; c && !isvisible(c); c = c->prev);
  125. }
  126. }
  127. else {
  128. for(c = sel->next; c && !isvisible(c); c = c->next);
  129. if(!c)
  130. for(c = clients; c && !isvisible(c); c = c->next);
  131. }
  132. if(c) {
  133. focus(c);
  134. restack();
  135. }
  136. }
  137. void
  138. inchratio(const char *arg) {
  139. incratio(arg, &hratio, HRATIO);
  140. }
  141. void
  142. incvratio(const char *arg) {
  143. incratio(arg, &vratio, VRATIO);
  144. }
  145. void
  146. incnmaster(const char *arg) {
  147. int i;
  148. if(!arg)
  149. nmaster = NMASTER;
  150. else {
  151. i = atoi(arg);
  152. if((lt->arrange != tile) || (nmaster + i < 1)
  153. || (wah / (nmaster + i) <= 2 * BORDERPX))
  154. return;
  155. nmaster += i;
  156. }
  157. if(sel)
  158. lt->arrange();
  159. else
  160. drawstatus();
  161. }
  162. void
  163. initlayouts(void) {
  164. unsigned int i, w;
  165. lt = &layout[0];
  166. nlayouts = sizeof layout / sizeof layout[0];
  167. for(blw = i = 0; i < nlayouts; i++) {
  168. w = textw(layout[i].symbol);
  169. if(w > blw)
  170. blw = w;
  171. }
  172. }
  173. Client *
  174. nexttiled(Client *c) {
  175. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  176. return c;
  177. }
  178. void
  179. restack(void) {
  180. Client *c;
  181. XEvent ev;
  182. XWindowChanges wc;
  183. drawstatus();
  184. if(!sel)
  185. return;
  186. if(sel->isfloating || lt->arrange == floating)
  187. XRaiseWindow(dpy, sel->win);
  188. if(lt->arrange != floating) {
  189. wc.stack_mode = Below;
  190. wc.sibling = barwin;
  191. if(!sel->isfloating) {
  192. XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
  193. wc.sibling = sel->win;
  194. }
  195. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  196. if(c == sel)
  197. continue;
  198. XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
  199. wc.sibling = c->win;
  200. }
  201. }
  202. XSync(dpy, False);
  203. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  204. }
  205. void
  206. setlayout(const char *arg) {
  207. int i;
  208. if(!arg) {
  209. lt++;
  210. if(lt == layout + nlayouts)
  211. lt = layout;
  212. }
  213. else {
  214. i = atoi(arg);
  215. if(i < 0 || i >= nlayouts)
  216. return;
  217. lt = &layout[i];
  218. }
  219. if(sel)
  220. lt->arrange();
  221. else
  222. drawstatus();
  223. }
  224. void
  225. togglebar(const char *arg) {
  226. if(bpos == BarOff)
  227. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  228. else
  229. bpos = BarOff;
  230. updatebarpos();
  231. lt->arrange();
  232. }
  233. void
  234. togglemax(const char *arg) {
  235. XEvent ev;
  236. if(!sel || (lt->arrange != floating && !sel->isfloating) || sel->isfixed)
  237. return;
  238. if((sel->ismax = !sel->ismax)) {
  239. sel->rx = sel->x;
  240. sel->ry = sel->y;
  241. sel->rw = sel->w;
  242. sel->rh = sel->h;
  243. resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
  244. }
  245. else
  246. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  247. drawstatus();
  248. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  249. }
  250. void
  251. zoom(const char *arg) {
  252. Client *c;
  253. if(!sel || lt->arrange == floating || sel->isfloating)
  254. return;
  255. if((c = sel) == nexttiled(clients))
  256. if(!(c = nexttiled(c->next)))
  257. return;
  258. detach(c);
  259. attach(c);
  260. focus(c);
  261. lt->arrange();
  262. }