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.

251 lines
4.6 KiB

  1. /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. unsigned int blw = 0;
  6. Layout *lt = NULL;
  7. /* static */
  8. static unsigned int nlayouts = 0;
  9. static unsigned int masterw = MASTERWIDTH;
  10. static unsigned int nmaster = NMASTER;
  11. static void
  12. tile(void) {
  13. unsigned int i, n, nx, ny, nw, nh, mw, mh, tw, th;
  14. Client *c;
  15. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  16. n++;
  17. /* window geoms */
  18. mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
  19. mw = (n > nmaster) ? (waw * masterw) / 1000 : waw;
  20. th = (n > nmaster) ? wah / (n - nmaster) : 0;
  21. tw = waw - mw;
  22. for(i = 0, c = clients; c; c = c->next)
  23. if(isvisible(c)) {
  24. if(c->isbanned)
  25. XMoveWindow(dpy, c->win, c->x, c->y);
  26. c->isbanned = False;
  27. if(c->isversatile)
  28. continue;
  29. c->ismax = False;
  30. nx = wax;
  31. ny = way;
  32. if(i < nmaster) {
  33. ny += i * mh;
  34. nw = mw - 2 * BORDERPX;
  35. nh = mh - 2 * BORDERPX;
  36. }
  37. else { /* tile window */
  38. nx += mw;
  39. nw = tw - 2 * BORDERPX;
  40. if(th > 2 * BORDERPX) {
  41. ny += (i - nmaster) * th;
  42. nh = th - 2 * BORDERPX;
  43. }
  44. else /* fallback if th <= 2 * BORDERPX */
  45. nh = wah - 2 * BORDERPX;
  46. }
  47. resize(c, nx, ny, nw, nh, False);
  48. i++;
  49. }
  50. else {
  51. c->isbanned = True;
  52. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  53. }
  54. if(!sel || !isvisible(sel)) {
  55. for(c = stack; c && !isvisible(c); c = c->snext);
  56. focus(c);
  57. }
  58. restack();
  59. }
  60. LAYOUTS
  61. /* extern */
  62. void
  63. focusnext(Arg arg) {
  64. Client *c;
  65. if(!sel)
  66. return;
  67. for(c = sel->next; c && !isvisible(c); c = c->next);
  68. if(!c)
  69. for(c = clients; c && !isvisible(c); c = c->next);
  70. if(c) {
  71. focus(c);
  72. restack();
  73. }
  74. }
  75. void
  76. focusprev(Arg arg) {
  77. Client *c;
  78. if(!sel)
  79. return;
  80. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  81. if(!c) {
  82. for(c = clients; c && c->next; c = c->next);
  83. for(; c && !isvisible(c); c = c->prev);
  84. }
  85. if(c) {
  86. focus(c);
  87. restack();
  88. }
  89. }
  90. void
  91. incmasterw(Arg arg) {
  92. if(lt->arrange != tile)
  93. return;
  94. if(arg.i == 0)
  95. masterw = MASTERWIDTH;
  96. else {
  97. if(waw * (masterw + arg.i) / 1000 >= waw - 2 * BORDERPX
  98. || waw * (masterw + arg.i) / 1000 <= 2 * BORDERPX)
  99. return;
  100. masterw += arg.i;
  101. }
  102. lt->arrange();
  103. }
  104. void
  105. incnmaster(Arg arg) {
  106. if((lt->arrange != tile) || (nmaster + arg.i < 1)
  107. || (wah / (nmaster + arg.i) <= 2 * BORDERPX))
  108. return;
  109. nmaster += arg.i;
  110. if(sel)
  111. lt->arrange();
  112. else
  113. drawstatus();
  114. }
  115. void
  116. initlayouts(void) {
  117. unsigned int i, w;
  118. lt = &layout[0];
  119. nlayouts = sizeof layout / sizeof layout[0];
  120. for(blw = i = 0; i < nlayouts; i++) {
  121. w = textw(layout[i].symbol);
  122. if(w > blw)
  123. blw = w;
  124. }
  125. }
  126. Client *
  127. nexttiled(Client *c) {
  128. for(; c && (c->isversatile || !isvisible(c)); c = c->next);
  129. return c;
  130. }
  131. void
  132. restack(void) {
  133. Client *c;
  134. XEvent ev;
  135. drawstatus();
  136. if(!sel)
  137. return;
  138. if(sel->isversatile || lt->arrange == versatile)
  139. XRaiseWindow(dpy, sel->win);
  140. if(lt->arrange != versatile) {
  141. if(!sel->isversatile)
  142. XLowerWindow(dpy, sel->win);
  143. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  144. if(c == sel)
  145. continue;
  146. XLowerWindow(dpy, c->win);
  147. }
  148. }
  149. XSync(dpy, False);
  150. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  151. }
  152. void
  153. setlayout(Arg arg) {
  154. unsigned int i;
  155. if(arg.i == -1) {
  156. for(i = 0; i < nlayouts && lt != &layout[i]; i++);
  157. if(i == nlayouts - 1)
  158. lt = &layout[0];
  159. else
  160. lt = &layout[++i];
  161. }
  162. else {
  163. if(arg.i < 0 || arg.i >= nlayouts)
  164. return;
  165. lt = &layout[arg.i];
  166. }
  167. if(sel)
  168. lt->arrange();
  169. else
  170. drawstatus();
  171. }
  172. void
  173. togglemax(Arg arg) {
  174. XEvent ev;
  175. if(!sel || (lt->arrange != versatile && !sel->isversatile) || sel->isfixed)
  176. return;
  177. if((sel->ismax = !sel->ismax)) {
  178. sel->rx = sel->x;
  179. sel->ry = sel->y;
  180. sel->rw = sel->w;
  181. sel->rh = sel->h;
  182. resize(sel, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
  183. }
  184. else
  185. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  186. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  187. }
  188. void
  189. versatile(void) {
  190. Client *c;
  191. for(c = clients; c; c = c->next) {
  192. if(isvisible(c)) {
  193. if(c->isbanned)
  194. XMoveWindow(dpy, c->win, c->x, c->y);
  195. c->isbanned = False;
  196. resize(c, c->x, c->y, c->w, c->h, True);
  197. }
  198. else {
  199. c->isbanned = True;
  200. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  201. }
  202. }
  203. if(!sel || !isvisible(sel)) {
  204. for(c = stack; c && !isvisible(c); c = c->snext);
  205. focus(c);
  206. }
  207. restack();
  208. }
  209. void
  210. zoom(Arg arg) {
  211. unsigned int n;
  212. Client *c;
  213. if(!sel || lt->arrange != tile || sel->isversatile)
  214. return;
  215. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  216. n++;
  217. if((c = sel) == nexttiled(clients))
  218. if(!(c = nexttiled(c->next)))
  219. return;
  220. detach(c);
  221. attach(c);
  222. focus(c);
  223. lt->arrange();
  224. }