コード例 #1
0
ファイル: amixer.c プロジェクト: zhouguanying/null
static int cset(int argc, char *argv[], int roflag, int keep_handle)
{
    int err;
    static snd_ctl_t *handle = NULL;
    snd_ctl_elem_info_t *info;
    snd_ctl_elem_id_t *id;
    snd_ctl_elem_value_t *control;
    char *ptr;
    unsigned int idx, count;
    long tmp;
    snd_ctl_elem_type_t type;
    snd_ctl_elem_info_alloca(&info);
    snd_ctl_elem_id_alloca(&id);
    snd_ctl_elem_value_alloca(&control);

    if (argc < 1)
        return -EINVAL;
    if (parse_control_id(argv[0], id))
        return -EINVAL;
    if (handle == NULL && (err = snd_ctl_open(&handle, card, 0)) < 0)
        return err;
    snd_ctl_elem_info_set_id(info, id);
    if ((err = snd_ctl_elem_info(handle, info)) < 0)
    {
        if (!keep_handle)
        {
            snd_ctl_close(handle);
            handle = NULL;
        }
        return err;
    }
    snd_ctl_elem_info_get_id(info, id);
    type = snd_ctl_elem_info_get_type(info);
    count = snd_ctl_elem_info_get_count(info);
    snd_ctl_elem_value_set_id(control, id);

    if (!roflag)
    {
        ptr = argv[1];
        for (idx = 0; idx < count && idx < 128 && ptr && *ptr; idx++)
        {
            switch (type)
            {
            case SND_CTL_ELEM_TYPE_BOOLEAN:
                tmp = 0;
                if (!strncasecmp(ptr, "on", 2) || !strncasecmp(ptr, "up", 2))
                {
                    tmp = 1;
                    ptr += 2;
                }
                else if (!strncasecmp(ptr, "yes", 3))
                {
                    tmp = 1;
                    ptr += 3;
                }
                else if (!strncasecmp(ptr, "toggle", 6))
                {
                    tmp = snd_ctl_elem_value_get_boolean(control, idx);
                    tmp = tmp > 0 ? 0 : 1;
                    ptr += 6;
                }
                else if (isdigit(*ptr))
                {
                    tmp = atoi(ptr) > 0 ? 1 : 0;
                    while (isdigit(*ptr))
                        ptr++;
                }
                else
                {
                    while (*ptr && *ptr != ',')
                        ptr++;
                }
                snd_ctl_elem_value_set_boolean(control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER:
                tmp = get_integer(&ptr,
                                  snd_ctl_elem_info_get_min(info),
                                  snd_ctl_elem_info_get_max(info));
                snd_ctl_elem_value_set_integer(control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER64:
                tmp = get_integer64(&ptr,
                                    snd_ctl_elem_info_get_min64(info),
                                    snd_ctl_elem_info_get_max64(info));
                snd_ctl_elem_value_set_integer64(control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_ENUMERATED:
                tmp = get_ctl_enum_item_index(handle, info, &ptr);
                if (tmp < 0)
                    tmp = get_integer(&ptr, 0, snd_ctl_elem_info_get_items(info) - 1);
                snd_ctl_elem_value_set_enumerated(control, idx, tmp);
                break;
            case SND_CTL_ELEM_TYPE_BYTES:
                tmp = get_integer(&ptr, 0, 255);
                snd_ctl_elem_value_set_byte(control, idx, tmp);
                break;
            default:
                break;
            }
            if (!strchr(argv[1], ','))
                ptr = argv[1];
            else if (*ptr == ',')
                ptr++;
        }
        if ((err = snd_ctl_elem_write(handle, control)) < 0)
        {
            if (!keep_handle)
            {
                snd_ctl_close(handle);
                handle = NULL;
            }
            return err;
        }
    }
    if (! keep_handle)
    {
        snd_ctl_close(handle);
        handle = NULL;
    }
    if (!quiet)
    {
        snd_hctl_t *hctl;
        snd_hctl_elem_t *elem;
        if ((err = snd_hctl_open(&hctl, card, 0)) < 0)
            return err;
        if ((err = snd_hctl_load(hctl)) < 0)
            return err;
        elem = snd_hctl_find_elem(hctl, id);
        if (elem)
            show_control("  ", elem, LEVEL_BASIC | LEVEL_ID);
        snd_hctl_close(hctl);
    }
    return 0;
}
コード例 #2
0
ファイル: ctlparse.c プロジェクト: tiwai/alsa-lib
/**
 * \brief parse ASCII string as CTL element value
 * \param handle CTL handle
 * \param dst destination CTL element value
 * \param info CTL element info structure
 * \param value source ASCII string
 * \return zero on success, otherwise a negative error code
 *
 * Note: For toggle command, the dst must contain previous (current)
 * state (do the #snd_ctl_elem_read call to obtain it).
 */
int snd_ctl_ascii_value_parse(snd_ctl_t *handle,
			      snd_ctl_elem_value_t *dst,
			      snd_ctl_elem_info_t *info,
			      const char *value)
{
	const char *ptr = value;
	snd_ctl_elem_id_t myid = {0};
	snd_ctl_elem_type_t type;
	unsigned int idx, count;
	long tmp;
	long long tmp64;

	snd_ctl_elem_info_get_id(info, &myid);
	type = snd_ctl_elem_info_get_type(info);
	count = snd_ctl_elem_info_get_count(info);
	snd_ctl_elem_value_set_id(dst, &myid);
	
	for (idx = 0; idx < count && idx < 128 && ptr && *ptr; idx++) {
		if (*ptr == ',')
			goto skip;
		switch (type) {
		case SND_CTL_ELEM_TYPE_BOOLEAN:
			tmp = 0;
			if (!strncasecmp(ptr, "on", 2) ||
			    !strncasecmp(ptr, "up", 2)) {
				tmp = 1;
				ptr += 2;
			} else if (!strncasecmp(ptr, "yes", 3)) {
				tmp = 1;
				ptr += 3;
			} else if (!strncasecmp(ptr, "toggle", 6)) {
				tmp = snd_ctl_elem_value_get_boolean(dst, idx);
				tmp = tmp > 0 ? 0 : 1;
				ptr += 6;
			} else if (isdigit(*ptr)) {
				tmp = atoi(ptr) > 0 ? 1 : 0;
				while (isdigit(*ptr))
					ptr++;
			} else {
				while (*ptr && *ptr != ',')
					ptr++;
			}
			snd_ctl_elem_value_set_boolean(dst, idx, tmp);
			break;
		case SND_CTL_ELEM_TYPE_INTEGER:
			tmp = get_integer(&ptr,
					  snd_ctl_elem_info_get_min(info),
					  snd_ctl_elem_info_get_max(info));
			snd_ctl_elem_value_set_integer(dst, idx, tmp);
			break;
		case SND_CTL_ELEM_TYPE_INTEGER64:
			tmp64 = get_integer64(&ptr,
					  snd_ctl_elem_info_get_min64(info),
					  snd_ctl_elem_info_get_max64(info));
			snd_ctl_elem_value_set_integer64(dst, idx, tmp64);
			break;
		case SND_CTL_ELEM_TYPE_ENUMERATED:
			tmp = get_ctl_enum_item_index(handle, info, &ptr);
			if (tmp < 0)
				tmp = get_integer(&ptr, 0,
					snd_ctl_elem_info_get_items(info) - 1);
			snd_ctl_elem_value_set_enumerated(dst, idx, tmp);
			break;
		case SND_CTL_ELEM_TYPE_BYTES:
			tmp = get_integer(&ptr, 0, 255);
			snd_ctl_elem_value_set_byte(dst, idx, tmp);
			break;
		default:
			break;
		}
	skip:
		if (!strchr(value, ','))
			ptr = value;
		else if (*ptr == ',')
			ptr++;
	}
	return 0;
}