Skip to contentSkip to conversation

virtio-console: notify about the terminal size

RFC PATCH Superseded Filip Hejsek 4 participants 41 messages Sep 20, 2025 – Oct 13, 2025 Networking

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover virtio-console: notify about the terminal size

The goal of this series is to have a resizable terminal into a guest without having to set up networking and using, e.g. ssh.

I tagged this version as an RFC as we still need to wait for a decision on the swapped rows/cols issue by the virtio maintainers, so it cannot be merged right away. I would still like to request a review especially for the two (small) 'vc' chardev patches, which are new in this version.

The virtio spec allows a virtio-console device to notify the guest about terminal resizes in the host. Linux Kernel implements the driver part of the spec. This series implement the device part in QEMU.

Terminal size support is added for stdio and vc backends. It would make sense to also add size support to the pty and serial backends, however it's not possible to receive resize notifications for tty devices other than a process' controlling terminal. For the serial backend, it might still be useful to query the initial dimensions. (In a previous discussion, only the pty backend was considered, where the initial dimensions are useless.)

The 'vc' chardev changes are incomplete, because this chardev has multiple implementations in QEMU. This series contains adds size support to the generic and gtk/vte implementations. I think I also need to add a spice implementation, please let me know if there is anything else.

I would also like some advice on how to test resizing the virtual console. I tried resizing the window of the sdl backend, but that didn't change the dimensions of the console (instead rescaling the console image to fit the window).

This series also introduces resize messages that can be sent over QMP to notify QEMU about the size of the terminal connented to some chardev. In the libvirt setting, it will allow to implement a resizable terminal for virsh console and other libvirt clients.

For a long time, the Linux implementation of virtio-console had rows and columns swapped. This was fixed in Linux 6.15, but older versions will interpret the dimensions incorrectly. There are two ways this might be resolved:
- It has been proposed to change the virtio spec to match historical Linux behavior. If the change is accepted, the fix would be reverted and we would end up with only two or three broken Linux versions.
- Alternately, the Linux fix would be kept and backported to LTS versions.

In any case, users of broken Linux versions can disable the feature using the console-size property.

Thanks to Markus, Daniel and Max for their feedback on v4, and to Michael for helping with the swapped rows/cols issue.

v5:
- rename chardev-resize -> chardev-window-size-changed
- improve chardev-window-size-changed documentation
- add partial implementation for 'vc' chardev
- config space now returns port 0 size if multiport
- console-size property completely disables size support
- fix misnamed variables in char-win-stdio
- Link to v4: https://lore.kernel.org/qemu-devel/[email protected]

v4:
- changed order of rows and cols fields
- added support for terminal size on Windows
- trace event is also emitted for legacy (non-multiport) drivers
- minor fixes required because of changes in QEMU (DECLARE_INSTANCE_CHECKER, qmp-example)
- updated version numbers ('Since: 10.2', hw_compat_10_1)
- Link to v3: https://lore.kernel.org/qemu-devel/[email protected]

v3:
- add resize messages over QMP, as suggested by Daniel

v2:
- fix adding a new virtio feature bit to the virtio console device

---
Filip Hejsek (3):
char-win-stdio: add support for terminal size ui/console-vc: forward text console size to vc chardev ui/gtk: forward gtk console size to vc chardev

Szymon Lukasz (9):
chardev: add cols, rows fields
chardev: add CHR_EVENT_RESIZE
chardev: add qemu_chr_resize()
char-mux: add support for the terminal size
main-loop: change the handling of SIGWINCH
char-stdio: add support for the terminal size
qmp: add chardev-window-size-changed command
virtio-serial-bus: add terminal resize messages
virtio-console: notify the guest about terminal resizes

 backends/cryptodev-vhost-user.c   |  1 +
 chardev/char-fe.c                 | 13 ++++++++
 chardev/char-mux.c                | 18 ++++++++++-
 chardev/char-stdio.c              | 30 +++++++++++++++++++
 chardev/char-win-stdio.c          | 19 ++++++++++++
 chardev/char.c                    | 26 ++++++++++++++++
 hw/block/vhost-user-blk.c         |  1 +
 hw/char/terminal3270.c            |  1 +
 hw/char/trace-events              |  1 +
 hw/char/virtio-console.c          | 63 ++++++++++++++++++++++++++++++++++++---
 hw/char/virtio-serial-bus.c       | 51 +++++++++++++++++++++++++++++--
 hw/core/machine.c                 |  4 ++-
 hw/ipmi/ipmi_bmc_extern.c         |  1 +
 hw/scsi/vhost-user-scsi.c         |  1 +
 hw/usb/ccid-card-passthru.c       |  1 +
 hw/usb/dev-serial.c               |  1 +
 hw/usb/redirect.c                 |  1 +
 hw/virtio/vhost-user-base.c       |  1 +
 hw/virtio/vhost-user-scmi.c       |  1 +
 include/chardev/char-fe.h         | 10 +++++++
 include/chardev/char.h            |  7 +++++
 include/hw/virtio/virtio-serial.h |  5 ++++
 include/qemu/main-loop.h          |  4 +++
 monitor/hmp.c                     |  1 +
 monitor/qmp.c                     |  1 +
 net/passt.c                       |  1 +
 net/vhost-user.c                  |  1 +
 qapi/char.json                    | 30 +++++++++++++++++++
 ui/console-vc.c                   |  3 ++
 ui/curses.c                       | 11 +++----
 ui/gtk.c                          | 27 +++++++++++++++++
 util/main-loop.c                  | 21 +++++++++++++
 32 files changed, 344 insertions(+), 13 deletions(-)

---
base-commit: 190d5d7fd725ff754f94e8e0cbfb69f279c82b5d
change-id: 20250912-console-resize-96c42140ba08

Best regards,

Signature · 1 line
Filip Hejsek <[email protected]>
#1 lore raw

RFC PATCH v5 1/12 chardev: add cols, rows fields

From: Szymon Lukasz <[email protected]>

These fields should be interpreted as the size of the terminal connected to a given chardev.

---
 chardev/char-fe.c         | 13 +++++++++++++
 include/chardev/char-fe.h | 10 ++++++++++
 include/chardev/char.h    |  1 +
 3 files changed, 24 insertions(+)
chardev/char-fe.c +13 -0
index 158a5f4f551ee49120eee6ebdf772fb450739f47..8622898bd414c208b5a0397b439e18a8bf0b8a92 100644
@@ -329,6 +329,19 @@ void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
     }
 }
 
+void qemu_chr_fe_get_winsize(CharBackend *be, uint16_t *cols, uint16_t *rows)
+{
+    Chardev *chr = be->chr;
+
+    if (chr) {
+        *cols = chr->cols;
+        *rows = chr->rows;
+    } else {
+        *cols = 0;
+        *rows = 0;
+    }
+}
+
 void qemu_chr_fe_set_open(CharBackend *be, bool is_open)
 {
     Chardev *chr = be->chr;
include/chardev/char-fe.h +10 -0
index 8ef05b3dd095bdcaa51b10482261a29b1e8233c7..02d5606fa343ac64a76f48dcd250b5431a0a7761 100644
@@ -158,6 +158,16 @@ int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp);
  */
 void qemu_chr_fe_set_echo(CharBackend *be, bool echo);
 
+/**
+ * qemu_chr_fe_get_winsize:
+ * @cols: the address for storing columns
+ * @rows: the address for storing rows
+ *
+ * Get the size of the terminal connected to the chardev backend.
+ * Returns *cols = *rows = 0, if no associated Chardev.
+ */
+void qemu_chr_fe_get_winsize(CharBackend *be, uint16_t *cols, uint16_t *rows);
+
 /**
  * qemu_chr_fe_set_open:
  * @be: a CharBackend
include/chardev/char.h +1 -0
index 429852f8d9d3c5a7e061acea3561b019b15d658f..336b2e68d099e70a9abe71ef842b160bf12ea932 100644
@@ -65,6 +65,7 @@ struct Chardev {
     char *filename;
     int logfd;
     int be_open;
+    uint16_t cols, rows;
     /* used to coordinate the chardev-change special-case: */
     bool handover_yank_instance;
     GSource *gsource;
Signature · 1 line
2.51.0
Reviewed-by on v5 1/12
#2 lore raw

Re: [PATCH RFC v5 01/12] chardev: add cols, rows fields

Quoted 5 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

These fields should be interpreted as the size of the terminal connected to a given chardev.

