fibonacci.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. static void fibonacci(int s)
  2. {
  3. unsigned int nx, ny, nw, nnw, nh, nnh, i, n, mod;
  4. Client *c;
  5. for (n = 0, c = nextvisible(clients); c; c = nextvisible(c->next))
  6. if (!c->minimized)
  7. n++;
  8. /* initial position and dimensions */
  9. nx = wax;
  10. ny = way;
  11. nw = (n == 1) ? waw : screen.mfact * waw;
  12. /* don't waste space dviding by 2 doesn't work for odd numbers
  13. * plus we need space for the border too. therefore set up these
  14. * variables for the next new width/height
  15. */
  16. nnw = waw - nw - 1;
  17. nnh = nh = wah;
  18. /* set the mod factor, 2 for dwindle, 4 for spiral */
  19. mod = s ? 4 : 2;
  20. for (i = 0, c = nextvisible(clients); c; c = nextvisible(c->next)) {
  21. if (c->minimized)
  22. continue;
  23. /* dwindle: even case, spiral: case 0 */
  24. if (i % mod == 0) {
  25. if (i) {
  26. if (s) {
  27. nh = nnh;
  28. ny -= nh;
  29. } else {
  30. ny += nh;
  31. nh = nnh;
  32. }
  33. /* don't adjust the width for the last client */
  34. if (i < n - 1) {
  35. nw /= 2;
  36. nnw -= nw + 1;
  37. }
  38. mvaddch(ny, nx - 1, ACS_LTEE);
  39. }
  40. } else if (i % mod == 1) { /* dwindle: odd case, spiral: case 1 */
  41. nx += nw;
  42. mvvline(ny, nx, ACS_VLINE, nh);
  43. mvaddch(ny, nx, ACS_TTEE);
  44. ++nx;
  45. nw = nnw;
  46. /* don't adjust the height for the last client */
  47. if (i < n - 1) {
  48. nh /= 2;
  49. nnh -= nh;
  50. }
  51. } else if (i % mod == 2 && s) { /* spiral: case 2 */
  52. ny += nh;
  53. nh = nnh;
  54. /* don't adjust the width for the last client */
  55. if (i < n - 1) {
  56. nw /= 2;
  57. nnw -= nw + 1;
  58. nx += nnw;
  59. mvvline(ny, nx, ACS_VLINE, nh);
  60. mvaddch(ny, nx, ACS_TTEE);
  61. ++nx;
  62. } else {
  63. mvaddch(ny, nx - 1, ACS_LTEE);
  64. }
  65. } else if (s) { /* spiral: case 3 */
  66. nw = nnw;
  67. nx -= nw + 1; /* border */
  68. /* don't adjust the height for the last client */
  69. if (i < n - 1) {
  70. nh /= 2;
  71. nnh -= nh;
  72. ny += nnh;
  73. }
  74. mvaddch(ny, nx - 1, ACS_LTEE);
  75. }
  76. resize(c, nx, ny, nw, nh);
  77. i++;
  78. }
  79. }
  80. static void spiral(void)
  81. {
  82. fibonacci(1);
  83. }
  84. static void dwindle(void)
  85. {
  86. fibonacci(0);
  87. }