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
26 #include <pulse/xmalloc.h>
27 #include <pulse/volume.h>
28 #include <pulse/timeval.h>
29 #include <pulse/util.h>
31 #include <pulsecore/core-error.h>
32 #include <pulsecore/module.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/sink-input.h>
38 #include <pulsecore/source-output.h>
39 #include <pulsecore/namereg.h>
41 #include "module-intended-roles-symdef.h"
43 PA_MODULE_AUTHOR("Lennart Poettering");
44 PA_MODULE_DESCRIPTION("Automatically set device of streams based of intended roles of devices");
45 PA_MODULE_VERSION(PACKAGE_VERSION
);
46 PA_MODULE_LOAD_ONCE(TRUE
);
48 "on_hotplug=<When new device becomes available, recheck streams?> "
49 "on_rescue=<When device becomes unavailable, recheck streams?>");
51 static const char* const valid_modargs
[] = {
62 *sink_input_new_hook_slot
,
63 *source_output_new_hook_slot
,
65 *source_put_hook_slot
,
66 *sink_unlink_hook_slot
,
67 *source_unlink_hook_slot
;
69 pa_bool_t on_hotplug
:1;
70 pa_bool_t on_rescue
:1;
73 static pa_bool_t
role_match(pa_proplist
*proplist
, const char *role
) {
76 const char *state
= NULL
;
78 if (!(ir
= pa_proplist_gets(proplist
, PA_PROP_DEVICE_INTENDED_ROLES
)))
81 while ((r
= pa_split_spaces(ir
, &state
))) {
83 if (pa_streq(role
, r
)) {
94 static pa_hook_result_t
sink_input_new_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
103 if (!new_data
->proplist
) {
104 pa_log_debug("New stream lacks property data.");
108 if (new_data
->sink
) {
109 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data
->proplist
, PA_PROP_MEDIA_NAME
)));
113 if (!(role
= pa_proplist_gets(new_data
->proplist
, PA_PROP_MEDIA_ROLE
))) {
114 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
)));
118 /* Prefer the default sink over any other sink, just in case... */
119 if ((def
= pa_namereg_get_default_sink(c
)))
120 if (role_match(def
->proplist
, role
)) {
121 new_data
->sink
= def
;
122 new_data
->save_sink
= FALSE
;
126 PA_IDXSET_FOREACH(s
, c
->sinks
, idx
) {
130 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)))
133 if (role_match(s
->proplist
, role
)) {
135 new_data
->save_sink
= FALSE
;
143 static pa_hook_result_t
source_output_new_hook_callback(pa_core
*c
, pa_source_output_new_data
*new_data
, struct userdata
*u
) {
152 if (!new_data
->proplist
) {
153 pa_log_debug("New stream lacks property data.");
157 if (new_data
->source
) {
158 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data
->proplist
, PA_PROP_MEDIA_NAME
)));
162 if (!(role
= pa_proplist_gets(new_data
->proplist
, PA_PROP_MEDIA_ROLE
))) {
163 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
)));
167 /* Prefer the default source over any other source, just in case... */
168 if ((def
= pa_namereg_get_default_source(c
)))
169 if (role_match(def
->proplist
, role
)) {
170 new_data
->source
= def
;
171 new_data
->save_source
= FALSE
;
175 PA_IDXSET_FOREACH(s
, c
->sources
, idx
) {
179 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)))
182 if (role_match(s
->proplist
, role
)) {
183 new_data
->source
= s
;
184 new_data
->save_source
= FALSE
;
192 static pa_hook_result_t
sink_put_hook_callback(pa_core
*c
, pa_sink
*sink
, struct userdata
*u
) {
199 pa_assert(u
->on_hotplug
);
201 PA_IDXSET_FOREACH(si
, c
->sink_inputs
, idx
) {
204 if (si
->sink
== sink
)
210 /* Skip this if it is already in the process of being moved
215 /* It might happen that a stream and a sink are set up at the
216 same time, in which case we want to make sure we don't
217 interfere with that */
218 if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si
)))
221 if (!(role
= pa_proplist_gets(si
->proplist
, PA_PROP_MEDIA_ROLE
)))
224 if (role_match(si
->sink
->proplist
, role
))
227 if (!role_match(sink
->proplist
, role
))
230 pa_sink_input_move_to(si
, sink
, FALSE
);
236 static pa_hook_result_t
source_put_hook_callback(pa_core
*c
, pa_source
*source
, struct userdata
*u
) {
237 pa_source_output
*so
;
243 pa_assert(u
->on_hotplug
);
245 PA_IDXSET_FOREACH(so
, c
->source_outputs
, idx
) {
248 if (so
->source
== source
)
254 if (so
->direct_on_input
)
257 /* Skip this if it is already in the process of being moved
262 /* It might happen that a stream and a source are set up at the
263 same time, in which case we want to make sure we don't
264 interfere with that */
265 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so
)))
268 if (!(role
= pa_proplist_gets(so
->proplist
, PA_PROP_MEDIA_ROLE
)))
271 if (role_match(so
->source
->proplist
, role
))
274 if (!role_match(source
->proplist
, role
))
277 pa_source_output_move_to(so
, source
, FALSE
);
283 static pa_hook_result_t
sink_unlink_hook_callback(pa_core
*c
, pa_sink
*sink
, struct userdata
*u
) {
291 pa_assert(u
->on_rescue
);
293 /* There's no point in doing anything if the core is shut down anyway */
294 if (c
->state
== PA_CORE_SHUTDOWN
)
297 /* If there not default sink, then there is no sink at all */
298 if (!(def
= pa_namereg_get_default_sink(c
)))
301 PA_IDXSET_FOREACH(si
, sink
->inputs
, idx
) {
309 if (!(role
= pa_proplist_gets(si
->proplist
, PA_PROP_MEDIA_ROLE
)))
312 /* Would the default sink fit? If so, let's use it */
313 if (def
!= sink
&& role_match(def
->proplist
, role
))
314 if (pa_sink_input_move_to(si
, def
, FALSE
) >= 0)
317 /* Try to find some other fitting sink */
318 PA_IDXSET_FOREACH(d
, c
->sinks
, jdx
) {
319 if (d
== def
|| d
== sink
)
322 if (!PA_SINK_IS_LINKED(pa_sink_get_state(d
)))
325 if (role_match(d
->proplist
, role
))
326 if (pa_sink_input_move_to(si
, d
, FALSE
) >= 0)
334 static pa_hook_result_t
source_unlink_hook_callback(pa_core
*c
, pa_source
*source
, struct userdata
*u
) {
335 pa_source_output
*so
;
342 pa_assert(u
->on_rescue
);
344 /* There's no point in doing anything if the core is shut down anyway */
345 if (c
->state
== PA_CORE_SHUTDOWN
)
348 /* If there not default source, then there is no source at all */
349 if (!(def
= pa_namereg_get_default_source(c
)))
352 PA_IDXSET_FOREACH(so
, source
->outputs
, idx
) {
357 if (so
->direct_on_input
)
363 if (!(role
= pa_proplist_gets(so
->proplist
, PA_PROP_MEDIA_ROLE
)))
366 /* Would the default source fit? If so, let's use it */
367 if (def
!= source
&& role_match(def
->proplist
, role
) && !source
->monitor_of
== !def
->monitor_of
) {
368 pa_source_output_move_to(so
, def
, FALSE
);
372 /* Try to find some other fitting source */
373 PA_IDXSET_FOREACH(d
, c
->sources
, jdx
) {
374 if (d
== def
|| d
== source
)
377 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d
)))
380 if (role_match(d
->proplist
, role
) && !source
->monitor_of
== !d
->monitor_of
) {
381 pa_source_output_move_to(so
, d
, FALSE
);
390 int pa__init(pa_module
*m
) {
391 pa_modargs
*ma
= NULL
;
393 pa_bool_t on_hotplug
= TRUE
, on_rescue
= TRUE
;
397 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
398 pa_log("Failed to parse module arguments");
402 if (pa_modargs_get_value_boolean(ma
, "on_hotplug", &on_hotplug
) < 0 ||
403 pa_modargs_get_value_boolean(ma
, "on_rescue", &on_rescue
) < 0) {
404 pa_log("on_hotplug= and on_rescue= expect boolean arguments");
408 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
411 u
->on_hotplug
= on_hotplug
;
412 u
->on_rescue
= on_rescue
;
414 /* A little bit later than module-stream-restore */
415 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
);
416 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
);
419 /* A little bit later than module-stream-restore */
420 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
);
421 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
);
425 /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
426 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
);
427 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
);
442 void pa__done(pa_module
*m
) {
447 if (!(u
= m
->userdata
))
450 if (u
->sink_input_new_hook_slot
)
451 pa_hook_slot_free(u
->sink_input_new_hook_slot
);
452 if (u
->source_output_new_hook_slot
)
453 pa_hook_slot_free(u
->source_output_new_hook_slot
);
455 if (u
->sink_put_hook_slot
)
456 pa_hook_slot_free(u
->sink_put_hook_slot
);
457 if (u
->source_put_hook_slot
)
458 pa_hook_slot_free(u
->source_put_hook_slot
);
460 if (u
->sink_unlink_hook_slot
)
461 pa_hook_slot_free(u
->sink_unlink_hook_slot
);
462 if (u
->source_unlink_hook_slot
)
463 pa_hook_slot_free(u
->source_unlink_hook_slot
);