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.

290 lines
4.3 KiB

19 years ago
19 years ago
19 years ago
19 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdio.h>
  7. /* static */
  8. static Client *
  9. minclient()
  10. {
  11. Client *c, *min;
  12. for(min = c = clients; c; c = c->next)
  13. if(c->weight < min->weight)
  14. min = c;
  15. return min;
  16. }
  17. static void
  18. reorder()
  19. {
  20. Client *c, *newclients, *tail;
  21. newclients = tail = NULL;
  22. while((c = minclient())) {
  23. detach(c);
  24. if(tail) {
  25. c->prev = tail;
  26. tail->next = c;
  27. tail = c;
  28. }
  29. else
  30. tail = newclients = c;
  31. }
  32. clients = newclients;
  33. }
  34. /* extern */
  35. void (*arrange)(Arg *) = DEFMODE;
  36. void
  37. detach(Client *c)
  38. {
  39. if(c->prev)
  40. c->prev->next = c->next;
  41. if(c->next)
  42. c->next->prev = c->prev;
  43. if(c == clients)
  44. clients = c->next;
  45. c->next = c->prev = NULL;
  46. }
  47. void
  48. dofloat(Arg *arg)
  49. {
  50. Client *c;
  51. for(c = clients; c; c = c->next) {
  52. c->ismax = False;
  53. if(isvisible(c)) {
  54. resize(c, True, TopLeft);
  55. }
  56. else
  57. ban(c);
  58. }
  59. if((sel = getnext(clients)))
  60. focus(sel);
  61. else
  62. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  63. restack();
  64. }
  65. void
  66. dotile(Arg *arg)
  67. {
  68. int h, i, n, w;
  69. Client *c;
  70. w = sw - mw;
  71. for(n = 0, c = clients; c; c = c->next)
  72. if(isvisible(c) && !c->isfloat)
  73. n++;
  74. if(n > 1)
  75. h = (sh - bh) / (n - 1);
  76. else
  77. h = sh - bh;
  78. for(i = 0, c = clients; c; c = c->next) {
  79. c->ismax = False;
  80. if(isvisible(c)) {
  81. if(c->isfloat) {
  82. resize(c, True, TopLeft);
  83. continue;
  84. }
  85. if(n == 1) {
  86. c->x = sx;
  87. c->y = sy + bh;
  88. c->w = sw - 2;
  89. c->h = sh - 2 - bh;
  90. }
  91. else if(i == 0) {
  92. c->x = sx;
  93. c->y = sy + bh;
  94. c->w = mw - 2;
  95. c->h = sh - 2 - bh;
  96. }
  97. else if(h > bh) {
  98. c->x = sx + mw;
  99. c->y = sy + (i - 1) * h + bh;
  100. c->w = w - 2;
  101. if(i + 1 == n)
  102. c->h = sh - c->y - 2;
  103. else
  104. c->h = h - 2;
  105. }
  106. else { /* fallback if h < bh */
  107. c->x = sx + mw;
  108. c->y = sy + bh;
  109. c->w = w - 2;
  110. c->h = sh - 2 - bh;
  111. }
  112. resize(c, False, TopLeft);
  113. i++;
  114. }
  115. else
  116. ban(c);
  117. }
  118. if((sel = getnext(clients)))
  119. focus(sel);
  120. else
  121. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  122. restack();
  123. }
  124. void
  125. focusnext(Arg *arg)
  126. {
  127. Client *c;
  128. if(!sel)
  129. return;
  130. if(!(c = getnext(sel->next)))
  131. c = getnext(clients);
  132. if(c) {
  133. focus(c);
  134. restack();
  135. }
  136. }
  137. void
  138. focusprev(Arg *arg)
  139. {
  140. Client *c;
  141. if(!sel)
  142. return;
  143. if(!(c = getprev(sel->prev))) {
  144. for(c = clients; c && c->next; c = c->next);
  145. c = getprev(c);
  146. }
  147. if(c) {
  148. focus(c);
  149. restack();
  150. }
  151. }
  152. Bool
  153. isvisible(Client *c)
  154. {
  155. unsigned int i;
  156. for(i = 0; i < ntags; i++)
  157. if(c->tags[i] && seltag[i])
  158. return True;
  159. return False;
  160. }
  161. void
  162. restack()
  163. {
  164. static unsigned int nwins = 0;
  165. static Window *wins = NULL;
  166. unsigned int f, fi, m, mi, n;
  167. Client *c;
  168. XEvent ev;
  169. for(f = 0, m = 0, c = clients; c; c = c->next)
  170. if(isvisible(c)) {
  171. if(c->isfloat || arrange == dofloat)
  172. f++;
  173. else
  174. m++;
  175. }
  176. if(!(n = 2 * (f + m))) {
  177. drawstatus();
  178. return;
  179. }
  180. if(nwins < n) {
  181. nwins = n;
  182. wins = erealloc(wins, nwins * sizeof(Window));
  183. }
  184. fi = 0;
  185. mi = 2 * f;
  186. if(sel->isfloat || arrange == dofloat) {
  187. wins[fi++] = sel->twin;
  188. wins[fi++] = sel->win;
  189. }
  190. else {
  191. wins[mi++] = sel->twin;
  192. wins[mi++] = sel->win;
  193. }
  194. for(c = clients; c; c = c->next)
  195. if(isvisible(c) && c != sel) {
  196. if(c->isfloat || arrange == dofloat) {
  197. wins[fi++] = c->twin;
  198. wins[fi++] = c->win;
  199. }
  200. else {
  201. wins[mi++] = c->twin;
  202. wins[mi++] = c->win;
  203. }
  204. }
  205. XRestackWindows(dpy, wins, n);
  206. drawall();
  207. XSync(dpy, False);
  208. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  209. }
  210. void
  211. togglemode(Arg *arg)
  212. {
  213. arrange = (arrange == dofloat) ? dotile : dofloat;
  214. if(sel)
  215. arrange(NULL);
  216. else
  217. drawstatus();
  218. }
  219. void
  220. toggleview(Arg *arg)
  221. {
  222. unsigned int i;
  223. seltag[arg->i] = !seltag[arg->i];
  224. for(i = 0; i < ntags && !seltag[i]; i++);
  225. if(i == ntags)
  226. seltag[arg->i] = True; /* cannot toggle last view */
  227. reorder();
  228. arrange(NULL);
  229. }
  230. void
  231. view(Arg *arg)
  232. {
  233. unsigned int i;
  234. Client *c;
  235. for(i = 0; i < ntags; i++)
  236. seltag[i] = False;
  237. seltag[arg->i] = True;
  238. reorder();
  239. arrange(NULL);
  240. }
  241. void
  242. zoom(Arg *arg)
  243. {
  244. Client *c = sel;
  245. if(!c || (arrange != dotile) || c->isfloat || c->ismax)
  246. return;
  247. if(c == getnext(clients))
  248. if(!(c = getnext(c->next)))
  249. return;
  250. detach(c);
  251. c->next = clients;
  252. clients->prev = c;
  253. clients = c;
  254. focus(c);
  255. arrange(NULL);
  256. }