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);
51 void pa_card_profile_free(pa_card_profile *c) {
55 pa_xfree(c->description);
59 pa_card_new_data* pa_card_new_data_init(pa_card_new_data *data) {
62 memset(data, 0, sizeof(*data));
63 data->proplist = pa_proplist_new();
68 void pa_card_new_data_set_name(pa_card_new_data *data, const char *name) {
72 data->name = pa_xstrdup(name);
75 void pa_card_new_data_done(pa_card_new_data *data) {
79 pa_proplist_free(data->proplist);
84 while ((c = pa_hashmap_steal_first(data->profiles)))
85 pa_card_profile_free(c);
87 pa_hashmap_free(data->profiles, NULL, NULL);
93 pa_card *pa_card_new(pa_core *core, pa_card_new_data *data) {
97 pa_core_assert_ref(core);
99 pa_assert(data->name);
101 c = pa_xnew(pa_card, 1);
103 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_CARD, c, data->namereg_fail))) {
108 pa_card_new_data_set_name(data, name);
110 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_NEW], data) < 0) {
112 pa_namereg_unregister(core, name);
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;
122 c->sinks = pa_idxset_new(NULL, NULL);
123 c->sources = pa_idxset_new(NULL, NULL);
125 c->profiles = data->profiles;
126 data->profiles = NULL;
127 if (!(c->active_profile = data->active_profile))
129 c->active_profile = pa_hashmap_first(c->profiles);
130 data->active_profile = NULL;
133 c->set_profile = NULL;
135 pa_assert_se(pa_idxset_put(core->cards, c, &c->index) >= 0);
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);
140 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_PUT], c);
144 void pa_card_free(pa_card *c) {
146 pa_card_profile *profile;
153 pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_UNLINK], c);
155 pa_namereg_unregister(core, c->name);
157 pa_idxset_remove_by_data(c->core->cards, c, NULL);
159 pa_log_info("Freed %u \"%s\"", c->index, c->name);
161 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_REMOVE, c->index);
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);
168 while ((profile = pa_hashmap_steal_first(c->profiles)))
169 pa_card_profile_free(profile);
171 pa_hashmap_free(c->profiles, NULL, NULL);
173 pa_proplist_free(c->proplist);
178 pa_core_check_idle(core);
181 int pa_card_set_profile(pa_card *c, const char *name) {
182 pa_card_profile *profile;
185 if (!c->set_profile) {
186 pa_log_warn("set_profile() operation not implemented for card %u", c->index);
193 if (!(profile = pa_hashmap_get(c->profiles, name)))
196 if (c->active_profile == profile)
199 if (c->set_profile(c, profile) < 0)
202 pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);