2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
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.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-29 20:37:24 +00:00
|
|
|
#include <stdlib.h>
|
2004-06-27 22:42:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/source.h>
|
|
|
|
|
#include <pulsecore/sink.h>
|
|
|
|
|
#include <pulsecore/core-subscribe.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-27 22:42:17 +00:00
|
|
|
#include "namereg.h"
|
|
|
|
|
|
|
|
|
|
struct namereg_entry {
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_namereg_type_t type;
|
2004-06-27 22:42:17 +00:00
|
|
|
char *name;
|
|
|
|
|
void *data;
|
|
|
|
|
};
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
static pa_bool_t is_valid_char(char c) {
|
2006-08-11 16:38:51 +00:00
|
|
|
return
|
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
|
(c >= '0' && c <= '9') ||
|
|
|
|
|
c == '.' ||
|
2008-10-03 19:49:15 +02:00
|
|
|
c == '-' ||
|
2006-08-11 16:38:51 +00:00
|
|
|
c == '_';
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-29 20:01:49 +00:00
|
|
|
pa_bool_t pa_namereg_is_valid_name(const char *name) {
|
2006-08-11 16:38:51 +00:00
|
|
|
const char *c;
|
|
|
|
|
|
|
|
|
|
if (*name == 0)
|
2007-10-29 20:01:49 +00:00
|
|
|
return FALSE;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
for (c = name; *c && (c-name < PA_NAME_MAX); c++)
|
|
|
|
|
if (!is_valid_char(*c))
|
2007-10-29 20:01:49 +00:00
|
|
|
return FALSE;
|
2006-08-11 16:38:51 +00:00
|
|
|
|
|
|
|
|
if (*c)
|
2007-10-29 20:01:49 +00:00
|
|
|
return FALSE;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-29 20:01:49 +00:00
|
|
|
return TRUE;
|
2006-08-11 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
2008-06-26 00:27:02 +02:00
|
|
|
char* pa_namereg_make_valid_name(const char *name) {
|
2006-08-11 16:38:51 +00:00
|
|
|
const char *a;
|
|
|
|
|
char *b, *n;
|
|
|
|
|
|
|
|
|
|
if (*name == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
n = pa_xmalloc(strlen(name)+1);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
for (a = name, b = n; *a && (a-name < PA_NAME_MAX); a++, b++)
|
2008-08-19 22:39:54 +02:00
|
|
|
*b = (char) (is_valid_char(*a) ? *a : '_');
|
2006-08-11 16:38:51 +00:00
|
|
|
|
|
|
|
|
*b = 0;
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-03 17:14:35 +02:00
|
|
|
const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t type, void *data, pa_bool_t fail) {
|
2004-06-27 22:42:17 +00:00
|
|
|
struct namereg_entry *e;
|
|
|
|
|
char *n = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(name);
|
|
|
|
|
pa_assert(data);
|
2006-07-27 18:02:59 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
if (!*name)
|
2006-07-27 18:02:59 +00:00
|
|
|
return NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-15 18:29:16 +01:00
|
|
|
if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
|
2008-06-26 00:27:02 +02:00
|
|
|
!pa_namereg_is_valid_name(name)) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
if (fail)
|
|
|
|
|
return NULL;
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-24 23:50:12 +01:00
|
|
|
if (!(name = n = pa_namereg_make_valid_name(name)))
|
2006-08-11 16:38:51 +00:00
|
|
|
return NULL;
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
|
|
|
|
|
pa_xfree(n);
|
2004-06-27 22:42:17 +00:00
|
|
|
return NULL;
|
2006-08-11 16:38:51 +00:00
|
|
|
}
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
if (e) {
|
2004-06-27 22:42:17 +00:00
|
|
|
unsigned i;
|
|
|
|
|
size_t l = strlen(name);
|
2006-08-11 16:38:51 +00:00
|
|
|
char *k;
|
|
|
|
|
|
|
|
|
|
if (l+4 > PA_NAME_MAX) {
|
|
|
|
|
pa_xfree(n);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
k = pa_xmalloc(l+4);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
for (i = 2; i <= 99; i++) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_snprintf(k, l+4, "%s.%u", name, i);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
if (!(e = pa_hashmap_get(c->namereg, k)))
|
2004-06-27 22:42:17 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e) {
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(n);
|
2006-08-11 16:38:51 +00:00
|
|
|
pa_xfree(k);
|
2004-06-27 22:42:17 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
pa_xfree(n);
|
|
|
|
|
n = k;
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-11 16:38:51 +00:00
|
|
|
e = pa_xnew(struct namereg_entry, 1);
|
2004-06-27 22:42:17 +00:00
|
|
|
e->type = type;
|
2006-08-11 16:38:51 +00:00
|
|
|
e->name = n ? n : pa_xstrdup(name);
|
2004-06-27 22:42:17 +00:00
|
|
|
e->data = data;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
|
|
|
|
return e->name;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_namereg_unregister(pa_core *c, const char *name) {
|
2004-06-27 22:42:17 +00:00
|
|
|
struct namereg_entry *e;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(name);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if (c->default_sink == e->data)
|
|
|
|
|
pa_namereg_set_default_sink(c, NULL);
|
|
|
|
|
else if (c->default_source == e->data)
|
|
|
|
|
pa_namereg_set_default_source(c, NULL);
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(e->name);
|
|
|
|
|
pa_xfree(e);
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-15 20:07:13 +01:00
|
|
|
void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
|
2004-06-27 22:42:17 +00:00
|
|
|
struct namereg_entry *e;
|
2006-01-11 01:17:39 +00:00
|
|
|
uint32_t idx;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
|
|
|
|
|
pa_source *s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if ((s = pa_namereg_get_default_source(c)))
|
|
|
|
|
return s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
} else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
|
|
|
|
|
pa_sink *s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if ((s = pa_namereg_get_default_sink(c)))
|
|
|
|
|
return s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
} else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
|
|
|
|
|
pa_sink *s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
|
|
|
|
|
return s->monitor_source;
|
|
|
|
|
|
|
|
|
|
}
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if (*name == '@' || !name || !pa_namereg_is_valid_name(name))
|
2004-08-04 16:39:30 +00:00
|
|
|
return NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-29 16:25:29 +01:00
|
|
|
if ((e = pa_hashmap_get(c->namereg, name)))
|
2006-04-27 22:59:54 +00:00
|
|
|
if (e->type == type)
|
2004-06-27 22:42:17 +00:00
|
|
|
return e->data;
|
|
|
|
|
|
2009-01-15 20:07:13 +01:00
|
|
|
if (pa_atou(name, &idx) < 0)
|
2004-06-29 20:37:24 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (type == PA_NAMEREG_SINK)
|
2006-01-11 01:17:39 +00:00
|
|
|
return pa_idxset_get_by_index(c->sinks, idx);
|
2004-07-03 23:35:12 +00:00
|
|
|
else if (type == PA_NAMEREG_SOURCE)
|
2006-01-11 01:17:39 +00:00
|
|
|
return pa_idxset_get_by_index(c->sources, idx);
|
2004-08-19 23:14:59 +00:00
|
|
|
else if (type == PA_NAMEREG_SAMPLE && c->scache)
|
2006-01-11 01:17:39 +00:00
|
|
|
return pa_idxset_get_by_index(c->scache, idx);
|
2009-01-21 01:53:09 +01:00
|
|
|
else if (type == PA_NAMEREG_CARD)
|
|
|
|
|
return pa_idxset_get_by_index(c->cards, idx);
|
2004-12-11 00:10:41 +00:00
|
|
|
|
|
|
|
|
return NULL;
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if (c->default_sink != s) {
|
|
|
|
|
c->default_sink = s;
|
|
|
|
|
pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
|
|
|
|
|
}
|
2006-08-11 16:38:51 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
return s;
|
|
|
|
|
}
|
2006-08-11 16:38:51 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
|
|
|
|
|
pa_assert(c);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if (c->default_source != s) {
|
|
|
|
|
c->default_source = s;
|
|
|
|
|
pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
|
|
|
|
|
}
|
2006-08-11 16:38:51 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
return s;
|
2004-08-04 16:39:30 +00:00
|
|
|
}
|
2004-09-07 14:58:42 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
pa_sink *pa_namereg_get_default_sink(pa_core *c) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sink *s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2004-09-07 14:58:42 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if (c->default_sink)
|
|
|
|
|
return c->default_sink;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-07 14:58:42 +00:00
|
|
|
if ((s = pa_idxset_first(c->sinks, NULL)))
|
2009-01-28 01:46:27 +01:00
|
|
|
return pa_namereg_set_default_sink(c, s);
|
2004-09-07 14:58:42 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
return NULL;
|
2004-09-07 14:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
pa_source *pa_namereg_get_default_source(pa_core *c) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_source *s;
|
|
|
|
|
uint32_t idx;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2004-09-07 14:58:42 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if (c->default_source)
|
|
|
|
|
return c->default_source;
|
2004-09-07 14:58:42 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
for (s = PA_SOURCE(pa_idxset_first(c->sources, &idx)); s; s = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
|
|
|
|
|
if (!s->monitor_of)
|
|
|
|
|
return pa_namereg_set_default_source(c, s);
|
2004-09-07 14:58:42 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
if ((s = pa_idxset_first(c->sources, NULL)))
|
|
|
|
|
return pa_namereg_set_default_source(c, s);
|
2004-09-07 14:58:42 +00:00
|
|
|
|
2009-01-28 01:46:27 +01:00
|
|
|
return NULL;
|
2004-09-07 14:58:42 +00:00
|
|
|
}
|