diff mbox series

[v2,6/6] ui/gtk: enable backend to send multi-touch events

Message ID 20230316120624.46410-7-slp@redhat.com (mailing list archive)
State New, archived
Headers show
Series Implement virtio-multitouch and enable GTK3 to use it | expand

Commit Message

Sergio Lopez Pascual March 16, 2023, 12:06 p.m. UTC
GTK3 provides the infrastructure to receive and process multi-touch
events through the "touch-event" signal and the GdkEventTouch type.
Make use of it to transpose events from the host to the guest.

This allows users of machines with hardware capable of receiving
multi-touch events to run guests that can also receive those events
and interpret them as gestures, when appropriate.

An example of this in action can be seen here:

 https://fosstodon.org/@slp/109545849296546767

Signed-off-by: Sergio Lopez <slp@redhat.com>
---
 ui/gtk.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)

Comments

Marc-André Lureau March 17, 2023, 8:02 a.m. UTC | #1
Hi

On Thu, Mar 16, 2023 at 4:07 PM Sergio Lopez <slp@redhat.com> wrote:
>
> GTK3 provides the infrastructure to receive and process multi-touch
> events through the "touch-event" signal and the GdkEventTouch type.
> Make use of it to transpose events from the host to the guest.
>
> This allows users of machines with hardware capable of receiving
> multi-touch events to run guests that can also receive those events
> and interpret them as gestures, when appropriate.
>
> An example of this in action can be seen here:
>
>  https://fosstodon.org/@slp/109545849296546767
>
> Signed-off-by: Sergio Lopez <slp@redhat.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


