4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/memchunk.h>
37 #include <pulsecore/sound-file.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/core-scache.h>
40 #include <pulsecore/core-subscribe.h>
44 static void entry_free(pa_autoload_entry *e) {
46 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_REMOVE, PA_INVALID_INDEX);
49 pa_xfree(e->argument);
53 static void entry_remove_and_free(pa_autoload_entry *e) {
56 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
57 pa_hashmap_remove(e->core->autoload_hashmap, e->name);
61 static pa_autoload_entry* entry_new(pa_core *c, const char *name) {
62 pa_autoload_entry *e = NULL;
65 if (c->autoload_hashmap && (e = pa_hashmap_get(c->autoload_hashmap, name)))
68 e = pa_xmalloc(sizeof(pa_autoload_entry));
70 e->name = pa_xstrdup(name);
71 e->module = e->argument = NULL;
74 if (!c->autoload_hashmap)
75 c->autoload_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
76 assert(c->autoload_hashmap);
78 pa_hashmap_put(c->autoload_hashmap, e->name, e);
80 if (!c->autoload_idxset)
81 c->autoload_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
82 pa_idxset_put(c->autoload_idxset, e, &e->index);
84 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_NEW, e->index);
89 int pa_autoload_add(pa_core *c, const char*name, pa_namereg_type_t type, const char*module, const char *argument, uint32_t *idx) {
90 pa_autoload_entry *e = NULL;
91 assert(c && name && module && (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE));
93 if (!(e = entry_new(c, name)))
96 e->module = pa_xstrdup(module);
97 e->argument = pa_xstrdup(argument);
106 int pa_autoload_remove_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
107 pa_autoload_entry *e;
108 assert(c && name && (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE));
110 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
113 entry_remove_and_free(e);
117 int pa_autoload_remove_by_index(pa_core *c, uint32_t idx) {
118 pa_autoload_entry *e;
119 assert(c && idx != PA_IDXSET_INVALID);
121 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
124 entry_remove_and_free(e);
128 void pa_autoload_request(pa_core *c, const char *name, pa_namereg_type_t type) {
129 pa_autoload_entry *e;
133 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || (e->type != type))
141 if (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) {
142 if ((m = pa_module_load(c, e->module, e->argument)))
149 static void free_func(void *p, PA_GCC_UNUSED void *userdata) {
150 pa_autoload_entry *e = p;
151 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
155 void pa_autoload_free(pa_core *c) {
156 if (c->autoload_hashmap) {
157 pa_hashmap_free(c->autoload_hashmap, free_func, NULL);
158 c->autoload_hashmap = NULL;
161 if (c->autoload_idxset) {
162 pa_idxset_free(c->autoload_idxset, NULL, NULL);
163 c->autoload_idxset = NULL;
167 const pa_autoload_entry* pa_autoload_get_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
168 pa_autoload_entry *e;
171 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
177 const pa_autoload_entry* pa_autoload_get_by_index(pa_core *c, uint32_t idx) {
178 pa_autoload_entry *e;
179 assert(c && idx != PA_IDXSET_INVALID);
181 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))