2 * Control Interface - main file
3 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "control_local.h"
32 const char *snd_ctl_name(snd_ctl_t *ctl)
38 snd_ctl_type_t snd_ctl_type(snd_ctl_t *ctl)
44 int snd_ctl_close(snd_ctl_t *ctl)
47 res = ctl->ops->close(ctl);
54 int snd_ctl_nonblock(snd_ctl_t *ctl, int nonblock)
58 err = ctl->ops->nonblock(ctl, nonblock);
61 ctl->nonblock = nonblock;
65 int snd_ctl_async(snd_ctl_t *ctl, int sig, pid_t pid)
68 return ctl->ops->async(ctl, sig, pid);
71 int _snd_ctl_poll_descriptor(snd_ctl_t *ctl)
74 return ctl->ops->poll_descriptor(ctl);
77 int snd_ctl_poll_descriptors_count(snd_ctl_t *ctl)
83 int snd_ctl_poll_descriptors(snd_ctl_t *ctl, struct pollfd *pfds, unsigned int space)
87 pfds->fd = ctl->ops->poll_descriptor(ctl);
88 pfds->events = POLLIN;
94 int snd_ctl_subscribe_events(snd_ctl_t *ctl, int subscribe)
97 return ctl->ops->subscribe_events(ctl, subscribe);
100 int snd_ctl_card_info(snd_ctl_t *ctl, snd_ctl_card_info_t *info)
103 return ctl->ops->card_info(ctl, info);
106 int snd_ctl_elem_list(snd_ctl_t *ctl, snd_ctl_elem_list_t *list)
109 assert(list->space == 0 || list->pids);
110 return ctl->ops->element_list(ctl, list);
113 int snd_ctl_elem_info(snd_ctl_t *ctl, snd_ctl_elem_info_t *info)
115 assert(ctl && info && (info->id.name[0] || info->id.numid));
116 return ctl->ops->element_info(ctl, info);
119 int snd_ctl_elem_read(snd_ctl_t *ctl, snd_ctl_elem_value_t *control)
121 assert(ctl && control && (control->id.name[0] || control->id.numid));
122 return ctl->ops->element_read(ctl, control);
125 int snd_ctl_elem_write(snd_ctl_t *ctl, snd_ctl_elem_value_t *control)
127 assert(ctl && control && (control->id.name[0] || control->id.numid));
128 return ctl->ops->element_write(ctl, control);
131 int snd_ctl_hwdep_next_device(snd_ctl_t *ctl, int *device)
133 assert(ctl && device);
134 return ctl->ops->hwdep_next_device(ctl, device);
137 int snd_ctl_hwdep_info(snd_ctl_t *ctl, snd_hwdep_info_t * info)
140 return ctl->ops->hwdep_info(ctl, info);
143 int snd_ctl_pcm_next_device(snd_ctl_t *ctl, int * device)
145 assert(ctl && device);
146 return ctl->ops->pcm_next_device(ctl, device);
149 int snd_ctl_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t * info)
152 return ctl->ops->pcm_info(ctl, info);
155 int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *ctl, int subdev)
158 return ctl->ops->pcm_prefer_subdevice(ctl, subdev);
161 int snd_ctl_rawmidi_next_device(snd_ctl_t *ctl, int * device)
163 assert(ctl && device);
164 return ctl->ops->rawmidi_next_device(ctl, device);
167 int snd_ctl_rawmidi_info(snd_ctl_t *ctl, snd_rawmidi_info_t * info)
170 return ctl->ops->rawmidi_info(ctl, info);
173 int snd_ctl_rawmidi_prefer_subdevice(snd_ctl_t *ctl, int subdev)
176 return ctl->ops->rawmidi_prefer_subdevice(ctl, subdev);
179 int snd_ctl_read(snd_ctl_t *ctl, snd_ctl_event_t *event)
181 assert(ctl && event);
182 return ctl->ops->read(ctl, event);
185 int snd_ctl_wait(snd_ctl_t *ctl, int timeout)
189 err = snd_ctl_poll_descriptors(ctl, &pfd, 1);
191 err = poll(&pfd, 1, timeout);
197 int snd_ctl_open(snd_ctl_t **ctlp, const char *name)
202 snd_config_t *ctl_conf, *conf, *type_conf;
203 snd_config_iterator_t i, next;
204 const char *lib = NULL, *open = NULL;
205 int (*open_func)(snd_ctl_t **ctlp, const char *name, snd_config_t *conf);
207 assert(ctlp && name);
208 err = snd_config_update();
211 err = snd_config_searchv(snd_config, &ctl_conf, "ctl", name, 0);
214 char socket[256], sname[256];
215 err = sscanf(name, "hw:%d", &card);
217 return snd_ctl_hw_open(ctlp, name, card);
218 err = sscanf(name, "shm:%256[^,],%256[^,]", socket, sname);
220 return snd_ctl_shm_open(ctlp, name, socket, sname);
221 SNDERR("Unknown control %s", name);
224 if (snd_config_get_type(ctl_conf) != SND_CONFIG_TYPE_COMPOUND)
226 err = snd_config_search(ctl_conf, "type", &conf);
229 err = snd_config_get_string(conf, &str);
232 err = snd_config_searchv(snd_config, &type_conf, "ctltype", str, 0);
234 snd_config_for_each(i, next, type_conf) {
235 snd_config_t *n = snd_config_iterator_entry(i);
236 const char *id = snd_config_get_id(n);
237 if (strcmp(id, "comment") == 0)
239 if (strcmp(id, "lib") == 0) {
240 err = snd_config_get_string(n, &lib);
245 if (strcmp(id, "open") == 0) {
246 err = snd_config_get_string(n, &open);
251 SNDERR("Unknown field %s", id);
257 snprintf(buf, sizeof(buf), "_snd_ctl_%s_open", str);
260 lib = "libasound.so";
261 h = dlopen(lib, RTLD_NOW);
263 SNDERR("Cannot open shared library %s", lib);
266 open_func = dlsym(h, open);
268 SNDERR("symbol %s is not defined inside %s", open, lib);
272 return open_func(ctlp, name, ctl_conf);
275 void snd_ctl_elem_set_bytes(snd_ctl_elem_value_t *obj, void *data, size_t size)
278 assert(size <= sizeof(obj->value.bytes.data));
279 memcpy(obj->value.bytes.data, data, size);
282 #define TYPE(v) [SND_CTL_ELEM_TYPE_##v] = #v
283 #define IFACE(v) [SND_CTL_ELEM_IFACE_##v] = #v
284 #define EVENT(v) [SND_CTL_EVENT_##v] = #v
286 const char *snd_ctl_elem_type_names[] = {
295 const char *snd_ctl_elem_iface_names[] = {
305 const char *snd_ctl_event_type_names[] = {
309 const char *snd_ctl_elem_type_name(snd_ctl_elem_type_t type)
311 assert(type <= SND_CTL_ELEM_TYPE_LAST);
312 return snd_ctl_elem_type_names[snd_enum_to_int(type)];
315 const char *snd_ctl_elem_iface_name(snd_ctl_elem_iface_t iface)
317 assert(iface <= SND_CTL_ELEM_IFACE_LAST);
318 return snd_ctl_elem_iface_names[snd_enum_to_int(iface)];
321 const char *snd_ctl_event_type_name(snd_ctl_event_type_t type)
323 assert(type <= SND_CTL_EVENT_LAST);
324 return snd_ctl_event_type_names[snd_enum_to_int(type)];
327 int snd_ctl_elem_list_alloc_space(snd_ctl_elem_list_t *obj, unsigned int entries)
331 obj->pids = calloc(entries, sizeof(*obj->pids));
336 obj->space = entries;
340 void snd_ctl_elem_list_free_space(snd_ctl_elem_list_t *obj)
346 unsigned int snd_ctl_event_elem_get_mask(const snd_ctl_event_t *obj)
349 assert(obj->type == SND_CTL_EVENT_ELEM);
350 return obj->data.elem.mask;
353 unsigned int snd_ctl_event_elem_get_numid(const snd_ctl_event_t *obj)
356 assert(obj->type == SND_CTL_EVENT_ELEM);
357 return obj->data.elem.id.numid;
360 void snd_ctl_event_elem_get_id(const snd_ctl_event_t *obj, snd_ctl_elem_id_t *ptr)
363 assert(obj->type == SND_CTL_EVENT_ELEM);
364 *ptr = obj->data.elem.id;
367 snd_ctl_elem_iface_t snd_ctl_event_elem_get_interface(const snd_ctl_event_t *obj)
370 assert(obj->type == SND_CTL_EVENT_ELEM);
371 return snd_int_to_enum(obj->data.elem.id.iface);
374 unsigned int snd_ctl_event_elem_get_device(const snd_ctl_event_t *obj)
377 assert(obj->type == SND_CTL_EVENT_ELEM);
378 return obj->data.elem.id.device;
381 unsigned int snd_ctl_event_elem_get_subdevice(const snd_ctl_event_t *obj)
384 assert(obj->type == SND_CTL_EVENT_ELEM);
385 return obj->data.elem.id.subdevice;
388 const char *snd_ctl_event_elem_get_name(const snd_ctl_event_t *obj)
391 assert(obj->type == SND_CTL_EVENT_ELEM);
392 return obj->data.elem.id.name;
395 unsigned int snd_ctl_event_elem_get_index(const snd_ctl_event_t *obj)
398 assert(obj->type == SND_CTL_EVENT_ELEM);
399 return obj->data.elem.id.index;