vanitygaps.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /* Key binding functions */
  2. static void defaultgaps(const Arg *arg);
  3. static void incrgaps(const Arg *arg);
  4. static void incrigaps(const Arg *arg);
  5. static void incrogaps(const Arg *arg);
  6. static void incrohgaps(const Arg *arg);
  7. static void incrovgaps(const Arg *arg);
  8. static void incrihgaps(const Arg *arg);
  9. static void incrivgaps(const Arg *arg);
  10. static void togglegaps(const Arg *arg);
  11. /* Layouts (delete the ones you do not need) */
  12. static void bstack(Monitor *m);
  13. static void bstackhoriz(Monitor *m);
  14. static void centeredmaster(Monitor *m);
  15. static void centeredfloatingmaster(Monitor *m);
  16. static void deck(Monitor *m);
  17. static void dwindle(Monitor *m);
  18. static void fibonacci(Monitor *m, int s);
  19. static void grid(Monitor *m);
  20. static void nrowgrid(Monitor *m);
  21. static void spiral(Monitor *m);
  22. static void tile(Monitor *m);
  23. /* Internals */
  24. static void getgaps(Monitor *m, int *oh, int *ov, int *ih, int *iv, unsigned int *nc);
  25. static void getfacts(Monitor *m, int msize, int ssize, float *mf, float *sf, int *mr, int *sr);
  26. static void setgaps(int oh, int ov, int ih, int iv);
  27. /* Settings */
  28. #if !PERTAG_PATCH
  29. static int enablegaps = 1;
  30. #endif // PERTAG_PATCH
  31. void
  32. setgaps(int oh, int ov, int ih, int iv)
  33. {
  34. if (oh < 0) oh = 0;
  35. if (ov < 0) ov = 0;
  36. if (ih < 0) ih = 0;
  37. if (iv < 0) iv = 0;
  38. selmon->gappoh = oh;
  39. selmon->gappov = ov;
  40. selmon->gappih = ih;
  41. selmon->gappiv = iv;
  42. arrange(selmon);
  43. }
  44. void
  45. togglegaps(const Arg *arg)
  46. {
  47. #if PERTAG_PATCH
  48. selmon->pertag->enablegaps[selmon->pertag->curtag] = !selmon->pertag->enablegaps[selmon->pertag->curtag];
  49. #else
  50. enablegaps = !enablegaps;
  51. #endif // PERTAG_PATCH
  52. arrange(NULL);
  53. }
  54. void
  55. defaultgaps(const Arg *arg)
  56. {
  57. setgaps(gappoh, gappov, gappih, gappiv);
  58. }
  59. void
  60. incrgaps(const Arg *arg)
  61. {
  62. setgaps(
  63. selmon->gappoh + arg->i,
  64. selmon->gappov + arg->i,
  65. selmon->gappih + arg->i,
  66. selmon->gappiv + arg->i
  67. );
  68. }
  69. void
  70. incrigaps(const Arg *arg)
  71. {
  72. setgaps(
  73. selmon->gappoh,
  74. selmon->gappov,
  75. selmon->gappih + arg->i,
  76. selmon->gappiv + arg->i
  77. );
  78. }
  79. void
  80. incrogaps(const Arg *arg)
  81. {
  82. setgaps(
  83. selmon->gappoh + arg->i,
  84. selmon->gappov + arg->i,
  85. selmon->gappih,
  86. selmon->gappiv
  87. );
  88. }
  89. void
  90. incrohgaps(const Arg *arg)
  91. {
  92. setgaps(
  93. selmon->gappoh + arg->i,
  94. selmon->gappov,
  95. selmon->gappih,
  96. selmon->gappiv
  97. );
  98. }
  99. void
  100. incrovgaps(const Arg *arg)
  101. {
  102. setgaps(
  103. selmon->gappoh,
  104. selmon->gappov + arg->i,
  105. selmon->gappih,
  106. selmon->gappiv
  107. );
  108. }
  109. void
  110. incrihgaps(const Arg *arg)
  111. {
  112. setgaps(
  113. selmon->gappoh,
  114. selmon->gappov,
  115. selmon->gappih + arg->i,
  116. selmon->gappiv
  117. );
  118. }
  119. void
  120. incrivgaps(const Arg *arg)
  121. {
  122. setgaps(
  123. selmon->gappoh,
  124. selmon->gappov,
  125. selmon->gappih,
  126. selmon->gappiv + arg->i
  127. );
  128. }
  129. void
  130. getgaps(Monitor *m, int *oh, int *ov, int *ih, int *iv, unsigned int *nc)
  131. {
  132. unsigned int n, oe, ie;
  133. #if PERTAG_PATCH
  134. oe = ie = selmon->pertag->enablegaps[selmon->pertag->curtag];
  135. #else
  136. oe = ie = enablegaps;
  137. #endif // PERTAG_PATCH
  138. Client *c;
  139. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  140. if (smartgaps && n == 1) {
  141. oe = 0; // outer gaps disabled when only one client
  142. }
  143. *oh = m->gappoh*oe; // outer horizontal gap
  144. *ov = m->gappov*oe; // outer vertical gap
  145. *ih = m->gappih*ie; // inner horizontal gap
  146. *iv = m->gappiv*ie; // inner vertical gap
  147. *nc = n; // number of clients
  148. }
  149. void
  150. getfacts(Monitor *m, int msize, int ssize, float *mf, float *sf, int *mr, int *sr)
  151. {
  152. unsigned int n;
  153. float mfacts = 0, sfacts = 0;
  154. int mtotal = 0, stotal = 0;
  155. Client *c;
  156. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++)
  157. if (n < m->nmaster)
  158. mfacts += c->cfact;
  159. else
  160. sfacts += c->cfact;
  161. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++)
  162. if (n < m->nmaster)
  163. mtotal += msize * (c->cfact / mfacts);
  164. else
  165. stotal += ssize * (c->cfact / sfacts);
  166. *mf = mfacts; // total factor of master area
  167. *sf = sfacts; // total factor of stack area
  168. *mr = msize - mtotal; // the remainder (rest) of pixels after a cfacts master split
  169. *sr = ssize - stotal; // the remainder (rest) of pixels after a cfacts stack split
  170. }
  171. /***
  172. * Layouts
  173. */
  174. /*
  175. * Bottomstack layout + gaps
  176. * https://dwm.suckless.org/patches/bottomstack/
  177. */
  178. static void
  179. bstack(Monitor *m)
  180. {
  181. unsigned int i, n;
  182. int oh, ov, ih, iv;
  183. int mx = 0, my = 0, mh = 0, mw = 0;
  184. int sx = 0, sy = 0, sh = 0, sw = 0;
  185. float mfacts, sfacts;
  186. int mrest, srest;
  187. Client *c;
  188. getgaps(m, &oh, &ov, &ih, &iv, &n);
  189. if (n == 0)
  190. return;
  191. sx = mx = m->wx + ov;
  192. sy = my = m->wy + oh;
  193. sh = mh = m->wh - 2*oh;
  194. mw = m->ww - 2*ov - iv * (MIN(n, m->nmaster) - 1);
  195. sw = m->ww - 2*ov - iv * (n - m->nmaster - 1);
  196. if (m->nmaster && n > m->nmaster) {
  197. sh = (mh - ih) * (1 - m->mfact);
  198. mh = mh - ih - sh;
  199. sx = mx;
  200. sy = my + mh + ih;
  201. }
  202. getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest);
  203. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
  204. if (i < m->nmaster) {
  205. resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0);
  206. mx += WIDTH(c) + iv;
  207. } else {
  208. resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0);
  209. sx += WIDTH(c) + iv;
  210. }
  211. }
  212. }
  213. static void
  214. bstackhoriz(Monitor *m)
  215. {
  216. unsigned int i, n;
  217. int oh, ov, ih, iv;
  218. int mx = 0, my = 0, mh = 0, mw = 0;
  219. int sx = 0, sy = 0, sh = 0, sw = 0;
  220. float mfacts, sfacts;
  221. int mrest, srest;
  222. Client *c;
  223. getgaps(m, &oh, &ov, &ih, &iv, &n);
  224. if (n == 0)
  225. return;
  226. sx = mx = m->wx + ov;
  227. sy = my = m->wy + oh;
  228. mh = m->wh - 2*oh;
  229. sh = m->wh - 2*oh - ih * (n - m->nmaster - 1);
  230. mw = m->ww - 2*ov - iv * (MIN(n, m->nmaster) - 1);
  231. sw = m->ww - 2*ov;
  232. if (m->nmaster && n > m->nmaster) {
  233. sh = (mh - ih) * (1 - m->mfact);
  234. mh = mh - ih - sh;
  235. sy = my + mh + ih;
  236. sh = m->wh - mh - 2*oh - ih * (n - m->nmaster);
  237. }
  238. getfacts(m, mw, sh, &mfacts, &sfacts, &mrest, &srest);
  239. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
  240. if (i < m->nmaster) {
  241. resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0);
  242. mx += WIDTH(c) + iv;
  243. } else {
  244. resize(c, sx, sy, sw - (2*c->bw), sh * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), 0);
  245. sy += HEIGHT(c) + ih;
  246. }
  247. }
  248. }
  249. /*
  250. * Centred master layout + gaps
  251. * https://dwm.suckless.org/patches/centeredmaster/
  252. */
  253. void
  254. centeredmaster(Monitor *m)
  255. {
  256. unsigned int i, n;
  257. int oh, ov, ih, iv;
  258. int mx = 0, my = 0, mh = 0, mw = 0;
  259. int lx = 0, ly = 0, lw = 0, lh = 0;
  260. int rx = 0, ry = 0, rw = 0, rh = 0;
  261. float mfacts = 0, lfacts = 0, rfacts = 0;
  262. int mtotal = 0, ltotal = 0, rtotal = 0;
  263. int mrest = 0, lrest = 0, rrest = 0;
  264. Client *c;
  265. getgaps(m, &oh, &ov, &ih, &iv, &n);
  266. if (n == 0)
  267. return;
  268. /* initialize areas */
  269. mx = m->wx + ov;
  270. my = m->wy + oh;
  271. mh = m->wh - 2*oh - ih * ((!m->nmaster ? n : MIN(n, m->nmaster)) - 1);
  272. mw = m->ww - 2*ov;
  273. lh = m->wh - 2*oh - ih * (((n - m->nmaster) / 2) - 1);
  274. rh = m->wh - 2*oh - ih * (((n - m->nmaster) / 2) - ((n - m->nmaster) % 2 ? 0 : 1));
  275. if (m->nmaster && n > m->nmaster) {
  276. /* go mfact box in the center if more than nmaster clients */
  277. if (n - m->nmaster > 1) {
  278. /* ||<-S->|<---M--->|<-S->|| */
  279. mw = (m->ww - 2*ov - 2*iv) * m->mfact;
  280. lw = (m->ww - mw - 2*ov - 2*iv) / 2;
  281. rw = (m->ww - mw - 2*ov - 2*iv) - lw;
  282. mx += lw + iv;
  283. } else {
  284. /* ||<---M--->|<-S->|| */
  285. mw = (mw - iv) * m->mfact;
  286. lw = 0;
  287. rw = m->ww - mw - iv - 2*ov;
  288. }
  289. lx = m->wx + ov;
  290. ly = m->wy + oh;
  291. rx = mx + mw + iv;
  292. ry = m->wy + oh;
  293. }
  294. /* calculate facts */
  295. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
  296. if (!m->nmaster || n < m->nmaster)
  297. mfacts += c->cfact;
  298. else if ((n - m->nmaster) % 2)
  299. lfacts += c->cfact; // total factor of left hand stack area
  300. else
  301. rfacts += c->cfact; // total factor of right hand stack area
  302. }
  303. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++)
  304. if (!m->nmaster || n < m->nmaster)
  305. mtotal += mh * (c->cfact / mfacts);
  306. else if ((n - m->nmaster) % 2)
  307. ltotal += lh * (c->cfact / lfacts);
  308. else
  309. rtotal += rh * (c->cfact / rfacts);
  310. mrest = mh - mtotal;
  311. lrest = lh - ltotal;
  312. rrest = rh - rtotal;
  313. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
  314. if (!m->nmaster || i < m->nmaster) {
  315. /* nmaster clients are stacked vertically, in the center of the screen */
  316. resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0);
  317. my += HEIGHT(c) + ih;
  318. } else {
  319. /* stack clients are stacked vertically */
  320. if ((i - m->nmaster) % 2 ) {
  321. resize(c, lx, ly, lw - (2*c->bw), lh * (c->cfact / lfacts) + ((i - 2*m->nmaster) < 2*lrest ? 1 : 0) - (2*c->bw), 0);
  322. ly += HEIGHT(c) + ih;
  323. } else {
  324. resize(c, rx, ry, rw - (2*c->bw), rh * (c->cfact / rfacts) + ((i - 2*m->nmaster) < 2*rrest ? 1 : 0) - (2*c->bw), 0);
  325. ry += HEIGHT(c) + ih;
  326. }
  327. }
  328. }
  329. }
  330. void
  331. centeredfloatingmaster(Monitor *m)
  332. {
  333. unsigned int i, n;
  334. float mfacts, sfacts;
  335. float mivf = 1.0; // master inner vertical gap factor
  336. int oh, ov, ih, iv, mrest, srest;
  337. int mx = 0, my = 0, mh = 0, mw = 0;
  338. int sx = 0, sy = 0, sh = 0, sw = 0;
  339. Client *c;
  340. getgaps(m, &oh, &ov, &ih, &iv, &n);
  341. if (n == 0)
  342. return;
  343. sx = mx = m->wx + ov;
  344. sy = my = m->wy + oh;
  345. sh = mh = m->wh - 2*oh;
  346. mw = m->ww - 2*ov - iv*(n - 1);
  347. sw = m->ww - 2*ov - iv*(n - m->nmaster - 1);
  348. if (m->nmaster && n > m->nmaster) {
  349. mivf = 0.8;
  350. /* go mfact box in the center if more than nmaster clients */
  351. if (m->ww > m->wh) {
  352. mw = m->ww * m->mfact - iv*mivf*(MIN(n, m->nmaster) - 1);
  353. mh = m->wh * 0.9;
  354. } else {
  355. mw = m->ww * 0.9 - iv*mivf*(MIN(n, m->nmaster) - 1);
  356. mh = m->wh * m->mfact;
  357. }
  358. mx = m->wx + (m->ww - mw) / 2;
  359. my = m->wy + (m->wh - mh - 2*oh) / 2;
  360. sx = m->wx + ov;
  361. sy = m->wy + oh;
  362. sh = m->wh - 2*oh;
  363. }
  364. getfacts(m, mw, sw, &mfacts, &sfacts, &mrest, &srest);
  365. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  366. if (i < m->nmaster) {
  367. /* nmaster clients are stacked horizontally, in the center of the screen */
  368. resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0);
  369. mx += WIDTH(c) + iv*mivf;
  370. } else {
  371. /* stack clients are stacked horizontally */
  372. resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0);
  373. sx += WIDTH(c) + iv;
  374. }
  375. }
  376. /*
  377. * Deck layout + gaps
  378. * https://dwm.suckless.org/patches/deck/
  379. */
  380. void
  381. deck(Monitor *m)
  382. {
  383. unsigned int i, n;
  384. int oh, ov, ih, iv;
  385. int mx = 0, my = 0, mh = 0, mw = 0;
  386. int sx = 0, sy = 0, sh = 0, sw = 0;
  387. float mfacts, sfacts;
  388. int mrest, srest;
  389. Client *c;
  390. getgaps(m, &oh, &ov, &ih, &iv, &n);
  391. if (n == 0)
  392. return;
  393. sx = mx = m->wx + ov;
  394. sy = my = m->wy + oh;
  395. sh = mh = m->wh - 2*oh - ih * (MIN(n, m->nmaster) - 1);
  396. sw = mw = m->ww - 2*ov;
  397. if (m->nmaster && n > m->nmaster) {
  398. sw = (mw - iv) * (1 - m->mfact);
  399. mw = mw - iv - sw;
  400. sx = mx + mw + iv;
  401. sh = m->wh - 2*oh;
  402. }
  403. getfacts(m, mh, sh, &mfacts, &sfacts, &mrest, &srest);
  404. if (n - m->nmaster > 0) /* override layout symbol */
  405. snprintf(m->ltsymbol, sizeof m->ltsymbol, "D %d", n - m->nmaster);
  406. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  407. if (i < m->nmaster) {
  408. resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0);
  409. my += HEIGHT(c) + ih;
  410. } else {
  411. resize(c, sx, sy, sw - (2*c->bw), sh - (2*c->bw), 0);
  412. }
  413. }
  414. /*
  415. * Fibonacci layout + gaps
  416. * https://dwm.suckless.org/patches/fibonacci/
  417. */
  418. void
  419. fibonacci(Monitor *m, int s)
  420. {
  421. unsigned int i, n;
  422. int nx, ny, nw, nh;
  423. int oh, ov, ih, iv;
  424. int nv, hrest = 0, wrest = 0, r = 1;
  425. Client *c;
  426. getgaps(m, &oh, &ov, &ih, &iv, &n);
  427. if (n == 0)
  428. return;
  429. nx = m->wx + ov;
  430. ny = m->wy + oh;
  431. nw = m->ww - 2*ov;
  432. nh = m->wh - 2*oh;
  433. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next)) {
  434. if (r) {
  435. if ((i % 2 && (nh - ih) / 2 <= (bh + 2*c->bw))
  436. || (!(i % 2) && (nw - iv) / 2 <= (bh + 2*c->bw))) {
  437. r = 0;
  438. }
  439. if (r && i < n - 1) {
  440. if (i % 2) {
  441. nv = (nh - ih) / 2;
  442. hrest = nh - 2*nv - ih;
  443. nh = nv;
  444. } else {
  445. nv = (nw - iv) / 2;
  446. wrest = nw - 2*nv - iv;
  447. nw = nv;
  448. }
  449. if ((i % 4) == 2 && !s)
  450. nx += nw + iv;
  451. else if ((i % 4) == 3 && !s)
  452. ny += nh + ih;
  453. }
  454. if ((i % 4) == 0) {
  455. if (s) {
  456. ny += nh + ih;
  457. nh += hrest;
  458. }
  459. else {
  460. nh -= hrest;
  461. ny -= nh + ih;
  462. }
  463. }
  464. else if ((i % 4) == 1) {
  465. nx += nw + iv;
  466. nw += wrest;
  467. }
  468. else if ((i % 4) == 2) {
  469. ny += nh + ih;
  470. nh += hrest;
  471. if (i < n - 1)
  472. nw += wrest;
  473. }
  474. else if ((i % 4) == 3) {
  475. if (s) {
  476. nx += nw + iv;
  477. nw -= wrest;
  478. } else {
  479. nw -= wrest;
  480. nx -= nw + iv;
  481. nh += hrest;
  482. }
  483. }
  484. if (i == 0) {
  485. if (n != 1) {
  486. nw = (m->ww - iv - 2*ov) - (m->ww - iv - 2*ov) * (1 - m->mfact);
  487. wrest = 0;
  488. }
  489. ny = m->wy + oh;
  490. }
  491. else if (i == 1)
  492. nw = m->ww - nw - iv - 2*ov;
  493. i++;
  494. }
  495. resize(c, nx, ny, nw - (2*c->bw), nh - (2*c->bw), False);
  496. }
  497. }
  498. void
  499. dwindle(Monitor *m)
  500. {
  501. fibonacci(m, 1);
  502. }
  503. void
  504. spiral(Monitor *m)
  505. {
  506. fibonacci(m, 0);
  507. }
  508. /*
  509. * Gappless grid layout + gaps (ironically)
  510. * https://dwm.suckless.org/patches/gaplessgrid/
  511. */
  512. void
  513. gaplessgrid(Monitor *m)
  514. {
  515. unsigned int i, n;
  516. int x, y, cols, rows, ch, cw, cn, rn, rrest, crest; // counters
  517. int oh, ov, ih, iv;
  518. Client *c;
  519. getgaps(m, &oh, &ov, &ih, &iv, &n);
  520. if (n == 0)
  521. return;
  522. /* grid dimensions */
  523. for (cols = 0; cols <= n/2; cols++)
  524. if (cols*cols >= n)
  525. break;
  526. if (n == 5) /* set layout against the general calculation: not 1:2:2, but 2:3 */
  527. cols = 2;
  528. rows = n/cols;
  529. cn = rn = 0; // reset column no, row no, client count
  530. ch = (m->wh - 2*oh - ih * (rows - 1)) / rows;
  531. cw = (m->ww - 2*ov - iv * (cols - 1)) / cols;
  532. rrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows;
  533. crest = (m->ww - 2*ov - iv * (cols - 1)) - cw * cols;
  534. x = m->wx + ov;
  535. y = m->wy + oh;
  536. for (i = 0, c = nexttiled(m->clients); c; i++, c = nexttiled(c->next)) {
  537. if (i/rows + 1 > cols - n%cols) {
  538. rows = n/cols + 1;
  539. ch = (m->wh - 2*oh - ih * (rows - 1)) / rows;
  540. rrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows;
  541. }
  542. resize(c,
  543. x,
  544. y + rn*(ch + ih) + MIN(rn, rrest),
  545. cw + (cn < crest ? 1 : 0) - 2*c->bw,
  546. ch + (rn < rrest ? 1 : 0) - 2*c->bw,
  547. 0);
  548. rn++;
  549. if (rn >= rows) {
  550. rn = 0;
  551. x += cw + ih + (cn < crest ? 1 : 0);
  552. cn++;
  553. }
  554. }
  555. }
  556. /*
  557. * Gridmode layout + gaps
  558. * https://dwm.suckless.org/patches/gridmode/
  559. */
  560. void
  561. grid(Monitor *m)
  562. {
  563. unsigned int i, n;
  564. int cx, cy, cw, ch, cc, cr, chrest, cwrest, cols, rows;
  565. int oh, ov, ih, iv;
  566. Client *c;
  567. getgaps(m, &oh, &ov, &ih, &iv, &n);
  568. /* grid dimensions */
  569. for (rows = 0; rows <= n/2; rows++)
  570. if (rows*rows >= n)
  571. break;
  572. cols = (rows && (rows - 1) * rows >= n) ? rows - 1 : rows;
  573. /* window geoms (cell height/width) */
  574. ch = (m->wh - 2*oh - ih * (rows - 1)) / (rows ? rows : 1);
  575. cw = (m->ww - 2*ov - iv * (cols - 1)) / (cols ? cols : 1);
  576. chrest = (m->wh - 2*oh - ih * (rows - 1)) - ch * rows;
  577. cwrest = (m->ww - 2*ov - iv * (cols - 1)) - cw * cols;
  578. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
  579. cc = i / rows;
  580. cr = i % rows;
  581. cx = m->wx + ov + cc * (cw + iv) + MIN(cc, cwrest);
  582. cy = m->wy + oh + cr * (ch + ih) + MIN(cr, chrest);
  583. resize(c, cx, cy, cw + (cc < cwrest ? 1 : 0) - 2*c->bw, ch + (cr < chrest ? 1 : 0) - 2*c->bw, False);
  584. }
  585. }
  586. /*
  587. * Horizontal grid layout + gaps
  588. * https://dwm.suckless.org/patches/horizgrid/
  589. */
  590. void
  591. horizgrid(Monitor *m) {
  592. Client *c;
  593. unsigned int n, i;
  594. int oh, ov, ih, iv;
  595. int mx = 0, my = 0, mh = 0, mw = 0;
  596. int sx = 0, sy = 0, sh = 0, sw = 0;
  597. int ntop, nbottom = 1;
  598. float mfacts = 0, sfacts = 0;
  599. int mrest, srest, mtotal = 0, stotal = 0;
  600. /* Count windows */
  601. getgaps(m, &oh, &ov, &ih, &iv, &n);
  602. if (n == 0)
  603. return;
  604. if (n <= 2)
  605. ntop = n;
  606. else {
  607. ntop = n / 2;
  608. nbottom = n - ntop;
  609. }
  610. sx = mx = m->wx + ov;
  611. sy = my = m->wy + oh;
  612. sh = mh = m->wh - 2*oh;
  613. sw = mw = m->ww - 2*ov;
  614. if (n > ntop) {
  615. sh = (mh - ih) / 2;
  616. mh = mh - ih - sh;
  617. sy = my + mh + ih;
  618. mw = m->ww - 2*ov - iv * (ntop - 1);
  619. sw = m->ww - 2*ov - iv * (nbottom - 1);
  620. }
  621. /* calculate facts */
  622. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  623. if (i < ntop)
  624. mfacts += c->cfact;
  625. else
  626. sfacts += c->cfact;
  627. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  628. if (i < ntop)
  629. mtotal += mh * (c->cfact / mfacts);
  630. else
  631. stotal += sw * (c->cfact / sfacts);
  632. mrest = mh - mtotal;
  633. srest = sw - stotal;
  634. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  635. if (i < ntop) {
  636. resize(c, mx, my, mw * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), mh - (2*c->bw), 0);
  637. mx += WIDTH(c) + iv;
  638. } else {
  639. resize(c, sx, sy, sw * (c->cfact / sfacts) + ((i - ntop) < srest ? 1 : 0) - (2*c->bw), sh - (2*c->bw), 0);
  640. sx += WIDTH(c) + iv;
  641. }
  642. }
  643. /*
  644. * nrowgrid layout + gaps
  645. * https://dwm.suckless.org/patches/nrowgrid/
  646. */
  647. void
  648. nrowgrid(Monitor *m)
  649. {
  650. unsigned int n;
  651. int ri = 0, ci = 0; /* counters */
  652. int oh, ov, ih, iv; /* vanitygap settings */
  653. unsigned int cx, cy, cw, ch; /* client geometry */
  654. unsigned int uw = 0, uh = 0, uc = 0; /* utilization trackers */
  655. unsigned int cols, rows = m->nmaster + 1;
  656. Client *c;
  657. /* count clients */
  658. getgaps(m, &oh, &ov, &ih, &iv, &n);
  659. /* nothing to do here */
  660. if (n == 0)
  661. return;
  662. /* force 2 clients to always split vertically */
  663. if (FORCE_VSPLIT && n == 2)
  664. rows = 1;
  665. /* never allow empty rows */
  666. if (n < rows)
  667. rows = n;
  668. /* define first row */
  669. cols = n / rows;
  670. uc = cols;
  671. cy = m->wy + oh;
  672. ch = (m->wh - 2*oh - ih*(rows - 1)) / rows;
  673. uh = ch;
  674. for (c = nexttiled(m->clients); c; c = nexttiled(c->next), ci++) {
  675. if (ci == cols) {
  676. uw = 0;
  677. ci = 0;
  678. ri++;
  679. /* next row */
  680. cols = (n - uc) / (rows - ri);
  681. uc += cols;
  682. cy = m->wy + oh + uh + ih;
  683. uh += ch + ih;
  684. }
  685. cx = m->wx + ov + uw;
  686. cw = (m->ww - 2*ov - uw) / (cols - ci);
  687. uw += cw + iv;
  688. resize(c, cx, cy, cw - (2*c->bw), ch - (2*c->bw), 0);
  689. }
  690. }
  691. /*
  692. * Default tile layout + gaps
  693. */
  694. static void
  695. tile(Monitor *m)
  696. {
  697. unsigned int i, n;
  698. int oh, ov, ih, iv;
  699. int mx = 0, my = 0, mh = 0, mw = 0;
  700. int sx = 0, sy = 0, sh = 0, sw = 0;
  701. float mfacts, sfacts;
  702. int mrest, srest;
  703. Client *c;
  704. getgaps(m, &oh, &ov, &ih, &iv, &n);
  705. if (n == 0)
  706. return;
  707. sx = mx = m->wx + ov;
  708. sy = my = m->wy + oh;
  709. mh = m->wh - 2*oh - ih * (MIN(n, m->nmaster) - 1);
  710. sh = m->wh - 2*oh - ih * (n - m->nmaster - 1);
  711. sw = mw = m->ww - 2*ov;
  712. if (m->nmaster && n > m->nmaster) {
  713. sw = (mw - iv) * (1 - m->mfact);
  714. mw = mw - iv - sw;
  715. sx = mx + mw + iv;
  716. }
  717. getfacts(m, mh, sh, &mfacts, &sfacts, &mrest, &srest);
  718. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  719. if (i < m->nmaster) {
  720. resize(c, mx, my, mw - (2*c->bw), mh * (c->cfact / mfacts) + (i < mrest ? 1 : 0) - (2*c->bw), 0);
  721. my += HEIGHT(c) + ih;
  722. } else {
  723. resize(c, sx, sy, sw - (2*c->bw), sh * (c->cfact / sfacts) + ((i - m->nmaster) < srest ? 1 : 0) - (2*c->bw), 0);
  724. sy += HEIGHT(c) + ih;
  725. }
  726. }