Fix checking for NULL after usage
[pulseaudio-mirror.git] / src / pulse / ext-stream-restore.c
blob10e9fd5dd5915310ea691da6c5e2f05c2ac86d07
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <pulse/context.h>
27 #include <pulse/gccmacro.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/pstream-util.h>
32 #include "internal.h"
33 #include "operation.h"
34 #include "fork-detect.h"
36 #include "ext-stream-restore.h"
38 enum {
39 SUBCOMMAND_TEST,
40 SUBCOMMAND_READ,
41 SUBCOMMAND_WRITE,
42 SUBCOMMAND_DELETE,
43 SUBCOMMAND_SUBSCRIBE,
44 SUBCOMMAND_EVENT
47 static void ext_stream_restore_test_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
48 pa_operation *o = userdata;
49 uint32_t version = PA_INVALID_INDEX;
51 pa_assert(pd);
52 pa_assert(o);
53 pa_assert(PA_REFCNT_VALUE(o) >= 1);
55 if (!o->context)
56 goto finish;
58 if (command != PA_COMMAND_REPLY) {
59 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
60 goto finish;
62 } else if (pa_tagstruct_getu32(t, &version) < 0 ||
63 !pa_tagstruct_eof(t)) {
65 pa_context_fail(o->context, PA_ERR_PROTOCOL);
66 goto finish;
69 if (o->callback) {
70 pa_ext_stream_restore_test_cb_t cb = (pa_ext_stream_restore_test_cb_t) o->callback;
71 cb(o->context, version, o->userdata);
74 finish:
75 pa_operation_done(o);
76 pa_operation_unref(o);
79 pa_operation *pa_ext_stream_restore_test(
80 pa_context *c,
81 pa_ext_stream_restore_test_cb_t cb,
82 void *userdata) {
84 uint32_t tag;
85 pa_operation *o;
86 pa_tagstruct *t;
88 pa_assert(c);
89 pa_assert(PA_REFCNT_VALUE(c) >= 1);
91 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
92 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
93 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 14, PA_ERR_NOTSUPPORTED);
95 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
97 t = pa_tagstruct_command(c, PA_COMMAND_EXTENSION, &tag);
98 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
99 pa_tagstruct_puts(t, "module-stream-restore");
100 pa_tagstruct_putu32(t, SUBCOMMAND_TEST);
101 pa_pstream_send_tagstruct(c->pstream, t);
102 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);
104 return o;
107 static void ext_stream_restore_read_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
108 pa_operation *o = userdata;
109 int eol = 1;
111 pa_assert(pd);
112 pa_assert(o);
113 pa_assert(PA_REFCNT_VALUE(o) >= 1);
115 if (!o->context)
116 goto finish;
118 if (command != PA_COMMAND_REPLY) {
119 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
120 goto finish;
122 eol = -1;
123 } else {
125 while (!pa_tagstruct_eof(t)) {
126 pa_ext_stream_restore_info i;
127 pa_bool_t mute = FALSE;
129 memset(&i, 0, sizeof(i));
131 if (pa_tagstruct_gets(t, &i.name) < 0 ||
132 pa_tagstruct_get_channel_map(t, &i.channel_map) < 0 ||
133 pa_tagstruct_get_cvolume(t, &i.volume) < 0 ||
134 pa_tagstruct_gets(t, &i.device) < 0 ||
135 pa_tagstruct_get_boolean(t, &mute) < 0) {
137 pa_context_fail(o->context, PA_ERR_PROTOCOL);
138 goto finish;
141 i.mute = (int) mute;
143 if (o->callback) {
144 pa_ext_stream_restore_read_cb_t cb = (pa_ext_stream_restore_read_cb_t) o->callback;
145 cb(o->context, &i, 0, o->userdata);
150 if (o->callback) {
151 pa_ext_stream_restore_read_cb_t cb = (pa_ext_stream_restore_read_cb_t) o->callback;
152 cb(o->context, NULL, eol, o->userdata);
155 finish:
156 pa_operation_done(o);
157 pa_operation_unref(o);
160 pa_operation *pa_ext_stream_restore_read(
161 pa_context *c,
162 pa_ext_stream_restore_read_cb_t cb,
163 void *userdata) {
165 uint32_t tag;
166 pa_operation *o;
167 pa_tagstruct *t;
169 pa_assert(c);
170 pa_assert(PA_REFCNT_VALUE(c) >= 1);
172 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
173 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
174 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 14, PA_ERR_NOTSUPPORTED);
176 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
178 t = pa_tagstruct_command(c, PA_COMMAND_EXTENSION, &tag);
179 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
180 pa_tagstruct_puts(t, "module-stream-restore");
181 pa_tagstruct_putu32(t, SUBCOMMAND_READ);
182 pa_pstream_send_tagstruct(c->pstream, t);
183 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);
185 return o;
188 pa_operation *pa_ext_stream_restore_write(
189 pa_context *c,
190 pa_update_mode_t mode,
191 const pa_ext_stream_restore_info data[],
192 unsigned n,
193 int apply_immediately,
194 pa_context_success_cb_t cb,
195 void *userdata) {
197 uint32_t tag;
198 pa_operation *o = NULL;
199 pa_tagstruct *t = NULL;
201 pa_assert(c);
202 pa_assert(PA_REFCNT_VALUE(c) >= 1);
203 pa_assert(mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE || mode == PA_UPDATE_SET);
204 pa_assert(data);
206 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
207 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
208 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 14, PA_ERR_NOTSUPPORTED);
210 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
212 t = pa_tagstruct_command(c, PA_COMMAND_EXTENSION, &tag);
213 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
214 pa_tagstruct_puts(t, "module-stream-restore");
215 pa_tagstruct_putu32(t, SUBCOMMAND_WRITE);
217 pa_tagstruct_putu32(t, mode);
218 pa_tagstruct_put_boolean(t, apply_immediately);
220 for (; n > 0; n--, data++) {
221 if (!data->name || !*data->name)
222 goto fail;
224 pa_tagstruct_puts(t, data->name);
226 if (data->volume.channels > 0 &&
227 !pa_cvolume_compatible_with_channel_map(&data->volume, &data->channel_map))
228 goto fail;
230 pa_tagstruct_put_channel_map(t, &data->channel_map);
231 pa_tagstruct_put_cvolume(t, &data->volume);
232 pa_tagstruct_puts(t, data->device);
233 pa_tagstruct_put_boolean(t, data->mute);
236 pa_pstream_send_tagstruct(c->pstream, t);
237 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);
239 return o;
241 fail:
242 pa_operation_cancel(o);
243 pa_operation_unref(o);
245 pa_tagstruct_free(t);
247 pa_context_set_error(c, PA_ERR_INVALID);
248 return NULL;
251 pa_operation *pa_ext_stream_restore_delete(
252 pa_context *c,
253 const char *const s[],
254 pa_context_success_cb_t cb,
255 void *userdata) {
257 uint32_t tag;
258 pa_operation *o = NULL;
259 pa_tagstruct *t = NULL;
260 const char *const *k;
262 pa_assert(c);
263 pa_assert(PA_REFCNT_VALUE(c) >= 1);
264 pa_assert(s);
266 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
267 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
268 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 14, PA_ERR_NOTSUPPORTED);
270 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
272 t = pa_tagstruct_command(c, PA_COMMAND_EXTENSION, &tag);
273 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
274 pa_tagstruct_puts(t, "module-stream-restore");
275 pa_tagstruct_putu32(t, SUBCOMMAND_DELETE);
277 for (k = s; *k; k++) {
278 if (!*k || !**k)
279 goto fail;
281 pa_tagstruct_puts(t, *k);
284 pa_pstream_send_tagstruct(c->pstream, t);
285 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);
287 return o;
289 fail:
290 pa_operation_cancel(o);
291 pa_operation_unref(o);
293 pa_tagstruct_free(t);
295 pa_context_set_error(c, PA_ERR_INVALID);
296 return NULL;
299 pa_operation *pa_ext_stream_restore_subscribe(
300 pa_context *c,
301 int enable,
302 pa_context_success_cb_t cb,
303 void *userdata) {
305 uint32_t tag;
306 pa_operation *o;
307 pa_tagstruct *t;
309 pa_assert(c);
310 pa_assert(PA_REFCNT_VALUE(c) >= 1);
312 PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
313 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
314 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 14, PA_ERR_NOTSUPPORTED);
316 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
318 t = pa_tagstruct_command(c, PA_COMMAND_EXTENSION, &tag);
319 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
320 pa_tagstruct_puts(t, "module-stream-restore");
321 pa_tagstruct_putu32(t, SUBCOMMAND_SUBSCRIBE);
322 pa_tagstruct_put_boolean(t, enable);
323 pa_pstream_send_tagstruct(c->pstream, t);
324 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);
326 return o;
329 void pa_ext_stream_restore_set_subscribe_cb(
330 pa_context *c,
331 pa_ext_stream_restore_subscribe_cb_t cb,
332 void *userdata) {
334 pa_assert(c);
335 pa_assert(PA_REFCNT_VALUE(c) >= 1);
337 if (pa_detect_fork())
338 return;
340 c->ext_stream_restore.callback = cb;
341 c->ext_stream_restore.userdata = userdata;
344 void pa_ext_stream_restore_command(pa_context *c, uint32_t tag, pa_tagstruct *t) {
345 uint32_t subcommand;
347 pa_assert(c);
348 pa_assert(PA_REFCNT_VALUE(c) >= 1);
349 pa_assert(t);
351 if (pa_tagstruct_getu32(t, &subcommand) < 0 ||
352 !pa_tagstruct_eof(t)) {
354 pa_context_fail(c, PA_ERR_PROTOCOL);
355 return;
358 if (subcommand != SUBCOMMAND_EVENT) {
359 pa_context_fail(c, PA_ERR_PROTOCOL);
360 return;
363 if (c->ext_stream_restore.callback)
364 c->ext_stream_restore.callback(c, c->ext_stream_restore.userdata);