]> code.delx.au - pulseaudio/blob - src/pulsecore/card.c
fix destruction when no profiles are defined
[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 return c;
49 }
50
51 void pa_card_profile_free(pa_card_profile *c) {
52 pa_assert(c);
53
54 pa_xfree(c->name);
55 pa_xfree(c->description);
56 pa_xfree(c);
57 }
58
59 pa_card_new_data* pa_card_new_data_init(pa_card_new_data *data) {
60 pa_assert(data);
61
62 memset(data, 0, sizeof(*data));
63 data->proplist = pa_proplist_new();
64
65 return data;
66 }
67
68 void pa_card_new_data_set_name(pa_card_new_data *data, const char *name) {
69 pa_assert(data);
70
71 pa_xfree(data->name);
72 data->name = pa_xstrdup(name);
73 }
74
75 void pa_card_new_data_done(pa_card_new_data *data) {
76
77 pa_assert(data);
78
79 pa_proplist_free(data->proplist);
80
81 if (data->profiles) {
82 pa_card_profile *c;
83
84 while ((c = pa_hashmap_steal_first(data->profiles)))
85 pa_card_profile_free(c);
86
87 pa_hashmap_free(data->profiles, NULL, NULL);
88 }
89
90 pa_xfree(data->name);
91 }
92
93 pa_card *pa_card_new(pa_core *core, pa_card_new_data *data) {
94 pa_card *c;
95 const char *name;
96
97 pa_core_assert_ref(core);
98 pa_assert(data);
99 pa_assert(data->name);
100
101 c = pa_xnew(pa_card, 1);
102
103 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_CARD, c, data->namereg_fail))) {
104 pa_xfree(c);
105 return NULL;
106 }
107
108 pa_card_new_data_set_name(data, name);
109
110 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_NEW], data) < 0) {
111 pa_xfree(c);
112 pa_namereg_unregister(core, name);
113 return NULL;
114 }
115
116 c->core = core;
117 c->name = pa_xstrdup(data->name);
118 c->proplist = pa_proplist_copy(data->proplist);
119 c->driver = pa_xstrdup(data->driver);
120 c->module = data->module;
121
122 c->sinks = pa_idxset_new(NULL, NULL);
123 c->sources = pa_idxset_new(NULL, NULL);
124
125 c->profiles = data->profiles;
126 data->profiles = NULL;
127 if (!(c->active_profile = data->active_profile))
128 if (c->profiles)
129 c->active_profile = pa_hashmap_first(c->profiles);
130 data->active_profile = NULL;
131
132 c->userdata = NULL;
133 c->set_profile = NULL;
134
135 pa_assert_se(pa_idxset_put(core->cards, c, &c->index) >= 0);
136
137 pa_log_info("Created %u \"%s\"", c->index, c->name);
138 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, c->index);
139
140 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_PUT], c);
141 return c;
142 }
143
144 void pa_card_free(pa_card *c) {
145 pa_core *core;
146 pa_card_profile *profile;
147
148 pa_assert(c);
149 pa_assert(c->core);
150
151 core = c->core;
152
153 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_UNLINK], c);
154
155 pa_namereg_unregister(core, c->name);
156
157 pa_idxset_remove_by_data(c->core->cards, c, NULL);
158
159 pa_log_info("Freed %u \"%s\"", c->index, c->name);
160
161 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_REMOVE, c->index);
162
163 pa_assert(pa_idxset_isempty(c->sinks));
164 pa_idxset_free(c->sinks, NULL, NULL);
165 pa_assert(pa_idxset_isempty(c->sources));
166 pa_idxset_free(c->sources, NULL, NULL);
167
168 if (c->profiles) {
169 while ((profile = pa_hashmap_steal_first(c->profiles)))
170 pa_card_profile_free(profile);
171
172 pa_hashmap_free(c->profiles, NULL, NULL);
173 }
174
175 pa_proplist_free(c->proplist);
176 pa_xfree(c->driver);
177 pa_xfree(c->name);
178 pa_xfree(c);
179
180 pa_core_check_idle(core);
181 }
182
183 int pa_card_set_profile(pa_card *c, const char *name) {
184 pa_card_profile *profile;
185 pa_assert(c);
186
187 if (!c->set_profile) {
188 pa_log_warn("set_profile() operation not implemented for card %u", c->index);
189 return -1;
190 }
191
192 if (!c->profiles)
193 return -1;
194
195 if (!(profile = pa_hashmap_get(c->profiles, name)))
196 return -1;
197
198 if (c->active_profile == profile)
199 return 0;
200
201 if (c->set_profile(c, profile) < 0)
202 return -1;
203
204 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
205
206 return 0;
207 }