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.

273 lines
4.5 KiB

18 years ago
18 years ago
18 years ago
18 years ago
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. /* static */
  6. static Client *
  7. nexttiled(Client *c) {
  8. for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
  9. return c;
  10. }
  11. static void
  12. togglemax(Client *c) {
  13. XEvent ev;
  14. if(c->isfixed)
  15. return;
  16. if((c->ismax = !c->ismax)) {
  17. c->rx = c->x; c->x = wax;
  18. c->ry = c->y; c->y = way;
  19. c->rw = c->w; c->w = waw - 2 * BORDERPX;
  20. c->rh = c->h; c->h = wah - 2 * BORDERPX;
  21. }
  22. else {
  23. c->x = c->rx;
  24. c->y = c->ry;
  25. c->w = c->rw;
  26. c->h = c->rh;
  27. }
  28. resize(c, True, TopLeft);
  29. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  30. }
  31. /* extern */
  32. void (*arrange)(void) = DEFMODE;
  33. void
  34. detach(Client *c) {
  35. if(c->prev)
  36. c->prev->next = c->next;
  37. if(c->next)
  38. c->next->prev = c->prev;
  39. if(c == clients)
  40. clients = c->next;
  41. c->next = c->prev = NULL;
  42. }
  43. void
  44. dofloat(void) {
  45. Client *c;
  46. for(c = clients; c; c = c->next) {
  47. if(isvisible(c)) {
  48. resize(c, True, TopLeft);
  49. }
  50. else
  51. ban(c);
  52. }
  53. if(!sel || !isvisible(sel)) {
  54. for(c = stack; c && !isvisible(c); c = c->snext);
  55. focus(c);
  56. }
  57. restack();
  58. }
  59. void
  60. dotile(void) {
  61. unsigned int i, n, mw, mh, tw, th;
  62. Client *c;
  63. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  64. n++;
  65. /* window geoms */
  66. mw = (n > nmaster) ? (waw * master) / 1000 : waw;
  67. mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
  68. tw = waw - mw;
  69. th = (n > nmaster) ? wah / (n - nmaster) : 0;
  70. for(i = 0, c = clients; c; c = c->next)
  71. if(isvisible(c)) {
  72. if(c->isfloat) {
  73. resize(c, True, TopLeft);
  74. continue;
  75. }
  76. c->ismax = False;
  77. c->x = wax;
  78. c->y = way;
  79. if(i < nmaster) {
  80. c->y += i * mh;
  81. c->w = mw - 2 * BORDERPX;
  82. c->h = mh - 2 * BORDERPX;
  83. }
  84. else { /* tile window */
  85. c->x += mw;
  86. c->w = tw - 2 * BORDERPX;
  87. if(th > bh) {
  88. c->y += (i - nmaster) * th;
  89. c->h = th - 2 * BORDERPX;
  90. }
  91. else /* fallback if th < bh */
  92. c->h = wah - 2 * BORDERPX;
  93. }
  94. resize(c, False, TopLeft);
  95. i++;
  96. }
  97. else
  98. ban(c);
  99. if(!sel || !isvisible(sel)) {
  100. for(c = stack; c && !isvisible(c); c = c->snext);
  101. focus(c);
  102. }
  103. restack();
  104. }
  105. void
  106. focusnext(Arg *arg) {
  107. Client *c;
  108. if(!sel)
  109. return;
  110. if(!(c = getnext(sel->next)))
  111. c = getnext(clients);
  112. if(c) {
  113. focus(c);
  114. restack();
  115. }
  116. }
  117. void
  118. focusprev(Arg *arg) {
  119. Client *c;
  120. if(!sel)
  121. return;
  122. if(!(c = getprev(sel->prev))) {
  123. for(c = clients; c && c->next; c = c->next);
  124. c = getprev(c);
  125. }
  126. if(c) {
  127. focus(c);
  128. restack();
  129. }
  130. }
  131. void
  132. incnmaster(Arg *arg) {
  133. if((nmaster + arg->i < 1) || (wah / (nmaster + arg->i) < bh))
  134. return;
  135. nmaster += arg->i;
  136. arrange();
  137. }
  138. Bool
  139. isvisible(Client *c) {
  140. unsigned int i;
  141. for(i = 0; i < ntags; i++)
  142. if(c->tags[i] && seltag[i])
  143. return True;
  144. return False;
  145. }
  146. void
  147. resizemaster(Arg *arg) {
  148. if(arg->i == 0)
  149. master = MASTER;
  150. else {
  151. if(master + arg->i > 950 || master + arg->i < 50)
  152. return;
  153. master += arg->i;
  154. }
  155. arrange();
  156. }
  157. void
  158. restack(void) {
  159. Client *c;
  160. XEvent ev;
  161. if(!sel) {
  162. drawstatus();
  163. return;
  164. }
  165. if(sel->isfloat || arrange == dofloat) {
  166. XRaiseWindow(dpy, sel->win);
  167. XRaiseWindow(dpy, sel->twin);
  168. }
  169. if(arrange != dofloat) {
  170. if(!sel->isfloat) {
  171. XLowerWindow(dpy, sel->twin);
  172. XLowerWindow(dpy, sel->win);
  173. }
  174. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  175. if(c == sel)
  176. continue;
  177. XLowerWindow(dpy, c->twin);
  178. XLowerWindow(dpy, c->win);
  179. }
  180. }
  181. drawall();
  182. XSync(dpy, False);
  183. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  184. }
  185. void
  186. togglefloat(Arg *arg) {
  187. if (!sel || arrange == dofloat)
  188. return;
  189. sel->isfloat = !sel->isfloat;
  190. arrange();
  191. }
  192. void
  193. togglemode(Arg *arg) {
  194. arrange = (arrange == dofloat) ? dotile : dofloat;
  195. if(sel)
  196. arrange();
  197. else
  198. drawstatus();
  199. }
  200. void
  201. toggleview(Arg *arg) {
  202. unsigned int i;
  203. seltag[arg->i] = !seltag[arg->i];
  204. for(i = 0; i < ntags && !seltag[i]; i++);
  205. if(i == ntags)
  206. seltag[arg->i] = True; /* cannot toggle last view */
  207. arrange();
  208. }
  209. void
  210. view(Arg *arg) {
  211. unsigned int i;
  212. for(i = 0; i < ntags; i++)
  213. seltag[i] = (arg->i == -1) ? True : False;
  214. if(arg->i >= 0 && arg->i < ntags)
  215. seltag[arg->i] = True;
  216. arrange();
  217. }
  218. void
  219. zoom(Arg *arg) {
  220. unsigned int n;
  221. Client *c;
  222. if(!sel)
  223. return;
  224. if(sel->isfloat || (arrange == dofloat)) {
  225. togglemax(sel);
  226. return;
  227. }
  228. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  229. n++;
  230. c = sel;
  231. if((arrange != dofloat) && c != nexttiled(clients)) {
  232. detach(c);
  233. if(clients)
  234. clients->prev = c;
  235. c->next = clients;
  236. clients = c;
  237. focus(c);
  238. arrange();
  239. }
  240. }