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.

256 lines
4.7 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(const char *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(const char *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(const char *arg) {
  92. int i;
  93. if(lt->arrange != tile)
  94. return;
  95. if(!arg)
  96. masterw = MASTERWIDTH;
  97. else {
  98. i = atoi(arg);
  99. if(waw * (masterw + i) / 1000 >= waw - 2 * BORDERPX
  100. || waw * (masterw + i) / 1000 <= 2 * BORDERPX)
  101. return;
  102. masterw += i;
  103. }
  104. lt->arrange();
  105. }
  106. void
  107. incnmaster(const char *arg) {
  108. int i = arg ? atoi(arg) : 0;
  109. if((lt->arrange != tile) || (nmaster + i < 1)
  110. || (wah / (nmaster + i) <= 2 * BORDERPX))
  111. return;
  112. nmaster += i;
  113. if(sel)
  114. lt->arrange();
  115. else
  116. drawstatus();
  117. }
  118. void
  119. initlayouts(void) {
  120. unsigned int i, w;
  121. lt = &layout[0];
  122. nlayouts = sizeof layout / sizeof layout[0];
  123. for(blw = i = 0; i < nlayouts; i++) {
  124. w = textw(layout[i].symbol);
  125. if(w > blw)
  126. blw = w;
  127. }
  128. }
  129. Client *
  130. nexttiled(Client *c) {
  131. for(; c && (c->isversatile || !isvisible(c)); c = c->next);
  132. return c;
  133. }
  134. void
  135. restack(void) {
  136. Client *c;
  137. XEvent ev;
  138. drawstatus();
  139. if(!sel)
  140. return;
  141. if(sel->isversatile || lt->arrange == versatile)
  142. XRaiseWindow(dpy, sel->win);
  143. if(lt->arrange != versatile) {
  144. if(!sel->isversatile)
  145. XLowerWindow(dpy, sel->win);
  146. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  147. if(c == sel)
  148. continue;
  149. XLowerWindow(dpy, c->win);
  150. }
  151. }
  152. XSync(dpy, False);
  153. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  154. }
  155. void
  156. setlayout(const char *arg) {
  157. unsigned int i;
  158. if(!arg) {
  159. for(i = 0; i < nlayouts && lt != &layout[i]; i++);
  160. if(i == nlayouts - 1)
  161. lt = &layout[0];
  162. else
  163. lt = &layout[++i];
  164. }
  165. else {
  166. i = atoi(arg);
  167. if(i < 0 || i >= nlayouts)
  168. return;
  169. lt = &layout[i];
  170. }
  171. if(sel)
  172. lt->arrange();
  173. else
  174. drawstatus();
  175. }
  176. void
  177. togglemax(const char *arg) {
  178. XEvent ev;
  179. if(!sel || (lt->arrange != versatile && !sel->isversatile) || sel->isfixed)
  180. return;
  181. if((sel->ismax = !sel->ismax)) {
  182. sel->rx = sel->x;
  183. sel->ry = sel->y;
  184. sel->rw = sel->w;
  185. sel->rh = sel->h;
  186. resize(sel, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
  187. }
  188. else
  189. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  190. drawstatus();
  191. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  192. }
  193. void
  194. versatile(void) {
  195. Client *c;
  196. for(c = clients; c; c = c->next) {
  197. if(isvisible(c)) {
  198. if(c->isbanned)
  199. XMoveWindow(dpy, c->win, c->x, c->y);
  200. c->isbanned = False;
  201. resize(c, c->x, c->y, c->w, c->h, True);
  202. }
  203. else {
  204. c->isbanned = True;
  205. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  206. }
  207. }
  208. if(!sel || !isvisible(sel)) {
  209. for(c = stack; c && !isvisible(c); c = c->snext);
  210. focus(c);
  211. }
  212. restack();
  213. }
  214. void
  215. zoom(const char *arg) {
  216. unsigned int n;
  217. Client *c;
  218. if(!sel || lt->arrange != tile || sel->isversatile)
  219. return;
  220. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  221. n++;
  222. if((c = sel) == nexttiled(clients))
  223. if(!(c = nexttiled(c->next)))
  224. return;
  225. detach(c);
  226. attach(c);
  227. focus(c);
  228. lt->arrange();
  229. }