]> code.delx.au - pulseaudio/blob - src/pulsecore/card.c
card: Ensure we still pick a profile even if it's unavailable.
[pulseaudio] / src / pulsecore / card.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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulse/util.h>
32
33 #include <pulsecore/log.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/device-port.h>
38
39 #include "card.h"
40
41 pa_card_profile *pa_card_profile_new(const char *name, const char *description, size_t extra) {
42 pa_card_profile *c;
43
44 pa_assert(name);
45
46 c = pa_xmalloc(PA_ALIGN(sizeof(pa_card_profile)) + extra);
47 c->card = NULL;
48 c->name = pa_xstrdup(name);
49 c->description = pa_xstrdup(description);
50
51 c->priority = 0;
52 c->n_sinks = c->n_sources = 0;
53 c->max_sink_channels = c->max_source_channels = 0;
54 c->available = PA_AVAILABLE_UNKNOWN;
55
56 return c;
57 }
58
59 void pa_card_profile_free(pa_card_profile *c) {
60 pa_assert(c);
61
62 pa_xfree(c->name);
63 pa_xfree(c->description);
64 pa_xfree(c);
65 }
66
67 void pa_card_profile_set_available(pa_card_profile *c, pa_available_t available) {
68 pa_core *core;
69
70 pa_assert(c);
71 pa_assert(c->card); /* Modify member variable directly during creation instead of using this function */
72
73 if (c->available == available)
74 return;
75
76 c->available = available;
77 pa_log_debug("Setting card %s profile %s to availability status %s", c->card->name, c->name,
78 available == PA_AVAILABLE_YES ? "yes" : available == PA_AVAILABLE_NO ? "no" : "unknown");
79
80 /* Post subscriptions to the card which owns us */
81 pa_assert_se(core = c->card->core);
82 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->card->index);
83
84 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_PROFILE_AVAILABLE_CHANGED], c);
85 }
86
87 pa_card_new_data* pa_card_new_data_init(pa_card_new_data *data) {
88 pa_assert(data);
89
90 memset(data, 0, sizeof(*data));
91 data->proplist = pa_proplist_new();
92 data->profiles = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) pa_card_profile_free);
93 data->ports = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) pa_device_port_unref);
94 return data;
95 }
96
97 void pa_card_new_data_set_name(pa_card_new_data *data, const char *name) {
98 pa_assert(data);
99
100 pa_xfree(data->name);
101 data->name = pa_xstrdup(name);
102 }
103
104 void pa_card_add_profile(pa_card *c, pa_card_profile *profile) {
105 pa_assert(c);
106 pa_assert(profile);
107
108 /* take ownership of the profile */
109 pa_assert_se(pa_hashmap_put(c->profiles, profile->name, profile) >= 0);
110 profile->card = c;
111
112 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
113
114 pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_PROFILE_ADDED], profile);
115 }
116
117 void pa_card_new_data_set_profile(pa_card_new_data *data, const char *profile) {
118 pa_assert(data);
119
120 pa_xfree(data->active_profile);
121 data->active_profile = pa_xstrdup(profile);
122 }
123
124 void pa_card_new_data_done(pa_card_new_data *data) {
125
126 pa_assert(data);
127
128 pa_proplist_free(data->proplist);
129
130 if (data->profiles)
131 pa_hashmap_free(data->profiles);
132
133 if (data->ports)
134 pa_hashmap_free(data->ports);
135
136 pa_xfree(data->name);
137 pa_xfree(data->active_profile);
138 }
139
140 pa_card *pa_card_new(pa_core *core, pa_card_new_data *data) {
141 pa_card *c;
142 const char *name;
143 void *state;
144 pa_card_profile *profile;
145 pa_device_port *port;
146
147 pa_core_assert_ref(core);
148 pa_assert(data);
149 pa_assert(data->name);
150 pa_assert(data->profiles);
151 pa_assert(!pa_hashmap_isempty(data->profiles));
152
153 c = pa_xnew(pa_card, 1);
154
155 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_CARD, c, data->namereg_fail))) {
156 pa_xfree(c);
157 return NULL;
158 }
159
160 pa_card_new_data_set_name(data, name);
161
162 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_NEW], data) < 0) {
163 pa_xfree(c);
164 pa_namereg_unregister(core, name);
165 return NULL;
166 }
167
168 c->core = core;
169 c->name = pa_xstrdup(data->name);
170 c->proplist = pa_proplist_copy(data->proplist);
171 c->driver = pa_xstrdup(pa_path_get_filename(data->driver));
172 c->module = data->module;
173
174 c->sinks = pa_idxset_new(NULL, NULL);
175 c->sources = pa_idxset_new(NULL, NULL);
176
177 /* As a minor optimization we just steal the list instead of
178 * copying it here */
179 pa_assert_se(c->profiles = data->profiles);
180 data->profiles = NULL;
181 pa_assert_se(c->ports = data->ports);
182 data->ports = NULL;
183
184 PA_HASHMAP_FOREACH(profile, c->profiles, state)
185 profile->card = c;
186
187 PA_HASHMAP_FOREACH(port, c->ports, state)
188 port->card = c;
189
190 c->active_profile = NULL;
191 c->save_profile = false;
192
193 if (data->active_profile)
194 if ((c->active_profile = pa_hashmap_get(c->profiles, data->active_profile)))
195 c->save_profile = data->save_profile;
196
197 if (!c->active_profile) {
198 PA_HASHMAP_FOREACH(profile, c->profiles, state) {
199 if (profile->available == PA_AVAILABLE_NO)
200 continue;
201
202 if (!c->active_profile || profile->priority > c->active_profile->priority)
203 c->active_profile = profile;
204 }
205 /* If all profiles are not available, then we still need to pick one */
206 if (!c->active_profile) {
207 PA_HASHMAP_FOREACH(profile, c->profiles, state)
208 if (!c->active_profile || profile->priority > c->active_profile->priority)
209 c->active_profile = profile;
210 }
211 pa_assert(c->active_profile);
212 }
213
214 c->userdata = NULL;
215 c->set_profile = NULL;
216
217 pa_device_init_description(c->proplist);
218 pa_device_init_icon(c->proplist, true);
219 pa_device_init_intended_roles(c->proplist);
220
221 pa_assert_se(pa_idxset_put(core->cards, c, &c->index) >= 0);
222
223 pa_log_info("Created %u \"%s\"", c->index, c->name);
224 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, c->index);
225
226 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_PUT], c);
227 return c;
228 }
229
230 void pa_card_free(pa_card *c) {
231 pa_core *core;
232
233 pa_assert(c);
234 pa_assert(c->core);
235
236 core = c->core;
237
238 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_UNLINK], c);
239
240 pa_namereg_unregister(core, c->name);
241
242 pa_idxset_remove_by_data(c->core->cards, c, NULL);
243
244 pa_log_info("Freed %u \"%s\"", c->index, c->name);
245
246 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_REMOVE, c->index);
247
248 pa_assert(pa_idxset_isempty(c->sinks));
249 pa_idxset_free(c->sinks, NULL);
250 pa_assert(pa_idxset_isempty(c->sources));
251 pa_idxset_free(c->sources, NULL);
252
253 pa_hashmap_free(c->ports);
254
255 if (c->profiles)
256 pa_hashmap_free(c->profiles);
257
258 pa_proplist_free(c->proplist);
259 pa_xfree(c->driver);
260 pa_xfree(c->name);
261 pa_xfree(c);
262 }
263
264 int pa_card_set_profile(pa_card *c, const char *name, bool save) {
265 pa_card_profile *profile;
266 int r;
267
268 pa_assert(c);
269
270 if (!c->set_profile) {
271 pa_log_debug("set_profile() operation not implemented for card %u \"%s\"", c->index, c->name);
272 return -PA_ERR_NOTIMPLEMENTED;
273 }
274
275 if (!name)
276 return -PA_ERR_NOENTITY;
277
278 if (!(profile = pa_hashmap_get(c->profiles, name)))
279 return -PA_ERR_NOENTITY;
280
281 if (c->active_profile == profile) {
282 c->save_profile = c->save_profile || save;
283 return 0;
284 }
285
286 if ((r = c->set_profile(c, profile)) < 0)
287 return r;
288
289 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
290
291 pa_log_info("Changed profile of card %u \"%s\" to %s", c->index, c->name, profile->name);
292
293 c->active_profile = profile;
294 c->save_profile = save;
295
296 pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], c);
297
298 return 0;
299 }
300
301 int pa_card_suspend(pa_card *c, bool suspend, pa_suspend_cause_t cause) {
302 pa_sink *sink;
303 pa_source *source;
304 uint32_t idx;
305 int ret = 0;
306
307 pa_assert(c);
308 pa_assert(cause != 0);
309
310 PA_IDXSET_FOREACH(sink, c->sinks, idx) {
311 int r;
312
313 if ((r = pa_sink_suspend(sink, suspend, cause)) < 0)
314 ret = r;
315 }
316
317 PA_IDXSET_FOREACH(source, c->sources, idx) {
318 int r;
319
320 if ((r = pa_source_suspend(source, suspend, cause)) < 0)
321 ret = r;
322 }
323
324 return ret;
325 }