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