Skip to contentSkip to conversation

Fix relative pointer tracking on Gtk UI

PATCH In review Takashi Iwai 4 participants 15 messages Apr 4, 2014 – Apr 8, 2014

lore
Share & tools
lore
#0 lore raw

v3 cover Fix relative pointer tracking on Gtk UI

Hi,

this is the revisited patch series. The only difference from v2 is that now they are checkpatch-clean and Cole's acks have been added to patches 1-3.

Takashi

#1 lore raw

PATCH v3 1/4 gtk: Use gtk generic event signal instead of motion-notify-event

The GDK motion-notify-event isn't generated when the pointer goes out of the target window even if the pointer is grabbed, which essentially means to lose the pointer tracking in gtk-ui.

Meanwhile the generic "event" signal is sent when the pointer is grabbed, so we can use this and pick the motion notify events manually there instead.

Reference: https://bugzilla.novell.com/show_bug.cgi?id=849587

---
 ui/gtk.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
ui/gtk.c +10 -2
index f056e4034b3e..c9f6d24eeb1a 100644
@@ -765,6 +765,14 @@ static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
     return TRUE;
 }
 
+static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
+{
+    if (event->type == GDK_MOTION_NOTIFY) {
+        return gd_motion_event(widget, &event->motion, opaque);
+    }
+    return FALSE;
+}
+
 /** Window Menu Actions **/
 
 static void gd_menu_pause(GtkMenuItem *item, void *opaque)
@@ -1267,8 +1275,8 @@ static void gd_connect_signals(GtkDisplayState *s)
     g_signal_connect(s->drawing_area, "expose-event",
                      G_CALLBACK(gd_expose_event), s);
 #endif
-    g_signal_connect(s->drawing_area, "motion-notify-event",
-                     G_CALLBACK(gd_motion_event), s);
+    g_signal_connect(s->drawing_area, "event",
+                     G_CALLBACK(gd_event), s);
     g_signal_connect(s->drawing_area, "button-press-event",
                      G_CALLBACK(gd_button_event), s);
     g_signal_connect(s->drawing_area, "button-release-event",
Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 2/4 gtk: Fix the relative pointer tracking mode

In reply to Takashi Iwai

The relative pointer tracking mode was still buggy even after the previous fix of the motion-notify-event since the events are filtered out when the pointer moves outside the drawing window due to the boundary check for the absolute mode.

This patch fixes the issue by moving the unnecessary boundary check into the if block of absolute mode, and keep the coordinate in the relative mode even if it's outside the drawing area. But this makes the coordinate (last_x, last_y) possibly pointing to (-1,-1), introduce a new flag to indicate the last coordinate has been updated.

Reference: https://bugzilla.novell.com/show_bug.cgi?id=849587

---
 ui/gtk.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
ui/gtk.c +9 -9
index c9f6d24eeb1a..913cc3f70c02 100644
@@ -156,6 +156,7 @@ typedef struct GtkDisplayState
     DisplayChangeListener dcl;
     DisplaySurface *ds;
     int button_mask;
+    gboolean last_set;
     int last_x;
     int last_y;
 
@@ -616,25 +617,25 @@ static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
     x = (motion->x - mx) / s->scale_x;
     y = (motion->y - my) / s->scale_y;
 
-    if (x < 0 || y < 0 ||
-        x >= surface_width(s->ds) ||
-        y >= surface_height(s->ds)) {
-        return TRUE;
-    }
-
     if (qemu_input_is_absolute()) {
+        if (x < 0 || y < 0 ||
+            x >= surface_width(s->ds) ||
+            y >= surface_height(s->ds)) {
+            return TRUE;
+        }
         qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_X, x,
                              surface_width(s->ds));
         qemu_input_queue_abs(s->dcl.con, INPUT_AXIS_Y, y,
                              surface_height(s->ds));
         qemu_input_event_sync();
-    } else if (s->last_x != -1 && s->last_y != -1 && gd_is_grab_active(s)) {
+    } else if (s->last_set && gd_is_grab_active(s)) {
         qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_X, x - s->last_x);
         qemu_input_queue_rel(s->dcl.con, INPUT_AXIS_Y, y - s->last_y);
         qemu_input_event_sync();
     }
     s->last_x = x;
     s->last_y = y;
