diff mbox series

[1/2] ui/touch: Move event handling to a common helper

Message ID 20230613104535.356842-1-belmouss@redhat.com
State New
Headers show
Series [1/2] ui/touch: Move event handling to a common helper | expand

Commit Message

Bilal Elmoussaoui June 13, 2023, 10:45 a.m. UTC
To share code between the GTK and DBus UI bakcends
see the next commit for details

Signed-off-by: Bilal Elmoussaoui <belmouss@redhat.com>
---
 include/ui/console.h | 14 ++++++++++
 ui/console.c         | 65 ++++++++++++++++++++++++++++++++++++++++++++
 ui/gtk.c             | 62 ++++--------------------------------------
 3 files changed, 84 insertions(+), 57 deletions(-)

Comments

Marc-André Lureau June 13, 2023, 12:44 p.m. UTC | #1
Hi Bilal

On Tue, Jun 13, 2023 at 1:35 PM Bilal Elmoussaoui <belmouss@redhat.com>
wrote:

> To share code between the GTK and DBus UI bakcends
> see the next commit for details
>
> Signed-off-by: Bilal Elmoussaoui <belmouss@redhat.com>
> ---
>  include/ui/console.h | 14 ++++++++++
>  ui/console.c         | 65 ++++++++++++++++++++++++++++++++++++++++++++
>  ui/gtk.c             | 62 ++++--------------------------------------
>  3 files changed, 84 insertions(+), 57 deletions(-)
>
> diff --git a/include/ui/console.h b/include/ui/console.h
> index ae5ec46..04d4317 100644
> --- a/include/ui/console.h
> +++ b/include/ui/console.h
> @@ -5,6 +5,7 @@
>  #include "qom/object.h"
>  #include "qemu/notify.h"
>  #include "qapi/qapi-types-ui.h"
> +#include "ui/input.h"
>
>  #ifdef CONFIG_OPENGL
>  # include <epoxy/gl.h>
> @@ -95,6 +96,19 @@ bool kbd_put_qcode_console(QemuConsole *s, int qcode,
> bool ctrl);
>  void kbd_put_string_console(QemuConsole *s, const char *str, int len);
>  void kbd_put_keysym(int keysym);
>
> +/* Touch devices */
> +typedef struct touch_slot {
> +    int x;
> +    int y;
> +    int tracking_id;
> +} touch_slot;
> +
> +bool console_handle_touch_event(QemuConsole *con,
> +                                struct touch_slot
> touch_slots[INPUT_EVENT_SLOTS_MAX],
> +                                uint64_t num_slot,
> +                                int width, int height,
> +                                double x, double y,
> +                                InputMultiTouchType type);
>  /* consoles */
>
>  #define TYPE_QEMU_CONSOLE "qemu-console"
> diff --git a/ui/console.c b/ui/console.c
> index e173731..8bc6adb 100644
> --- a/ui/console.c
> +++ b/ui/console.c
> @@ -1635,6 +1635,71 @@ static bool console_compatible_with(QemuConsole
> *con,
>      return true;
>  }
>
> +bool console_handle_touch_event(QemuConsole *con,
> +                                struct touch_slot
> touch_slots[INPUT_EVENT_SLOTS_MAX],
> +                                uint64_t num_slot,
> +                                int width, int height,
> +                                double x, double y,
> +                                InputMultiTouchType type)
>

Better would be to use Error **errp.