Quoted 7 lines
---
 chardev/char-fe.c         | 13 +++++++++++++
 include/chardev/char-fe.h | 10 ++++++++++
 include/chardev/char.h    |  1 +
 3 files changed, 24 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 1/12
#3 lore raw

Re: [PATCH RFC v5 01/12] chardev: add cols, rows fields

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:31AM +0200:

Quoted 7 lines

From: Szymon Lukasz <[email protected]>

These fields should be interpreted as the size of the terminal connected to a given chardev.

Signature · 1 line
Dominique Martinet | Asmadeus
#4 lore raw

RFC PATCH v5 2/12 chardev: add CHR_EVENT_RESIZE

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

Add a new chardev event, CHR_EVENT_RESIZE, which a backend should trigger if it detects the size of the connected terminal changed.

---
 backends/cryptodev-vhost-user.c | 1 +
 chardev/char.c                  | 1 +
 hw/block/vhost-user-blk.c       | 1 +
 hw/char/terminal3270.c          | 1 +
 hw/char/virtio-console.c        | 1 +
 hw/ipmi/ipmi_bmc_extern.c       | 1 +
 hw/scsi/vhost-user-scsi.c       | 1 +
 hw/usb/ccid-card-passthru.c     | 1 +
 hw/usb/dev-serial.c             | 1 +
 hw/usb/redirect.c               | 1 +
 hw/virtio/vhost-user-base.c     | 1 +
 hw/virtio/vhost-user-scmi.c     | 1 +
 include/chardev/char.h          | 4 ++++
 monitor/hmp.c                   | 1 +
 monitor/qmp.c                   | 1 +
 net/passt.c                     | 1 +
 net/vhost-user.c                | 1 +
 17 files changed, 20 insertions(+)
backends/cryptodev-vhost-user.c +1 -0
index cb04e68b022abcc4d794df741dfc25181db660bc..9daca4b0f91e802f67cf3c7af1f8f26479026e1e 100644
@@ -173,6 +173,7 @@ static void cryptodev_vhost_user_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
chardev/char.c +1 -0
index bbebd246c3a46c848847effc775f6390b5078e14..635d19fea4fd4bd0c7f171f055fe940f9f5ebed5 100644
@@ -75,6 +75,7 @@ void qemu_chr_be_event(Chardev *s, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/block/vhost-user-blk.c +1 -0
index c0cc5f6942815aa5e8d972553b3c76a623523ba4..942bc46171824017c98774d6fe793059082385b6 100644
@@ -412,6 +412,7 @@ static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/char/terminal3270.c +1 -0
index d950c172921f91e41fad2b86a8d2a3791f6b0330..2fdf823e3bab3187fa6a4d2214ecd991bac5f473 100644
@@ -172,6 +172,7 @@ static void chr_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/char/virtio-console.c +1 -0
index 0932a3572b78eda866cd7680a32866f2077a91dd..881c862ce9d12027f392031bdea7dbe280ec5493 100644
@@ -168,6 +168,7 @@ static void chr_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/ipmi/ipmi_bmc_extern.c +1 -0
index 9f1ba7b2f8715daf3558fedde358c625c74da20e..b1f28c5b7db71d2cbd828227f176a484629a14f7 100644
@@ -436,6 +436,7 @@ static void chr_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/scsi/vhost-user-scsi.c +1 -0
index 25f2d894e7c827744a41302473712e7b34672536..a8d40046b714e5899262af7382a02611cb7f9a81 100644
@@ -226,6 +226,7 @@ static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/usb/ccid-card-passthru.c +1 -0
index 1eea21a7337747667e9b1db97900c67b061f3729..08fec48dbe00cebef7e73053f2be76932175fb47 100644
@@ -323,6 +323,7 @@ static void ccid_card_vscard_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
     case CHR_EVENT_CLOSED:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/usb/dev-serial.c +1 -0
index 1c116d8b0fef7b23dd076aa6e7e81e52bb51cdbb..dd70db7705def9b93bd69cf176ff0ed7d8cb2cad 100644
@@ -576,6 +576,7 @@ static void usb_serial_event(void *opaque, QEMUChrEvent event)
         break;
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/usb/redirect.c +1 -0
index f516ff42a114d014033ab9f2fce93d1ca48983a6..68c9b48f7a07558441c6d6cd3deccb98e833910b 100644
@@ -1390,6 +1390,7 @@ static void usbredir_chardev_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/virtio/vhost-user-base.c +1 -0
index ff67a020b471b2edfacc6d238ffc50b3ba36afc4..57ff26e775df46135b84449f22929fadd33c684a 100644
@@ -267,6 +267,7 @@ static void vub_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
hw/virtio/vhost-user-scmi.c +1 -0
index f9264c4374e87cb78f8937fda852089487b477e8..180787ec6d94793d4915a26389ae2bece231064e 100644
@@ -214,6 +214,7 @@ static void vu_scmi_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
include/chardev/char.h +4 -0
index 336b2e68d099e70a9abe71ef842b160bf12ea932..45cb6349756ac8072dffab9354108caf90cd3565 100644
@@ -22,6 +22,10 @@ typedef enum {
     CHR_EVENT_OPENED, /* new connection established */
     CHR_EVENT_MUX_IN, /* mux-focus was set to this terminal */
     CHR_EVENT_MUX_OUT, /* mux-focus will move on */
+    CHR_EVENT_RESIZE, /*
+                       * the size of the terminal connected to
+                       * the chardev changed
+                       */
     CHR_EVENT_CLOSED /* connection closed.  NOTE: currently this event
                       * is only bound to the read port of the chardev.
                       * Normally the read port and write port of a
monitor/hmp.c +1 -0
index 34e2b8f748b425e1e4446e8e64aa25b1433d1162..be623a0ef0d0ebdf091d87e4d620175ad60d1566 100644
@@ -1444,6 +1444,7 @@ static void monitor_event(void *opaque, QEMUChrEvent event)
         break;
 
     case CHR_EVENT_BREAK:
+    case CHR_EVENT_RESIZE:
         /* Ignored */
         break;
     }
monitor/qmp.c +1 -0
index cb99a12d94175b395033569e8ead908d9453e759..ff7548422bc60c64e47fc4f24adf092fc81c70b5 100644
@@ -484,6 +484,7 @@ static void monitor_qmp_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
net/passt.c +1 -0
index 32ecffb763b48af4e17b20b98537a4ffb3a6d3ea..2308e3b4d44aa410c374eb1b47e5081c2bef5cec 100644
@@ -421,6 +421,7 @@ static void passt_vhost_user_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
net/vhost-user.c +1 -0
index 8b96157145a7ab719b15b1b70aadac1e88ebfd5f..1c7fdf8267d8c214dcb6ae92e0521747a9f59a6f 100644
@@ -367,6 +367,7 @@ static void net_vhost_user_event(void *opaque, QEMUChrEvent event)
     case CHR_EVENT_BREAK:
     case CHR_EVENT_MUX_IN:
     case CHR_EVENT_MUX_OUT:
+    case CHR_EVENT_RESIZE:
         /* Ignore */
         break;
     }
Signature · 1 line
2.51.0
Reviewed-by on v5 2/12
#5 lore raw

Re: [PATCH RFC v5 02/12] chardev: add CHR_EVENT_RESIZE

Quoted 18 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

Add a new chardev event, CHR_EVENT_RESIZE, which a backend should trigger if it detects the size of the connected terminal changed.

---
 backends/cryptodev-vhost-user.c | 1 +
 chardev/char.c                  | 1 +
 hw/block/vhost-user-blk.c       | 1 +
 hw/char/terminal3270.c          | 1 +
 hw/char/virtio-console.c        | 1 +
 hw/ipmi/ipmi_bmc_extern.c       | 1 +
 hw/scsi/vhost-user-scsi.c       | 1 +
 hw/usb/ccid-card-passthru.c     | 1 +
 hw/usb/dev-serial.c             | 1 +
 hw/usb/redirect.c               | 1 +
Quoted 8 lines
 hw/virtio/vhost-user-base.c     | 1 +
 hw/virtio/vhost-user-scmi.c     | 1 +
 include/chardev/char.h          | 4 ++++
 monitor/hmp.c                   | 1 +
 monitor/qmp.c                   | 1 +
 net/passt.c                     | 1 +
 net/vhost-user.c                | 1 +
 17 files changed, 20 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 2/12
#6 lore raw

Re: [PATCH RFC v5 02/12] chardev: add CHR_EVENT_RESIZE

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:32AM +0200:

