From: Szymon Lukasz <[email protected]> Update the terminal size upon SIGWINCH delivery.
Signed-off-by: Szymon Lukasz <[email protected]> [Filip: use DECLARE_INSTANCE_CHECKER, qemu_chr_resize_stdio -> stdio_chr_resize] Reviewed-by: Daniel P. Berrangé <[email protected]> Signed-off-by: Filip Hejsek <[email protected]> --- chardev/char-stdio.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/chardev/char-stdio.c b/chardev/char-stdio.c index fe1cbb2d88..4fc8db212c 100644 --- a/chardev/char-stdio.c +++ b/chardev/char-stdio.c @@ -34,7 +34,9 @@ #include "chardev/char-win-stdio.h" #else #include <termios.h> +#include <sys/ioctl.h> #include "chardev/char-fd.h" +#include "qemu/main-loop.h" #endif #ifndef _WIN32 @@ -46,6 +48,14 @@ static bool stdio_in_use; static bool stdio_allow_signal; static bool stdio_echo_state; +typedef struct { + FDChardev parent; + Notifier resize_notifier; +} StdioChardev; + +DECLARE_INSTANCE_CHECKER(StdioChardev, STDIO_CHARDEV, + TYPE_CHARDEV_STDIO) + static void term_exit(void) { if (stdio_in_use) { @@ -85,8 +95,23 @@ static void term_stdio_handler(int sig) stdio_chr_set_echo(NULL, stdio_echo_state); } +static void stdio_chr_resize(Chardev *chr) +{ + struct winsize ws; + if (ioctl(1, TIOCGWINSZ, &ws) != -1) { + qemu_chr_resize(chr, ws.ws_col, ws.ws_row); + } +} + +static void term_resize_notify(Notifier *n, void *data) +{ + StdioChardev *s = container_of(n, StdioChardev, resize_notifier); + stdio_chr_resize(CHARDEV(s)); +} + static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) { + StdioChardev *s = STDIO_CHARDEV(chr); ChardevStdio *opts = backend->u.stdio.data; struct sigaction act; @@ -121,6 +146,10 @@ static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) stdio_allow_signal = !opts->has_signal || opts->signal; stdio_chr_set_echo(chr, false); + s->resize_notifier.notify = term_resize_notify; + sigwinch_add_notifier(&s->resize_notifier); + stdio_chr_resize(chr); + qemu_chr_be_event(chr, CHR_EVENT_OPENED); return true; } @@ -162,6 +191,7 @@ static const TypeInfo char_stdio_type_info = { .parent = TYPE_CHARDEV_WIN_STDIO, #else .parent = TYPE_CHARDEV_FD, + .instance_size = sizeof(StdioChardev), #endif .instance_finalize = char_stdio_finalize, .class_init = char_stdio_class_init, -- 2.55.0
