lib: add MksMouse press, release, and motion API

This commit is contained in:
Christian Hergert 2023-02-09 03:44:45 -08:00
parent 8898636ac4
commit 96f3235897
2 changed files with 449 additions and 2 deletions

View File

@ -141,3 +141,375 @@ mks_mouse_get_is_absolute (MksMouse *self)
return FALSE;
}
static gboolean
check_mouse (MksMouse *self,
GError **error)
{
if (self->mouse == NULL)
{
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_NOT_CONNECTED,
"Not connected");
return FALSE;
}
return TRUE;
}
static void
mks_mouse_press_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksQemuMouse *mouse = (MksQemuMouse *)object;
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
g_assert (MKS_QEMU_IS_MOUSE (mouse));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (G_IS_TASK (task));
if (!mks_qemu_mouse_call_press_finish (mouse, result, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_boolean (task, TRUE);
}
/**
* mks_mouse_press:
* @self: an #MksMouse
* @button: the #MksMouseButton that was pressed
* @cancellable: (nullable): a #GCancellable
* @callback: a #GAsyncReadyCallback to execute upon completion
* @user_data: closure data for @callback
*
* Presses a mouse button.
*/
void
mks_mouse_press (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr(GTask) task = NULL;
g_autoptr(GError) error = NULL;
g_return_if_fail (MKS_IS_MOUSE (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_mouse_press);
if (!check_mouse (self, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
mks_qemu_mouse_call_press (self->mouse,
button,
cancellable,
mks_mouse_press_cb,
g_steal_pointer (&task));
}
gboolean
mks_mouse_press_finish (MksMouse *self,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (G_IS_TASK (result), FALSE);
return g_task_propagate_boolean (G_TASK (result), error);
}
gboolean
mks_mouse_press_sync (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
if (!check_mouse (self, error))
return FALSE;
return mks_qemu_mouse_call_press_sync (self->mouse, button, cancellable, error);
}
static void
mks_mouse_release_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksQemuMouse *mouse = (MksQemuMouse *)object;
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
g_assert (MKS_QEMU_IS_MOUSE (mouse));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (G_IS_TASK (task));
if (!mks_qemu_mouse_call_release_finish (mouse, result, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_boolean (task, TRUE);
}
/**
* mks_mouse_release:
* @self: an #MksMouse
* @button: the #MksMouseButton that was releaseed
* @cancellable: (nullable): a #GCancellable
* @callback: a #GAsyncReadyCallback to execute upon completion
* @user_data: closure data for @callback
*
* Releases a mouse button.
*/
void
mks_mouse_release (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr(GTask) task = NULL;
g_autoptr(GError) error = NULL;
g_return_if_fail (MKS_IS_MOUSE (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_mouse_release);
if (!check_mouse (self, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
mks_qemu_mouse_call_release (self->mouse,
button,
cancellable,
mks_mouse_release_cb,
g_steal_pointer (&task));
}
gboolean
mks_mouse_release_finish (MksMouse *self,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (G_IS_TASK (result), FALSE);
return g_task_propagate_boolean (G_TASK (result), error);
}
gboolean
mks_mouse_release_sync (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
if (!check_mouse (self, error))
return FALSE;
return mks_qemu_mouse_call_release_sync (self->mouse, button, cancellable, error);
}
static void
mks_mouse_move_to_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksQemuMouse *mouse = (MksQemuMouse *)object;
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
g_assert (MKS_QEMU_IS_MOUSE (mouse));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (G_IS_TASK (task));
if (!mks_qemu_mouse_call_set_abs_position_finish (mouse, result, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_boolean (task, TRUE);
}
/**
* mks_mouse_move_to:
* @self: an #MksMouse
* @x: the x coordinate
* @y: the y coordinate
* @cancellable: (nullable): a #GCancellable
* @callback: a #GAsyncReadyCallback to execute upon completion
* @user_data: closure data for @callback
*
* Moves to the absolute position at coordinates (x,y).
*/
void
mks_mouse_move_to (MksMouse *self,
guint x,
guint y,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr(GTask) task = NULL;
g_autoptr(GError) error = NULL;
g_return_if_fail (MKS_IS_MOUSE (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_mouse_move_to);
if (!check_mouse (self, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
mks_qemu_mouse_call_set_abs_position (self->mouse,
x, y,
cancellable,
mks_mouse_move_to_cb,
g_steal_pointer (&task));
}
gboolean
mks_mouse_move_to_finish (MksMouse *self,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (G_IS_TASK (result), FALSE);
return g_task_propagate_boolean (G_TASK (result), error);
}
/**
* mks_mouse_move_to:
* @self: an #MksMouse
* @x: the x coordinate
* @y: the y coordinate
* @cancellable: (nullable): a #GCancellable
* @error: a location for a #GError, or %NULL
*
* Moves to the absolute position at coordinates (x,y).
*
* Returns: %TRUE if the operation was acknowledged by the Qemu instance;
* otherwise %FALSE and @error is set.
*/
gboolean
mks_mouse_move_to_sync (MksMouse *self,
guint x,
guint y,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
if (!check_mouse (self, error))
return FALSE;
return mks_qemu_mouse_call_set_abs_position_sync (self->mouse, x, y, cancellable, error);
}
static void
mks_mouse_move_by_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksQemuMouse *mouse = (MksQemuMouse *)object;
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
g_assert (MKS_QEMU_IS_MOUSE (mouse));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (G_IS_TASK (task));
if (!mks_qemu_mouse_call_rel_motion_finish (mouse, result, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_boolean (task, TRUE);
}
/**
* mks_mouse_move_by:
* @self: an #MksMouse
* @delta_x: the x coordinate delta
* @delta_y: the y coordinate delta
* @cancellable: (nullable): a #GCancellable
* @callback: a #GAsyncReadyCallback to execute upon completion
* @user_data: closure data for @callback
*
* Moves the mouse by delta_x and delta_y.
*/
void
mks_mouse_move_by (MksMouse *self,
int delta_x,
int delta_y,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr(GTask) task = NULL;
g_autoptr(GError) error = NULL;
g_return_if_fail (MKS_IS_MOUSE (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_mouse_move_by);
if (!check_mouse (self, &error))
g_task_return_error (task, g_steal_pointer (&error));
else
mks_qemu_mouse_call_rel_motion (self->mouse,
delta_x, delta_y,
cancellable,
mks_mouse_move_by_cb,
g_steal_pointer (&task));
}
gboolean
mks_mouse_move_by_finish (MksMouse *self,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (G_IS_TASK (result), FALSE);
return g_task_propagate_boolean (G_TASK (result), error);
}
/**
* mks_mouse_move_by:
* @self: an #MksMouse
* @delta_x: the x coordinate delta
* @delta_y: the y coordinate delta
* @cancellable: (nullable): a #GCancellable
* @error: a location for a #GError, or %NULL
*
* Moves the mouse by delta_x and delta_y.
*
* Returns: %TRUE if the operation was acknowledged by the Qemu instance;
* otherwise %FALSE and @error is set.
*/
gboolean
mks_mouse_move_by_sync (MksMouse *self,
int delta_x,
int delta_y,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (MKS_IS_MOUSE (self), FALSE);
g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
if (!check_mouse (self, error))
return FALSE;
return mks_qemu_mouse_call_rel_motion_sync (self->mouse, delta_x, delta_y, cancellable, error);
}

View File

@ -25,7 +25,7 @@
# error "Only <libmks.h> can be included directly."
#endif
#include <glib-object.h>
#include <gio/gio.h>
#include "mks-types.h"
#include "mks-version-macros.h"
@ -42,9 +42,84 @@ G_BEGIN_DECLS
typedef struct _MksMouseClass MksMouseClass;
typedef enum _MksMouseButton
{
MKS_MOUSE_BUTTON_LEFT = 0,
MKS_MOUSE_BUTTON_MIDDLE = 1,
MKS_MOUSE_BUTTON_RIGHT = 2,
MKS_MOUSE_BUTTON_WHEEL_UP = 3,
MKS_MOUSE_BUTTON_WHEEL_DOWN = 4,
MKS_MOUSE_BUTTON_SIDE = 5,
MKS_MOUSE_BUTTON_EXTRA = 6,
} MksMouseButton;
MKS_AVAILABLE_IN_ALL
GType mks_mouse_get_type (void) G_GNUC_CONST;
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_get_is_absolute (MksMouse *self);
gboolean mks_mouse_get_is_absolute (MksMouse *self);
MKS_AVAILABLE_IN_ALL
void mks_mouse_press (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_press_finish (MksMouse *self,
GAsyncResult *result,
GError **error);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_press_sync (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GError **error);
MKS_AVAILABLE_IN_ALL
void mks_mouse_release (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_release_finish (MksMouse *self,
GAsyncResult *result,
GError **error);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_release_sync (MksMouse *self,
MksMouseButton button,
GCancellable *cancellable,
GError **error);
MKS_AVAILABLE_IN_ALL
void mks_mouse_move_to (MksMouse *self,
guint x,
guint y,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_move_to_finish (MksMouse *self,
GAsyncResult *result,
GError **error);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_move_to_sync (MksMouse *self,
guint x,
guint y,
GCancellable *cancellable,
GError **error);
MKS_AVAILABLE_IN_ALL
void mks_mouse_move_by (MksMouse *self,
int delta_x,
int delta_y,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_move_by_finish (MksMouse *self,
GAsyncResult *result,
GError **error);
MKS_AVAILABLE_IN_ALL
gboolean mks_mouse_move_by_sync (MksMouse *self,
int delta_x,
int delta_y,
GCancellable *cancellable,
GError **error);
G_END_DECLS