Quoted 7 lines

From: Szymon Lukasz <[email protected]>

Add a new chardev event, CHR_EVENT_RESIZE, which a backend should trigger if it detects the size of the connected terminal changed.

--
Dominique Martinet | Asmadeus

#7 lore raw

RFC PATCH v5 3/12 chardev: add qemu_chr_resize()

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

This function should be called whenever we learn about a new size of the terminal connected to a chardev.

---
 chardev/char.c         | 11 +++++++++++
 include/chardev/char.h |  2 ++
 2 files changed, 13 insertions(+)
chardev/char.c +11 -0
index 635d19fea4fd4bd0c7f171f055fe940f9f5ebed5..b45d79cb9b57643827eb7479257fdda2cf6b0434 100644
@@ -351,6 +351,17 @@ int qemu_chr_wait_connected(Chardev *chr, Error **errp)
     return 0;
 }
 
+void qemu_chr_resize(Chardev *chr, uint16_t cols, uint16_t rows)
+{
+    if (cols != chr->cols || rows != chr->rows) {
+        chr->cols = cols;
+        chr->rows = rows;
+        if (chr->be_open) {
+            qemu_chr_be_event(chr, CHR_EVENT_RESIZE);
+        }
+    }
+}
+
 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
                                 bool permit_mux_mon)
 {
include/chardev/char.h +2 -0
index 45cb6349756ac8072dffab9354108caf90cd3565..1e69b038241074d627ebb7f096e98aee9953ebdf 100644
@@ -232,6 +232,8 @@ int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all);
 #define qemu_chr_write_all(s, buf, len) qemu_chr_write(s, buf, len, true)
 int qemu_chr_wait_connected(Chardev *chr, Error **errp);
 
+void qemu_chr_resize(Chardev *chr, uint16_t cols, uint16_t rows);
+
 #define TYPE_CHARDEV "chardev"
 OBJECT_DECLARE_TYPE(Chardev, ChardevClass, CHARDEV)
 
Signature · 1 line
2.51.0
Reviewed-by on v5 3/12
#8 lore raw

Re: [PATCH RFC v5 03/12] chardev: add qemu_chr_resize()

Quoted 11 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

This function should be called whenever we learn about a new size of the terminal connected to a chardev.

---
 chardev/char.c         | 11 +++++++++++
 include/chardev/char.h |  2 ++
 2 files changed, 13 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 3/12
#9 lore raw

Re: [PATCH RFC v5 03/12] chardev: add qemu_chr_resize()

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:33AM +0200:

Quoted 7 lines

From: Szymon Lukasz <[email protected]>

This function should be called whenever we learn about a new size of the terminal connected to a chardev.

Signature · 1 line
Dominique Martinet | Asmadeus
#10 lore raw

RFC PATCH v5 4/12 char-mux: add support for the terminal size

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

The terminal size of a mux chardev should be the same as the real chardev, so listen for CHR_EVENT_RESIZE to be up to date.

We forward CHR_EVENT_RESIZE only to the focused frontend. This means frontends should update their view of the terminal size on receiving CHR_EVENT_MUX_IN.

---
 chardev/char-mux.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
chardev/char-mux.c +17 -1
index 6b36290e2c49f579580d2abb5aa552806f019d4a..4d3d05b82f13e002c766142f9d9c24977b8b9bd2 100644
@@ -264,9 +264,24 @@ void mux_chr_send_all_event(Chardev *chr, QEMUChrEvent event)
     }
 }
 
+static void mux_update_winsize(Chardev *chr)
+{
+    MuxChardev *d = MUX_CHARDEV(chr);
+    uint16_t cols, rows;
+
+    qemu_chr_fe_get_winsize(&d->chr, &cols, &rows);
+    qemu_chr_resize(chr, cols, rows);
+}
+
 static void mux_chr_event(void *opaque, QEMUChrEvent event)
 {
-    mux_chr_send_all_event(CHARDEV(opaque), event);
+    Chardev *chr = CHARDEV(opaque);
+
+    if (event == CHR_EVENT_RESIZE) {
+        mux_update_winsize(chr);
+    } else {
+        mux_chr_send_all_event(chr, event);
+    }
 }
 
 static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond)
@@ -382,6 +397,7 @@ static void qemu_chr_open_mux(Chardev *chr,
      */
     *be_opened = muxes_opened;
     qemu_chr_fe_init(&d->chr, drv, errp);
+    mux_update_winsize(chr);
 }
 
 static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
Signature · 1 line
2.51.0
Reviewed-by on v5 4/12
#11 lore raw

Re: [PATCH RFC v5 04/12] char-mux: add support for the terminal size

Quoted 9 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

The terminal size of a mux chardev should be the same as the real chardev, so listen for CHR_EVENT_RESIZE to be up to date.

We forward CHR_EVENT_RESIZE only to the focused frontend. This means frontends should update their view of the terminal size on receiving CHR_EVENT_MUX_IN.

Quoted 5 lines
---
 chardev/char-mux.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 4/12
#12 lore raw

Re: [PATCH RFC v5 04/12] char-mux: add support for the terminal size

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:34AM +0200:

Quoted 11 lines

From: Szymon Lukasz <[email protected]>

The terminal size of a mux chardev should be the same as the real chardev, so listen for CHR_EVENT_RESIZE to be up to date.

We forward CHR_EVENT_RESIZE only to the focused frontend. This means frontends should update their view of the terminal size on receiving CHR_EVENT_MUX_IN.

--
Dominique Martinet | Asmadeus

#13 lore raw

RFC PATCH v5 5/12 main-loop: change the handling of SIGWINCH

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

Block SIGWINCH, so it is delivered only via signalfd. Install a handler that uses NotifierList to tell interested parties about SIGWINCH delivery.

---
 include/qemu/main-loop.h |  4 ++++
 ui/curses.c              | 11 ++++++-----
 util/main-loop.c         | 21 +++++++++++++++++++++
 3 files changed, 31 insertions(+), 5 deletions(-)
