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