]> code.delx.au - pulseaudio/blob - src/pulsecore/card.c
make pa_card_new_data::active_profile a string
[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 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
32 #include <pulsecore/log.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/namereg.h>
36
37 #include "card.h"
38
39 pa_card_profile *pa_card_profile_new(const char *name, const char *description, size_t extra) {
40 pa_card_profile *c;
41
42 pa_assert(name);
43
44 c = pa_xmalloc(PA_ALIGN(sizeof(pa_card_profile)) + extra);
45 c->name = pa_xstrdup(name);
46 c->description = pa_xstrdup(description);
47
48 c->priority = 0;
49 c->n_sinks = c->n_sources = 0;
50 c->max_sink_channels = c->max_source_channels = 0;
51
52 return c;
53 }
54
55 void pa_card_profile_free(pa_card_profile *c) {
56 pa_assert(c);
57
58 pa_xfree(c->name);
59 pa_xfree(c->description);
60 pa_xfree(c);
61 }
62
63 pa_card_new_data* pa_card_new_data_init(pa_card_new_data *data) {
64 pa_assert(data);
65
66 memset(data, 0, sizeof(*data));
67 data->proplist = pa_proplist_new();
68
69 return data;
70 }
71
72 void pa_card_new_data_set_name(pa_card_new_data *data, const char *name) {
73 pa_assert(data);
74
75 pa_xfree(data->name);
76 data->name = pa_xstrdup(name);
77 }
78
79 void pa_card_new_data_set_profile(pa_card_new_data *data, const char *profile) {
80 pa_assert(data);
81
82 pa_xfree(data->active_profile);
83 data->active_profile = pa_xstrdup(profile);
84 }
85
86 void pa_card_new_data_done(pa_card_new_data *data) {
87
88 pa_assert(data);
89
90 pa_proplist_free(data->proplist);
91
92 if (data->profiles) {
93 pa_card_profile *c;
94
95 while ((c = pa_hashmap_steal_first(data->profiles)))
96 pa_card_profile_free(c);
97
98 pa_hashmap_free(data->profiles, NULL, NULL);
99 }
100
101 pa_xfree(data->name);
102 pa_xfree(data->active_profile);
103 }
104
105 pa_card *pa_card_new(pa_core *core, pa_card_new_data *data) {
106 pa_card *c;
107 const char *name;
108
109 pa_core_assert_ref(core);
110 pa_assert(data);
111 pa_assert(data->name);
112
113 c = pa_xnew(pa_card, 1);
114
115 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_CARD, c, data->namereg_fail))) {
116 pa_xfree(c);
117 return NULL;
118 }
119
120 pa_card_new_data_set_name(data, name);
121
122 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_NEW], data) < 0) {
123 pa_xfree(c);
124 pa_namereg_unregister(core, name);
125 return NULL;
126 }
127
128 c->core = core;
129 c->name = pa_xstrdup(data->name);
130 c->proplist = pa_proplist_copy(data->proplist);
131 c->driver = pa_xstrdup(data->driver);
132 c->module = data->module;
133
134 c->sinks = pa_idxset_new(NULL, NULL);
135 c->sources = pa_idxset_new(NULL, NULL);
136
137 /* As a minor optimization we just steal the list instead of
138 * copying it here */
139 c->profiles = data->profiles;
140 data->profiles = NULL;
141
142 c->active_profile = NULL;
143
144 if (data->active_profile && c->profiles)
145 c->active_profile = pa_hashmap_get(c->profiles, data->active_profile);
146
147 if (!c->active_profile && c->profiles) {
148 void *state = NULL;
149 pa_card_profile *p;
150
151 while ((p = pa_hashmap_iterate(c->profiles, &state, NULL))) {
152 if (!c->active_profile ||
153 p->priority > c->active_profile->priority)
154
155 c->active_profile = p;
156 }
157 }
158
159 c->userdata = NULL;
160 c->set_profile = NULL;
161
162 pa_assert_se(pa_idxset_put(core->cards, c, &c->index) >= 0);
163
164 pa_log_info("Created %u \"%s\"", c->index, c->name);
165 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, c->index);
166
167 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_PUT], c);
168 return c;
169 }
170
171 void pa_card_free(pa_card *c) {
172 pa_core *core;
173 pa_card_profile *profile;
174
175 pa_assert(c);
176 pa_assert(c->core);
177
178 core = c->core;
179
180 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_UNLINK], c);
181
182 pa_namereg_unregister(core, c->name);
183
184 pa_idxset_remove_by_data(c->core->cards, c, NULL);
185
186 pa_log_info("Freed %u \"%s\"", c->index, c->name);
187
188 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_REMOVE, c->index);
189
190 pa_assert(pa_idxset_isempty(c->sinks));
191 pa_idxset_free(c->sinks, NULL, NULL);
192 pa_assert(pa_idxset_isempty(c->sources));
193 pa_idxset_free(c->sources, NULL, NULL);
194
195 if (c->profiles) {
196 while ((profile = pa_hashmap_steal_first(c->profiles)))
197 pa_card_profile_free(profile);
198
199 pa_hashmap_free(c->profiles, NULL, NULL);
200 }
201
202 pa_proplist_free(c->proplist);
203 pa_xfree(c->driver);
204 pa_xfree(c->name);
205 pa_xfree(c);
206 }
207
208 int pa_card_set_profile(pa_card *c, const char *name) {
209 pa_card_profile *profile;
210 pa_assert(c);
211
212 if (!c->set_profile) {
213 pa_log_warn("set_profile() operation not implemented for card %u \"%s\"", c->index, c->name);
214 return -1;
215 }
216
217 if (!c->profiles)
218 return -1;
219
220 if (!(profile = pa_hashmap_get(c->profiles, name)))
221 return -1;
222
223 if (c->active_profile == profile)
224 return 0;
225
226 if (c->set_profile(c, profile) < 0)
227 return -1;
228
229 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
230
231 pa_log_info("Successfully changed profile of card %u \"%s\" to %s", c->index, c->name, profile->name);
232
233 c->active_profile = profile;
234
235 return 0;
236 }