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.

258 lines
4.8 KiB

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