[Spice-devel] [spice-gtk PATCH v3] Grab keyboard based on session focus

Snir Sheriber ssheribe at redhat.com
Sun Nov 29 02:19:05 PST 2015


When using multiple monitors moving mouse between monitors releases
keyboard grab.

Reproduce bug
-Open multiple monitors remote-viewer session
-Click on one of the monitors to get focus & keyboard-grab
-Move mouse to another monitor and try any keyboard command (do not click)
At this point all keyboard commands are being executed on the client machine
instead of the remote machine

I added keyboard_have_focus and mouse_have_pointer variables at the session and
now these properties are being tested for the session instead for the current
widget (works also when using alt-tab).

Resolves: rhbz#1275231

---

No GObject properties were set
---
 src/spice-gtk-session-priv.h |  6 ++++--
 src/spice-gtk-session.c      | 27 +++++++++++++++++++++------
 src/spice-widget.c           | 14 ++++++++------
 3 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/src/spice-gtk-session-priv.h b/src/spice-gtk-session-priv.h
index 2a9b752..dbb255e 100644
--- a/src/spice-gtk-session-priv.h
+++ b/src/spice-gtk-session-priv.h
@@ -28,8 +28,10 @@ gboolean spice_gtk_session_get_read_only(SpiceGtkSession *self);
 void spice_gtk_session_sync_keyboard_modifiers(SpiceGtkSession *self);
 void spice_gtk_session_set_pointer_grabbed(SpiceGtkSession *self, gboolean grabbed);
 gboolean spice_gtk_session_get_pointer_grabbed(SpiceGtkSession *self);
-void spice_gtk_session_set_keyboard_grabbed(SpiceGtkSession *self, gboolean grabbed);
-gboolean spice_gtk_session_get_keyboard_grabbed(SpiceGtkSession *self);
+void spice_gtk_session_set_keyboard_have_focus(SpiceGtkSession *self, gboolean grabbed);
+gboolean spice_gtk_session_get_keyboard_have_focus(SpiceGtkSession *self);
+void spice_gtk_session_set_mouse_have_pointer(SpiceGtkSession *self, gboolean grabbed);
+gboolean spice_gtk_session_get_mouse_have_pointer(SpiceGtkSession *self);
 
 G_END_DECLS
 
diff --git a/src/spice-gtk-session.c b/src/spice-gtk-session.c
index 7b413d2..dd102d5 100644
--- a/src/spice-gtk-session.c
+++ b/src/spice-gtk-session.c
@@ -64,7 +64,8 @@ struct _SpiceGtkSessionPrivate {
     gboolean                auto_usbredir_enable;
     int                     auto_usbredir_reqs;
     gboolean                pointer_grabbed;
-    gboolean                keyboard_grabbed;
+    gboolean                keyboard_have_focus;
+    gboolean                mouse_have_pointer;
 };
 
 /**
@@ -1221,14 +1222,20 @@ void spice_gtk_session_set_pointer_grabbed(SpiceGtkSession *self, gboolean grabb
     g_object_notify(G_OBJECT(self), "pointer-grabbed");
 }
 
+G_GNUC_INTERNAL
+void spice_gtk_session_set_keyboard_have_focus(SpiceGtkSession *self, gboolean grabbed)
+{
+    g_return_if_fail(SPICE_IS_GTK_SESSION(self));
+
+    self->priv->keyboard_have_focus = grabbed;
+}
 
 G_GNUC_INTERNAL
-void spice_gtk_session_set_keyboard_grabbed(SpiceGtkSession *self, gboolean grabbed)
+void spice_gtk_session_set_mouse_have_pointer(SpiceGtkSession *self, gboolean grabbed)
 {
     g_return_if_fail(SPICE_IS_GTK_SESSION(self));
 
-    self->priv->keyboard_grabbed = grabbed;
-    //g_object_notify(G_OBJECT(self), "pointer-grabbed");
+    self->priv->mouse_have_pointer = grabbed;
 }
 
 G_GNUC_INTERNAL
@@ -1240,9 +1247,17 @@ gboolean spice_gtk_session_get_pointer_grabbed(SpiceGtkSession *self)
 }
 
 G_GNUC_INTERNAL
-gboolean spice_gtk_session_get_keyboard_grabbed(SpiceGtkSession *self)
+gboolean spice_gtk_session_get_keyboard_have_focus(SpiceGtkSession *self)
+{
+    g_return_val_if_fail(SPICE_IS_GTK_SESSION(self), FALSE);
+
+    return self->priv->keyboard_have_focus;
+}
+
+G_GNUC_INTERNAL
+gboolean spice_gtk_session_get_mouse_have_pointer(SpiceGtkSession *self)
 {
     g_return_val_if_fail(SPICE_IS_GTK_SESSION(self), FALSE);
 
-    return self->priv->keyboard_grabbed;
+    return self->priv->mouse_have_pointer;
 }
diff --git a/src/spice-widget.c b/src/spice-widget.c
index 99050b5..c784a54 100644
--- a/src/spice-widget.c
+++ b/src/spice-widget.c
@@ -207,7 +207,8 @@ static void update_keyboard_focus(SpiceDisplay *display, gboolean state)
 {
     SpiceDisplayPrivate *d = display->priv;
     d->keyboard_have_focus = state;
-    spice_gtk_session_set_keyboard_grabbed(d->gtk_session, state);
+
+    spice_gtk_session_set_keyboard_have_focus(d->gtk_session, state);
     /* keyboard grab gets inhibited by usb-device-manager when it is
        in the process of redirecting a usb-device (as this may show a
        policykit dialog). Making autoredir/automount setting changes while
@@ -729,17 +730,16 @@ static void try_keyboard_grab(SpiceDisplay *display)
         return;
     if (d->disable_inputs)
         return;
+
     if (d->keyboard_grab_inhibit)
         return;
     if (!d->keyboard_grab_enable)
         return;
     if (d->keyboard_grab_active)
         return;
-    if (!spice_gtk_session_get_keyboard_grabbed(d->gtk_session)){
-        if (!d->keyboard_have_focus)
-            return;
-    }
-    if (!d->mouse_have_pointer)
+    if (!spice_gtk_session_get_keyboard_have_focus(d->gtk_session))
+        return;
+    if (!spice_gtk_session_get_mouse_have_pointer(d->gtk_session))
         return;
     if (d->keyboard_grab_released)
         return;
@@ -1465,6 +1465,7 @@ static gboolean enter_event(GtkWidget *widget, GdkEventCrossing *crossing G_GNUC
     SPICE_DEBUG("%s", __FUNCTION__);
 
     d->mouse_have_pointer = true;
+    spice_gtk_session_set_mouse_have_pointer(d->gtk_session, true);
     try_keyboard_grab(display);
     update_display(display);
 
@@ -1482,6 +1483,7 @@ static gboolean leave_event(GtkWidget *widget, GdkEventCrossing *crossing G_GNUC
         return true;
 
     d->mouse_have_pointer = false;
+    spice_gtk_session_set_mouse_have_pointer(d->gtk_session, false);
     try_keyboard_ungrab(display);
 
     return true;
-- 
2.4.3



More information about the Spice-devel mailing list