device: Implement the new touch interface

This commit is contained in:
Bilal Elmoussaoui 2023-06-15 13:48:55 +02:00
parent f8a8b61171
commit c28a6f85e2
12 changed files with 566 additions and 3 deletions

View File

@ -39,8 +39,9 @@
"Text" (see :dbus:prop:`Type` and other properties).
Interactions with a console may be done with
:dbus:iface:`org.qemu.Display1.Keyboard` and
:dbus:iface:`org.qemu.Display1.Mouse` interfaces when available.
:dbus:iface:`org.qemu.Display1.Keyboard`,
:dbus:iface:`org.qemu.Display1.Mouse` and
:dbus:iface:`org.qemu.Display1.Touch` interfaces when available.
-->
<interface name="org.qemu.Display1.Console">
<!--
@ -237,6 +238,46 @@
<property name="IsAbsolute" type="b" access="read"/>
</interface>
<!--
org.qemu.Display1.Touch:
This interface in implemented on ``/org/qemu/Display1/Console_$id`` (see
:dbus:iface:`~org.qemu.Display1.Console` documentation).
.. _dbus-kind-values:
**Kind values**::
Begin = 0
Update = 1
End = 2
Cancel = 3
-->
<interface name="org.qemu.Display1.Touch">
<!--
SendEvent:
@kind: The touch event kind
@num_slot: The slot number.
@x: The x coordinates.
@y: The y coordinates.
Send a touch gesture begin event.
-->
<method name="SendEvent">
<arg type="u" name="kind" direction="in"/>
<arg type="t" name="num_slot" direction="in"/>
<arg type="d" name="x" direction="in"/>
<arg type="d" name="y" direction="in"/>
</method>
<!--
MaxSlots:
The maximum number of slots.
-->
<property name="MaxSlots" type="i" access="read"/>
</interface>
<!--
org.qemu.Display1.Listener:

View File

@ -35,6 +35,7 @@ G_BEGIN_DECLS
# include "mks-screen.h"
# include "mks-screen-attributes.h"
# include "mks-session.h"
# include "mks-touchable.h"
# include "mks-types.h"
# include "mks-version.h"
# include "mks-version-macros.h"

View File

