]> code.delx.au - pulseaudio/blob - polyp/namereg.c
change the way the default sink/source is selected
[pulseaudio] / polyp / namereg.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; 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 <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <string.h>
30 #include <malloc.h>
31 #include <stdio.h>
32
33 #include "namereg.h"
34 #include "autoload.h"
35 #include "source.h"
36 #include "sink.h"
37 #include "xmalloc.h"
38 #include "subscribe.h"
39
40 struct namereg_entry {
41 enum pa_namereg_type type;
42 char *name;
43 void *data;
44 };
45
46 void pa_namereg_free(struct pa_core *c) {
47 assert(c);
48 if (!c->namereg)
49 return;
50 assert(pa_hashmap_ncontents(c->namereg) == 0);
51 pa_hashmap_free(c->namereg, NULL, NULL);
52 }
53
54 const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_namereg_type type, void *data, int fail) {
55 struct namereg_entry *e;
56 char *n = NULL;
57 int r;
58
59 assert(c && name && data);
60
61 if (!c->namereg) {
62 c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
63 assert(c->namereg);
64 }
65
66 if ((e = pa_hashmap_get(c->namereg, name)) && fail)
67 return NULL;
68
69 if (!e)
70 n = pa_xstrdup(name);
71 else {
72 unsigned i;
73 size_t l = strlen(name);
74 n = pa_xmalloc(l+3);
75
76 for (i = 1; i <= 99; i++) {
77 snprintf(n, l+2, "%s%u", name, i);
78
79 if (!(e = pa_hashmap_get(c->namereg, n)))
80 break;
81 }
82
83 if (e) {
84 pa_xfree(n);
85 return NULL;
86 }
87 }
88
89 assert(n);
90 e = pa_xmalloc(sizeof(struct namereg_entry));
91 e->type = type;
92 e->name = n;
93 e->data = data;
94
95 r = pa_hashmap_put(c->namereg, e->name, e);
96 assert (r >= 0);
97
98 return e->name;
99
100 }
101
102 void pa_namereg_unregister(struct pa_core *c, const char *name) {
103 struct namereg_entry *e;
104 int r;
105 assert(c && name);
106
107 e = pa_hashmap_get(c->namereg, name);
108 assert(e);
109
110 r = pa_hashmap_remove(c->namereg, name);
111 assert(r >= 0);
112
113 pa_xfree(e->name);
114 pa_xfree(e);
115 }
116
117 void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type, int autoload) {
118 struct namereg_entry *e;
119 uint32_t index;
120 char *x = NULL;
121 void *d = NULL;
122 assert(c);
123
124 if (!name) {
125 if (type == PA_NAMEREG_SOURCE)
126 name = pa_namereg_get_default_source_name(c);
127 else if (type == PA_NAMEREG_SINK)
128 name = pa_namereg_get_default_sink_name(c);
129 }
130
131 if (!name)
132 return NULL;
133
134 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
135 if (e->type == e->type)
136 return e->data;
137
138 index = (uint32_t) strtol(name, &x, 0);
139
140 if (!x || *x != 0) {
141
142 if (autoload) {
143 pa_autoload_request(c, name, type);
144
145 if (c->namereg && (e = pa_hashmap_get(c->namereg, name)))
146 if (e->type == e->type)
147 return e->data;
148 }
149
150 return NULL;
151 }
152
153 if (type == PA_NAMEREG_SINK)
154 d = pa_idxset_get_by_index(c->sinks, index);
155 else if (type == PA_NAMEREG_SOURCE)
156 d = pa_idxset_get_by_index(c->sources, index);
157 else if (type == PA_NAMEREG_SAMPLE && c->scache)
158 d = pa_idxset_get_by_index(c->scache, index);
159
160 return d;
161 }
162
163 void pa_namereg_set_default(struct pa_core*c, const char *name, enum pa_namereg_type type) {
164 char **s;
165 assert(c && (type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE));
166
167 s = type == PA_NAMEREG_SINK ? &c->default_sink_name : &c->default_source_name;
168 assert(s);
169
170 if (!name && !*s)
171 return;
172
173 if (name && *s && !strcmp(name, *s))
174 return;
175
176 pa_xfree(*s);
177 *s = pa_xstrdup(name);
178 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
179 }
180
181 const char *pa_namereg_get_default_sink_name(struct pa_core *c) {
182 struct pa_sink *s;
183 assert(c);
184
185 if (c->default_sink_name)
186 return c->default_sink_name;
187
188 if ((s = pa_idxset_first(c->sinks, NULL)))
189 pa_namereg_set_default(c, s->name, PA_NAMEREG_SINK);
190
191 if (c->default_sink_name)
192 return c->default_sink_name;
193
194 return NULL;
195 }
196
197 const char *pa_namereg_get_default_source_name(struct pa_core *c) {
198 struct pa_source *s;
199 uint32_t index;
200
201 assert(c);
202
203 if (c->default_source_name)
204 return c->default_source_name;
205
206 for (s = pa_idxset_first(c->sources, &index); s; s = pa_idxset_next(c->sources, &index))
207 if (!s->monitor_of) {
208 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
209 break;
210 }
211
212 if (!c->default_source_name)
213 if ((s = pa_idxset_first(c->sources, NULL)))
214 pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE);
215
216 if (c->default_source_name)
217 return c->default_source_name;
218
219 return NULL;
220 }