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.

304 lines
4.4 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 || !isvisible(sel))
  60. sel = getnext(clients);
  61. if(sel)
  62. focus(sel);
  63. else
  64. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  65. restack();
  66. }
  67. void
  68. dotile(Arg *arg)
  69. {
  70. int h, i, n, w;
  71. Client *c;
  72. w = sw - mw;
  73. for(n = 0, c = clients; c; c = c->next)
  74. if(isvisible(c) && !c->isfloat)
  75. n++;
  76. if(n > 1)
  77. h = (sh - bh) / (n - 1);
  78. else
  79. h = sh - bh;
  80. for(i = 0, c = clients; c; c = c->next) {
  81. c->ismax = False;
  82. if(isvisible(c)) {
  83. if(c->isfloat) {
  84. resize(c, True, TopLeft);
  85. continue;
  86. }
  87. if(n == 1) {
  88. c->x = sx;
  89. c->y = sy + bh;
  90. c->w = sw - 2;
  91. c->h = sh - 2 - bh;
  92. }
  93. else if(i == 0) {
  94. c->x = sx;
  95. c->y = sy + bh;
  96. c->w = mw - 2;
  97. c->h = sh - 2 - bh;
  98. }
  99. else if(h > bh) {
  100. c->x = sx + mw;
  101. c->y = sy + (i - 1) * h + bh;
  102. c->w = w - 2;
  103. if(i + 1 == n)
  104. c->h = sh - c->y - 2;
  105. else
  106. c->h = h - 2;
  107. }
  108. else { /* fallback if h < bh */
  109. c->x = sx + mw;
  110. c->y = sy + bh;
  111. c->w = w - 2;
  112. c->h = sh - 2 - bh;
  113. }
  114. resize(c, False, TopLeft);
  115. i++;
  116. }
  117. else
  118. ban(c);
  119. }
  120. if(!sel || !isvisible(sel))
  121. sel = getnext(clients);
  122. if(sel)
  123. focus(sel);
  124. else
  125. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  126. restack();
  127. }
  128. void
  129. focusnext(Arg *arg)
  130. {
  131. Client *c;
  132. if(!sel)
  133. return;
  134. if(!(c = getnext(sel->next)))
  135. c = getnext(clients);
  136. if(c) {
  137. focus(c);
  138. restack();
  139. }
  140. }
  141. void
  142. focusprev(Arg *arg)
  143. {
  144. Client *c;
  145. if(!sel)
  146. return;
  147. if(!(c = getprev(sel->prev))) {
  148. for(c = clients; c && c->next; c = c->next);
  149. c = getprev(c);
  150. }
  151. if(c) {
  152. focus(c);
  153. restack();
  154. }
  155. }
  156. Bool
  157. isvisible(Client *c)
  158. {
  159. unsigned int i;
  160. for(i = 0; i < ntags; i++)
  161. if(c->tags[i] && seltag[i])
  162. return True;
  163. return False;
  164. }
  165. void
  166. restack()
  167. {
  168. static unsigned int nwins = 0;
  169. static Window *wins = NULL;
  170. unsigned int f, fi, m, mi, n;
  171. Client *c;
  172. XEvent ev;
  173. for(f = 0, m = 0, c = clients; c; c = c->next)
  174. if(isvisible(c)) {
  175. if(c->isfloat || arrange == dofloat)
  176. f++;
  177. else
  178. m++;
  179. }
  180. if(!(n = 2 * (f + m))) {
  181. drawstatus();
  182. return;
  183. }
  184. if(nwins < n) {
  185. nwins = n;
  186. wins = erealloc(wins, nwins * sizeof(Window));
  187. }
  188. fi = 0;
  189. mi = 2 * f;
  190. if(sel->isfloat || arrange == dofloat) {
  191. wins[fi++] = sel->twin;
  192. wins[fi++] = sel->win;
  193. }
  194. else {
  195. wins[mi++] = sel->twin;
  196. wins[mi++] = sel->win;
  197. }
  198. for(c = clients; c; c = c->next)
  199. if(isvisible(c) && c != sel) {
  200. if(c->isfloat || arrange == dofloat) {
  201. wins[fi++] = c->twin;
  202. wins[fi++] = c->win;
  203. }
  204. else {
  205. wins[mi++] = c->twin;
  206. wins[mi++] = c->win;
  207. }
  208. }
  209. XRestackWindows(dpy, wins, n);
  210. drawall();
  211. XSync(dpy, False);
  212. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  213. }
  214. void
  215. togglemode(Arg *arg)
  216. {
  217. arrange = (arrange == dofloat) ? dotile : dofloat;
  218. if(sel)
  219. arrange(NULL);
  220. else
  221. drawstatus();
  222. }
  223. void
  224. toggleview(Arg *arg)
  225. {
  226. unsigned int i;
  227. seltag[arg->i] = !seltag[arg->i];
  228. for(i = 0; i < ntags && !seltag[i]; i++);
  229. if(i == ntags)
  230. seltag[arg->i] = True; /* cannot toggle last view */
  231. reorder();
  232. arrange(NULL);
  233. }
  234. void
  235. view(Arg *arg)
  236. {
  237. unsigned int i;
  238. for(i = 0; i < ntags; i++)
  239. seltag[i] = False;
  240. seltag[arg->i] = True;
  241. reorder();
  242. arrange(NULL);
  243. }
  244. void
  245. viewall(Arg *arg)
  246. {
  247. unsigned int i;
  248. for(i = 0; i < ntags; i++)
  249. seltag[i] = True;
  250. reorder();
  251. arrange(NULL);
  252. }
  253. void
  254. zoom(Arg *arg)
  255. {
  256. Client *c = sel;
  257. if(!c || (arrange != dotile) || c->isfloat || c->ismax)
  258. return;
  259. if(c == getnext(clients))
  260. if(!(c = getnext(c->next)))
  261. return;
  262. detach(c);
  263. c->next = clients;
  264. clients->prev = c;
  265. clients = c;
  266. focus(c);
  267. arrange(NULL);
  268. }