lib: expose keyboard modifiers as flags property

This commit is contained in:
Christian Hergert 2023-02-09 02:47:42 -08:00
parent 65ee3e10ae
commit 4075f1c2ba
3 changed files with 102 additions and 18 deletions

View File

@ -22,11 +22,14 @@
#include "config.h" #include "config.h"
#include "mks-device-private.h" #include "mks-device-private.h"
#include "mks-enums.h"
#include "mks-keyboard.h" #include "mks-keyboard.h"
struct _MksKeyboard struct _MksKeyboard
{ {
MksDevice parent_instance; MksDevice parent_instance;
MksQemuKeyboard *keyboard;
guint modifiers;
}; };
struct _MksKeyboardClass struct _MksKeyboardClass
@ -38,16 +41,77 @@ G_DEFINE_FINAL_TYPE (MksKeyboard, mks_keyboard, MKS_TYPE_DEVICE)
enum { enum {
PROP_0, PROP_0,
PROP_MODIFIERS,
N_PROPS N_PROPS
}; };
static GParamSpec *properties [N_PROPS]; static GParamSpec *properties [N_PROPS];
static void
mks_keyboard_keyboard_notify_cb (MksKeyboard *self,
GParamSpec *pspec,
MksQemuKeyboard *keyboard)
{
g_assert (MKS_IS_KEYBOARD (self));
g_assert (pspec != NULL);
g_assert (MKS_QEMU_IS_KEYBOARD (keyboard));
if (FALSE) {}
else if (strcmp (pspec->name, "modifiers") == 0)
{
self->modifiers = mks_qemu_keyboard_get_modifiers (keyboard);
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_MODIFIERS]);
}
}
static void
mks_keyboard_set_keyboard (MksKeyboard *self,
MksQemuKeyboard *keyboard)
{
g_assert (MKS_IS_KEYBOARD (self));
g_assert (!keyboard || MKS_QEMU_IS_KEYBOARD (keyboard));
if (g_set_object (&self->keyboard, keyboard))
{
g_signal_connect_object (self->keyboard,
"notify",
G_CALLBACK (mks_keyboard_keyboard_notify_cb),
self,
G_CONNECT_SWAPPED);
self->modifiers = mks_qemu_keyboard_get_modifiers (keyboard);
}
}
static gboolean
mks_keyboard_setup (MksDevice *device,
MksQemuObject *object)
{
MksKeyboard *self = (MksKeyboard *)device;
g_autolist(GDBusInterface) interfaces = NULL;
g_assert (MKS_IS_KEYBOARD (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_KEYBOARD (iface))
mks_keyboard_set_keyboard (self, MKS_QEMU_KEYBOARD (iface));
}
return self->keyboard != NULL;
}
static void static void
mks_keyboard_dispose (GObject *object) mks_keyboard_dispose (GObject *object)
{ {
MksKeyboard *self = (MksKeyboard *)object; MksKeyboard *self = (MksKeyboard *)object;
g_clear_object (&self->keyboard);
G_OBJECT_CLASS (mks_keyboard_parent_class)->dispose (object); G_OBJECT_CLASS (mks_keyboard_parent_class)->dispose (object);
} }
@ -61,21 +125,10 @@ mks_keyboard_get_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
default: case PROP_MODIFIERS:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); g_value_set_flags (value, mks_keyboard_get_modifiers (self));
} break;
}
static void
mks_keyboard_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MksKeyboard *self = MKS_KEYBOARD (object);
switch (prop_id)
{
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
} }
@ -85,13 +138,31 @@ static void
mks_keyboard_class_init (MksKeyboardClass *klass) mks_keyboard_class_init (MksKeyboardClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
MksDeviceClass *device_class = MKS_DEVICE_CLASS (klass);
device_class->setup = mks_keyboard_setup;
object_class->dispose = mks_keyboard_dispose; object_class->dispose = mks_keyboard_dispose;
object_class->get_property = mks_keyboard_get_property; object_class->get_property = mks_keyboard_get_property;
object_class->set_property = mks_keyboard_set_property;
properties [PROP_MODIFIERS] =
g_param_spec_flags ("modifiers", NULL, NULL,
MKS_TYPE_KEYBOARD_MODIFIER,
MKS_KEYBOARD_MODIFIER_NONE,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, N_PROPS, properties);
} }
static void static void
mks_keyboard_init (MksKeyboard *self) mks_keyboard_init (MksKeyboard *self)
{ {
} }
MksKeyboardModifier
mks_keyboard_get_modifiers (MksKeyboard *self)
{
g_return_val_if_fail (MKS_IS_KEYBOARD (self), 0);
return self->modifiers;
}

View File

@ -42,7 +42,17 @@ G_BEGIN_DECLS
typedef struct _MksKeyboardClass MksKeyboardClass; typedef struct _MksKeyboardClass MksKeyboardClass;
typedef enum _MksKeyboardModifier
{
MKS_KEYBOARD_MODIFIER_NONE = 0,
MKS_KEYBOARD_MODIFIER_SCROLL_LOCK = 1 << 0,
MKS_KEYBOARD_MODIFIER_NUM_LOCK = 1 << 1,
MKS_KEYBOARD_MODIFIER_CAPS_LOCK = 1 << 2,
} MksKeyboardModifier;
MKS_AVAILABLE_IN_ALL MKS_AVAILABLE_IN_ALL
GType mks_keyboard_get_type (void) G_GNUC_CONST; GType mks_keyboard_get_type (void) G_GNUC_CONST;
MKS_AVAILABLE_IN_ALL
MksKeyboardModifier mks_keyboard_get_modifiers (MksKeyboard *self);
G_END_DECLS G_END_DECLS

View File

@ -42,10 +42,13 @@ print_device_info (MksDevice *device,
G_OBJECT_TYPE_NAME (device), G_OBJECT_TYPE_NAME (device),
mks_device_get_name (device) ?: ""); mks_device_get_name (device) ?: "");
if (MKS_IS_SCREEN (device)) if (MKS_IS_SCREEN (device))
g_print (" number=%u width=%u height=%u", g_print (", number=%u, width=%u, height=%u",
mks_screen_get_number (MKS_SCREEN (device)), mks_screen_get_number (MKS_SCREEN (device)),
mks_screen_get_width (MKS_SCREEN (device)), mks_screen_get_width (MKS_SCREEN (device)),
mks_screen_get_height (MKS_SCREEN (device))); mks_screen_get_height (MKS_SCREEN (device)));
else if (MKS_IS_KEYBOARD (device))
g_print (", modifiers=0x%x",
mks_keyboard_get_modifiers (MKS_KEYBOARD (device)));
g_print (")\n"); g_print (")\n");
if (MKS_IS_SCREEN (device)) if (MKS_IS_SCREEN (device))