include/qemu/main-loop.h +4 -0
index 4e2436b1968b5c513f7d4e84e010b0d4fb31a1b1..7cc45c3a274434020fe33b1ca0a4d839de994e97 100644
@@ -431,4 +431,8 @@ typedef struct MainLoopPoll {
 void main_loop_poll_add_notifier(Notifier *notify);
 void main_loop_poll_remove_notifier(Notifier *notify);
 
+#ifndef _WIN32
+void sigwinch_add_notifier(Notifier *n);
+#endif
+
 #endif
ui/curses.c +6 -5
index 161f78c35c32fc03ad576d2bd8e91bdfe09b265d..d1b308d5f8051e99f12f4d32435a04e294060a10 100644
@@ -33,6 +33,7 @@
 #include <iconv.h>
 
 #include "qapi/error.h"
+#include "qemu/main-loop.h"
 #include "qemu/module.h"
 #include "ui/console.h"
 #include "ui/input.h"
@@ -149,7 +150,7 @@ static void curses_resize(DisplayChangeListener *dcl,
 }
 
 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
-static volatile sig_atomic_t got_sigwinch;
+static bool got_sigwinch;
 static void curses_winch_check(void)
 {
     struct winsize {
@@ -172,17 +173,17 @@ static void curses_winch_check(void)
     invalidate = 1;
 }
 
-static void curses_winch_handler(int signum)
+static void curses_winch_handler(Notifier *n, void *data)
 {
     got_sigwinch = true;
 }
 
 static void curses_winch_init(void)
 {
-    struct sigaction old, winch = {
-        .sa_handler  = curses_winch_handler,
+    static Notifier n = {
+        .notify = curses_winch_handler
     };
-    sigaction(SIGWINCH, &winch, &old);
+    sigwinch_add_notifier(&n);
 }
 #else
 static void curses_winch_check(void) {}
util/main-loop.c +21 -0
index 51aeb2432e77eae7081c6945e21812acc71b5f37..db4bb9c88dade805bc98322c1a053c65e9e97f7e 100644
@@ -100,6 +100,7 @@ static int qemu_signal_init(Error **errp)
     sigaddset(&set, SIGIO);
     sigaddset(&set, SIGALRM);
     sigaddset(&set, SIGBUS);
+    sigaddset(&set, SIGWINCH);
     /* SIGINT cannot be handled via signalfd, so that ^C can be used
      * to interrupt QEMU when it is being run under gdb.  SIGHUP and
      * SIGTERM are also handled asynchronously, even though it is not
@@ -121,6 +122,26 @@ static int qemu_signal_init(Error **errp)
     return 0;
 }
 
+static NotifierList sigwinch_notifiers =
+    NOTIFIER_LIST_INITIALIZER(sigwinch_notifiers);
+
+static void sigwinch_handler(int signum)
+{
+    notifier_list_notify(&sigwinch_notifiers, NULL);
+}
+
+void sigwinch_add_notifier(Notifier *n)
+{
+    if (notifier_list_empty(&sigwinch_notifiers)) {
+        struct sigaction action = {
+            .sa_handler = sigwinch_handler,
+        };
+        sigaction(SIGWINCH, &action, NULL);
+    }
+
+    notifier_list_add(&sigwinch_notifiers, n);
+}
+
 #else /* _WIN32 */
 
 static int qemu_signal_init(Error **errp)
Signature · 1 line
2.51.0
Reviewed-by on v5 5/12
#14 lore raw

Re: [PATCH RFC v5 05/12] main-loop: change the handling of SIGWINCH

Quoted 6 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

Block SIGWINCH, so it is delivered only via signalfd. Install a handler that uses NotifierList to tell interested parties about SIGWINCH delivery.

Quoted 7 lines
---
 include/qemu/main-loop.h |  4 ++++
 ui/curses.c              | 11 ++++++-----
 util/main-loop.c         | 21 +++++++++++++++++++++
 3 files changed, 31 insertions(+), 5 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 5/12
#15 lore raw

Re: [PATCH RFC v5 05/12] main-loop: change the handling of SIGWINCH

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:35AM +0200:

Quoted 8 lines

From: Szymon Lukasz <[email protected]>

Block SIGWINCH, so it is delivered only via signalfd. Install a handler that uses NotifierList to tell interested parties about SIGWINCH delivery.

Signature · 1 line
Dominique Martinet | Asmadeus
#16 lore raw

RFC PATCH v5 6/12 char-stdio: add support for the terminal size

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

Update the terminal size upon SIGWINCH delivery.

[Filip: use DECLARE_INSTANCE_CHECKER]
---
 chardev/char-stdio.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
chardev/char-stdio.c +30 -0
index 48db8d2f30fcf0b481c79ea69aab720454596a05..b3475391f088f1e570b74cc40e30f679dbe9b574 100644
@@ -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,11 +95,26 @@ static void term_stdio_handler(int sig)
     qemu_chr_set_echo_stdio(NULL, stdio_echo_state);
 }
 
+static void qemu_chr_resize_stdio(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);
+    qemu_chr_resize_stdio(CHARDEV(s));
+}
+
 static void qemu_chr_open_stdio(Chardev *chr,
                                 ChardevBackend *backend,
                                 bool *be_opened,
                                 Error **errp)
 {
+    StdioChardev *s = STDIO_CHARDEV(chr);
     ChardevStdio *opts = backend->u.stdio.data;
     struct sigaction act;
 
@@ -121,6 +146,10 @@ static void qemu_chr_open_stdio(Chardev *chr,
 
     stdio_allow_signal = !opts->has_signal || opts->signal;
     qemu_chr_set_echo_stdio(chr, false);
+
+    qemu_chr_resize_stdio(chr);
+    s->resize_notifier.notify = term_resize_notify;
+    sigwinch_add_notifier(&s->resize_notifier);
 }
 #endif
 
@@ -160,6 +189,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,
Signature · 1 line
2.51.0
Reviewed-by on v5 6/12
#17 lore raw

Re: [PATCH RFC v5 06/12] char-stdio: add support for the terminal size

Quoted 10 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

Update the terminal size upon SIGWINCH delivery.

[Filip: use DECLARE_INSTANCE_CHECKER]
---
 chardev/char-stdio.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 6/12
#18 lore raw

Re: [PATCH RFC v5 06/12] char-stdio: add support for the terminal size

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:36AM +0200:

Quoted 7 lines

From: Szymon Lukasz <[email protected]>

Update the terminal size upon SIGWINCH delivery.

[Filip: use DECLARE_INSTANCE_CHECKER]
Signature · 1 line
Dominique Martinet | Asmadeus
#19 lore raw

RFC PATCH v5 7/12 qmp: add chardev-window-size-changed command

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

The managment software can use this command to notify QEMU about the size of the terminal connected to a chardev, QEMU can then forward this information to the guest if the chardev is connected to a virtio console device.

[Filip: rename command, change documentation]
---
 chardev/char.c | 14 ++++++++++++++
 qapi/char.json | 30 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
chardev/char.c +14 -0
index b45d79cb9b57643827eb7479257fdda2cf6b0434..f3aad545afe3d325aac388015b3d5517c2e48f0d 100644
@@ -1269,6 +1269,20 @@ bool qmp_add_client_char(int fd, bool has_skipauth, bool skipauth,
     return true;
 }
 
+void qmp_chardev_window_size_changed(const char *id, uint16_t cols,
+                                     uint16_t rows, Error **errp)
+{
+    Chardev *chr;
+
+    chr = qemu_chr_find(id);
+    if (chr == NULL) {
+        error_setg(errp, "Chardev '%s' not found", id);
+        return;
+    }
+
+    qemu_chr_resize(chr, cols, rows);
+}
+
 /*
  * Add a timeout callback for the chardev (in milliseconds), return
  * the GSource object created. Please use this to add timeout hook for
qapi/char.json +30 -0
index f0a53f742c8bee24c377551803a864fd36ac78cf..93b0d239276077d808af2e5479d539728377a99f 100644
@@ -874,6 +874,36 @@
 { 'command': 'chardev-send-break',
   'data': { 'id': 'str' } }
 
+##
+# @chardev-window-size-changed:
+#
+# Notifies a chardev about the current size of the terminal connected
+# to this chardev. The information will be forwarded to the guest if
+# the chardev is connected to a virtio console device.
+#
+# The initial size is 0x0, which should be interpreted as an unknown size.
+#
+# Some backends detect the terminal size automatically, in which case
+# the size may unpredictably revert to the detected one at any time.
+#
+# @id: the chardev's ID, must exist
+#
+# @cols: the number of columns
+#
+# @rows: the number of rows
+#
+# Since: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "chardev-window-size-changed", "arguments": { "id": "foo", "cols": 80, "rows": 24 } }
+#     <- { "return": {} }
+##
+{ 'command': 'chardev-window-size-changed',
+  'data': { 'id': 'str',
+            'cols': 'uint16',
+            'rows': 'uint16' } }
+
 ##
 # @VSERPORT_CHANGE:
 #
Signature · 1 line
2.51.0
Acked-by on v5 7/12
#20 lore raw

Re: [PATCH RFC v5 07/12] qmp: add chardev-window-size-changed command

Quoted 50 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

The managment software can use this command to notify QEMU about the size of the terminal connected to a chardev, QEMU can then forward this information to the guest if the chardev is connected to a virtio console device.

[Filip: rename command, change documentation]
---
 chardev/char.c | 14 ++++++++++++++
 qapi/char.json | 30 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
chardev/char.c +14 -0
index b45d79cb9b57643827eb7479257fdda2cf6b0434..f3aad545afe3d325aac388015b3d5517c2e48f0d 100644
@@ -1269,6 +1269,20 @@ bool qmp_add_client_char(int fd, bool has_skipauth, bool skipauth,
     return true;
 }
 
+void qmp_chardev_window_size_changed(const char *id, uint16_t cols,
+                                     uint16_t rows, Error **errp)
+{
+    Chardev *chr;
+
+    chr = qemu_chr_find(id);
+    if (chr == NULL) {
+        error_setg(errp, "Chardev '%s' not found", id);
+        return;
+    }
+
+    qemu_chr_resize(chr, cols, rows);
+}
+
 /*
  * Add a timeout callback for the chardev (in milliseconds), return
  * the GSource object created. Please use this to add timeout hook for
qapi/char.json +1 -0
index f0a53f742c8bee24c377551803a864fd36ac78cf..93b0d239276077d808af2e5479d539728377a99f 100644
@@ -874,6 +874,36 @@
 { 'command': 'chardev-send-break',
   'data': { 'id': 'str' } }
 
+##

qapi/char.json line 887

Quoted 10 lines
qapi/char.json +10 -0
+# @chardev-window-size-changed:
+#
+# Notifies a chardev about the current size of the terminal connected
+# to this chardev. The information will be forwarded to the guest if
+# the chardev is connected to a virtio console device.
+#
+# The initial size is 0x0, which should be interpreted as an unknown size.
+#
+# Some backends detect the terminal size automatically, in which case
+# the size may unpredictably revert to the detected one at any time.

Nicely done now!

docs/devel/qapi-code-gen.rst:

For legibility, wrap text paragraphs so every line is at most 70 characters long.

Separate sentences with two spaces.

qapi/char.json line 889

Quoted 2 lines
qapi/char.json +2 -0
+#
+# @id: the chardev's ID, must exist

Let's drop ", must exist" for consistency with ID descriptions elsewhere.

Quoted 10 lines
qapi/char.json +10 -0
+#
+# @cols: the number of columns
+#
+# @rows: the number of rows
+#
+# Since: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "chardev-window-size-changed", "arguments": { "id": "foo", "cols": 80, "rows": 24 } }

qapi/char.json line 909

Quoted 10 lines
qapi/char.json +7 -0
+#     <- { "return": {} }
+##
+{ 'command': 'chardev-window-size-changed',
+  'data': { 'id': 'str',
+            'cols': 'uint16',
+            'rows': 'uint16' } }
+
 ##
 # @VSERPORT_CHANGE:
 #
#21 lore raw

Re: [PATCH RFC v5 07/12] qmp: add chardev-window-size-changed command

Quoted 70 lines · Markus Armbruster
Quoted 50 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

The managment software can use this command to notify QEMU about the size of the terminal connected to a chardev, QEMU can then forward this information to the guest if the chardev is connected to a virtio console device.

[Filip: rename command, change documentation]
---
 chardev/char.c | 14 ++++++++++++++
 qapi/char.json | 30 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
chardev/char.c +14 -0
index b45d79cb9b57643827eb7479257fdda2cf6b0434..f3aad545afe3d325aac388015b3d5517c2e48f0d 100644
@@ -1269,6 +1269,20 @@ bool qmp_add_client_char(int fd, bool has_skipauth, bool skipauth,
     return true;
 }
 
+void qmp_chardev_window_size_changed(const char *id, uint16_t cols,
+                                     uint16_t rows, Error **errp)
+{
+    Chardev *chr;
+
+    chr = qemu_chr_find(id);
+    if (chr == NULL) {
+        error_setg(errp, "Chardev '%s' not found", id);
+        return;
+    }
+
+    qemu_chr_resize(chr, cols, rows);
+}
+
 /*
  * Add a timeout callback for the chardev (in milliseconds), return
  * the GSource object created. Please use this to add timeout hook for
qapi/char.json +1 -0
index f0a53f742c8bee24c377551803a864fd36ac78cf..93b0d239276077d808af2e5479d539728377a99f 100644
@@ -874,6 +874,36 @@
 { 'command': 'chardev-send-break',
   'data': { 'id': 'str' } }
 
+##

qapi/char.json line 887

Quoted 10 lines
qapi/char.json +10 -0
+# @chardev-window-size-changed:
+#
+# Notifies a chardev about the current size of the terminal connected
+# to this chardev. The information will be forwarded to the guest if
+# the chardev is connected to a virtio console device.
+#
+# The initial size is 0x0, which should be interpreted as an unknown size.
+#
+# Some backends detect the terminal size automatically, in which case
+# the size may unpredictably revert to the detected one at any time.

Nicely done now!

docs/devel/qapi-code-gen.rst:

For legibility, wrap text paragraphs so every line is at most 70 characters long.

Quoted 7 lines

Separate sentences with two spaces.

qapi/char.json line 889

Quoted 2 lines
qapi/char.json +2 -0
+#
+# @id: the chardev's ID, must exist

Let's drop ", must exist" for consistency with ID descriptions elsewhere.

All chardev @id parameters in qapi/char.json have ", must exist" (although you're right that nothing else has it).

Quoted 22 lines
Quoted 10 lines
qapi/char.json +10 -0
+#
+# @cols: the number of columns
+#
+# @rows: the number of rows
+#
+# Since: 10.2
+#
+# .. qmp-example::
+#
+#     -> { "execute": "chardev-window-size-changed", "arguments": { "id": "foo", "cols": 80, "rows": 24 } }

qapi/char.json line 909

Quoted 10 lines
qapi/char.json +7 -0
+#     <- { "return": {} }
+##
+{ 'command': 'chardev-window-size-changed',
+  'data': { 'id': 'str',
+            'cols': 'uint16',
+            'rows': 'uint16' } }
+
 ##
 # @VSERPORT_CHANGE:
 #
#22 lore raw

Re: [PATCH RFC v5 07/12] qmp: add chardev-window-size-changed command

Quoted 14 lines · Filip Hejsek
Quoted 13 lines · Markus Armbruster
Quoted 11 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

The managment software can use this command to notify QEMU about the size of the terminal connected to a chardev, QEMU can then forward this information to the guest if the chardev is connected to a virtio console device.

[Filip: rename command, change documentation]

[...]

Quoted 29 lines
Quoted 29 lines
Quoted 9 lines
qapi/char.json +1 -0
index f0a53f742c8bee24c377551803a864fd36ac78cf..93b0d239276077d808af2e5479d539728377a99f 100644
@@ -874,6 +874,36 @@
 { 'command': 'chardev-send-break',
   'data': { 'id': 'str' } }
 
+##

qapi/char.json line 887

Quoted 10 lines
qapi/char.json +10 -0
+# @chardev-window-size-changed:
+#
+# Notifies a chardev about the current size of the terminal connected
+# to this chardev. The information will be forwarded to the guest if
+# the chardev is connected to a virtio console device.
+#
+# The initial size is 0x0, which should be interpreted as an unknown size.
+#
+# Some backends detect the terminal size automatically, in which case
+# the size may unpredictably revert to the detected one at any time.

Nicely done now!

docs/devel/qapi-code-gen.rst:

For legibility, wrap text paragraphs so every line is at most 70 characters long.

Separate sentences with two spaces.

Quoted 8 lines
Quoted 5 lines

qapi/char.json line 889

Quoted 2 lines
qapi/char.json +2 -0
+#
+# @id: the chardev's ID, must exist

Let's drop ", must exist" for consistency with ID descriptions elsewhere.

All chardev @id parameters in qapi/char.json have ", must exist" (although you're right that nothing else has it).

Right. Feel free to stay locally consistent then.

[...]

Reviewed-by on v5 7/12
#23 lore raw

Re: [PATCH RFC v5 07/12] qmp: add chardev-window-size-changed command

In reply to Filip Hejsek
Quoted 7 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

The managment software can use this command to notify QEMU about the size of the terminal connected to a chardev, QEMU can then forward this information to the guest if the chardev is connected to a virtio console device.

Quoted 8 lines
[Filip: rename command, change documentation]
---
 chardev/char.c | 14 ++++++++++++++
 qapi/char.json | 30 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 7/12
#24 lore raw

Re: [PATCH RFC v5 07/12] qmp: add chardev-window-size-changed command

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:37AM +0200:

Quoted 11 lines

From: Szymon Lukasz <[email protected]>

The managment software can use this command to notify QEMU about the size of the terminal connected to a chardev, QEMU can then forward this information to the guest if the chardev is connected to a virtio console device.

[Filip: rename command, change documentation]

(checked with stty in guest after running the command)

Signature · 1 line
Dominique Martinet | Asmadeus
#25 lore raw

Re: [PATCH RFC v5 07/12] qmp: add chardev-window-size-changed command

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:37AM +0200:

qapi/char.json line 884

Quoted 8 lines
qapi/char.json +8 -0
+##
+# @chardev-window-size-changed:
+#
+# Notifies a chardev about the current size of the terminal connected
+# to this chardev. The information will be forwarded to the guest if
+# the chardev is connected to a virtio console device.
+#
+# The initial size is 0x0, which should be interpreted as an unknown size.

Actually coming back to this: what does that "initial size" mean?

If there had been a qmp command to get the current size I'd understand, but this is set-only as far as I can see, so there is no initial size?

(not that it matters)

Thanks,

Signature · 1 line
Dominique Martinet | Asmadeus
#26 lore raw

RFC PATCH v5 8/12 virtio-serial-bus: add terminal resize messages

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

Implement the part of the virtio spec that allows to notify the virtio driver about terminal resizes. The virtio spec contains two methods to achieve that:

For legacy drivers, we have only one port and we put the terminal size in the config space and inject the config changed interrupt.

For multiport devices, we use the control virtqueue to send a packet containing the terminal size. Note that old versions of the Linux kernel used an incorrect order for the fields (rows then cols instead of cols then rows), until it was fixed by commit 5326ab737a47278dbd16ed3ee7380b26c7056ddd.

As a result, when using a Linux kernel older than 6.15, the number of rows and columns will be swapped.

[Filip: swap rows/cols, console-size affects multiport too,
size config always updated, use use_multiport, move trace call]

---
 hw/char/trace-events              |  1 +
 hw/char/virtio-serial-bus.c       | 51 +++++++++++++++++++++++++++++++++++++--
 hw/core/machine.c                 |  4 ++-
 include/hw/virtio/virtio-serial.h |  5 ++++
 4 files changed, 58 insertions(+), 3 deletions(-)
hw/char/trace-events +1 -0
index 05a33036c12070242c2b193c19011839d623bec4..9a975ab1e2a525a9391d0f0a85ddbe80aa6361fc 100644
@@ -11,6 +11,7 @@ serial_update_parameters(uint64_t baudrate, char parity, int data_bits, int stop
 
 # virtio-serial-bus.c
 virtio_serial_send_control_event(unsigned int port, uint16_t event, uint16_t value) "port %u, event %u, value %u"
+virtio_serial_send_console_resize(unsigned int port, uint16_t cols, uint16_t rows) "port %u, cols %u, rows %u"
 virtio_serial_throttle_port(unsigned int port, bool throttle) "port %u, throttle %d"
 virtio_serial_handle_control_message(uint16_t event, uint16_t value) "event %u, value %u"
 virtio_serial_handle_control_message_port(unsigned int port) "port %u"
hw/char/virtio-serial-bus.c +49 -2

69 lines not on this page · raw message

hw/core/machine.c +3 -1
index 38c949c4f2ce4a117cbfca62f56919711ce392b4..74a747ec6578c958b35e1f9712e5dbed7bca72e8 100644
@@ -37,7 +37,9 @@
 #include "hw/virtio/virtio-iommu.h"
 #include "audio/audio.h"
 
-GlobalProperty hw_compat_10_1[] = {};
+GlobalProperty hw_compat_10_1[] = {
+    { "virtio-serial-device", "console-size", "off" },
+};
 const size_t hw_compat_10_1_len = G_N_ELEMENTS(hw_compat_10_1);
 
 GlobalProperty hw_compat_10_0[] = {
include/hw/virtio/virtio-serial.h +5 -0
index d87c62eab7a270809daf47f932a73dd1fa3d5a6e..81efa853f804a52866890a9ec2c71bfbcabca4a0 100644
@@ -187,6 +187,8 @@ struct VirtIOSerial {
     virtio_serial_conf serial;
 
     uint64_t host_features;
+
+    uint16_t port0_cols, port0_rows;
 };
 
 /* Interface to the virtio-serial bus */
@@ -221,6 +223,9 @@ size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
  */
 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
 
+void virtio_serial_send_console_resize(VirtIOSerialPort *port,
+                                       uint16_t cols, uint16_t rows);
+
 #define TYPE_VIRTIO_SERIAL "virtio-serial-device"
 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSerial, VIRTIO_SERIAL)
 
Signature · 1 line
2.51.0
#27 lore raw

Re: [PATCH RFC v5 08/12] virtio-serial-bus: add terminal resize messages

Quoted 9 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

Implement the part of the virtio spec that allows to notify the virtio driver about terminal resizes. The virtio spec contains two methods to achieve that:

For legacy drivers, we have only one port and we put the terminal size in the config space and inject the config changed interrupt.

Quoted 7 lines

For multiport devices, we use the control virtqueue to send a packet containing the terminal size. Note that old versions of the Linux kernel used an incorrect order for the fields (rows then cols instead of cols then rows), until it was fixed by commit 5326ab737a47278dbd16ed3ee7380b26c7056ddd.

As a result, when using a Linux kernel older than 6.15, the number of rows and columns will be swapped.

That kernel patch is being reverted:

https://lore.kernel.org/lkml/[email protected]/

so this patch needs to use the historical ordering.

That revert needs to go into linux -stable trees, and any recent distro that has picked up the recent swapped ordering in their kernels.

Quoted 142 lines

[Filip: swap rows/cols, console-size affects multiport too,
size config always updated, use use_multiport, move trace call]

---
 hw/char/trace-events              |  1 +
 hw/char/virtio-serial-bus.c       | 51 +++++++++++++++++++++++++++++++++++++--
 hw/core/machine.c                 |  4 ++-
 include/hw/virtio/virtio-serial.h |  5 ++++
 4 files changed, 58 insertions(+), 3 deletions(-)
hw/char/trace-events +1 -0
index 05a33036c12070242c2b193c19011839d623bec4..9a975ab1e2a525a9391d0f0a85ddbe80aa6361fc 100644
@@ -11,6 +11,7 @@ serial_update_parameters(uint64_t baudrate, char parity, int data_bits, int stop
 
 # virtio-serial-bus.c
 virtio_serial_send_control_event(unsigned int port, uint16_t event, uint16_t value) "port %u, event %u, value %u"
+virtio_serial_send_console_resize(unsigned int port, uint16_t cols, uint16_t rows) "port %u, cols %u, rows %u"
 virtio_serial_throttle_port(unsigned int port, bool throttle) "port %u, throttle %d"
 virtio_serial_handle_control_message(uint16_t event, uint16_t value) "event %u, value %u"
 virtio_serial_handle_control_message_port(unsigned int port) "port %u"
hw/char/virtio-serial-bus.c +49 -2
index 673c50f0be08ef9b7142c16eaf8e6e31c7a00ca5..30e3ec73a1733449b2505f231a3d2b3516ae4b4e 100644
@@ -260,6 +260,51 @@ static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
     return send_control_msg(vser, &cpkt, sizeof(cpkt));
 }
 
+/*
+ * This struct should be added to the Linux kernel uapi headers
+ * and later imported to standard-headers/linux/virtio_console.h
+ */
+struct virtio_console_resize {
+    __virtio16 cols;
+    __virtio16 rows;
+};
+
+void virtio_serial_send_console_resize(VirtIOSerialPort *port,
+                                       uint16_t cols, uint16_t rows)
+{
+    VirtIOSerial *vser = port->vser;
+    VirtIODevice *vdev = VIRTIO_DEVICE(vser);
+
+    if (!virtio_has_feature(vser->host_features, VIRTIO_CONSOLE_F_SIZE)) {
+        return;
+    }
+
+    trace_virtio_serial_send_console_resize(port->id, cols, rows);
+
+    if (port->id == 0) {
+        vser->port0_cols = cols;
+        vser->port0_rows = rows;
+        if (!use_multiport(vser) &&
+            virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
+            virtio_notify_config(vdev);
+        }
+    }
+
+    if (use_multiport(vser)) {
+        struct {
+            struct virtio_console_control control;
+            struct virtio_console_resize resize;
+        } buffer;
+
+        virtio_stl_p(vdev, &buffer.control.id, port->id);
+        virtio_stw_p(vdev, &buffer.control.event, VIRTIO_CONSOLE_RESIZE);
+        virtio_stw_p(vdev, &buffer.resize.cols, cols);
+        virtio_stw_p(vdev, &buffer.resize.rows, rows);
+
+        send_control_msg(vser, &buffer, sizeof(buffer));
+    }
+}
+
 /* Functions for use inside qemu to open and read from/write to ports */
 int virtio_serial_open(VirtIOSerialPort *port)
 {
@@ -571,8 +616,8 @@ static void get_config(VirtIODevice *vdev, uint8_t *config_data)
     struct virtio_console_config *config =
         (struct virtio_console_config *)config_data;
 
-    config->cols = 0;
-    config->rows = 0;
+    config->cols = virtio_tswap16(vdev, vser->port0_cols);
+    config->rows = virtio_tswap16(vdev, vser->port0_rows);
     config->max_nr_ports = virtio_tswap32(vdev,
                                           vser->serial.max_virtserial_ports);
 }
@@ -1158,6 +1203,8 @@ static const Property virtio_serial_properties[] = {
                                                   31),
     DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
                       VIRTIO_CONSOLE_F_EMERG_WRITE, true),
