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