示例#1
0
int mixer_ctl_set(struct mixer_ctl *ctl, unsigned percent)
{
    struct snd_ctl_elem_value ev;
    unsigned n;

    memset(&ev, 0, sizeof(ev));
    ev.id.numid = ctl->info->id.numid;
    switch (ctl->info->type) {
    case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer.value[n] = !!percent;
        break;
    case SNDRV_CTL_ELEM_TYPE_INTEGER: {
        long value = scale_int(ctl->info, percent);
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer.value[n] = value;
        break;
    }
    case SNDRV_CTL_ELEM_TYPE_INTEGER64: {
        long long value = scale_int64(ctl->info, percent);
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer64.value[n] = value;
        break;
    }
    default:
        errno = EINVAL;
        return -1;
    }

    return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
}
/*
 * Add support for controls taking more than one parameter as input value
 * This is useful for volume controls which take two parameters as input value.
 */
int mixer_ctl_mulvalues(struct mixer_ctl *ctl, int count, char ** argv)
{
    struct snd_ctl_elem_value ev;
    unsigned n;

    if (!ctl) {
        ALOGV("can't find control\n");
        return -1;
    }
    if (count < ctl->info->count || count > ctl->info->count)
        return -EINVAL;

    memset(&ev, 0, sizeof(ev));
    ev.id.numid = ctl->info->id.numid;
    switch (ctl->info->type) {
    case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer.value[n] = !!atoi(argv[n]);
        break;
    case SNDRV_CTL_ELEM_TYPE_INTEGER: {
        for (n = 0; n < ctl->info->count; n++) {
             fprintf( stderr, "Value: %d idx:%d\n", atoi(argv[n]), n);
             ev.value.integer.value[n] = atoi(argv[n]);
        }
        break;
    }
    case SNDRV_CTL_ELEM_TYPE_INTEGER64: {
        for (n = 0; n < ctl->info->count; n++) {
             long long value_ll = scale_int64(ctl->info, atoi(argv[n]));
             fprintf( stderr, "ll_value = %lld\n", value_ll);
             ev.value.integer64.value[n] = value_ll;
        }
        break;
    }
    case SNDRV_CTL_ELEM_TYPE_ENUMERATED: {
        for (n = 0; n < ctl->info->count; n++) {
            fprintf( stderr, "Value: %d idx:%d\n", atoi(argv[n]), n);
            ev.value.enumerated.item[n] = (unsigned int)atoi(argv[n]);
        }
        break;
    }
    default:
        errno = EINVAL;
        return errno;
    }

    return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
}
static int set_volume_simple(struct mixer_ctl *ctl,
    char **ptr, long pmin, long pmax, int count)
{
    long val, orig;
    char *p = *ptr, *s;
    struct snd_ctl_elem_value ev;
    unsigned n;

    if (*p == ':')
        p++;
    if (*p == '\0' || (!isdigit(*p) && *p != '-'))
        goto skip;

    s = p;
    val = strtol(s, &p, 10);
    if (*p == '.') {
        p++;
        strtol(p, &p, 10);
    }
    if (*p == '%') {
        val = (long)percent_to_index(strtod(s, NULL), pmin, pmax);
        p++;
    } else if (p[0] == 'd' && p[1] == 'B') {
        val = (long)(strtod(s, NULL) * 100.0);
        p += 2;
    } else {
        if (pmin < 0) {
            pmax = pmax - pmin;
            pmin = 0;
        }
    }
    val = check_range(val, pmin, pmax);
    ALOGV("val = %x", val);

    if (!ctl) {
        ALOGV("can't find control\n");
        return -EPERM;
    }
    if (count < ctl->info->count || count > ctl->info->count)
        return -EINVAL;

    ALOGV("Value = ");

    memset(&ev, 0, sizeof(ev));
    ev.id.numid = ctl->info->id.numid;
    switch (ctl->info->type) {
    case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer.value[n] = !!val;
        print_dB(val);
        break;
    case SNDRV_CTL_ELEM_TYPE_INTEGER: {
        for (n = 0; n < ctl->info->count; n++)
             ev.value.integer.value[n] = val;
        print_dB(val);
        break;
    }
    case SNDRV_CTL_ELEM_TYPE_INTEGER64: {
        for (n = 0; n < ctl->info->count; n++) {
             long long value_ll = scale_int64(ctl->info, val);
             print_dB(value_ll);
             ev.value.integer64.value[n] = value_ll;
        }
        break;
    }
    default:
        errno = EINVAL;
        return errno;
    }

    ALOGV("\n");
    return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);

skip:
        if (*p == ',')
                p++;
        *ptr = p;
        return 0;
}
int mixer_ctl_set(struct mixer_ctl *ctl, unsigned percent)
{
    struct snd_ctl_elem_value ev;
    unsigned n;
    long min, max;
    unsigned int *tlv = NULL;
    enum ctl_type type;
    int volume = 0;
    unsigned int tlv_type;

    if (!ctl) {
        ALOGV("can't find control\n");
        return -1;
    }

    if (is_volume(ctl->info->id.name, &type)) {
        ALOGV("capability: volume\n");
        tlv = calloc(1, DEFAULT_TLV_SIZE);
        if (tlv == NULL) {
            ALOGE("failed to allocate memory\n");
        } else if (!mixer_ctl_read_tlv(ctl, tlv, &min, &max, &tlv_type)) {
            switch(tlv_type) {
            case SNDRV_CTL_TLVT_DB_LINEAR:
            case SNDRV_CTL_TLVT_DB_MINMAX:
                ALOGV("tlv db linear/db minmax: b4 %d\n", percent);

		if (min < 0) {
			max = max - min;
			min = 0;
		}
                percent = check_range(percent, min, max);
                ALOGV("tlv db linear: %d %d %d\n", percent, min, max);
                volume = 1;
                break;
            default:
                percent = (long)percent_to_index(percent, min, max);
                percent = check_range(percent, min, max);
                volume = 1;
                break;
            }
        } else
            ALOGV("mixer_ctl_read_tlv failed\n");
        free(tlv);
    }
    memset(&ev, 0, sizeof(ev));
    ev.id.numid = ctl->info->id.numid;
    switch (ctl->info->type) {
    case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer.value[n] = !!percent;
        break;
    case SNDRV_CTL_ELEM_TYPE_INTEGER: {
        int value;
        if (!volume)
             value = scale_int(ctl->info, percent);
        else
             value = (int) percent;
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer.value[n] = value;
        break;
    }
    case SNDRV_CTL_ELEM_TYPE_INTEGER64: {
        long long value;
        if (!volume)
             value = scale_int64(ctl->info, percent);
        else
             value = (long long)percent;
        for (n = 0; n < ctl->info->count; n++)
            ev.value.integer64.value[n] = value;
        break;
    }
    case SNDRV_CTL_ELEM_TYPE_IEC958: {
        struct snd_aes_iec958 *iec958;
        iec958 = (struct snd_aes_iec958 *)percent;
        memcpy(ev.value.iec958.status,iec958->status,SPDIF_CHANNEL_STATUS_SIZE);
        break;
    }
    case SNDRV_CTL_ELEM_TYPE_ENUMERATED: {
        for (n = 0; n < ctl->info->count; n++) {
            ev.value.enumerated.item[n] = (unsigned int)percent;
        }
        break;
    }
    default:
        errno = EINVAL;
        return errno;
    }

    return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
}