+    DEFINE_PROP_BIT64("console-size", VirtIOSerial, host_features,
+                      VIRTIO_CONSOLE_F_SIZE, true),
 };
 
 static void virtio_serial_class_init(ObjectClass *klass, const void *data)
hw/core/machine.c +3 -1
index 38c949c4f2ce4a117cbfca62f56919711ce392b4..74a747ec6578c958b35e1f9712e5dbed7bca72e8 100644
@@ -37,7 +37,9 @@
 #include "hw/virtio/virtio-iommu.h"
 #include "audio/audio.h"
 
-GlobalProperty hw_compat_10_1[] = {};
+GlobalProperty hw_compat_10_1[] = {
+    { "virtio-serial-device", "console-size", "off" },
+};
 const size_t hw_compat_10_1_len = G_N_ELEMENTS(hw_compat_10_1);
 
 GlobalProperty hw_compat_10_0[] = {
include/hw/virtio/virtio-serial.h +5 -0
index d87c62eab7a270809daf47f932a73dd1fa3d5a6e..81efa853f804a52866890a9ec2c71bfbcabca4a0 100644
@@ -187,6 +187,8 @@ struct VirtIOSerial {
     virtio_serial_conf serial;
 
     uint64_t host_features;
+
+    uint16_t port0_cols, port0_rows;
 };
 
 /* Interface to the virtio-serial bus */
@@ -221,6 +223,9 @@ size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
  */
 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
 
+void virtio_serial_send_console_resize(VirtIOSerialPort *port,
+                                       uint16_t cols, uint16_t rows);
+
 #define TYPE_VIRTIO_SERIAL "virtio-serial-device"
 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSerial, VIRTIO_SERIAL)
 