+{
> +    struct touch_slot *slot;
> +    bool needs_sync = false;
> +    int update;
> +    int i;
> +
> +    if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
> +        warn_report("unexpected touch slot number: % " PRId64" >= %d\n",
> +                    num_slot, INPUT_EVENT_SLOTS_MAX);
> +        return FALSE;
> +    }
> +
> +    slot = &touch_slots[num_slot];
> +    slot->x = x;
> +    slot->y = y;
> +
> +    if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
> +        slot->tracking_id = num_slot;
> +    }
> +
> +    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
> +        if (i == num_slot) {
> +            update = type;
> +        } else {
> +            update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
> +        }
> +
> +        slot = &touch_slots[i];
> +
> +        if (slot->tracking_id == -1) {
> +            continue;
> +        }
> +
> +        if (update == INPUT_MULTI_TOUCH_TYPE_END) {
> +            slot->tracking_id = -1;
> +            qemu_input_queue_mtt(con, update, i, slot->tracking_id);
> +            needs_sync = true;
> +        } else {
> +            qemu_input_queue_mtt(con, update, i, slot->tracking_id);
> +            qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
> +            qemu_input_queue_mtt_abs(con,
> +                                    INPUT_AXIS_X, (int) slot->x,
> +                                    0, width,
> +                                    i, slot->tracking_id);
> +            qemu_input_queue_mtt_abs(con,
> +                                    INPUT_AXIS_Y, (int) slot->y,
> +                                    0, height,
> +                                    i, slot->tracking_id);
> +            needs_sync = true;
> +        }
> +    }
> +
> +    if (needs_sync) {
> +        qemu_input_event_sync();
> +    }
> +
> +    return TRUE;
> +}
> +
>  void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
>  {
>      /* display has opengl support */
> diff --git a/ui/gtk.c b/ui/gtk.c
> index e50f950..ebbd304 100644
> --- a/ui/gtk.c
> +++ b/ui/gtk.c
> @@ -130,11 +130,6 @@ 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;
> @@ -1068,27 +1063,12 @@ 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) {
> -        warn_report("gtk: unexpected touch slot number: % " PRId64" >=
> %d\n",
> -                    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_MULTI_TOUCH_TYPE_BEGIN;
> -        slot->tracking_id = num_slot;
>          break;
>      case GDK_TOUCH_UPDATE:
>          type = INPUT_MULTI_TOUCH_TYPE_UPDATE;
> @@ -1099,45 +1079,13 @@ static gboolean gd_touch_event(GtkWidget *widget,
> GdkEventTouch *touch,
>          break;
>      default:
>          warn_report("gtk: unexpected touch event type\n");
> +        return FALSE;
>      }
>
> -    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
> -        if (i == num_slot) {
> -            update = type;
> -        } else {
> -            update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
> -        }
> -
> -        slot = &touch_slots[i];
> -
> -        if (slot->tracking_id == -1) {
> -            continue;
> -        }
> -
> -        if (update == INPUT_MULTI_TOUCH_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;
> +    return console_handle_touch_event(vc->gfx.dcl.con, touch_slots,
> +                                      num_slot, surface_width(vc->gfx.ds),
> +                                      surface_height(vc->gfx.ds),
> touch->x,
> +                                      touch->y, type);
>

Then we can call with &error_warn form here.

otherwise, lgtm
diff mbox series

Patch

diff --git a/include/ui/console.h b/include/ui/console.h
index ae5ec46..04d4317 100644
--- a/include/ui/console.h
+++ b/include/ui/console.h
@@ -5,6 +5,7 @@ 
 #include "qom/object.h"
 #include "qemu/notify.h"
 #include "qapi/qapi-types-ui.h"
+#include "ui/input.h"
 
 #ifdef CONFIG_OPENGL
 # include <epoxy/gl.h>
@@ -95,6 +96,19 @@  bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl);
 void kbd_put_string_console(QemuConsole *s, const char *str, int len);
 void kbd_put_keysym(int keysym);
 
+/* Touch devices */
+typedef struct touch_slot {
+    int x;
+    int y;
+    int tracking_id;
+} touch_slot;
+
+bool console_handle_touch_event(QemuConsole *con,
+                                struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
+                                uint64_t num_slot,
+                                int width, int height,
+                                double x, double y,
+                                InputMultiTouchType type);
 /* consoles */
 
 #define TYPE_QEMU_CONSOLE "qemu-console"
diff --git a/ui/console.c b/ui/console.c
index e173731..8bc6adb 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -1635,6 +1635,71 @@  static bool console_compatible_with(QemuConsole *con,
     return true;
 }
 
+bool console_handle_touch_event(QemuConsole *con,
+                                struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
+                                uint64_t num_slot,
+                                int width, int height,
+                                double x, double y,
+                                InputMultiTouchType type)
+{
+    struct touch_slot *slot;
+    bool needs_sync = false;
+    int update;
+    int i;
+
+    if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
+        warn_report("unexpected touch slot number: % " PRId64" >= %d\n",
+                    num_slot, INPUT_EVENT_SLOTS_MAX);
+        return FALSE;
+    }
+
+    slot = &touch_slots[num_slot];
+    slot->x = x;
+    slot->y = y;
+
+    if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
+        slot->tracking_id = num_slot;
+    }
+
+    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
+        if (i == num_slot) {
+            update = type;
+        } else {
+            update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
+        }
+
+        slot = &touch_slots[i];
+
+        if (slot->tracking_id == -1) {
+            continue;
+        }
+
+        if (update == INPUT_MULTI_TOUCH_TYPE_END) {
+            slot->tracking_id = -1;
+            qemu_input_queue_mtt(con, update, i, slot->tracking_id);
+            needs_sync = true;
+        } else {
+            qemu_input_queue_mtt(con, update, i, slot->tracking_id);
+            qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
+            qemu_input_queue_mtt_abs(con,
+                                    INPUT_AXIS_X, (int) slot->x,
+                                    0, width,
+                                    i, slot->tracking_id);
+            qemu_input_queue_mtt_abs(con,
+                                    INPUT_AXIS_Y, (int) slot->y,
+                                    0, height,
+                                    i, slot->tracking_id);
+            needs_sync = true;
+        }
+    }
+
+    if (needs_sync) {
+        qemu_input_event_sync();
+    }
+
+    return TRUE;
+}
+
 void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
 {
     /* display has opengl support */
diff --git a/ui/gtk.c b/ui/gtk.c
index e50f950..ebbd304 100644
--- a/ui/gtk.c
+++ b/ui/gtk.c
@@ -130,11 +130,6 @@  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;
@@ -1068,27 +1063,12 @@  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) {
-        warn_report("gtk: unexpected touch slot number: % " PRId64" >= %d\n",
-                    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_MULTI_TOUCH_TYPE_BEGIN;
-        slot->tracking_id = num_slot;
         break;
     case GDK_TOUCH_UPDATE:
         type = INPUT_MULTI_TOUCH_TYPE_UPDATE;
@@ -1099,45 +1079,13 @@  static gboolean gd_touch_event(GtkWidget *widget, GdkEventTouch *touch,
         break;
     default:
         warn_report("gtk: unexpected touch event type\n");
+        return FALSE;
     }
 
-    for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
-        if (i == num_slot) {
-            update = type;
-        } else {
-            update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
-        }
-
-        slot = &touch_slots[i];
-
-        if (slot->tracking_id == -1) {
-            continue;
-        }
-
-        if (update == INPUT_MULTI_TOUCH_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;
+    return console_handle_touch_event(vc->gfx.dcl.con, touch_slots,
+                                      num_slot, surface_width(vc->gfx.ds),
+                                      surface_height(vc->gfx.ds), touch->x,
+                                      touch->y, type);
 }
 
 static const guint16 *gd_get_keymap(size_t *maplen)