view.c 4.8 KB

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