Newer
Older
/* WirePlumber
*
* Copyright © 2019 Collabora Ltd.
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
#include "object-manager.h"
#include "private.h"
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <pipewire/pipewire.h>
static void wp_object_manager_add_global (WpObjectManager * self, WpGlobal * global);
static void wp_object_manager_add_object (WpObjectManager * self, gpointer object);
static void wp_object_manager_rm_object (WpObjectManager * self, gpointer object);
/*
* WpRegistry
*
* The registry keeps track of registered objects on the wireplumber core.
* There are 3 kinds of registered objects:
*
* 1) PipeWire global objects, which live in another process.
*
* These objects are represented by a WpGlobal with the
* WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY flag set. They appear when
* the registry_global() event is fired and are removed by
* registry_global_remove(). These objects do not have an associated
* WpProxy, unless there is at least one WpObjectManager that is interested
* in them. In this case, a WpProxy is constructed and it is owned by the
* WpGlobal until the global is removed by the registry_global_remove() event.
*
* 2) PipeWire global objects, which were constructed by this process, either
* by calling into a remove factory (see wp_node_new_from_factory()) or
* by exporting a local object (WpImplNode etc...).
*
* These objects are also represented by a WpGlobal, which is however
* constructed before they appear on the registry. The associated WpProxy
* calls into wp_registry_new_global_for_proxy() at the time it receives
* the 'bound' event and creates a global that has the
* WP_GLOBAL_FLAG_OWNED_BY_PROXY flag enabled. As the flag name suggests,
* these globals are "owned" by the WpProxy and the WpGlobal has no ref
* on the WpProxy itself. This allows destroying the proxy in client code
* by dropping its last reference.
*
* Normally, these global objects also appear on the pipewire registry. When
* this happens, the WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY flag is also added
* and that keeps an additional reference on the global (both flags must
* be dropped before the WpGlobal is destroyed).
*
* 3) WirePlumber global objects (WpModule, WpFactory).
*
* These are local objects that have nothing to do with PipeWire. They do not
* have a global id and they are also not subclasses of WpProxy. The registry
* always owns a reference on them, so that they are kept alive for as long
* as the WpCore is alive.
*/
static void
wp_registry_notify_add_object (WpRegistry *self, gpointer object)
{
for (guint i = 0; i < self->object_managers->len; i++) {
WpObjectManager *om = g_ptr_array_index (self->object_managers, i);
wp_object_manager_add_object (om, object);
}
}
static void
wp_registry_notify_rm_object (WpRegistry *self, gpointer object)
{
for (guint i = 0; i < self->object_managers->len; i++) {
WpObjectManager *om = g_ptr_array_index (self->object_managers, i);
wp_object_manager_rm_object (om, object);
}
}
static void
object_manager_destroyed (gpointer data, GObject * om)
{
WpRegistry *self = data;
g_ptr_array_remove_fast (self->object_managers, om);
}
/* find the subclass of WpProxy that can handle
the given pipewire interface type of the given version */
static inline GType
find_proxy_instance_type (const char * type, guint32 version)
{
g_autofree GType *children;
guint n_children;
children = g_type_children (WP_TYPE_PROXY, &n_children);
for (gint i = 0; i < n_children; i++) {
WpProxyClass *klass = (WpProxyClass *) g_type_class_ref (children[i]);
if (g_strcmp0 (klass->pw_iface_type, type) == 0 &&
klass->pw_iface_version == version) {
g_type_class_unref (klass);
return children[i];
}
g_type_class_unref (klass);
}
return WP_TYPE_PROXY;
}
/* called by the registry when a global appears */
static void
registry_global (void *data, uint32_t id, uint32_t permissions,
const char *type, uint32_t version, const struct spa_dict *props)
{
WpRegistry *self = data;
WpGlobal *global = NULL;
if (id < self->globals->len)
global = g_ptr_array_index (self->globals, id);
g_debug ("registry global:%u perm:0x%x type:%s version:%u proxy:%p(%s)",
id, permissions, type, version, global ? global->proxy : NULL,
(global && global->proxy) ? G_OBJECT_TYPE_NAME (global->proxy) : NULL);
if (!global) {
/* global did not exist; object was just advertised on the registry */
global = wp_global_new (self, id, permissions,
find_proxy_instance_type (type, version),
props ? wp_properties_new_copy_dict (props) : wp_properties_new_empty (),
NULL, WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
/* we do not need this ref; the globals array also has one */
wp_global_unref (global);
} else {
/* global existed; proxy was created as a result of local action */
global->flags |= WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY;
global->permissions = permissions;
global->type = find_proxy_instance_type (type, version);
if (props)
wp_properties_update_from_dict (global->properties, props);
}
}
/* called by the registry when a global is removed */
static void
registry_global_remove (void *data, uint32_t id)
{
WpRegistry *self = data;
WpGlobal *global = NULL;
global = g_ptr_array_index (self->globals, id);
g_return_if_fail (global &&
global->flags & WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
g_debug ("registry global removed:%u type:%s", id, g_type_name (global->type));
wp_global_rm_flag (global, WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
}
static const struct pw_registry_events registry_events = {
PW_VERSION_REGISTRY_EVENTS,
.global = registry_global,
.global_remove = registry_global_remove,
};
void
wp_registry_init (WpRegistry *self)
{
self->globals = g_ptr_array_new ();
self->objects = g_ptr_array_new_with_free_func (g_object_unref);
self->object_managers = g_ptr_array_new ();
}
void
wp_registry_clear (WpRegistry *self)
{
wp_registry_detach (self);
g_clear_pointer (&self->globals, g_ptr_array_unref);
/* remove all the registered objects
this will normally also destroy the object managers, eventually, since
they are normally ref'ed by modules, which are registered objects */
{
g_autoptr (GPtrArray) objlist = g_steal_pointer (&self->objects);
while (objlist->len > 0) {
g_autoptr (GObject) object = g_ptr_array_steal_index_fast (objlist,
objlist->len - 1);
wp_registry_notify_rm_object (self, object);
}
}
/* in case there are any object managers left,
remove the weak ref on them and let them be... */
{
g_autoptr (GPtrArray) object_mgrs;
GObject *om;
object_mgrs = g_steal_pointer (&self->object_managers);
while (object_mgrs->len > 0) {
om = g_ptr_array_steal_index_fast (object_mgrs, object_mgrs->len - 1);
g_object_weak_unref (om, object_manager_destroyed, self);
}
}
}
void
wp_registry_attach (WpRegistry *self, struct pw_core *pw_core)
{
self->pw_registry = pw_core_get_registry (pw_core,
PW_VERSION_REGISTRY, 0);
pw_registry_add_listener (self->pw_registry, &self->listener,
®istry_events, self);
}
void
wp_registry_detach (WpRegistry *self)
{
if (self->pw_registry) {
spa_hook_remove (&self->listener);
pw_proxy_destroy ((struct pw_proxy *) self->pw_registry);
self->pw_registry = NULL;
}
/* remove pipewire globals */
GPtrArray *objlist = self->globals;
if (!objlist)
return;
while (objlist->len > 0) {
g_autoptr (WpGlobal) global = g_ptr_array_steal_index_fast (objlist,
objlist->len - 1);
if (!global)
continue;
if (global->proxy)
wp_registry_notify_rm_object (self, global->proxy);
/* remove the APPEARS_ON_REGISTRY flag to unref the proxy if it is owned
by the registry; set registry to NULL to avoid further interference */
global->registry = NULL;
wp_global_rm_flag (global, WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
/* the registry's ref on global is dropped here; it may still live if
there is a proxy that owns a ref on it, but global->registry is set
to NULL, so there is no further interference */
}
}
/*
* wp_core_find_object:
* @core: the core
* @func: (scope call): a function that takes the object being searched
* as the first argument and @data as the second. it should return TRUE if
* the object is found or FALSE otherwise
* @data: the second argument to @func
*
* Finds a registered object
*
* Returns: (transfer full) (type GObject *) (nullable): the registered object
* or NULL if not found
*/
gpointer
wp_core_find_object (WpCore * core, GEqualFunc func, gconstpointer data)
{
WpRegistry *reg;
GObject *object;
guint i;
g_return_val_if_fail (WP_IS_CORE (core), NULL);
reg = &core->registry;
/* prevent bad things when called from within wp_registry_clear() */
if (G_UNLIKELY (!reg->objects))
return NULL;
for (i = 0; i < reg->objects->len; i++) {
object = g_ptr_array_index (reg->objects, i);
if (func (object, data))
return g_object_ref (object);
}
return NULL;
}
/*
* wp_core_register_object:
* @core: the core
* @obj: (transfer full) (type GObject*): the object to register
*
* Registers @obj with the core, making it appear on #WpObjectManager
* instances as well. The core will also maintain a ref to that object
* until it is removed.
*/
void
wp_core_register_object (WpCore * core, gpointer obj)
{
WpRegistry *reg;
g_return_if_fail (WP_IS_CORE (core));
g_return_if_fail (G_IS_OBJECT (obj));
reg = &core->registry;
/* prevent bad things when called from within wp_registry_clear() */
if (G_UNLIKELY (!reg->objects)) {
g_object_unref (obj);
return;
}
g_ptr_array_add (reg->objects, obj);
/* notify object managers */
wp_registry_notify_add_object (reg, obj);
}
/*
* wp_core_remove_object:
* @core: the core
* @obj: (transfer none) (type GObject*): a pointer to the object to remove
*
* Detaches and unrefs the specified object from this core
*/
void
wp_core_remove_object (WpCore * core, gpointer obj)
{
WpRegistry *reg;
g_return_if_fail (WP_IS_CORE (core));
g_return_if_fail (G_IS_OBJECT (obj));
reg = &core->registry;
/* prevent bad things when called from within wp_registry_clear() */
if (G_UNLIKELY (!reg->objects))
return;
/* notify object managers */
wp_registry_notify_rm_object (reg, obj);
g_ptr_array_remove_fast (reg->objects, obj);
}
/**
* wp_core_install_object_manager: (method)
* @core: the core
* @om: (transfer none): a #WpObjectManager
*
* Installs the object manager on this core, activating its internal management
* engine. This will immediately emit signals about objects added on @om
* if objects that the @om is interested in were in existence already.
*/
void
wp_core_install_object_manager (WpCore * core, WpObjectManager * om)
{
WpRegistry *reg;
guint i;
g_return_if_fail (WP_IS_CORE (core));
g_return_if_fail (WP_IS_OBJECT_MANAGER (om));
reg = &core->registry;
g_object_weak_ref (G_OBJECT (om), object_manager_destroyed, reg);
g_ptr_array_add (reg->object_managers, om);
g_object_set (om, "core", core, NULL);
/* add pre-existing objects to the object manager,
in case it's interested in them */
for (i = 0; i < reg->globals->len; i++) {
WpGlobal *g = g_ptr_array_index (reg->globals, i);
/* check if null because the globals array can have gaps */
if (g)
wp_object_manager_add_global (om, g);
}
for (i = 0; i < reg->objects->len; i++) {
GObject *o = g_ptr_array_index (reg->objects, i);
wp_object_manager_add_object (om, o);
}
}
/* WpGlobal */
G_DEFINE_BOXED_TYPE (WpGlobal, wp_global, wp_global_ref, wp_global_unref)
WpGlobal *
wp_global_new (WpRegistry * reg, guint32 id, guint32 permissions,
GType type, WpProperties * properties, WpProxy * proxy, guint32 flags)
{
g_return_val_if_fail (flags != 0, NULL);
WpGlobal *global = g_rc_box_new0 (WpGlobal);
global->flags = flags;
global->id = id;
global->type = type;
global->permissions = permissions;
global->properties = properties;
global->proxy = proxy;
global->registry = reg;
if (reg->globals->len <= id)
g_ptr_array_set_size (reg->globals, id + 1);
g_ptr_array_index (reg->globals, id) = wp_global_ref (global);
/* notify object managers */
for (guint i = 0; i < reg->object_managers->len; i++) {
WpObjectManager *om = g_ptr_array_index (reg->object_managers, i);
wp_object_manager_add_global (om, global);
}
return global;
}
void
wp_global_rm_flag (WpGlobal *global, guint rm_flag)
{
WpRegistry *reg = global->registry;
/* no flag to remove */
if (!(global->flags & rm_flag))
return;
/* global was owned by the proxy; by removing the flag, we clear out
also the proxy pointer, which is presumably no longer valid and we
notify all listeners that the proxy is gone */
if (rm_flag == WP_GLOBAL_FLAG_OWNED_BY_PROXY) {
global->flags &= ~WP_GLOBAL_FLAG_OWNED_BY_PROXY;
if (reg)
wp_registry_notify_rm_object (reg, global->proxy);
global->proxy = NULL;
}
/* registry removed the global */
else if (rm_flag == WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY) {
global->flags &= ~WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY;
/* if there is a proxy and it's not owning the global, destroy it */
if (global->flags == 0 && global->proxy) {
if (reg)
wp_registry_notify_rm_object (reg, global->proxy);
g_clear_object (&global->proxy);
}
}
/* drop the registry's ref on global when it has no flags anymore */
if (global->flags == 0 && reg) {
g_clear_pointer (&g_ptr_array_index (reg->globals, global->id), wp_global_unref);
}
}
struct pw_proxy *
wp_global_bind (WpGlobal * global)
{
g_return_val_if_fail (global->proxy, NULL);
g_return_val_if_fail (global->registry, NULL);
WpProxyClass *klass = WP_PROXY_GET_CLASS (global->proxy);
return pw_registry_bind (global->registry->pw_registry, global->id,
klass->pw_iface_type, klass->pw_iface_version, 0);
}
/* WpObjectManager */
struct interest
{
GType g_type;
WpProxyFeatures wanted_features;
GVariant *constraints; // aa{sv}
};
struct _WpObjectManager
{
GObject parent;
GWeakRef core;
/* array of struct interest;
pw_array has a better API for our use case than GArray */
struct pw_array interests;
/* objects that we are interested in, without a ref */
GPtrArray *objects;
gboolean pending_objchanged;
};
enum {
PROP_0,
PROP_CORE,
};
enum {
SIGNAL_OBJECT_ADDED,
SIGNAL_OBJECT_REMOVED,
SIGNAL_OBJECTS_CHANGED,
LAST_SIGNAL,
};
static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (WpObjectManager, wp_object_manager, G_TYPE_OBJECT)
static void
wp_object_manager_init (WpObjectManager * self)
{
g_weak_ref_init (&self->core, NULL);
pw_array_init (&self->interests, sizeof (struct interest));
self->objects = g_ptr_array_new ();
self->pending_objchanged = FALSE;
}
static void
wp_object_manager_finalize (GObject * object)
{
WpObjectManager *self = WP_OBJECT_MANAGER (object);
struct interest *i;
g_clear_pointer (&self->objects, g_ptr_array_unref);
pw_array_for_each (i, &self->interests) {
g_clear_pointer (&i->constraints, g_variant_unref);
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
pw_array_clear (&self->interests);
g_weak_ref_clear (&self->core);
G_OBJECT_CLASS (wp_object_manager_parent_class)->finalize (object);
}
static void
wp_object_manager_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
WpObjectManager *self = WP_OBJECT_MANAGER (object);
switch (property_id) {
case PROP_CORE:
g_weak_ref_set (&self->core, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
wp_object_manager_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
WpObjectManager *self = WP_OBJECT_MANAGER (object);
switch (property_id) {
case PROP_CORE:
g_value_take_object (value, g_weak_ref_get (&self->core));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
wp_object_manager_class_init (WpObjectManagerClass * klass)
{
GObjectClass *object_class = (GObjectClass *) klass;
object_class->finalize = wp_object_manager_finalize;
object_class->get_property = wp_object_manager_get_property;
object_class->set_property = wp_object_manager_set_property;
/* Install the properties */
g_object_class_install_property (object_class, PROP_CORE,
g_param_spec_object ("core", "core", "The WpCore", WP_TYPE_CORE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
signals[SIGNAL_OBJECT_ADDED] = g_signal_new (
"object-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_OBJECT);
signals[SIGNAL_OBJECT_REMOVED] = g_signal_new (
"object-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_OBJECT);
signals[SIGNAL_OBJECTS_CHANGED] = g_signal_new (
"objects-changed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
}
WpObjectManager *
wp_object_manager_new (void)
{
return g_object_new (WP_TYPE_OBJECT_MANAGER, NULL);
}
void
wp_object_manager_add_interest (WpObjectManager *self,
GType gtype, GVariant * constraints,
{
struct interest *i;
g_return_if_fail (WP_IS_OBJECT_MANAGER (self));
g_return_if_fail (constraints == NULL ||
g_variant_is_of_type (constraints, G_VARIANT_TYPE ("aa{sv}")));
/* grow the array by 1 struct interest and fill it in */
i = pw_array_add (&self->interests, sizeof (struct interest));
i->g_type = gtype;
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
i->wanted_features = wanted_features;
i->constraints = constraints ? g_variant_ref_sink (constraints) : NULL;
}
/**
* wp_object_manager_get_objects:
* @self: the object manager
* @type_filter: a #GType filter to get only the objects that are of this type,
* or 0 to return all the objects
*
* Returns: (transfer full) (element-type GObject*): all the objects managed
* by this #WpObjectManager that match the @type_filter
*/
GPtrArray *
wp_object_manager_get_objects (WpObjectManager *self, GType type_filter)
{
GPtrArray *result = g_ptr_array_new_with_free_func (g_object_unref);
guint i;
for (i = 0; i < self->objects->len; i++) {
gpointer obj = g_ptr_array_index (self->objects, i);
if (type_filter == 0 || g_type_is_a (G_OBJECT_TYPE (obj), type_filter)) {
g_ptr_array_add (result, g_object_ref (obj));
}
}
return result;
}
static gboolean
check_constraints (GVariant *constraints,
WpProperties *global_props,
GObject *object)
{
GVariantIter iter;
GVariant *c;
WpObjectManagerConstraintType ctype;
g_autoptr (WpProperties) props = NULL;
const gchar *prop_name, *prop_value;
/* pipewire properties are contained in a GObj property called "properties" */
if (object &&
g_object_class_find_property (G_OBJECT_GET_CLASS (object), "properties"))
g_object_get (object, "properties", &props, NULL);
g_variant_iter_init (&iter, constraints);
while (g_variant_iter_next (&iter, "@a{sv}", &c)) {
GVariantDict dict = G_VARIANT_DICT_INIT (c);
if (!g_variant_dict_lookup (&dict, "type", "i", &ctype)) {
g_warning ("Invalid object manager constraint without a type");
goto error;
}
switch (ctype) {
case WP_OBJECT_MANAGER_CONSTRAINT_PW_GLOBAL_PROPERTY:
if (!global_props)
goto next;
if (!g_variant_dict_lookup (&dict, "name", "&s", &prop_name)) {
g_warning ("property constraint is without a property name");
goto error;
}
if (!g_variant_dict_lookup (&dict, "value", "&s", &prop_value)) {
g_warning ("property constraint is without a property value");
goto error;
}
if (!g_strcmp0 (wp_properties_get (global_props, prop_name), prop_value))
goto match;
break;
case WP_OBJECT_MANAGER_CONSTRAINT_PW_PROPERTY:
if (!props)
goto next;
if (!g_variant_dict_lookup (&dict, "name", "&s", &prop_name)) {
g_warning ("property constraint is without a property name");
goto error;
}
if (!g_variant_dict_lookup (&dict, "value", "&s", &prop_value)) {
g_warning ("property constraint is without a property value");
goto error;
}
if (!g_strcmp0 (wp_properties_get (props, prop_name), prop_value))
goto match;
break;
case WP_OBJECT_MANAGER_CONSTRAINT_G_PROPERTY:
if (!object)
goto next;
if (!g_variant_dict_lookup (&dict, "name", "&s", &prop_name)) {
g_warning ("property constraint is without a property name");
goto error;
}
if (!g_variant_dict_lookup (&dict, "value", "&s", &prop_value)) {
g_warning ("property constraint is without a property value");
goto error;
}
if (!g_object_class_find_property (G_OBJECT_GET_CLASS (object), prop_name))
goto next;
if (({
g_auto (GValue) value = G_VALUE_INIT;
g_auto (GValue) str_value = G_VALUE_INIT;
g_object_get_property (object, prop_name, &value);
g_value_init (&str_value, G_TYPE_STRING);
g_value_transform (&value, &str_value) &&
!g_strcmp0 (g_value_get_string (&str_value), prop_value);
}))
goto match;
break;
default:
g_warning ("Unknown constraint type '%d'", ctype);
goto error;
}
next:
{
g_variant_dict_clear (&dict);
g_clear_pointer (&c, g_variant_unref);
continue;
}
match:
{
g_variant_dict_clear (&dict);
g_clear_pointer (&c, g_variant_unref);
return TRUE;
}
error:
{
g_autofree gchar *dbgstr = g_variant_print (c, TRUE);
g_warning ("offending constraint was: %s", dbgstr);
goto next;
}
}
return FALSE;
}
static gboolean
wp_object_manager_is_interested_in_object (WpObjectManager * self,
GObject * object)
{
struct interest *i;
pw_array_for_each (i, &self->interests) {
if (g_type_is_a (G_OBJECT_TYPE (object), i->g_type)
&& (!i->constraints ||
check_constraints (i->constraints, NULL, object)))
{
return TRUE;
}
}
return FALSE;
}
static gboolean
wp_object_manager_is_interested_in_global (WpObjectManager * self,
WpGlobal * global, WpProxyFeatures * wanted_features)
{
struct interest *i;
pw_array_for_each (i, &self->interests) {
if (g_type_is_a (global->type, i->g_type)
&& (!i->constraints ||
check_constraints (i->constraints, global->properties, NULL)))
{
*wanted_features = i->wanted_features;
return TRUE;
}
}
return FALSE;
}
static void
sync_emit_objects_changed (WpCore *core, GAsyncResult *res, gpointer data)
g_autoptr (WpObjectManager) self = WP_OBJECT_MANAGER (data);
g_signal_emit (self, signals[SIGNAL_OBJECTS_CHANGED], 0);
self->pending_objchanged = FALSE;
}
static inline void
schedule_emit_objects_changed (WpObjectManager * self)
{
if (self->pending_objchanged)
return;
g_autoptr (WpCore) core = g_weak_ref_get (&self->core);
if (core) {
wp_core_sync (core, NULL, (GAsyncReadyCallback)sync_emit_objects_changed,
g_object_ref (self));
self->pending_objchanged = TRUE;
}
static void
on_proxy_ready (GObject * proxy, GAsyncResult * res, gpointer data)
{
g_autoptr (WpObjectManager) self = WP_OBJECT_MANAGER (data);
g_ptr_array_add (self->objects, proxy);
g_signal_emit (self, signals[SIGNAL_OBJECT_ADDED], 0, proxy);
schedule_emit_objects_changed (self);
}
static void
wp_object_manager_add_global (WpObjectManager * self, WpGlobal * global)
{
WpProxyFeatures features = 0;
if (wp_object_manager_is_interested_in_global (self, global, &features)) {
g_autoptr (WpCore) core = g_weak_ref_get (&self->core);
if (!global->proxy)
global->proxy = g_object_new (global->type,
"core", core,
"global", global,
NULL);
wp_proxy_augment (global->proxy, features, NULL, on_proxy_ready,
g_object_ref (self));
}
}
static void
wp_object_manager_add_object (WpObjectManager * self, gpointer object)
{
if (wp_object_manager_is_interested_in_object (self, object)) {
g_ptr_array_add (self->objects, object);
g_signal_emit (self, signals[SIGNAL_OBJECT_ADDED], 0, object);
schedule_emit_objects_changed (self);
}
}
static void
wp_object_manager_rm_object (WpObjectManager * self, gpointer object)
{
guint index;
if (g_ptr_array_find (self->objects, object, &index)) {
g_signal_emit (self, signals[SIGNAL_OBJECT_REMOVED], 0, object);
g_ptr_array_remove_index_fast (self->objects, index);
schedule_emit_objects_changed (self);
}
}