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.

262 lines
4.9 KiB

18 years ago
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, remainder;
  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. remainder = (n > nmaster) ? wah - th * (n - nmaster) : 0;
  23. tw = waw - mw;
  24. for(i = 0, c = clients; c; c = c->next)
  25. if(isvisible(c)) {
  26. if(c->isbanned)
  27. XMoveWindow(dpy, c->win, c->x, c->y);
  28. c->isbanned = False;
  29. if(c->isfloating)
  30. continue;
  31. c->ismax = False;
  32. nx = wax;
  33. ny = way;
  34. if(i < nmaster) {
  35. ny += i * mh;
  36. nw = mw - 2 * c->border;
  37. nh = mh - 2 * c->border;
  38. }
  39. else { /* tile window */
  40. nx += mw;
  41. nw = tw - 2 * c->border;
  42. if(th > 2 * c->border) {
  43. ny += (i - nmaster) * th;
  44. nh = th - 2 * c->border;
  45. if (i == n - 1)
  46. nh += remainder;
  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. focustopvisible();
  60. restack();
  61. }
  62. LAYOUTS
  63. /* extern */
  64. void
  65. floating(void) {
  66. Client *c;
  67. for(c = clients; c; c = c->next) {
  68. if(isvisible(c)) {
  69. if(c->isbanned)
  70. XMoveWindow(dpy, c->win, c->x, c->y);
  71. c->isbanned = False;
  72. resize(c, c->x, c->y, c->w, c->h, True);
  73. }
  74. else {
  75. c->isbanned = True;
  76. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  77. }
  78. }
  79. if(!sel || !isvisible(sel))
  80. focustopvisible();
  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. XRaiseWindow(dpy, barwin);
  173. XSync(dpy, False);
  174. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  175. }
  176. void
  177. setlayout(const char *arg) {
  178. int i;
  179. if(!arg) {
  180. for(i = 0; i < nlayouts && lt != &layout[i]; i++);
  181. if(i == nlayouts - 1)
  182. lt = &layout[0];
  183. else
  184. lt = &layout[++i];
  185. }
  186. else {
  187. i = atoi(arg);
  188. if(i < 0 || i >= nlayouts)
  189. return;
  190. lt = &layout[i];
  191. }
  192. if(sel)
  193. lt->arrange();
  194. else
  195. drawstatus();
  196. }
  197. void
  198. togglebar(const char *arg) {
  199. if(bpos == BarOff)
  200. bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
  201. else
  202. bpos = BarOff;
  203. updatebarpos();
  204. lt->arrange();
  205. }
  206. void
  207. togglemax(const char *arg) {
  208. XEvent ev;
  209. if(!sel || (lt->arrange != floating && !sel->isfloating) || sel->isfixed)
  210. return;
  211. if((sel->ismax = !sel->ismax)) {
  212. sel->rx = sel->x;
  213. sel->ry = sel->y;
  214. sel->rw = sel->w;
  215. sel->rh = sel->h;
  216. resize(sel, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
  217. }
  218. else
  219. resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
  220. drawstatus();
  221. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  222. }
  223. void
  224. zoom(const char *arg) {
  225. Client *c;
  226. if(!sel || lt->arrange != tile || sel->isfloating)
  227. return;
  228. if((c = sel) == nexttiled(clients))
  229. if(!(c = nexttiled(c->next)))
  230. return;
  231. detach(c);
  232. attach(c);
  233. focus(c);
  234. lt->arrange();
  235. }