lib: add session weak pointer to devices

This is handy because it gives devices the ability to locate/coordinate
with other subsystems.
This commit is contained in:
Christian Hergert 2023-02-09 12:54:48 -08:00
parent dac3fc284a
commit 0478d202f8
4 changed files with 10 additions and 3 deletions

View File

@ -23,12 +23,14 @@
#include "mks-device.h" #include "mks-device.h"
#include "mks-qemu.h" #include "mks-qemu.h"
#include "mks-session.h"
G_BEGIN_DECLS G_BEGIN_DECLS
struct _MksDevice struct _MksDevice
{ {
GObject parent_instance; GObject parent_instance;
MksSession *session;
MksQemuObject *object; MksQemuObject *object;
char *name; char *name;
}; };
@ -42,6 +44,7 @@ struct _MksDeviceClass
}; };
gpointer _mks_device_new (GType device_type, gpointer _mks_device_new (GType device_type,
MksSession *session,
MksQemuObject *object); MksQemuObject *object);
void _mks_device_set_name (MksDevice *self, void _mks_device_set_name (MksDevice *self,
const char *name); const char *name);

View File

@ -48,6 +48,7 @@ mks_device_dispose (GObject *object)
{ {
MksDevice *self = (MksDevice *)object; MksDevice *self = (MksDevice *)object;
g_clear_weak_pointer (&self->session);
g_clear_pointer (&self->name, g_free); g_clear_pointer (&self->name, g_free);
g_clear_object (&self->object); g_clear_object (&self->object);
@ -116,17 +117,20 @@ _mks_device_set_name (MksDevice *self,
gpointer gpointer
_mks_device_new (GType device_type, _mks_device_new (GType device_type,
MksSession *session,
MksQemuObject *object) MksQemuObject *object)
{ {
g_autoptr(MksDevice) self = NULL; g_autoptr(MksDevice) self = NULL;
g_return_val_if_fail (g_type_is_a (device_type, MKS_TYPE_DEVICE), NULL); g_return_val_if_fail (g_type_is_a (device_type, MKS_TYPE_DEVICE), NULL);
g_return_val_if_fail (device_type != MKS_TYPE_DEVICE, NULL); g_return_val_if_fail (device_type != MKS_TYPE_DEVICE, NULL);
g_return_val_if_fail (MKS_IS_SESSION (session), NULL);
g_return_val_if_fail (MKS_QEMU_IS_OBJECT (object), NULL); g_return_val_if_fail (MKS_QEMU_IS_OBJECT (object), NULL);
if (!(self = g_object_new (device_type, NULL))) if (!(self = g_object_new (device_type, NULL)))
return NULL; return NULL;
g_set_weak_pointer (&self->session, session);
self->object = g_object_ref (object); self->object = g_object_ref (object);
if (!MKS_DEVICE_GET_CLASS (self)->setup (self, object)) if (!MKS_DEVICE_GET_CLASS (self)->setup (self, object))

View File

@ -178,9 +178,9 @@ mks_screen_setup (MksDevice *device,
if (MKS_QEMU_IS_CONSOLE (iface)) if (MKS_QEMU_IS_CONSOLE (iface))
mks_screen_set_console (self, MKS_QEMU_CONSOLE (iface)); mks_screen_set_console (self, MKS_QEMU_CONSOLE (iface));
else if (MKS_QEMU_IS_KEYBOARD (iface)) else if (MKS_QEMU_IS_KEYBOARD (iface))
self->keyboard = _mks_device_new (MKS_TYPE_KEYBOARD, object); self->keyboard = _mks_device_new (MKS_TYPE_KEYBOARD, device->session, object);
else if (MKS_QEMU_IS_MOUSE (iface)) else if (MKS_QEMU_IS_MOUSE (iface))
self->mouse = _mks_device_new (MKS_TYPE_MOUSE, object); self->mouse = _mks_device_new (MKS_TYPE_MOUSE, device->session, object);
} }
return self->console != NULL && return self->console != NULL &&

View File

@ -225,7 +225,7 @@ mks_session_object_manager_object_added_cb (MksSession *self,
if (MKS_QEMU_IS_VM (iface)) if (MKS_QEMU_IS_VM (iface))
mks_session_set_vm (self, object, MKS_QEMU_VM (iface)); mks_session_set_vm (self, object, MKS_QEMU_VM (iface));
else if (MKS_QEMU_IS_CONSOLE (iface)) else if (MKS_QEMU_IS_CONSOLE (iface))
mks_session_add_device (self, _mks_device_new (MKS_TYPE_SCREEN, object)); mks_session_add_device (self, _mks_device_new (MKS_TYPE_SCREEN, self, object));
} }
} }