]> code.delx.au - pulseaudio/blob - src/pulsecore/autoload.c
Huge trailing whitespace cleanup. Let's keep the tree pure from here on,
[pulseaudio] / src / pulsecore / autoload.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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 of the License,
9 or (at your option) any later version.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
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/core-scache.h>
37 #include <pulsecore/core-subscribe.h>
38
39 #include "autoload.h"
40
41 static void entry_free(pa_autoload_entry *e) {
42 assert(e);
43 pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_REMOVE, PA_INVALID_INDEX);
44 pa_xfree(e->name);
45 pa_xfree(e->module);
46 pa_xfree(e->argument);
47 pa_xfree(e);
48 }
49
50 static void entry_remove_and_free(pa_autoload_entry *e) {
51 assert(e && e->core);
52
53 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
54 pa_hashmap_remove(e->core->autoload_hashmap, e->name);
55 entry_free(e);
56 }
57
58 static pa_autoload_entry* entry_new(pa_core *c, const char *name) {
59 pa_autoload_entry *e = NULL;
60 assert(c && name);
61
62 if (c->autoload_hashmap && (e = pa_hashmap_get(c->autoload_hashmap, name)))
63 return NULL;
64
65 e = pa_xmalloc(sizeof(pa_autoload_entry));
66 e->core = c;
67 e->name = pa_xstrdup(name);
68 e->module = e->argument = NULL;
69 e->in_action = 0;
70
71 if (!c->autoload_hashmap)
72 c->autoload_hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
73 assert(c->autoload_hashmap);
74
75 pa_hashmap_put(c->autoload_hashmap, e->name, e);
76
77 if (!c->autoload_idxset)
78 c->autoload_idxset = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
79 pa_idxset_put(c->autoload_idxset, e, &e->index);
80
81 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_NEW, e->index);
82
83 return e;
84 }
85
86 int pa_autoload_add(pa_core *c, const char*name, pa_namereg_type_t type, const char*module, const char *argument, uint32_t *idx) {
87 pa_autoload_entry *e = NULL;
88 assert(c && name && module && (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE));
89
90 if (!(e = entry_new(c, name)))
91 return -1;
92
93 e->module = pa_xstrdup(module);
94 e->argument = pa_xstrdup(argument);
95 e->type = type;
96
97 if (idx)
98 *idx = e->index;
99
100 return 0;
101 }
102
103 int pa_autoload_remove_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
104 pa_autoload_entry *e;
105 assert(c && name && (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE));
106
107 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
108 return -1;
109
110 entry_remove_and_free(e);
111 return 0;
112 }
113
114 int pa_autoload_remove_by_index(pa_core *c, uint32_t idx) {
115 pa_autoload_entry *e;
116 assert(c && idx != PA_IDXSET_INVALID);
117
118 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
119 return -1;
120
121 entry_remove_and_free(e);
122 return 0;
123 }
124
125 void pa_autoload_request(pa_core *c, const char *name, pa_namereg_type_t type) {
126 pa_autoload_entry *e;
127 pa_module *m;
128 assert(c && name);
129
130 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || (e->type != type))
131 return;
132
133 if (e->in_action)
134 return;
135
136 e->in_action = 1;
137
138 if (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) {
139 if ((m = pa_module_load(c, e->module, e->argument)))
140 m->auto_unload = 1;
141 }
142
143 e->in_action = 0;
144 }
145
146 static void free_func(void *p, PA_GCC_UNUSED void *userdata) {
147 pa_autoload_entry *e = p;
148 pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
149 entry_free(e);
150 }
151
152 void pa_autoload_free(pa_core *c) {
153 if (c->autoload_hashmap) {
154 pa_hashmap_free(c->autoload_hashmap, free_func, NULL);
155 c->autoload_hashmap = NULL;
156 }
157
158 if (c->autoload_idxset) {
159 pa_idxset_free(c->autoload_idxset, NULL, NULL);
160 c->autoload_idxset = NULL;
161 }
162 }
163
164 const pa_autoload_entry* pa_autoload_get_by_name(pa_core *c, const char*name, pa_namereg_type_t type) {
165 pa_autoload_entry *e;
166 assert(c && name);
167
168 if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
169 return NULL;
170
171 return e;
172 }
173
174 const pa_autoload_entry* pa_autoload_get_by_index(pa_core *c, uint32_t idx) {
175 pa_autoload_entry *e;
176 assert(c && idx != PA_IDXSET_INVALID);
177
178 if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
179 return NULL;
180
181 return e;
182 }