lib: add basic input controllers to MksDisplay

Still plenty to do on the motion bits, but at least basic key controller
events propagate now.
This commit is contained in:
Christian Hergert 2023-02-14 13:34:45 -08:00
parent 9609a63e2a
commit beb7299d25
2 changed files with 197 additions and 0 deletions

View File

@ -20,10 +20,16 @@
#include "config.h" #include "config.h"
#include <stdlib.h>
#include "mks-css-private.h" #include "mks-css-private.h"
#include "mks-display.h" #include "mks-display.h"
#include "mks-keyboard.h"
#include "mks-mouse.h"
#include "mks-screen.h" #include "mks-screen.h"
#include "mks-keymap-xorgevdev2qnum-private.h"
typedef struct typedef struct
{ {
/* The screen being displayed. We've gotten a GdkPaintable from it /* The screen being displayed. We've gotten a GdkPaintable from it
@ -35,6 +41,16 @@ typedef struct
* painable when available. * painable when available.
*/ */
GtkPicture *picture; GtkPicture *picture;
/* Used to update the cursor position by calling into the MksMouse
* API using move_to/move_by.
*/
GtkEventControllerMotion *motion;
/* Used to send key press/release events by calling into MksKeyboard
* API using press/release and the hardware keycode.
*/
GtkEventControllerKey *key;
} MksDisplayPrivate; } MksDisplayPrivate;
enum { enum {
@ -48,6 +64,165 @@ G_DEFINE_TYPE_WITH_PRIVATE (MksDisplay, mks_display, GTK_TYPE_WIDGET)
static GParamSpec *properties [N_PROPS]; static GParamSpec *properties [N_PROPS];
static void
mks_display_translate_keycode (MksDisplay *self,
guint keyval,
guint keycode,
guint *translated)
{
g_assert (MKS_IS_DISPLAY (self));
g_assert (translated != NULL);
if (keycode < xorgevdev_to_qnum_len &&
xorgevdev_to_qnum[keycode] != 0)
*translated = xorgevdev_to_qnum[keycode];
else
*translated = keycode;
}
static void
mks_display_keyboard_press_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksKeyboard *keyboard = (MksKeyboard *)object;
g_autoptr(MksDisplay) self = user_data;
g_autoptr(GError) error = NULL;
g_assert (MKS_IS_KEYBOARD (keyboard));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (MKS_IS_DISPLAY (self));
if (!mks_keyboard_press_finish (keyboard, result, &error))
g_warning ("Keyboard press failed: %s", error->message);
}
static void
mks_display_key_key_pressed_cb (MksDisplay *self,
guint keyval,
guint keycode,
GdkModifierType state,
GtkEventControllerKey *key)
{
MksDisplayPrivate *priv = mks_display_get_instance_private (self);
MksKeyboard *keyboard;
guint qkeycode;
g_assert (MKS_IS_DISPLAY (self));
g_assert (GTK_IS_EVENT_CONTROLLER_KEY (key));
if (priv->screen == NULL)
return;
keyboard = mks_screen_get_keyboard (priv->screen);
mks_display_translate_keycode (self, keyval, keycode, &qkeycode);
mks_keyboard_press (keyboard,
qkeycode,
NULL,
mks_display_keyboard_press_cb,
g_object_ref (self));
}
static void
mks_display_keyboard_release_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
MksKeyboard *keyboard = (MksKeyboard *)object;
g_autoptr(MksDisplay) self = user_data;
g_autoptr(GError) error = NULL;
g_assert (MKS_IS_KEYBOARD (keyboard));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (MKS_IS_DISPLAY (self));
if (!mks_keyboard_release_finish (keyboard, result, &error))
g_warning ("Keyboard release failed: %s", error->message);
}
static void
mks_display_key_key_released_cb (MksDisplay *self,
guint keyval,
guint keycode,
GdkModifierType state,
GtkEventControllerKey *key)
{
MksDisplayPrivate *priv = mks_display_get_instance_private (self);
MksKeyboard *keyboard;
guint qkeycode;
g_assert (MKS_IS_DISPLAY (self));
g_assert (GTK_IS_EVENT_CONTROLLER_KEY (key));
if (priv->screen == NULL)
return;
keyboard = mks_screen_get_keyboard (priv->screen);
mks_display_translate_keycode (self, keyval, keycode, &qkeycode);
mks_keyboard_release (keyboard,
qkeycode,
NULL,
mks_display_keyboard_release_cb,
g_object_ref (self));
}
static void
mks_display_translate_coordinate (MksDisplay *self,
double *x,
double *y)
{
g_assert (MKS_IS_DISPLAY (self));
}
static void
mks_display_motion_enter_cb (MksDisplay *self,
double x,
double y,
GtkEventControllerMotion *motion)
{
MksDisplayPrivate *priv = mks_display_get_instance_private (self);
MksMouse *mouse;
g_assert (MKS_IS_DISPLAY (self));
g_assert (GTK_IS_EVENT_CONTROLLER_MOTION (motion));
if (priv->screen == NULL)
return;
mouse = mks_screen_get_mouse (priv->screen);
mks_display_translate_coordinate (self, &x, &y);
mks_mouse_move_to (mouse, x, y, NULL, NULL, NULL);
}
static void
mks_display_motion_motion_cb (MksDisplay *self,
double x,
double y,
GtkEventControllerMotion *motion)
{
MksDisplayPrivate *priv = mks_display_get_instance_private (self);
MksMouse *mouse;
g_assert (MKS_IS_DISPLAY (self));
g_assert (GTK_IS_EVENT_CONTROLLER_MOTION (motion));
if (priv->screen == NULL)
return;
mouse = mks_screen_get_mouse (priv->screen);
mks_display_translate_coordinate (self, &x, &y);
mks_mouse_move_to (mouse, x, y, NULL, NULL, NULL);
}
static void
mks_display_motion_leave_cb (MksDisplay *self,
GtkEventControllerMotion *motion)
{
g_assert (MKS_IS_DISPLAY (self));
g_assert (GTK_IS_EVENT_CONTROLLER_MOTION (motion));
}
static void static void
mks_display_attach_cb (GObject *object, mks_display_attach_cb (GObject *object,
GAsyncResult *result, GAsyncResult *result,
@ -186,7 +361,14 @@ mks_display_class_init (MksDisplayClass *klass)
gtk_widget_class_set_css_name (widget_class, "MksDisplay"); gtk_widget_class_set_css_name (widget_class, "MksDisplay");
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT); gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/libmks/mks-display.ui"); gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/libmks/mks-display.ui");
gtk_widget_class_bind_template_child_private (widget_class, MksDisplay, key);
gtk_widget_class_bind_template_child_private (widget_class, MksDisplay, motion);
gtk_widget_class_bind_template_child_private (widget_class, MksDisplay, picture); gtk_widget_class_bind_template_child_private (widget_class, MksDisplay, picture);
gtk_widget_class_bind_template_callback (widget_class, mks_display_motion_enter_cb);
gtk_widget_class_bind_template_callback (widget_class, mks_display_motion_motion_cb);
gtk_widget_class_bind_template_callback (widget_class, mks_display_motion_leave_cb);
gtk_widget_class_bind_template_callback (widget_class, mks_display_key_key_pressed_cb);
gtk_widget_class_bind_template_callback (widget_class, mks_display_key_key_released_cb);
_mks_css_init (); _mks_css_init ();
} }

View File

@ -1,9 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<template class="MksDisplay" parent="GtkWidget"> <template class="MksDisplay" parent="GtkWidget">
<property name="focusable">false</property>
<child> <child>
<object class="GtkPicture" id="picture"> <object class="GtkPicture" id="picture">
<property name="content-fit">scale-down</property> <property name="content-fit">scale-down</property>
<property name="focusable">true</property>
<child>
<object class="GtkEventControllerMotion" id="motion">
<signal name="enter" handler="mks_display_motion_enter_cb" swapped="true"/>
<signal name="leave" handler="mks_display_motion_leave_cb" swapped="true"/>
<signal name="motion" handler="mks_display_motion_motion_cb" swapped="true"/>
</object>
</child>
<child>
<object class="GtkEventControllerKey" id="key">
<signal name="key-pressed" handler="mks_display_key_key_pressed_cb" swapped="true"/>
<signal name="key-released" handler="mks_display_key_key_released_cb" swapped="true"/>
</object>
</child>
</object> </object>
</child> </child>
</template> </template>