2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.
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.
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
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/namereg.h>
39 pa_card_profile
*pa_card_profile_new(const char *name
, const char *description
, size_t extra
) {
44 c
= pa_xmalloc(PA_ALIGN(sizeof(pa_card_profile
)) + extra
);
45 c
->name
= pa_xstrdup(name
);
46 c
->description
= pa_xstrdup(description
);
49 c
->n_sinks
= c
->n_sources
= 0;
50 c
->max_sink_channels
= c
->max_source_channels
= 0;
55 void pa_card_profile_free(pa_card_profile
*c
) {
59 pa_xfree(c
->description
);
63 pa_card_new_data
* pa_card_new_data_init(pa_card_new_data
*data
) {
66 memset(data
, 0, sizeof(*data
));
67 data
->proplist
= pa_proplist_new();
72 void pa_card_new_data_set_name(pa_card_new_data
*data
, const char *name
) {
76 data
->name
= pa_xstrdup(name
);
79 void pa_card_new_data_done(pa_card_new_data
*data
) {
83 pa_proplist_free(data
->proplist
);
88 while ((c
= pa_hashmap_steal_first(data
->profiles
)))
89 pa_card_profile_free(c
);
91 pa_hashmap_free(data
->profiles
, NULL
, NULL
);
97 pa_card
*pa_card_new(pa_core
*core
, pa_card_new_data
*data
) {
101 pa_core_assert_ref(core
);
103 pa_assert(data
->name
);
105 c
= pa_xnew(pa_card
, 1);
107 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_CARD
, c
, data
->namereg_fail
))) {
112 pa_card_new_data_set_name(data
, name
);
114 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_NEW
], data
) < 0) {
116 pa_namereg_unregister(core
, name
);
121 c
->name
= pa_xstrdup(data
->name
);
122 c
->proplist
= pa_proplist_copy(data
->proplist
);
123 c
->driver
= pa_xstrdup(data
->driver
);
124 c
->module
= data
->module
;
126 c
->sinks
= pa_idxset_new(NULL
, NULL
);
127 c
->sources
= pa_idxset_new(NULL
, NULL
);
129 c
->profiles
= data
->profiles
;
130 data
->profiles
= NULL
;
131 if (!(c
->active_profile
= data
->active_profile
))
136 while ((p
= pa_hashmap_iterate(c
->profiles
, &state
, NULL
))) {
137 if (!c
->active_profile
||
138 p
->priority
> c
->active_profile
->priority
)
140 c
->active_profile
= p
;
143 data
->active_profile
= NULL
;
146 c
->set_profile
= NULL
;
148 pa_assert_se(pa_idxset_put(core
->cards
, c
, &c
->index
) >= 0);
150 pa_log_info("Created %u \"%s\"", c
->index
, c
->name
);
151 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_NEW
, c
->index
);
153 pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_PUT
], c
);
157 void pa_card_free(pa_card
*c
) {
159 pa_card_profile
*profile
;
166 pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_UNLINK
], c
);
168 pa_namereg_unregister(core
, c
->name
);
170 pa_idxset_remove_by_data(c
->core
->cards
, c
, NULL
);
172 pa_log_info("Freed %u \"%s\"", c
->index
, c
->name
);
174 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_REMOVE
, c
->index
);
176 pa_assert(pa_idxset_isempty(c
->sinks
));
177 pa_idxset_free(c
->sinks
, NULL
, NULL
);
178 pa_assert(pa_idxset_isempty(c
->sources
));
179 pa_idxset_free(c
->sources
, NULL
, NULL
);
182 while ((profile
= pa_hashmap_steal_first(c
->profiles
)))
183 pa_card_profile_free(profile
);
185 pa_hashmap_free(c
->profiles
, NULL
, NULL
);
188 pa_proplist_free(c
->proplist
);
193 pa_core_check_idle(core
);
196 int pa_card_set_profile(pa_card
*c
, const char *name
) {
197 pa_card_profile
*profile
;
200 if (!c
->set_profile
) {
201 pa_log_warn("set_profile() operation not implemented for card %u", c
->index
);
208 if (!(profile
= pa_hashmap_get(c
->profiles
, name
)))
211 if (c
->active_profile
== profile
)
214 if (c
->set_profile(c
, profile
) < 0)
217 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->index
);