intended-roles: fix symdef header inclusion
[profile/ivi/pulseaudio-panda.git] / src / modules / module-intended-roles.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2009 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulse/volume.h>
36 #include <pulse/timeval.h>
37 #include <pulse/util.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/core-subscribe.h>
45 #include <pulsecore/sink-input.h>
46 #include <pulsecore/source-output.h>
47 #include <pulsecore/namereg.h>
48 #include <pulsecore/protocol-native.h>
49 #include <pulsecore/pstream.h>
50 #include <pulsecore/pstream-util.h>
51 #include <pulsecore/database.h>
52
53 #include "module-intended-roles-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering");
56 PA_MODULE_DESCRIPTION("Automatically set device of streams based of intended roles of devices");
57 PA_MODULE_VERSION(PACKAGE_VERSION);
58 PA_MODULE_LOAD_ONCE(TRUE);
59 PA_MODULE_USAGE(
60         "on_hotplug=<When new device becomes available, recheck streams?> "
61         "on_rescue=<When device becomes unavailable, recheck streams?>");
62
63 static const char* const valid_modargs[] = {
64     "on_hotplug",
65     "on_rescue",
66     NULL
67 };
68
69 struct userdata {
70     pa_core *core;
71     pa_module *module;
72     pa_hook_slot
73         *sink_input_new_hook_slot,
74         *source_output_new_hook_slot,
75         *sink_put_hook_slot,
76         *source_put_hook_slot,
77         *sink_unlink_hook_slot,
78         *source_unlink_hook_slot;
79
80     pa_bool_t on_hotplug:1;
81     pa_bool_t on_rescue:1;
82 };
83
84 static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
85     const char *ir;
86     char *r;
87     const char *state;
88
89     if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES)))
90         return FALSE;
91
92     while ((r = pa_split_spaces(ir, &state))) {
93
94         if (pa_streq(role, r)) {
95             pa_xfree(r);
96             return TRUE;
97         }
98
99         pa_xfree(r);
100     }
101
102     return FALSE;
103 }
104
105 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
106     const char *role;
107     pa_sink *s, *def;
108     uint32_t idx;
109
110     pa_assert(c);
111     pa_assert(new_data);
112     pa_assert(u);
113
114     if (!new_data->proplist) {
115         pa_log_debug("New stream lacks property data.");
116         return PA_HOOK_OK;
117     }
118
119     if (new_data->sink) {
120         pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
121         return PA_HOOK_OK;
122     }
123
124     if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
125         pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
126         return PA_HOOK_OK;
127     }
128
129     /* Prefer the default sink over any other sink, just in case... */
130     if ((def = pa_namereg_get_default_sink(c)))
131         if (role_match(def->proplist, role)) {
132             new_data->sink = def;
133             new_data->save_sink = FALSE;
134             return PA_HOOK_OK;
135         }
136
137     PA_IDXSET_FOREACH(s, c->sinks, idx) {
138         if (s == def)
139             continue;
140
141         if (role_match(s->proplist, role)) {
142             new_data->sink = s;
143             new_data->save_sink = FALSE;
144             return PA_HOOK_OK;
145         }
146     }
147
148     return PA_HOOK_OK;
149 }
150
151 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
152     const char *role;
153     pa_source *s, *def;
154     uint32_t idx;
155
156     pa_assert(c);
157     pa_assert(new_data);
158     pa_assert(u);
159
160     if (!new_data->proplist) {
161         pa_log_debug("New stream lacks property data.");
162         return PA_HOOK_OK;
163     }
164
165     if (new_data->source) {
166         pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
167         return PA_HOOK_OK;
168     }
169
170     if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
171         pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
172         return PA_HOOK_OK;
173     }
174
175     /* Prefer the default source over any other source, just in case... */
176     if ((def = pa_namereg_get_default_source(c)))
177         if (role_match(def->proplist, role)) {
178             new_data->source = def;
179             new_data->save_source = FALSE;
180             return PA_HOOK_OK;
181         }
182
183     PA_IDXSET_FOREACH(s, c->sources, idx) {
184         if (s == def)
185             continue;
186
187         if (role_match(s->proplist, role)) {
188             new_data->source = s;
189             new_data->save_source = FALSE;
190             return PA_HOOK_OK;
191         }
192     }
193
194     return PA_HOOK_OK;
195 }
196
197 static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
198     pa_sink_input *si;
199     uint32_t idx;
200
201     pa_assert(c);
202     pa_assert(sink);
203     pa_assert(u);
204     pa_assert(u->on_hotplug);
205
206     PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
207         const char *role;
208
209         if (si->sink == sink)
210             continue;
211
212         if (si->save_sink)
213             continue;
214
215         if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
216             continue;
217
218         if (role_match(si->sink->proplist, role))
219             continue;
220
221         if (!role_match(sink->proplist, role))
222             continue;
223
224         pa_sink_input_move_to(si, sink, FALSE);
225     }
226
227     return PA_HOOK_OK;
228 }
229
230 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
231     pa_source_output *so;
232     uint32_t idx;
233
234     pa_assert(c);
235     pa_assert(source);
236     pa_assert(u);
237     pa_assert(u->on_hotplug);
238
239     PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
240         const char *role;
241
242         if (so->source == source)
243             continue;
244
245         if (so->save_source)
246             continue;
247
248         if (so->direct_on_input)
249             continue;
250
251         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
252             continue;
253
254         if (role_match(so->source->proplist, role))
255             continue;
256
257         if (!role_match(source->proplist, role))
258             continue;
259
260         pa_source_output_move_to(so, source, FALSE);
261     }
262
263     return PA_HOOK_OK;
264 }
265
266 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
267     pa_sink_input *si;
268     uint32_t idx;
269     pa_sink *def;
270
271     pa_assert(c);
272     pa_assert(sink);
273     pa_assert(u);
274     pa_assert(u->on_rescue);
275
276     /* There's no point in doing anything if the core is shut down anyway */
277     if (c->state == PA_CORE_SHUTDOWN)
278         return PA_HOOK_OK;
279
280     /* If there not default sink, then there is no sink at all */
281     if (!(def = pa_namereg_get_default_sink(c)))
282         return PA_HOOK_OK;
283
284     PA_IDXSET_FOREACH(si, sink->inputs, idx) {
285         const char *role;
286         uint32_t jdx;
287         pa_sink *d;
288
289         if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
290             continue;
291
292         /* Would the default sink fit? If so, let's use it */
293         if (def != sink && role_match(def->proplist, role)) {
294             pa_sink_input_move_to(si, def, FALSE);
295             continue;
296         }
297
298         /* Try to find some other fitting sink */
299         PA_IDXSET_FOREACH(d, c->sinks, jdx) {
300             if (d == def || d == sink)
301                 continue;
302
303             if (role_match(d->proplist, role)) {
304                 pa_sink_input_move_to(si, d, FALSE);
305                 break;
306             }
307         }
308     }
309
310     return PA_HOOK_OK;
311 }
312
313 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
314     pa_source_output *so;
315     uint32_t idx;
316     pa_source *def;
317
318     pa_assert(c);
319     pa_assert(source);
320     pa_assert(u);
321     pa_assert(u->on_rescue);
322
323     /* There's no point in doing anything if the core is shut down anyway */
324     if (c->state == PA_CORE_SHUTDOWN)
325         return PA_HOOK_OK;
326
327     /* If there not default source, then there is no source at all */
328     if (!(def = pa_namereg_get_default_source(c)))
329         return PA_HOOK_OK;
330
331     PA_IDXSET_FOREACH(so, source->outputs, idx) {
332         const char *role;
333         uint32_t jdx;
334         pa_source *d;
335
336         if (so->direct_on_input)
337             continue;
338
339         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
340             continue;
341
342         /* Would the default source fit? If so, let's use it */
343         if (def != source && role_match(def->proplist, role) && !source->monitor_of == !def->monitor_of) {
344             pa_source_output_move_to(so, def, FALSE);
345             continue;
346         }
347
348         /* Try to find some other fitting source */
349         PA_IDXSET_FOREACH(d, c->sources, jdx) {
350             if (d == def || d == source)
351                 continue;
352
353             if (role_match(d->proplist, role) && !source->monitor_of == !d->monitor_of) {
354                 pa_source_output_move_to(so, d, FALSE);
355                 break;
356             }
357         }
358     }
359
360     return PA_HOOK_OK;
361 }
362
363 int pa__init(pa_module*m) {
364     pa_modargs *ma = NULL;
365     struct userdata *u;
366     pa_bool_t on_hotplug = TRUE, on_rescue = TRUE;
367
368     pa_assert(m);
369
370     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
371         pa_log("Failed to parse module arguments");
372         goto fail;
373     }
374
375     if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
376         pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
377         pa_log("on_hotplug= and on_rescue= expect boolean arguments");
378         goto fail;
379     }
380
381     m->userdata = u = pa_xnew0(struct userdata, 1);
382     u->core = m->core;
383     u->module = m;
384     u->on_hotplug = on_hotplug;
385     u->on_rescue = on_rescue;
386
387     /* A little bit later than module-stream-restore */
388     u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) sink_input_new_hook_callback, u);
389     u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) source_output_new_hook_callback, u);
390
391     if (on_hotplug) {
392         /* A little bit later than module-stream-restore */
393         u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_put_hook_callback, u);
394         u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) source_put_hook_callback, u);
395     }
396
397     if (on_rescue) {
398         /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
399         u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_unlink_hook_callback, NULL);
400         u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) source_unlink_hook_callback, NULL);
401     }
402
403     pa_modargs_free(ma);
404     return 0;
405
406 fail:
407     pa__done(m);
408
409     if (ma)
410         pa_modargs_free(ma);
411
412     return  -1;
413 }
414
415 void pa__done(pa_module*m) {
416     struct userdata* u;
417
418     pa_assert(m);
419
420     if (!(u = m->userdata))
421         return;
422
423     if (u->sink_input_new_hook_slot)
424         pa_hook_slot_free(u->sink_input_new_hook_slot);
425     if (u->source_output_new_hook_slot)
426         pa_hook_slot_free(u->source_output_new_hook_slot);
427
428     if (u->sink_put_hook_slot)
429         pa_hook_slot_free(u->sink_put_hook_slot);
430     if (u->source_put_hook_slot)
431         pa_hook_slot_free(u->source_put_hook_slot);
432
433     if (u->sink_unlink_hook_slot)
434         pa_hook_slot_free(u->sink_unlink_hook_slot);
435     if (u->source_unlink_hook_slot)
436         pa_hook_slot_free(u->source_unlink_hook_slot);
437
438     pa_xfree(u);
439 }