resize.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <string.h>
  19. #include "tmux.h"
  20. #include "tmate.h"
  21. /*
  22. * Recalculate window and session sizes.
  23. *
  24. * Every session has the size of the smallest client it is attached to and
  25. * every window the size of the smallest session it is attached to.
  26. *
  27. * So, when a client is resized or a session attached to or detached from a
  28. * client, the window sizes must be recalculated. For each session, find the
  29. * smallest client it is attached to, and resize it to that size. Then for
  30. * every window, find the smallest session it is attached to, resize it to that
  31. * size and clear and redraw every client with it as the current window.
  32. *
  33. * This is quite inefficient - better/additional data structures are needed
  34. * to make it better.
  35. *
  36. * As a side effect, this function updates the SESSION_UNATTACHED flag. This
  37. * flag is necessary to make sure unattached sessions do not limit the size of
  38. * windows that are attached both to them and to other (attached) sessions.
  39. */
  40. void
  41. recalculate_sizes(void)
  42. {
  43. struct session *s;
  44. struct client *c;
  45. struct window *w;
  46. struct window_pane *wp;
  47. u_int ssx, ssy, has, limit;
  48. int flag, has_status, is_zoomed, forced;
  49. #ifdef TMATE
  50. int tmate_sx = tmate_session.min_sx;
  51. int tmate_sy = tmate_session.min_sy;
  52. #endif
  53. RB_FOREACH(s, sessions, &sessions) {
  54. has_status = options_get_number(s->options, "status");
  55. s->attached = 0;
  56. ssx = ssy = UINT_MAX;
  57. TAILQ_FOREACH(c, &clients, entry) {
  58. if (c->flags & CLIENT_SUSPENDED)
  59. continue;
  60. if (c->session == s) {
  61. #ifdef TMATE
  62. if (c->flags & CLIENT_FORCE_STATUS)
  63. has_status = 1;
  64. #endif
  65. if (c->tty.sx < ssx)
  66. ssx = c->tty.sx;
  67. if (has_status &&
  68. !(c->flags & CLIENT_CONTROL) &&
  69. c->tty.sy > 1 && c->tty.sy - 1 < ssy)
  70. ssy = c->tty.sy - 1;
  71. else if (c->tty.sy < ssy)
  72. ssy = c->tty.sy;
  73. s->attached++;
  74. }
  75. }
  76. #ifdef TMATE
  77. /* We assume a single session */
  78. if (tmate_sx > 0 && tmate_sy > 0) {
  79. if ((u_int)tmate_sx < ssx)
  80. ssx = tmate_sx;
  81. if ((u_int)tmate_sy < ssy)
  82. ssy = tmate_sy;
  83. }
  84. #endif
  85. if (ssx == UINT_MAX || ssy == UINT_MAX) {
  86. s->flags |= SESSION_UNATTACHED;
  87. continue;
  88. }
  89. s->flags &= ~SESSION_UNATTACHED;
  90. if (has_status && ssy == 0)
  91. ssy = 1;
  92. if (s->sx == ssx && s->sy == ssy)
  93. continue;
  94. log_debug("session size %u,%u (was %u,%u)", ssx, ssy, s->sx,
  95. s->sy);
  96. s->sx = ssx;
  97. s->sy = ssy;
  98. }
  99. RB_FOREACH(w, windows, &windows) {
  100. if (w->active == NULL)
  101. continue;
  102. flag = options_get_number(w->options, "aggressive-resize");
  103. ssx = ssy = UINT_MAX;
  104. RB_FOREACH(s, sessions, &sessions) {
  105. if (s->flags & SESSION_UNATTACHED)
  106. continue;
  107. if (flag)
  108. has = s->curw->window == w;
  109. else
  110. has = session_has(s, w);
  111. if (has) {
  112. if (s->sx < ssx)
  113. ssx = s->sx;
  114. if (s->sy < ssy)
  115. ssy = s->sy;
  116. }
  117. }
  118. if (ssx == UINT_MAX || ssy == UINT_MAX)
  119. continue;
  120. forced = 0;
  121. limit = options_get_number(w->options, "force-width");
  122. if (limit >= PANE_MINIMUM && ssx > limit) {
  123. ssx = limit;
  124. forced |= WINDOW_FORCEWIDTH;
  125. }
  126. limit = options_get_number(w->options, "force-height");
  127. if (limit >= PANE_MINIMUM && ssy > limit) {
  128. ssy = limit;
  129. forced |= WINDOW_FORCEHEIGHT;
  130. }
  131. if (w->sx == ssx && w->sy == ssy)
  132. continue;
  133. log_debug("window size %u,%u (was %u,%u)", ssx, ssy, w->sx,
  134. w->sy);
  135. w->flags &= ~(WINDOW_FORCEWIDTH|WINDOW_FORCEHEIGHT);
  136. w->flags |= forced;
  137. is_zoomed = w->flags & WINDOW_ZOOMED;
  138. if (is_zoomed)
  139. window_unzoom(w);
  140. layout_resize(w, ssx, ssy);
  141. window_resize(w, ssx, ssy);
  142. if (is_zoomed && window_pane_visible(w->active))
  143. window_zoom(w->active);
  144. /*
  145. * If the current pane is now not visible, move to the next
  146. * that is.
  147. */
  148. wp = w->active;
  149. while (!window_pane_visible(w->active)) {
  150. w->active = TAILQ_PREV(w->active, window_panes, entry);
  151. if (w->active == NULL)
  152. w->active = TAILQ_LAST(&w->panes, window_panes);
  153. if (w->active == wp)
  154. break;
  155. }
  156. server_redraw_window(w);
  157. notify_window_layout_changed(w);
  158. }
  159. }