Signature · 2 lines
-- 
2.51.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 8/12
#28 lore raw

Re: [PATCH RFC v5 08/12] virtio-serial-bus: add terminal resize messages

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:38AM +0200:

Quoted 9 lines

From: Szymon Lukasz <[email protected]>

Implement the part of the virtio spec that allows to notify the virtio driver about terminal resizes. The virtio spec contains two methods to achieve that:

For legacy drivers, we have only one port and we put the terminal size in the config space and inject the config changed interrupt.

Quoted 7 lines

For multiport devices, we use the control virtqueue to send a packet containing the terminal size. Note that old versions of the Linux kernel used an incorrect order for the fields (rows then cols instead of cols then rows), until it was fixed by commit 5326ab737a47278dbd16ed3ee7380b26c7056ddd.

As a result, when using a Linux kernel older than 6.15, the number of rows and columns will be swapped.

(subject to this part of the discussion being settled...)

Quoted 4 lines

[Filip: swap rows/cols, console-size affects multiport too,
size config always updated, use use_multiport, move trace call]

hw/core/machine.c line 41

Quoted 11 lines
hw/core/machine.c +2 -1
index 38c949c4f2ce4a117cbfca62f56919711ce392b4..74a747ec6578c958b35e1f9712e5dbed7bca72e8 100644
@@ -37,7 +37,9 @@
 #include "hw/virtio/virtio-iommu.h"
 #include "audio/audio.h"
 
