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