Debian Codegraph @

lib/wp/object-interest.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "object-interest.h"
10 #include "global-proxy.h"
11 #include "session-item.h"
12 #include "proxy-interfaces.h"
13 #include "event-dispatcher.h"
14 #include "log.h"
15 #include "error.h"
16
17 #include <pipewire/pipewire.h>
18
19 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-object-interest")
20
21 /*! \defgroup wpobjectinterest WpObjectInterest */
22 /*!
23 * \struct WpObjectInterest
24 *
25 * An object interest is a helper that is used in WpObjectManager to
26 * declare interest in certain kinds of objects.
27 *
28 * An interest is defined by a GType and a set of constraints on the object's
29 * properties. An object "matches" the interest if it is of the specified
30 * GType (either the same type or a descendant of it) and all the constraints
31 * are satisfied.
32 */
33
34 struct constraint
35 {
36 WpConstraintType type;
37 WpConstraintVerb verb;
38 gchar subject_type; /* a basic GVariantType as a single char */
39 gchar *subject;
40 GVariant *value;
41 };
42
43 struct _WpObjectInterest
44 {
45 grefcount ref;
46 gboolean valid;
47 GType gtype;
48 struct pw_array constraints;
49 };
50
51 G_DEFINE_BOXED_TYPE (WpObjectInterest, wp_object_interest,
52 wp_object_interest_ref, wp_object_interest_unref)
53
54 /*!
55 * \brief Creates a new interest that declares interest in objects of the specified
56 * \a gtype, with the constraints specified in the variable arguments.
57 *
58 * The variable arguments should be a list of constraints terminated with NULL,
59 * where each constraint consists of the following arguments:
60 * - a `WpConstraintType`: the constraint type
61 * - a `const gchar *`: the subject name
62 * - a `const gchar *`: the format string
63 * - 0 or more arguments according to the format string
64 *
65 * The format string is interpreted as follows:
66 * - the first character is the constraint verb:
67 * - `=`: WP_CONSTRAINT_VERB_EQUALS
68 * - `!`: WP_CONSTRAINT_VERB_NOT_EQUALS
69 * - `c`: WP_CONSTRAINT_VERB_IN_LIST
70 * - `~`: WP_CONSTRAINT_VERB_IN_RANGE
71 * - `#`: WP_CONSTRAINT_VERB_MATCHES
72 * - `+`: WP_CONSTRAINT_VERB_IS_PRESENT
73 * - `-`: WP_CONSTRAINT_VERB_IS_ABSENT
74 * - the rest of the characters are interpreted as a GVariant format string,
75 * as it would be used in g_variant_new()
76 *
77 * The rest of this function's arguments up to the start of the next constraint
78 * depend on the GVariant format part of the format string and are used to
79 * construct a GVariant for the constraint's value argument.
80 *
81 * For further reading on the constraint's arguments, see
82 * wp_object_interest_add_constraint()
83 *
84 * For example, this interest matches objects that are descendants of WpProxy
85 * with a "bound-id" between 0 and 100 (inclusive), with a pipewire property
86 * called "format.dsp" that contains the string "audio" somewhere in the value
87 * and with a pipewire property "port.name" being present (with any value):
88 * \code
89 * interest = wp_object_interest_new (WP_TYPE_PROXY,
90 * WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "~(uu)", 0, 100,
91 * WP_CONSTRAINT_TYPE_PW_PROPERTY, "format.dsp", "#s", "*audio*",
92 * WP_CONSTRAINT_TYPE_PW_PROPERTY, "port.name", "+",
93 * NULL);
94 * \endcode
95 *
96 * \ingroup wpobjectinterest
97 * \param gtype the type of the object to declare interest in
98 * \param ... a set of constraints, terminated with NULL
99 * \returns (transfer full): the new object interest
100 */
101 WpObjectInterest *
102 wp_object_interest_new (GType gtype, ...)
103 {
104 WpObjectInterest *self;
105 va_list args;
106 va_start (args, gtype);
107 self = wp_object_interest_new_valist (gtype, &args);
108 va_end (args);
109 return self;
110 }
111
112 /*!
113 * \brief va_list version of wp_object_interest_new()
114 *
115 * \ingroup wpobjectinterest
116 * \param gtype the type of the object to declare interest in
117 * \param args pointer to va_list containing the constraints
118 * \returns (transfer full): the new object interest
119 */
120 WpObjectInterest *
121 wp_object_interest_new_valist (GType gtype, va_list *args)
122 {
123 WpObjectInterest *self = wp_object_interest_new_type (gtype);
124 WpConstraintType type;
125
126 g_return_val_if_fail (self != NULL, NULL);
127
128 for (type = va_arg (*args, WpConstraintType);
129 type != WP_CONSTRAINT_TYPE_NONE;
130 type = va_arg (*args, WpConstraintType))
131 {
132 const gchar *subject, *format;
133 WpConstraintVerb verb = 0;
134 GVariant *value = NULL;
135
136 subject = va_arg (*args, const gchar *);
137 g_return_val_if_fail (subject != NULL, NULL);
138
139 format = va_arg (*args, const gchar *);
140 g_return_val_if_fail (format != NULL, NULL);
141
142 verb = format[0];
143 if (verb != 0 && format[1] != '\0')
144 value = g_variant_new_va (format + 1, NULL, args);
145
146 wp_object_interest_add_constraint (self, type, subject, verb, value);
147 }
148 return self;
149 }
150
151 /*!
152 * \brief Creates a new interest that declares interest in objects of the
153 * specified \a gtype, without any property constraints.
154 *
155 * To add property constraints, you can call wp_object_interest_add_constraint()
156 * afterwards.
157 *
158 * \ingroup wpobjectinterest
159 * \param gtype the type of the object to declare interest in
160 * \returns (transfer full): the new object interest
161 */
162 WpObjectInterest *
163 wp_object_interest_new_type (GType gtype)
164 {
165 WpObjectInterest *self = g_slice_new0 (WpObjectInterest);
166 g_return_val_if_fail (self != NULL, NULL);
167 g_ref_count_init (&self->ref);
168 self->gtype = gtype;
169 pw_array_init (&self->constraints, sizeof (struct constraint));
170 return self;
171 }
172
173 /*!
174 * \brief Adds a constraint to this interest. Constraints consist of a \a type,
175 * a \a subject, a \a verb and, depending on the \a verb, a \a value.
176 *
177 * Constraints are almost like a spoken language sentence that declare a
178 * condition that must be true in order to consider that an object can match
179 * this interest. For instance, a constraint can be "pipewire property
180 * 'object.id' equals 10". This would be translated to:
181 * \code
182 * wp_object_interest_add_constraint (i,
183 * WP_CONSTRAINT_TYPE_PW_PROPERTY, "object.id",
184 * WP_CONSTRAINT_VERB_EQUALS, g_variant_new_int (10));
185 * \endcode
186 *
187 * Some verbs require a \a value and some others do not. For those that do,
188 * the \a value must be of a specific type:
189 * - WP_CONSTRAINT_VERB_EQUALS: \a value can be a string, a (u)int32,
190 * a (u)int64, a double or a boolean. The \a subject value must equal this
191 * value for the constraint to be satisfied
192 * - WP_CONSTRAINT_VERB_IN_LIST: \a value must be a tuple that contains any
193 * number of items of the same type; the items can be string, (u)int32,
194 * (u)int64 or double. These items make a list that the \a subject's value
195 * will be checked against. If any of the items equals the \a subject value,
196 * the constraint is satisfied
197 * - WP_CONSTRAINT_VERB_IN_RANGE: \a value must be a tuple that contains exactly
198 * 2 numbers of the same type ((u)int32, (u)int64 or double), meaning the
199 * minimum and maximum (inclusive) of the range. If the \a subject value is a
200 * number within this range, the constraint is satisfied
201 * - WP_CONSTRAINT_VERB_MATCHES: \a value must be a string that defines a
202 * pattern usable with GPatternSpec If the \a subject value matches this
203 * pattern, the constraint is satisfied
204 *
205 * In case the type of the \a subject value is not the same type as the one
206 * requested by the type of the \a value, the \a subject value is converted.
207 * For GObject properties, this conversion is done using g_value_transform(),
208 * so limitations of this function apply. In the case of PipeWire properties,
209 * which are *always* strings, conversion is done as follows:
210 * - to boolean: `"true"` or `"1"` means TRUE, `"false"` or `"0"` means FALSE
211 * - to int / uint / int64 / uint64: One of the `strtol()` family of functions
212 * is used to convert, using base 10
213 * - to double: `strtod()` is used
214 *
215 * This method does not fail if invalid arguments are given. However,
216 * wp_object_interest_validate() should be called after adding all the
217 * constraints on an interest in order to catch errors.
218 *
219 * \ingroup wpobjectinterest
220 * \param self the object interest
221 * \param type the constraint type
222 * \param subject the subject that the constraint applies to
223 * \param verb the operation that is performed to check the constraint
224 * \param value (transfer floating)(nullable): the value to check for
225 */
226 void
227 wp_object_interest_add_constraint (WpObjectInterest * self,
228 WpConstraintType type, const gchar * subject,
229 WpConstraintVerb verb, GVariant * value)
230 {
231 struct constraint *c;
232
233 g_return_if_fail (self != NULL);
234
235 c = pw_array_add (&self->constraints, sizeof (struct constraint));
236 g_return_if_fail (c != NULL);
237 c->type = type;
238 c->verb = verb;
239 /* subject_type is filled in by _validate() */
240 c->subject_type = '\0';
241 c->subject = g_strdup (subject);
242 c->value = value ? g_variant_ref_sink (value) : NULL;
243
244 /* mark as invalid to force validation */
245 self->valid = FALSE;
246 }
247
248 /*!
249 * \brief Increases the reference count of an object interest
250 * \ingroup wpobjectinterest
251 * \param self the object interest to ref
252 * \returns (transfer full): \a self with an additional reference count on it
253 */
254 WpObjectInterest *
255 wp_object_interest_ref (WpObjectInterest *self)
256 {
257 g_ref_count_inc (&self->ref);
258 return self;
259 }
260
261 static void
262 wp_object_interest_free (WpObjectInterest * self)
263 {
264 struct constraint *c;
265
266 g_return_if_fail (self != NULL);
267
268 pw_array_for_each (c, &self->constraints) {
269 g_clear_pointer (&c->subject, g_free);
270 g_clear_pointer (&c->value, g_variant_unref);
271 }
272 pw_array_clear (&self->constraints);
273 g_slice_free (WpObjectInterest, self);
274 }
275
276 /*!
277 * \brief Decreases the reference count on \a self and frees it when the ref
278 * count reaches zero.
279 *
280 * \ingroup wpobjectinterest
281 * \param self (transfer full): the object interest to unref
282 */
283 void
284 wp_object_interest_unref (WpObjectInterest * self)
285 {
286 if (g_ref_count_dec (&self->ref))
287 wp_object_interest_free (self);
288 }
289
290 /*!
291 * \brief Validates the interest, ensuring that the interest GType
292 * is a valid object and that all the constraints have been expressed properly.
293 *
294 * \remark This is called internally when \a self is first used to find a match,
295 * so it is not necessary to call it explicitly
296 *
297 * \ingroup wpobjectinterest
298 * \param self the object interest to validate
299 * \param error (out) (optional): the error, in case validation failed
300 * \returns TRUE if the interest is valid and can be used in a match,
301 * FALSE otherwise
302 */
303 gboolean
304 wp_object_interest_validate (WpObjectInterest * self, GError ** error)
305 {
306 struct constraint *c;
307 gboolean is_props;
308
309 g_return_val_if_fail (self != NULL, FALSE);
310
311 /* if already validated, we are done */
312 if (self->valid)
313 return TRUE;
314
315 if (!G_TYPE_IS_OBJECT (self->gtype) && !G_TYPE_IS_INTERFACE (self->gtype) &&
316 !g_type_is_a (self->gtype, WP_TYPE_PROPERTIES) &&
317 !g_type_is_a (self->gtype, WP_TYPE_EVENT)) {
318 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
319 "type '%s' is not a valid interest type", g_type_name (self->gtype));
320 return FALSE;
321 }
322
323 is_props = g_type_is_a (self->gtype, WP_TYPE_PROPERTIES);
324
325 pw_array_for_each (c, &self->constraints) {
326 const GVariantType *value_type = NULL;
327
328 if (c->type <= WP_CONSTRAINT_TYPE_NONE ||
329 c->type > WP_CONSTRAINT_TYPE_G_PROPERTY) {
330 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
331 "invalid constraint type %d", c->type);
332 return FALSE;
333 }
334
335 if (is_props && c->type == WP_CONSTRAINT_TYPE_G_PROPERTY) {
336 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
337 "constraint type %d cannot apply to type '%s'",
338 c->type, g_type_name (self->gtype));
339 return FALSE;
340 }
341
342 if (!c->subject) {
343 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
344 "constraint subject cannot be NULL");
345 return FALSE;
346 }
347
348 switch (c->verb) {
349 case WP_CONSTRAINT_VERB_EQUALS:
350 case WP_CONSTRAINT_VERB_NOT_EQUALS:
351 case WP_CONSTRAINT_VERB_IN_LIST:
352 case WP_CONSTRAINT_VERB_IN_RANGE:
353 case WP_CONSTRAINT_VERB_MATCHES:
354 if (!c->value) {
355 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
356 "verb %d (%c) requires a value", c->verb, (gchar) c->verb);
357 return FALSE;
358 }
359 value_type = g_variant_get_type (c->value);
360 break;
361
362 case WP_CONSTRAINT_VERB_IS_PRESENT:
363 case WP_CONSTRAINT_VERB_IS_ABSENT:
364 if (c->value) {
365 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
366 "verb %d (%c) should not have a value", c->verb, (gchar) c->verb);
367 return FALSE;
368 }
369 break;
370
371 default:
372 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
373 "invalid constraint verb %d (%c)", c->verb, (gchar) c->verb);
374 return FALSE;
375 }
376
377 switch (c->verb) {
378 case WP_CONSTRAINT_VERB_EQUALS:
379 case WP_CONSTRAINT_VERB_NOT_EQUALS:
380 if (!g_variant_type_equal (value_type, G_VARIANT_TYPE_STRING) &&
381 !g_variant_type_equal (value_type, G_VARIANT_TYPE_BOOLEAN) &&
382 !g_variant_type_equal (value_type, G_VARIANT_TYPE_INT32) &&
383 !g_variant_type_equal (value_type, G_VARIANT_TYPE_UINT32) &&
384 !g_variant_type_equal (value_type, G_VARIANT_TYPE_INT64) &&
385 !g_variant_type_equal (value_type, G_VARIANT_TYPE_UINT64) &&
386 !g_variant_type_equal (value_type, G_VARIANT_TYPE_DOUBLE)) {
387 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
388 "WP_CONSTRAINT_VERB_{NOT_,}EQUALS requires a basic GVariant type"
389 " (actual type was '%s')", g_variant_get_type_string (c->value));
390 return FALSE;
391 }
392
393 break;
394 case WP_CONSTRAINT_VERB_IN_LIST: {
395 const GVariantType *tuple_type;
396
397 if (!g_variant_type_is_definite (value_type) ||
398 !g_variant_type_is_tuple (value_type)) {
399 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
400 "WP_CONSTRAINT_VERB_IN_LIST requires a tuple GVariant type"
401 " (actual type was '%s')", g_variant_get_type_string (c->value));
402 return FALSE;
403 }
404
405 for (tuple_type = value_type = g_variant_type_first (value_type);
406 tuple_type != NULL;
407 tuple_type = g_variant_type_next (tuple_type)) {
408 if (!g_variant_type_equal (tuple_type, value_type)) {
409 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
410 "tuple must contain children of the same type"
411 " (mismatching type was '%s' at '%.*s')",
412 g_variant_get_type_string (c->value),
413 (int) g_variant_type_get_string_length (tuple_type),
414 g_variant_type_peek_string (tuple_type));
415 return FALSE;
416 }
417 }
418
419 if (!g_variant_type_equal (value_type, G_VARIANT_TYPE_STRING) &&
420 !g_variant_type_equal (value_type, G_VARIANT_TYPE_INT32) &&
421 !g_variant_type_equal (value_type, G_VARIANT_TYPE_UINT32) &&
422 !g_variant_type_equal (value_type, G_VARIANT_TYPE_INT64) &&
423 !g_variant_type_equal (value_type, G_VARIANT_TYPE_UINT64) &&
424 !g_variant_type_equal (value_type, G_VARIANT_TYPE_DOUBLE)) {
425 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
426 "list tuple must contain string, (u)int32, (u)int64 or double"
427 " (mismatching type was '%s' at '%.*s')",
428 g_variant_get_type_string (c->value),
429 (int) g_variant_type_get_string_length (value_type),
430 g_variant_type_peek_string (value_type));
431 return FALSE;
432 }
433
434 break;
435 }
436 case WP_CONSTRAINT_VERB_IN_RANGE: {
437 const GVariantType *tuple_type;
438
439 if (!g_variant_type_is_definite (value_type) ||
440 !g_variant_type_is_tuple (value_type)) {
441 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
442 "range requires a tuple GVariant type (actual type was '%s')",
443 g_variant_get_type_string (c->value));
444 return FALSE;
445 }
446
447 tuple_type = value_type = g_variant_type_first (value_type);
448 if (!tuple_type) {
449 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
450 "range requires a non-empty tuple (actual type was '%s')",
451 g_variant_get_type_string (c->value));
452 return FALSE;
453 }
454
455 if (!g_variant_type_equal (tuple_type, G_VARIANT_TYPE_INT32) &&
456 !g_variant_type_equal (tuple_type, G_VARIANT_TYPE_UINT32) &&
457 !g_variant_type_equal (tuple_type, G_VARIANT_TYPE_INT64) &&
458 !g_variant_type_equal (tuple_type, G_VARIANT_TYPE_UINT64) &&
459 !g_variant_type_equal (tuple_type, G_VARIANT_TYPE_DOUBLE)) {
460 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
461 "range tuple must contain (u)int32, (u)int64 or double"
462 " (mismatching type was '%s' at '%.*s')",
463 g_variant_get_type_string (c->value),
464 (int) g_variant_type_get_string_length (tuple_type),
465 g_variant_type_peek_string (tuple_type));
466 return FALSE;
467 }
468
469 tuple_type = g_variant_type_next (tuple_type);
470 if (!tuple_type || !g_variant_type_equal (tuple_type, value_type)) {
471 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
472 "range tuple must contain 2 children of the same type"
473 " (mismatching type was '%s' at '%.*s')",
474 g_variant_get_type_string (c->value),
475 (int) g_variant_type_get_string_length (tuple_type),
476 g_variant_type_peek_string (tuple_type));
477 return FALSE;
478 }
479
480 tuple_type = g_variant_type_next (tuple_type);
481 if (tuple_type) {
482 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
483 "range tuple must contain exactly 2 children, not more"
484 " (mismatching type was '%s')",
485 g_variant_get_type_string (c->value));
486 return FALSE;
487 }
488
489 break;
490 }
491 case WP_CONSTRAINT_VERB_MATCHES:
492 if (!g_variant_type_equal (value_type, G_VARIANT_TYPE_STRING)) {
493 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
494 "WP_CONSTRAINT_VERB_MATCHES requires a string GVariant"
495 " (actual type was '%s')", g_variant_get_type_string (c->value));
496 return FALSE;
497 }
498
499 break;
500 case WP_CONSTRAINT_VERB_IS_PRESENT:
501 case WP_CONSTRAINT_VERB_IS_ABSENT:
502 break;
503 default:
504 g_return_val_if_reached (FALSE);
505 }
506
507 /* cache the type that the property must have */
508 if (value_type)
509 c->subject_type = *g_variant_type_peek_string (value_type);
510 }
511
512 return (self->valid = TRUE);
513 }
514
515 G_GNUC_CONST static GType
516 subject_type_to_gtype (gchar type)
517 {
518 switch (type) {
519 case 'b': return G_TYPE_BOOLEAN;
520 case 'i': return G_TYPE_INT;
521 case 'u': return G_TYPE_UINT;
522 case 'x': return G_TYPE_INT64;
523 case 't': return G_TYPE_UINT64;
524 case 'd': return G_TYPE_DOUBLE;
525 case 's': return G_TYPE_STRING;
526 default: g_return_val_if_reached (G_TYPE_INVALID);
527 }
528 }
529
530 static inline gboolean
531 property_string_to_gvalue (gchar subj_type, const gchar * str, GValue * val)
532 {
533 g_value_init (val, subject_type_to_gtype (subj_type));
534
535 switch (subj_type) {
536 case 'b':
537 if (!strcmp (str, "true") || !strcmp (str, "1"))
538 g_value_set_boolean (val, TRUE);
539 else if (!strcmp (str, "false") || !strcmp (str, "0"))
540 g_value_set_boolean (val, FALSE);
541 else {
542 wp_trace ("failed to convert '%s' to boolean", str);
543 return FALSE;
544 }
545 break;
546 case 's':
547 g_value_set_static_string (val, str);
548 break;
549
550 #define CASE_NUMBER(l, T, convert) \
551 case l: { \
552 g##T number; \
553 errno = 0; \
554 number = convert; \
555 if (errno != 0) { \
556 wp_trace ("failed to convert '%s' to " #T, str); \
557 return FALSE; \
558 } \
559 g_value_set_##T (val, number); \
560 break; \
561 }
562 CASE_NUMBER ('i', int, strtol (str, NULL, 10))
563 CASE_NUMBER ('u', uint, strtoul (str, NULL, 10))
564 CASE_NUMBER ('x', int64, strtoll (str, NULL, 10))
565 CASE_NUMBER ('t', uint64, strtoull (str, NULL, 10))
566 CASE_NUMBER ('d', double, strtod (str, NULL))
567 #undef CASE_NUMBER
568 default:
569 g_return_val_if_reached (FALSE);
570 }
571 return TRUE;
572 }
573
574 static inline gboolean
575 constraint_verb_equals (gchar subj_type, const GValue * subj_val,
576 GVariant * check_val)
577 {
578 switch (subj_type) {
579 case 'd': {
580 gdouble a = g_value_get_double (subj_val);
581 gdouble b = g_variant_get_double (check_val);
582 return G_APPROX_VALUE (a, b, FLT_EPSILON);
583 }
584 case 's':
585 return !g_strcmp0 (g_value_get_string (subj_val),
586 g_variant_get_string (check_val, NULL));
587 #define CASE_BASIC(l, T, R) \
588 case l: \
589 return (g_value_get_##T (subj_val) == g_variant_get_##R (check_val));
590 CASE_BASIC ('b', boolean, boolean)
591 CASE_BASIC ('i', int, int32)
592 CASE_BASIC ('u', uint, uint32)
593 CASE_BASIC ('x', int64, int64)
594 CASE_BASIC ('t', uint64, uint64)
595 #undef CASE_BASIC
596 default:
597 g_return_val_if_reached (FALSE);
598 }
599 }
600
601 static inline gboolean
602 constraint_verb_matches (gchar subj_type, const GValue * subj_val,
603 GVariant * check_val)
604 {
605 switch (subj_type) {
606 case 's': {
607 const gchar *check_str = g_variant_get_string (check_val, NULL);
608 const gchar *subj_str = g_value_get_string (subj_val);
609 if (!check_str || !subj_str)
610 return FALSE;
611 return g_pattern_match_simple (check_str, subj_str);
612 }
613 default:
614 g_return_val_if_reached (FALSE);
615 }
616 return TRUE;
617 }
618
619 static inline gboolean
620 constraint_verb_in_list (gchar subj_type, const GValue * subj_val,
621 GVariant * check_val)
622 {
623 GVariantIter iter;
624 g_autoptr (GVariant) child = NULL;
625
626 g_variant_iter_init (&iter, check_val);
627 while ((child = g_variant_iter_next_value (&iter))) {
628 if (constraint_verb_equals (subj_type, subj_val, child))
629 return TRUE;
630 g_variant_unref (child);
631 }
632 return FALSE;
633 }
634
635 static inline gboolean
636 constraint_verb_in_range (gchar subj_type, const GValue * subj_val,
637 GVariant * check_val)
638 {
639 switch (subj_type) {
640 #define CASE_RANGE(l, t, T) \
641 case l: { \
642 g##T val, min, max; \
643 g_variant_get (check_val, "("#t#t")", &min, &max); \
644 val = g_value_get_##T (subj_val); \
645 if (val < min || val > max) \
646 return FALSE; \
647 break; \
648 }
649 CASE_RANGE('i', i, int)
650 CASE_RANGE('u', u, uint)
651 CASE_RANGE('x', x, int64)
652 CASE_RANGE('t', t, uint64)
653 CASE_RANGE('d', d, double)
654 #undef CASE_RANGE
655 default:
656 g_return_val_if_reached (FALSE);
657 }
658 return TRUE;
659 }
660
661 /*!
662 * \brief Checks if the specified \a object matches the type and all the
663 * constraints that are described in \a self
664 *
665 * If \a self is configured to match GObject subclasses, this is equivalent to
666 * `wp_object_interest_matches_full (self, G_OBJECT_TYPE (object), object,
667 * NULL, NULL)` and if it is configured to match WpProperties, this is
668 * equivalent to `wp_object_interest_matches_full (self, self->gtype, NULL,
669 * (WpProperties *) object, NULL);`
670 *
671 * \ingroup wpobjectinterest
672 * \param self the object interest
673 * \param object the target object to check for a match
674 * \returns TRUE if the object matches, FALSE otherwise
675 */
676 gboolean
677 wp_object_interest_matches (WpObjectInterest * self, gpointer object)
678 {
679 if (g_type_is_a (self->gtype, WP_TYPE_PROPERTIES)) {
680 g_return_val_if_fail (object != NULL, FALSE);
681 return wp_object_interest_matches_full (self, 0, self->gtype, NULL,
682 (WpProperties *) object, NULL) == WP_INTEREST_MATCH_ALL;
683 }
684 else {
685 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
686 return wp_object_interest_matches_full (self, 0, G_OBJECT_TYPE (object),
687 object, NULL, NULL) == WP_INTEREST_MATCH_ALL;
688 }
689 }
690
691 /*!
692 * \brief A low-level version of wp_object_interest_matches().
693 *
694 * In this version, the object's type is directly given in \a object_type and
695 * is not inferred from the \a object. \a object is only used to check for
696 * constraints against GObject properties.
697 *
698 * \a pw_props and \a pw_global_props are used to check constraints against
699 * PipeWire object properties and global properties, respectively.
700 *
701 * \a object, \a pw_props and \a pw_global_props may be NULL, but in case there
702 * are any constraints that require them, the match will fail.
703 * As a special case, if \a object is not NULL and is a subclass of WpProxy,
704 * then \a pw_props and \a pw_global_props, if required, will be internally
705 * retrieved from \a object by calling wp_pipewire_object_get_properties() and
706 * wp_global_proxy_get_global_properties() respectively.
707 *
708 * When \a flags contains WP_INTEREST_MATCH_FLAGS_CHECK_ALL, all the constraints
709 * are checked and the returned value contains accurate information about which
710 * types of constraints have failed to match, if any. When this flag is not
711 * present, this function returns after the first failure has been encountered.
712 * This means that the returned flags set will contain all but one flag, which
713 * will indicate the kind of constraint that failed (more could have failed,
714 * but they are not checked...)
715 *
716 * \ingroup wpobjectinterest
717 * \param self the object interest
718 * \param flags flags to alter the behavior of this function
719 * \param object_type the type to be checked against the interest's type
720 * \param object (type GObject)(transfer none)(nullable): the object to be used for
721 * checking constraints of type WP_CONSTRAINT_TYPE_G_PROPERTY
722 * \param pw_props (transfer none)(nullable): the properties to be used for
723 * checking constraints of type WP_CONSTRAINT_TYPE_PW_PROPERTY
724 * \param pw_global_props (transfer none)(nullable): the properties to be used for
725 * checking constraints of type WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY
726 * \returns flags that indicate which components of the interest match.
727 * WP_INTEREST_MATCH_ALL indicates a fully successful match; any other
728 * combination indicates a failure on the component(s) that do not appear on
729 * the flag set
730 */
731 WpInterestMatch
732 wp_object_interest_matches_full (WpObjectInterest * self,
733 WpInterestMatchFlags flags, GType object_type, gpointer object,
734 WpProperties * pw_props, WpProperties * pw_global_props)
735 {
736 WpInterestMatch result = WP_INTEREST_MATCH_ALL;
737 g_autoptr (WpProperties) props = NULL;
738 g_autoptr (WpProperties) global_props = NULL;
739 g_autoptr (GError) error = NULL;
740 struct constraint *c;
741
742 g_return_val_if_fail (self != NULL, WP_INTEREST_MATCH_NONE);
743
744 if (G_UNLIKELY (!wp_object_interest_validate (self, &error))) {
745 wp_critical_boxed (WP_TYPE_OBJECT_INTEREST, self, "validation failed: %s",
746 error->message);
747 return WP_INTEREST_MATCH_NONE;
748 }
749
750 /* check if the GType matches */
751 if (!g_type_is_a (object_type, self->gtype))
752 result &= ~WP_INTEREST_MATCH_GTYPE;
753
754 /* prepare for constraint lookups on proxy properties */
755 if (object) {
756 if (!pw_global_props && WP_IS_GLOBAL_PROXY (object)) {
757 WpGlobalProxy *pwg = (WpGlobalProxy *) object;
758 pw_global_props = global_props =
759 wp_global_proxy_get_global_properties (pwg);
760 }
761
762 if (!pw_props && WP_IS_PIPEWIRE_OBJECT (object)) {
763 WpObject *oo = (WpObject *) object;
764 WpPipewireObject *pwo = (WpPipewireObject *) object;
765
766 if (wp_object_test_active_features (oo, WP_PIPEWIRE_OBJECT_FEATURE_INFO))
767 pw_props = props = wp_pipewire_object_get_properties (pwo);
768 }
769
770 if (!pw_global_props && WP_IS_SESSION_ITEM (object)) {
771 WpSessionItem *si = (WpSessionItem *) object;
772 pw_global_props = props = wp_session_item_get_properties (si);
773 if (!pw_props)
774 pw_props = props;
775 }
776 }
777
778 /* check all constraints; if any of them fails at any point, fail the match */
779 pw_array_for_each (c, &self->constraints) {
780 WpProperties *lookup_props = pw_global_props;
781 g_auto (GValue) value = G_VALUE_INIT;
782 gboolean exists = FALSE;
783
784 /* return early if the match failed and CHECK_ALL is not specified */
785 if (!(flags & WP_INTEREST_MATCH_FLAGS_CHECK_ALL) &&
786 result != WP_INTEREST_MATCH_ALL)
787 return result;
788
789 /* collect, check & convert the subject property */
790 switch (c->type) {
791 case WP_CONSTRAINT_TYPE_PW_PROPERTY:
792 lookup_props = pw_props;
793 SPA_FALLTHROUGH;
794
795 case WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY: {
796 const gchar *lookup_str = NULL;
797
798 if (lookup_props)
799 exists = !!(lookup_str = wp_properties_get (lookup_props, c->subject));
800
801 if (exists && c->subject_type)
802 property_string_to_gvalue (c->subject_type, lookup_str, &value);
803 break;
804 }
805 case WP_CONSTRAINT_TYPE_G_PROPERTY: {
806 GType value_type;
807 GParamSpec *pspec = NULL;
808
809 if (object)
810 exists = !!(pspec = g_object_class_find_property (
811 G_OBJECT_GET_CLASS (object), c->subject));
812
813 if (exists && c->subject_type) {
814 g_value_init (&value, pspec->value_type);
815 g_object_get_property (object, c->subject, &value);
816 value_type = G_VALUE_TYPE (&value);
817
818 /* transform if not compatible */
819 if (value_type != subject_type_to_gtype (c->subject_type)) {
820 if (g_value_type_transformable (value_type,
821 subject_type_to_gtype (c->subject_type))) {
822 g_auto (GValue) orig = G_VALUE_INIT;
823 g_value_init (&orig, value_type);
824 g_value_copy (&value, &orig);
825 g_value_unset (&value);
826 g_value_init (&value, subject_type_to_gtype (c->subject_type));
827 g_value_transform (&orig, &value);
828 }
829 else {
830 result &= ~(1 << c->type);
831 continue;
832 }
833 }
834 }
835
836 break;
837 }
838 default:
839 g_return_val_if_reached (WP_INTEREST_MATCH_NONE);
840 }
841
842 /* match the subject to the constraint's value,
843 according to the operation defined by the verb */
844 switch (c->verb) {
845 case WP_CONSTRAINT_VERB_EQUALS:
846 if (!exists ||
847 !constraint_verb_equals (c->subject_type, &value, c->value))
848 result &= ~(1 << c->type);
849 break;
850 case WP_CONSTRAINT_VERB_NOT_EQUALS:
851 if (exists &&
852 constraint_verb_equals (c->subject_type, &value, c->value))
853 result &= ~(1 << c->type);
854 break;
855 case WP_CONSTRAINT_VERB_MATCHES:
856 if (!exists ||
857 !constraint_verb_matches (c->subject_type, &value, c->value))
858 result &= ~(1 << c->type);
859 break;
860 case WP_CONSTRAINT_VERB_IN_LIST:
861 if (!exists ||
862 !constraint_verb_in_list (c->subject_type, &value, c->value))
863 result &= ~(1 << c->type);
864 break;
865 case WP_CONSTRAINT_VERB_IN_RANGE:
866 if (!exists ||
867 !constraint_verb_in_range (c->subject_type, &value, c->value))
868 result &= ~(1 << c->type);
869 break;
870 case WP_CONSTRAINT_VERB_IS_PRESENT:
871 if (!exists)
872 result &= ~(1 << c->type);
873 break;
874 case WP_CONSTRAINT_VERB_IS_ABSENT:
875 if (exists)
876 result &= ~(1 << c->type);
877 break;
878 default:
879 g_return_val_if_reached (WP_INTEREST_MATCH_NONE);
880 }
881 }
882 return result;
883 }
884
885 /*!
886 * \brief Finds all the defined constraint values for a subject in \a self.
887 *
888 * A defined constraint value is the value of a constraint with the 'equal' or
889 * 'in-list' verb, because the full value must be defined with those verbs. This
890 * can be useful for cases where we want to enumerate interests that are
891 * interested in specific subjects.
892 *
893 * \ingroup wpobjectinterest
894 * \param self the object interest
895 * \param type the constraint type
896 * \param subject the subject that the constraint applies to
897 * \returns (element-type GVariant) (transfer full) (nullable): the defined
898 * constraint values for this object interest.
899 * \since 0.5.13
900 */
901 GPtrArray *
902 wp_object_interest_find_defined_constraint_values (WpObjectInterest * self,
903 WpConstraintType type, const gchar * subject)
904 {
905 GPtrArray *res = g_ptr_array_new_with_free_func (
906 (GDestroyNotify)g_variant_unref);
907 struct constraint *c;
908
909 pw_array_for_each (c, &self->constraints) {
910 if ((c->type == type || WP_CONSTRAINT_TYPE_NONE == type) &&
911 g_str_equal (c->subject, subject)) {
912 switch (c->verb) {
913 case WP_CONSTRAINT_VERB_EQUALS:
914 g_ptr_array_add (res, g_variant_ref (c->value));
915 break;
916 case WP_CONSTRAINT_VERB_IN_LIST: {
917 GVariantIter iter;
918 GVariant *child;
919 g_variant_iter_init (&iter, c->value);
920 while ((child = g_variant_iter_next_value (&iter)))
921 g_ptr_array_add (res, child);
922 break;
923 }
924 default:
925 break;
926 }
927 }
928 }
929
930 return res;
931 }