]> code.delx.au - pulseaudio/blob - src/modules/module-switch-on-port-available.c
module-switch-on-port-available: Do not switch profile if current port is available
[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 *callback_slot;
35 };
36
37 static pa_device_port* find_best_port(pa_hashmap *ports) {
38 void *state;
39 pa_device_port* port, *result = NULL;
40
41 PA_HASHMAP_FOREACH(port, ports, state) {
42 if (result == NULL ||
43 result->available == PA_PORT_AVAILABLE_NO ||
44 (port->available != PA_PORT_AVAILABLE_NO && port->priority > result->priority)) {
45 result = port;
46 }
47 }
48
49 return result;
50 }
51
52 static pa_bool_t try_to_switch_profile(pa_card *card, pa_device_port *port) {
53 pa_card_profile *best_profile = NULL, *profile;
54 void *state;
55
56 pa_log_debug("Finding best profile");
57
58 if (port->profiles)
59 PA_HASHMAP_FOREACH(profile, port->profiles, state) {
60 if (best_profile && best_profile->priority >= profile->priority)
61 continue;
62
63 if (!card->active_profile) {
64 best_profile = profile;
65 continue;
66 }
67
68 /* We make a best effort to keep other direction unchanged */
69 if (!port->is_input) {
70 if (card->active_profile->n_sources != profile->n_sources)
71 continue;
72
73 if (card->active_profile->max_source_channels != profile->max_source_channels)
74 continue;
75 }
76
77 if (!port->is_output) {
78 if (card->active_profile->n_sinks != profile->n_sinks)
79 continue;
80
81 if (card->active_profile->max_sink_channels != profile->max_sink_channels)
82 continue;
83 }
84
85 if (port->is_output) {
86 /* Try not to switch to HDMI sinks from analog when HDMI is becoming available */
87 uint32_t state2;
88 pa_sink *sink;
89 pa_bool_t found_active_port = FALSE;
90 PA_IDXSET_FOREACH(sink, card->sinks, state2) {
91 if (!sink->active_port)
92 continue;
93 if (sink->active_port->available != PA_PORT_AVAILABLE_NO)
94 found_active_port = TRUE;
95 }
96 if (found_active_port)
97 continue;
98 }
99
100 best_profile = profile;
101 }
102
103 if (!best_profile) {
104 pa_log_debug("No suitable profile found");
105 return FALSE;
106 }
107
108 if (pa_card_set_profile(card, best_profile->name, FALSE) != 0) {
109 pa_log_debug("Could not set profile %s", best_profile->name);
110 return FALSE;
111 }
112
113 return TRUE;
114 }
115
116 static void find_sink_and_source(pa_card *card, pa_device_port *port, pa_sink **si, pa_source **so)
117 {
118 pa_sink *sink = NULL;
119 pa_source *source = NULL;
120 uint32_t state;
121
122 if (port->is_output)
123 PA_IDXSET_FOREACH(sink, card->sinks, state)
124 if (sink->ports && port == pa_hashmap_get(sink->ports, port->name))
125 break;
126
127 if (port->is_input)
128 PA_IDXSET_FOREACH(source, card->sources, state)
129 if (source->ports && port == pa_hashmap_get(source->ports, port->name))
130 break;
131
132 *si = sink;
133 *so = source;
134 }
135
136 static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port *port, void* userdata) {
137 uint32_t state;
138 pa_card* card;
139 pa_sink *sink;
140 pa_source *source;
141 pa_bool_t is_active_profile, is_active_port;
142
143 if (port->available == PA_PORT_AVAILABLE_UNKNOWN)
144 return PA_HOOK_OK;
145
146 pa_log_debug("finding port %s", port->name);
147
148 PA_IDXSET_FOREACH(card, c->cards, state)
149 if (card->ports && port == pa_hashmap_get(card->ports, port->name))
150 break;
151
152 if (!card) {
153 pa_log_warn("Did not find port %s in array of cards", port->name);
154 return PA_HOOK_OK;
155 }
156
157 find_sink_and_source(card, port, &sink, &source);
158
159 is_active_profile = port->profiles && card->active_profile &&
160 card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name);
161 is_active_port = (sink && sink->active_port == port) || (source && source->active_port == port);
162
163 if (port->available == PA_PORT_AVAILABLE_NO && !is_active_port)
164 return PA_HOOK_OK;
165
166 if (port->available == PA_PORT_AVAILABLE_YES) {
167 if (is_active_port)
168 return PA_HOOK_OK;
169
170 if (!is_active_profile) {
171 if (!try_to_switch_profile(card, port))
172 return PA_HOOK_OK;
173
174 pa_assert(card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name));
175
176 /* Now that profile has changed, our sink and source pointers must be updated */
177 find_sink_and_source(card, port, &sink, &source);
178 }
179
180 if (source)
181 pa_source_set_port(source, port->name, FALSE);
182 if (sink)
183 pa_sink_set_port(sink, port->name, FALSE);
184 }
185
186 if (port->available == PA_PORT_AVAILABLE_NO) {
187 if (sink) {
188 pa_device_port *p2 = find_best_port(sink->ports);
189
190 if (p2 && p2->available != PA_PORT_AVAILABLE_NO)
191 pa_sink_set_port(sink, p2->name, FALSE);
192 else {
193 /* Maybe try to switch to another profile? */
194 }
195 }
196
197 if (source) {
198 pa_device_port *p2 = find_best_port(source->ports);
199
200 if (p2 && p2->available != PA_PORT_AVAILABLE_NO)
201 pa_source_set_port(source, p2->name, FALSE);
202 else {
203 /* Maybe try to switch to another profile? */
204 }
205 }
206 }
207
208 return PA_HOOK_OK;
209 }
210
211 static void handle_all_unavailable(pa_core *core) {
212 pa_card *card;
213 uint32_t state;
214
215 PA_IDXSET_FOREACH(card, core->cards, state) {
216 pa_device_port *port;
217 void *state2;
218
219 if (!card->ports)
220 continue;
221
222 PA_HASHMAP_FOREACH(port, card->ports, state2) {
223 if (port->available == PA_PORT_AVAILABLE_NO)
224 port_available_hook_callback(core, port, NULL);
225 }
226 }
227 }
228
229 int pa__init(pa_module*m) {
230 struct userdata *u;
231
232 pa_assert(m);
233
234 m->userdata = u = pa_xnew(struct userdata, 1);
235
236 u->callback_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED],
237 PA_HOOK_LATE, (pa_hook_cb_t) port_available_hook_callback, u);
238
239 handle_all_unavailable(m->core);
240
241 return 0;
242 }
243
244 void pa__done(pa_module*m) {
245 struct userdata *u;
246
247 pa_assert(m);
248
249 if (!(u = m->userdata))
250 return;
251
252 if (u->callback_slot)
253 pa_hook_slot_free(u->callback_slot);
254
255 pa_xfree(u);
256 }