+    s->last_set = TRUE;
 
     if (!qemu_input_is_absolute() && gd_is_grab_active(s)) {
         GdkScreen *screen = gtk_widget_get_screen(s->drawing_area);
@@ -669,8 +670,7 @@ static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
             GdkDisplay *display = gtk_widget_get_display(widget);
             gdk_display_warp_pointer(display, screen, x, y);
 #endif
-            s->last_x = -1;
-            s->last_y = -1;
+            s->last_set = FALSE;
             return FALSE;
         }
     }
Signature · 1 line
1.9.1
#3 lore raw

PATCH v3 3/4 gtk: Remember the last grabbed pointer position

In reply to Takashi Iwai

It's pretty annoying that the pointer reappears at a random place once after grabbing and ungrabbing the input. Better to restore to the original position where the pointer was grabbed.

Reference: https://bugzilla.novell.com/show_bug.cgi?id=849587

---
 ui/gtk.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
ui/gtk.c +14 -2
index 913cc3f70c02..6668bd8226d5 100644
@@ -159,6 +159,8 @@ typedef struct GtkDisplayState
     gboolean last_set;
     int last_x;
     int last_y;
+    int grab_x_root;
+    int grab_y_root;
 
     double scale_x;
     double scale_y;
@@ -971,8 +973,8 @@ static void gd_ungrab_keyboard(GtkDisplayState *s)
 
 static void gd_grab_pointer(GtkDisplayState *s)
 {
-#if GTK_CHECK_VERSION(3, 0, 0)
     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
+#if GTK_CHECK_VERSION(3, 0, 0)
     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
     GList *devices = gdk_device_manager_list_devices(mgr,
                                                      GDK_DEVICE_TYPE_MASTER);
@@ -996,6 +998,8 @@ static void gd_grab_pointer(GtkDisplayState *s)
         tmp = tmp->next;
     }
     g_list_free(devices);
+    gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
+                            NULL, &s->grab_x_root, &s->grab_y_root);
 #else
     gdk_pointer_grab(gtk_widget_get_window(s->drawing_area),
                      FALSE, /* All events to come to our window directly */
@@ -1007,13 +1011,15 @@ static void gd_grab_pointer(GtkDisplayState *s)
                      NULL, /* Allow cursor to move over entire desktop */
                      s->null_cursor,
                      GDK_CURRENT_TIME);
+    gdk_display_get_pointer(display, NULL,
+                            &s->grab_x_root, &s->grab_y_root, NULL);
 #endif
 }
 
 static void gd_ungrab_pointer(GtkDisplayState *s)
 {
-#if GTK_CHECK_VERSION(3, 0, 0)
     GdkDisplay *display = gtk_widget_get_display(s->drawing_area);
+#if GTK_CHECK_VERSION(3, 0, 0)
     GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
     GList *devices = gdk_device_manager_list_devices(mgr,
                                                      GDK_DEVICE_TYPE_MASTER);
@@ -1027,8 +1033,14 @@ static void gd_ungrab_pointer(GtkDisplayState *s)
         tmp = tmp->next;
     }
     g_list_free(devices);
+    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
+                    gtk_widget_get_screen(s->drawing_area),
+                    s->grab_x_root, s->grab_y_root);
 #else
     gdk_pointer_ungrab(GDK_CURRENT_TIME);
+    gdk_display_warp_pointer(display,
+                             gtk_widget_get_screen(s->drawing_area),
+                             s->grab_x_root, s->grab_y_root);
 #endif
 }
 
Signature · 1 line
1.9.1
#4 lore raw

PATCH v3 4/4 gtk: Add "Grab On Click" option

In reply to Takashi Iwai

I simply like it better, you don't? :)

---
 ui/gtk.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
ui/gtk.c +15 -0
index 6668bd8226d5..4427d9f6c1e9 100644
@@ -141,6 +141,7 @@ typedef struct GtkDisplayState
     GtkWidget *zoom_fit_item;
     GtkWidget *grab_item;
     GtkWidget *grab_on_hover_item;
+    GtkWidget *grab_on_click_item;
     GtkWidget *vga_item;
 
     int nb_vcs;
@@ -189,6 +190,11 @@ static bool gd_grab_on_hover(GtkDisplayState *s)
     return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
 }
 
+static bool gd_grab_on_click(GtkDisplayState *s)
+{
+    return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_click_item));
+}
+
 static bool gd_on_vga(GtkDisplayState *s)
 {
     return gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook)) == 0;
@@ -685,6 +691,12 @@ static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
     GtkDisplayState *s = opaque;
     InputButton btn;
 
