Skip to content

Commit

Permalink
refactor(core): coalesce some common methods in components
Browse files Browse the repository at this point in the history
  • Loading branch information
andyholmes committed Nov 23, 2024
1 parent ecfe555 commit e9d5b4d
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 380 deletions.
48 changes: 0 additions & 48 deletions src/libvalent/contacts/valent-contacts.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,51 +207,3 @@ valent_contacts_get_default (void)
return default_contacts;
}

/**
* valent_contacts_export_adapter:
* @contacts: a `ValentContacts`
* @object: a `ValentContactsAdapter`
*
* Export @object on all adapters that support it.
*
* Since: 1.0
*/
void
valent_contacts_export_adapter (ValentContacts *contacts,
ValentContactsAdapter *object)
{
VALENT_ENTRY;

g_return_if_fail (VALENT_IS_CONTACTS (contacts));
g_return_if_fail (VALENT_IS_CONTACTS_ADAPTER (object));

valent_contacts_bind_extension (VALENT_COMPONENT (contacts),
G_OBJECT (object));

VALENT_EXIT;
}

/**
* valent_contacts_unexport_adapter:
* @contacts: a `ValentContacts`
* @object: a `ValentContactsAdapter`
*
* Unexport @object from all adapters that support it.
*
* Since: 1.0
*/
void
valent_contacts_unexport_adapter (ValentContacts *contacts,
ValentContactsAdapter *object)
{
VALENT_ENTRY;

g_return_if_fail (VALENT_IS_CONTACTS (contacts));
g_return_if_fail (VALENT_IS_CONTACTS_ADAPTER (object));

valent_contacts_unbind_extension (VALENT_COMPONENT (contacts),
G_OBJECT (object));

VALENT_EXIT;
}

6 changes: 0 additions & 6 deletions src/libvalent/contacts/valent-contacts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ G_DECLARE_FINAL_TYPE (ValentContacts, valent_contacts, VALENT, CONTACTS, ValentC

VALENT_AVAILABLE_IN_1_0
ValentContacts * valent_contacts_get_default (void);
VALENT_AVAILABLE_IN_1_0
void valent_contacts_export_adapter (ValentContacts *contacts,
ValentContactsAdapter *object);
VALENT_AVAILABLE_IN_1_0
void valent_contacts_unexport_adapter (ValentContacts *contacts,
ValentContactsAdapter *object);

G_END_DECLS

48 changes: 48 additions & 0 deletions src/libvalent/core/valent-component.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,3 +656,51 @@ valent_component_set_primary_adapter (ValentComponent *component,
}
}

/**
* valent_component_export_adapter:
* @component: a `ValentComponent`
* @extension: a `ValentExtension`
*
* Export @extension on the component and all adapters that support it.
*
* Since: 1.0
*/
void
valent_component_export_adapter (ValentComponent *component,
ValentExtension *extension)
{
VALENT_ENTRY;

g_return_if_fail (VALENT_IS_COMPONENT (component));
g_return_if_fail (VALENT_IS_EXTENSION (extension));

VALENT_COMPONENT_GET_CLASS (component)->bind_extension (component,
G_OBJECT (extension));

VALENT_EXIT;
}

/**
* valent_component_unexport_adapter:
* @component: a `ValentComponent`
* @extension: a `ValentExtension`
*
* Unexport @extension from the component and all other adapters.
*
* Since: 1.0
*/
void
valent_component_unexport_adapter (ValentComponent *component,
ValentExtension *extension)
{
VALENT_ENTRY;

g_return_if_fail (VALENT_IS_COMPONENT (component));
g_return_if_fail (VALENT_IS_EXTENSION (extension));

VALENT_COMPONENT_GET_CLASS (component)->unbind_extension (component,
G_OBJECT (extension));

VALENT_EXIT;
}

10 changes: 10 additions & 0 deletions src/libvalent/core/valent-component.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ struct _ValentComponentClass
GObject *extension);
void (*bind_preferred) (ValentComponent *component,
GObject *extension);
void (*export_adapter) (ValentComponent *component,
ValentExtension *extension);
void (*unexport_adapter) (ValentComponent *component,
ValentExtension *extension);

