ui: better console hotplug support
cover ui: better console hotplug support
Hi,
This series improves handling of dynamic consoles in ui/ (commit 9588d67e72 "console: minimal hotplug suport" added basic unplug & replug). It allows for better hotplug of vfio-pci display devices or bochs-display (the other device supporting hotplug today), with GTK and D-Bus backends.
As usual, the first patches are various preliminary cleanups and fixes I hit while developping the rest, they could be merged earlier. I improved the display cleanup to replace the scattered atexit() handler, making leaks more visible to sanitizers.
Then console ADDED/REMOVED notifications are added, so display backends can react to hotplug. The GTK code is rework a bit to allow easier hotplug events handling. Finally, D-Bus supports is implemented with a qtest to exercise it.
thanks
---
Marc-André Lureau (32):
ui/gtk: fix bad widget realize on non-GFX VC
build-sys: build with -fno-omit-frame-pointer with ASAN
irq: add per-IRQ observer to fix qemu_irq_intercept_in leak scripts/lsan_suppressions: suppress fontconfig leaks vfio/pci: close display console during unrealize, not finalize
glib-compat: add fallback for g_clear_fd/g_autofd ui/dbus: remove mouse handler on dispose
ui/qmp: keep a reference of console across yield
ui: stop ui timer when closing
ui/console: init gl_unblock_timer in qemu_console_init ui/spice: remove dead spice_displays
ui/spice: add cleanup on shutdown
ui: add display cleanup infrastructure
ui/curses: implement display cleanup
ui/sdl2: implement display cleanup
ui/spice-app: implement display cleanup
ui/egl: implement display and EGL cleanup
ui/cocoa: implement display cleanup
ui/dbus: implement display cleanup
ui/gtk: implement display cleanup
ui/console: add console event notifier infrastructure ui/console: fire console ADDED/REMOVED notifications ui/console-vc: fire ADDED/REMOVED notifications
ui/gtk: convert VirtualConsole storage from fixed array to GPtrArray ui/gtk: move global display settings out of per-console init ui/gtk: fix tab re-insertion order on window close ui/gtk: centralize console menu and shortcut management ui/gtk: handle console hotplug/unplug events
ui/console: register console in QOM tree dynamically ui/console: unregister console from QOM tree on close ui/dbus: handle console hotplug/unplug events tests/qtest: add D-Bus display hotplug test
meson.build | 2 + hw/vfio/pci.h | 1 + include/glib-compat.h | 29 ++++ include/hw/core/irq.h | 1 + include/ui/console.h | 20 +++ include/ui/egl-helpers.h | 1 + include/ui/gtk.h | 6 +- include/ui/qemu-spice-module.h | 1 + include/ui/qemu-spice.h | 9 +- ui/dbus.h | 3 + hw/core/irq.c | 10 +- hw/vfio/display.c | 30 ++-- hw/vfio/pci.c | 1 + system/qtest.c | 3 - system/runstate.c | 4 +- tests/qtest/dbus-display-test.c | 101 ++++++++++- ui/console-vc.c | 13 ++ ui/console.c | 71 +++++++- ui/curses.c | 17 +- ui/dbus-console.c | 9 + ui/dbus.c | 106 ++++++++++-- ui/egl-headless.c | 31 ++++ ui/egl-helpers.c | 19 +++ ui/gtk-clipboard.c | 15 ++ ui/gtk.c | 369 +++++++++++++++++++++++++++++++--------- ui/sdl2.c | 23 ++- ui/spice-app.c | 10 +- ui/spice-core.c | 25 ++- ui/spice-display.c | 52 ++++++ ui/spice-input.c | 52 ++++-- ui/spice-module.c | 5 + ui/ui-qmp-cmds.c | 3 + scripts/lsan_suppressions.txt | 15 +- ui/cocoa.m | 19 ++- ui/meson.build | 4 +- 35 files changed, 901 insertions(+), 179 deletions(-)
---
base-commit: 2db91528542672cf0db78b3f2cc0e22b36302b38
change-id: 20260529-b4-ui-909bfa695735
Best regards,
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH 1/32 ui/gtk: fix bad widget realize on non-GFX VC
The GTK VirtualConsole is a union, it may be .gfx or .vte depending on the type.
--- ui/gtk.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
ui/gtk.c +3 -1
@@ -2585,7 +2585,9 @@ static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)if (!con) {break ; } -gtk_widget_realize (s->vc[idx].gfx.drawing_area); +if (s->vc[idx].type ==GD_VC_GFX ) { +gtk_widget_realize (s->vc[idx].gfx.drawing_area); + } } if (opts->u.gtk.has_show_menubar &&
Signature · 1 line
2.54.0
PATCH 2/32 build-sys: build with -fno-omit-frame-pointer with ASAN
In reply to Marc-André LureauOn fc44, LSan fails to suppress leak:qemu_irq_intercept_in, because the backtrace isn't deep enough.
--- meson.build | 2 ++ 1 file changed, 2 insertions(+)
meson.build +2 -0
@@ -545,6 +545,8 @@ if get_option('asan') if cc.has_argument('-fsanitize=address') qemu_cflags = ['-fsanitize=address'] + qemu_cflags qemu_ldflags = ['-fsanitize=address'] + qemu_ldflags + # Ensure complete stack traces for LSan suppressions to match correctly. + qemu_cflags += ['-fno-omit-frame-pointer'] else error('Your compiler does not support -fsanitize=address') endif
Signature · 1 line
2.54.0
PATCH 3/32 irq: add per-IRQ observer to fix qemu_irq_intercept_in leak
In reply to Marc-André Lureauqemu_irq_intercept_in() saves original IRQ handlers by allocating new QOM objects, which are never freed. On a PC machine, this leaks IRQ objects (one per IOAPIC pin) on every qtest run.
Rather than tracking allocations to free later, avoid them: add an "observer" field to IRQState, called by qemu_set_irq() after the real handler. Interception sets the observer instead of rewriting handlers, so there's nothing to save and nothing to leak.
Fix qemu_notirq() to route through qemu_set_irq() so inverted IRQs trigger observers too. Drop the LSan suppression.
--- include/hw/core/irq.h | 1 + hw/core/irq.c | 10 +++++----- system/qtest.c | 3 --- scripts/lsan_suppressions.txt | 8 -------- 4 files changed, 6 insertions(+), 16 deletions(-)
include/hw/core/irq.h +1 -0
@@ -14,6 +14,7 @@ struct IRQState {qemu_irq_handler handler;void *opaque;int n; +qemu_irq_handler observer; };void qemu_set_irq (qemu_irq irq,int level);
hw/core/irq.c +5 -5
@@ -32,6 +32,9 @@ void qemu_set_irq(qemu_irq irq, int level)return ; irq->handler (irq->opaque, irq->n, level); +if (unlikely (irq->observer)) { + irq->observer (irq->opaque, irq->n, level); + } } static void init_irq_fields(IRQState *irq, qemu_irq_handler handler,
@@ -111,7 +114,7 @@ static void qemu_notirq(void *opaque, int line, int level) {IRQState *irq = opaque; - irq->handler (irq->opaque, irq->n, !level); +qemu_set_irq (irq, !level); }qemu_irq qemu_irq_invert (qemu_irq irq)
@@ -124,11 +127,8 @@ qemu_irq qemu_irq_invert(qemu_irq irq)void qemu_irq_intercept_in (qemu_irq *gpio_in,qemu_irq_handler handler,int n) {int i; -qemu_irq *old_irqs =qemu_allocate_irqs (NULL ,NULL , n);for (i =0 ; i < n; i++) { - *old_irqs[i] = *gpio_in[i]; - gpio_in[i]->handler = handler; - gpio_in[i]->opaque = &old_irqs[i]; + gpio_in[i]->observer = handler; } }
system/qtest.c +0 -3
@@ -324,9 +324,6 @@ void qtest_sendf(CharFrontend *chr, const char *fmt, ...)static void qtest_irq_handler (void *opaque,int n,int level) { -qemu_irq old_irq = *(qemu_irq *)opaque; -qemu_set_irq (old_irq, level); -if (irq_levels[n] != level) {CharFrontend *chr = &qtest->qtest_chr; irq_levels[n] = level;
scripts/lsan_suppressions.txt +0 -8
@@ -16,11 +16,3 @@ leak:/lib64/libxkbcommon.so.0 # https://github.com/GNOME/glib/blob/main/tools/glib.supp # This avoids false positive leak reports for the qga-ssh-test. leak:g_set_user_dirs - -# qemu_irq_intercept_in is only used by the qtest harness, and -# its API inherently involves a leak. -# While we could keep track of the old IRQ data structure -# in order to free it, it doesn't seem very important to fix -# since it is only used by the qtest test harness. -# Just ignore the leak, at least for the moment. -leak:qemu_irq_intercept_in
Signature · 1 line
2.54.0
Re: [PATCH 03/32] irq: add per-IRQ observer to fix qemu_irq_intercept_in leak
Quoted 88 lines · Marc-André Lureau
qemu_irq_intercept_in()saves original IRQ handlers by allocating new QOM objects, which are never freed. On a PC machine, this leaks IRQ objects (one per IOAPIC pin) on every qtest run.Rather than tracking allocations to free later, avoid them: add an "observer" field to IRQState, called by
qemu_set_irq()after the real handler. Interception sets the observer instead of rewriting handlers, so there's nothing to save and nothing to leak.Fix
qemu_notirq()to route throughqemu_set_irq()so inverted IRQs trigger observers too. Drop the LSan suppression.Signed-off-by Marc-André Lureau <[email protected]>--- include/hw/core/irq.h | 1 + hw/core/irq.c | 10 +++++----- system/qtest.c | 3 --- scripts/lsan_suppressions.txt | 8 -------- 4 files changed, 6 insertions(+), 16 deletions(-)include/hw/core/irq.h +1
-0@@ -14,6 +14,7 @@ struct IRQState {qemu_irq_handler handler;void *opaque;int n; +qemu_irq_handler observer; };void qemu_set_irq (qemu_irq irq,int level);hw/core/irq.c +5
-5@@ -32,6 +32,9 @@ void qemu_set_irq(qemu_irq irq, int level)return ; irq->handler (irq->opaque, irq->n, level); +if (unlikely (irq->observer)) { + irq->observer (irq->opaque, irq->n, level); + } } static void init_irq_fields(IRQState *irq, qemu_irq_handler handler,@@ -111,7 +114,7 @@ static void qemu_notirq(void *opaque, int line, int level) {IRQState *irq = opaque; - irq->handler (irq->opaque, irq->n, !level); +qemu_set_irq (irq, !level); }qemu_irq qemu_irq_invert (qemu_irq irq)@@ -124,11 +127,8 @@ qemu_irq qemu_irq_invert(qemu_irq irq)void qemu_irq_intercept_in (qemu_irq *gpio_in,qemu_irq_handler handler,int n) {int i; -qemu_irq *old_irqs =qemu_allocate_irqs (NULL ,NULL , n);for (i =0 ; i < n; i++) { - *old_irqs[i] = *gpio_in[i]; - gpio_in[i]->handler = handler; - gpio_in[i]->opaque = &old_irqs[i]; + gpio_in[i]->observer = handler; } }system/qtest.c +0
-3@@ -324,9 +324,6 @@ void qtest_sendf(CharFrontend *chr, const char *fmt, ...)static void qtest_irq_handler (void *opaque,int n,int level) { -qemu_irq old_irq = *(qemu_irq *)opaque; -qemu_set_irq (old_irq, level); -if (irq_levels[n] != level) {CharFrontend *chr = &qtest->qtest_chr; irq_levels[n] = level;scripts/lsan_suppressions.txt +0
-0@@ -16,11 +16,3 @@ leak:/lib64/libxkbcommon.so.0 # https://github.com/GNOME/glib/blob/main/tools/glib.supp
scripts/lsan_suppressions.txt leak:/lib64/libxkbcommon.so.0 · line 26
Quoted 10 lines
scripts/lsan_suppressions.txt +0
-8# This avoids false positive leak reports for the qga-ssh-test. leak:g_set_user_dirs - -# qemu_irq_intercept_in is only used by the qtest harness, and -# its API inherently involves a leak. -# While we could keep track of the old IRQ data structure -# in order to free it, it doesn't seem very important to fix -# since it is only used by the qtest test harness. -# Just ignore the leak, at least for the moment. -leak:qemu_irq_intercept_in
Re: [PATCH 03/32] irq: add per-IRQ observer to fix qemu_irq_intercept_in leak
In reply to Marc-André LureauQuoted 38 lines · Marc-André Lureau
qemu_irq_intercept_in()saves original IRQ handlers by allocating new QOM objects, which are never freed. On a PC machine, this leaks IRQ objects (one per IOAPIC pin) on every qtest run.Rather than tracking allocations to free later, avoid them: add an "observer" field to IRQState, called by
qemu_set_irq()after the real handler. Interception sets the observer instead of rewriting handlers, so there's nothing to save and nothing to leak.Fix
qemu_notirq()to route throughqemu_set_irq()so inverted IRQs trigger observers too. Drop the LSan suppression.Signed-off-by Marc-André Lureau <[email protected]>--- include/hw/core/irq.h | 1 + hw/core/irq.c | 10 +++++----- system/qtest.c | 3 --- scripts/lsan_suppressions.txt | 8 -------- 4 files changed, 6 insertions(+), 16 deletions(-)include/hw/core/irq.h +1
-0@@ -14,6 +14,7 @@ struct IRQState {qemu_irq_handler handler;void *opaque;int n; +qemu_irq_handler observer; };void qemu_set_irq (qemu_irq irq,int level);hw/core/irq.c +0
-0
hw/core/irq.c void qemu_set_irq(qemu_irq irq, int level) · line 38
Quoted 7 lines
hw/core/irq.c +3
-0@@ -32,6 +32,9 @@ void qemu_set_irq(qemu_irq irq, int level)return ; irq->handler (irq->opaque, irq->n, level); +if (unlikely (irq->observer)) { + irq->observer (irq->opaque, irq->n, level); + } }
Hmm. "observer" semantics are probably nicer than "steal the irq from the thing that would otherwise be connected to it", but on the other hand this means that this purely test-relevant thing is now in the code path for the common "signal an IRQ" that we use everywhere...
-- PMM
Re: [PATCH 03/32] irq: add per-IRQ observer to fix qemu_irq_intercept_in leak
Hi
Quoted 43 lines · Peter Maydell
Quoted 40 lines · Marc-André Lureau
qemu_irq_intercept_in()saves original IRQ handlers by allocating new QOM objects, which are never freed. On a PC machine, this leaks IRQ objects (one per IOAPIC pin) on every qtest run.Rather than tracking allocations to free later, avoid them: add an "observer" field to IRQState, called by
qemu_set_irq()after the real handler. Interception sets the observer instead of rewriting handlers, so there's nothing to save and nothing to leak.Fix
qemu_notirq()to route throughqemu_set_irq()so inverted IRQs trigger observers too. Drop the LSan suppression.Signed-off-by Marc-André Lureau <[email protected]>--- include/hw/core/irq.h | 1 + hw/core/irq.c | 10 +++++----- system/qtest.c | 3 --- scripts/lsan_suppressions.txt | 8 -------- 4 files changed, 6 insertions(+), 16 deletions(-)include/hw/core/irq.h +1
-0@@ -14,6 +14,7 @@ struct IRQState {qemu_irq_handler handler;void *opaque;int n; +qemu_irq_handler observer; };void qemu_set_irq (qemu_irq irq,int level);hw/core/irq.c +0
-0@@ -32,6 +32,9 @@ void qemu_set_irq(qemu_irq irq, int level)return ;
Quoted 11 lines
hw/core/irq.c void qemu_set_irq(qemu_irq irq, int level) · line 38
Quoted 5 lines
hw/core/irq.c +3
-0irq->handler (irq->opaque, irq->n, level); +if (unlikely (irq->observer)) { + irq->observer (irq->opaque, irq->n, level); + } }Hmm. "observer" semantics are probably nicer than "steal the irq from the thing that would otherwise be connected to it", but on the other hand this means that this purely test-relevant thing is now in the code path for the common "signal an IRQ" that we use everywhere...
Sure, but is this more intrusive or expansive than a trace at this point?
PATCH 4/32 scripts/lsan_suppressions: suppress fontconfig leaks
In reply to Marc-André LureauThose are annoying reports for gtk/sdl etc.
--- scripts/lsan_suppressions.txt | 4 ++++ 1 file changed, 4 insertions(+)
scripts/lsan_suppressions.txt +4 -0
@@ -10,6 +10,10 @@ leak:/lib64/libtcmalloc_minimal.so.4 # libxkbcommon also leaks in qemu-keymap leak:/lib64/libxkbcommon.so.0 +# libfontconfig leaks are notorious, for ex +# https://gitlab.freedesktop.org/fontconfig/fontconfig/-/work_items/519 +leak:libfontconfig.so + # g_set_user_dirs() deliberately leaks the previous cached g_get_user_* # values. This is documented in upstream glib's valgrind-format # suppression file:
Signature · 1 line
2.54.0
PATCH 5/32 vfio/pci: close display console during unrealize, not finalize
In reply to Marc-André LureauThe QemuGraphicConsole holds a strong QOM link back to the device via its "device" property (OBJ_PROP_LINK_STRONG). When graphic_console_close() is only called from vfio_display_finalize() during object finalize, this creates a ref-cycle deadlock: the device can't reach refcount 0 because the console holds a strong ref, but the console's ref is only dropped by graphic_console_close() which runs inside finalize.
Split the display teardown into two phases:
- vfio_display_exit(): called during unrealize (vfio_exitfn), closes the graphic console to break the ref cycle, and removes display region subregions while the parent memory regions are still alive.
- vfio_display_finalize(): remains in finalize (vfio_pci_put_device), frees display region memory, dmabuf, and edid resources. The region memory contains QOM child objects (MemoryRegions) that must stay alive until QOM finalization has processed them.
--- hw/vfio/pci.h | 1 + hw/vfio/display.c | 30 +++++++++++++++++++----------- hw/vfio/pci.c | 1 + 3 files changed, 21 insertions(+), 11 deletions(-)
hw/vfio/pci.h +1 -0
@@ -270,6 +270,7 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp);void vfio_display_reset (VFIOPCIDevice *vdev);bool vfio_display_probe (VFIOPCIDevice *vdev,Error **errp); +void vfio_display_exit (VFIOPCIDevice *vdev);void vfio_display_finalize (VFIOPCIDevice *vdev);extern const VMStateDescription vfio_display_vmstate;
hw/vfio/display.c +19 -11
@@ -505,15 +505,6 @@ static bool vfio_display_region_init(VFIOPCIDevice *vdev, Error **errp)return true; } -static void vfio_display_region_exit (VFIODisplay *dpy) -{ -if (!dpy->region.buffer.size) { -return ; - } - -vfio_region_exit (&dpy->region.buffer); -vfio_region_finalize (&dpy->region.buffer); -}/* ---------------------------------------------------------------------- */
@@ -547,17 +538,34 @@ bool vfio_display_probe(VFIOPCIDevice *vdev, Error **errp)return false; } -void vfio_display_finalize (VFIOPCIDevice *vdev) +void vfio_display_exit (VFIOPCIDevice *vdev) {if (!vdev->dpy) {return ; } +if (display_opengl) { +qemu_console_set_display_gl_ctx (vdev->dpy->con,NULL ); + }qemu_graphic_console_close (vdev->dpy->con); +if (vdev->dpy->region.buffer.size) { +vfio_region_exit (&vdev->dpy->region.buffer); + } +} + +void vfio_display_finalize (VFIOPCIDevice *vdev) +{ +if (!vdev->dpy) { +return ; + } +vfio_display_dmabuf_exit (vdev->dpy); -vfio_display_region_exit (vdev->dpy); +if (vdev->dpy->region.buffer.size) { +vfio_region_finalize (&vdev->dpy->region.buffer); + }vfio_display_edid_exit (vdev->dpy);g_free (vdev->dpy); + vdev->dpy =NULL ; }static bool migrate_needed (void *opaque)
hw/vfio/pci.c +1 -0
@@ -3624,6 +3624,7 @@ static void vfio_exitfn(PCIDevice *pdev)VFIOPCIDevice *vdev =VFIO_PCI_DEVICE (pdev);VFIODevice *vbasedev = &vdev->vbasedev; +vfio_display_exit (vdev);vfio_unregister_req_notifier (vdev);vfio_unregister_err_notifier (vdev);pci_device_set_intx_routing_notifier (pdev,NULL );
Signature · 1 line
2.54.0
PATCH 6/32 glib-compat: add fallback for g_clear_fd/g_autofd
In reply to Marc-André LureauThose helpers were added in glib 2.76.
--- include/glib-compat.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
include/glib-compat.h +29 -0
@@ -30,6 +30,7 @@#pragma GCC diagnostic ignored "-Wdeprecated-declarations"#include <glib.h> +#include <glib/gstdio.h> #if defined(G_OS_UNIX )#include <glib-unix.h> #include <sys/types.h>
@@ -129,6 +130,34 @@ qemu_g_test_slow(void)#define g_test_thorough () qemu_g_test_slow()#define g_test_quick () (!qemu_g_test_slow()) +static inline gboolean g_clear_fd_qemu (int *fd_ptr,GError **error) +{ +#if GLIB_CHECK_VERSION (2 ,76 ,0 ) +return g_clear_fd (fd_ptr, error); +#else +int fd = *fd_ptr; + + *fd_ptr =-1 ; + +if (fd <0 ) { +return TRUE; + } + +return g_close (fd, error); +#endif +} +#define g_clear_fd (fd, err) g_clear_fd_qemu(fd, err) + +#if !GLIB_CHECK_VERSION (2 ,76 ,0 ) +static inline void _g_clear_fd_ignore_error (int *fd_ptr) +{ +int errsv = errno; +g_clear_fd (fd_ptr,NULL ); + errno = errsv; +} +#define g_autofd __attribute__((cleanup(_g_clear_fd_ignore_error))) +#endif +#pragma GCC diagnostic pop#ifndef G_NORETURN
Signature · 1 line
2.54.0
PATCH 7/32 ui/dbus: remove mouse handler on dispose
In reply to Marc-André Lureau--- ui/dbus-console.c | 1 + 1 file changed, 1 insertion(+)
ui/dbus-console.c +1 -0
@@ -151,6 +151,7 @@ dbus_display_console_dispose(GObject *object)DBusDisplayConsole *ddc =DBUS_DISPLAY_CONSOLE (object);qemu_console_unregister_listener (&ddc->dcl); +qemu_remove_mouse_mode_change_notifier (&ddc->mouse_mode_notifier);g_clear_object (&ddc->iface_touch);g_clear_object (&ddc->iface_mouse);g_clear_object (&ddc->iface_kbd);
Signature · 1 line
2.54.0
PATCH 8/32 ui/qmp: keep a reference of console across yield
In reply to Marc-André LureauWhile the coroutine is waiting, the console could be finalized. Keep a reference to prevent this.
--- ui/ui-qmp-cmds.c | 3 +++ 1 file changed, 3 insertions(+)
ui/ui-qmp-cmds.c +3 -0
@@ -348,6 +348,7 @@ qmp_screendump(const char *filename, const char *device, } } +object_ref (con);qemu_console_co_wait_update (con);/*
@@ -358,9 +359,11 @@ qmp_screendump(const char *filename, const char *device, surface =qemu_console_surface (con);if (!surface) {error_setg (errp,"no surface" ); +object_unref (con);return ; } image =pixman_image_ref (surface->image); +object_unref (con); fd =qemu_create (filename,O_WRONLY |O_TRUNC |O_BINARY ,0666 , errp);if (fd ==-1 ) {
Signature · 1 line
2.54.0
PATCH 9/32 ui: stop ui timer when closing
In reply to Marc-André Lureauhwops is reset, so if the UI timer is pending it will crash.
--- ui/console.c | 1 + 1 file changed, 1 insertion(+)
ui/console.c +1 -0
@@ -1136,6 +1136,7 @@ void qemu_graphic_console_close(QemuConsole *con)trace_console_gfx_close (con->index);object_property_set_link (OBJECT (con),"device" ,NULL , &error_abort);qemu_graphic_console_set_hwops (con, &unused_ops,NULL ); +timer_del (con->ui_timer);if (con->gl) {qemu_console_gl_scanout_disable (con);
Signature · 1 line
2.54.0
PATCH 10/32 ui/console: init gl_unblock_timer in qemu_console_init
In reply to Marc-André LureauMove gl_unblock_timer allocation from graphic_console_init() to qemu_console_init(), similar to what was done in commit cfde05d15b ("ui/console: allocate ui_timer in QemuConsole").
This fixes leaking timers on console recycling.
--- ui/console.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ui/console.c +2 -2
@@ -433,6 +433,8 @@ qemu_console_init(Object *obj) c->window_id =-1 ; c->ui_timer =timer_new_ms (QEMU_CLOCK_REALTIME , dpy_set_ui_info_timer, c); + c->gl_unblock_timer =timer_new_ms (QEMU_CLOCK_REALTIME , + console_hw_gl_unblock_timer, c);qemu_console_register (c); }
@@ -1116,8 +1118,6 @@ QemuConsole *qemu_graphic_console_create(DeviceState *dev, uint32_t head, surface =qemu_create_placeholder_surface (width, height, noinit);qemu_console_set_surface (s, surface); - s->gl_unblock_timer =timer_new_ms (QEMU_CLOCK_REALTIME , - console_hw_gl_unblock_timer, s);return s; }
Signature · 1 line
2.54.0
PATCH 11/32 ui/spice: remove dead spice_displays
In reply to Marc-André LureauThis is left-over from commit 9fa032866da ("spice: fix multihead support")
--- include/ui/qemu-spice.h | 7 ------- 1 file changed, 7 deletions(-)
include/ui/qemu-spice.h +0 -7
@@ -33,13 +33,6 @@ bool qemu_spice_have_display_interface(QemuConsole *con);int qemu_spice_add_display_interface (QXLInstance *qxlin,QemuConsole *con);int qemu_spice_migrate_info (const char *hostname,int port,int tls_port,const char *subject); - -#else /* CONFIG_SPICE */ - -#include "qemu/error-report.h" - -#define spice_displays 0 -#endif /* CONFIG_SPICE */ static inline bool qemu_using_spice (Error **errp)
Signature · 1 line
2.54.0
PATCH 12/32 ui/spice: add cleanup on shutdown
In reply to Marc-André LureauSPICE resources were never freed on shutdown. Add per-subsystem cleanup (display, input, core) and call it from qemu_cleanup().
Move spice-module.c into libui so the qemu_spice ops table links with the rest of the UI code. Add an LSan suppression for a known spice-server leak.
--- include/ui/qemu-spice-module.h | 1 + include/ui/qemu-spice.h | 2 ++ system/runstate.c | 4 ++++ ui/spice-core.c | 25 ++++++++++++++++++-- ui/spice-display.c | 52 ++++++++++++++++++++++++++++++++++++++++++ ui/spice-input.c | 52 ++++++++++++++++++++++++++++-------------- ui/spice-module.c | 5 ++++ scripts/lsan_suppressions.txt | 5 ++++ ui/meson.build | 4 ++-- 9 files changed, 129 insertions(+), 21 deletions(-)
include/ui/qemu-spice-module.h +1 -0
@@ -26,6 +26,7 @@ typedef struct SpiceInfo SpiceInfo;struct QemuSpiceOps {void (*init)(void ); +void (*cleanup)(void );void (*display_init)(void );int (*migrate_info)(const char *h,int p,int t,const char *s); int (*set_passwd)(const char *passwd,
include/ui/qemu-spice.h +2 -0
@@ -27,7 +27,9 @@#include "qemu/config-file.h" void qemu_spice_input_init (void ); +void qemu_spice_input_cleanup (void );void qemu_spice_display_init (void ); +void qemu_spice_display_cleanup (void );void qemu_spice_display_init_done (void );bool qemu_spice_have_display_interface (QemuConsole *con);int qemu_spice_add_display_interface (QXLInstance *qxlin,QemuConsole *con);
system/runstate.c +4 -0
@@ -62,6 +62,7 @@#include "system/system.h" #include "system/tpm.h" #include "ui/console.h" +#include "ui/qemu-spice-module.h" #include "trace.h"
@@ -1048,6 +1049,9 @@ void qemu_cleanup(int status)user_creatable_cleanup ();#ifdef CONFIG_VNC vnc_cleanup (); +#endif +#ifdef CONFIG_SPICE + qemu_spice.cleanup ();#endif /* TODO: unref root container, check all devices are ok */ }
ui/spice-core.c +23 -2
@@ -651,12 +651,15 @@ static void vm_change_state_handler(void *opaque, bool running, } } +static VMChangeStateEntry *vm_change_entry; +void qemu_spice_display_init_done (void ) {if (runstate_is_running ()) {qemu_spice_display_start (); } -qemu_add_vm_change_state_handler (vm_change_state_handler,NULL ); + vm_change_entry = +qemu_add_vm_change_state_handler (vm_change_state_handler,NULL ); }static void qemu_spice_init (void )
@@ -894,7 +897,8 @@ static int qemu_spice_add_interface(SpiceBaseInstance *sin) spice_server =spice_server_new ();spice_server_set_sasl_appname (spice_server,"qemu" );spice_server_init (spice_server, &core_interface); -qemu_add_vm_change_state_handler (vm_change_state_handler,NULL ); + vm_change_entry = +qemu_add_vm_change_state_handler (vm_change_state_handler,NULL ); }return spice_server_add_interface (spice_server, sin);
@@ -1005,8 +1009,25 @@ int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)return spice_display_is_running; } +static void qemu_spice_cleanup (void ) +{ +if (!spice_server) { +return ; + } + +qemu_spice_display_cleanup (); +qemu_spice_input_cleanup (); +migration_remove_notifier (&migration_state); +g_clear_pointer (&spice_consoles, g_slist_free); +g_clear_pointer (&auth_passwd, g_free); +g_clear_pointer (&spice_server, spice_server_destroy); +g_clear_pointer (&vm_change_entry, qemu_del_vm_change_state_handler); + using_spice =0 ; +} +static struct QemuSpiceOps real_spice_ops = { .init = qemu_spice_init, + .cleanup = qemu_spice_cleanup, .display_init = qemu_spice_display_init, .migrate_info = qemu_spice_migrate_info, .set_passwd = qemu_spice_set_passwd,
ui/spice-display.c +52 -0
70 lines not on this page · raw message
ui/spice-input.c +35 -17
58 lines not on this page · raw message
ui/spice-module.c +5 -0
@@ -62,6 +62,10 @@ static int qemu_spice_display_add_client_stub(int csock, int skipauth,return -1 ; } +static void qemu_spice_cleanup_stub (void ) +{ +} +struct QemuSpiceOps qemu_spice = { .init = qemu_spice_init_stub, .display_init = qemu_spice_display_init_stub,
@@ -69,6 +73,7 @@ struct QemuSpiceOps qemu_spice = { .set_passwd = qemu_spice_set_passwd_stub, .set_pw_expire = qemu_spice_set_pw_expire_stub, .display_add_client = qemu_spice_display_add_client_stub, + .cleanup = qemu_spice_cleanup_stub, };#ifdef CONFIG_SPICE
scripts/lsan_suppressions.txt +5 -0
@@ -20,3 +20,8 @@ leak:libfontconfig.so # https://github.com/GNOME/glib/blob/main/tools/glib.supp # This avoids false positive leak reports for the qga-ssh-test. leak:g_set_user_dirs + +# spice_server_add_interface allocates internal channel data that +# spice_server_destroy does not free +# https://gitlab.freedesktop.org/spice/spice/-/merge_requests/246 +leak:spice_server_add_interface
ui/meson.build +2 -2
@@ -42,13 +42,14 @@ libui_sources = files( 'kbd-state.c', 'keymaps.c', 'qemu-pixman.c', + 'spice-module.c', 'vgafont.c', ) if pixman.found() libui_sources += files('cp437.c', 'vt100.c') endif libui = static_library('qemuui', libui_sources + genh, - dependencies: [pixman], + dependencies: [pixman, spice_headers], build_by_default: false) ui = declare_dependency(objects: libui.extract_all_objects(recursive: false), dependencies: [pixman]) system_ss.add(png)
@@ -65,7 +66,6 @@ system_ss.add(when: pixman, if_true: files('console-vc.c'), if_false: files('con if dbus_display system_ss.add(files('dbus-module.c')) endif -system_ss.add([spice_headers, files('spice-module.c')]) system_ss.add(when: spice_protocol, if_true: files('vdagent.c')) if host_os == 'linux'
Signature · 1 line
2.54.0
PATCH 13/32 ui: add display cleanup infrastructure
In reply to Marc-André LureauAdd a cleanup callback to QemuDisplay and a qemu_display_cleanup() function that iterates all registered display types and calls their cleanup handler. Wire it into qemu_cleanup() in runstate.c, replacing the ad-hoc vnc_cleanup() call.
This provides a structured alternative to atexit() handlers, giving deterministic teardown ordering and making resource leaks visible to sanitizers.
The cleanup should happen before user_creatable_cleanup(), since some display have weak user-creatable references to cleanup before.
--- include/ui/console.h | 2 ++ system/runstate.c | 8 +------- ui/console.c | 20 +++++++++++++++++++- 3 files changed, 22 insertions(+), 8 deletions(-)
include/ui/console.h +2 -0
@@ -430,6 +430,7 @@ struct QemuDisplay {DisplayType type;void (*early_init)(DisplayOptions *opts);void (*init)(DisplayState *ds,DisplayOptions *opts); +void (*cleanup)(void );const char *vc; };
@@ -438,6 +439,7 @@ bool qemu_display_find_default(DisplayOptions *opts);void qemu_display_early_init (DisplayOptions *opts);void qemu_display_init (DisplayState *ds,DisplayOptions *opts);const char *qemu_display_get_vc (DisplayOptions *opts); +void qemu_display_cleanup (void );void qemu_display_help (void );/* vnc.c */
system/runstate.c +1 -7
@@ -62,7 +62,6 @@#include "system/system.h" #include "system/tpm.h" #include "ui/console.h" -#include "ui/qemu-spice-module.h" #include "trace.h"
@@ -1046,12 +1045,7 @@ void qemu_cleanup(int status)audio_cleanup ();monitor_cleanup ();qemu_chr_cleanup (); +qemu_display_cleanup ();user_creatable_cleanup (); -#ifdef CONFIG_VNC -vnc_cleanup (); -#endif -#ifdef CONFIG_SPICE - qemu_spice.cleanup (); -#endif /* TODO: unref root container, check all devices are ok */ }
ui/console.c +19 -1
@@ -42,6 +42,7 @@#include "qemu/memfd.h" #include "ui/vt100.h" #include "vgafont.h" +#include "ui/qemu-spice.h" #include "console-priv.h"
@@ -575,7 +576,7 @@ void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl) {/* display has opengl support */ assert (con); -if (con->gl) { +if (gl && con->gl) {error_report ("The console already has an OpenGL context." );exit (1 ); }
@@ -1414,6 +1415,23 @@ void qemu_display_init(DisplayState *ds, DisplayOptions *opts) dpys[opts->type]->init (ds, opts); } +void qemu_display_cleanup (void ) +{ +int i; + +for (i =0 ; i <DISPLAY_TYPE__MAX ; i++) { +if (dpys[i] && dpys[i]->cleanup) { + dpys[i]->cleanup (); + } + } +#ifdef CONFIG_VNC +vnc_cleanup (); +#endif +#ifdef CONFIG_SPICE + qemu_spice.cleanup (); +#endif +} +const char *qemu_display_get_vc (DisplayOptions *opts) {#ifdef CONFIG_PIXMAN
Signature · 1 line
2.54.0
PATCH 14/32 ui/curses: implement display cleanup
In reply to Marc-André LureauReplace the atexit() handler with a proper cleanup callback. The new curses_cleanup() unregisters the display listener, destroy & free the allocated resources.
--- ui/curses.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
ui/curses.c +12 -5
@@ -411,11 +411,19 @@ static void curses_refresh(DisplayChangeListener *dcl) } } -static void curses_atexit (void ) +static void curses_cleanup (void ) { +if (!dcl) { +return ; + } +endwin (); -g_free (vga_to_curses); -g_free (screen); +qemu_console_unregister_listener (dcl); +g_clear_pointer (&dcl, g_free); +g_clear_pointer (&screenpad, delwin); +g_clear_pointer (&vga_to_curses, g_free); +g_clear_pointer (&screen, g_free); +g_clear_pointer (&kbd_layout, kbd_layout_free); }/*
@@ -799,8 +807,6 @@ static void curses_display_init(DisplayState *ds, DisplayOptions *opts) vga_to_curses =g_new0 (cchar_t,256 );curses_setup ();curses_keyboard_setup (); -atexit (curses_atexit); -curses_winch_init (); dcl =g_new0 (DisplayChangeListener,1 );
@@ -812,6 +818,7 @@ static void curses_display_init(DisplayState *ds, DisplayOptions *opts)static QemuDisplay qemu_display_curses = { .type =DISPLAY_TYPE_CURSES , .init = curses_display_init, + .cleanup = curses_cleanup, };static void register_curses (void )
Signature · 1 line
2.54.0
PATCH 15/32 ui/sdl2: implement display cleanup
In reply to Marc-André LureauReplace the atexit() handler with a proper cleanup callback. Extend sdl_cleanup() to unregister display listeners, free keyboard state, destroy windows, and clean up all cursor resources.
--- ui/sdl2.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
ui/sdl2.c +19 -4
@@ -788,9 +788,25 @@ static void sdl_mouse_define(DisplayChangeListener *dcl,static void sdl_cleanup (void ) { -if (guest_sprite) { -SDL_FreeCursor (guest_sprite); +int i; + +if (!sdl2_console) { +return ; } + +qemu_remove_mouse_mode_change_notifier (&mouse_mode_notifier); + +for (i =0 ; i < sdl2_num_outputs; i++) { +qemu_console_unregister_listener (&sdl2_console[i].dcl); +qkbd_state_free (sdl2_console[i].kbd); +sdl2_window_destroy (&sdl2_console[i]); + } +g_clear_pointer (&sdl2_console, g_free); + sdl2_num_outputs =0 ; + +g_clear_pointer (&guest_sprite, SDL_FreeCursor); +g_clear_pointer (&guest_sprite_surface, SDL_FreeSurface); +g_clear_pointer (&sdl_cursor_hidden, SDL_FreeCursor);SDL_QuitSubSystem (SDL_INIT_VIDEO ); }
@@ -995,8 +1011,6 @@ static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)sdl_grab_start (&sdl2_console[0 ]); } -atexit (sdl_cleanup); -/* SDL's event polling (in dpy_refresh) must happen on the main thread. */ qemu_main =NULL ; }
@@ -1005,6 +1019,7 @@ static QemuDisplay qemu_display_sdl2 = { .type = DISPLAY_TYPE_SDL, .early_init = sdl2_display_early_init, .init = sdl2_display_init, + .cleanup = sdl_cleanup, };static void register_sdl1 (void )
Signature · 1 line
2.54.0
PATCH 16/32 ui/spice-app: implement display cleanup
In reply to Marc-André LureauReplace the atexit() handler with the display cleanup callback, reusing the existing spice_app_atexit() function.
--- ui/spice-app.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
ui/spice-app.c +5 -5
@@ -119,16 +119,17 @@ static const TypeInfo char_vc_type_info = { .class_size = sizeof(VCChardevClass), }; -static void spice_app_atexit (void ) +static void spice_app_cleanup (void ) {if (sock_path) {unlink (sock_path); +g_clear_pointer (&sock_path, g_free); }if (tmp_dir) {rmdir (tmp_dir); + tmp_dir =NULL ; } -g_free (sock_path); -g_free (app_dir); +g_clear_pointer (&app_dir, g_free); }static void spice_app_display_early_init (DisplayOptions *opts)
@@ -146,8 +147,6 @@ static void spice_app_display_early_init(DisplayOptions *opts)exit (1 ); } -atexit (spice_app_atexit); -if (qemu_name) { app_dir =g_build_filename (g_get_user_runtime_dir (),"qemu" , qemu_name,NULL );
@@ -218,6 +217,7 @@ static QemuDisplay qemu_display_spice_app = { .type = DISPLAY_TYPE_SPICE_APP, .early_init = spice_app_display_early_init, .init = spice_app_display_init, + .cleanup = spice_app_cleanup, .vc = "vc", };
Signature · 1 line
2.54.0
PATCH 17/32 ui/egl: implement display and EGL cleanup
In reply to Marc-André LureauAdd egl_cleanup() to tear down the EGL render node context, GBM device, and EGL display. Add egl_headless_cleanup() to unregister listeners, destroy framebuffers, and free per-console state tracked via a new GPtrArray.
--- include/ui/egl-helpers.h | 1 + ui/egl-headless.c | 31 +++++++++++++++++++++++++++++++ ui/egl-helpers.c | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+)
include/ui/egl-helpers.h +1 -0
@@ -76,6 +76,7 @@ EGLContext qemu_egl_init_ctx(void);bool qemu_egl_has_dmabuf (void );bool egl_init (const char *rendernode,DisplayGLMode mode,Error **errp); +void egl_cleanup (void );const char *qemu_egl_get_error_string (void );
ui/egl-headless.c +31 -0
@@ -9,6 +9,7 @@ typedef struct egl_dpy { DisplayChangeListener dcl; + DisplayGLCtx *ctx; DisplaySurface *ds; QemuGLShader *gls; egl_fb guest_fb;
@@ -19,6 +20,8 @@ typedef struct egl_dpy {uint32_t pos_y; } egl_dpy; +static GPtrArray *egl_dpys; +/* ------------------------------------------------------------------ */ static void egl_refresh (DisplayChangeListener *dcl)
@@ -220,6 +223,8 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)egl_dpy *edpy;int idx; + egl_dpys =g_ptr_array_new (); +for (idx =0 ;; idx++) {DisplayGLCtx *ctx;
@@ -232,15 +237,41 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts) edpy->gls =qemu_gl_init_shader (); ctx =g_new0 (DisplayGLCtx,1 ); ctx->ops = &eglctx_ops; + edpy->ctx = ctx;qemu_console_set_display_gl_ctx (con, ctx);qemu_console_register_listener (con, &edpy->dcl, &egl_ops); +g_ptr_array_add (egl_dpys, edpy); + } +} + +static void egl_headless_cleanup (void ) +{ +if (!egl_dpys) { +return ; } + +for (guint i =0 ; i < egl_dpys->len; i++) { +egl_dpy *edpy =g_ptr_array_index (egl_dpys, i); + +qemu_console_unregister_listener (&edpy->dcl); +qemu_console_set_display_gl_ctx (edpy->dcl.con,NULL ); +egl_fb_destroy (&edpy->guest_fb); +egl_fb_destroy (&edpy->cursor_fb); +egl_fb_destroy (&edpy->blit_fb); +qemu_gl_fini_shader (edpy->gls); +g_free (edpy->ctx); +g_free (edpy); + } +g_clear_pointer (&egl_dpys, g_ptr_array_unref); + +egl_cleanup (); }static QemuDisplay qemu_display_egl = { .type =DISPLAY_TYPE_EGL_HEADLESS , .early_init = early_egl_headless_init, .init = egl_headless_init, + .cleanup = egl_headless_cleanup, };static void register_egl (void )
ui/egl-helpers.c +19 -0
@@ -733,3 +733,22 @@ bool egl_init(const char *rendernode, DisplayGLMode mode, Error **errp) display_opengl =1 ;return true; } + +void egl_cleanup (void ) +{ +if (qemu_egl_rn_ctx) { +eglDestroyContext (qemu_egl_display, qemu_egl_rn_ctx); + qemu_egl_rn_ctx =NULL ; + } + +#ifdef CONFIG_GBM +g_clear_pointer (&qemu_egl_rn_gbm_dev, gbm_device_destroy); +g_clear_fd (&qemu_egl_rn_fd,NULL ); +#endif + +if (qemu_egl_display) { +eglReleaseThread (); +eglTerminate (qemu_egl_display); + qemu_egl_display =NULL ; + } +}
Signature · 1 line
2.54.0
PATCH 18/32 ui/cocoa: implement display cleanup
In reply to Marc-André LureauMove cbowner release from QemuCocoaAppController -dealloc to cocoa_display_cleanup(), since cbowner is allocated in cocoa_display_init() and cleanup is the symmetric teardown path.
--- ui/cocoa.m | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
ui/cocoa.m +17 -2
@@ -1331,8 +1331,6 @@ - (void) dealloc COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); [cocoaView release]; - [cbowner release]; - cbowner = nil; [super dealloc]; }
@@ -2163,9 +2161,26 @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts) qemu_main = cocoa_main; } +static void cocoa_display_cleanup(void) +{ + if (!kbd) { + return; + } + + qemu_console_unregister_listener(&dcl); + g_clear_pointer(&kbd, qkbd_state_free); + qemu_remove_mouse_mode_change_notifier(&mouse_mode_change_notifier); + qemu_clipboard_peer_unregister(&cbpeer); + g_clear_pointer(&cbinfo, qemu_clipboard_info_unref); + qemu_event_destroy(&cbevent); + [cbowner release]; + cbowner = nil; +} + static QemuDisplay qemu_display_cocoa = { .type = DISPLAY_TYPE_COCOA, .init = cocoa_display_init, + .cleanup = cocoa_display_cleanup, }; static void register_cocoa(void)
Signature · 1 line
2.54.0
PATCH 19/32 ui/dbus: implement display cleanup
In reply to Marc-André LureauAdd dbus_cleanup() to unparent the D-Bus display object, ensuring proper teardown before user_creatable_cleanup() runs.
--- ui/dbus-console.c | 3 +++ ui/dbus.c | 13 +++++++++++++ 2 files changed, 16 insertions(+)
ui/dbus-console.c +3 -0
@@ -151,6 +151,9 @@ dbus_display_console_dispose(GObject *object)DBusDisplayConsole *ddc =DBUS_DISPLAY_CONSOLE (object);qemu_console_unregister_listener (&ddc->dcl); +if (ddc->dcl.con) { +qemu_console_set_display_gl_ctx (ddc->dcl.con,NULL ); + }qemu_remove_mouse_mode_change_notifier (&ddc->mouse_mode_notifier);g_clear_object (&ddc->iface_touch);g_clear_object (&ddc->iface_mouse);
ui/dbus.c +13 -0
@@ -615,10 +615,23 @@ static const TypeInfo dbus_display_info = { } }; +static void +dbus_cleanup (void ) +{ +Object *o; + + o =object_resolve_path_component (object_get_objects_root (), +"dbus-display" ); +if (o) { +object_unparent (o); + } +} +static QemuDisplay qemu_display_dbus = { .type =DISPLAY_TYPE_DBUS , .early_init = early_dbus_init, .init = dbus_init, + .cleanup = dbus_cleanup, .vc ="vc" , };
Signature · 1 line
2.54.0
Re: [PATCH 19/32] ui/dbus: implement display cleanup
Quoted 2 lines · Marc-André Lureau
Add
dbus_cleanup()to unparent the D-Bus display object, ensuring proper teardown beforeuser_creatable_cleanup()runs.
"[PATCH 07/32] ui/dbus: remove mouse handler on dispose" removes the mouse handler, but the LED event handler still remains.
Regards,
Akihiko Odaki
Quoted 50 lines
Signed-off-by Marc-André Lureau <[email protected]>--- ui/dbus-console.c | 3 +++ ui/dbus.c | 13 +++++++++++++ 2 files changed, 16 insertions(+)ui/dbus-console.c +3
-0@@ -151,6 +151,9 @@ dbus_display_console_dispose(GObject *object)DBusDisplayConsole *ddc =DBUS_DISPLAY_CONSOLE (object);qemu_console_unregister_listener (&ddc->dcl); +if (ddc->dcl.con) { +qemu_console_set_display_gl_ctx (ddc->dcl.con,NULL ); + }qemu_remove_mouse_mode_change_notifier (&ddc->mouse_mode_notifier);g_clear_object (&ddc->iface_touch);g_clear_object (&ddc->iface_mouse);ui/dbus.c +13
-0@@ -615,10 +615,23 @@ static const TypeInfo dbus_display_info = { } }; +static void +dbus_cleanup (void ) +{ +Object *o; + + o =object_resolve_path_component (object_get_objects_root (), +"dbus-display" ); +if (o) { +object_unparent (o); + } +} +static QemuDisplay qemu_display_dbus = { .type =DISPLAY_TYPE_DBUS , .early_init = early_dbus_init, .init = dbus_init, + .cleanup = dbus_cleanup, .vc ="vc" , };
PATCH 20/32 ui/gtk: implement display cleanup
In reply to Marc-André LureauAdd gtk_display_cleanup() to properly tear down GTK display state:
remove console and mouse notifiers, unregister clipboard peer, destroy the main window and virtual consoles.
--- include/ui/gtk.h | 1 + ui/gtk-clipboard.c | 15 +++++++++++++++ ui/gtk.c | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+)
include/ui/gtk.h +1 -0
@@ -225,6 +225,7 @@ int gd_gl_area_make_current(DisplayGLCtx *dgc,/* gtk-clipboard.c */ void gd_clipboard_init (GtkDisplayState *gd); +void gd_clipboard_cleanup (GtkDisplayState *gd);void gd_update_scale (VirtualConsole *vc,int ww,int wh,int fbw,int fbh);
ui/gtk-clipboard.c +15 -0
@@ -235,3 +235,18 @@ void gd_clipboard_init(GtkDisplayState *gd)g_signal_connect (gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY ],"owner-change" ,G_CALLBACK (gd_owner_change), gd); } + +void gd_clipboard_cleanup (GtkDisplayState *gd) +{ +if (!gd->cbpeer.name) { +return ; + } +qemu_clipboard_peer_unregister (&gd->cbpeer); +g_signal_handlers_disconnect_by_data ( + gd->gtkcb[QEMU_CLIPBOARD_SELECTION_CLIPBOARD ], gd); +g_signal_handlers_disconnect_by_data ( + gd->gtkcb[QEMU_CLIPBOARD_SELECTION_PRIMARY ], gd); +g_signal_handlers_disconnect_by_data ( + gd->gtkcb[QEMU_CLIPBOARD_SELECTION_SECONDARY ], gd); + gd->cbpeer.name =NULL ; +}
ui/gtk.c +18 -0
@@ -2505,6 +2505,7 @@ static void gd_create_menus(GtkDisplayState *s, DisplayOptions *opts) } +static GtkDisplayState *gtk_display_state;static gboolean gtkinit;static void gtk_display_init (DisplayState *ds,DisplayOptions *opts)
@@ -2523,6 +2524,7 @@ static void gtk_display_init(DisplayState *ds, DisplayOptions *opts) }assert (opts->type ==DISPLAY_TYPE_GTK ); s =g_malloc0 (sizeof (*s)); + gtk_display_state = s; s->opts = opts; theme =gtk_icon_theme_get_default ();
@@ -2681,10 +2683,26 @@ static void early_gtk_display_init(DisplayOptions *opts)#endif } +static void gtk_display_cleanup (void ) +{ +GtkDisplayState *s = gtk_display_state; + +if (!s) { +return ; + } + +qemu_remove_mouse_mode_change_notifier (&s->mouse_mode_notifier); +gd_clipboard_cleanup (s); +g_clear_pointer (&s->window, gtk_widget_destroy); +g_clear_object (&s->null_cursor); +g_clear_pointer (>k_display_state, g_free); +} +static QemuDisplay qemu_display_gtk = { .type =DISPLAY_TYPE_GTK , .early_init = early_gtk_display_init, .init = gtk_display_init, + .cleanup = gtk_display_cleanup, .vc ="vc" , };
Signature · 1 line
2.54.0
PATCH 21/32 ui/console: add console event notifier infrastructure
In reply to Marc-André LureauAdd a NotifierList to DisplayState so display backends can be notified when consoles are added or removed at runtime.
No events are fired yet, that follows in the next commits.
--- include/ui/console.h | 18 ++++++++++++++++++ ui/console.c | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+)
include/ui/console.h +18 -0
@@ -442,6 +442,24 @@ const char *qemu_display_get_vc(DisplayOptions *opts);void qemu_display_cleanup (void );void qemu_display_help (void ); +/* +* Console notifications: +* Handlers must be idempotent, it may notify multiple times. +*/ +typedef enum { +QEMU_CONSOLE_ADDED , +QEMU_CONSOLE_REMOVED , +}QemuConsoleEventType ; + +typedef struct QemuConsoleEvent { +QemuConsoleEventType type; +QemuConsole *con; +}QemuConsoleEvent ; + +void qemu_console_add_notifier (Notifier *notifier); +void qemu_console_remove_notifier (Notifier *notifier); +void qemu_console_notify (QemuConsoleEventType type,QemuConsole *con); +/* vnc.c */ void vnc_display_add_client (const char *id,int csock,bool skipauth);int vnc_display_password (const char *id,const char *password,Error **errp);
ui/console.c +23 -0
@@ -70,6 +70,7 @@ struct DisplayState {bool refreshing; QLIST_HEAD(, DisplayChangeListener) listeners; +NotifierList console_notifiers; };static DisplayState *display_state;
@@ -84,6 +85,16 @@ static bool console_compatible_with(QemuConsole *con,static QemuConsole *qemu_graphic_console_lookup_unused (void );static void dpy_set_ui_info_timer (void *opaque); +void qemu_console_notify (QemuConsoleEventType type,QemuConsole *con) +{ +DisplayState *ds =get_alloc_displaystate (); +QemuConsoleEvent event = { + .type = type, + .con = con, + }; +notifier_list_notify (&ds->console_notifiers, &event); +} +static void gui_update (void *opaque) {uint64_t interval =GUI_REFRESH_INTERVAL_IDLE ;
@@ -1056,10 +1067,22 @@ static DisplayState *get_alloc_displaystate(void) {if (!display_state) { display_state =g_new0 (DisplayState,1 ); +notifier_list_init (&display_state->console_notifiers); }return display_state; } +void qemu_console_add_notifier (Notifier *notifier) +{ +DisplayState *ds =get_alloc_displaystate (); +notifier_list_add (&ds->console_notifiers, notifier); +} + +void qemu_console_remove_notifier (Notifier *notifier) +{ +notifier_remove (notifier); +} +/* * Called by main(), after creating QemuConsoles * and before initializing ui (sdl/vnc/...).
Signature · 1 line
2.54.0
PATCH 22/32 ui/console: fire console ADDED/REMOVED notifications
In reply to Marc-André LureauFire CONSOLE_ADDED at the end of graphic_console_init() and CONSOLE_REMOVED at the start of graphic_console_close(), so display backends can react to console hotplug/unplug events.
REMOVED fires before the device link is cleared and before the placeholder surface swap, so handlers can unregister their DCL while the console is still in a known state.
--- ui/console.c | 2 ++ 1 file changed, 2 insertions(+)
ui/console.c +2 -0
@@ -1142,6 +1142,7 @@ QemuConsole *qemu_graphic_console_create(DeviceState *dev, uint32_t head, surface =qemu_create_placeholder_surface (width, height, noinit);qemu_console_set_surface (s, surface); +qemu_console_notify (QEMU_CONSOLE_ADDED , s);return s; }
@@ -1158,6 +1159,7 @@ void qemu_graphic_console_close(QemuConsole *con)int height =qemu_console_get_height (con,480 );trace_console_gfx_close (con->index); +qemu_console_notify (QEMU_CONSOLE_REMOVED , con);object_property_set_link (OBJECT (con),"device" ,NULL , &error_abort);qemu_graphic_console_set_hwops (con, &unused_ops,NULL );timer_del (con->ui_timer);
Signature · 1 line
2.54.0
PATCH 23/32 ui/console-vc: fire ADDED/REMOVED notifications
In reply to Marc-André LureauFire CONSOLE_ADDED when the chardev is opened.
Fire CONSOLE_REMOVED in char_vc_finalize() before dropping the console reference, so the console is still in a valid state when listeners handle the event. Also fixes a console object leak by adding the missing object_unref().
--- ui/console-vc.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
ui/console-vc.c +13 -0
@@ -255,6 +255,7 @@ static bool vc_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) }qemu_chr_be_event (chr,CHR_EVENT_OPENED ); +qemu_console_notify (QEMU_CONSOLE_ADDED ,QEMU_CONSOLE (s));return true; }
@@ -327,12 +328,24 @@ static void char_vc_init(Object *obj) vc->encoding =CHARDEV_VC_ENCODING_UTF8 ; } +static void char_vc_finalize (Object *obj) +{ +VCChardev *vc =VC_CHARDEV (obj); +QemuConsole *con =QEMU_CONSOLE (vc->console); + +if (con) { +qemu_console_notify (QEMU_CONSOLE_REMOVED , con); +object_unref (con); + } +} +static const TypeInfo char_vc_type_info = { .name =TYPE_CHARDEV_VC , .parent =TYPE_CHARDEV , .instance_size =sizeof (VCChardev), .instance_init = char_vc_init, .instance_post_init = object_apply_compat_props, + .instance_finalize = char_vc_finalize, .class_init = char_vc_class_init, };
Signature · 1 line
2.54.0
PATCH 24/32 ui/gtk: convert VirtualConsole storage from fixed array to GPtrArray
In reply to Marc-André LureauReplace the fixed-size vc[MAX_VCS] with GPtrArray.
This is a preparatory refactoring for console hotplug support, which needs to add/remove VCs dynamically.
--- include/ui/gtk.h | 3 +- ui/gtk.c | 102 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 81 insertions(+), 24 deletions(-)
include/ui/gtk.h +1 -2
@@ -118,8 +118,7 @@ struct GtkDisplayState {GtkWidget *grab_item;GtkWidget *grab_on_hover_item; -int nb_vcs; -VirtualConsole vc[MAX_VCS ]; +GPtrArray *vcs;GtkWidget *show_tabs_item;GtkWidget *untabify_item;
ui/gtk.c +80 -22
172 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH 24/32] ui/gtk: convert VirtualConsole storage from fixed array to GPtrArray
Quoted 149 lines · Marc-André Lureau
Replace the fixed-size vc[MAX_VCS] with GPtrArray.
This is a preparatory refactoring for console hotplug support, which needs to add/remove VCs dynamically.
Signed-off-by Marc-André Lureau <[email protected]>--- include/ui/gtk.h | 3 +- ui/gtk.c | 102 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 81 insertions(+), 24 deletions(-)include/ui/gtk.h +1
-2@@ -118,8 +118,7 @@ struct GtkDisplayState {GtkWidget *grab_item;GtkWidget *grab_on_hover_item; -int nb_vcs; -VirtualConsole vc[MAX_VCS ]; +GPtrArray *vcs;GtkWidget *show_tabs_item;GtkWidget *untabify_item;ui/gtk.c +48
-15@@ -150,8 +150,8 @@ static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)VirtualConsole *vc;gint i; -for (i =0 ; i < s->nb_vcs; i++) { - vc = &s->vc[i]; +for (i =0 ; i < s->vcs->len; i++) { + vc =g_ptr_array_index (s->vcs, i);if (gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (vc->menu_item))) {return vc;@@ -165,8 +165,11 @@ static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)VirtualConsole *vc;gint i, p; -for (i =0 ; i < s->nb_vcs; i++) { - vc = &s->vc[i]; +if (!s->vcs) { +return NULL ; + } +for (i =0 ; i < s->vcs->len; i++) { + vc =g_ptr_array_index (s->vcs, i); p =gtk_notebook_page_num (GTK_NOTEBOOK (s->notebook), vc->tab_item);if (p == page) {return vc;@@ -247,8 +250,8 @@ static void gd_update_caption(GtkDisplayState *s)gtk_window_set_title (GTK_WINDOW (s->window), title);g_free (title); -for (i =0 ; i < s->nb_vcs; i++) { -VirtualConsole *vc = &s->vc[i]; +for (i =0 ; i < s->vcs->len; i++) { +VirtualConsole *vc =g_ptr_array_index (s->vcs, i);if (!vc->window) {continue ;@@ -357,7 +360,7 @@ static void gtk_release_modifiers(GtkDisplayState *s) {VirtualConsole *vc =gd_vc_find_current (s); -if (vc->type !=GD_VC_GFX || +if (!vc || vc->type !=GD_VC_GFX || !qemu_console_is_graphic (vc->gfx.dcl.con)) {return ; }@@ -702,8 +705,8 @@ static void gd_mouse_mode_change(Notifier *notify, void *data)gd_ungrab_pointer (s); } } -for (i =0 ; i < s->nb_vcs; i++) { -VirtualConsole *vc = &s->vc[i]; +for (i =0 ; i < s->vcs->len; i++) { +VirtualConsole *vc =g_ptr_array_index (s->vcs, i);gd_update_cursor (vc); } }@@ -2114,9 +2117,10 @@ static void gd_vcs_init(GtkDisplayState *s, GSList *group,int i;for (i =0 ; i < nb_vcs; i++) { -VirtualConsole *vc = &s->vc[s->nb_vcs]; - group =gd_vc_vte_init (s, vc, vcs[i], s->nb_vcs, group, view_menu); - s->nb_vcs++; +VirtualConsole *vc =g_new0 (VirtualConsole,1 ); +g_ptr_array_add (s->vcs, vc); + group =gd_vc_vte_init (s, vc, vcs[i], s->vcs->len -1 , + group, view_menu); } }#endif /* CONFIG_VTE */ @@ -2441,13 +2445,14 @@ static GtkWidget *gd_create_menu_view(GtkDisplayState *s, DisplayOptions *opts)/* gfx */ for (vc =0 ;; vc++) { +VirtualConsole *v; con =qemu_console_lookup_by_index (vc);if (!con) {break ; } - group =gd_vc_gfx_init (s, &s->vc[vc], con, - vc, group, view_menu); - s->nb_vcs++; + v =g_new0 (VirtualConsole,1 ); +g_ptr_array_add (s->vcs, v); + group =gd_vc_gfx_init (s, v, con, vc, group, view_menu); }#if defined(CONFIG_VTE )@@ -2505,6 +2510,58 @@ static void gd_create_menus(GtkDisplayState *s, DisplayOptions *opts) } +static void gd_vc_free (void *p) +{ +VirtualConsole *vc = p; + +switch (vc->type) { +case GD_VC_GFX : +qemu_console_unregister_listener (&vc->gfx.dcl); +#if defined(CONFIG_OPENGL ) +if (display_opengl) { +qemu_console_set_display_gl_ctx (vc->gfx.dcl.con,NULL ); + } +if (vc->gfx.esurface) { +eglDestroySurface (qemu_egl_display, vc->gfx.esurface); + } +if (vc->gfx.ectx) { +eglDestroyContext (qemu_egl_display, vc->gfx.ectx); + } +if (vc->gfx.gls) { +surface_gl_destroy_texture (vc->gfx.gls, vc->gfx.ds); +qemu_gl_fini_shader (vc->gfx.gls); + } +egl_fb_destroy (&vc->gfx.guest_fb); +egl_fb_destroy (&vc->gfx.win_fb); +egl_fb_destroy (&vc->gfx.cursor_fb); +#endif +qkbd_state_free (vc->gfx.kbd); +if (vc->gfx.surface) { +cairo_surface_destroy (vc->gfx.surface);
ui/gtk.c static void gd_create_menus(GtkDisplayState *s, DisplayOptions *opts) · line 2550
Quoted 10 lines
ui/gtk.c +10
-0+ } +if (vc->gfx.convert) { +pixman_image_unref (vc->gfx.convert); + } +break ; +#if defined(CONFIG_VTE ) +case GD_VC_VTE : +fifo8_destroy (&vc->vte.out_fifo); +break ; +#endif
GCC emits a warning if !defined(CONFIG_VTE) because this doesn't handle GD_VC_VTE while the enum value is still present.
Regards,
Akihiko Odaki
Quoted 54 lines
ui/gtk.c +22
-7+ } + +if (vc->window) { +gtk_widget_destroy (vc->window); + }else if (vc->tab_item) { +gtk_widget_destroy (vc->tab_item); + } +if (vc->menu_item) { +gtk_widget_destroy (vc->menu_item); + } +g_free (vc->label); +g_free (vc); +} +static GtkDisplayState *gtk_display_state;static gboolean gtkinit;@@ -2525,6 +2582,7 @@ static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)assert (opts->type ==DISPLAY_TYPE_GTK ); s =g_malloc0 (sizeof (*s)); gtk_display_state = s; + s->vcs =g_ptr_array_new_with_free_func (gd_vc_free); s->opts = opts; theme =gtk_icon_theme_get_default ();@@ -2582,13 +2640,10 @@ static void gtk_display_init(DisplayState *ds, DisplayOptions *opts)gtk_widget_show_all (s->window); -for (idx =0 ;; idx++) { -QemuConsole *con =qemu_console_lookup_by_index (idx); -if (!con) { -break ; - } -if (s->vc[idx].type ==GD_VC_GFX ) { -gtk_widget_realize (s->vc[idx].gfx.drawing_area); +for (idx =0 ; idx < s->vcs->len; idx++) { +VirtualConsole *v =g_ptr_array_index (s->vcs, idx); +if (v->type ==GD_VC_GFX ) { +gtk_widget_realize (v->gfx.drawing_area); } }@@ -2693,6 +2748,9 @@ static void gtk_display_cleanup(void)qemu_remove_mouse_mode_change_notifier (&s->mouse_mode_notifier);gd_clipboard_cleanup (s); +g_signal_handlers_disconnect_by_func (s->notebook, +G_CALLBACK (gd_change_page), s); +g_clear_pointer (&s->vcs, g_ptr_array_unref);g_clear_pointer (&s->window, gtk_widget_destroy);g_clear_object (&s->null_cursor);g_clear_pointer (>k_display_state, g_free);
PATCH 25/32 ui/gtk: move global display settings out of per-console init
In reply to Marc-André LureauMove zoom_to_fit, keep_aspect_ratio and touch_slots initialisation from gd_vc_gfx_init() to gd_create_menu_view(). These are global display settings that should be set once after all consoles are created, not repeated on every per-console init (where the last iteration's values silently win).
--- ui/gtk.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-)
ui/gtk.c +23 -23
@@ -2269,8 +2269,6 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc, GSList *group, GtkWidget *view_menu) {const DisplayChangeListenerOps *ops = &dcl_ops; -bool zoom_to_fit = false; -int i; vc->label =qemu_console_get_label (con); vc->s = s;
@@ -2350,26 +2348,6 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,gd_connect_vc_gfx_signals (vc); group =gd_vc_menu_init (s, vc, idx, group, view_menu); -if (qemu_console_ui_info_supported (vc->gfx.dcl.con)) { - zoom_to_fit = true; - } -if (s->opts->u.gtk.has_zoom_to_fit) { - zoom_to_fit = s->opts->u.gtk.zoom_to_fit; - } -if (zoom_to_fit) { -gtk_menu_item_activate (GTK_MENU_ITEM (s->zoom_fit_item)); - s->free_scale = true; - } - - s->keep_aspect_ratio = true; -if (s->opts->u.gtk.has_keep_aspect_ratio) - s->keep_aspect_ratio = s->opts->u.gtk.keep_aspect_ratio; - -for (i =0 ; i <INPUT_EVENT_SLOTS_MAX ; i++) { -struct touch_slot *slot = &touch_slots[i]; - slot->tracking_id =-1 ; - } -return group; }
@@ -2379,7 +2357,8 @@ static GtkWidget *gd_create_menu_view(GtkDisplayState *s, DisplayOptions *opts)GtkWidget *view_menu;GtkWidget *separator;QemuConsole *con; -int vc; +bool zoom_to_fit = false; +int vc, i; view_menu =gtk_menu_new ();gtk_menu_set_accel_group (GTK_MENU (view_menu), s->accel_group);
@@ -2453,6 +2432,27 @@ static GtkWidget *gd_create_menu_view(GtkDisplayState *s, DisplayOptions *opts) v =g_new0 (VirtualConsole,1 );g_ptr_array_add (s->vcs, v); group =gd_vc_gfx_init (s, v, con, vc, group, view_menu); +if (qemu_console_ui_info_supported (con)) { + zoom_to_fit = true; + } + } + +if (s->opts->u.gtk.has_zoom_to_fit) { + zoom_to_fit = s->opts->u.gtk.zoom_to_fit; + } +if (zoom_to_fit) { +gtk_menu_item_activate (GTK_MENU_ITEM (s->zoom_fit_item)); + s->free_scale = true; + } + + s->keep_aspect_ratio = true; +if (s->opts->u.gtk.has_keep_aspect_ratio) { + s->keep_aspect_ratio = s->opts->u.gtk.keep_aspect_ratio; + } + +for (i =0 ; i <INPUT_EVENT_SLOTS_MAX ; i++) { +struct touch_slot *slot = &touch_slots[i]; + slot->tracking_id =-1 ; }#if defined(CONFIG_VTE )
Signature · 1 line
2.54.0
PATCH 26/32 ui/gtk: fix tab re-insertion order on window close
In reply to Marc-André LureauAdd gd_vc_notebook_pos() which computes the correct notebook position for a console by counting only non-detached (non-windowed) tabs before it. Use it in gd_tab_window_close() so a re-attached tab is inserted at its logical position rather than appended at the end.
--- ui/gtk.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
ui/gtk.c +24 -3
@@ -1464,16 +1464,37 @@ static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)gd_update_windowsize (vc); } +static int gd_vc_notebook_pos (GtkDisplayState *s,VirtualConsole *target) +{ +int pos =0 ; +guint i; + +for (i =0 ; i < s->vcs->len; i++) { +VirtualConsole *vc =g_ptr_array_index (s->vcs, i); +if (vc == target) { +return pos; + } +if (!vc->window) { + pos++; + } + } +g_assert_not_reached (); +} +static gboolean gd_tab_window_close (GtkWidget *widget,GdkEvent *event,void *opaque) {VirtualConsole *vc = opaque;GtkDisplayState *s = vc->s; +int page;gtk_widget_set_sensitive (vc->menu_item, true); -gd_widget_reparent (vc->window, s->notebook, vc->tab_item); -gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (s->notebook), - vc->tab_item, vc->label); +g_object_ref (vc->tab_item); +gtk_container_remove (GTK_CONTAINER (vc->window), vc->tab_item); + page =gd_vc_notebook_pos (s, vc); +gtk_notebook_insert_page (GTK_NOTEBOOK (s->notebook), + vc->tab_item,gtk_label_new (vc->label), page); +g_object_unref (vc->tab_item);gtk_widget_destroy (vc->window); vc->window =NULL ;#if defined(CONFIG_OPENGL )
Signature · 1 line
2.54.0
PATCH 27/32 ui/gtk: centralize console menu and shortcut management
In reply to Marc-André LureauReplace the per-console gd_vc_menu_init() with gd_rebuild_vc_menu() that tears down and rebuilds all console radio menu items and Ctrl+Alt+N accelerators at once. This is called from initialization and whenever consoles are detached or reattached.
Shortcuts now skip detached (windowed) consoles, so they always map to reachable tabs. Rename gd_vc_gfx_init() to add_gfx_console() and simplify the init function signatures now that menu creation is decoupled.
--- include/ui/gtk.h | 1 + ui/gtk.c | 127 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 85 insertions(+), 43 deletions(-)
include/ui/gtk.h +1 -0
@@ -120,6 +120,7 @@ struct GtkDisplayState {GPtrArray *vcs; +GtkWidget *vc_menu_separator;GtkWidget *show_tabs_item;GtkWidget *untabify_item;GtkWidget *show_menubar_item;
ui/gtk.c +84 -43
246 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH 28/32 ui/gtk: handle console hotplug/unplug events
In reply to Marc-André LureauRegister a console notifier so the GTK display dynamically creates and destroys VirtualConsole tabs when graphic consoles are added or removed at runtime (e.g. vfio-pci with display=on hotplug).
Add skips consoles that already have a VC binding, remove skips unknown consoles.
--- include/ui/gtk.h | 1 + ui/gtk.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-)
include/ui/gtk.h +1 -0
@@ -140,6 +140,7 @@ struct GtkDisplayState {GdkCursor *null_cursor;Notifier mouse_mode_notifier; +Notifier console_notifier;gboolean free_scale;gboolean keep_aspect_ratio;
ui/gtk.c +76 -1
101 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH 29/32 ui/console: register console in QOM tree dynamically
In reply to Marc-André LureauConsoles created after init_displaystate() (e.g. hotplugged display devices) were never added to the /backend/console[N] QOM tree. Extract qemu_console_add_to_qom() and call it from qemu_console_register() when the display is already initialized.
--- ui/console.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
ui/console.c +12 -5
@@ -376,6 +376,13 @@ void qemu_text_console_put_string(QemuTextConsole *s, const char *str, int len) } } +static void qemu_console_add_to_qom (QemuConsole *con) +{ + g_autofree gchar *name = g_strdup_printf("console[%d]", con->index); +object_property_add_child (object_get_container ("backend" ), + name,OBJECT (con)); +} +static void qemu_console_register (QemuConsole *c) {
@@ -413,6 +420,10 @@ qemu_console_register(QemuConsole *c) } } } + +if (phase_check (PHASE_MACHINE_READY )) { +qemu_console_add_to_qom (c); + } } static void
@@ -1089,17 +1100,13 @@ void qemu_console_remove_notifier(Notifier *notifier)*/ DisplayState *init_displaystate (void ) { -gchar *name;QemuConsole *con; QTAILQ_FOREACH(con, &consoles, next) {/* Hook up into the qom tree here (not in object_new()), once * all QemuConsoles are created and the order / numbering * doesn't change any more */ - name =g_strdup_printf ("console[%d]" , con->index); -object_property_add_child (object_get_container ("backend" ), - name,OBJECT (con)); -g_free (name); +qemu_console_add_to_qom (con); }return display_state;
Signature · 1 line
2.54.0
PATCH 30/32 ui/console: unregister console from QOM tree on close
In reply to Marc-André LureauCall object_unparent() in graphic_console_close() to remove the console from /backend/console[N]. This drops the QOM tree reference, while the initial object_new() reference keeps the console alive for potential reuse.
When graphic_console_init() reuses a closed console, re-register it in the QOM tree.
--- ui/console.c | 4 ++++ 1 file changed, 4 insertions(+)
ui/console.c +4 -0
@@ -1136,6 +1136,9 @@ QemuConsole *qemu_graphic_console_create(DeviceState *dev, uint32_t head,trace_console_gfx_reuse (s->index); width =qemu_console_get_width (s,0 ); height =qemu_console_get_height (s,0 ); +if (phase_check (PHASE_MACHINE_READY )) { +qemu_console_add_to_qom (s); + } }else {trace_console_gfx_new (); s = (QemuConsole *)object_new (TYPE_QEMU_GRAPHIC_CONSOLE );
@@ -1176,6 +1179,7 @@ void qemu_graphic_console_close(QemuConsole *con) } surface =qemu_create_placeholder_surface (width, height, unplugged);qemu_console_set_surface (con, surface); +object_unparent (OBJECT (con)); }QemuConsole *qemu_console_lookup_default (void )
Signature · 1 line
2.54.0
PATCH 31/32 ui/dbus: handle console hotplug/unplug events
In reply to Marc-André LureauSubscribe to QemuConsoleEvent notifications to dynamically add and remove D-Bus display consoles. This mirrors the GTK backend's handling added in the previous commits.
--- ui/dbus.h | 3 ++ ui/dbus-console.c | 5 +++ ui/dbus.c | 93 +++++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 84 insertions(+), 17 deletions(-)
ui/dbus.h +3 -0
@@ -60,6 +60,7 @@ struct DBusDisplay {DBusClipboardRequest clipboard_request[QEMU_CLIPBOARD_SELECTION__COUNT ];Notifier notifier; +Notifier console_notifier; };#ifdef WIN32
@@ -86,6 +87,8 @@ dbus_display_console_new(DBusDisplay *display, QemuConsole *con);int dbus_display_console_get_index (DBusDisplayConsole *ddc); +QemuConsole * +dbus_display_console_get_qemu_console (DBusDisplayConsole *ddc);extern const DisplayChangeListenerOps dbus_console_dcl_ops;
ui/dbus-console.c +5 -0
@@ -531,6 +531,11 @@ int dbus_display_console_get_index(DBusDisplayConsole *ddc)return qemu_console_get_index (ddc->dcl.con); } +QemuConsole *dbus_display_console_get_qemu_console (DBusDisplayConsole *ddc) +{ +return ddc->dcl.con; +} +DBusDisplayConsole *dbus_display_console_new (DBusDisplay *display,QemuConsole *con) {
ui/dbus.c +76 -17
143 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH 32/32 tests/qtest: add D-Bus display hotplug test
In reply to Marc-André LureauAdd a qtest that verifies display consoles are dynamically added and removed over D-Bus when a bochs-display device is hotplugged and unplugged on a q35 machine.
The test plugs device_add a bochs-display, waits for the DEVICE_ADDED QMP event, and checks that the D-Bus VM interface reports a second console. It then device_del it, forces a system reset
(q35 removal is ACPI-based and needs guest cooperation qtest cannot provide), waits for DEVICE_DELETED, and checks the console count again.
--- tests/qtest/dbus-display-test.c | 101 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-)
tests/qtest/dbus-display-test.c +99 -2
@@ -7,6 +7,8 @@#include <gio/gio.h> #include <gio/gunixfdlist.h> #include "libqtest.h" +#include "qobject/qdict.h" +#include "qobject/qstring.h" #include "ui/dbus-display1.h" static GDBusConnection*
@@ -38,11 +40,11 @@ test_dbus_p2p_from_fd(int fd) }static void -test_setup (QTestState **qts,GDBusConnection **conn) +test_setup_args (QTestState **qts,GDBusConnection **conn,const char *args) {int pair[2 ]; - *qts =qtest_init ("-display dbus,p2p=yes -name dbus-test" ); + *qts =qtest_init (args); g_assert_cmpint(qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, pair), ==, 0);
@@ -52,6 +54,12 @@ test_setup(QTestState **qts, GDBusConnection **conn)g_dbus_connection_start_message_processing (*conn); } +static void +test_setup (QTestState **qts,GDBusConnection **conn) +{ +test_setup_args (qts, conn,"-display dbus,p2p=yes -name dbus-test" ); +} +static void test_dbus_display_vm (void ) {
@@ -360,6 +368,92 @@ test_dbus_display_keyboard(void)qtest_quit (qts); } +static gsize +get_console_ids_count (GDBusConnection *conn) +{ +g_autoptr (GError) err =NULL ; +g_autoptr (QemuDBusDisplay1VMProxy) vm =NULL ; +GVariant *console_ids; +gsize n_ids =0 ; + + vm =QEMU_DBUS_DISPLAY1_VM_PROXY ( +qemu_dbus_display1_vm_proxy_new_sync ( + conn, +G_DBUS_PROXY_FLAGS_NONE , +NULL , +DBUS_DISPLAY1_ROOT "/VM" , +NULL , + &err)); +g_assert_no_error (err); + + console_ids =qemu_dbus_display1_vm_get_console_ids ( +QEMU_DBUS_DISPLAY1_VM (vm)); +if (console_ids) { + n_ids =g_variant_n_children (console_ids); + } +return n_ids; +} + +static void +wait_device_event (QTestState *qts,const char *event_name,const char *id) +{ +QDict *resp, *data; +QString *qstr; + +for (;;) { + resp =qtest_qmp_eventwait_ref (qts, event_name); + data =qdict_get_qdict (resp,"data" ); +if (!data || !qdict_get (data,"device" )) { +qobject_unref (resp); +continue ; + } + qstr =qobject_to (QString,qdict_get (data,"device" )); +if (!strcmp (qstring_get_str (qstr), id)) { +qobject_unref (resp); +break ; + } +qobject_unref (resp); + } +} + +static void +test_dbus_display_hotplug (void ) +{ +g_autoptr (GDBusConnection) conn =NULL ; +QTestState *qts =NULL ; +gsize n; + +test_setup_args (&qts, &conn, +"-machine q35" +" -device pcie-root-port,id=rp0" +" -display dbus,p2p=yes" +" -name dbus-test" ); + + n =get_console_ids_count (conn); + g_assert_cmpuint(n, ==, 1); + +qtest_qmp_device_add (qts,"bochs-display" ,"bochs0" , +"{'bus': 'rp0'}" ); + + n =get_console_ids_count (conn); + g_assert_cmpuint(n, ==, 2); + +/* +* On q35, PCI device removal is ACPI-based and requires guest +* acknowledgement. Since qtest has no guest OS, issue the delete +* request and force removal via system reset. +*/ +qtest_qmp_device_del_send (qts,"bochs0" ); +qtest_system_reset_nowait (qts); +wait_device_event (qts,"DEVICE_DELETED" ,"bochs0" ); + + n =get_console_ids_count (conn); + g_assert_cmpuint(n, ==, 1); + +g_clear_object (&conn); +qtest_quit (qts); +} +int main (int argc,char **argv) {
@@ -369,6 +463,9 @@ main(int argc, char **argv)qtest_add_data_func ("/dbus-display/console" ,GINT_TO_POINTER (false), test_dbus_display_console);qtest_add_data_func ("/dbus-display/console/map" ,GINT_TO_POINTER (true), test_dbus_display_console);qtest_add_func ("/dbus-display/keyboard" , test_dbus_display_keyboard); +if (qtest_has_machine ("q35" ) &&qtest_has_device ("bochs-display" )) { +qtest_add_func ("/dbus-display/hotplug" , test_dbus_display_hotplug); + }return g_test_run (); }
Signature · 1 line
2.54.0
Re: [PATCH 32/32] tests/qtest: add D-Bus display hotplug test
Quoted 149 lines · Marc-André Lureau
Add a qtest that verifies display consoles are dynamically added and removed over D-Bus when a bochs-display device is hotplugged and unplugged on a q35 machine.
The test plugs
device_adda bochs-display, waits for theDEVICE_ADDEDQMP event, and checks that the D-Bus VM interface reports a second console. It thendevice_delit, forces a system reset
(q35 removal is ACPI-based and needs guest cooperation qtest cannot provide), waits forDEVICE_DELETED, and checks the console count again.Signed-off-by Marc-André Lureau <[email protected]>--- tests/qtest/dbus-display-test.c | 101 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-)tests/qtest/dbus-display-test.c +96
-2@@ -7,6 +7,8 @@#include <gio/gio.h> #include <gio/gunixfdlist.h> #include "libqtest.h" +#include "qobject/qdict.h" +#include "qobject/qstring.h" #include "ui/dbus-display1.h" static GDBusConnection*@@ -38,11 +40,11 @@ test_dbus_p2p_from_fd(int fd) }static void -test_setup (QTestState **qts,GDBusConnection **conn) +test_setup_args (QTestState **qts,GDBusConnection **conn,const char *args) {int pair[2 ]; - *qts =qtest_init ("-display dbus,p2p=yes -name dbus-test" ); + *qts =qtest_init (args); g_assert_cmpint(qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, pair), ==, 0);@@ -52,6 +54,12 @@ test_setup(QTestState **qts, GDBusConnection **conn)g_dbus_connection_start_message_processing (*conn); } +static void +test_setup (QTestState **qts,GDBusConnection **conn) +{ +test_setup_args (qts, conn,"-display dbus,p2p=yes -name dbus-test" ); +} +static void test_dbus_display_vm (void ) {@@ -360,6 +368,92 @@ test_dbus_display_keyboard(void)qtest_quit (qts); } +static gsize +get_console_ids_count (GDBusConnection *conn) +{ +g_autoptr (GError) err =NULL ; +g_autoptr (QemuDBusDisplay1VMProxy) vm =NULL ; +GVariant *console_ids; +gsize n_ids =0 ; + + vm =QEMU_DBUS_DISPLAY1_VM_PROXY ( +qemu_dbus_display1_vm_proxy_new_sync ( + conn, +G_DBUS_PROXY_FLAGS_NONE , +NULL , +DBUS_DISPLAY1_ROOT "/VM" , +NULL , + &err)); +g_assert_no_error (err); + + console_ids =qemu_dbus_display1_vm_get_console_ids ( +QEMU_DBUS_DISPLAY1_VM (vm)); +if (console_ids) { + n_ids =g_variant_n_children (console_ids); + } +return n_ids; +} + +static void +wait_device_event (QTestState *qts,const char *event_name,const char *id) +{ +QDict *resp, *data; +QString *qstr; + +for (;;) { + resp =qtest_qmp_eventwait_ref (qts, event_name); + data =qdict_get_qdict (resp,"data" ); +if (!data || !qdict_get (data,"device" )) { +qobject_unref (resp); +continue ; + } + qstr =qobject_to (QString,qdict_get (data,"device" )); +if (!strcmp (qstring_get_str (qstr), id)) { +qobject_unref (resp); +break ; + } +qobject_unref (resp); + } +} + +static void +test_dbus_display_hotplug (void ) +{ +g_autoptr (GDBusConnection) conn =NULL ; +QTestState *qts =NULL ; +gsize n; + +test_setup_args (&qts, &conn, +"-machine q35" +" -device pcie-root-port,id=rp0" +" -display dbus,p2p=yes" +" -name dbus-test" ); + + n =get_console_ids_count (conn); + g_assert_cmpuint(n, ==, 1); + +qtest_qmp_device_add (qts,"bochs-display" ,"bochs0" , +"{'bus': 'rp0'}" ); + + n =get_console_ids_count (conn); + g_assert_cmpuint(n, ==, 2); + +/* +* On q35, PCI device removal is ACPI-based and requires guest +* acknowledgement. Since qtest has no guest OS, issue the delete +* request and force removal via system reset. +*/ +qtest_qmp_device_del_send (qts,"bochs0" ); +qtest_system_reset_nowait (qts); +wait_device_event (qts,"DEVICE_DELETED" ,"bochs0" ); + + n =get_console_ids_count (conn); + g_assert_cmpuint(n, ==, 1); + +g_clear_object (&conn); +qtest_quit (qts); +} +int main (int argc,char **argv) {
tests/qtest/dbus-display-test.c main(int argc, char **argv) · line 471
Quoted 9 lines
tests/qtest/dbus-display-test.c +3
-0@@ -369,6 +463,9 @@ main(int argc, char **argv)qtest_add_data_func ("/dbus-display/console" ,GINT_TO_POINTER (false), test_dbus_display_console);qtest_add_data_func ("/dbus-display/console/map" ,GINT_TO_POINTER (true), test_dbus_display_console);qtest_add_func ("/dbus-display/keyboard" , test_dbus_display_keyboard); +if (qtest_has_machine ("q35" ) &&qtest_has_device ("bochs-display" )) { +qtest_add_func ("/dbus-display/hotplug" , test_dbus_display_hotplug); + }return g_test_run (); }