device-port: Create the profiles hashmap at initialization.
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / modules / module-intended-roles.c
blobd1e9b818e8d26e6c8ad48305f491d32e2f419713
1 /***
2 This file is part of PulseAudio.
4 Copyright 2009 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/xmalloc.h>
28 #include <pulsecore/module.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/source-output.h>
34 #include <pulsecore/namereg.h>
36 #include "module-intended-roles-symdef.h"
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Automatically set device of streams based of intended roles of devices");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(TRUE);
42 PA_MODULE_USAGE(
43 "on_hotplug=<When new device becomes available, recheck streams?> "
44 "on_rescue=<When device becomes unavailable, recheck streams?>");
46 static const char* const valid_modargs[] = {
47 "on_hotplug",
48 "on_rescue",
49 NULL
52 struct userdata {
53 pa_core *core;
54 pa_module *module;
56 pa_hook_slot
57 *sink_input_new_hook_slot,
58 *source_output_new_hook_slot,
59 *sink_put_hook_slot,
60 *source_put_hook_slot,
61 *sink_unlink_hook_slot,
62 *source_unlink_hook_slot;
64 pa_bool_t on_hotplug:1;
65 pa_bool_t on_rescue:1;
68 static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
69 return pa_str_in_list_spaces(pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES), role);
72 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
73 const char *role;
74 pa_sink *s, *def;
75 uint32_t idx;
77 pa_assert(c);
78 pa_assert(new_data);
79 pa_assert(u);
81 if (!new_data->proplist) {
82 pa_log_debug("New stream lacks property data.");
83 return PA_HOOK_OK;
86 if (new_data->sink) {
87 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
88 return PA_HOOK_OK;
91 if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
92 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)));
93 return PA_HOOK_OK;
96 /* Prefer the default sink over any other sink, just in case... */
97 if ((def = pa_namereg_get_default_sink(c)))
98 if (role_match(def->proplist, role) && pa_sink_input_new_data_set_sink(new_data, def, FALSE))
99 return PA_HOOK_OK;
101 /* @todo: favour the highest priority device, not the first one we find? */
102 PA_IDXSET_FOREACH(s, c->sinks, idx) {
103 if (s == def)
104 continue;
106 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)))
107 continue;
109 if (role_match(s->proplist, role) && pa_sink_input_new_data_set_sink(new_data, s, FALSE))
110 return PA_HOOK_OK;
113 return PA_HOOK_OK;
116 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
117 const char *role;
118 pa_source *s, *def;
119 uint32_t idx;
121 pa_assert(c);
122 pa_assert(new_data);
123 pa_assert(u);
125 if (!new_data->proplist) {
126 pa_log_debug("New stream lacks property data.");
127 return PA_HOOK_OK;
130 if (new_data->source) {
131 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
132 return PA_HOOK_OK;
135 if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
136 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)));
137 return PA_HOOK_OK;
140 /* Prefer the default source over any other source, just in case... */
141 if ((def = pa_namereg_get_default_source(c)))
142 if (role_match(def->proplist, role)) {
143 pa_source_output_new_data_set_source(new_data, def, FALSE);
144 return PA_HOOK_OK;
147 PA_IDXSET_FOREACH(s, c->sources, idx) {
148 if (s->monitor_of)
149 continue;
151 if (s == def)
152 continue;
154 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
155 continue;
157 /* @todo: favour the highest priority device, not the first one we find? */
158 if (role_match(s->proplist, role)) {
159 pa_source_output_new_data_set_source(new_data, s, FALSE);
160 return PA_HOOK_OK;
164 return PA_HOOK_OK;
167 static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
168 pa_sink_input *si;
169 uint32_t idx;
171 pa_assert(c);
172 pa_assert(sink);
173 pa_assert(u);
174 pa_assert(u->on_hotplug);
176 PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
177 const char *role;
179 if (si->sink == sink)
180 continue;
182 if (si->save_sink)
183 continue;
185 /* Skip this if it is already in the process of being moved
186 * anyway */
187 if (!si->sink)
188 continue;
190 /* It might happen that a stream and a sink are set up at the
191 same time, in which case we want to make sure we don't
192 interfere with that */
193 if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
194 continue;
196 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
197 continue;
199 if (role_match(si->sink->proplist, role))
200 continue;
202 if (!role_match(sink->proplist, role))
203 continue;
205 pa_sink_input_move_to(si, sink, FALSE);
208 return PA_HOOK_OK;
211 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
212 pa_source_output *so;
213 uint32_t idx;
215 pa_assert(c);
216 pa_assert(source);
217 pa_assert(u);
218 pa_assert(u->on_hotplug);
220 if (source->monitor_of)
221 return PA_HOOK_OK;
223 PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
224 const char *role;
226 if (so->source == source)
227 continue;
229 if (so->save_source)
230 continue;
232 if (so->direct_on_input)
233 continue;
235 /* Skip this if it is already in the process of being moved
236 * anyway */
237 if (!so->source)
238 continue;
240 /* It might happen that a stream and a source are set up at the
241 same time, in which case we want to make sure we don't
242 interfere with that */
243 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
244 continue;
246 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
247 continue;
249 if (role_match(so->source->proplist, role))
250 continue;
252 if (!role_match(source->proplist, role))
253 continue;
255 pa_source_output_move_to(so, source, FALSE);
258 return PA_HOOK_OK;
261 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
262 pa_sink_input *si;
263 uint32_t idx;
264 pa_sink *def;
266 pa_assert(c);
267 pa_assert(sink);
268 pa_assert(u);
269 pa_assert(u->on_rescue);
271 /* There's no point in doing anything if the core is shut down anyway */
272 if (c->state == PA_CORE_SHUTDOWN)
273 return PA_HOOK_OK;
275 /* If there not default sink, then there is no sink at all */
276 if (!(def = pa_namereg_get_default_sink(c)))
277 return PA_HOOK_OK;
279 PA_IDXSET_FOREACH(si, sink->inputs, idx) {
280 const char *role;
281 uint32_t jdx;
282 pa_sink *d;
284 if (!si->sink)
285 continue;
287 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
288 continue;
290 /* Would the default sink fit? If so, let's use it */
291 if (def != sink && role_match(def->proplist, role))
292 if (pa_sink_input_move_to(si, def, FALSE) >= 0)
293 continue;
295 /* Try to find some other fitting sink */
296 /* @todo: favour the highest priority device, not the first one we find? */
297 PA_IDXSET_FOREACH(d, c->sinks, jdx) {
298 if (d == def || d == sink)
299 continue;
301 if (!PA_SINK_IS_LINKED(pa_sink_get_state(d)))
302 continue;
304 if (role_match(d->proplist, role))
305 if (pa_sink_input_move_to(si, d, FALSE) >= 0)
306 break;
310 return PA_HOOK_OK;
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;
318 pa_assert(c);
319 pa_assert(source);
320 pa_assert(u);
321 pa_assert(u->on_rescue);
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;
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;
331 PA_IDXSET_FOREACH(so, source->outputs, idx) {
332 const char *role;
333 uint32_t jdx;
334 pa_source *d;
336 if (so->direct_on_input)
337 continue;
339 if (!so->source)
340 continue;
342 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
343 continue;
345 /* Would the default source fit? If so, let's use it */
346 if (def != source && role_match(def->proplist, role) && !source->monitor_of == !def->monitor_of) {
347 pa_source_output_move_to(so, def, FALSE);
348 continue;
351 /* Try to find some other fitting source */
352 /* @todo: favour the highest priority device, not the first one we find? */
353 PA_IDXSET_FOREACH(d, c->sources, jdx) {
354 if (d == def || d == source)
355 continue;
357 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
358 continue;
360 /* If moving from a monitor, move to another monitor */
361 if (!source->monitor_of == !d->monitor_of && role_match(d->proplist, role)) {
362 pa_source_output_move_to(so, d, FALSE);
363 break;
368 return PA_HOOK_OK;
371 int pa__init(pa_module*m) {
372 pa_modargs *ma = NULL;
373 struct userdata *u;
374 pa_bool_t on_hotplug = TRUE, on_rescue = TRUE;
376 pa_assert(m);
378 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
379 pa_log("Failed to parse module arguments");
380 goto fail;
383 if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
384 pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
385 pa_log("on_hotplug= and on_rescue= expect boolean arguments");
386 goto fail;
389 m->userdata = u = pa_xnew0(struct userdata, 1);
390 u->core = m->core;
391 u->module = m;
392 u->on_hotplug = on_hotplug;
393 u->on_rescue = on_rescue;
395 /* A little bit later than module-stream-restore */
396 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);
397 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);
399 if (on_hotplug) {
400 /* A little bit later than module-stream-restore */
401 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);
402 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);
405 if (on_rescue) {
406 /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
407 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, u);
408 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, u);
411 pa_modargs_free(ma);
412 return 0;
414 fail:
415 pa__done(m);
417 if (ma)
418 pa_modargs_free(ma);
420 return -1;
423 void pa__done(pa_module*m) {
424 struct userdata* u;
426 pa_assert(m);
428 if (!(u = m->userdata))
429 return;
431 if (u->sink_input_new_hook_slot)
432 pa_hook_slot_free(u->sink_input_new_hook_slot);
433 if (u->source_output_new_hook_slot)
434 pa_hook_slot_free(u->source_output_new_hook_slot);
436 if (u->sink_put_hook_slot)
437 pa_hook_slot_free(u->sink_put_hook_slot);
438 if (u->source_put_hook_slot)
439 pa_hook_slot_free(u->source_put_hook_slot);
441 if (u->sink_unlink_hook_slot)
442 pa_hook_slot_free(u->sink_unlink_hook_slot);
443 if (u->source_unlink_hook_slot)
444 pa_hook_slot_free(u->source_unlink_hook_slot);
446 pa_xfree(u);