PipeWire 1.7.0
Loading...
Searching...
No Matches
compare.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_POD_COMPARE_H
6#define SPA_POD_COMPARE_H
7
8#include <stdarg.h>
9#include <errno.h>
10#include <stdint.h>
11#include <stddef.h>
12#include <stdio.h>
13#include <string.h>
14
15#include <spa/param/props.h>
16#include <spa/pod/iter.h>
17#include <spa/pod/builder.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#ifndef SPA_API_POD_COMPARE
24 #ifdef SPA_API_IMPL
25 #define SPA_API_POD_COMPARE SPA_API_IMPL
26 #else
27 #define SPA_API_POD_COMPARE static inline
28 #endif
29#endif
30
35
36SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
37{
38 switch (type) {
39 case SPA_TYPE_None:
40 return 0;
41 case SPA_TYPE_Bool:
42 return SPA_CMP(!!*(int32_t *)r1, !!*(int32_t *)r2);
44 return SPA_CMP(*(uint32_t *)r1, *(uint32_t *)r2);
45 case SPA_TYPE_Int:
46 return SPA_CMP(*(int32_t *)r1, *(int32_t *)r2);
47 case SPA_TYPE_Long:
48 return SPA_CMP(*(int64_t *)r1, *(int64_t *)r2);
49 case SPA_TYPE_Float:
50 return SPA_CMP(*(float *)r1, *(float *)r2);
51 case SPA_TYPE_Double:
52 return SPA_CMP(*(double *)r1, *(double *)r2);
53 case SPA_TYPE_String:
54 return strncmp((char *)r1, (char *)r2, size);
56 {
57 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
58 *rec2 = (struct spa_rectangle *) r2;
59 uint64_t a1, a2;
60 a1 = ((uint64_t) rec1->width) * rec1->height;
61 a2 = ((uint64_t) rec2->width) * rec2->height;
62 if (a1 < a2)
63 return -1;
64 if (a1 > a2)
65 return 1;
66 return SPA_CMP(rec1->width, rec2->width);
67 }
69 {
70 const struct spa_fraction *f1 = (struct spa_fraction *) r1,
71 *f2 = (struct spa_fraction *) r2;
72
73 return spa_fraction_cmp(f1, f2);
74 }
75 default:
76 return memcmp(r1, r2, size);
77 }
78 return 0;
79}
80
82 const struct spa_pod *b)
83{
84 return ((a == b) || (a && b && SPA_POD_SIZE(a) == SPA_POD_SIZE(b) &&
85 memcmp(a, b, SPA_POD_SIZE(b)) == 0)) ? 0 : 1;
86}
87
89 const struct spa_pod *pod2)
90{
91 int res = 0;
92 uint32_t n_vals1, n_vals2;
93 uint32_t choice1, choice2;
94
95 spa_return_val_if_fail(pod1 != NULL, -EINVAL);
96 spa_return_val_if_fail(pod2 != NULL, -EINVAL);
97
98 pod1 = spa_pod_get_values(pod1, &n_vals1, &choice1);
99 pod2 = spa_pod_get_values(pod2, &n_vals2, &choice2);
100
101 if (n_vals1 != n_vals2)
102 return -EINVAL;
103
104 if (pod1->type != pod2->type)
105 return -EINVAL;
106
107 if (n_vals1 < 1)
108 return -EINVAL; /* empty choice */
109
110 switch (pod1->type) {
111 case SPA_TYPE_Struct:
112 {
113 const struct spa_pod *p1, *p2;
114 size_t p1s, p2s;
115
116 p1 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod1);
117 p1s = SPA_POD_BODY_SIZE(pod1);
118 p2 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod2);
119 p2s = SPA_POD_BODY_SIZE(pod2);
120
121 while (true) {
122 if (!spa_pod_is_inside(pod1, p1s, p1) ||
123 !spa_pod_is_inside(pod2, p2s, p2))
124 return -EINVAL;
125
126 if ((res = spa_pod_compare(p1, p2)) != 0)
127 return res;
128
129 p1 = (const struct spa_pod*)spa_pod_next(p1);
130 p2 = (const struct spa_pod*)spa_pod_next(p2);
131 }
132 break;
133 }
134 case SPA_TYPE_Object:
135 {
136 const struct spa_pod_prop *p1, *p2;
137 const struct spa_pod_object *o1, *o2;
138
139 o1 = (const struct spa_pod_object*)pod1;
140 o2 = (const struct spa_pod_object*)pod2;
141
142 p2 = NULL;
143 SPA_POD_OBJECT_FOREACH(o1, p1) {
144 if ((p2 = spa_pod_object_find_prop(o2, p2, p1->key)) == NULL)
145 return 1;
146 if ((res = spa_pod_compare(&p1->value, &p2->value)) != 0)
147 return res;
148 }
149 p1 = NULL;
150 SPA_POD_OBJECT_FOREACH(o2, p2) {
151 if ((p1 = spa_pod_object_find_prop(o1, p1, p2->key)) == NULL)
152 return -1;
153 }
154 break;
155 }
156 case SPA_TYPE_Array:
157 res = spa_pod_memcmp(pod1, pod2);
158 break;
159 default:
160 if (pod1->size != pod2->size)
161 return -EINVAL;
162 if (pod1->size < spa_pod_type_size(pod1->type))
163 return -EINVAL;
165 SPA_POD_BODY(pod1), SPA_POD_BODY(pod2),
166 pod1->size);
167 break;
168 }
169 return res;
170}
171
172SPA_API_POD_COMPARE int spa_pod_compare_is_compatible_flags(uint32_t type, const void *r1,
173 const void *r2, uint32_t size SPA_UNUSED)
174{
175 switch (type) {
176 case SPA_TYPE_Int:
177 return ((*(int32_t *) r1) & (*(int32_t *) r2)) != 0;
178 case SPA_TYPE_Long:
179 return ((*(int64_t *) r1) & (*(int64_t *) r2)) != 0;
180 default:
181 return -ENOTSUP;
182 }
183 return 0;
184}
185
186
187SPA_API_POD_COMPARE int spa_pod_compare_is_step_of(uint32_t type, const void *r1,
188 const void *r2, uint32_t size)
189{
190 switch (type) {
191 case SPA_TYPE_Int:
192 if (*(int32_t *)r2 < 1)
193 return -EINVAL;
194 return *(int32_t *) r1 % *(int32_t *) r2 == 0;
195 case SPA_TYPE_Long:
196 if (*(int64_t *)r2 < 1)
197 return -EINVAL;
198 return *(int64_t *) r1 % *(int64_t *) r2 == 0;
200 {
201 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
202 *rec2 = (struct spa_rectangle *) r2;
203
204 if (rec2->width < 1 || rec2->height < 1)
205 return -EINVAL;
206
207 return (rec1->width % rec2->width == 0 &&
208 rec1->height % rec2->height == 0);
209 }
210 default:
211 return -ENOTSUP;
212 }
213 return 0;
214}
215
216SPA_API_POD_COMPARE int spa_pod_compare_is_in_range(uint32_t type, const void *v,
217 const void *min, const void *max, const void *step, uint32_t size SPA_UNUSED)
218{
219 if (spa_pod_compare_value(type, v, min, size) < 0 ||
220 spa_pod_compare_value(type, v, max, size) > 0)
221 return 0;
222 if (step != NULL)
223 return spa_pod_compare_is_step_of(type, v, step, size);
224 return 1;
225}
226
227SPA_API_POD_COMPARE int spa_pod_compare_is_valid_choice(uint32_t type, uint32_t size,
228 const void *val, const void *vals, uint32_t n_vals, uint32_t choice)
229{
230 switch (choice) {
231 case SPA_CHOICE_None:
232 if (spa_pod_compare_value(type, val, vals, size) == 0)
233 return 1;
234 return 0;
235 case SPA_CHOICE_Enum:
236 {
237 const void *next = vals;
238 for (uint32_t i = 1; i < n_vals; i++) {
239 next = SPA_PTROFF(next, size, void);
240 if (spa_pod_compare_value(type, val, next, size) == 0)
241 return 1;
242 }
243 return 0;
244 }
245 case SPA_CHOICE_Range:
246 case SPA_CHOICE_Step:
247 {
248 void *min = SPA_PTROFF(vals,size,void);
249 void *max = SPA_PTROFF(min,size,void);
250 void *step = choice == SPA_CHOICE_Step ? SPA_PTROFF(max,size,void) : NULL;
251 return spa_pod_compare_is_in_range(type, val, min, max, step, size);
252 }
253 case SPA_CHOICE_Flags:
254 return 1;
255 }
256 return 0;
257}
258
262
263#ifdef __cplusplus
264}
265#endif
266
267#endif
spa/pod/builder.h
#define SPA_API_POD_COMPARE
Definition compare.h:34
uint32_t int int res
Definition core.h:433
bool const char const char char * val
Definition json-builder.h:377
SPA_API_POD_COMPARE int spa_pod_compare_is_in_range(uint32_t type, const void *v, const void *min, const void *max, const void *step, uint32_t size 1)
Definition compare.h:223
SPA_API_POD_COMPARE int spa_pod_compare_is_step_of(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:194
#define SPA_POD_BODY_CONST(pod)
Definition pod.h:46
SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1, const struct spa_pod *pod2)
Definition compare.h:95
SPA_API_POD_ITER struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition iter.h:223
SPA_API_POD_COMPARE int spa_pod_compare_is_valid_choice(uint32_t type, uint32_t size, const void *val, const void *vals, uint32_t n_vals, uint32_t choice)
Definition compare.h:234
SPA_API_POD_ITER const struct spa_pod_prop * spa_pod_object_find_prop(const struct spa_pod_object *pod, const struct spa_pod_prop *start, uint32_t key)
Definition iter.h:248
SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:43
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:118
#define SPA_POD_BODY(pod)
Definition pod.h:44
SPA_API_POD_ITER void * spa_pod_next(const void *iter)
Definition iter.h:46
#define SPA_POD_BODY_SIZE(pod)
Definition pod.h:31
SPA_API_POD_COMPARE int spa_pod_compare_is_compatible_flags(uint32_t type, const void *r1, const void *r2, uint32_t size 1)
Definition compare.h:179
SPA_API_POD_BODY uint32_t spa_pod_type_size(uint32_t type)
Definition body.h:45
SPA_API_POD_ITER bool spa_pod_is_inside(const void *pod, uint32_t size, const void *iter)
Definition iter.h:38
#define SPA_POD_SIZE(pod)
Definition pod.h:35
SPA_API_POD_COMPARE int spa_pod_memcmp(const struct spa_pod *a, const struct spa_pod *b)
Definition compare.h:88
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition pod.h:163
@ SPA_CHOICE_None
no choice, first value is current
Definition pod.h:161
@ SPA_CHOICE_Flags
flags: first value is flags
Definition pod.h:165
@ SPA_CHOICE_Range
range: default, min, max
Definition pod.h:162
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition pod.h:164
@ SPA_TYPE_Int
Definition type.h:45
@ SPA_TYPE_Rectangle
Definition type.h:51
@ SPA_TYPE_Long
Definition type.h:46
@ SPA_TYPE_Bool
Definition type.h:43
@ SPA_TYPE_Object
Definition type.h:56
@ SPA_TYPE_Float
Definition type.h:47
@ SPA_TYPE_Fraction
Definition type.h:52
@ SPA_TYPE_None
Definition type.h:42
@ SPA_TYPE_Double
Definition type.h:48
@ SPA_TYPE_Id
Definition type.h:44
@ SPA_TYPE_Array
Definition type.h:54
@ SPA_TYPE_String
Definition type.h:49
@ SPA_TYPE_Struct
Definition type.h:55
SPA_API_UTILS_DEFS int spa_fraction_cmp(const struct spa_fraction *a, const struct spa_fraction *b)
Definition defs.h:605
#define SPA_UNUSED
Definition defs.h:310
#define spa_return_val_if_fail(expr, val)
Definition defs.h:505
#define SPA_CMP(a, b)
3-way comparison.
Definition defs.h:210
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:223
spa/pod/iter.h
spa/utils/string.h
Definition defs.h:138
Definition pod.h:202
Definition pod.h:227
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:228
struct spa_pod value
Definition pod.h:249
Definition pod.h:57
uint32_t type
Definition pod.h:59
uint32_t size
Definition pod.h:58
Definition defs.h:117
uint32_t width
Definition defs.h:118
uint32_t height
Definition defs.h:119