Blame src/control/namehint.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file control/namehint.c
Packit 4a16fb
 * \brief Give device name hints
Packit 4a16fb
 * \author Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 * \date 2006
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Give device name hints  - main file
Packit 4a16fb
 *  Copyright (c) 2006 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include "local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
#define DEV_SKIP	9999 /* some non-existing device number */
Packit 4a16fb
struct hint_list {
Packit 4a16fb
	char **list;
Packit 4a16fb
	unsigned int count;
Packit 4a16fb
	unsigned int allocated;
Packit 4a16fb
	const char *siface;
Packit 4a16fb
	snd_ctl_elem_iface_t iface;
Packit 4a16fb
	snd_ctl_t *ctl;
Packit 4a16fb
	snd_ctl_card_info_t *info;	
Packit 4a16fb
	int card;
Packit 4a16fb
	int device;
Packit 4a16fb
	long device_input;
Packit 4a16fb
	long device_output;
Packit 4a16fb
	int stream;
Packit 4a16fb
	int show_all;
Packit 4a16fb
	char *cardname;
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static int hint_list_add(struct hint_list *list,
Packit 4a16fb
			 const char *name,
Packit 4a16fb
			 const char *description)
Packit 4a16fb
{
Packit 4a16fb
	char *x;
Packit 4a16fb
Packit 4a16fb
	if (list->count + 1 >= list->allocated) {
Packit 4a16fb
		char **n = realloc(list->list, (list->allocated + 10) * sizeof(char *));
Packit 4a16fb
		if (n == NULL)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		memset(n + list->allocated, 0, 10 * sizeof(*n));
Packit 4a16fb
		list->allocated += 10;
Packit 4a16fb
		list->list = n;
Packit 4a16fb
	}
Packit 4a16fb
	if (name == NULL) {
Packit 4a16fb
		x = NULL;
Packit 4a16fb
	} else {
Packit 4a16fb
		x = malloc(4 + strlen(name) + (description != NULL ? (4 + strlen(description) + 1) : 0) + 1);
Packit 4a16fb
		if (x == NULL)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		memcpy(x, "NAME", 4);
Packit 4a16fb
		strcpy(x + 4, name);
Packit 4a16fb
		if (description != NULL) {
Packit 4a16fb
			strcat(x, "|DESC");
Packit 4a16fb
			strcat(x, description);
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	list->list[list->count++] = x;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void zero_handler(const char *file ATTRIBUTE_UNUSED,
Packit 4a16fb
			 int line ATTRIBUTE_UNUSED,
Packit 4a16fb
			 const char *function ATTRIBUTE_UNUSED,
Packit 4a16fb
			 int err ATTRIBUTE_UNUSED,
Packit 4a16fb
			 const char *fmt ATTRIBUTE_UNUSED,
Packit 4a16fb
			 va_list arg ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_dev_name1(struct hint_list *list, char **res, int device,
Packit 4a16fb
			 int stream)
Packit 4a16fb
{
Packit 4a16fb
	*res = NULL;
Packit 4a16fb
	if (device < 0 || device == DEV_SKIP)
Packit 4a16fb
		return 0;
Packit 4a16fb
	switch (list->iface) {
Packit 4a16fb
#ifdef BUILD_HWDEP
Packit 4a16fb
	case SND_CTL_ELEM_IFACE_HWDEP:
Packit 4a16fb
		{
Packit 4a16fb
			snd_hwdep_info_t info = {0};
Packit 4a16fb
			snd_hwdep_info_set_device(&info, device);
Packit 4a16fb
			if (snd_ctl_hwdep_info(list->ctl, &info) < 0)
Packit 4a16fb
				return 0;
Packit 4a16fb
			*res = strdup(snd_hwdep_info_get_name(&info));
Packit 4a16fb
			return 0;
Packit 4a16fb
		}
Packit 4a16fb
#endif
Packit 4a16fb
#ifdef BUILD_PCM
Packit 4a16fb
	case SND_CTL_ELEM_IFACE_PCM:
Packit 4a16fb
		{
Packit 4a16fb
			snd_pcm_info_t info = {0};
Packit 4a16fb
			snd_pcm_info_set_device(&info, device);
Packit 4a16fb
			snd_pcm_info_set_stream(&info, stream ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK);
Packit 4a16fb
			if (snd_ctl_pcm_info(list->ctl, &info) < 0)
Packit 4a16fb
				return 0;
Packit 4a16fb
			switch (snd_pcm_info_get_class(&info)) {
Packit 4a16fb
			case SND_PCM_CLASS_MODEM:
Packit 4a16fb
			case SND_PCM_CLASS_DIGITIZER:
Packit 4a16fb
				return -ENODEV;
Packit 4a16fb
			default:
Packit 4a16fb
				break;
Packit 4a16fb
			}
Packit 4a16fb
			*res = strdup(snd_pcm_info_get_name(&info));
Packit 4a16fb
			return 0;
Packit 4a16fb
		}
Packit 4a16fb
#endif
Packit 4a16fb
#ifdef BUILD_RAWMIDI
Packit 4a16fb
	case SND_CTL_ELEM_IFACE_RAWMIDI:
Packit 4a16fb
		{
Packit 4a16fb
			snd_rawmidi_info_t info = {0};
Packit 4a16fb
			snd_rawmidi_info_set_device(&info, device);
Packit 4a16fb
			snd_rawmidi_info_set_stream(&info, stream ? SND_RAWMIDI_STREAM_INPUT : SND_RAWMIDI_STREAM_OUTPUT);
Packit 4a16fb
			if (snd_ctl_rawmidi_info(list->ctl, &info) < 0)
Packit 4a16fb
				return 0;
Packit 4a16fb
			*res = strdup(snd_rawmidi_info_get_name(&info));
Packit 4a16fb
			return 0;
Packit 4a16fb
		}
Packit 4a16fb
#endif
Packit 4a16fb
	default:
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *get_dev_name(struct hint_list *list)
Packit 4a16fb
{
Packit 4a16fb
	char *str1, *str2, *res;
Packit 4a16fb
	int device;
Packit 4a16fb
	
Packit 4a16fb
	device = list->device_input >= 0 ? list->device_input : list->device;
Packit 4a16fb
	if (get_dev_name1(list, &str1, device, 1) < 0)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	device = list->device_output >= 0 ? list->device_output : list->device;
Packit 4a16fb
	if (get_dev_name1(list, &str2, device, 0) < 0) {
Packit 4a16fb
		if (str1)
Packit 4a16fb
			free(str1);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	if (str1 != NULL || str2 != NULL) {
Packit 4a16fb
		if (str1 != NULL && str2 != NULL) {
Packit 4a16fb
			if (strcmp(str1, str2) == 0) {
Packit 4a16fb
				res = malloc(strlen(list->cardname) + strlen(str2) + 3);
Packit 4a16fb
				if (res != NULL) {
Packit 4a16fb
					strcpy(res, list->cardname);
Packit 4a16fb
					strcat(res, ", ");
Packit 4a16fb
					strcat(res, str2);
Packit 4a16fb
				}
Packit 4a16fb
			} else {
Packit 4a16fb
				res = malloc(strlen(list->cardname) + strlen(str2) + strlen(str1) + 6);
Packit 4a16fb
				if (res != NULL) {
Packit 4a16fb
					strcpy(res, list->cardname);
Packit 4a16fb
					strcat(res, ", ");
Packit 4a16fb
					strcat(res, str2);
Packit 4a16fb
					strcat(res, " / ");
Packit 4a16fb
					strcat(res, str1);
Packit 4a16fb
				}
Packit 4a16fb
			}
Packit 4a16fb
			free(str2);
Packit 4a16fb
			free(str1);
Packit 4a16fb
			return res;
Packit 4a16fb
		} else {
Packit 4a16fb
			if (str1 != NULL) {
Packit 4a16fb
				str2 = "Input";
Packit 4a16fb
			} else {
Packit 4a16fb
				str1 = str2;
Packit 4a16fb
				str2 = "Output";
Packit 4a16fb
			}
Packit 4a16fb
			res = malloc(strlen(list->cardname) + strlen(str1) + 19);
Packit 4a16fb
			if (res == NULL) {
Packit 4a16fb
				free(str1);
Packit 4a16fb
				return NULL;
Packit 4a16fb
			}
Packit 4a16fb
			strcpy(res, list->cardname);
Packit 4a16fb
			strcat(res, ", ");
Packit 4a16fb
			strcat(res, str1);
Packit 4a16fb
			strcat(res, "|IOID");
Packit 4a16fb
			strcat(res, str2);
Packit 4a16fb
			free(str1);
Packit 4a16fb
			return res;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	/* if the specified device doesn't exist, skip this entry */
Packit 4a16fb
	if (list->device >= 0 || list->device_input >= 0 || list->device_output >= 0)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return strdup(list->cardname);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
#define BUF_SIZE 128
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static int try_config(snd_config_t *config,
Packit 4a16fb
		      struct hint_list *list,
Packit 4a16fb
		      const char *base,
Packit 4a16fb
		      const char *name)
Packit 4a16fb
{
Packit 4a16fb
	snd_local_error_handler_t eh;
Packit 4a16fb
	snd_config_t *res = NULL, *cfg, *cfg1, *n;
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	char *buf, *buf1 = NULL, *buf2;
Packit 4a16fb
	const char *str;
Packit 4a16fb
	int err = 0, level;
Packit 4a16fb
	long dev = list->device;
Packit 4a16fb
	int cleanup_res = 0;
Packit 4a16fb
Packit 4a16fb
	list->device_input = -1;
Packit 4a16fb
	list->device_output = -1;
Packit 4a16fb
	buf = malloc(BUF_SIZE);
Packit 4a16fb
	if (buf == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	sprintf(buf, "%s.%s", base, name);
Packit 4a16fb
	/* look for redirection */
Packit 4a16fb
	if (snd_config_search(config, buf, &cfg) >= 0 &&
Packit 4a16fb
	    snd_config_get_string(cfg, &str) >= 0 &&
Packit 4a16fb
	    ((strncmp(base, str, strlen(base)) == 0 &&
Packit 4a16fb
	     str[strlen(base)] == '.') || strchr(str, '.') == NULL))
Packit 4a16fb
	     	goto __skip_add;
Packit 4a16fb
	if (list->card >= 0 && list->device >= 0)
Packit 4a16fb
		sprintf(buf, "%s:CARD=%s,DEV=%i", name, snd_ctl_card_info_get_id(list->info), list->device);
Packit 4a16fb
	else if (list->card >= 0)
Packit 4a16fb
		sprintf(buf, "%s:CARD=%s", name, snd_ctl_card_info_get_id(list->info));
Packit 4a16fb
	else
Packit 4a16fb
		strcpy(buf, name);
Packit 4a16fb
	eh = snd_lib_error_set_local(&zero_handler);
Packit 4a16fb
	err = snd_config_search_definition(config, base, buf, &res;;
Packit 4a16fb
	snd_lib_error_set_local(eh);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		goto __skip_add;
Packit 4a16fb
	cleanup_res = 1;
Packit 4a16fb
	err = -EINVAL;
Packit 4a16fb
	if (snd_config_get_type(res) != SND_CONFIG_TYPE_COMPOUND)
Packit 4a16fb
		goto __cleanup;
Packit 4a16fb
	if (snd_config_search(res, "type", NULL) < 0)
Packit 4a16fb
		goto __cleanup;
Packit 4a16fb
Packit 4a16fb
#if 0	/* for debug purposes */
Packit 4a16fb
		{
Packit 4a16fb
			snd_output_t *out;
Packit 4a16fb
			fprintf(stderr, "********* PCM '%s':\n", buf);
Packit 4a16fb
			snd_output_stdio_attach(&out, stderr, 0);
Packit 4a16fb
			snd_config_save(res, out);
Packit 4a16fb
			snd_output_close(out);
Packit 4a16fb
			fprintf(stderr, "\n");
Packit 4a16fb
		}
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
	cfg1 = res;
Packit 4a16fb
	level = 0;
Packit 4a16fb
      __hint:
Packit 4a16fb
      	level++;
Packit 4a16fb
	if (snd_config_search(cfg1, "type", &cfg) >= 0 &&
Packit 4a16fb
	    snd_config_get_string(cfg, &str) >= 0 &&
Packit 4a16fb
	    strcmp(str, "hw") == 0) {
Packit 4a16fb
		list->device_input = -1;
Packit 4a16fb
		list->device_output = -1;
Packit 4a16fb
		if (snd_config_search(cfg1, "device", &cfg) >= 0) {
Packit 4a16fb
			if (snd_config_get_integer(cfg, &dev) < 0) {
Packit 4a16fb
				SNDERR("(%s) device must be an integer", buf);
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __cleanup;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
      	
Packit 4a16fb
	if (snd_config_search(cfg1, "hint", &cfg) >= 0) {
Packit 4a16fb
		if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
Packit 4a16fb
			SNDERR("hint (%s) must be a compound", buf);
Packit 4a16fb
			err = -EINVAL;
Packit 4a16fb
			goto __cleanup;
Packit 4a16fb
		}
Packit 4a16fb
		if (level == 1 &&
Packit 4a16fb
		    snd_config_search(cfg, "show", &n) >= 0 &&
Packit 4a16fb
		    snd_config_get_bool(n) <= 0)
Packit 4a16fb
		    	goto __skip_add;
Packit 4a16fb
		if (buf1 == NULL &&
Packit 4a16fb
		    snd_config_search(cfg, "description", &n) >= 0 &&
Packit 4a16fb
		    snd_config_get_string(n, &str) >= 0) {
Packit 4a16fb
			buf1 = strdup(str);
Packit 4a16fb
			if (buf1 == NULL) {
Packit 4a16fb
				err = -ENOMEM;
Packit 4a16fb
				goto __cleanup;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
		if (snd_config_search(cfg, "device", &n) >= 0) {
Packit 4a16fb
			if (snd_config_get_integer(n, &dev) < 0) {
Packit 4a16fb
				SNDERR("(%s) device must be an integer", buf);
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __cleanup;
Packit 4a16fb
			}
Packit 4a16fb
			list->device_input = dev;
Packit 4a16fb
			list->device_output = dev;
Packit 4a16fb
		}
Packit 4a16fb
		if (snd_config_search(cfg, "device_input", &n) >= 0) {
Packit 4a16fb
			if (snd_config_get_integer(n, &list->device_input) < 0) {
Packit 4a16fb
				SNDERR("(%s) device_input must be an integer", buf);
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __cleanup;
Packit 4a16fb
			}
Packit 4a16fb
			/* skip the counterpart if only a single direction is defined */
Packit 4a16fb
			if (list->device_output < 0)
Packit 4a16fb
				list->device_output = DEV_SKIP;
Packit 4a16fb
		}
Packit 4a16fb
		if (snd_config_search(cfg, "device_output", &n) >= 0) {
Packit 4a16fb
			if (snd_config_get_integer(n, &list->device_output) < 0) {
Packit 4a16fb
				SNDERR("(%s) device_output must be an integer", buf);
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __cleanup;
Packit 4a16fb
			}
Packit 4a16fb
			/* skip the counterpart if only a single direction is defined */
Packit 4a16fb
			if (list->device_input < 0)
Packit 4a16fb
				list->device_input = DEV_SKIP;
Packit 4a16fb
		}
Packit 4a16fb
	} else if (level == 1 && !list->show_all)
Packit 4a16fb
		goto __skip_add;
Packit 4a16fb
	if (snd_config_search(cfg1, "slave", &cfg) >= 0 &&
Packit 4a16fb
	    snd_config_search(cfg, base, &cfg1) >= 0)
Packit 4a16fb
	    	goto __hint;
Packit 4a16fb
	snd_config_delete(res);
Packit 4a16fb
	res = NULL;
Packit 4a16fb
	cleanup_res = 0;
Packit 4a16fb
	if (strchr(buf, ':') != NULL)
Packit 4a16fb
		goto __ok;
Packit 4a16fb
	/* find, if all parameters have a default, */
Packit 4a16fb
	/* otherwise filter this definition */
Packit 4a16fb
	eh = snd_lib_error_set_local(&zero_handler);
Packit 4a16fb
	err = snd_config_search_alias_hooks(config, base, buf, &res;;
Packit 4a16fb
	snd_lib_error_set_local(eh);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		goto __cleanup;
Packit 4a16fb
	if (snd_config_search(res, "@args", &cfg) >= 0) {
Packit 4a16fb
		snd_config_for_each(i, next, cfg) {
Packit 4a16fb
			if (snd_config_search(snd_config_iterator_entry(i),
Packit 4a16fb
					      "default", NULL) < 0) {
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __cleanup;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
      __ok:
Packit 4a16fb
	err = 0;
Packit 4a16fb
      __cleanup:
Packit 4a16fb
      	if (err >= 0) {
Packit 4a16fb
      		list->device = dev;
Packit 4a16fb
 		str = list->card >= 0 ? get_dev_name(list) : NULL;
Packit 4a16fb
      		if (str != NULL) {
Packit 4a16fb
      			level = (buf1 == NULL ? 0 : strlen(buf1)) + 1 + strlen(str);
Packit 4a16fb
      			buf2 = realloc((char *)str, level + 1);
Packit 4a16fb
      			if (buf2 != NULL) {
Packit 4a16fb
      				if (buf1 != NULL) {
Packit 4a16fb
      					str = strchr(buf2, '|');
Packit 4a16fb
      					if (str != NULL)
Packit 4a16fb
						memmove(buf2 + (level - strlen(str)), str, strlen(str));
Packit 4a16fb
					else
Packit 4a16fb
						str = buf2 + strlen(buf2);
Packit 4a16fb
      					*(char *)str++ = '\n';
Packit 4a16fb
	      				memcpy((char *)str, buf1, strlen(buf1));
Packit 4a16fb
	      				buf2[level] = '\0';
Packit 4a16fb
					free(buf1);
Packit 4a16fb
				}
Packit 4a16fb
				buf1 = buf2;
Packit 4a16fb
			} else {
Packit 4a16fb
				free((char *)str);
Packit 4a16fb
			}
Packit 4a16fb
      		} else if (list->device >= 0)
Packit 4a16fb
      			goto __skip_add;
Packit 4a16fb
	      	err = hint_list_add(list, buf, buf1);
Packit 4a16fb
	}
Packit 4a16fb
      __skip_add:
Packit 4a16fb
	if (res && cleanup_res)
Packit 4a16fb
	      	snd_config_delete(res);
Packit 4a16fb
	if (buf1)
Packit 4a16fb
		free(buf1);
Packit 4a16fb
      	free(buf);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
#define IFACE(v, fcn) [SND_CTL_ELEM_IFACE_##v] = (next_devices_t)fcn
Packit 4a16fb
Packit 4a16fb
typedef int (*next_devices_t)(snd_ctl_t *, int *);
Packit 4a16fb
Packit 4a16fb
static const next_devices_t next_devices[] = {
Packit 4a16fb
	IFACE(CARD, NULL),
Packit 4a16fb
	IFACE(HWDEP, snd_ctl_hwdep_next_device),
Packit 4a16fb
	IFACE(MIXER, NULL),
Packit 4a16fb
	IFACE(PCM, snd_ctl_pcm_next_device),
Packit 4a16fb
	IFACE(RAWMIDI, snd_ctl_rawmidi_next_device),
Packit 4a16fb
	IFACE(TIMER, NULL),
Packit 4a16fb
	IFACE(SEQUENCER, NULL)
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static int add_card(snd_config_t *config, snd_config_t *rw_config, struct hint_list *list, int card)
Packit 4a16fb
{
Packit 4a16fb
	int err, ok;
Packit 4a16fb
	snd_config_t *conf, *n;
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	const char *str;
Packit 4a16fb
	char ctl_name[16];
Packit 4a16fb
	snd_ctl_card_info_t info = {0};
Packit 4a16fb
	int device, max_device = 0;
Packit 4a16fb
	
Packit 4a16fb
	list->info = &info;
Packit 4a16fb
	err = snd_config_search(config, list->siface, &conf;;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	sprintf(ctl_name, "hw:%i", card);
Packit 4a16fb
	err = snd_ctl_open(&list->ctl, ctl_name, 0);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = snd_ctl_card_info(list->ctl, &info;;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		goto __error;
Packit 4a16fb
	snd_config_for_each(i, next, conf) {
Packit 4a16fb
		n = snd_config_iterator_entry(i);
Packit 4a16fb
		if (snd_config_get_id(n, &str) < 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		
Packit 4a16fb
		if (next_devices[list->iface] != NULL) {
Packit 4a16fb
			list->card = card;
Packit 4a16fb
			device = max_device = -1;
Packit 4a16fb
			err = next_devices[list->iface](list->ctl, &device);
Packit 4a16fb
			if (device < 0)
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
			else
Packit 4a16fb
				max_device = device;
Packit 4a16fb
			while (err >= 0 && device >= 0) {
Packit 4a16fb
				err = next_devices[list->iface](list->ctl, &device);
Packit 4a16fb
				if (err >= 0 && device > max_device)
Packit 4a16fb
					max_device = device;
Packit 4a16fb
			}
Packit 4a16fb
			ok = 0;
Packit 4a16fb
			for (device = 0; err >= 0 && device <= max_device; device++) {
Packit 4a16fb
				list->device = device;
Packit 4a16fb
				err = try_config(rw_config, list, list->siface, str);
Packit 4a16fb
				if (err < 0)
Packit 4a16fb
					break;
Packit 4a16fb
				ok++;
Packit 4a16fb
			}
Packit 4a16fb
			if (ok)
Packit 4a16fb
				continue;
Packit 4a16fb
		} else {
Packit 4a16fb
			err = -EINVAL;
Packit 4a16fb
		}
Packit 4a16fb
		if (err == -EXDEV)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			list->card = card;
Packit 4a16fb
			list->device = -1;
Packit 4a16fb
			err = try_config(rw_config, list, list->siface, str);
Packit 4a16fb
		}
Packit 4a16fb
		if (err == -ENOMEM)
Packit 4a16fb
			goto __error;
Packit 4a16fb
	}
Packit 4a16fb
	err = 0;
Packit 4a16fb
      __error:
Packit 4a16fb
      	snd_ctl_close(list->ctl);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_card_name(struct hint_list *list, int card)
Packit 4a16fb
{
Packit 4a16fb
	char scard[16], *s;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	free(list->cardname);
Packit 4a16fb
	list->cardname = NULL;
Packit 4a16fb
	err = snd_card_get_name(card, &list->cardname);
Packit 4a16fb
	if (err <= 0)
Packit 4a16fb
		return 0;
Packit 4a16fb
	sprintf(scard, " #%i", card);
Packit 4a16fb
	s = realloc(list->cardname, strlen(list->cardname) + strlen(scard) + 1);
Packit 4a16fb
	if (s == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	list->cardname = s;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int add_software_devices(snd_config_t *config, snd_config_t *rw_config,
Packit 4a16fb
				struct hint_list *list)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_config_t *conf, *n;
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	const char *str;
Packit 4a16fb
Packit 4a16fb
	err = snd_config_search(config, list->siface, &conf;;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	snd_config_for_each(i, next, conf) {
Packit 4a16fb
		n = snd_config_iterator_entry(i);
Packit 4a16fb
		if (snd_config_get_id(n, &str) < 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		list->card = -1;
Packit 4a16fb
		list->device = -1;
Packit 4a16fb
		err = try_config(rw_config, list, list->siface, str);
Packit 4a16fb
		if (err == -ENOMEM)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get a set of device name hints
Packit 4a16fb
 * \param card Card number or -1 (means all cards)
Packit 4a16fb
 * \param iface Interface identification (like "pcm", "rawmidi", "timer", "seq")
Packit 4a16fb
 * \param hints Result - array of device name hints
Packit 4a16fb
 * \result zero if success, otherwise a negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * hints will receive a NULL-terminated array of device name hints,
Packit 4a16fb
 * which can be passed to #snd_device_name_get_hint to extract usable
Packit 4a16fb
 * values. When no longer needed, hints should be passed to
Packit 4a16fb
 * #snd_device_name_free_hint to release resources.
Packit 4a16fb
 *
Packit 4a16fb
 * User-defined hints are gathered from namehint.IFACE tree like:
Packit 4a16fb
 *
Packit 4a16fb
 * 
Packit 4a16fb
 * namehint.pcm {
Packit 4a16fb
 *   myfile "file:FILE=/tmp/soundwave.raw|Save sound output to /tmp/soundwave.raw"
Packit 4a16fb
 *   myplug "plug:front:Do all conversions for front speakers"
Packit 4a16fb
 * }
Packit 4a16fb
 * 
Packit 4a16fb
 *
Packit 4a16fb
 * Note: The device description is separated with '|' char.
Packit 4a16fb
 *
Packit 4a16fb
 * Special variables: defaults.namehint.showall specifies if all device
Packit 4a16fb
 * definitions are accepted (boolean type).
Packit 4a16fb
 */
Packit 4a16fb
int snd_device_name_hint(int card, const char *iface, void ***hints)
Packit 4a16fb
{
Packit 4a16fb
	struct hint_list list;
Packit 4a16fb
	char ehints[24];
Packit 4a16fb
	const char *str;
Packit 4a16fb
	snd_config_t *conf, *local_config = NULL, *local_config_rw = NULL;
Packit 4a16fb
	snd_config_update_t *local_config_update = NULL;
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (hints == NULL)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	err = snd_config_update_r(&local_config, &local_config_update, NULL);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = snd_config_copy(&local_config_rw, local_config);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	list.list = NULL;
Packit 4a16fb
	list.count = list.allocated = 0;
Packit 4a16fb
	list.siface = iface;
Packit 4a16fb
	list.show_all = 0;
Packit 4a16fb
	list.cardname = NULL;
Packit 4a16fb
	if (strcmp(iface, "card") == 0)
Packit 4a16fb
		list.iface = SND_CTL_ELEM_IFACE_CARD;
Packit 4a16fb
	else if (strcmp(iface, "pcm") == 0)
Packit 4a16fb
		list.iface = SND_CTL_ELEM_IFACE_PCM;
Packit 4a16fb
	else if (strcmp(iface, "rawmidi") == 0)
Packit 4a16fb
		list.iface = SND_CTL_ELEM_IFACE_RAWMIDI;
Packit 4a16fb
	else if (strcmp(iface, "timer") == 0)
Packit 4a16fb
		list.iface = SND_CTL_ELEM_IFACE_TIMER;
Packit 4a16fb
	else if (strcmp(iface, "seq") == 0)
Packit 4a16fb
		list.iface = SND_CTL_ELEM_IFACE_SEQUENCER;
Packit 4a16fb
	else if (strcmp(iface, "hwdep") == 0)
Packit 4a16fb
		list.iface = SND_CTL_ELEM_IFACE_HWDEP;
Packit 4a16fb
	else if (strcmp(iface, "ctl") == 0)
Packit 4a16fb
		list.iface = SND_CTL_ELEM_IFACE_MIXER;
Packit 4a16fb
	else {
Packit 4a16fb
		err = -EINVAL;
Packit 4a16fb
		goto __error;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (snd_config_search(local_config, "defaults.namehint.showall", &conf) >= 0)
Packit 4a16fb
		list.show_all = snd_config_get_bool(conf) > 0;
Packit 4a16fb
	if (card >= 0) {
Packit 4a16fb
		err = get_card_name(&list, card);
Packit 4a16fb
		if (err >= 0)
Packit 4a16fb
			err = add_card(local_config, local_config_rw, &list, card);
Packit 4a16fb
	} else {
Packit 4a16fb
		add_software_devices(local_config, local_config_rw, &list);
Packit 4a16fb
		err = snd_card_next(&card;;
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __error;
Packit 4a16fb
		while (card >= 0) {
Packit 4a16fb
			err = get_card_name(&list, card);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				goto __error;
Packit 4a16fb
			err = add_card(local_config, local_config_rw, &list, card);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				goto __error;
Packit 4a16fb
			err = snd_card_next(&card;;
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				goto __error;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	sprintf(ehints, "namehint.%s", list.siface);
Packit 4a16fb
	err = snd_config_search(local_config, ehints, &conf;;
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		snd_config_for_each(i, next, conf) {
Packit 4a16fb
			if (snd_config_get_string(snd_config_iterator_entry(i),
Packit 4a16fb
						  &str) < 0)
Packit 4a16fb
				continue;
Packit 4a16fb
			err = hint_list_add(&list, str, NULL);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				goto __error;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	err = 0;
Packit 4a16fb
      __error:
Packit 4a16fb
	/* add an empty entry if nothing has been added yet; the caller
Packit 4a16fb
	 * expects non-NULL return
Packit 4a16fb
	 */
Packit 4a16fb
	if (!err && !list.list)
Packit 4a16fb
		err = hint_list_add(&list, NULL, NULL);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
      		snd_device_name_free_hint((void **)list.list);
Packit 4a16fb
	else
Packit 4a16fb
      		*hints = (void **)list.list;
Packit 4a16fb
	free(list.cardname);
Packit 4a16fb
	if (local_config_rw)
Packit 4a16fb
		snd_config_delete(local_config_rw);
Packit 4a16fb
	if (local_config)
Packit 4a16fb
		snd_config_delete(local_config);
Packit 4a16fb
	if (local_config_update)
Packit 4a16fb
		snd_config_update_free(local_config_update);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Free a list of device name hints.
Packit 4a16fb
 * \param hints List to free
Packit 4a16fb
 * \result zero if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_device_name_free_hint(void **hints)
Packit 4a16fb
{
Packit 4a16fb
	char **h;
Packit 4a16fb
Packit 4a16fb
	if (hints == NULL)
Packit 4a16fb
		return 0;
Packit 4a16fb
	h = (char **)hints;
Packit 4a16fb
	while (*h) {
Packit 4a16fb
		free(*h);
Packit 4a16fb
		h++;
Packit 4a16fb
	}
Packit 4a16fb
	free(hints);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Extract a value from a hint
Packit 4a16fb
 * \param hint A pointer to hint
Packit 4a16fb
 * \param id Hint value to extract ("NAME", "DESC", or "IOID", see below)
Packit 4a16fb
 * \result an allocated ASCII string if success, otherwise NULL
Packit 4a16fb
 *
Packit 4a16fb
 * List of valid IDs:
Packit 4a16fb
 * NAME - name of device
Packit 4a16fb
 * DESC - description of device
Packit 4a16fb
 * IOID - input / output identification ("Input" or "Output"), NULL means both
Packit 4a16fb
 *
Packit 4a16fb
 * The return value should be freed when no longer needed.
Packit 4a16fb
 */
Packit 4a16fb
char *snd_device_name_get_hint(const void *hint, const char *id)
Packit 4a16fb
{
Packit 4a16fb
	const char *hint1 = (const char *)hint, *delim;
Packit 4a16fb
	char *res;
Packit 4a16fb
	unsigned size;
Packit 4a16fb
Packit 4a16fb
	if (strlen(id) != 4)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	while (*hint1 != '\0') {
Packit 4a16fb
		delim = strchr(hint1, '|');
Packit 4a16fb
		if (memcmp(id, hint1, 4) != 0) {
Packit 4a16fb
			if (delim == NULL)
Packit 4a16fb
				return NULL;
Packit 4a16fb
			hint1 = delim + 1;
Packit 4a16fb
			continue;
Packit 4a16fb
		} 
Packit 4a16fb
		if (delim == NULL)
Packit 4a16fb
			return strdup(hint1 + 4);
Packit 4a16fb
		size = delim - hint1 - 4;
Packit 4a16fb
		res = malloc(size + 1);
Packit 4a16fb
		if (res != NULL) {
Packit 4a16fb
			memcpy(res, hint1 + 4, size);
Packit 4a16fb
			res[size] = '\0';
Packit 4a16fb
		}
Packit 4a16fb
		return res;
Packit 4a16fb
	}
Packit 4a16fb
	return NULL;
Packit 4a16fb
}