-GlobalProperty hw_compat_10_1[] = {};
+GlobalProperty hw_compat_10_1[] = {
+    { "virtio-serial-device", "console-size", "off" },

Oh the original patches had this wrong, good catch!

Signature · 1 line
Dominique Martinet | Asmadeus
#29 lore raw

RFC PATCH v5 9/12 virtio-console: notify the guest about terminal resizes

In reply to Filip Hejsek

From: Szymon Lukasz <[email protected]>

If a virtio serial port is a console port, forward terminal resize messages from the chardev backend to the guest.

---
 hw/char/virtio-console.c | 62 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 58 insertions(+), 4 deletions(-)
hw/char/virtio-console.c +58 -4
index 881c862ce9d12027f392031bdea7dbe280ec5493..0dd10a81f151b0606f6060ab2b4936117d81dd83 100644
@@ -32,6 +32,7 @@ struct VirtConsole {
 
     CharBackend chr;
     guint watch;
+    uint16_t cols, rows;
 };
 
 /*
@@ -107,6 +108,33 @@ static ssize_t flush_buf(VirtIOSerialPort *port,
     return ret;
 }
 
+static void virtconsole_send_resize(VirtIOSerialPort *port)
+{
+    uint16_t cols, rows;
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    /*
+     * We probably shouldn't send these messages before
+     * we told the guest it is a console port (which we do
+     * by sending VIRTIO_CONSOLE_CONSOLE_PORT message).
+     * Instead of adding a new field to the device state
+     * lets just use the guest_connected field for that purpose
+     * since the guest should not care about the terminal size
+     * before opening the port.
+     */
+    if (!port->guest_connected) {
+        return;
+    }
+
+    qemu_chr_fe_get_winsize(&vcon->chr, &cols, &rows);
+
+    if (cols != vcon->cols || rows != vcon->rows) {
+        vcon->cols = cols;
+        vcon->rows = rows;
+        virtio_serial_send_console_resize(port, cols, rows);
+    }
+}
+
 /* Callback function that's called when the guest opens/closes the port */
 static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
 {
@@ -114,7 +142,9 @@ static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
     DeviceState *dev = DEVICE(port);
     VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
 
-    if (!k->is_console) {
+    if (k->is_console) {
+        virtconsole_send_resize(port);
+    } else {
         qemu_chr_fe_set_open(&vcon->chr, guest_connected);
     }
 
@@ -174,6 +204,23 @@ static void chr_event(void *opaque, QEMUChrEvent event)
     }
 }
 
+static void chr_event_console(void *opaque, QEMUChrEvent event)
+{
+    VirtConsole *vcon = opaque;
+    VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
+
+    trace_virtio_console_chr_event(port->id, event);
+    switch (event) {
+    case CHR_EVENT_OPENED:
+    case CHR_EVENT_MUX_IN:
+    case CHR_EVENT_RESIZE:
+        virtconsole_send_resize(port);
+        break;
+    default:
+        break;
+    }
+}
+
 static int chr_be_change(void *opaque)
 {
     VirtConsole *vcon = opaque;
@@ -182,7 +229,9 @@ static int chr_be_change(void *opaque)
 
     if (k->is_console) {
         qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
-                                 NULL, chr_be_change, vcon, NULL, true);
+                                 chr_event_console, chr_be_change,
+                                 vcon, NULL, true);
+        virtconsole_send_resize(port);
     } else {
         qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
                                  chr_event, chr_be_change, vcon, NULL, false);
@@ -210,7 +259,7 @@ static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
         VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
 
         qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
-                                 k->is_console ? NULL : chr_event,
+                                 k->is_console ? chr_event_console : chr_event,
                                  chr_be_change, vcon, NULL, false);
     } else {
         qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL,
@@ -230,6 +279,11 @@ static void virtconsole_realize(DeviceState *dev, Error **errp)
         return;
     }
 
