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.

254 lines
4.7 KiB

18 years ago
18 years ago
  1. /* © 2004-2007 Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details. */
  3. #include "dwm.h"
  4. #include <stdlib.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->isfloating)
  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. floating(void) {
  64. Client *c;
  65. for(c = clients; c; c = c->next) {
  66. if(isvisible(c)) {
  67. if(c->isbanned)
  68. XMoveWindow(dpy, c->win, c->x, c->y);
  69. c->isbanned = False;
  70. resize(c, c->x, c->y, c->w, c->h, True);
  71. }
  72. else {
  73. c->isbanned = True;
  74. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  75. }
  76. }
  77. if(!sel || !isvisible(sel)) {
  78. for(c = stack; c && !isvisible(c); c = c->snext);
  79. focus(c);
  80. }
  81. restack();
  82. }
  83. void
  84. focusclient(const char *arg) {
  85. Client *c;
  86. if(!sel || !arg)
  87. return;
  88. if(atoi(arg) < 0) {
  89. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  90. if(!c) {
  91. for(c = clients; c && c->next; c = c->next);
  92. for(; c && !isvisible(c); c = c->prev);
  93. }
  94. }
  95. else {
  96. for(c = sel->next; c && !isvisible(c); c = c->next);
  97. if(!c)
  98. for(c = clients; c && !isvisible(c); c = c->next);
  99. }
  100. if(c) {
  101. focus(c);
  102. restack();
  103. }
  104. }
  105. void
  106. incmasterw(const char *arg) {
  107. int i;
  108. if(lt->arrange != tile)
  109. return;
  110. if(!arg)
  111. masterw = MASTERWIDTH;
  112. else {
  113. i = atoi(arg);
  114. if(waw * (masterw + i) / 1000 >= waw - 2 * BORDERPX
  115. || waw * (masterw + i) / 1000 <= 2 * BORDERPX)
  116. return;
  117. masterw += i;
  118. }
  119. lt->arrange();
  120. }
  121. void
  122. incnmaster(const char *arg) {
  123. int i;
  124. if(!arg)
  125. nmaster = NMASTER;
  126. else {
  127. i = atoi(arg);
  128. if((lt->arrange != tile) || (nmaster + i < 1)
  129. || (wah / (nmaster + i) <= 2 * BORDERPX))
  130. return;
  131. nmaster += i;
  132. }
  133. if(sel)
  134. lt->arrange();
  135. else
  136. drawstatus();
  137. }
  138. void
  139. initlayouts(void) {
  140. unsigned int i, w;
  141. lt = &layout[0];
  142. nlayouts = sizeof layout / sizeof layout[0];
  143. for(blw = i = 0; i < nlayouts; i++) {
  144. w = textw(layout[i].symbol);
  145. if(w > blw)
  146. blw = w;
  147. }
  148. }
  149. Client *
  150. nexttiled(Client *c) {
  151. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  152. return c;
  153. }
  154. void
  155. restack(void) {
  156. Client *c;
  157. XEvent ev;
  158. drawstatus();
  159. if(!sel)
  160. return;
  161. if(sel->isfloating || lt->arrange == floating)
  162. XRaiseWindow(dpy, sel->win);
  163. if(lt->arrange != floating) {
  164. if(!sel->isfloating)
  165. XLowerWindow(dpy, sel->win);
  166. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  167. if(c == sel)
  168. continue;
  169. XLowerWindow(dpy, c->win);
  170. }
  171. }
  172. XSync(dpy, False);
  173. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  174. }
  175. void
  176. setlayout(const char *arg) {
  177. int i;
  178. if(!arg) {
  179. for(i = 0; i < nlayouts && lt != &layout[i]; i++);
  180. if(i == nlayouts - 1)
  181. lt = &layout[0];
  182. else
  183. lt = &layout[++i];
  184. }
  185. else {
  186. i = atoi(arg);
  187. if(i < 0 || i >= nlayouts)
  188. return;
  189. lt = &layout[i];
  190. }
  191. if(sel)
  192. lt->arrange();
  193. else
  194. drawstatus();
  195. }
  196. void
  197. togglemax(const char *arg) {
  198. XEvent ev;
  199. if(!sel || (lt->arrange != floating && !sel->isfloating) || sel->isfixed)
  200. return;
  201. if((sel->ismax = !sel->ismax)) {
  202. sel->rx = sel->x;
  203. sel->ry = sel->y;
  204. sel->rw = sel->w;
  205. sel->rh = sel->h;
  206. resize(sel, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
  207. }
  208. else
  209. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  210. drawstatus();
  211. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  212. }
  213. void
  214. zoom(const char *arg) {
  215. unsigned int n;
  216. Client *c;
  217. if(!sel || lt->arrange != tile || sel->isfloating)
  218. return;
  219. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  220. n++;
  221. if((c = sel) == nexttiled(clients))
  222. if(!(c = nexttiled(c->next)))
  223. return;
  224. detach(c);
  225. attach(c);
  226. focus(c);
  227. lt->arrange();
  228. }