+    if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
+        !gd_is_grab_active(s) && gd_grab_on_click(s)) {
+        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item), TRUE);
+        return TRUE;
+    }
+
     if (button->button == 1) {
         btn = INPUT_BUTTON_LEFT;
     } else if (button->button == 2) {
@@ -1417,6 +1429,9 @@ static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_g
     s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
     gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
 
+    s->grab_on_click_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Click"));
+    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_click_item);
+
     s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
     gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
                                  "<QEMU>/View/Grab Input");
Signature · 1 line
1.9.1
#5 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

I'll quickly go put patches 1-3 into a 2.0 pull request, so debating patch #4 doesn't stop the clear improvements from entering the tree.

cheers,
Gerd

#6 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

At Mon, 07 Apr 2014 10:07:43 +0200,

Quoted 6 lines · Gerd Hoffmann
Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

Yes, agreed, it's more intuitive.
OTOH, we still want the explicit input grab in absolute mode. So, we'll need two flags in the end, the explicit grab by menu or key combo, and the implicit grab by click in relative mode.

Quoted 2 lines

I'll quickly go put patches 1-3 into a 2.0 pull request, so debating patch #4 doesn't stop the clear improvements from entering the tree.

Thanks!

Takashi

#7 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

In reply to Gerd Hoffmann
Quoted 5 lines · Gerd Hoffmann
Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

Hmm how do I set absolute/relative mode?
I think it's a matter of taste mostly. Sometimes I feel I want click to grab sometimes I don't.

Quoted 7 lines

I'll quickly go put patches 1-3 into a 2.0 pull request, so debating patch #4 doesn't stop the clear improvements from entering the tree.

cheers,
Gerd

#8 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Gerd Hoffmann
Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

Hmm how do I set absolute/relative mode?

Depends on the pointer input device being used.

Start guest with "-device usb-tablet". Go to HMP. 'info mice' should list the ps/2 mouse and the usb tablet. Using 'mouse_set' (or was it set_mouse?) you can force one of the two devides being used.