@ -15,6 +15,7 @@ libmks_sources = [
'mks-screen.c',
'mks-screen-attributes.c',
'mks-session.c',
'mks-touchable.c',
]
libmks_headers = [
@ -27,6 +28,7 @@ libmks_headers = [
'mks-screen.h',
'mks-screen-attributes.h',
'mks-session.h',
'mks-touchable.h',
'mks-types.h',
'mks-version-macros.h',
]

View File

@ -42,5 +42,8 @@ void mks_display_picture_set_mouse (MksDisplayPicture *self,
MksKeyboard *mks_display_picture_get_keyboard (MksDisplayPicture *self);
void mks_display_picture_set_keyboard (MksDisplayPicture *self,
MksKeyboard *keyboard);
MksTouchable *mks_display_picture_get_touchable (MksDisplayPicture *self);
void mks_display_picture_set_touchable (MksDisplayPicture *self,
MksTouchable *touchable);
G_END_DECLS

View File

@ -26,6 +26,7 @@
#include "mks-display-picture-private.h"
#include "mks-keyboard.h"
#include "mks-mouse.h"
#include "mks-touchable.h"
#include "mks-util-private.h"
struct _MksDisplayPicture
@ -36,6 +37,7 @@ struct _MksDisplayPicture
MksPaintable *paintable;
MksKeyboard *keyboard;
MksMouse *mouse;
MksTouchable *touchable;
double last_mouse_x;
double last_mouse_y;
@ -46,6 +48,7 @@ enum {
PROP_PAINTABLE,
PROP_KEYBOARD,
PROP_MOUSE,
PROP_TOUCHABLE,
N_PROPS
};
@ -121,6 +124,23 @@ mks_display_picture_mouse_move_by_cb (GObject *object,
g_warning ("Failed move_by: %s", error->message);
}
static void
mks_display_picture_touchable_send_event_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksTouchable *touchable = (MksTouchable *)object;
g_autoptr(MksDisplayPicture) self = user_data;
g_autoptr(GError) error = NULL;
g_assert (MKS_IS_TOUCHABLE (touchable));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (MKS_IS_DISPLAY_PICTURE (self));
if (!mks_touchable_send_event_finish (touchable, result, &error))
g_debug ("Failed to send touch event: %s", error->message);
}
static void
mks_display_picture_translate_button (MksDisplayPicture *self,
int *button)
@ -230,19 +250,52 @@ mks_display_picture_legacy_event_cb (MksDisplayPicture *self,
{
GdkPaintable *paintable;
GdkEventType event_type;
GdkEventSequence *sequence;
g_assert (MKS_IS_DISPLAY_PICTURE (self));
g_assert (GDK_IS_EVENT (event));
g_assert (GTK_IS_EVENT_CONTROLLER_LEGACY (controller));
if (self->keyboard == NULL || self->mouse == NULL || self->paintable == NULL)
if (self->keyboard == NULL || self->mouse == NULL || self->touchable == NULL || self->paintable == NULL)
return GDK_EVENT_PROPAGATE;
event_type = gdk_event_get_event_type (event);
paintable = GDK_PAINTABLE (self->paintable);
sequence = gdk_event_get_event_sequence (event);
switch ((int)event_type)
{
case GDK_TOUCH_BEGIN:
case GDK_TOUCH_UPDATE:
case GDK_TOUCH_CANCEL:
case GDK_TOUCH_END:
{
double guest_x, guest_y;
uint64_t num_slot = GPOINTER_TO_UINT (sequence);
MksTouchEventKind kind;
if (event_type == GDK_TOUCH_BEGIN)
kind = MKS_TOUCH_EVENT_BEGIN;
else if (event_type == GDK_TOUCH_UPDATE)
kind = MKS_TOUCH_EVENT_UPDATE;
else if (event_type == GDK_TOUCH_CANCEL)
kind = MKS_TOUCH_EVENT_CANCEL;
else
kind = MKS_TOUCH_EVENT_END;
if (mks_display_picture_event_get_guest_position (self, event, &guest_x, &guest_y))
{
mks_touchable_send_event (self->touchable, kind,
num_slot,
guest_x, guest_y,
NULL,
mks_display_picture_touchable_send_event_cb,
g_object_ref (self));
return GDK_EVENT_STOP;
}
break;
}
case GDK_MOTION_NOTIFY:
{
GdkSurface *surface = gdk_event_get_surface (event);
@ -588,6 +641,10 @@ mks_display_picture_get_property (GObject *object,
g_value_set_object (value, mks_display_picture_get_mouse (self));
break;
case PROP_TOUCHABLE:
g_value_set_object (value, mks_display_picture_get_touchable (self));
break;
case PROP_PAINTABLE:
g_value_set_object (value, mks_display_picture_get_paintable (self));
break;
@ -615,6 +672,10 @@ mks_display_picture_set_property (GObject *object,
mks_display_picture_set_mouse (self, g_value_get_object (value));
break;
case PROP_TOUCHABLE:
mks_display_picture_set_touchable (self, g_value_get_object (value));
break;
case PROP_PAINTABLE:
mks_display_picture_set_paintable (self, g_value_get_object (value));
break;
@ -648,6 +709,11 @@ mks_display_picture_class_init (MksDisplayPictureClass *klass)
MKS_TYPE_MOUSE,
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
properties[PROP_TOUCHABLE] =
g_param_spec_object ("touchable", NULL, NULL,
MKS_TYPE_TOUCHABLE,
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
properties[PROP_PAINTABLE] =
g_param_spec_object ("paintable", NULL, NULL,
MKS_TYPE_PAINTABLE,
@ -763,3 +829,22 @@ mks_display_picture_set_keyboard (MksDisplayPicture *self,
if (g_set_object (&self->keyboard, keyboard))
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_KEYBOARD]);
}
MksTouchable *
mks_display_picture_get_touchable (MksDisplayPicture *self)
{
g_return_val_if_fail (MKS_IS_DISPLAY_PICTURE (self), NULL);
return self->touchable;
}
void
mks_display_picture_set_touchable (MksDisplayPicture *self,
MksTouchable *touchable)
{
g_return_if_fail (MKS_IS_DISPLAY_PICTURE (self));
g_return_if_fail (!touchable || MKS_IS_TOUCHABLE (touchable));
if (g_set_object (&self->touchable, touchable))
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TOUCHABLE]);
}

View File

@ -141,6 +141,7 @@ mks_display_connect (MksDisplay *self,
{
mks_display_picture_set_keyboard (priv->picture, mks_screen_get_keyboard (screen));
mks_display_picture_set_mouse (priv->picture, mks_screen_get_mouse (screen));
mks_display_picture_set_touchable (priv->picture, mks_screen_get_touchable (screen));
mks_screen_resizer_set_screen (priv->resizer, screen);
mks_screen_attach (screen,
@ -172,6 +173,7 @@ mks_display_disconnect (MksDisplay *self)
mks_display_picture_set_paintable (priv->picture, NULL);
mks_display_picture_set_keyboard (priv->picture, NULL);
mks_display_picture_set_mouse (priv->picture, NULL);
mks_display_picture_set_touchable (priv->picture, NULL);
}
MKS_EXIT;

View File

@ -35,6 +35,7 @@
#include "mks-screen.h"
#include "mks-screen-attributes.h"
#include "mks-session.h"
#include "mks-touchable.h"
#include "mks-version.h"
static void
@ -65,6 +66,7 @@ mks_init_gtypes (void)
g_type_ensure (MKS_TYPE_SCREEN);
g_type_ensure (MKS_TYPE_SCREEN_ATTRIBUTES);
g_type_ensure (MKS_TYPE_SESSION);
g_type_ensure (MKS_TYPE_TOUCHABLE);
}
/**

View File

@ -34,6 +34,7 @@
#include "mks-paintable-private.h"
#include "mks-screen-attributes-private.h"
#include "mks-screen.h"
#include "mks-touchable.h"
struct _MksScreenClass
{
@ -49,6 +50,7 @@ struct _MksScreen
MksKeyboard *keyboard;
MksMouse *mouse;
MksTouchable *touchable;
guint number;
guint width;
@ -206,6 +208,8 @@ mks_screen_setup (MksDevice *device,
self->keyboard = _mks_device_new (MKS_TYPE_KEYBOARD, device->session, object);
else if (MKS_QEMU_IS_MOUSE (iface))
self->mouse = _mks_device_new (MKS_TYPE_MOUSE, device->session, object);
else if (MKS_QEMU_IS_TOUCH (iface))
self->touchable = _mks_device_new (MKS_TYPE_TOUCHABLE, device->session, object);
}
return self->console != NULL &&
@ -360,6 +364,22 @@ mks_screen_get_mouse (MksScreen *self)
return self->mouse;
}
/**
* mks_screen_get_touchable:
* @self: a #MksScreen
*
* Gets the #MksScreen:touchable property.
*
* Returns: (transfer none): a #MksTouchable
*/
MksTouchable *
mks_screen_get_touchable (MksScreen *self)
{
g_return_val_if_fail (MKS_IS_SCREEN (self), NULL);
return self->touchable;
}
/**
* mks_screen_get_kind:
* @self: a #MksScreen

View File

@ -65,6 +65,8 @@ MksKeyboard *mks_screen_get_keyboard (MksScreen *self);
MKS_AVAILABLE_IN_ALL
MksMouse *mks_screen_get_mouse (MksScreen *self);
MKS_AVAILABLE_IN_ALL
MksTouchable *mks_screen_get_touchable (MksScreen *self);
MKS_AVAILABLE_IN_ALL
guint mks_screen_get_width (MksScreen *self);
MKS_AVAILABLE_IN_ALL
guint mks_screen_get_height (MksScreen *self);

316
lib/mks-touchable.c Normal file
View File

@ -0,0 +1,316 @@
/*
* mks-touchable.c
*
* Copyright 2023 Bilal Elmoussaoui <belmouss@redhat.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include "mks-device-private.h"
#include "mks-touchable.h"
#include "mks-util-private.h"
/**
* MksTouchable:
*
* A virtualized QEMU touch device.
*/
struct _MksTouchable
{
MksDevice parent_instance;
MksQemuTouch *touch;
};
struct _MksTouchableClass
{
MksDeviceClass parent_instance;
};
G_DEFINE_FINAL_TYPE (MksTouchable, mks_touchable, MKS_TYPE_DEVICE)
enum {
PROP_0,
PROP_MAX_SLOTS,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
static void
mks_touchable_set_touch (MksTouchable *self,
MksQemuTouch *touch)
{
g_assert (MKS_IS_TOUCHABLE (self));
g_assert (!touch || MKS_QEMU_IS_TOUCH (touch));
g_set_object (&self->touch, touch);
}
static gboolean
mks_touchable_setup (MksDevice *device,
MksQemuObject *object)
{
MksTouchable *self = (MksTouchable *)device;
g_autolist(GDBusInterface) interfaces = NULL;
g_assert (MKS_IS_TOUCHABLE (self));
g_assert (MKS_QEMU_IS_OBJECT (object));
interfaces = g_dbus_object_get_interfaces (G_DBUS_OBJECT (object));
for (const GList *iter = interfaces; iter; iter = iter->next)
{
GDBusInterface *iface = iter->data;
if (MKS_QEMU_IS_TOUCH (iface))
mks_touchable_set_touch (self, MKS_QEMU_TOUCH (iface));
}
return self->touch != NULL;
}
static void
mks_touchable_dispose (GObject *object)
{
MksTouchable *self = (MksTouchable *)object;
g_clear_object (&self->touch);
G_OBJECT_CLASS (mks_touchable_parent_class)->dispose (object);
}
static void
mks_touchable_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MksTouchable *self = MKS_TOUCHABLE (object);
switch (prop_id)
{
case PROP_MAX_SLOTS:
g_value_set_int (value, mks_touchable_get_max_slots (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
mks_touchable_class_init (MksTouchableClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
MksDeviceClass *device_class = MKS_DEVICE_CLASS (klass);
device_class->setup = mks_touchable_setup;
object_class->dispose = mks_touchable_dispose;
object_class->get_property = mks_touchable_get_property;
/**
* MksTouchable:max-slots:
*
* The maximum number of slots.
*/
properties [PROP_MAX_SLOTS] =
g_param_spec_int ("max-slots", NULL, NULL,
0, G_MAXINT, 0,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
mks_touchable_init (MksTouchable *self)
{
}
static gboolean
check_touch (MksTouchable *self,
GError **error)
{
if (self->touch == NULL)
{
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_NOT_CONNECTED,
"Not connected");
return FALSE;
}
return TRUE;
}
static void
mks_touchable_send_event_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksQemuTouch *touch = (MksQemuTouch *)object;
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
MKS_ENTRY;
g_assert (MKS_QEMU_IS_TOUCH (touch));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (G_IS_TASK (task));
if (!mks_qemu_touch_call_send_event_finish (touch, result, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_boolean (task, TRUE);
MKS_EXIT;
}
/**
* mks_touchable_send_event:
* @self: an #MksTouchable
* @num_slot: the slot number
* @x: the x absolute coordinate
* @y: the y absolute coordinate
* @cancellable: (nullable): a #GCancellable
* @callback: a #GAsyncReadyCallback to execute upon completion
* @user_data: closure data for @callback
*
* Send a touch event.
*/
void
mks_touchable_send_event (MksTouchable *self,
MksTouchEventKind kind,
guint64 num_slot,
double x,
double y,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr(GTask) task = NULL;
g_autoptr(GError) error = NULL;
MKS_ENTRY;
g_return_if_fail (MKS_IS_TOUCHABLE (self));
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_source_tag (task, mks_touchable_send_event);
if (!check_touch (self, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
mks_qemu_touch_call_send_event (self->touch, kind,
num_slot, x, y,
cancellable,
mks_touchable_send_event_cb,
g_steal_pointer (&task));
MKS_EXIT;
}
/**
* mks_touchable_send_event_finish:
* @self: a `MksTouchable`
* @result: a #GAsyncResult provided to callback
* @error: a location for a #GError, or %NULL
*
* Completes a call to [method@Mks.Touchable.send_event].
*
* Returns: %TRUE if the operation completed successfully; otherwise %FALSE
* and @error is set.
*/
gboolean
mks_touchable_send_event_finish (MksTouchable *self,
GAsyncResult *result,
GError **error)
{
gboolean ret;
MKS_ENTRY;
g_return_val_if_fail (MKS_IS_TOUCHABLE (self), FALSE);
g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
ret = g_task_propagate_boolean (G_TASK (result), error);
MKS_RETURN (ret);
}
/**
* mks_touchable_send_event_sync:
* @self: a `MksTouchable`
* @kind: the event kind
* @num_slot: the slot number
* @x: the x absolute coordinate
* @y: the y absolute coordinate
* @cancellable: (nullable): a #GCancellable
* @error: a location for a #GError, or %NULL
*
* Synchronously send a touch event.
*
* Returns: %TRUE if the operation was acknowledged by the QEMU instance;
* otherwise %FALSE and @error is set.
*/
gboolean
mks_touchable_send_event_sync (MksTouchable *self,
MksTouchEventKind kind,
guint64 num_slot,
double x,
double y,
GCancellable *cancellable,
GError **error)
{
gboolean ret;
MKS_ENTRY;
g_return_val_if_fail (MKS_IS_TOUCHABLE (self), FALSE);
g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
if (!check_touch (self, error))
MKS_RETURN (FALSE);
ret = mks_qemu_touch_call_send_event_sync (self->touch, kind,
num_slot, x, y,
cancellable, error);
MKS_RETURN (ret);
}
/**
* mks_touchable_get_max_slots:
* @self: A `MksTouchable`.
*
* Returns the maximum number of slots.
*/
int
mks_touchable_get_max_slots (MksTouchable *self)
{
g_return_val_if_fail (MKS_IS_TOUCHABLE (self), 0);
if (self->touch)
return mks_qemu_touch_get_max_slots (self->touch);
return 0;
}

88
lib/mks-touchable.h Normal file
View File

@ -0,0 +1,88 @@
/*
* mks-touchable.h
*
* Copyright 2023 Bilal Elmoussaoui <belmouss@redhat.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once
#if !defined(MKS_INSIDE) && !defined(MKS_COMPILATION)
# error "Only <libmks.h> can be included directly."
#endif
#include <gio/gio.h>
#include "mks-types.h"
#include "mks-version-macros.h"
G_BEGIN_DECLS
#define MKS_TYPE_TOUCHABLE (mks_touchable_get_type ())
#define MKS_TOUCHABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MKS_TYPE_TOUCHABLE, MksTouchable))
#define MKS_TOUCHABLE_CONST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MKS_TYPE_TOUCHABLE, MksTouchable const))
#define MKS_TOUCHABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MKS_TYPE_TOUCHABLE, MksTouchableClass))
#define MKS_IS_TOUCHABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MKS_TYPE_TOUCHABLE))
#define MKS_IS_TOUCHABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MKS_TYPE_TOUCHABLE))
#define MKS_TOUCHABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MKS_TYPE_TOUCHABLE, MksTouchableClass))
typedef struct _MksTouchableClass MksTouchableClass;
/**
* MksTouchEventKind:
* @MKS_TOUCH_EVENT_BEGIN: The touch event has just started.
* @MKS_TOUCH_EVENT_UPDATE: The touch event has been updated.
* @MKS_TOUCH_EVENT_END: The touch event has finished.
* @MKS_TOUCH_EVENT_CANCEL: The touch event has been canceled.
*
* The type of a touch event.
*/
typedef enum _MksTouchEventKind
{
MKS_TOUCH_EVENT_BEGIN = 0,
MKS_TOUCH_EVENT_UPDATE = 1,
MKS_TOUCH_EVENT_END = 2,
MKS_TOUCH_EVENT_CANCEL = 3,
} MksTouchEventKind;
MKS_AVAILABLE_IN_ALL
GType mks_touchable_get_type (void) G_GNUC_CONST;
MKS_AVAILABLE_IN_ALL
void mks_touchable_send_event (MksTouchable *self,
MksTouchEventKind kind,
guint64 num_slot,
double x,
double y,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
MKS_AVAILABLE_IN_ALL
gboolean mks_touchable_send_event_finish (MksTouchable *self,
GAsyncResult *result,
GError **error);
MKS_AVAILABLE_IN_ALL
gboolean mks_touchable_send_event_sync (MksTouchable *self,
MksTouchEventKind kind,
guint64 num_slot,
double x,
double y,
GCancellable *cancellable,
GError **error);
MKS_AVAILABLE_IN_ALL
int mks_touchable_get_max_slots (MksTouchable *self);
G_END_DECLS

View File

@ -36,5 +36,6 @@ typedef struct _MksScreen MksScreen;
typedef struct _MksSession MksSession;
typedef struct _MksScreenAttributes MksScreenAttributes;
typedef struct _MksSpeaker MksSpeaker;
typedef struct _MksTouchable MksTouchable;
G_END_DECLS