[PATCH] ui/gtk: Fix focus loss on re-attachment with single VC

dongwon.kim@intel.com posted 1 patch 1 week, 3 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260520002645.1910740-1-dongwon.kim@intel.com
Maintainers: "Marc-André Lureau" <marcandre.lureau@redhat.com>
ui/gtk.c | 5 +++++
1 file changed, 5 insertions(+)
[PATCH] ui/gtk: Fix focus loss on re-attachment with single VC
Posted by dongwon.kim@intel.com 1 week, 3 days ago
From: Dongwon Kim <dongwon.kim@intel.com>

When QEMU is launched with a single virtual console (e.g., using
--nodefaults), detaching and then closing the detached window leaves
the main window's notebook without an active focus target which makes
keyboard unfunctional on re-attached VC.

Fix this by explicitly calling gtk_widget_grab_focus on the active
VC's focus widget during the window close handler.

Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Cc: Philippe Mathieu-Daudé <philmd@linaro.org>
Cc: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Dongwon Kim <dongwon.kim@intel.com>
---
 ui/gtk.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/ui/gtk.c b/ui/gtk.c
index 2c61b601f7..8eb1a80377 100644
--- a/ui/gtk.c
+++ b/ui/gtk.c
@@ -1475,6 +1475,11 @@ static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
         vc->gfx.ectx = NULL;
     }
 #endif
+
+    if (vc == gd_vc_find_by_menu(s)) {
+        gtk_widget_grab_focus(vc->focus);
+    }
+
     return TRUE;
 }
 
-- 
2.43.0


Re: [PATCH] ui/gtk: Fix focus loss on re-attachment with single VC
Posted by Marc-André Lureau 1 week, 3 days ago
Hi

On Wed, May 20, 2026 at 4:33 AM <dongwon.kim@intel.com> wrote:
>
> From: Dongwon Kim <dongwon.kim@intel.com>
>
> When QEMU is launched with a single virtual console (e.g., using
> --nodefaults), detaching and then closing the detached window leaves
> the main window's notebook without an active focus target which makes
> keyboard unfunctional on re-attached VC.
>
> Fix this by explicitly calling gtk_widget_grab_focus on the active
> VC's focus widget during the window close handler.
>
> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> Cc: Philippe Mathieu-Daudé <philmd@linaro.org>
> Cc: Daniel P. Berrangé <berrange@redhat.com>
> Signed-off-by: Dongwon Kim <dongwon.kim@intel.com>
> ---
>  ui/gtk.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/ui/gtk.c b/ui/gtk.c
> index 2c61b601f7..8eb1a80377 100644
> --- a/ui/gtk.c
> +++ b/ui/gtk.c
> @@ -1475,6 +1475,11 @@ static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
>          vc->gfx.ectx = NULL;
>      }
>  #endif
> +
> +    if (vc == gd_vc_find_by_menu(s)) {
> +        gtk_widget_grab_focus(vc->focus);
> +    }
> +

Looks ok to me. But could you share your test case? launching qemu
with just -nodefaults doesn't help reproduce your issue :)

(fwiw, I have a bunch of patches pending to fix/improve dynamic
console handling, I will try to send them later this week)

thanks

-- 
Marc-André Lureau
RE: [PATCH] ui/gtk: Fix focus loss on re-attachment with single VC
Posted by Kim, Dongwon 1 week, 2 days ago
Hi Marc-André,

> Subject: Re: [PATCH] ui/gtk: Fix focus loss on re-attachment with single VC
> 
> Hi
> 
> On Wed, May 20, 2026 at 4:33 AM <dongwon.kim@intel.com> wrote:
> >
> > From: Dongwon Kim <dongwon.kim@intel.com>
> >
> > When QEMU is launched with a single virtual console (e.g., using
> > --nodefaults), detaching and then closing the detached window leaves
> > the main window's notebook without an active focus target which makes
> > keyboard unfunctional on re-attached VC.
> >
> > Fix this by explicitly calling gtk_widget_grab_focus on the active
> > VC's focus widget during the window close handler.
> >
> > Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> > Cc: Philippe Mathieu-Daudé <philmd@linaro.org>
> > Cc: Daniel P. Berrangé <berrange@redhat.com>
> > Signed-off-by: Dongwon Kim <dongwon.kim@intel.com>
> > ---
> >  ui/gtk.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/ui/gtk.c b/ui/gtk.c
> > index 2c61b601f7..8eb1a80377 100644
> > --- a/ui/gtk.c
> > +++ b/ui/gtk.c
> > @@ -1475,6 +1475,11 @@ static gboolean
> gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
> >          vc->gfx.ectx = NULL;
> >      }
> >  #endif
> > +
> > +    if (vc == gd_vc_find_by_menu(s)) {
> > +        gtk_widget_grab_focus(vc->focus);
> > +    }
> > +
> 
> Looks ok to me. But could you share your test case? launching qemu with just
> -nodefaults doesn't help reproduce your issue :)

Hmm, -nodefaults should be only thing needed. The point is to have just one VC created.
Please take a look at the video shared here:
https://drive.google.com/file/d/1O6K9GSWHFRFaxaTLcJKN_jYOPvOQPr7t/view?usp=sharing

The QEMU used in this demo is yesterday's master.

commit e89049b3ba5f1f0468bc0d294173345597514a1b (origin/staging, origin/master, origin/HEAD)
Merge: 992ca15de7 8b663f7d0d
Author: Stefan Hajnoczi <stefanha@redhat.com>
Date:   Tue May 19 15:23:10 2026 -0400

    Merge tag 'for-upstream' of https://repo.or.cz/qemu/kevin into staging

And this is the QEMU launching command line.

qemu-system-x86_64 -nodefaults -machine q35 -m 2048 -enable-kvm -cpu host -smp cores=4,threads=2,sockets=1 -drive file=../OVMF.fd,format=raw,if=pflash -drive format=raw,if=pflash,if=virtio,file=/home/user/mnt/ubuntu-test/ubuntu-desktop-24.04-302-custom.img,cache=none -device pcie-root-port,id=root_port1,bus=pcie.0 -device vfio-pci,host=0000:00:02.1 -device e1000,netdev=net0,mac=DE:AD:BE:EF:BF:FA -netdev user,id=net0,hostfwd=tcp::1111-:22 -device virtio-vga,max_outputs=1,blob=true,bus=root_port1,x-pcie-pm-no-soft-reset=true -display gtk,gl=on,full-screen=off -object memory-backend-memfd,hugetlb=off,id=mem1,size=2048M -machine memory-backend=mem1

I am using Ubuntu 24.04LTS for both host and guest.

> 
> (fwiw, I have a bunch of patches pending to fix/improve dynamic console
> handling, I will try to send them later this week)

Sounds good!! Thanks
DW

> 
> thanks
> 
> --
> Marc-André Lureau