/*< private >*/
gpointer padding[8];
Expand All @@ -40,6 +44,12 @@ ValentExtension * valent_component_get_primary_adapter (ValentComponent *compone
VALENT_AVAILABLE_IN_1_0
void valent_component_set_primary_adapter (ValentComponent *component,
ValentExtension *extension);
VALENT_AVAILABLE_IN_1_0
void valent_component_export_adapter (ValentComponent *component,
ValentExtension *extension);
VALENT_AVAILABLE_IN_1_0
void valent_component_unexport_adapter (ValentComponent *component,
ValentExtension *extension);

G_END_DECLS

191 changes: 60 additions & 131 deletions src/libvalent/input/valent-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,72 +32,20 @@ struct _ValentInput
ValentComponent parent_instance;

ValentInputAdapter *default_adapter;
GPtrArray *adapters; /* complete list of adapters */
GListModel *exports; /* adapters marked for export */
GPtrArray *items; /* adapters exposed by GListModel */
GPtrArray *items;
};

static void g_list_model_iface_init (GListModelInterface *iface);

G_DEFINE_FINAL_TYPE_WITH_CODE (ValentInput, valent_input, VALENT_TYPE_COMPONENT,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, g_list_model_iface_init))

static void valent_input_unbind_extension (ValentComponent *component,
GObject *extension);

static ValentInput *default_input = NULL;


static void
on_items_changed (GListModel *list,
unsigned int position,
unsigned int removed,
unsigned int added,
ValentInput *self)
{
unsigned int real_position = 0;

VALENT_ENTRY;

g_assert (G_IS_LIST_MODEL (list));
g_assert (VALENT_IS_INPUT (self));

/* Translate the adapter position */
for (unsigned int i = 0; i < self->adapters->len; i++)
{
GListModel *adapter = g_ptr_array_index (self->adapters, i);

if (adapter == list)
break;

real_position += g_list_model_get_n_items (adapter);
}

real_position += position;

/* Propagate the changes */
for (unsigned int i = 0; i < removed; i++)
{
g_autoptr (ValentInputAdapter) adapter = NULL;

adapter = g_ptr_array_steal_index (self->items, real_position);

VALENT_NOTE ("removed %s", G_OBJECT_TYPE_NAME (adapter));
}

for (unsigned int i = 0; i < added; i++)
{
ValentInputAdapter *adapter = NULL;

adapter = g_list_model_get_item (list, position + i);
g_ptr_array_insert (self->items, real_position + i, adapter);

VALENT_NOTE ("added %s", G_OBJECT_TYPE_NAME (adapter));
}

g_list_model_items_changed (G_LIST_MODEL (self), real_position, removed, added);

VALENT_EXIT;
}

/*
* GListModel
*/
Expand Down Expand Up @@ -159,17 +107,65 @@ valent_input_bind_preferred (ValentComponent *component,
VALENT_EXIT;
}

/*
* ValentObject
*/
static void
valent_input_destroy (ValentObject *object)
valent_input_bind_extension (ValentComponent *component,
GObject *extension)
{
ValentInput *self = VALENT_INPUT (object);
ValentInput *self = VALENT_INPUT (component);
unsigned int position = 0;

VALENT_ENTRY;

g_assert (VALENT_IS_INPUT (self));
g_assert (VALENT_IS_INPUT_ADAPTER (extension));

if (g_ptr_array_find (self->items, extension, &position))
{
g_warning ("Adapter \"%s\" already exported in \"%s\"",
G_OBJECT_TYPE_NAME (extension),
G_OBJECT_TYPE_NAME (component));
return;
}

g_signal_connect_object (extension,
"destroy",
G_CALLBACK (valent_input_unbind_extension),
self,
G_CONNECT_SWAPPED);

position = self->items->len;
g_ptr_array_add (self->items, g_object_ref (extension));
g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);

VALENT_EXIT;
}

