2 This file is part of PulseAudio.
4 Copyright 2008 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulse/context.h>
27 #include <pulse/gccmacro.h>
28 #include <pulse/fork-detect.h>
29 #include <pulse/operation.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/pstream-util.h>
35 #include "ext-stream-restore.h"
46 static void ext_stream_restore_test_cb(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
47 pa_operation
*o
= userdata
;
48 uint32_t version
= PA_INVALID_INDEX
;
52 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
57 if (command
!= PA_COMMAND_REPLY
) {
58 if (pa_context_handle_error(o
->context
, command
, t
, FALSE
) < 0)
61 } else if (pa_tagstruct_getu32(t
, &version
) < 0 ||
62 !pa_tagstruct_eof(t
)) {
64 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
69 pa_ext_stream_restore_test_cb_t cb
= (pa_ext_stream_restore_test_cb_t
) o
->callback
;
70 cb(o
->context
, version
, o
->userdata
);
75 pa_operation_unref(o
);
78 pa_operation
*pa_ext_stream_restore_test(
80 pa_ext_stream_restore_test_cb_t cb
,
88 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
90 PA_CHECK_VALIDITY_RETURN_NULL(c
, !pa_detect_fork(), PA_ERR_FORKED
);
91 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
92 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 14, PA_ERR_NOTSUPPORTED
);
94 o
= pa_operation_new(c
, NULL
, (pa_operation_cb_t
) cb
, userdata
);
96 t
= pa_tagstruct_command(c
, PA_COMMAND_EXTENSION
, &tag
);
97 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
98 pa_tagstruct_puts(t
, "module-stream-restore");
99 pa_tagstruct_putu32(t
, SUBCOMMAND_TEST
);
100 pa_pstream_send_tagstruct(c
->pstream
, t
);
101 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, ext_stream_restore_test_cb
, pa_operation_ref(o
), (pa_free_cb_t
) pa_operation_unref
);
106 static void ext_stream_restore_read_cb(pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, pa_tagstruct
*t
, void *userdata
) {
107 pa_operation
*o
= userdata
;
112 pa_assert(PA_REFCNT_VALUE(o
) >= 1);
117 if (command
!= PA_COMMAND_REPLY
) {
118 if (pa_context_handle_error(o
->context
, command
, t
, FALSE
) < 0)
124 while (!pa_tagstruct_eof(t
)) {
125 pa_ext_stream_restore_info i
;
126 pa_bool_t mute
= FALSE
;
128 memset(&i
, 0, sizeof(i
));
130 if (pa_tagstruct_gets(t
, &i
.name
) < 0 ||
131 pa_tagstruct_get_channel_map(t
, &i
.channel_map
) < 0 ||
132 pa_tagstruct_get_cvolume(t
, &i
.volume
) < 0 ||
133 pa_tagstruct_gets(t
, &i
.device
) < 0 ||
134 pa_tagstruct_get_boolean(t
, &mute
) < 0) {
136 pa_context_fail(o
->context
, PA_ERR_PROTOCOL
);
143 pa_ext_stream_restore_read_cb_t cb
= (pa_ext_stream_restore_read_cb_t
) o
->callback
;
144 cb(o
->context
, &i
, 0, o
->userdata
);
150 pa_ext_stream_restore_read_cb_t cb
= (pa_ext_stream_restore_read_cb_t
) o
->callback
;
151 cb(o
->context
, NULL
, eol
, o
->userdata
);
155 pa_operation_done(o
);
156 pa_operation_unref(o
);
159 pa_operation
*pa_ext_stream_restore_read(
161 pa_ext_stream_restore_read_cb_t cb
,
169 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
171 PA_CHECK_VALIDITY_RETURN_NULL(c
, !pa_detect_fork(), PA_ERR_FORKED
);
172 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
173 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 14, PA_ERR_NOTSUPPORTED
);
175 o
= pa_operation_new(c
, NULL
, (pa_operation_cb_t
) cb
, userdata
);
177 t
= pa_tagstruct_command(c
, PA_COMMAND_EXTENSION
, &tag
);
178 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
179 pa_tagstruct_puts(t
, "module-stream-restore");
180 pa_tagstruct_putu32(t
, SUBCOMMAND_READ
);
181 pa_pstream_send_tagstruct(c
->pstream
, t
);
182 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, ext_stream_restore_read_cb
, pa_operation_ref(o
), (pa_free_cb_t
) pa_operation_unref
);
187 pa_operation
*pa_ext_stream_restore_write(
189 pa_update_mode_t mode
,
190 const pa_ext_stream_restore_info data
[],
192 int apply_immediately
,
193 pa_context_success_cb_t cb
,
197 pa_operation
*o
= NULL
;
198 pa_tagstruct
*t
= NULL
;
201 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
202 pa_assert(mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
|| mode
== PA_UPDATE_SET
);
205 PA_CHECK_VALIDITY_RETURN_NULL(c
, !pa_detect_fork(), PA_ERR_FORKED
);
206 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
207 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 14, PA_ERR_NOTSUPPORTED
);
209 o
= pa_operation_new(c
, NULL
, (pa_operation_cb_t
) cb
, userdata
);
211 t
= pa_tagstruct_command(c
, PA_COMMAND_EXTENSION
, &tag
);
212 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
213 pa_tagstruct_puts(t
, "module-stream-restore");
214 pa_tagstruct_putu32(t
, SUBCOMMAND_WRITE
);
216 pa_tagstruct_putu32(t
, mode
);
217 pa_tagstruct_put_boolean(t
, apply_immediately
);
219 for (; n
> 0; n
--, data
++) {
220 if (!data
->name
|| !*data
->name
)
223 pa_tagstruct_puts(t
, data
->name
);
225 if (data
->volume
.channels
> 0 &&
226 !pa_cvolume_compatible_with_channel_map(&data
->volume
, &data
->channel_map
))
229 pa_tagstruct_put_channel_map(t
, &data
->channel_map
);
230 pa_tagstruct_put_cvolume(t
, &data
->volume
);
231 pa_tagstruct_puts(t
, data
->device
);
232 pa_tagstruct_put_boolean(t
, data
->mute
);
235 pa_pstream_send_tagstruct(c
->pstream
, t
);
236 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_context_simple_ack_callback
, pa_operation_ref(o
), (pa_free_cb_t
) pa_operation_unref
);
241 pa_operation_cancel(o
);
242 pa_operation_unref(o
);
244 pa_tagstruct_free(t
);
246 pa_context_set_error(c
, PA_ERR_INVALID
);
250 pa_operation
*pa_ext_stream_restore_delete(
252 const char *const s
[],
253 pa_context_success_cb_t cb
,
257 pa_operation
*o
= NULL
;
258 pa_tagstruct
*t
= NULL
;
259 const char *const *k
;
262 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
265 PA_CHECK_VALIDITY_RETURN_NULL(c
, !pa_detect_fork(), PA_ERR_FORKED
);
266 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
267 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 14, PA_ERR_NOTSUPPORTED
);
269 o
= pa_operation_new(c
, NULL
, (pa_operation_cb_t
) cb
, userdata
);
271 t
= pa_tagstruct_command(c
, PA_COMMAND_EXTENSION
, &tag
);
272 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
273 pa_tagstruct_puts(t
, "module-stream-restore");
274 pa_tagstruct_putu32(t
, SUBCOMMAND_DELETE
);
276 for (k
= s
; *k
; k
++) {
280 pa_tagstruct_puts(t
, *k
);
283 pa_pstream_send_tagstruct(c
->pstream
, t
);
284 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_context_simple_ack_callback
, pa_operation_ref(o
), (pa_free_cb_t
) pa_operation_unref
);
289 pa_operation_cancel(o
);
290 pa_operation_unref(o
);
292 pa_tagstruct_free(t
);
294 pa_context_set_error(c
, PA_ERR_INVALID
);
298 pa_operation
*pa_ext_stream_restore_subscribe(
301 pa_context_success_cb_t cb
,
309 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
311 PA_CHECK_VALIDITY_RETURN_NULL(c
, !pa_detect_fork(), PA_ERR_FORKED
);
312 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->state
== PA_CONTEXT_READY
, PA_ERR_BADSTATE
);
313 PA_CHECK_VALIDITY_RETURN_NULL(c
, c
->version
>= 14, PA_ERR_NOTSUPPORTED
);
315 o
= pa_operation_new(c
, NULL
, (pa_operation_cb_t
) cb
, userdata
);
317 t
= pa_tagstruct_command(c
, PA_COMMAND_EXTENSION
, &tag
);
318 pa_tagstruct_putu32(t
, PA_INVALID_INDEX
);
319 pa_tagstruct_puts(t
, "module-stream-restore");
320 pa_tagstruct_putu32(t
, SUBCOMMAND_SUBSCRIBE
);
321 pa_tagstruct_put_boolean(t
, enable
);
322 pa_pstream_send_tagstruct(c
->pstream
, t
);
323 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_context_simple_ack_callback
, pa_operation_ref(o
), (pa_free_cb_t
) pa_operation_unref
);
328 void pa_ext_stream_restore_set_subscribe_cb(
330 pa_ext_stream_restore_subscribe_cb_t cb
,
334 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
336 if (pa_detect_fork())
339 c
->ext_stream_restore
.callback
= cb
;
340 c
->ext_stream_restore
.userdata
= userdata
;
343 void pa_ext_stream_restore_command(pa_context
*c
, uint32_t tag
, pa_tagstruct
*t
) {
347 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
350 if (pa_tagstruct_getu32(t
, &subcommand
) < 0 ||
351 !pa_tagstruct_eof(t
)) {
353 pa_context_fail(c
, PA_ERR_PROTOCOL
);
357 if (subcommand
!= SUBCOMMAND_EVENT
) {
358 pa_context_fail(c
, PA_ERR_PROTOCOL
);
362 if (c
->ext_stream_restore
.callback
)
363 c
->ext_stream_restore
.callback(c
, c
->ext_stream_restore
.userdata
);