]> code.delx.au - pulseaudio/blob - src/modules/module-switch-on-port-available.c
module-switch-on-port-available: Handle hotplugged cards
[pulseaudio] / src / modules / module-switch-on-port-available.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5 Copyright 2011 Canonical Ltd
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <pulsecore/core.h>
28 #include <pulsecore/device-port.h>
29 #include <pulsecore/hashmap.h>
30
31 #include "module-switch-on-port-available-symdef.h"
32
33 struct userdata {
34 pa_hook_slot *available_slot;
35 pa_hook_slot *sink_new_slot;
36 pa_hook_slot *source_new_slot;
37 };
38
39 static pa_device_port* find_best_port(pa_hashmap *ports) {
40 void *state;
41 pa_device_port* port, *result = NULL;
42
43 PA_HASHMAP_FOREACH(port, ports, state) {
44 if (result == NULL ||
45 result->available == PA_AVAILABLE_NO ||
46 (port->available != PA_AVAILABLE_NO && port->priority > result->priority)) {
47 result = port;
48 }
49 }
50
51 return result;
52 }
53
54 static pa_bool_t try_to_switch_profile(pa_card *card, pa_device_port *port) {
55 pa_card_profile *best_profile = NULL, *profile;
56 void *state;
57
58 pa_log_debug("Finding best profile");
59
60 PA_HASHMAP_FOREACH(profile, port->profiles, state) {
61 if (best_profile && best_profile->priority >= profile->priority)
62 continue;
63
64 /* We make a best effort to keep other direction unchanged */
65 if (!port->is_input) {
66 if (card->active_profile->n_sources != profile->n_sources)
67 continue;
68
69 if (card->active_profile->max_source_channels != profile->max_source_channels)
70 continue;
71 }
72
73 if (!port->is_output) {
74 if (card->active_profile->n_sinks != profile->n_sinks)
75 continue;
76
77 if (card->active_profile->max_sink_channels != profile->max_sink_channels)
78 continue;
79 }
80
81 if (port->is_output) {
82 /* Try not to switch to HDMI sinks from analog when HDMI is becoming available */
83 uint32_t state2;
84 pa_sink *sink;
85 pa_bool_t found_active_port = FALSE;
86
87 PA_IDXSET_FOREACH(sink, card->sinks, state2) {
88 if (!sink->active_port)
89 continue;
90 if (sink->active_port->available != PA_AVAILABLE_NO)
91 found_active_port = TRUE;
92 }
93
94 if (found_active_port)
95 continue;
96 }
97
98 best_profile = profile;
99 }
100
101 if (!best_profile) {
102 pa_log_debug("No suitable profile found");
103 return FALSE;
104 }
105
106 if (pa_card_set_profile(card, best_profile->name, FALSE) != 0) {
107 pa_log_debug("Could not set profile %s", best_profile->name);
108 return FALSE;
109 }
110
111 return TRUE;
112 }
113
114 static void find_sink_and_source(pa_card *card, pa_device_port *port, pa_sink **si, pa_source **so)
115 {
116 pa_sink *sink = NULL;
117 pa_source *source = NULL;
118 uint32_t state;
119
120 if (port->is_output)
121 PA_IDXSET_FOREACH(sink, card->sinks, state)
122 if (port == pa_hashmap_get(sink->ports, port->name))
123 break;
124
125 if (port->is_input)
126 PA_IDXSET_FOREACH(source, card->sources, state)
127 if (port == pa_hashmap_get(source->ports, port->name))
128 break;
129
130 *si = sink;
131 *so = source;
132 }
133
134 static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port *port, void* userdata) {
135 uint32_t state;
136 pa_card* card;
137 pa_sink *sink;
138 pa_source *source;
139 pa_bool_t is_active_profile, is_active_port;
140
141 if (port->available == PA_AVAILABLE_UNKNOWN)
142 return PA_HOOK_OK;
143
144 pa_log_debug("finding port %s", port->name);
145
146 PA_IDXSET_FOREACH(card, c->cards, state)
147 if (port == pa_hashmap_get(card->ports, port->name))
148 break;
149
150 if (!card) {
151 pa_log_warn("Did not find port %s in array of cards", port->name);
152 return PA_HOOK_OK;
153 }
154
155 find_sink_and_source(card, port, &sink, &source);
156
157 is_active_profile = card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name);
158 is_active_port = (sink && sink->active_port == port) || (source && source->active_port == port);
159
160 if (port->available == PA_AVAILABLE_NO && !is_active_port)
161 return PA_HOOK_OK;
162
163 if (port->available == PA_AVAILABLE_YES) {
164 if (is_active_port)
165 return PA_HOOK_OK;
166
167 if (!is_active_profile) {
168 if (!try_to_switch_profile(card, port))
169 return PA_HOOK_OK;
170
171 pa_assert(card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name));
172
173 /* Now that profile has changed, our sink and source pointers must be updated */
174 find_sink_and_source(card, port, &sink, &source);
175 }
176
177 if (source)
178 pa_source_set_port(source, port->name, FALSE);
179 if (sink)
180 pa_sink_set_port(sink, port->name, FALSE);
181 }
182
183 if (port->available == PA_AVAILABLE_NO) {
184 if (sink) {
185 pa_device_port *p2 = find_best_port(sink->ports);
186
187 if (p2 && p2->available != PA_AVAILABLE_NO)
188 pa_sink_set_port(sink, p2->name, FALSE);
189 else {
190 /* Maybe try to switch to another profile? */
191 }
192 }
193
194 if (source) {
195 pa_device_port *p2 = find_best_port(source->ports);
196
197 if (p2 && p2->available != PA_AVAILABLE_NO)
198 pa_source_set_port(source, p2->name, FALSE);
199 else {
200 /* Maybe try to switch to another profile? */
201 }
202 }
203 }
204
205 return PA_HOOK_OK;
206 }
207
208 static void handle_all_unavailable(pa_core *core) {
209 pa_card *card;
210 uint32_t state;
211
212 PA_IDXSET_FOREACH(card, core->cards, state) {
213 pa_device_port *port;
214 void *state2;
215
216 PA_HASHMAP_FOREACH(port, card->ports, state2) {
217 if (port->available == PA_AVAILABLE_NO)
218 port_available_hook_callback(core, port, NULL);
219 }
220 }
221 }
222
223 static pa_device_port *new_sink_source(pa_hashmap *ports, const char *name) {
224
225 void *state;
226 pa_device_port *i, *p = NULL;
227
228 if (!ports)
229 return NULL;
230 if (name)
231 p = pa_hashmap_get(ports, name);
232 if (!p)
233 PA_HASHMAP_FOREACH(i, ports, state)
234 if (!p || i->priority > p->priority)
235 p = i;
236 if (!p)
237 return NULL;
238 if (p->available != PA_AVAILABLE_NO)
239 return NULL;
240
241 pa_assert_se(p = find_best_port(ports));
242 return p;
243 }
244
245 static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
246
247 pa_device_port *p = new_sink_source(new_data->ports, new_data->active_port);
248
249 if (p) {
250 pa_log_debug("Switching initial port for sink '%s' to '%s'", new_data->name, p->name);
251 pa_sink_new_data_set_port(new_data, p->name);
252 }
253 return PA_HOOK_OK;
254 }
255
256 static pa_hook_result_t source_new_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) {
257
258 pa_device_port *p = new_sink_source(new_data->ports, new_data->active_port);
259
260 if (p) {
261 pa_log_debug("Switching initial port for source '%s' to '%s'", new_data->name,
262 new_data->active_port);
263 pa_source_new_data_set_port(new_data, p->name);
264 }
265 return PA_HOOK_OK;
266 }
267
268
269 int pa__init(pa_module*m) {
270 struct userdata *u;
271
272 pa_assert(m);
273
274 m->userdata = u = pa_xnew(struct userdata, 1);
275
276 /* Make sure we are after module-device-restore, so we can overwrite that suggestion if necessary */
277 u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW],
278 PA_HOOK_NORMAL, (pa_hook_cb_t) sink_new_hook_callback, u);
279 u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW],
280 PA_HOOK_NORMAL, (pa_hook_cb_t) source_new_hook_callback, u);
281 u->available_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED],
282 PA_HOOK_LATE, (pa_hook_cb_t) port_available_hook_callback, u);
283
284 handle_all_unavailable(m->core);
285
286 return 0;
287 }
288
289 void pa__done(pa_module*m) {
290 struct userdata *u;
291
292 pa_assert(m);
293
294 if (!(u = m->userdata))
295 return;
296
297 if (u->available_slot)
298 pa_hook_slot_free(u->available_slot);
299 if (u->sink_new_slot)
300 pa_hook_slot_free(u->sink_new_slot);
301 if (u->source_new_slot)
302 pa_hook_slot_free(u->source_new_slot);
303
304 pa_xfree(u);
305 }