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.

255 lines
4.7 KiB

18 years ago
18 years ago
  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. #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->isuntiled)
  29. continue;
  30. c->ismax = False;
  31. nx = wax;
  32. ny = way;
  33. if(i < nmaster) {
  34. ny += i * mh;
  35. nw = mw - 2 * BORDERPX;
  36. nh = mh - 2 * BORDERPX;
  37. }
  38. else { /* tile window */
  39. nx += mw;
  40. nw = tw - 2 * BORDERPX;
  41. if(th > 2 * BORDERPX) {
  42. ny += (i - nmaster) * th;
  43. nh = th - 2 * BORDERPX;
  44. }
  45. else /* fallback if th <= 2 * BORDERPX */
  46. nh = wah - 2 * BORDERPX;
  47. }
  48. resize(c, nx, ny, nw, nh, False);
  49. i++;
  50. }
  51. else {
  52. c->isbanned = True;
  53. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  54. }
  55. if(!sel || !isvisible(sel)) {
  56. for(c = stack; c && !isvisible(c); c = c->snext);
  57. focus(c);
  58. }
  59. restack();
  60. }
  61. LAYOUTS
  62. /* extern */
  63. void
  64. focusclient(const char *arg) {
  65. Client *c;
  66. if(!sel || !arg)
  67. return;
  68. if(atoi(arg) < 0) {
  69. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  70. if(!c) {
  71. for(c = clients; c && c->next; c = c->next);
  72. for(; c && !isvisible(c); c = c->prev);
  73. }
  74. }
  75. else {
  76. for(c = sel->next; c && !isvisible(c); c = c->next);
  77. if(!c)
  78. for(c = clients; c && !isvisible(c); c = c->next);
  79. }
  80. if(c) {
  81. focus(c);
  82. restack();
  83. }
  84. }
  85. void
  86. incmasterw(const char *arg) {
  87. int i;
  88. if(lt->arrange != tile)
  89. return;
  90. if(!arg)
  91. masterw = MASTERWIDTH;
  92. else {
  93. i = atoi(arg);
  94. if(waw * (masterw + i) / 1000 >= waw - 2 * BORDERPX
  95. || waw * (masterw + i) / 1000 <= 2 * BORDERPX)
  96. return;
  97. masterw += i;
  98. }
  99. lt->arrange();
  100. }
  101. void
  102. incnmaster(const char *arg) {
  103. int i;
  104. if(!arg)
  105. nmaster = NMASTER;
  106. else {
  107. i = atoi(arg);
  108. if((lt->arrange != tile) || (nmaster + i < 1)
  109. || (wah / (nmaster + i) <= 2 * BORDERPX))
  110. return;
  111. nmaster += i;
  112. }
  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->isuntiled || !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->isuntiled || lt->arrange == untile)
  142. XRaiseWindow(dpy, sel->win);
  143. if(lt->arrange != untile) {
  144. if(!sel->isuntiled)
  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. 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 != untile && !sel->isuntiled) || 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. untile(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->isuntiled)
  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. }