1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
* Control Interface - main file
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "control_local.h"
snd_ctl_type_t snd_ctl_type(snd_ctl_t *ctl)
{
return ctl->type;
}
int snd_ctl_close(snd_ctl_t *ctl)
{
int res;
assert(ctl);
res = ctl->ops->close(ctl);
free(ctl);
return res;
}
int snd_ctl_poll_descriptor(snd_ctl_t *ctl)
{
assert(ctl);
return ctl->ops->poll_descriptor(ctl);
}
int snd_ctl_info(snd_ctl_t *ctl, snd_ctl_info_t *info)
{
assert(ctl && info);
return ctl->ops->hw_info(ctl, info);
}
int snd_ctl_clist(snd_ctl_t *ctl, snd_control_list_t *list)
{
assert(ctl && list);
return ctl->ops->clist(ctl, list);
}
int snd_ctl_cinfo(snd_ctl_t *ctl, snd_control_info_t *info)
{
assert(ctl && info && (info->id.name[0] || info->id.numid));
return ctl->ops->cinfo(ctl, info);
}
int snd_ctl_cread(snd_ctl_t *ctl, snd_control_t *control)
{
assert(ctl && control && (control->id.name[0] || control->id.numid));
return ctl->ops->cread(ctl, control);
}
int snd_ctl_cwrite(snd_ctl_t *ctl, snd_control_t *control)
{
assert(ctl && control && (control->id.name[0] || control->id.numid));
return ctl->ops->cwrite(ctl, control);
}
int snd_ctl_hwdep_next_device(snd_ctl_t *ctl, int *device)
{
assert(ctl && device);
return ctl->ops->hwdep_next_device(ctl, device);
}
int snd_ctl_hwdep_info(snd_ctl_t *ctl, snd_hwdep_info_t * info)
{
assert(ctl && info);
return ctl->ops->hwdep_info(ctl, info);
}
int snd_ctl_pcm_next_device(snd_ctl_t *ctl, int * device)
{
assert(ctl && device);
return ctl->ops->pcm_next_device(ctl, device);
}
int snd_ctl_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t * info)
{
assert(ctl && info);
return ctl->ops->pcm_info(ctl, info);
}
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
assert(ctl);
return ctl->ops->pcm_prefer_subdevice(ctl, subdev);
}
int snd_ctl_rawmidi_next_device(snd_ctl_t *ctl, int * device)
{
assert(ctl && device);
return ctl->ops->rawmidi_next_device(ctl, device);
}
int snd_ctl_rawmidi_info(snd_ctl_t *ctl, snd_rawmidi_info_t * info)
{
assert(ctl && info);
return ctl->ops->rawmidi_info(ctl, info);
}
int snd_ctl_rawmidi_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
assert(ctl);
return ctl->ops->rawmidi_prefer_subdevice(ctl, subdev);
}
int snd_ctl_read1(snd_ctl_t *ctl, snd_ctl_event_t *event)
{
assert(ctl && event);
return ctl->ops->read(ctl, event);
}
int snd_ctl_read(snd_ctl_t *ctl, snd_ctl_callbacks_t * callbacks)
{
int result, count;
snd_ctl_event_t r;
assert(ctl);
count = 0;
while ((result = snd_ctl_read1(ctl, &r)) > 0) {
if (result != sizeof(r))
return -EIO;
if (!callbacks)
continue;
switch (r.type) {
case SND_CTL_EVENT_REBUILD:
if (callbacks->rebuild)
callbacks->rebuild(ctl, callbacks->private_data);
break;
case SND_CTL_EVENT_VALUE:
if (callbacks->value)
callbacks->value(ctl, callbacks->private_data, &r.data.id);
break;
case SND_CTL_EVENT_CHANGE:
if (callbacks->change)
callbacks->change(ctl, callbacks->private_data, &r.data.id);
break;
case SND_CTL_EVENT_ADD:
if (callbacks->add)
callbacks->add(ctl, callbacks->private_data, &r.data.id);
break;
case SND_CTL_EVENT_REMOVE:
if (callbacks->remove)
callbacks->remove(ctl, callbacks->private_data, &r.data.id);
break;
}
count++;
}
return result >= 0 ? count : -errno;
}
int snd_ctl_open(snd_ctl_t **ctlp, char *name)
{
const char *str;
int err;
snd_config_t *ctl_conf, *conf, *type_conf;
snd_config_iterator_t i;
const char *lib = NULL, *open = NULL;
int (*open_func)(snd_ctl_t **ctlp, char *name, snd_config_t *conf);
void *h;
assert(ctlp && name);
err = snd_config_update();
if (err < 0)
return err;
err = snd_config_searchv(snd_config, &ctl_conf, "ctl", name, 0);
if (err < 0) {
int card;
char socket[256], sname[256];
err = sscanf(name, "hw:%d", &card);
if (err == 1)
return snd_ctl_hw_open(ctlp, NULL, card);
err = sscanf(name, "shm:%256[^,],%256[^,]", socket, sname);
if (err == 2)
return snd_ctl_shm_open(ctlp, NULL, socket, sname);
ERR("Unknown control %s", name);
return -ENOENT;
}
if (snd_config_get_type(ctl_conf) != SND_CONFIG_TYPE_COMPOUND)
return -EINVAL;
err = snd_config_search(ctl_conf, "type", &conf);
if (err < 0)
return err;
err = snd_config_get_string(conf, &str);
if (err < 0)
return err;
err = snd_config_searchv(snd_config, &type_conf, "ctltype", str, 0);
snd_config_foreach(i, type_conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id = snd_config_get_id(n);
if (strcmp(id, "comment") == 0)
continue;
if (strcmp(id, "lib") == 0) {
err = snd_config_get_string(n, &lib);
if (err < 0)
return -EINVAL;
continue;
}
if (strcmp(id, "open") == 0) {
err = snd_config_get_string(n, &open);
if (err < 0)
return -EINVAL;
continue;
return -EINVAL;
}
}
if (!open)
return -EINVAL;
if (!lib)
lib = "libasound.so";
h = dlopen(lib, RTLD_NOW);
if (!h)
return -ENOENT;
open_func = dlsym(h, open);
dlclose(h);
if (!open_func)
return -ENXIO;
return open_func(ctlp, name, ctl_conf);
}
void snd_control_set_bytes(snd_control_t *obj, void *data, size_t size)
{
assert(obj);
assert(size <= sizeof(obj->value.bytes.data));
memcpy(obj->value.bytes.data, data, size);
}
#define TYPE(v) [SND_CONTROL_TYPE_##v] = #v
#define IFACE(v) [SND_CONTROL_IFACE_##v] = #v
#define EVENT(v) [SND_CTL_EVENT_##v] = #v
const char *snd_control_type_names[] = {
TYPE(NONE),
TYPE(BOOLEAN),
TYPE(INTEGER),
TYPE(ENUMERATED),
TYPE(BYTES),
TYPE(IEC958),
};
const char *snd_control_iface_names[] = {
IFACE(CARD),
IFACE(HWDEP),
IFACE(MIXER),
IFACE(PCM),
IFACE(RAWMIDI),
IFACE(TIMER),
IFACE(SEQUENCER),
};
const char *snd_ctl_event_type_names[] = {
EVENT(REBUILD),
EVENT(VALUE),
EVENT(CHANGE),
EVENT(ADD),
EVENT(REMOVE),
};
const char *snd_control_type_name(snd_control_type_t type)
{
assert(type <= SND_CONTROL_TYPE_LAST);
return snd_control_type_names[snd_enum_to_int(type)];
}
const char *snd_control_iface_name(snd_control_iface_t iface)
{
assert(iface <= SND_CONTROL_IFACE_LAST);
return snd_control_iface_names[snd_enum_to_int(iface)];
}
const char *snd_ctl_event_type_name(snd_ctl_event_type_t type)
{
assert(type <= SND_CTL_EVENT_LAST);
return snd_ctl_event_type_names[snd_enum_to_int(type)];
}
int snd_control_list_alloc_space(snd_control_list_t *obj, unsigned int entries)
{
obj->pids = calloc(entries, sizeof(*obj->pids));
if (!obj->pids) {
obj->space = 0;
return -ENOMEM;
}
obj->space = entries;
return 0;
}
void snd_control_list_free_space(snd_control_list_t *obj)
{
free(obj->pids);
obj->pids = NULL;
}
|