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.1 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>
31 #include <pulse/util.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/device-port.h>
41 pa_card_profile
*pa_card_profile_new(const char *name
, const char *description
, size_t extra
) {
46 c
= pa_xmalloc(PA_ALIGN(sizeof(pa_card_profile
)) + extra
);
47 c
->name
= pa_xstrdup(name
);
48 c
->description
= pa_xstrdup(description
);
51 c
->n_sinks
= c
->n_sources
= 0;
52 c
->max_sink_channels
= c
->max_source_channels
= 0;
57 void pa_card_profile_free(pa_card_profile
*c
) {
61 pa_xfree(c
->description
);
65 pa_card_new_data
* pa_card_new_data_init(pa_card_new_data
*data
) {
68 memset(data
, 0, sizeof(*data
));
69 data
->proplist
= pa_proplist_new();
70 data
->profiles
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
71 data
->ports
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
75 void pa_card_new_data_set_name(pa_card_new_data
*data
, const char *name
) {
79 data
->name
= pa_xstrdup(name
);
82 void pa_card_new_data_set_profile(pa_card_new_data
*data
, const char *profile
) {
85 pa_xfree(data
->active_profile
);
86 data
->active_profile
= pa_xstrdup(profile
);
89 void pa_card_new_data_done(pa_card_new_data
*data
) {
93 pa_proplist_free(data
->proplist
);
98 while ((c
= pa_hashmap_steal_first(data
->profiles
)))
99 pa_card_profile_free(c
);
101 pa_hashmap_free(data
->profiles
, NULL
, NULL
);
105 pa_device_port_hashmap_free(data
->ports
);
107 pa_xfree(data
->name
);
108 pa_xfree(data
->active_profile
);
111 pa_card
*pa_card_new(pa_core
*core
, pa_card_new_data
*data
) {
115 pa_core_assert_ref(core
);
117 pa_assert(data
->name
);
118 pa_assert(data
->profiles
);
119 pa_assert(!pa_hashmap_isempty(data
->profiles
));
121 c
= pa_xnew(pa_card
, 1);
123 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_CARD
, c
, data
->namereg_fail
))) {
128 pa_card_new_data_set_name(data
, name
);
130 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_NEW
], data
) < 0) {
132 pa_namereg_unregister(core
, name
);
137 c
->name
= pa_xstrdup(data
->name
);
138 c
->proplist
= pa_proplist_copy(data
->proplist
);
139 c
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
140 c
->module
= data
->module
;
142 c
->sinks
= pa_idxset_new(NULL
, NULL
);
143 c
->sources
= pa_idxset_new(NULL
, NULL
);
145 /* As a minor optimization we just steal the list instead of
147 c
->profiles
= data
->profiles
;
148 data
->profiles
= NULL
;
149 c
->ports
= data
->ports
;
152 c
->active_profile
= NULL
;
153 c
->save_profile
= FALSE
;
155 if (data
->active_profile
)
156 if ((c
->active_profile
= pa_hashmap_get(c
->profiles
, data
->active_profile
)))
157 c
->save_profile
= data
->save_profile
;
159 if (!c
->active_profile
) {
163 PA_HASHMAP_FOREACH(p
, c
->profiles
, state
)
164 if (!c
->active_profile
|| p
->priority
> c
->active_profile
->priority
)
165 c
->active_profile
= p
;
169 c
->set_profile
= NULL
;
171 pa_device_init_description(c
->proplist
);
172 pa_device_init_icon(c
->proplist
, TRUE
);
173 pa_device_init_intended_roles(c
->proplist
);
175 pa_assert_se(pa_idxset_put(core
->cards
, c
, &c
->index
) >= 0);
177 pa_log_info("Created %u \"%s\"", c
->index
, c
->name
);
178 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_NEW
, c
->index
);
180 pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_PUT
], c
);
184 void pa_card_free(pa_card
*c
) {
192 pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_UNLINK
], c
);
194 pa_namereg_unregister(core
, c
->name
);
196 pa_idxset_remove_by_data(c
->core
->cards
, c
, NULL
);
198 pa_log_info("Freed %u \"%s\"", c
->index
, c
->name
);
200 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_REMOVE
, c
->index
);
202 pa_assert(pa_idxset_isempty(c
->sinks
));
203 pa_idxset_free(c
->sinks
, NULL
, NULL
);
204 pa_assert(pa_idxset_isempty(c
->sources
));
205 pa_idxset_free(c
->sources
, NULL
, NULL
);
207 pa_device_port_hashmap_free(c
->ports
);
212 while ((p
= pa_hashmap_steal_first(c
->profiles
)))
213 pa_card_profile_free(p
);
215 pa_hashmap_free(c
->profiles
, NULL
, NULL
);
218 pa_proplist_free(c
->proplist
);
224 int pa_card_set_profile(pa_card
*c
, const char *name
, pa_bool_t save
) {
225 pa_card_profile
*profile
;
230 if (!c
->set_profile
) {
231 pa_log_debug("set_profile() operation not implemented for card %u \"%s\"", c
->index
, c
->name
);
232 return -PA_ERR_NOTIMPLEMENTED
;
236 return -PA_ERR_NOENTITY
;
238 if (!(profile
= pa_hashmap_get(c
->profiles
, name
)))
239 return -PA_ERR_NOENTITY
;
241 if (c
->active_profile
== profile
) {
242 c
->save_profile
= c
->save_profile
|| save
;
246 if ((r
= c
->set_profile(c
, profile
)) < 0)
249 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->index
);
251 pa_log_info("Changed profile of card %u \"%s\" to %s", c
->index
, c
->name
, profile
->name
);
253 c
->active_profile
= profile
;
254 c
->save_profile
= save
;
256 pa_hook_fire(&c
->core
->hooks
[PA_CORE_HOOK_CARD_PROFILE_CHANGED
], c
);
261 int pa_card_suspend(pa_card
*c
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
268 pa_assert(cause
!= 0);
270 PA_IDXSET_FOREACH(sink
, c
->sinks
, idx
) {
273 if ((r
= pa_sink_suspend(sink
, suspend
, cause
)) < 0)
277 PA_IDXSET_FOREACH(source
, c
->sources
, idx
) {
280 if ((r
= pa_source_suspend(source
, suspend
, cause
)) < 0)