Example #1
0
size_t
actlstr(char *buf, size_t n, char *ch, struct mixer *mx) {
	size_t ret;
	char *status;
	struct mixer_ctl *ctl;

	if (!(ctl = mixer_get_ctl_by_name(mx, ch))) {
		mixer_close(mx);
		die("couldn't find mixer ctl '%s'\n", ch);
	}

	switch (mixer_ctl_get_type(ctl)) {
	case MIXER_CTL_TYPE_INT:
		if ((ret = snprintf(buf, n, "%d%%",
				mixer_ctl_get_percent(ctl, 0))) > n)
			ret = n;
		break;
	case MIXER_CTL_TYPE_BOOL:
		status = mixer_ctl_get_value(ctl, 0) ? "On" : "Off";
		ret = stpncpy(buf, status, n) - buf;
		break;
	default:
		mixer_close(mx);
		die("unsupported ctl type '%s'\n",
			mixer_ctl_get_type_string(ctl));
	};

	return ret;
}
int write_percentage(int argc, char **argv) {
    int nMixer = -1, nControl = -1;
    int location = -1, value = -1;

    if (argc == 6) {
        nMixer = atoi(argv[2]);
        nControl = atoi(argv[3]);
        location = atoi(argv[4]);
        value = atoi(argv[5]);
    }

    if (argc != 6 || nMixer < 0 || nMixer > 7 || nControl < 0 || location < 0) {
        printf("Usage: ainfo write-percentage <card number> <control number> <location> <value>\n"
               "where <card number> is between 0 and 7\n"
               "<control number> is the control to be written\n"
               "<location> is the location to be written\n"
               "<value> is the value to be written\n");
        return 0;
    }

    mixer *m = mixer_open(nMixer);

    if (m == NULL) {
        printf("Unable to open card #%d\n", nMixer);
        return 0;
    }

    mixer_ctl *c = mixer_get_ctl(m, nControl);

    if (c == NULL) {
        printf("Unable to open control #%d\n", nControl);
        return 0;        
    }

    char name[64], type[64];
    mixer_ctl_get_name(c, name, sizeof(name));
    mixer_ctl_set_percent(c, location, value);
    printf("Control: %s\nPercent: %d\nValue: %d\n", name, 
        mixer_ctl_get_percent(c, location), mixer_ctl_get_value(c, location));

    return 0;
}