static void
valent_input_unbind_extension (ValentComponent *component,
GObject *extension)
{
ValentInput *self = VALENT_INPUT (component);
g_autoptr (ValentExtension) item = NULL;
unsigned int position = 0;

VALENT_ENTRY;

g_list_store_remove_all (G_LIST_STORE (self->exports));
g_assert (VALENT_IS_INPUT (self));
g_assert (VALENT_IS_INPUT_ADAPTER (extension));

VALENT_OBJECT_CLASS (valent_input_parent_class)->destroy (object);
if (!g_ptr_array_find (self->items, extension, &position))
{
g_warning ("Adapter \"%s\" not found in \"%s\"",
G_OBJECT_TYPE_NAME (extension),
G_OBJECT_TYPE_NAME (component));
return;
}

g_signal_handlers_disconnect_by_func (extension, valent_input_unbind_extension, self);
item = g_ptr_array_steal_index (self->items, position);
g_list_model_items_changed (G_LIST_MODEL (self), position, 1, 0);

VALENT_EXIT;
}

/*
Expand All @@ -180,8 +176,6 @@ valent_input_finalize (GObject *object)
{
ValentInput *self = VALENT_INPUT (object);

g_clear_object (&self->exports);
g_clear_pointer (&self->adapters, g_ptr_array_unref);
g_clear_pointer (&self->items, g_ptr_array_unref);

G_OBJECT_CLASS (valent_input_parent_class)->finalize (object);
Expand All @@ -191,28 +185,19 @@ static void
valent_input_class_init (ValentInputClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
ValentComponentClass *component_class = VALENT_COMPONENT_CLASS (klass);

object_class->finalize = valent_input_finalize;

vobject_class->destroy = valent_input_destroy;

component_class->bind_preferred = valent_input_bind_preferred;
component_class->bind_extension = valent_input_bind_extension;
component_class->unbind_extension = valent_input_unbind_extension;
}

static void
valent_input_init (ValentInput *self)
{
self->adapters = g_ptr_array_new_with_free_func (g_object_unref);
self->items = g_ptr_array_new_with_free_func (g_object_unref);

self->exports = G_LIST_MODEL (g_list_store_new (VALENT_TYPE_INPUT_ADAPTER));
g_signal_connect_object (self->exports,
"items-changed",
G_CALLBACK (on_items_changed),
self, 0);
g_ptr_array_add (self->adapters, g_object_ref (self->exports));
}

/**
Expand Down Expand Up @@ -241,62 +226,6 @@ valent_input_get_default (void)
return default_input;
}

/**
* valent_input_export_adapter:
* @input: a `ValentInput`
* @adapter: a `ValentInputAdapter`
*
* Export @adapter on all adapters that support it.
*
* Since: 1.0
*/
void
valent_input_export_adapter (ValentInput *input,
ValentInputAdapter *adapter)
{
VALENT_ENTRY;

g_return_if_fail (VALENT_IS_INPUT (input));
g_return_if_fail (VALENT_IS_INPUT_ADAPTER (adapter));

g_list_store_append (G_LIST_STORE (input->exports), adapter);

VALENT_EXIT;
}

/**
* valent_input_unexport_adapter:
* @input: a `ValentInput`
* @adapter: a `ValentInputAdapter`
*
* Unexport @adapter from all adapters that support it.
*
* Since: 1.0
*/
void
valent_input_unexport_adapter (ValentInput *input,
ValentInputAdapter *adapter)
{
unsigned int position = 0;

VALENT_ENTRY;

g_return_if_fail (VALENT_IS_INPUT (input));
g_return_if_fail (VALENT_IS_INPUT_ADAPTER (adapter));

if (!g_list_store_find (G_LIST_STORE (input->exports), adapter, &position))
{
g_critical ("%s(): unknown adapter %s",
G_STRFUNC,
G_OBJECT_TYPE_NAME (adapter));
return;
}

g_list_store_remove (G_LIST_STORE (input->exports), position);

VALENT_EXIT;
}

/**
* valent_input_keyboard_keysym:
* @input: a `ValentInput`
Expand Down
Loading

0 comments on commit e9d5b4d

Please sign in to comment.