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
);
48 c
->name
= pa_xstrdup(name
);
49 c
->description
= pa_xstrdup(description
);
52 c
->n_sinks
= c
->n_sources
= 0;
53 c
->max_sink_channels
= c
->max_source_channels
= 0;
54 c
->available
= PA_AVAILABLE_UNKNOWN
;
59 void pa_card_profile_free(pa_card_profile
*c
) {
63 pa_xfree(c
->description
);
67 void pa_card_profile_set_available(pa_card_profile
*c
, pa_available_t available
) {
71 pa_assert(c
->card
); /* Modify member variable directly during creation instead of using this function */
73 if (c
->available
== available
)
76 c
->available
= available
;
77 pa_log_debug("Setting card %s profile %s to availability status %s", c
->card
->name
, c
->name
,
78 available
== PA_AVAILABLE_YES
? "yes" : available
== PA_AVAILABLE_NO
? "no" : "unknown");
80 /* Post subscriptions to the card which owns us */
81 pa_assert_se(core
= c
->card
->core
);
82 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->card
->index
);
84 pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_PROFILE_AVAILABLE_CHANGED
], c
);
87 pa_card_new_data
* pa_card_new_data_init(pa_card_new_data
*data
) {
90 memset(data
, 0, sizeof(*data
));
91 data
->proplist
= pa_proplist_new();
92 data
->profiles
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
93 data
->ports
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
97 void pa_card_new_data_set_name(pa_card_new_data
*data
, const char *name
) {
100 pa_xfree(data
->name
);
101 data
->name
= pa_xstrdup(name
);
104 void pa_card_add_profile(pa_card
*c
, pa_card_profile
*profile
) {
108 /* take ownership of the profile */
109 pa_assert_se(pa_hashmap_put(c
->profiles
, profile
->name
, profile
) >= 0);
112 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->index
);
114 pa_hook_fire(&c
->core
->hooks
[PA_CORE_HOOK_CARD_PROFILE_ADDED
], profile
);
117 void pa_card_add_ports(pa_card
*c
, pa_hashmap
*ports
) {
124 /* take ownership of the ports */
125 PA_HASHMAP_FOREACH(p
, ports
, state
) {
127 pa_assert_se(pa_hashmap_put(c
->ports
, p
->name
, p
) >= 0);
130 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->index
);
132 while ((p
= pa_hashmap_steal_first(ports
)) != NULL
)
133 pa_hook_fire(&c
->core
->hooks
[PA_CORE_HOOK_PORT_ADDED
], p
);
136 void pa_card_new_data_set_profile(pa_card_new_data
*data
, const char *profile
) {
139 pa_xfree(data
->active_profile
);
140 data
->active_profile
= pa_xstrdup(profile
);
143 void pa_card_new_data_done(pa_card_new_data
*data
) {
147 pa_proplist_free(data
->proplist
);
150 pa_hashmap_free(data
->profiles
, (pa_free_cb_t
) pa_card_profile_free
);
153 pa_hashmap_free(data
->ports
, (pa_free_cb_t
) pa_device_port_unref
);
155 pa_xfree(data
->name
);
156 pa_xfree(data
->active_profile
);
159 pa_card
*pa_card_new(pa_core
*core
, pa_card_new_data
*data
) {
163 pa_card_profile
*profile
;
164 pa_device_port
*port
;
166 pa_core_assert_ref(core
);
168 pa_assert(data
->name
);
169 pa_assert(data
->profiles
);
170 pa_assert(!pa_hashmap_isempty(data
->profiles
));
172 c
= pa_xnew(pa_card
, 1);
174 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_CARD
, c
, data
->namereg_fail
))) {
179 pa_card_new_data_set_name(data
, name
);
181 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_NEW
], data
) < 0) {
183 pa_namereg_unregister(core
, name
);
188 c
->name
= pa_xstrdup(data
->name
);
189 c
->proplist
= pa_proplist_copy(data
->proplist
);
190 c
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
191 c
->module
= data
->module
;
193 c
->sinks
= pa_idxset_new(NULL
, NULL
);
194 c
->sources
= pa_idxset_new(NULL
, NULL
);
196 /* As a minor optimization we just steal the list instead of
198 pa_assert_se(c
->profiles
= data
->profiles
);
199 data
->profiles
= NULL
;
200 pa_assert_se(c
->ports
= data
->ports
);
203 PA_HASHMAP_FOREACH(profile
, c
->profiles
, state
)
206 PA_HASHMAP_FOREACH(port
, c
->ports
, state
)
209 c
->active_profile
= NULL
;
210 c
->save_profile
= FALSE
;
212 if (data
->active_profile
)
213 if ((c
->active_profile
= pa_hashmap_get(c
->profiles
, data
->active_profile
)))
214 c
->save_profile
= data
->save_profile
;
216 if (!c
->active_profile
) {
217 PA_HASHMAP_FOREACH(profile
, c
->profiles
, state
)
218 if (!c
->active_profile
|| profile
->priority
> c
->active_profile
->priority
)
219 c
->active_profile
= profile
;
223 c
->set_profile
= NULL
;
225 pa_device_init_description(c
->proplist
);
226 pa_device_init_icon(c
->proplist
, TRUE
);
227 pa_device_init_intended_roles(c
->proplist
);
229 pa_assert_se(pa_idxset_put(core
->cards
, c
, &c
->index
) >= 0);
231 pa_log_info("Created %u \"%s\"", c
->index
, c
->name
);
232 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_NEW
, c
->index
);
234 pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_PUT
], c
);
238 void pa_card_free(pa_card
*c
) {
246 pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_CARD_UNLINK
], c
);
248 pa_namereg_unregister(core
, c
->name
);
250 pa_idxset_remove_by_data(c
->core
->cards
, c
, NULL
);
252 pa_log_info("Freed %u \"%s\"", c
->index
, c
->name
);
254 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_REMOVE
, c
->index
);
256 pa_assert(pa_idxset_isempty(c
->sinks
));
257 pa_idxset_free(c
->sinks
, NULL
);
258 pa_assert(pa_idxset_isempty(c
->sources
));
259 pa_idxset_free(c
->sources
, NULL
);
261 pa_hashmap_free(c
->ports
, (pa_free_cb_t
) pa_device_port_unref
);
264 pa_hashmap_free(c
->profiles
, (pa_free_cb_t
) pa_card_profile_free
);
266 pa_proplist_free(c
->proplist
);
272 int pa_card_set_profile(pa_card
*c
, const char *name
, pa_bool_t save
) {
273 pa_card_profile
*profile
;
278 if (!c
->set_profile
) {
279 pa_log_debug("set_profile() operation not implemented for card %u \"%s\"", c
->index
, c
->name
);
280 return -PA_ERR_NOTIMPLEMENTED
;
284 return -PA_ERR_NOENTITY
;
286 if (!(profile
= pa_hashmap_get(c
->profiles
, name
)))
287 return -PA_ERR_NOENTITY
;
289 if (c
->active_profile
== profile
) {
290 c
->save_profile
= c
->save_profile
|| save
;
294 if ((r
= c
->set_profile(c
, profile
)) < 0)
297 pa_subscription_post(c
->core
, PA_SUBSCRIPTION_EVENT_CARD
|PA_SUBSCRIPTION_EVENT_CHANGE
, c
->index
);
299 pa_log_info("Changed profile of card %u \"%s\" to %s", c
->index
, c
->name
, profile
->name
);
301 c
->active_profile
= profile
;
302 c
->save_profile
= save
;
304 pa_hook_fire(&c
->core
->hooks
[PA_CORE_HOOK_CARD_PROFILE_CHANGED
], c
);
309 int pa_card_suspend(pa_card
*c
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
316 pa_assert(cause
!= 0);
318 PA_IDXSET_FOREACH(sink
, c
->sinks
, idx
) {
321 if ((r
= pa_sink_suspend(sink
, suspend
, cause
)) < 0)
325 PA_IDXSET_FOREACH(source
, c
->sources
, idx
) {
328 if ((r
= pa_source_suspend(source
, suspend
, cause
)) < 0)