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