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