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.

337 lines
5.6 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 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. /* static */
  7. static Client *
  8. minclient(void) {
  9. Client *c, *min;
  10. if((clients && clients->isfloat) || arrange == dofloat)
  11. return clients; /* don't touch floating order */
  12. for(min = c = clients; c; c = c->next)
  13. if(c->weight < min->weight)
  14. min = c;
  15. return min;
  16. }
  17. static Client *
  18. nexttiled(Client *c) {
  19. for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
  20. return c;
  21. }
  22. static void
  23. reorder(void) {
  24. Client *c, *newclients, *tail;
  25. newclients = tail = NULL;
  26. while((c = minclient())) {
  27. detach(c);
  28. if(tail) {
  29. c->prev = tail;
  30. tail->next = c;
  31. tail = c;
  32. }
  33. else
  34. tail = newclients = c;
  35. }
  36. clients = newclients;
  37. }
  38. static void
  39. togglemax(Client *c)
  40. {
  41. XEvent ev;
  42. if((c->ismax = !c->ismax)) {
  43. c->rx = c->x; c->x = sx;
  44. c->ry = c->y; c->y = bh;
  45. c->rw = c->w; c->w = sw - 2 * BORDERPX;
  46. c->rh = c->h; c->h = sh - bh - 2 * BORDERPX;
  47. }
  48. else {
  49. c->x = c->rx;
  50. c->y = c->ry;
  51. c->w = c->rw;
  52. c->h = c->rh;
  53. }
  54. resize(c, True, TopLeft);
  55. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  56. }
  57. /* extern */
  58. void (*arrange)(Arg *) = DEFMODE;
  59. void
  60. detach(Client *c) {
  61. if(c->prev)
  62. c->prev->next = c->next;
  63. if(c->next)
  64. c->next->prev = c->prev;
  65. if(c == clients)
  66. clients = c->next;
  67. c->next = c->prev = NULL;
  68. }
  69. void
  70. dofloat(Arg *arg) {
  71. Client *c;
  72. for(c = clients; c; c = c->next) {
  73. if(isvisible(c)) {
  74. resize(c, True, TopLeft);
  75. }
  76. else
  77. ban(c);
  78. }
  79. if(!sel || !isvisible(sel)) {
  80. for(c = stack; c && !isvisible(c); c = c->snext);
  81. focus(c);
  82. }
  83. restack();
  84. }
  85. /* This algorithm is based on a (M)aster area and a (S)tacking area.
  86. * It supports following arrangements:
  87. *
  88. * MMMS MMMM
  89. * MMMS MMMM
  90. * MMMS SSSS
  91. *
  92. * The stacking area can be set to arrange clients vertically or horizontally.
  93. * Through inverting the algorithm it can be used to achieve following setup in
  94. * a dual head environment (due to running two dwm instances concurrently on
  95. * the specific screen):
  96. *
  97. * SMM MMS MMM MMM
  98. * SMM MMS MMM MMM
  99. * SMM MMS SSS SSS
  100. *
  101. * This uses the center of the two screens for master areas.
  102. */
  103. void
  104. dotile(Arg *arg) {
  105. int h, i, n, w;
  106. Client *c;
  107. w = sw - mw;
  108. for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
  109. n++;
  110. if(n > 1)
  111. h = (sh - bh) / (n - 1);
  112. else
  113. h = sh - bh;
  114. for(i = 0, c = clients; c; c = c->next) {
  115. if(isvisible(c)) {
  116. if(c->isfloat) {
  117. resize(c, True, TopLeft);
  118. continue;
  119. }
  120. c->ismax = False;
  121. if(n == 1) {
  122. c->x = sx;
  123. c->y = sy + bh;
  124. c->w = sw - 2 * BORDERPX;
  125. c->h = sh - 2 * BORDERPX - bh;
  126. }
  127. else if(i == 0) {
  128. c->x = sx;
  129. c->y = sy + bh;
  130. c->w = mw - 2 * BORDERPX;
  131. c->h = sh - 2 * BORDERPX - bh;
  132. }
  133. else if(h > bh) {
  134. c->x = sx + mw;
  135. c->y = sy + (i - 1) * h + bh;
  136. c->w = w - 2 * BORDERPX;
  137. if(i + 1 == n)
  138. c->h = sh - c->y - 2 * BORDERPX;
  139. else
  140. c->h = h - 2 * BORDERPX;
  141. }
  142. else { /* fallback if h < bh */
  143. c->x = sx + mw;
  144. c->y = sy + bh;
  145. c->w = w - 2 * BORDERPX;
  146. c->h = sh - 2 * BORDERPX - bh;
  147. }
  148. resize(c, False, TopLeft);
  149. i++;
  150. }
  151. else
  152. ban(c);
  153. }
  154. if(!sel || !isvisible(sel)) {
  155. for(c = stack; c && !isvisible(c); c = c->snext);
  156. focus(c);
  157. }
  158. restack();
  159. }
  160. void
  161. focusnext(Arg *arg) {
  162. Client *c;
  163. if(!sel)
  164. return;
  165. if(!(c = getnext(sel->next)))
  166. c = getnext(clients);
  167. if(c) {
  168. focus(c);
  169. restack();
  170. }
  171. }
  172. void
  173. focusprev(Arg *arg) {
  174. Client *c;
  175. if(!sel)
  176. return;
  177. if(!(c = getprev(sel->prev))) {
  178. for(c = clients; c && c->next; c = c->next);
  179. c = getprev(c);
  180. }
  181. if(c) {
  182. focus(c);
  183. restack();
  184. }
  185. }
  186. Bool
  187. isvisible(Client *c) {
  188. unsigned int i;
  189. for(i = 0; i < ntags; i++)
  190. if(c->tags[i] && seltag[i])
  191. return True;
  192. return False;
  193. }
  194. void
  195. resizecol(Arg *arg) {
  196. unsigned int n;
  197. Client *c;
  198. for(n = 0, c = clients; c; c = c->next)
  199. if(isvisible(c) && !c->isfloat)
  200. n++;
  201. if(!sel || sel->isfloat || n < 2 || (arrange == dofloat))
  202. return;
  203. if(sel == getnext(clients)) {
  204. if(mw + arg->i > sw - 100 || mw + arg->i < 100)
  205. return;
  206. mw += arg->i;
  207. }
  208. else {
  209. if(mw - arg->i > sw - 100 || mw - arg->i < 100)
  210. return;
  211. mw -= arg->i;
  212. }
  213. arrange(NULL);
  214. }
  215. void
  216. restack(void) {
  217. Client *c;
  218. XEvent ev;
  219. if(!sel) {
  220. drawstatus();
  221. return;
  222. }
  223. if(sel->isfloat || arrange == dofloat) {
  224. XRaiseWindow(dpy, sel->win);
  225. XRaiseWindow(dpy, sel->twin);
  226. }
  227. if(arrange != dofloat)
  228. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  229. XLowerWindow(dpy, c->twin);
  230. XLowerWindow(dpy, c->win);
  231. }
  232. drawall();
  233. XSync(dpy, False);
  234. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  235. }
  236. void
  237. togglemode(Arg *arg) {
  238. arrange = (arrange == dofloat) ? dotile : dofloat;
  239. if(sel)
  240. arrange(NULL);
  241. else
  242. drawstatus();
  243. }
  244. void
  245. toggleview(Arg *arg) {
  246. unsigned int i;
  247. seltag[arg->i] = !seltag[arg->i];
  248. for(i = 0; i < ntags && !seltag[i]; i++);
  249. if(i == ntags)
  250. seltag[arg->i] = True; /* cannot toggle last view */
  251. reorder();
  252. arrange(NULL);
  253. }
  254. void
  255. view(Arg *arg) {
  256. unsigned int i;
  257. for(i = 0; i < ntags; i++)
  258. seltag[i] = False;
  259. seltag[arg->i] = True;
  260. reorder();
  261. arrange(NULL);
  262. }
  263. void
  264. viewall(Arg *arg) {
  265. unsigned int i;
  266. for(i = 0; i < ntags; i++)
  267. seltag[i] = True;
  268. reorder();
  269. arrange(NULL);
  270. }
  271. void
  272. zoom(Arg *arg) {
  273. unsigned int n;
  274. Client *c;
  275. if(!sel)
  276. return;
  277. if(sel->isfloat || (arrange == dofloat)) {
  278. togglemax(sel);
  279. return;
  280. }
  281. for(n = 0, c = clients; c; c = c->next)
  282. if(isvisible(c) && !c->isfloat)
  283. n++;
  284. if(n < 2 || (arrange == dofloat))
  285. return;
  286. if((c = sel) == nexttiled(clients))
  287. if(!(c = nexttiled(c->next)))
  288. return;
  289. detach(c);
  290. if(clients)
  291. clients->prev = c;
  292. c->next = clients;
  293. clients = c;
  294. focus(c);
  295. arrange(NULL);
  296. }