]> code.delx.au - pulseaudio/blob - src/modules/module-intended-roles.c
intended-roles: drop quite a few unnecessary includes
[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 PA_IDXSET_FOREACH(s, c->sinks, idx) {
127 if (s == def)
128 continue;
129
130 if (role_match(s->proplist, role)) {
131 new_data->sink = s;
132 new_data->save_sink = FALSE;
133 return PA_HOOK_OK;
134 }
135 }
136
137 return PA_HOOK_OK;
138 }
139
140 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
141 const char *role;
142 pa_source *s, *def;
143 uint32_t idx;
144
145 pa_assert(c);
146 pa_assert(new_data);
147 pa_assert(u);
148
149 if (!new_data->proplist) {
150 pa_log_debug("New stream lacks property data.");
151 return PA_HOOK_OK;
152 }
153
154 if (new_data->source) {
155 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
156 return PA_HOOK_OK;
157 }
158
159 if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
160 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)));
161 return PA_HOOK_OK;
162 }
163
164 /* Prefer the default source over any other source, just in case... */
165 if ((def = pa_namereg_get_default_source(c)))
166 if (role_match(def->proplist, role)) {
167 new_data->source = def;
168 new_data->save_source = FALSE;
169 return PA_HOOK_OK;
170 }
171
172 PA_IDXSET_FOREACH(s, c->sources, idx) {
173 if (s == def)
174 continue;
175
176 if (role_match(s->proplist, role)) {
177 new_data->source = s;
178 new_data->save_source = FALSE;
179 return PA_HOOK_OK;
180 }
181 }
182
183 return PA_HOOK_OK;
184 }
185
186 static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
187 pa_sink_input *si;
188 uint32_t idx;
189
190 pa_assert(c);
191 pa_assert(sink);
192 pa_assert(u);
193 pa_assert(u->on_hotplug);
194
195 PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
196 const char *role;
197
198 if (si->sink == sink)
199 continue;
200
201 if (si->save_sink)
202 continue;
203
204 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
205 continue;
206
207 if (role_match(si->sink->proplist, role))
208 continue;
209
210 if (!role_match(sink->proplist, role))
211 continue;
212
213 pa_sink_input_move_to(si, sink, FALSE);
214 }
215
216 return PA_HOOK_OK;
217 }
218
219 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
220 pa_source_output *so;
221 uint32_t idx;
222
223 pa_assert(c);
224 pa_assert(source);
225 pa_assert(u);
226 pa_assert(u->on_hotplug);
227
228 PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
229 const char *role;
230
231 if (so->source == source)
232 continue;
233
234 if (so->save_source)
235 continue;
236
237 if (so->direct_on_input)
238 continue;
239
240 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
241 continue;
242
243 if (role_match(so->source->proplist, role))
244 continue;
245
246 if (!role_match(source->proplist, role))
247 continue;
248
249 pa_source_output_move_to(so, source, FALSE);
250 }
251
252 return PA_HOOK_OK;
253 }
254
255 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
256 pa_sink_input *si;
257 uint32_t idx;
258 pa_sink *def;
259
260 pa_assert(c);
261 pa_assert(sink);
262 pa_assert(u);
263 pa_assert(u->on_rescue);
264
265 /* There's no point in doing anything if the core is shut down anyway */
266 if (c->state == PA_CORE_SHUTDOWN)
267 return PA_HOOK_OK;
268
269 /* If there not default sink, then there is no sink at all */
270 if (!(def = pa_namereg_get_default_sink(c)))
271 return PA_HOOK_OK;
272
273 PA_IDXSET_FOREACH(si, sink->inputs, idx) {
274 const char *role;
275 uint32_t jdx;
276 pa_sink *d;
277
278 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
279 continue;
280
281 /* Would the default sink fit? If so, let's use it */
282 if (def != sink && role_match(def->proplist, role)) {
283 pa_sink_input_move_to(si, def, FALSE);
284 continue;
285 }
286
287 /* Try to find some other fitting sink */
288 PA_IDXSET_FOREACH(d, c->sinks, jdx) {
289 if (d == def || d == sink)
290 continue;
291
292 if (role_match(d->proplist, role)) {
293 pa_sink_input_move_to(si, d, FALSE);
294 break;
295 }
296 }
297 }
298
299 return PA_HOOK_OK;
300 }
301
302 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
303 pa_source_output *so;
304 uint32_t idx;
305 pa_source *def;
306
307 pa_assert(c);
308 pa_assert(source);
309 pa_assert(u);
310 pa_assert(u->on_rescue);
311
312 /* There's no point in doing anything if the core is shut down anyway */
313 if (c->state == PA_CORE_SHUTDOWN)
314 return PA_HOOK_OK;
315
316 /* If there not default source, then there is no source at all */
317 if (!(def = pa_namereg_get_default_source(c)))
318 return PA_HOOK_OK;
319
320 PA_IDXSET_FOREACH(so, source->outputs, idx) {
321 const char *role;
322 uint32_t jdx;
323 pa_source *d;
324
325 if (so->direct_on_input)
326 continue;
327
328 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
329 continue;
330
331 /* Would the default source fit? If so, let's use it */
332 if (def != source && role_match(def->proplist, role) && !source->monitor_of == !def->monitor_of) {
333 pa_source_output_move_to(so, def, FALSE);
334 continue;
335 }
336
337 /* Try to find some other fitting source */
338 PA_IDXSET_FOREACH(d, c->sources, jdx) {
339 if (d == def || d == source)
340 continue;
341
342 if (role_match(d->proplist, role) && !source->monitor_of == !d->monitor_of) {
343 pa_source_output_move_to(so, d, FALSE);
344 break;
345 }
346 }
347 }
348
349 return PA_HOOK_OK;
350 }
351
352 int pa__init(pa_module*m) {
353 pa_modargs *ma = NULL;
354 struct userdata *u;
355 pa_bool_t on_hotplug = TRUE, on_rescue = TRUE;
356
357 pa_assert(m);
358
359 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
360 pa_log("Failed to parse module arguments");
361 goto fail;
362 }
363
364 if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
365 pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
366 pa_log("on_hotplug= and on_rescue= expect boolean arguments");
367 goto fail;
368 }
369
370 m->userdata = u = pa_xnew0(struct userdata, 1);
371 u->core = m->core;
372 u->module = m;
373 u->on_hotplug = on_hotplug;
374 u->on_rescue = on_rescue;
375
376 /* A little bit later than module-stream-restore */
377 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);
378 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);
379
380 if (on_hotplug) {
381 /* A little bit later than module-stream-restore */
382 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);
383 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);
384 }
385
386 if (on_rescue) {
387 /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
388 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);
389 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);
390 }
391
392 pa_modargs_free(ma);
393 return 0;
394
395 fail:
396 pa__done(m);
397
398 if (ma)
399 pa_modargs_free(ma);
400
401 return -1;
402 }
403
404 void pa__done(pa_module*m) {
405 struct userdata* u;
406
407 pa_assert(m);
408
409 if (!(u = m->userdata))
410 return;
411
412 if (u->sink_input_new_hook_slot)
413 pa_hook_slot_free(u->sink_input_new_hook_slot);
414 if (u->source_output_new_hook_slot)
415 pa_hook_slot_free(u->source_output_new_hook_slot);
416
417 if (u->sink_put_hook_slot)
418 pa_hook_slot_free(u->sink_put_hook_slot);
419 if (u->source_put_hook_slot)
420 pa_hook_slot_free(u->source_put_hook_slot);
421
422 if (u->sink_unlink_hook_slot)
423 pa_hook_slot_free(u->sink_unlink_hook_slot);
424 if (u->source_unlink_hook_slot)
425 pa_hook_slot_free(u->source_unlink_hook_slot);
426
427 pa_xfree(u);
428 }