2009-01-15 18:29:16 +01:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
|
|
|
|
|
|
|
|
|
Copyright 2009 Lennart Poettering
|
|
|
|
|
|
|
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2009-01-15 18:29:16 +01:00
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
along with PulseAudio; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2009-01-22 00:15:19 +01:00
|
|
|
#include <pulse/util.h>
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/macro.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/namereg.h>
|
2011-11-25 15:17:13 +01:00
|
|
|
#include <pulsecore/device-port.h>
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
#include "card.h"
|
|
|
|
|
|
2009-01-17 02:03:35 +01:00
|
|
|
pa_card_profile *pa_card_profile_new(const char *name, const char *description, size_t extra) {
|
2009-01-15 23:44:46 +01:00
|
|
|
pa_card_profile *c;
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
pa_assert(name);
|
|
|
|
|
|
2009-01-17 02:03:35 +01:00
|
|
|
c = pa_xmalloc(PA_ALIGN(sizeof(pa_card_profile)) + extra);
|
2012-06-29 18:04:55 +03:00
|
|
|
c->card = NULL;
|
2009-01-15 18:29:16 +01:00
|
|
|
c->name = pa_xstrdup(name);
|
2009-01-17 02:03:35 +01:00
|
|
|
c->description = pa_xstrdup(description);
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-01-20 20:35:18 +01:00
|
|
|
c->priority = 0;
|
|
|
|
|
c->n_sinks = c->n_sources = 0;
|
|
|
|
|
c->max_sink_channels = c->max_source_channels = 0;
|
|
|
|
|
|
2009-01-15 18:29:16 +01:00
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-15 23:44:46 +01:00
|
|
|
void pa_card_profile_free(pa_card_profile *c) {
|
2009-01-15 18:29:16 +01:00
|
|
|
pa_assert(c);
|
2012-06-29 18:04:55 +03:00
|
|
|
pa_assert(!c->card); /* Card profiles shouldn't be freed before removing them from the card. */
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
pa_xfree(c->name);
|
2009-01-17 02:03:35 +01:00
|
|
|
pa_xfree(c->description);
|
2009-01-15 18:29:16 +01:00
|
|
|
pa_xfree(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_card_new_data* pa_card_new_data_init(pa_card_new_data *data) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
data->proplist = pa_proplist_new();
|
2012-06-08 21:49:10 +03:00
|
|
|
data->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2011-11-25 15:17:13 +01:00
|
|
|
data->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2009-01-15 18:29:16 +01:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_card_new_data_set_name(pa_card_new_data *data, const char *name) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
pa_xfree(data->name);
|
|
|
|
|
data->name = pa_xstrdup(name);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-23 16:41:55 +02:00
|
|
|
void pa_card_add_profile(pa_card *c, pa_card_profile *profile) {
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(profile);
|
|
|
|
|
|
|
|
|
|
/* take ownership of the profile */
|
|
|
|
|
pa_assert_se(pa_hashmap_put(c->profiles, profile->name, profile) >= 0);
|
2012-06-29 18:04:55 +03:00
|
|
|
profile->card = c;
|
2012-10-23 16:41:55 +02:00
|
|
|
|
|
|
|
|
pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
|
|
|
|
|
|
|
|
|
|
pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_PROFILE_ADDED], profile);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-23 16:41:56 +02:00
|
|
|
void pa_card_add_ports(pa_card *c, pa_hashmap *ports) {
|
|
|
|
|
pa_device_port *p;
|
|
|
|
|
void *state;
|
|
|
|
|
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(ports);
|
|
|
|
|
|
|
|
|
|
/* take ownership of the ports */
|
|
|
|
|
PA_HASHMAP_FOREACH(p, ports, state)
|
|
|
|
|
pa_assert_se(pa_hashmap_put(c->ports, p->name, p) >= 0);
|
|
|
|
|
|
|
|
|
|
pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
|
|
|
|
|
|
|
|
|
|
while ((p = pa_hashmap_steal_first(ports)) != NULL)
|
|
|
|
|
pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_PORT_ADDED], p);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 02:46:36 +01:00
|
|
|
void pa_card_new_data_set_profile(pa_card_new_data *data, const char *profile) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
pa_xfree(data->active_profile);
|
|
|
|
|
data->active_profile = pa_xstrdup(profile);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-15 18:29:16 +01:00
|
|
|
void pa_card_new_data_done(pa_card_new_data *data) {
|
|
|
|
|
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
pa_proplist_free(data->proplist);
|
|
|
|
|
|
2009-01-15 23:44:46 +01:00
|
|
|
if (data->profiles) {
|
|
|
|
|
pa_card_profile *c;
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-01-15 23:44:46 +01:00
|
|
|
while ((c = pa_hashmap_steal_first(data->profiles)))
|
|
|
|
|
pa_card_profile_free(c);
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-01-15 23:44:46 +01:00
|
|
|
pa_hashmap_free(data->profiles, NULL, NULL);
|
2009-01-15 18:29:16 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 15:17:13 +01:00
|
|
|
if (data->ports)
|
|
|
|
|
pa_device_port_hashmap_free(data->ports);
|
|
|
|
|
|
2009-01-15 18:29:16 +01:00
|
|
|
pa_xfree(data->name);
|
2009-01-21 02:46:36 +01:00
|
|
|
pa_xfree(data->active_profile);
|
2009-01-15 18:29:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_card *pa_card_new(pa_core *core, pa_card_new_data *data) {
|
|
|
|
|
pa_card *c;
|
|
|
|
|
const char *name;
|
2012-06-29 18:04:55 +03:00
|
|
|
void *state;
|
|
|
|
|
pa_card_profile *profile;
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
pa_core_assert_ref(core);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(data->name);
|
2012-06-08 21:49:10 +03:00
|
|
|
pa_assert(data->profiles);
|
|
|
|
|
pa_assert(!pa_hashmap_isempty(data->profiles));
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
c = pa_xnew(pa_card, 1);
|
|
|
|
|
|
|
|
|
|
if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_CARD, c, data->namereg_fail))) {
|
|
|
|
|
pa_xfree(c);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_card_new_data_set_name(data, name);
|
|
|
|
|
|
|
|
|
|
if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_NEW], data) < 0) {
|
|
|
|
|
pa_xfree(c);
|
|
|
|
|
pa_namereg_unregister(core, name);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c->core = core;
|
|
|
|
|
c->name = pa_xstrdup(data->name);
|
|
|
|
|
c->proplist = pa_proplist_copy(data->proplist);
|
2009-01-22 00:15:19 +01:00
|
|
|
c->driver = pa_xstrdup(pa_path_get_filename(data->driver));
|
2009-01-15 18:29:16 +01:00
|
|
|
c->module = data->module;
|
|
|
|
|
|
2009-01-15 18:38:20 +01:00
|
|
|
c->sinks = pa_idxset_new(NULL, NULL);
|
|
|
|
|
c->sources = pa_idxset_new(NULL, NULL);
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-01-21 02:46:36 +01:00
|
|
|
/* As a minor optimization we just steal the list instead of
|
|
|
|
|
* copying it here */
|
2009-01-15 23:44:46 +01:00
|
|
|
c->profiles = data->profiles;
|
|
|
|
|
data->profiles = NULL;
|
2011-11-25 15:17:13 +01:00
|
|
|
c->ports = data->ports;
|
|
|
|
|
data->ports = NULL;
|
2009-01-20 20:35:18 +01:00
|
|
|
|
2012-06-29 18:04:55 +03:00
|
|
|
if (c->profiles) {
|
|
|
|
|
PA_HASHMAP_FOREACH(profile, c->profiles, state)
|
|
|
|
|
profile->card = c;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 02:46:36 +01:00
|
|
|
c->active_profile = NULL;
|
2009-03-25 23:49:26 +01:00
|
|
|
c->save_profile = FALSE;
|
2009-01-20 20:35:18 +01:00
|
|
|
|
2012-06-08 21:49:10 +03:00
|
|
|
if (data->active_profile)
|
2009-03-23 19:31:36 +01:00
|
|
|
if ((c->active_profile = pa_hashmap_get(c->profiles, data->active_profile)))
|
|
|
|
|
c->save_profile = data->save_profile;
|
2009-01-21 02:46:36 +01:00
|
|
|
|
2012-06-08 21:49:10 +03:00
|
|
|
if (!c->active_profile) {
|
2012-06-29 18:04:55 +03:00
|
|
|
PA_HASHMAP_FOREACH(profile, c->profiles, state)
|
|
|
|
|
if (!c->active_profile || profile->priority > c->active_profile->priority)
|
|
|
|
|
c->active_profile = profile;
|
2009-01-21 02:46:36 +01:00
|
|
|
}
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
c->userdata = NULL;
|
2009-01-15 23:44:46 +01:00
|
|
|
c->set_profile = NULL;
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-03-01 21:34:01 +01:00
|
|
|
pa_device_init_description(c->proplist);
|
|
|
|
|
pa_device_init_icon(c->proplist, TRUE);
|
2009-06-08 16:58:45 +02:00
|
|
|
pa_device_init_intended_roles(c->proplist);
|
2009-03-01 21:34:01 +01:00
|
|
|
|
2009-01-15 18:29:16 +01:00
|
|
|
pa_assert_se(pa_idxset_put(core->cards, c, &c->index) >= 0);
|
|
|
|
|
|
|
|
|
|
pa_log_info("Created %u \"%s\"", c->index, c->name);
|
|
|
|
|
pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_NEW, c->index);
|
|
|
|
|
|
|
|
|
|
pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_PUT], c);
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_card_free(pa_card *c) {
|
|
|
|
|
pa_core *core;
|
|
|
|
|
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(c->core);
|
|
|
|
|
|
|
|
|
|
core = c->core;
|
|
|
|
|
|
|
|
|
|
pa_hook_fire(&core->hooks[PA_CORE_HOOK_CARD_UNLINK], c);
|
|
|
|
|
|
|
|
|
|
pa_namereg_unregister(core, c->name);
|
|
|
|
|
|
|
|
|
|
pa_idxset_remove_by_data(c->core->cards, c, NULL);
|
|
|
|
|
|
|
|
|
|
pa_log_info("Freed %u \"%s\"", c->index, c->name);
|
|
|
|
|
|
|
|
|
|
pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_REMOVE, c->index);
|
|
|
|
|
|
2009-01-15 18:38:20 +01:00
|
|
|
pa_assert(pa_idxset_isempty(c->sinks));
|
2009-01-15 18:29:16 +01:00
|
|
|
pa_idxset_free(c->sinks, NULL, NULL);
|
2009-01-15 18:38:20 +01:00
|
|
|
pa_assert(pa_idxset_isempty(c->sources));
|
2009-01-15 18:29:16 +01:00
|
|
|
pa_idxset_free(c->sources, NULL, NULL);
|
|
|
|
|
|
2011-11-25 15:17:13 +01:00
|
|
|
pa_device_port_hashmap_free(c->ports);
|
|
|
|
|
|
2009-01-20 03:24:40 +01:00
|
|
|
if (c->profiles) {
|
2009-06-17 03:15:36 +02:00
|
|
|
pa_card_profile *p;
|
|
|
|
|
|
2012-06-29 18:04:55 +03:00
|
|
|
while ((p = pa_hashmap_steal_first(c->profiles))) {
|
|
|
|
|
p->card = NULL;
|
2009-06-17 03:15:36 +02:00
|
|
|
pa_card_profile_free(p);
|
2012-06-29 18:04:55 +03:00
|
|
|
}
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-01-20 03:24:40 +01:00
|
|
|
pa_hashmap_free(c->profiles, NULL, NULL);
|
|
|
|
|
}
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
pa_proplist_free(c->proplist);
|
|
|
|
|
pa_xfree(c->driver);
|
|
|
|
|
pa_xfree(c->name);
|
|
|
|
|
pa_xfree(c);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-23 19:31:36 +01:00
|
|
|
int pa_card_set_profile(pa_card *c, const char *name, pa_bool_t save) {
|
2009-01-15 23:44:46 +01:00
|
|
|
pa_card_profile *profile;
|
2009-06-17 03:15:56 +02:00
|
|
|
int r;
|
2012-06-08 21:49:10 +03:00
|
|
|
|
2009-01-15 18:29:16 +01:00
|
|
|
pa_assert(c);
|
|
|
|
|
|
2009-01-15 23:44:46 +01:00
|
|
|
if (!c->set_profile) {
|
2009-06-17 03:15:56 +02:00
|
|
|
pa_log_debug("set_profile() operation not implemented for card %u \"%s\"", c->index, c->name);
|
|
|
|
|
return -PA_ERR_NOTIMPLEMENTED;
|
2009-01-15 18:29:16 +01:00
|
|
|
}
|
|
|
|
|
|
2012-06-08 21:49:10 +03:00
|
|
|
if (!name)
|
2009-06-17 03:15:56 +02:00
|
|
|
return -PA_ERR_NOENTITY;
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-01-15 23:44:46 +01:00
|
|
|
if (!(profile = pa_hashmap_get(c->profiles, name)))
|
2009-06-17 03:15:56 +02:00
|
|
|
return -PA_ERR_NOENTITY;
|
2009-01-15 18:29:16 +01:00
|
|
|
|
2009-03-23 19:31:36 +01:00
|
|
|
if (c->active_profile == profile) {
|
|
|
|
|
c->save_profile = c->save_profile || save;
|
2009-01-17 02:03:35 +01:00
|
|
|
return 0;
|
2009-03-23 19:31:36 +01:00
|
|
|
}
|
2009-01-17 02:03:35 +01:00
|
|
|
|
2009-06-17 03:15:56 +02:00
|
|
|
if ((r = c->set_profile(c, profile)) < 0)
|
|
|
|
|
return r;
|
2009-01-15 18:29:16 +01:00
|
|
|
|
|
|
|
|
pa_subscription_post(c->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, c->index);
|
|
|
|
|
|
2009-02-12 22:09:17 +01:00
|
|
|
pa_log_info("Changed profile of card %u \"%s\" to %s", c->index, c->name, profile->name);
|
2009-01-21 01:59:15 +01:00
|
|
|
|
|
|
|
|
c->active_profile = profile;
|
2009-03-23 19:31:36 +01:00
|
|
|
c->save_profile = save;
|
2009-01-21 01:59:15 +01:00
|
|
|
|
2011-02-25 10:27:23 +00:00
|
|
|
pa_hook_fire(&c->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], c);
|
|
|
|
|
|
2009-01-15 18:29:16 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2009-01-22 00:17:11 +01:00
|
|
|
|
2009-06-05 19:05:07 +02:00
|
|
|
int pa_card_suspend(pa_card *c, pa_bool_t suspend, pa_suspend_cause_t cause) {
|
2009-01-22 00:17:11 +01:00
|
|
|
pa_sink *sink;
|
|
|
|
|
pa_source *source;
|
|
|
|
|
uint32_t idx;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
pa_assert(c);
|
2009-06-05 19:05:07 +02:00
|
|
|
pa_assert(cause != 0);
|
2009-01-22 00:17:11 +01:00
|
|
|
|
2012-05-29 17:58:12 +05:30
|
|
|
PA_IDXSET_FOREACH(sink, c->sinks, idx) {
|
2009-06-17 03:15:56 +02:00
|
|
|
int r;
|
2009-01-22 00:17:11 +01:00
|
|
|
|
2009-06-17 03:15:56 +02:00
|
|
|
if ((r = pa_sink_suspend(sink, suspend, cause)) < 0)
|
|
|
|
|
ret = r;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 17:58:12 +05:30
|
|
|
PA_IDXSET_FOREACH(source, c->sources, idx) {
|
2009-06-17 03:15:56 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
if ((r = pa_source_suspend(source, suspend, cause)) < 0)
|
|
|
|
|
ret = r;
|
|
|
|
|
}
|
2009-01-22 00:17:11 +01:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|