]> code.delx.au - pulseaudio/blob - src/modules/module-intended-roles.c
virtual-sink: Add a modarg for forcing flat volume.
[pulseaudio] / 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 <pulse/xmalloc.h>
27 #include <pulse/volume.h>
28 #include <pulse/timeval.h>
29 #include <pulse/util.h>
30
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>
40
41 #include "module-intended-roles-symdef.h"
42
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);
47 PA_MODULE_USAGE(
48 "on_hotplug=<When new device becomes available, recheck streams?> "
49 "on_rescue=<When device becomes unavailable, recheck streams?>");
50
51 static const char* const valid_modargs[] = {
52 "on_hotplug",
53 "on_rescue",
54 NULL
55 };
56
57 struct userdata {
58 pa_core *core;
59 pa_module *module;
60
61 pa_hook_slot
62 *sink_input_new_hook_slot,
63 *source_output_new_hook_slot,
64 *sink_put_hook_slot,
65 *source_put_hook_slot,
66 *sink_unlink_hook_slot,
67 *source_unlink_hook_slot;
68
69 pa_bool_t on_hotplug:1;
70 pa_bool_t on_rescue:1;
71 };
72
73 static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
74 const char *ir;
75 char *r;
76 const char *state = NULL;
77
78 if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES)))
79 return FALSE;
80
81 while ((r = pa_split_spaces(ir, &state))) {
82
83 if (pa_streq(role, r)) {
84 pa_xfree(r);
85 return TRUE;
86 }
87
88 pa_xfree(r);
89 }
90
91 return FALSE;
92 }
93
94 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
95 const char *role;
96 pa_sink *s, *def;
97 uint32_t idx;
98
99 pa_assert(c);
100 pa_assert(new_data);
101 pa_assert(u);
102
103 if (!new_data->proplist) {
104 pa_log_debug("New stream lacks property data.");
105 return PA_HOOK_OK;
106 }
107
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)));
110 return PA_HOOK_OK;
111 }
112
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)));
115 return PA_HOOK_OK;
116 }
117
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;
123 return PA_HOOK_OK;
124 }
125
126 /* @todo: favour the highest priority device, not the first one we find? */
127 PA_IDXSET_FOREACH(s, c->sinks, idx) {
128 if (s == def)
129 continue;
130
131 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)))
132 continue;
133
134 if (role_match(s->proplist, role)) {
135 new_data->sink = s;
136 new_data->save_sink = FALSE;
137 return PA_HOOK_OK;
138 }
139 }
140
141 return PA_HOOK_OK;
142 }
143
144 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
145 const char *role;
146 pa_source *s, *def;
147 uint32_t idx;
148
149 pa_assert(c);
150 pa_assert(new_data);
151 pa_assert(u);
152
153 if (!new_data->proplist) {
154 pa_log_debug("New stream lacks property data.");
155 return PA_HOOK_OK;
156 }
157
158 if (new_data->source) {
159 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
160 return PA_HOOK_OK;
161 }
162
163 if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
164 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)));
165 return PA_HOOK_OK;
166 }
167
168 /* Prefer the default source over any other source, just in case... */
169 if ((def = pa_namereg_get_default_source(c)))
170 if (role_match(def->proplist, role)) {
171 new_data->source = def;
172 new_data->save_source = FALSE;
173 return PA_HOOK_OK;
174 }
175
176 PA_IDXSET_FOREACH(s, c->sources, idx) {
177 if (s->monitor_of)
178 continue;
179
180 if (s == def)
181 continue;
182
183 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
184 continue;
185
186 /* @todo: favour the highest priority device, not the first one we find? */
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 /* Skip this if it is already in the process of being moved
216 * anyway */
217 if (!si->sink)
218 continue;
219
220 /* It might happen that a stream and a sink are set up at the
221 same time, in which case we want to make sure we don't
222 interfere with that */
223 if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
224 continue;
225
226 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
227 continue;
228
229 if (role_match(si->sink->proplist, role))
230 continue;
231
232 if (!role_match(sink->proplist, role))
233 continue;
234
235 pa_sink_input_move_to(si, sink, FALSE);
236 }
237
238 return PA_HOOK_OK;
239 }
240
241 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
242 pa_source_output *so;
243 uint32_t idx;
244
245 pa_assert(c);
246 pa_assert(source);
247 pa_assert(u);
248 pa_assert(u->on_hotplug);
249
250 if (source->monitor_of)
251 return PA_HOOK_OK;
252
253 PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
254 const char *role;
255
256 if (so->source == source)
257 continue;
258
259 if (so->save_source)
260 continue;
261
262 if (so->direct_on_input)
263 continue;
264
265 /* Skip this if it is already in the process of being moved
266 * anyway */
267 if (!so->source)
268 continue;
269
270 /* It might happen that a stream and a source are set up at the
271 same time, in which case we want to make sure we don't
272 interfere with that */
273 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
274 continue;
275
276 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
277 continue;
278
279 if (role_match(so->source->proplist, role))
280 continue;
281
282 if (!role_match(source->proplist, role))
283 continue;
284
285 pa_source_output_move_to(so, source, FALSE);
286 }
287
288 return PA_HOOK_OK;
289 }
290
291 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
292 pa_sink_input *si;
293 uint32_t idx;
294 pa_sink *def;
295
296 pa_assert(c);
297 pa_assert(sink);
298 pa_assert(u);
299 pa_assert(u->on_rescue);
300
301 /* There's no point in doing anything if the core is shut down anyway */
302 if (c->state == PA_CORE_SHUTDOWN)
303 return PA_HOOK_OK;
304
305 /* If there not default sink, then there is no sink at all */
306 if (!(def = pa_namereg_get_default_sink(c)))
307 return PA_HOOK_OK;
308
309 PA_IDXSET_FOREACH(si, sink->inputs, idx) {
310 const char *role;
311 uint32_t jdx;
312 pa_sink *d;
313
314 if (!si->sink)
315 continue;
316
317 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
318 continue;
319
320 /* Would the default sink fit? If so, let's use it */
321 if (def != sink && role_match(def->proplist, role))
322 if (pa_sink_input_move_to(si, def, FALSE) >= 0)
323 continue;
324
325 /* Try to find some other fitting sink */
326 /* @todo: favour the highest priority device, not the first one we find? */
327 PA_IDXSET_FOREACH(d, c->sinks, jdx) {
328 if (d == def || d == sink)
329 continue;
330
331 if (!PA_SINK_IS_LINKED(pa_sink_get_state(d)))
332 continue;
333
334 if (role_match(d->proplist, role))
335 if (pa_sink_input_move_to(si, d, FALSE) >= 0)
336 break;
337 }
338 }
339
340 return PA_HOOK_OK;
341 }
342
343 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
344 pa_source_output *so;
345 uint32_t idx;
346 pa_source *def;
347
348 pa_assert(c);
349 pa_assert(source);
350 pa_assert(u);
351 pa_assert(u->on_rescue);
352
353 /* There's no point in doing anything if the core is shut down anyway */
354 if (c->state == PA_CORE_SHUTDOWN)
355 return PA_HOOK_OK;
356
357 /* If there not default source, then there is no source at all */
358 if (!(def = pa_namereg_get_default_source(c)))
359 return PA_HOOK_OK;
360
361 PA_IDXSET_FOREACH(so, source->outputs, idx) {
362 const char *role;
363 uint32_t jdx;
364 pa_source *d;
365
366 if (so->direct_on_input)
367 continue;
368
369 if (!so->source)
370 continue;
371
372 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
373 continue;
374
375 /* Would the default source fit? If so, let's use it */
376 if (def != source && role_match(def->proplist, role) && !source->monitor_of == !def->monitor_of) {
377 pa_source_output_move_to(so, def, FALSE);
378 continue;
379 }
380
381 /* Try to find some other fitting source */
382 /* @todo: favour the highest priority device, not the first one we find? */
383 PA_IDXSET_FOREACH(d, c->sources, jdx) {
384 if (d == def || d == source)
385 continue;
386
387 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
388 continue;
389
390 /* If moving from a monitor, move to another monitor */
391 if (!source->monitor_of == !d->monitor_of && role_match(d->proplist, role)) {
392 pa_source_output_move_to(so, d, FALSE);
393 break;
394 }
395 }
396 }
397
398 return PA_HOOK_OK;
399 }
400
401 int pa__init(pa_module*m) {
402 pa_modargs *ma = NULL;
403 struct userdata *u;
404 pa_bool_t on_hotplug = TRUE, on_rescue = TRUE;
405
406 pa_assert(m);
407
408 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
409 pa_log("Failed to parse module arguments");
410 goto fail;
411 }
412
413 if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
414 pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
415 pa_log("on_hotplug= and on_rescue= expect boolean arguments");
416 goto fail;
417 }
418
419 m->userdata = u = pa_xnew0(struct userdata, 1);
420 u->core = m->core;
421 u->module = m;
422 u->on_hotplug = on_hotplug;
423 u->on_rescue = on_rescue;
424
425 /* A little bit later than module-stream-restore */
426 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);
427 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);
428
429 if (on_hotplug) {
430 /* A little bit later than module-stream-restore */
431 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);
432 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);
433 }
434
435 if (on_rescue) {
436 /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
437 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);
438 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);
439 }
440
441 pa_modargs_free(ma);
442 return 0;
443
444 fail:
445 pa__done(m);
446
447 if (ma)
448 pa_modargs_free(ma);
449
450 return -1;
451 }
452
453 void pa__done(pa_module*m) {
454 struct userdata* u;
455
456 pa_assert(m);
457
458 if (!(u = m->userdata))
459 return;
460
461 if (u->sink_input_new_hook_slot)
462 pa_hook_slot_free(u->sink_input_new_hook_slot);
463 if (u->source_output_new_hook_slot)
464 pa_hook_slot_free(u->source_output_new_hook_slot);
465
466 if (u->sink_put_hook_slot)
467 pa_hook_slot_free(u->sink_put_hook_slot);
468 if (u->source_put_hook_slot)
469 pa_hook_slot_free(u->source_put_hook_slot);
470
471 if (u->sink_unlink_hook_slot)
472 pa_hook_slot_free(u->sink_unlink_hook_slot);
473 if (u->source_unlink_hook_slot)
474 pa_hook_slot_free(u->source_unlink_hook_slot);
475
476 pa_xfree(u);
477 }