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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
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
597
598
599
600
601
602
603
604
605
606
607
608
609
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
/***
This file is part of PulseAudio.
Copyright 2006 Lennart Poettering
Copyright 2006 Shams E. King
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/utf8.h>
#include <pulse/xmalloc.h>
#include <pulsecore/core-rtclock.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include "dbus-util.h"
struct pa_dbus_wrap_connection {
pa_mainloop_api *mainloop;
DBusConnection *connection;
pa_defer_event* dispatch_event;
bool use_rtclock:1;
};
struct timeout_data {
pa_dbus_wrap_connection *connection;
DBusTimeout *timeout;
};
static void dispatch_cb(pa_mainloop_api *ea, pa_defer_event *ev, void *userdata) {
DBusConnection *conn = userdata;
if (dbus_connection_dispatch(conn) == DBUS_DISPATCH_COMPLETE)
/* no more data to process, disable the deferred */
ea->defer_enable(ev, 0);
}
/* DBusDispatchStatusFunction callback for the pa mainloop */
static void dispatch_status(DBusConnection *conn, DBusDispatchStatus status, void *userdata) {
pa_dbus_wrap_connection *c = userdata;
pa_assert(c);
switch(status) {
case DBUS_DISPATCH_COMPLETE:
c->mainloop->defer_enable(c->dispatch_event, 0);
break;
case DBUS_DISPATCH_DATA_REMAINS:
case DBUS_DISPATCH_NEED_MEMORY:
default:
c->mainloop->defer_enable(c->dispatch_event, 1);
break;
}
}
static pa_io_event_flags_t get_watch_flags(DBusWatch *watch) {
unsigned int flags;
pa_io_event_flags_t events = 0;
pa_assert(watch);
flags = dbus_watch_get_flags(watch);
/* no watch flags for disabled watches */
if (!dbus_watch_get_enabled(watch))
return PA_IO_EVENT_NULL;
if (flags & DBUS_WATCH_READABLE)
events |= PA_IO_EVENT_INPUT;
if (flags & DBUS_WATCH_WRITABLE)
events |= PA_IO_EVENT_OUTPUT;
return events | PA_IO_EVENT_HANGUP | PA_IO_EVENT_ERROR;
}
/* pa_io_event_cb_t IO event handler */
static void handle_io_event(pa_mainloop_api *ea, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
unsigned int flags = 0;
DBusWatch *watch = userdata;
#if HAVE_DBUS_WATCH_GET_UNIX_FD
pa_assert(fd == dbus_watch_get_unix_fd(watch));
#else
pa_assert(fd == dbus_watch_get_fd(watch));
#endif
if (!dbus_watch_get_enabled(watch)) {
pa_log_warn("Asked to handle disabled watch: %p %i", (void*) watch, fd);
return;
}
if (events & PA_IO_EVENT_INPUT)
flags |= DBUS_WATCH_READABLE;
if (events & PA_IO_EVENT_OUTPUT)
flags |= DBUS_WATCH_WRITABLE;
if (events & PA_IO_EVENT_HANGUP)
flags |= DBUS_WATCH_HANGUP;
if (events & PA_IO_EVENT_ERROR)
flags |= DBUS_WATCH_ERROR;
dbus_watch_handle(watch, flags);
}
/* pa_time_event_cb_t timer event handler */
static void handle_time_event(pa_mainloop_api *ea, pa_time_event* e, const struct timeval *t, void *userdata) {
struct timeval tv;
struct timeout_data *d = userdata;
pa_assert(d);
pa_assert(d->connection);
if (dbus_timeout_get_enabled(d->timeout)) {
/* Restart it for the next scheduled time. We do this before
* calling dbus_timeout_handle() to make sure that the time
* event is still around. */
ea->time_restart(e, pa_timeval_rtstore(&tv,
pa_timeval_load(t) + dbus_timeout_get_interval(d->timeout) * PA_USEC_PER_MSEC,
d->connection->use_rtclock));
dbus_timeout_handle(d->timeout);
}
}
/* DBusAddWatchFunction callback for pa mainloop */
static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
pa_dbus_wrap_connection *c = data;
pa_io_event *ev;
pa_assert(watch);
pa_assert(c);
ev = c->mainloop->io_new(
c->mainloop,
#if HAVE_DBUS_WATCH_GET_UNIX_FD
dbus_watch_get_unix_fd(watch),
#else
dbus_watch_get_fd(watch),
#endif
get_watch_flags(watch), handle_io_event, watch);
dbus_watch_set_data(watch, ev, NULL);
return TRUE;
}
/* DBusRemoveWatchFunction callback for pa mainloop */
static void remove_watch(DBusWatch *watch, void *data) {
pa_dbus_wrap_connection *c = data;
pa_io_event *ev;
pa_assert(watch);
pa_assert(c);
if ((ev = dbus_watch_get_data(watch)))
c->mainloop->io_free(ev);
}
/* DBusWatchToggledFunction callback for pa mainloop */
static void toggle_watch(DBusWatch *watch, void *data) {
pa_dbus_wrap_connection *c = data;
pa_io_event *ev;
pa_assert(watch);
pa_assert(c);
pa_assert_se(ev = dbus_watch_get_data(watch));
/* get_watch_flags() checks if the watch is enabled */
c->mainloop->io_enable(ev, get_watch_flags(watch));
}
static void time_event_destroy_cb(pa_mainloop_api *a, pa_time_event *e, void *userdata) {
pa_xfree(userdata);
}
/* DBusAddTimeoutFunction callback for pa mainloop */
static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) {
pa_dbus_wrap_connection *c = data;
pa_time_event *ev;
struct timeval tv;
struct timeout_data *d;
pa_assert(timeout);
pa_assert(c);
if (!dbus_timeout_get_enabled(timeout))
return FALSE;
d = pa_xnew(struct timeout_data, 1);
d->connection = c;
d->timeout = timeout;
ev = c->mainloop->time_new(c->mainloop, pa_timeval_rtstore(&tv, pa_rtclock_now() + dbus_timeout_get_interval(timeout) * PA_USEC_PER_MSEC, c->use_rtclock), handle_time_event, d);
c->mainloop->time_set_destroy(ev, time_event_destroy_cb);
dbus_timeout_set_data(timeout, ev, NULL);
return TRUE;
}
/* DBusRemoveTimeoutFunction callback for pa mainloop */
static void remove_timeout(DBusTimeout *timeout, void *data) {
pa_dbus_wrap_connection *c = data;
pa_time_event *ev;
pa_assert(timeout);
pa_assert(c);
if ((ev = dbus_timeout_get_data(timeout)))
c->mainloop->time_free(ev);
}
/* DBusTimeoutToggledFunction callback for pa mainloop */
static void toggle_timeout(DBusTimeout *timeout, void *data) {
struct timeout_data *d = data;
pa_time_event *ev;
struct timeval tv;
pa_assert(d);
pa_assert(d->connection);
pa_assert(timeout);
pa_assert_se(ev = dbus_timeout_get_data(timeout));
if (dbus_timeout_get_enabled(timeout))
d->connection->mainloop->time_restart(ev, pa_timeval_rtstore(&tv, pa_rtclock_now() + dbus_timeout_get_interval(timeout) * PA_USEC_PER_MSEC, d->connection->use_rtclock));
else
d->connection->mainloop->time_restart(ev, pa_timeval_rtstore(&tv, PA_USEC_INVALID, d->connection->use_rtclock));
}
static void wakeup_main(void *userdata) {
pa_dbus_wrap_connection *c = userdata;
pa_assert(c);
/* this will wakeup the mainloop and dispatch events, although
* it may not be the cleanest way of accomplishing it */
c->mainloop->defer_enable(c->dispatch_event, 1);
}
pa_dbus_wrap_connection* pa_dbus_wrap_connection_new(pa_mainloop_api *m, bool use_rtclock, DBusBusType type, DBusError *error) {
DBusConnection *conn;
pa_dbus_wrap_connection *pconn;
char *id;
pa_assert(type == DBUS_BUS_SYSTEM || type == DBUS_BUS_SESSION || type == DBUS_BUS_STARTER);
if (!(conn = dbus_bus_get_private(type, error)))
return NULL;
pconn = pa_xnew(pa_dbus_wrap_connection, 1);
pconn->mainloop = m;
pconn->connection = conn;
pconn->use_rtclock = use_rtclock;
dbus_connection_set_exit_on_disconnect(conn, FALSE);
dbus_connection_set_dispatch_status_function(conn, dispatch_status, pconn, NULL);
dbus_connection_set_watch_functions(conn, add_watch, remove_watch, toggle_watch, pconn, NULL);
dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout, toggle_timeout, pconn, NULL);
dbus_connection_set_wakeup_main_function(conn, wakeup_main, pconn, NULL);
pconn->dispatch_event = pconn->mainloop->defer_new(pconn->mainloop, dispatch_cb, conn);
pa_log_debug("Successfully connected to D-Bus %s bus %s as %s",
type == DBUS_BUS_SYSTEM ? "system" : (type == DBUS_BUS_SESSION ? "session" : "starter"),
pa_strnull((id = dbus_connection_get_server_id(conn))),
pa_strnull(dbus_bus_get_unique_name(conn)));
dbus_free(id);
return pconn;
}
pa_dbus_wrap_connection* pa_dbus_wrap_connection_new_from_existing(
pa_mainloop_api *m,
bool use_rtclock,
DBusConnection *conn) {
pa_dbus_wrap_connection *pconn;
pa_assert(m);
pa_assert(conn);
pconn = pa_xnew(pa_dbus_wrap_connection, 1);
pconn->mainloop = m;
pconn->connection = dbus_connection_ref(conn);
pconn->use_rtclock = use_rtclock;
dbus_connection_set_exit_on_disconnect(conn, FALSE);
dbus_connection_set_dispatch_status_function(conn, dispatch_status, pconn, NULL);
dbus_connection_set_watch_functions(conn, add_watch, remove_watch, toggle_watch, pconn, NULL);
dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout, toggle_timeout, pconn, NULL);
dbus_connection_set_wakeup_main_function(conn, wakeup_main, pconn, NULL);
pconn->dispatch_event = pconn->mainloop->defer_new(pconn->mainloop, dispatch_cb, conn);
return pconn;
}
void pa_dbus_wrap_connection_free(pa_dbus_wrap_connection* c) {
pa_assert(c);
if (dbus_connection_get_is_connected(c->connection)) {
dbus_connection_close(c->connection);
/* must process remaining messages, bit of a kludge to handle
* both unload and shutdown */
while (dbus_connection_read_write_dispatch(c->connection, -1))
;
}
c->mainloop->defer_free(c->dispatch_event);
dbus_connection_unref(c->connection);
pa_xfree(c);
}
DBusConnection* pa_dbus_wrap_connection_get(pa_dbus_wrap_connection *c) {
pa_assert(c);
pa_assert(c->connection);
return c->connection;
}
int pa_dbus_add_matches(DBusConnection *c, DBusError *error, ...) {
const char *t;
va_list ap;
unsigned k = 0;
pa_assert(c);
pa_assert(error);
va_start(ap, error);
while ((t = va_arg(ap, const char*))) {
dbus_bus_add_match(c, t, error);
if (dbus_error_is_set(error))
goto fail;
k++;
}
va_end(ap);
return 0;
fail:
va_end(ap);
va_start(ap, error);
for (; k > 0; k--) {
pa_assert_se(t = va_arg(ap, const char*));
dbus_bus_remove_match(c, t, NULL);
}
va_end(ap);
return -1;
}
void pa_dbus_remove_matches(DBusConnection *c, ...) {
const char *t;
va_list ap;
pa_assert(c);
va_start(ap, c);
while ((t = va_arg(ap, const char*)))
dbus_bus_remove_match(c, t, NULL);
va_end(ap);
}
pa_dbus_pending *pa_dbus_pending_new(
DBusConnection *c,
DBusMessage *m,
DBusPendingCall *pending,
void *context_data,
void *call_data) {
pa_dbus_pending *p;
pa_assert(pending);
p = pa_xnew(pa_dbus_pending, 1);
p->connection = c;
p->message = m;
p->pending = pending;
p->context_data = context_data;
p->call_data = call_data;
PA_LLIST_INIT(pa_dbus_pending, p);
return p;
}
void pa_dbus_pending_free(pa_dbus_pending *p) {
pa_assert(p);
if (p->pending) {
dbus_pending_call_cancel(p->pending);
dbus_pending_call_unref(p->pending);
}
if (p->message)
dbus_message_unref(p->message);
pa_xfree(p);
}
void pa_dbus_sync_pending_list(pa_dbus_pending **p) {
pa_assert(p);
while (*p && dbus_connection_read_write_dispatch((*p)->connection, -1))
;
}
void pa_dbus_free_pending_list(pa_dbus_pending **p) {
pa_dbus_pending *i;
pa_assert(p);
while ((i = *p)) {
PA_LLIST_REMOVE(pa_dbus_pending, *p, i);
pa_dbus_pending_free(i);
}
}
const char *pa_dbus_get_error_message(DBusMessage *m) {
const char *message;
pa_assert(m);
pa_assert(dbus_message_get_type(m) == DBUS_MESSAGE_TYPE_ERROR);
if (dbus_message_get_signature(m)[0] != 's')
return "<no explanation>";
pa_assert_se(dbus_message_get_args(m, NULL, DBUS_TYPE_STRING, &message, DBUS_TYPE_INVALID));
return message;
}
void pa_dbus_send_error(DBusConnection *c, DBusMessage *in_reply_to, const char *name, const char *format, ...) {
va_list ap;
char *message;
DBusMessage *reply = NULL;
pa_assert(c);
pa_assert(in_reply_to);
pa_assert(name);
pa_assert(format);
va_start(ap, format);
message = pa_vsprintf_malloc(format, ap);
va_end(ap);
pa_assert_se((reply = dbus_message_new_error(in_reply_to, name, message)));
pa_assert_se(dbus_connection_send(c, reply, NULL));
dbus_message_unref(reply);
pa_xfree(message);
}
void pa_dbus_send_empty_reply(DBusConnection *c, DBusMessage *in_reply_to) {
DBusMessage *reply = NULL;
pa_assert(c);
pa_assert(in_reply_to);
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
pa_assert_se(dbus_connection_send(c, reply, NULL));
dbus_message_unref(reply);
}
void pa_dbus_send_basic_value_reply(DBusConnection *c, DBusMessage *in_reply_to, int type, void *data) {
DBusMessage *reply = NULL;
pa_assert(c);
pa_assert(in_reply_to);
pa_assert(dbus_type_is_basic(type));
pa_assert(data);
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
pa_assert_se(dbus_message_append_args(reply, type, data, DBUS_TYPE_INVALID));
pa_assert_se(dbus_connection_send(c, reply, NULL));
dbus_message_unref(reply);
}
static const char *signature_from_basic_type(int type) {
switch (type) {
case DBUS_TYPE_BOOLEAN: return DBUS_TYPE_BOOLEAN_AS_STRING;
case DBUS_TYPE_BYTE: return DBUS_TYPE_BYTE_AS_STRING;
case DBUS_TYPE_INT16: return DBUS_TYPE_INT16_AS_STRING;
case DBUS_TYPE_UINT16: return DBUS_TYPE_UINT16_AS_STRING;
case DBUS_TYPE_INT32: return DBUS_TYPE_INT32_AS_STRING;
case DBUS_TYPE_UINT32: return DBUS_TYPE_UINT32_AS_STRING;
case DBUS_TYPE_INT64: return DBUS_TYPE_INT64_AS_STRING;
case DBUS_TYPE_UINT64: return DBUS_TYPE_UINT64_AS_STRING;
case DBUS_TYPE_DOUBLE: return DBUS_TYPE_DOUBLE_AS_STRING;
case DBUS_TYPE_STRING: return DBUS_TYPE_STRING_AS_STRING;
case DBUS_TYPE_OBJECT_PATH: return DBUS_TYPE_OBJECT_PATH_AS_STRING;
case DBUS_TYPE_SIGNATURE: return DBUS_TYPE_SIGNATURE_AS_STRING;
default: pa_assert_not_reached();
}
}
void pa_dbus_send_basic_variant_reply(DBusConnection *c, DBusMessage *in_reply_to, int type, void *data) {
DBusMessage *reply = NULL;
DBusMessageIter msg_iter;
DBusMessageIter variant_iter;
pa_assert(c);
pa_assert(in_reply_to);
pa_assert(dbus_type_is_basic(type));
pa_assert(data);
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
dbus_message_iter_init_append(reply, &msg_iter);
pa_assert_se(dbus_message_iter_open_container(&msg_iter,
DBUS_TYPE_VARIANT,
signature_from_basic_type(type),
&variant_iter));
pa_assert_se(dbus_message_iter_append_basic(&variant_iter, type, data));
pa_assert_se(dbus_message_iter_close_container(&msg_iter, &variant_iter));
pa_assert_se(dbus_connection_send(c, reply, NULL));
dbus_message_unref(reply);
}
/* Note: returns sizeof(char*) for strings, object paths and signatures. */
static unsigned basic_type_size(int type) {
switch (type) {
case DBUS_TYPE_BOOLEAN: return sizeof(dbus_bool_t);
case DBUS_TYPE_BYTE: return 1;
case DBUS_TYPE_INT16: return sizeof(dbus_int16_t);
case DBUS_TYPE_UINT16: return sizeof(dbus_uint16_t);
case DBUS_TYPE_INT32: return sizeof(dbus_int32_t);
case DBUS_TYPE_UINT32: return sizeof(dbus_uint32_t);
case DBUS_TYPE_INT64: return sizeof(dbus_int64_t);
case DBUS_TYPE_UINT64: return sizeof(dbus_uint64_t);
case DBUS_TYPE_DOUBLE: return sizeof(double);
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE: return sizeof(char*);
default: pa_assert_not_reached();
}
}
void pa_dbus_send_basic_array_variant_reply(
DBusConnection *c,
DBusMessage *in_reply_to,
int item_type,
void *array,
unsigned n) {
DBusMessage *reply = NULL;
DBusMessageIter msg_iter;
pa_assert(c);
pa_assert(in_reply_to);
pa_assert(dbus_type_is_basic(item_type));
pa_assert(array || n == 0);
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
dbus_message_iter_init_append(reply, &msg_iter);
pa_dbus_append_basic_array_variant(&msg_iter, item_type, array, n);
pa_assert_se(dbus_connection_send(c, reply, NULL));
dbus_message_unref(reply);
}
void pa_dbus_send_proplist_variant_reply(DBusConnection *c, DBusMessage *in_reply_to, pa_proplist *proplist) {
DBusMessage *reply = NULL;
DBusMessageIter msg_iter;
pa_assert(c);
pa_assert(in_reply_to);
pa_assert(proplist);
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
dbus_message_iter_init_append(reply, &msg_iter);
pa_dbus_append_proplist_variant(&msg_iter, proplist);
pa_assert_se(dbus_connection_send(c, reply, NULL));
dbus_message_unref(reply);
}
void pa_dbus_append_basic_array(DBusMessageIter *iter, int item_type, const void *array, unsigned n) {
DBusMessageIter array_iter;
unsigned i;
unsigned item_size;
pa_assert(iter);
pa_assert(dbus_type_is_basic(item_type));
pa_assert(array || n == 0);
item_size = basic_type_size(item_type);
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, signature_from_basic_type(item_type), &array_iter));
for (i = 0; i < n; ++i)
pa_assert_se(dbus_message_iter_append_basic(&array_iter, item_type, &((uint8_t*) array)[i * item_size]));
pa_assert_se(dbus_message_iter_close_container(iter, &array_iter));
}
void pa_dbus_append_basic_variant(DBusMessageIter *iter, int type, void *data) {
DBusMessageIter variant_iter;
pa_assert(iter);
pa_assert(dbus_type_is_basic(type));
pa_assert(data);
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, signature_from_basic_type(type), &variant_iter));
pa_assert_se(dbus_message_iter_append_basic(&variant_iter, type, data));
pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
}
void pa_dbus_append_basic_array_variant(DBusMessageIter *iter, int item_type, const void *array, unsigned n) {
DBusMessageIter variant_iter;
char *array_signature;
pa_assert(iter);
pa_assert(dbus_type_is_basic(item_type));
pa_assert(array || n == 0);
array_signature = pa_sprintf_malloc("a%c", *signature_from_basic_type(item_type));
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, array_signature, &variant_iter));
pa_dbus_append_basic_array(&variant_iter, item_type, array, n);
pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
pa_xfree(array_signature);
}
void pa_dbus_append_basic_variant_dict_entry(DBusMessageIter *dict_iter, const char *key, int type, void *data) {
DBusMessageIter dict_entry_iter;
pa_assert(dict_iter);
pa_assert(key);
pa_assert(dbus_type_is_basic(type));
pa_assert(data);
pa_assert_se(dbus_message_iter_open_container(dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
pa_dbus_append_basic_variant(&dict_entry_iter, type, data);
pa_assert_se(dbus_message_iter_close_container(dict_iter, &dict_entry_iter));
}
void pa_dbus_append_basic_array_variant_dict_entry(
DBusMessageIter *dict_iter,
const char *key,
int item_type,
const void *array,
unsigned n) {
DBusMessageIter dict_entry_iter;
pa_assert(dict_iter);
pa_assert(key);
pa_assert(dbus_type_is_basic(item_type));
pa_assert(array || n == 0);
pa_assert_se(dbus_message_iter_open_container(dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
pa_dbus_append_basic_array_variant(&dict_entry_iter, item_type, array, n);
pa_assert_se(dbus_message_iter_close_container(dict_iter, &dict_entry_iter));
}
void pa_dbus_append_proplist(DBusMessageIter *iter, pa_proplist *proplist) {
DBusMessageIter dict_iter;
DBusMessageIter dict_entry_iter;
DBusMessageIter array_iter;
void *state = NULL;
const char *key;
pa_assert(iter);
pa_assert(proplist);
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{say}", &dict_iter));
while ((key = pa_proplist_iterate(proplist, &state))) {
const void *value = NULL;
size_t nbytes;
pa_assert_se(pa_proplist_get(proplist, key, &value, &nbytes) >= 0);
pa_assert_se(dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
pa_assert_se(dbus_message_iter_open_container(&dict_entry_iter, DBUS_TYPE_ARRAY, "y", &array_iter));
pa_assert_se(dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE, &value, nbytes));
pa_assert_se(dbus_message_iter_close_container(&dict_entry_iter, &array_iter));
pa_assert_se(dbus_message_iter_close_container(&dict_iter, &dict_entry_iter));
}
pa_assert_se(dbus_message_iter_close_container(iter, &dict_iter));
}
void pa_dbus_append_proplist_variant(DBusMessageIter *iter, pa_proplist *proplist) {
DBusMessageIter variant_iter;
pa_assert(iter);
pa_assert(proplist);
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{say}", &variant_iter));
pa_dbus_append_proplist(&variant_iter, proplist);
pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
}
void pa_dbus_append_proplist_variant_dict_entry(DBusMessageIter *dict_iter, const char *key, pa_proplist *proplist) {
DBusMessageIter dict_entry_iter;
pa_assert(dict_iter);
pa_assert(key);
pa_assert(proplist);
pa_assert_se(dbus_message_iter_open_container(dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
pa_dbus_append_proplist_variant(&dict_entry_iter, proplist);
pa_assert_se(dbus_message_iter_close_container(dict_iter, &dict_entry_iter));
}
pa_proplist *pa_dbus_get_proplist_arg(DBusConnection *c, DBusMessage *msg, DBusMessageIter *iter) {
DBusMessageIter dict_iter;
DBusMessageIter dict_entry_iter;
pa_proplist *proplist = NULL;
const char *key = NULL;
const uint8_t *value = NULL;
int value_length = 0;
pa_assert(c);
pa_assert(msg);
pa_assert(iter);
pa_assert(pa_streq(dbus_message_iter_get_signature(iter), "a{say}"));
proplist = pa_proplist_new();
dbus_message_iter_recurse(iter, &dict_iter);
while (dbus_message_iter_get_arg_type(&dict_iter) != DBUS_TYPE_INVALID) {
dbus_message_iter_recurse(&dict_iter, &dict_entry_iter);
dbus_message_iter_get_basic(&dict_entry_iter, &key);
dbus_message_iter_next(&dict_entry_iter);
if (strlen(key) <= 0 || !pa_ascii_valid(key)) {
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Invalid property list key: '%s'.", key);
goto fail;
}
dbus_message_iter_get_fixed_array(&dict_entry_iter, &value, &value_length);
pa_assert(value_length >= 0);
pa_assert_se(pa_proplist_set(proplist, key, value, value_length) >= 0);
dbus_message_iter_next(&dict_iter);
}
dbus_message_iter_next(iter);
return proplist;
fail:
if (proplist)
pa_proplist_free(proplist);
return NULL;
}
|