1
2
3
4
5
6
7
8
9
10
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
|
/* PipeWire
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <pipewire/private.h>
#include <pipewire/global.h>
#include <pipewire/interfaces.h>
#include <pipewire/type.h>
#include <spa/debug/types.h>
/** \cond */
struct impl {
struct pw_global this;
bool registered;
};
/** \endcond */
static inline uint32_t get_permissions(struct pw_global *global, struct pw_client *client, bool recurse)
{
uint32_t perms;
if (client->permission_func == NULL)
return PW_PERM_RWX;
perms = client->permission_func(global, client, client->permission_data);
if (recurse) {
while (global->parent != NULL && global != global->parent) {
global = global->parent;
perms &= client->permission_func(global, client, client->permission_data);
}
}
return perms;
}
SPA_EXPORT
uint32_t pw_global_get_permissions(struct pw_global *global, struct pw_client *client)
{
return get_permissions(global, client, true);
}
/** Create a new global
*
* \param core a core object
* \param type the type of the global
* \param version the version of the type
* \param properties extra properties
* \param bind a function to bind to this global
* \param object the associated object
* \return a result global
*
* \memberof pw_global
*/
SPA_EXPORT
struct pw_global *
pw_global_new(struct pw_core *core,
uint32_t type,
uint32_t version,
struct pw_properties *properties,
pw_global_bind_func_t func,
void *object)
{
struct impl *impl;
struct pw_global *this;
int res;
impl = calloc(1, sizeof(struct impl));
if (impl == NULL) {
res = -errno;
goto error_cleanup;
}
this = &impl->this;
this->core = core;
this->type = type;
this->version = version;
this->func = func;
this->object = object;
this->properties = properties;
this->id = pw_map_insert_new(&core->globals, this);
if (this->id == SPA_ID_INVALID) {
res = -errno;
pw_log_error("global %p: can't allocate new id: %m", this);
goto error_free;
}
spa_list_init(&this->child_list);
spa_list_init(&this->resource_list);
spa_hook_list_init(&this->listener_list);
pw_log_debug("global %p: new %s %d", this,
spa_debug_type_find_name(pw_type_info(), this->type),
this->id);
return this;
error_free:
free(impl);
error_cleanup:
if (properties)
pw_properties_free(properties);
errno = -res;
return NULL;
}
/** register a global to the core registry
*
* \param global a global to add
* \param owner an optional owner client of the global
* \param parent an optional parent of the global
* \return 0 on success < 0 errno value on failure
*
* \memberof pw_global
*/
SPA_EXPORT
int
pw_global_register(struct pw_global *global,
struct pw_client *owner,
struct pw_global *parent)
{
struct impl *impl = SPA_CONTAINER_OF(global, struct impl, this);
struct pw_resource *registry;
struct pw_core *core = global->core;
if (impl->registered)
return -EEXIST;
global->owner = owner;
if (owner && parent == NULL)
parent = owner->global;
if (parent == NULL)
parent = core->global;
if (parent == NULL)
parent = global;
global->parent = parent;
spa_list_append(&parent->child_list, &global->child_link);
spa_list_append(&core->global_list, &global->link);
impl->registered = true;
spa_list_for_each(registry, &core->registry_resource_list, link) {
uint32_t permissions = pw_global_get_permissions(global, registry->client);
pw_log_debug("registry %p: global %d %08x", registry, global->id, permissions);
if (PW_PERM_IS_R(permissions))
pw_registry_resource_global(registry,
global->id,
global->parent->id,
permissions,
global->type,
global->version,
global->properties ?
&global->properties->dict : NULL);
}
pw_log_debug("global %p: registered %u owner %p parent %p", global, global->id, owner, parent);
pw_core_emit_global_added(core, global);
return 0;
}
static int global_unregister(struct pw_global *global)
{
struct impl *impl = SPA_CONTAINER_OF(global, struct impl, this);
struct pw_core *core = global->core;
struct pw_resource *resource;
struct pw_global *g;
if (!impl->registered)
return 0;
spa_list_consume(g, &global->child_list, child_link) {
if (g == global)
break;
global_unregister(g);
}
spa_list_for_each(resource, &core->registry_resource_list, link) {
uint32_t permissions = pw_global_get_permissions(global, resource->client);
pw_log_debug("registry %p: global %d %08x", resource, global->id, permissions);
if (PW_PERM_IS_R(permissions))
pw_registry_resource_global_remove(resource, global->id);
}
global->parent = NULL;
spa_list_remove(&global->child_link);
spa_list_remove(&global->link);
pw_map_remove(&core->globals, global->id);
impl->registered = false;
pw_log_debug("global %p: unregistered %u", global, global->id);
pw_core_emit_global_removed(core, global);
return 0;
}
SPA_EXPORT
struct pw_core *pw_global_get_core(struct pw_global *global)
{
return global->core;
}
SPA_EXPORT
struct pw_client *pw_global_get_owner(struct pw_global *global)
{
return global->owner;
}
SPA_EXPORT
struct pw_global *pw_global_get_parent(struct pw_global *global)
{
return global->parent;
}
SPA_EXPORT
uint32_t pw_global_get_type(struct pw_global *global)
{
return global->type;
}
SPA_EXPORT
uint32_t pw_global_get_version(struct pw_global *global)
{
return global->version;
}
SPA_EXPORT
const struct pw_properties *pw_global_get_properties(struct pw_global *global)
{
return global->properties;
}
SPA_EXPORT
void * pw_global_get_object(struct pw_global *global)
{
return global->object;
}
SPA_EXPORT
uint32_t pw_global_get_id(struct pw_global *global)
{
return global->id;
}
SPA_EXPORT
void pw_global_add_listener(struct pw_global *global,
struct spa_hook *listener,
const struct pw_global_events *events,
void *data)
{
spa_hook_list_append(&global->listener_list, listener, events, data);
}
/** Bind to a global
*
* \param global the global to bind to
* \param client the client that binds
* \param version the version
* \param id the id of the resource
*
* Let \a client bind to \a global with the given version and id.
* After binding, the client and the global object will be able to
* exchange messages on the proxy/resource with \a id.
*
* \memberof pw_global
*/
SPA_EXPORT int
pw_global_bind(struct pw_global *global, struct pw_client *client, uint32_t permissions,
uint32_t version, uint32_t id)
{
int res;
if (global->version < version)
goto error_version;
if ((res = global->func(global->object, client, permissions, version, id)) < 0)
goto error_bind;
return res;
error_version:
res = -EPROTO;
pw_core_resource_errorf(client->core_resource, id, client->recv_seq,
res, "id %d: interface version %d < %d",
id, global->version, version);
goto error_exit;
error_bind:
pw_core_resource_errorf(client->core_resource, id, client->recv_seq,
res, "can't bind global %u/%u: %d (%s)", id, version, res, spa_strerror(res));
goto error_exit;
error_exit:
pw_log_error("can't bind global %u/%u: %d (%s)", id, version, res, spa_strerror(res));
pw_map_insert_at(&client->objects, id, NULL);
pw_core_resource_remove_id(client->core_resource, id);
return res;
}
SPA_EXPORT
int pw_global_update_permissions(struct pw_global *global, struct pw_client *client,
uint32_t old_permissions, uint32_t new_permissions)
{
struct pw_core *core = global->core;
struct pw_resource *resource, *t;
struct pw_global *g;
uint32_t perms;
pw_global_emit_permissions_changed(global, client, old_permissions, new_permissions);
spa_list_for_each(g, &global->child_list, child_link) {
if (g == global)
continue;
perms = get_permissions(g, client, false);
pw_global_update_permissions(g, client,
perms & old_permissions,
perms & new_permissions);
}
spa_list_for_each(resource, &core->registry_resource_list, link) {
if (resource->client != client)
continue;
if (PW_PERM_IS_R(old_permissions) && !PW_PERM_IS_R(new_permissions)) {
pw_log_debug("client %p: hide global %d", client, global->id);
pw_registry_resource_global_remove(resource, global->id);
}
else if (!PW_PERM_IS_R(old_permissions) && PW_PERM_IS_R(new_permissions)) {
pw_log_debug("client %p: show global %d", client, global->id);
pw_registry_resource_global(resource,
global->id,
global->parent->id,
new_permissions,
global->type,
global->version,
global->properties ?
&global->properties->dict : NULL);
}
}
spa_list_for_each_safe(resource, t, &global->resource_list, link) {
if (resource->client != client)
continue;
/* don't ever destroy the core resource */
if (!PW_PERM_IS_R(new_permissions) && global->id != 0)
pw_resource_destroy(resource);
else
resource->permissions = new_permissions;
}
return 0;
}
/** Destroy a global
*
* \param global a global to destroy
*
* \memberof pw_global
*/
SPA_EXPORT
void pw_global_destroy(struct pw_global *global)
{
struct pw_resource *resource;
pw_log_debug("global %p: destroy %u", global, global->id);
pw_global_emit_destroy(global);
spa_list_consume(resource, &global->resource_list, link)
pw_resource_destroy(resource);
global_unregister(global);
pw_log_debug("global %p: free", global);
pw_global_emit_free(global);
if (global->properties)
pw_properties_free(global->properties);
free(global);
}
|