+    if (k->is_console) {
+        vcon->cols = (uint16_t) -1;
+        vcon->rows = (uint16_t) -1;
+    }
+
     if (qemu_chr_fe_backend_connected(&vcon->chr)) {
         /*
          * For consoles we don't block guest data transfer just
@@ -242,7 +296,7 @@ static void virtconsole_realize(DeviceState *dev, Error **errp)
          */
         if (k->is_console) {
             qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
-                                     NULL, chr_be_change,
+                                     chr_event_console, chr_be_change,
                                      vcon, NULL, true);
             virtio_serial_open(port);
         } else {
Signature · 1 line
2.51.0
Reviewed-by on v5 9/12
#30 lore raw

Re: [PATCH RFC v5 09/12] virtio-console: notify the guest about terminal resizes

Quoted 10 lines · Filip Hejsek

From: Szymon Lukasz <[email protected]>

If a virtio serial port is a console port, forward terminal resize messages from the chardev backend to the guest.

---
 hw/char/virtio-console.c | 62 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 58 insertions(+), 4 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by Tested-by on v5 9/12
#31 lore raw

Re: [PATCH RFC v5 09/12] virtio-console: notify the guest about terminal resizes

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:39AM +0200:

Quoted 7 lines

From: Szymon Lukasz <[email protected]>

If a virtio serial port is a console port, forward terminal resize messages from the chardev backend to the guest.

Nitpick below, but it works anyway:

hw/char/virtio-console.c static ssize_t flush_buf(VirtIOSerialPort *port, · line 131

Quoted 7 lines
hw/char/virtio-console.c +3 -0
index 881c862ce9d12027f392031bdea7dbe280ec5493..0dd10a81f151b0606f6060ab2b4936117d81dd83 100644
+    qemu_chr_fe_get_winsize(&vcon->chr, &cols, &rows);
+
+    if (cols != vcon->cols || rows != vcon->rows) {

given vcon cols/rows is initialized to -1 that means we'd send a 0,0 event if the chr size isn't set; would it make more sense to initialize to 0 as well?

Signature · 1 line
Dominique Martinet | Asmadeus
#32 lore raw

RFC PATCH v5 10/12 char-win-stdio: add support for terminal size

In reply to Filip Hejsek

Use GetConsoleScreenBufferInfo to obtain terminal size and set ENABLE_WINDOW_INPUT to receive resize notifications.

---
 chardev/char-win-stdio.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
chardev/char-win-stdio.c +19 -0
index fb802a00b13ac4089abf3bd4f8c4198d8325764b..0d33138d0dd83b582e959d21c3d991de52f8d3e4 100644
@@ -44,6 +44,20 @@ typedef struct WinStdioChardev WinStdioChardev;
 DECLARE_INSTANCE_CHECKER(WinStdioChardev, WIN_STDIO_CHARDEV,
                          TYPE_CHARDEV_WIN_STDIO)
 
+static void char_win_stdio_resize(Chardev *chr)
+{
+    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
+    CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo;
+
+    if (GetConsoleScreenBufferInfo(hStdOut, &ScreenBufferInfo)) {
+        uint16_t cols = ScreenBufferInfo.srWindow.Right + 1
+                        - ScreenBufferInfo.srWindow.Left;
+        uint16_t rows = ScreenBufferInfo.srWindow.Bottom + 1
+                        - ScreenBufferInfo.srWindow.Top;
+        qemu_chr_resize(chr, cols, rows);
+    }
+}
+
 static void win_stdio_wait_func(void *opaque)
 {
     Chardev *chr = CHARDEV(opaque);
@@ -75,6 +89,9 @@ static void win_stdio_wait_func(void *opaque)
                 }
             }
         }
+        if (buf[i].EventType == WINDOW_BUFFER_SIZE_EVENT) {
+            char_win_stdio_resize(chr);
+        }
     }
 }
 
@@ -202,6 +219,8 @@ static void qemu_chr_open_stdio(Chardev *chr,
         } else {
             dwMode &= ~ENABLE_PROCESSED_INPUT;
         }
+        dwMode |= ENABLE_WINDOW_INPUT;
+        char_win_stdio_resize(chr);
     }
 
     SetConsoleMode(stdio->hStdIn, dwMode);
Signature · 1 line
2.51.0
Reviewed-by on v5 10/12
#33 lore raw

Re: [PATCH RFC v5 10/12] char-win-stdio: add support for terminal size

Quoted 7 lines · Filip Hejsek

Use GetConsoleScreenBufferInfo to obtain terminal size and set ENABLE_WINDOW_INPUT to receive resize notifications.

---
 chardev/char-win-stdio.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v5 10/12
#34 lore raw

Re: [PATCH RFC v5 10/12] char-win-stdio: add support for terminal size

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:40AM +0200:

Quoted 4 lines

Use GetConsoleScreenBufferInfo to obtain terminal size and set ENABLE_WINDOW_INPUT to receive resize notifications.

(not tested)

Signature · 1 line
Dominique Martinet | Asmadeus
#35 lore raw

RFC PATCH v5 11/12 ui/console-vc: forward text console size to vc chardev

In reply to Filip Hejsek

Update the vc chardev size when the size of a text console is recomputed.

NOTE: It seems wrong that the dpy_text_resize callback is not called
when the size is recomputed. I believe this is a bug, although it doesn't matter much because the only user of the callback is ui/ncurses and it only uses the size of fixed consoles.

---
 ui/console-vc.c | 3 +++
 1 file changed, 3 insertions(+)
ui/console-vc.c +3 -0
index 830842064d6982ee4e7882e50e5c3ef9b1e18f59..a3a90440d52754b02961e29063dda91c9f4097f2 100644
@@ -414,6 +414,9 @@ static void text_console_resize(QemuTextConsole *t)
     }
     g_free(t->cells);
     t->cells = cells;
+
+    /* XXX Shouldn't qemu_text_console_update_size() also be called here? */
+    qemu_chr_resize(t->chr, t->width, t->height);
 }
 
 static void vc_put_lf(VCChardev *vc)
Signature · 1 line
2.51.0
Reviewed-by on v5 11/12
#36 lore raw

Re: [PATCH RFC v5 11/12] ui/console-vc: forward text console size to vc chardev

Quoted 11 lines · Filip Hejsek

Update the vc chardev size when the size of a text console is recomputed.

NOTE: It seems wrong that the dpy_text_resize callback is not called
when the size is recomputed. I believe this is a bug, although it doesn't matter much because the only user of the callback is ui/ncurses and it only uses the size of fixed consoles.

---
 ui/console-vc.c | 3 +++
 1 file changed, 3 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v5 11/12
#37 lore raw

Re: [PATCH RFC v5 11/12] ui/console-vc: forward text console size to vc chardev

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:41AM +0200:

Quoted 8 lines

Update the vc chardev size when the size of a text console is recomputed.

NOTE: It seems wrong that the dpy_text_resize callback is not called
when the size is recomputed. I believe this is a bug, although it doesn't matter much because the only user of the callback is ui/ncurses and it only uses the size of fixed consoles.

(not tested)

Signature · 1 line
Dominique Martinet | Asmadeus
#38 lore raw

RFC PATCH v5 12/12 ui/gtk: forward gtk console size to vc chardev

In reply to Filip Hejsek

Query the terminal size from the vte library when creating the console and every time it might change. Vte doesn't send any signal specifically for terminal size changes, so instead we register callbacks for size-allocate and char-size-changed.

---
 ui/gtk.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
ui/gtk.c +27 -0
index e91d093a49e5d05c10f1dbea5b9b94ff1389b456..ff2709160787f0e2974c83802d932758866f6d44 100644
@@ -2025,6 +2025,27 @@ static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
     return TRUE;
 }
 
+static void gd_vc_vte_update_size(VirtualConsole *vc)
+{
+    uint16_t cols = vte_terminal_get_column_count(VTE_TERMINAL(vc->vte.terminal));
+    uint16_t rows = vte_terminal_get_row_count(VTE_TERMINAL(vc->vte.terminal));
+    qemu_chr_resize(vc->vte.chr, cols, rows);
+}
+
+static void gd_vc_size_allocate(VteTerminal *terminal,
+                                GtkAllocation *allocation, gpointer user_data)
+{
+    VirtualConsole *vc = user_data;
+    gd_vc_vte_update_size(vc);
+}
+
+static void gd_vc_char_size_changed(VteTerminal *terminal, guint width,
+                                    guint height, gpointer user_data)
+{
+    VirtualConsole *vc = user_data;
+    gd_vc_vte_update_size(vc);
+}
+
 static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
                               Chardev *chr, int idx,
                               GSList *group, GtkWidget *view_menu)
@@ -2090,6 +2111,12 @@ static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
 
     qemu_chr_be_event(vc->vte.chr, CHR_EVENT_OPENED);
 
+    g_signal_connect(vc->vte.terminal, "size-allocate",
+                     G_CALLBACK(gd_vc_size_allocate), vc);
+    g_signal_connect(vc->vte.terminal, "char-size-changed",
+                     G_CALLBACK(gd_vc_char_size_changed), vc);
+    gd_vc_vte_update_size(vc);
+
     return group;
 }
 
Signature · 1 line
2.51.0
Reviewed-by on v5 12/12
#39 lore raw

Re: [PATCH RFC v5 12/12] ui/gtk: forward gtk console size to vc chardev

Quoted 9 lines · Filip Hejsek

Query the terminal size from the vte library when creating the console and every time it might change. Vte doesn't send any signal specifically for terminal size changes, so instead we register callbacks for size-allocate and char-size-changed.

---
 ui/gtk.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v5 12/12
#40 lore raw

Re: [PATCH RFC v5 12/12] ui/gtk: forward gtk console size to vc chardev

In reply to Filip Hejsek

Filip Hejsek wrote on Sun, Sep 21, 2025 at 01:45:42AM +0200:

Quoted 6 lines

Query the terminal size from the vte library when creating the console and every time it might change. Vte doesn't send any signal specifically for terminal size changes, so instead we register callbacks for size-allocate and char-size-changed.

(not tested; I couldn't figure how to make the console available to the gtk mux locally... ohwell, it looks good to me™)

And that's the last patch, thanks a lot for this work! A few things cleaner than I had :)

Signature · 1 line
Dominique Martinet | Asmadeus