]> code.delx.au - pulseaudio/blob - src/pulsecore/autoload.c
Merge dead branch 'liboil-test'
[pulseaudio] / src / pulsecore / autoload.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <pulse/xmalloc.h>
31
32 #include <pulsecore/module.h>
33 #include <pulsecore/memchunk.h>
34 #include <pulsecore/sound-file.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-scache.h>
38 #include <pulsecore/core-subscribe.h>
39
40 #include "autoload.h"
41
42 static void entry_free(pa_autoload_entry *e) {
43 pa_assert(e);
44 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_REMOVE, PA_INVALID_INDEX);
45 pa_xfree(e->name);
46 pa_xfree(e->module);
47 pa_xfree(e->argument);
48 pa_xfree(e);
49 }
50
51 static void entry_remove_and_free(pa_autoload_entry *e) {
52 pa_assert(e);
53 pa_assert(e->core);
54
55 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
56 pa_hashmap_remove(e->core->autoload_hashmap, e->name);
57 entry_free(e);
58 }
59
60 static pa_autoload_entry* entry_new(pa_core *c, const char *name) {
61 pa_autoload_entry *e = NULL;
62
63 pa_core_assert_ref(c);
64 pa_assert(name);
65
66 if (c->autoload_hashmap && (e = pa_hashmap_get(c->autoload_hashmap, name)))
67 return NULL;
68
69 e = pa_xnew(pa_autoload_entry, 1);
70 e->core = c;
71 e->name = pa_xstrdup(name);
72 e->module = e->argument = NULL;
73 e->in_action = 0;
74
75 if (!c->autoload_hashmap)
76 c->autoload_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
77 pa_assert(c->autoload_hashmap);
78
79 pa_hashmap_put(c->autoload_hashmap, e->name, e);
80
81 if (!c->autoload_idxset)
82 c->autoload_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
83 pa_idxset_put(c->autoload_idxset, e, &e->index);
84
85 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_NEW, e->index);
86
87 return e;
88 }
89
90 int pa_autoload_add(pa_core *c, const char*name, pa_namereg_type_t type, const char*module, const char *argument, uint32_t *idx) {
91 pa_autoload_entry *e = NULL;
92
93 pa_assert(c);
94 pa_assert(name);
95 pa_assert(module);
96 pa_assert(type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE);
97
98 if (!(e = entry_new(c, name)))
99 return -1;
100
101 e->module = pa_xstrdup(module);
102 e->argument = pa_xstrdup(argument);
103 e->type = type;
104
105 if (idx)
106 *idx = e->index;
107
108 return 0;
109 }
110
111 int pa_autoload_remove_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
112 pa_autoload_entry *e;
113
114 pa_assert(c);
115 pa_assert(name);
116 pa_assert(type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE);
117
118 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
119 return -1;
120
121 entry_remove_and_free(e);
122 return 0;
123 }
124
125 int pa_autoload_remove_by_index(pa_core *c, uint32_t idx) {
126 pa_autoload_entry *e;
127
128 pa_assert(c);
129 pa_assert(idx != PA_IDXSET_INVALID);
130
131 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
132 return -1;
133
134 entry_remove_and_free(e);
135 return 0;
136 }
137
138 void pa_autoload_request(pa_core *c, const char *name, pa_namereg_type_t type) {
139 pa_autoload_entry *e;
140 pa_module *m;
141
142 pa_assert(c);
143 pa_assert(name);
144
145 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || (e->type != type))
146 return;
147
148 if (e->in_action)
149 return;
150
151 e->in_action = 1;
152
153 if (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) {
154 if ((m = pa_module_load(c, e->module, e->argument)))
155 m->auto_unload = 1;
156 }
157
158 e->in_action = 0;
159 }
160
161 static void free_func(void *p, PA_GCC_UNUSED void *userdata) {
162 pa_autoload_entry *e = p;
163 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
164 entry_free(e);
165 }
166
167 void pa_autoload_free(pa_core *c) {
168
169 if (c->autoload_hashmap) {
170 pa_hashmap_free(c->autoload_hashmap, free_func, NULL);
171 c->autoload_hashmap = NULL;
172 }
173
174 if (c->autoload_idxset) {
175 pa_idxset_free(c->autoload_idxset, NULL, NULL);
176 c->autoload_idxset = NULL;
177 }
178 }
179
180 const pa_autoload_entry* pa_autoload_get_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
181 pa_autoload_entry *e;
182
183 pa_core_assert_ref(c);
184 pa_assert(name);
185
186 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
187 return NULL;
188
189 return e;
190 }
191
192 const pa_autoload_entry* pa_autoload_get_by_index(pa_core *c, uint32_t idx) {
193 pa_autoload_entry *e;
194
195 pa_core_assert_ref(c);
196 pa_assert(idx != PA_IDXSET_INVALID);
197
198 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
199 return NULL;
200
201 return e;
202 }