> ---
>  ui/gtk.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 92 insertions(+)
>
> diff --git a/ui/gtk.c b/ui/gtk.c
> index fd82e9b1ca..3a667bfba6 100644
> --- a/ui/gtk.c
> +++ b/ui/gtk.c
> @@ -130,6 +130,13 @@ typedef struct VCChardev VCChardev;
>  DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
>                           TYPE_CHARDEV_VC)
>
> +struct touch_slot {
> +    int x;
> +    int y;
> +    int tracking_id;
> +};
> +static struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX];
> +
>  bool gtk_use_gl_area;
>
>  static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
> @@ -1058,6 +1065,82 @@ static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
>  }
>
>
> +static gboolean gd_touch_event(GtkWidget *widget, GdkEventTouch *touch,
> +                               void *opaque)
> +{
> +    VirtualConsole *vc = opaque;
> +    struct touch_slot *slot;
> +    uint64_t num_slot = GPOINTER_TO_UINT(touch->sequence);
> +    bool needs_sync = false;
> +    int update;
> +    int type = -1;
> +    int i;
> +
> +    if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
> +        fprintf(stderr, "%s: unexpected touch slot number: %ld >= %d\n",
> +                __func__, num_slot, INPUT_EVENT_SLOTS_MAX);

(may be better use warn_report?).

> +        return FALSE;
> +    }
> +
> +    slot = &touch_slots[num_slot];
> +    slot->x = touch->x;
> +    slot->y = touch->y;
> +
> +    switch (touch->type) {
> +    case GDK_TOUCH_BEGIN:
> +        type = INPUT_MULTITOUCH_TYPE_BEGIN;
> +        slot->tracking_id = num_slot;
> +        break;
> +    case GDK_TOUCH_UPDATE:
> +        type = INPUT_MULTITOUCH_TYPE_UPDATE;
> +        break;
> +    case GDK_TOUCH_END:
> +    case GDK_TOUCH_CANCEL:
> +        type = INPUT_MULTITOUCH_TYPE_END;
> +        break;
> +    default:
> +        fprintf(stderr, "%s: unexpected touch event\n", __func__);

same

> +    }
> +
> +    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
> +        if (i == num_slot) {
> +            update = type;
> +        } else {
> +            update = INPUT_MULTITOUCH_TYPE_UPDATE;
> +        }
> +
> +        slot = &touch_slots[i];
> +
> +        if (slot->tracking_id == -1) {
> +            continue;
> +        }
> +
> +        if (update == INPUT_MULTITOUCH_TYPE_END) {
> +            slot->tracking_id = -1;
> +            qemu_input_queue_mtt(vc->gfx.dcl.con, update, i, slot->tracking_id);
> +            needs_sync = true;
> +        } else {
> +            qemu_input_queue_mtt(vc->gfx.dcl.con, update, i, slot->tracking_id);
> +            qemu_input_queue_btn(vc->gfx.dcl.con, INPUT_BUTTON_TOUCH, true);
> +            qemu_input_queue_mtt_abs(vc->gfx.dcl.con,
> +                                     INPUT_AXIS_X, (int) slot->x,
> +                                     0, surface_width(vc->gfx.ds),
> +                                     i, slot->tracking_id);
> +            qemu_input_queue_mtt_abs(vc->gfx.dcl.con,
> +                                     INPUT_AXIS_Y, (int) slot->y,
> +                                     0, surface_height(vc->gfx.ds),
> +                                     i, slot->tracking_id);
> +            needs_sync = true;
> +        }
> +    }
> +
> +    if (needs_sync) {
> +        qemu_input_event_sync();
> +    }
> +
> +    return TRUE;
> +}
> +
>  static const guint16 *gd_get_keymap(size_t *maplen)
>  {
>      GdkDisplay *dpy = gdk_display_get_default();
> @@ -1977,6 +2060,8 @@ static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
>                           G_CALLBACK(gd_key_event), vc);
>          g_signal_connect(vc->gfx.drawing_area, "key-release-event",
>                           G_CALLBACK(gd_key_event), vc);
> +        g_signal_connect(vc->gfx.drawing_area, "touch-event",
> +                         G_CALLBACK(gd_touch_event), vc);
>
>          g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
>                           G_CALLBACK(gd_enter_event), vc);
> @@ -2086,6 +2171,7 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
>                                GSList *group, GtkWidget *view_menu)
>  {
>      bool zoom_to_fit = false;
> +    int i;
>
>      vc->label = qemu_console_get_label(con);
>      vc->s = s;
> @@ -2133,6 +2219,7 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
>                            GDK_BUTTON_PRESS_MASK |
>                            GDK_BUTTON_RELEASE_MASK |
>                            GDK_BUTTON_MOTION_MASK |
> +                          GDK_TOUCH_MASK |
>                            GDK_ENTER_NOTIFY_MASK |
>                            GDK_LEAVE_NOTIFY_MASK |
>                            GDK_SCROLL_MASK |
> @@ -2168,6 +2255,11 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
>          s->free_scale = true;
>      }
>
> +    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; i++) {
> +        struct touch_slot *slot = &touch_slots[i];
> +        slot->tracking_id = -1;
> +    }
> +
>      return group;
>  }
>
> --
> 2.38.1
>
>
Sergio Lopez Pascual March 20, 2023, 10:16 a.m. UTC | #2
On Fri, Mar 17, 2023 at 12:02:41PM +0400, Marc-André Lureau wrote:
> Hi
> 
> On Thu, Mar 16, 2023 at 4:07 PM Sergio Lopez <slp@redhat.com> wrote:
> >
> > GTK3 provides the infrastructure to receive and process multi-touch
> > events through the "touch-event" signal and the GdkEventTouch type.
> > Make use of it to transpose events from the host to the guest.
> >
> > This allows users of machines with hardware capable of receiving
> > multi-touch events to run guests that can also receive those events
> > and interpret them as gestures, when appropriate.
> >
> > An example of this in action can be seen here:
> >
> >  https://fosstodon.org/@slp/109545849296546767
> >
> > Signed-off-by: Sergio Lopez <slp@redhat.com>
> 
> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> 
> > ---
> >  ui/gtk.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 92 insertions(+)
> >
> > diff --git a/ui/gtk.c b/ui/gtk.c
> > index fd82e9b1ca..3a667bfba6 100644
> > --- a/ui/gtk.c
> > +++ b/ui/gtk.c
> > @@ -130,6 +130,13 @@ typedef struct VCChardev VCChardev;
> >  DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
> >                           TYPE_CHARDEV_VC)
> >
> > +struct touch_slot {
> > +    int x;
> > +    int y;
> > +    int tracking_id;
> > +};
> > +static struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX];
> > +
> >  bool gtk_use_gl_area;
> >
> >  static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
> > @@ -1058,6 +1065,82 @@ static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
> >  }
> >
> >
> > +static gboolean gd_touch_event(GtkWidget *widget, GdkEventTouch *touch,
> > +                               void *opaque)
> > +{
> > +    VirtualConsole *vc = opaque;
> > +    struct touch_slot *slot;
> > +    uint64_t num_slot = GPOINTER_TO_UINT(touch->sequence);
> > +    bool needs_sync = false;
> > +    int update;
> > +    int type = -1;
> > +    int i;
> > +
> > +    if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
> > +        fprintf(stderr, "%s: unexpected touch slot number: %ld >= %d\n",
> > +                __func__, num_slot, INPUT_EVENT_SLOTS_MAX);
> 
> (may be better use warn_report?).
> 
> > +        return FALSE;
> > +    }
> > +
> > +    slot = &touch_slots[num_slot];
> > +    slot->x = touch->x;
> > +    slot->y = touch->y;
> > +
> > +    switch (touch->type) {
> > +    case GDK_TOUCH_BEGIN:
> > +        type = INPUT_MULTITOUCH_TYPE_BEGIN;
> > +        slot->tracking_id = num_slot;
> > +        break;
> > +    case GDK_TOUCH_UPDATE:
> > +        type = INPUT_MULTITOUCH_TYPE_UPDATE;
> > +        break;
> > +    case GDK_TOUCH_END:
> > +    case GDK_TOUCH_CANCEL:
> > +        type = INPUT_MULTITOUCH_TYPE_END;
> > +        break;
> > +    default:
> > +        fprintf(stderr, "%s: unexpected touch event\n", __func__);
> 
> same

I've used "fprintf" for consistency, because it was already being used in
gd_win_grab() and gtk_display_init(). I don't have a preference, would it be
better to use "warn_report" instead?

Sergio.

> > +    }
> > +
> > +    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
> > +        if (i == num_slot) {
> > +            update = type;
> > +        } else {
> > +            update = INPUT_MULTITOUCH_TYPE_UPDATE;
> > +        }
> > +
> > +        slot = &touch_slots[i];
> > +
> > +        if (slot->tracking_id == -1) {
> > +            continue;
> > +        }
> > +
> > +        if (update == INPUT_MULTITOUCH_TYPE_END) {
> > +            slot->tracking_id = -1;
> > +            qemu_input_queue_mtt(vc->gfx.dcl.con, update, i, slot->tracking_id);
> > +            needs_sync = true;
> > +        } else {
> > +            qemu_input_queue_mtt(vc->gfx.dcl.con, update, i, slot->tracking_id);
> > +            qemu_input_queue_btn(vc->gfx.dcl.con, INPUT_BUTTON_TOUCH, true);
> > +            qemu_input_queue_mtt_abs(vc->gfx.dcl.con,
> > +                                     INPUT_AXIS_X, (int) slot->x,
> > +                                     0, surface_width(vc->gfx.ds),
> > +                                     i, slot->tracking_id);
> > +            qemu_input_queue_mtt_abs(vc->gfx.dcl.con,
> > +                                     INPUT_AXIS_Y, (int) slot->y,
> > +                                     0, surface_height(vc->gfx.ds),
> > +                                     i, slot->tracking_id);
> > +            needs_sync = true;
> > +        }
> > +    }
> > +
> > +    if (needs_sync) {
> > +        qemu_input_event_sync();
> > +    }
> > +
> > +    return TRUE;
> > +}
> > +
> >  static const guint16 *gd_get_keymap(size_t *maplen)
> >  {
> >      GdkDisplay *dpy = gdk_display_get_default();
> > @@ -1977,6 +2060,8 @@ static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
> >                           G_CALLBACK(gd_key_event), vc);
> >          g_signal_connect(vc->gfx.drawing_area, "key-release-event",
> >                           G_CALLBACK(gd_key_event), vc);
> > +        g_signal_connect(vc->gfx.drawing_area, "touch-event",
> > +                         G_CALLBACK(gd_touch_event), vc);
> >
> >          g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
> >                           G_CALLBACK(gd_enter_event), vc);
> > @@ -2086,6 +2171,7 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
> >                                GSList *group, GtkWidget *view_menu)
> >  {
> >      bool zoom_to_fit = false;
> > +    int i;
> >
> >      vc->label = qemu_console_get_label(con);
> >      vc->s = s;
> > @@ -2133,6 +2219,7 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
> >                            GDK_BUTTON_PRESS_MASK |
> >                            GDK_BUTTON_RELEASE_MASK |
> >                            GDK_BUTTON_MOTION_MASK |
> > +                          GDK_TOUCH_MASK |
> >                            GDK_ENTER_NOTIFY_MASK |
> >                            GDK_LEAVE_NOTIFY_MASK |
> >                            GDK_SCROLL_MASK |
> > @@ -2168,6 +2255,11 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
> >          s->free_scale = true;
> >      }
> >
> > +    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; i++) {
> > +        struct touch_slot *slot = &touch_slots[i];
> > +        slot->tracking_id = -1;
> > +    }
> > +
> >      return group;
> >  }
> >
> > --
> > 2.38.1
> >
> >
> 
> 
> -- 
> Marc-André Lureau
>
diff mbox series