If you pick the tablet (should be active by default) absolute pointer events are passed to the guest, and qemu works in absolute mode (i.e. sdl doesn't do pointer grabs).

If you pick the mouse relative mouse events are passed to the guest, and qemu works in relative mode (pointer grab is pretty much required to work with the guest).

cheers,
Gerd

#9 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

Quoted 20 lines · Gerd Hoffmann
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Gerd Hoffmann
Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

Hmm how do I set absolute/relative mode?

Depends on the pointer input device being used.

Start guest with "-device usb-tablet". Go to HMP. 'info mice' should list the ps/2 mouse and the usb tablet. Using 'mouse_set' (or was it set_mouse?) you can force one of the two devides being used.

If you pick the tablet (should be active by default) absolute pointer events are passed to the guest, and qemu works in absolute mode (i.e. sdl doesn't do pointer grabs).

Quoted 7 lines

If you pick the mouse relative mouse events are passed to the guest, and qemu works in relative mode (pointer grab is pretty much required to work with the guest).

cheers,
Gerd

I have to say grab on click is easier to understand - more predictable. Not requesting that it's made the default, but an option would be nice.

Signature · 1 line
MST
#10 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

Quoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Gerd Hoffmann
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Gerd Hoffmann
Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

Hmm how do I set absolute/relative mode?

Depends on the pointer input device being used.

Start guest with "-device usb-tablet". Go to HMP. 'info mice' should list the ps/2 mouse and the usb tablet. Using 'mouse_set' (or was it set_mouse?) you can force one of the two devides being used.

If you pick the tablet (should be active by default) absolute pointer events are passed to the guest, and qemu works in absolute mode (i.e. sdl doesn't do pointer grabs).

If you pick the mouse relative mouse events are passed to the guest, and qemu works in relative mode (pointer grab is pretty much required to work with the guest).

Quoted 6 lines
Quoted 3 lines

cheers,
Gerd

I have to say grab on click is easier to understand - more predictable. Not requesting that it's made the default, but an option would be nice.

Well, usually you'll never ever pick the pointer device manually, this is mainly meant for testing things or checkout what the behavior is.

Another topic is how to actually activate the grab (when in relative mode where it is needed). You surely don't want grab the pointer on hover as simply crossing the guest window will activate it. Grab-(+ungrab)-by-hotkey and grab-on-click is what SDL, virt-viewer & friends are doing today. I think gtk should simply do the same for consistency, and I don't see a need for a config option.

cheers,
Gerd

#11 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

Quoted 33 lines · Gerd Hoffmann
Quoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Gerd Hoffmann
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Gerd Hoffmann
Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

Hmm how do I set absolute/relative mode?

Depends on the pointer input device being used.

Start guest with "-device usb-tablet". Go to HMP. 'info mice' should list the ps/2 mouse and the usb tablet. Using 'mouse_set' (or was it set_mouse?) you can force one of the two devides being used.

If you pick the tablet (should be active by default) absolute pointer events are passed to the guest, and qemu works in absolute mode (i.e. sdl doesn't do pointer grabs).

If you pick the mouse relative mouse events are passed to the guest, and qemu works in relative mode (pointer grab is pretty much required to work with the guest).

Quoted 6 lines
Quoted 3 lines

cheers,
Gerd

I have to say grab on click is easier to understand - more predictable. Not requesting that it's made the default, but an option would be nice.

Quoted 6 lines

Well, usually you'll never ever pick the pointer device manually, this is mainly meant for testing things or checkout what the behavior is.

Another topic is how to actually activate the grab (when in relative mode where it is needed). You surely don't want grab the pointer on hover as simply crossing the guest window will activate it.

Sounds kind of useful. Why isn't it?

Quoted 6 lines

Grab-(+ungrab)-by-hotkey and grab-on-click is what SDL, virt-viewer & friends are doing today. I think gtk should simply do the same for consistency, and I don't see a need for a config option.

cheers,
Gerd

Aha.
It's lack of grab on click that makes me unhappy. So how about doing this for 2.0?
Drop grab on hover make grab on click the default.

Signature · 1 line
MST
#12 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] gtk: Add "Grab On Click" option

At Tue, 8 Apr 2014 11:57:19 +0300,

Quoted 48 lines · Michael S. Tsirkin
Quoted 33 lines · Gerd Hoffmann
Quoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Gerd Hoffmann
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Gerd Hoffmann
Quoted 1 line · Takashi Iwai

I simply like it better, you don't? :)

I still think we should make this simply depend on absolute/relative pointer mode instead of asking the user to switch it manually.

Hmm how do I set absolute/relative mode?

Depends on the pointer input device being used.

Start guest with "-device usb-tablet". Go to HMP. 'info mice' should list the ps/2 mouse and the usb tablet. Using 'mouse_set' (or was it set_mouse?) you can force one of the two devides being used.

If you pick the tablet (should be active by default) absolute pointer events are passed to the guest, and qemu works in absolute mode (i.e. sdl doesn't do pointer grabs).

If you pick the mouse relative mouse events are passed to the guest, and qemu works in relative mode (pointer grab is pretty much required to work with the guest).

Quoted 6 lines
Quoted 3 lines

cheers,
Gerd

I have to say grab on click is easier to understand - more predictable. Not requesting that it's made the default, but an option would be nice.

Quoted 6 lines

Well, usually you'll never ever pick the pointer device manually, this is mainly meant for testing things or checkout what the behavior is.

Another topic is how to actually activate the grab (when in relative mode where it is needed). You surely don't want grab the pointer on hover as simply crossing the guest window will activate it.

Sounds kind of useful. Why isn't it?

Quoted 4 lines

Grab-(+ungrab)-by-hotkey and grab-on-click is what SDL, virt-viewer & friends are doing today. I think gtk should simply do the same for consistency, and I don't see a need for a config option.

Quoted 7 lines
Quoted 2 lines

cheers,
Gerd

Aha.
It's lack of grab on click that makes me unhappy. So how about doing this for 2.0?
Drop grab on hover make grab on click the default.

OK, I'll submit a revised patch to implement grab-on-click only in relative mode.

Takashi

#13 lore raw
In reply to Takashi Iwai
Quoted 5 lines · Takashi Iwai

Hi,

this is the revisited patch series. The only difference from v2 is that now they are checkpatch-clean and Cole's acks have been added to patches 1-3.

Is this 2.0 material?

Quoted 4 lines

Takashi

Tested-by on the v3 series
#14 lore raw
In reply to Takashi Iwai
Quoted 8 lines · Takashi Iwai

Hi,

this is the revisited patch series. The only difference from v2 is that now they are checkpatch-clean and Cole's acks have been added to patches 1-3.

Takashi

For the series:

Any chance this can be merged for 2.0?

This greatly increases the usability of GTK UI:
as it is it's almost useless with mouse pointer jumping about in jerks to the point that I'm forcing it off with -sdl.
Lack of grab on click is also a regression from SDL.