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