Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulsecore / autoload.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <pulse/xmalloc.h>
34
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>
41
42 #include "autoload.h"
43
44 static void entry_free(pa_autoload_entry *e) {
45     assert(e);
46     pa_subscription_post(e->core, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_REMOVE, PA_INVALID_INDEX);
47     pa_xfree(e->name);
48     pa_xfree(e->module);
49     pa_xfree(e->argument);
50     pa_xfree(e);
51 }
52
53 static void entry_remove_and_free(pa_autoload_entry *e) {
54     assert(e && e->core);
55
56     pa_idxset_remove_by_data(e->core->autoload_idxset, e, NULL);
57     pa_hashmap_remove(e->core->autoload_hashmap, e->name);
58     entry_free(e);
59 }
60
61 static pa_autoload_entry* entry_new(pa_core *c, const char *name) {
62     pa_autoload_entry *e = NULL;
63     assert(c && name);
64
65     if (c->autoload_hashmap && (e = pa_hashmap_get(c->autoload_hashmap, name)))
66         return NULL;
67
68     e = pa_xmalloc(sizeof(pa_autoload_entry));
69     e->core = c;
70     e->name = pa_xstrdup(name);
71     e->module = e->argument = NULL;
72     e->in_action = 0;
73
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);
77
78     pa_hashmap_put(c->autoload_hashmap, e->name, e);
79
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);
83
84     pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_AUTOLOAD|PA_SUBSCRIPTION_EVENT_NEW, e->index);
85
86     return e;
87 }
88
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));
92
93     if (!(e = entry_new(c, name)))
94         return -1;
95
96     e->module = pa_xstrdup(module);
97     e->argument = pa_xstrdup(argument);
98     e->type = type;
99
100     if (idx)
101         *idx = e->index;
102
103     return 0;
104 }
105
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));
109
110     if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
111         return -1;
112
113     entry_remove_and_free(e);
114     return 0;
115 }
116
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);
120
121     if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
122         return -1;
123
124     entry_remove_and_free(e);
125     return 0;
126 }
127
128 void pa_autoload_request(pa_core *c, const char *name, pa_namereg_type_t type) {
129     pa_autoload_entry *e;
130     pa_module *m;
131     assert(c && name);
132
133     if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || (e->type != type))
134         return;
135
136     if (e->in_action)
137         return;
138
139     e->in_action = 1;
140
141     if (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE) {
142         if ((m = pa_module_load(c, e->module, e->argument)))
143             m->auto_unload = 1;
144     }
145
146     e->in_action = 0;
147 }
148
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);
152     entry_free(e);
153 }
154
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;
159     }
160
161     if (c->autoload_idxset) {
162         pa_idxset_free(c->autoload_idxset, NULL, NULL);
163         c->autoload_idxset = NULL;
164     }
165 }
166
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;
169     assert(c && name);
170
171     if (!c->autoload_hashmap || !(e = pa_hashmap_get(c->autoload_hashmap, name)) || e->type != type)
172         return NULL;
173
174     return e;
175 }
176
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);
180
181     if (!c->autoload_idxset || !(e = pa_idxset_get_by_index(c->autoload_idxset, idx)))
182         return NULL;
183
184     return e;
185 }