Patch

diff --git a/ui/gtk.c b/ui/gtk.c
index fd82e9b1ca..3a667bfba6 100644
--- a/ui/gtk.c
+++ b/ui/gtk.c
@@ -130,6 +130,13 @@  typedef struct VCChardev VCChardev;
 DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
                          TYPE_CHARDEV_VC)
 
+struct touch_slot {
+    int x;
+    int y;
+    int tracking_id;
+};
+static struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX];
+
 bool gtk_use_gl_area;
 
 static void gd_grab_pointer(VirtualConsole *vc, const char *reason);
@@ -1058,6 +1065,82 @@  static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
 }
 
 
+static gboolean gd_touch_event(GtkWidget *widget, GdkEventTouch *touch,
+                               void *opaque)
+{
+    VirtualConsole *vc = opaque;
+    struct touch_slot *slot;
+    uint64_t num_slot = GPOINTER_TO_UINT(touch->sequence);
+    bool needs_sync = false;
+    int update;
+    int type = -1;
+    int i;
+
+    if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
+        fprintf(stderr, "%s: unexpected touch slot number: %ld >= %d\n",
+                __func__, num_slot, INPUT_EVENT_SLOTS_MAX);
+        return FALSE;
+    }
+
+    slot = &touch_slots[num_slot];
+    slot->x = touch->x;
+    slot->y = touch->y;
+
+    switch (touch->type) {
+    case GDK_TOUCH_BEGIN:
+        type = INPUT_MULTITOUCH_TYPE_BEGIN;
+        slot->tracking_id = num_slot;
+        break;
+    case GDK_TOUCH_UPDATE:
+        type = INPUT_MULTITOUCH_TYPE_UPDATE;
+        break;
+    case GDK_TOUCH_END:
+    case GDK_TOUCH_CANCEL:
+        type = INPUT_MULTITOUCH_TYPE_END;
+        break;
+    default:
+        fprintf(stderr, "%s: unexpected touch event\n", __func__);
+    }
+
+    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
+        if (i == num_slot) {
+            update = type;
+        } else {
+            update = INPUT_MULTITOUCH_TYPE_UPDATE;
+        }
+
+        slot = &touch_slots[i];
+
+        if (slot->tracking_id == -1) {
+            continue;
+        }
+
+        if (update == INPUT_MULTITOUCH_TYPE_END) {
+            slot->tracking_id = -1;
+            qemu_input_queue_mtt(vc->gfx.dcl.con, update, i, slot->tracking_id);
+            needs_sync = true;
+        } else {
+            qemu_input_queue_mtt(vc->gfx.dcl.con, update, i, slot->tracking_id);
+            qemu_input_queue_btn(vc->gfx.dcl.con, INPUT_BUTTON_TOUCH, true);
+            qemu_input_queue_mtt_abs(vc->gfx.dcl.con,
+                                     INPUT_AXIS_X, (int) slot->x,
+                                     0, surface_width(vc->gfx.ds),
+                                     i, slot->tracking_id);
+            qemu_input_queue_mtt_abs(vc->gfx.dcl.con,
+                                     INPUT_AXIS_Y, (int) slot->y,
+                                     0, surface_height(vc->gfx.ds),
+                                     i, slot->tracking_id);
+            needs_sync = true;
+        }
+    }
+
+    if (needs_sync) {
+        qemu_input_event_sync();
+    }
+
+    return TRUE;
+}
+
 static const guint16 *gd_get_keymap(size_t *maplen)
 {
     GdkDisplay *dpy = gdk_display_get_default();
@@ -1977,6 +2060,8 @@  static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
                          G_CALLBACK(gd_key_event), vc);
         g_signal_connect(vc->gfx.drawing_area, "key-release-event",
                          G_CALLBACK(gd_key_event), vc);
+        g_signal_connect(vc->gfx.drawing_area, "touch-event",
+                         G_CALLBACK(gd_touch_event), vc);
 
         g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
                          G_CALLBACK(gd_enter_event), vc);
@@ -2086,6 +2171,7 @@  static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
                               GSList *group, GtkWidget *view_menu)
 {
     bool zoom_to_fit = false;
+    int i;
 
     vc->label = qemu_console_get_label(con);
     vc->s = s;
@@ -2133,6 +2219,7 @@  static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
                           GDK_BUTTON_PRESS_MASK |
                           GDK_BUTTON_RELEASE_MASK |
                           GDK_BUTTON_MOTION_MASK |
+                          GDK_TOUCH_MASK |
                           GDK_ENTER_NOTIFY_MASK |
                           GDK_LEAVE_NOTIFY_MASK |
                           GDK_SCROLL_MASK |
@@ -2168,6 +2255,11 @@  static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
         s->free_scale = true;
     }
 
+    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; i++) {
+        struct touch_slot *slot = &touch_slots[i];
+        slot->tracking_id = -1;
+    }
+
     return group;
 }