Home | History | Annotate | Line # | Download | only in audiocfg
main.c revision 1.7.26.2
      1  1.7.26.2    martin /* $NetBSD: main.c,v 1.7.26.2 2020/04/13 08:05:40 martin Exp $ */
      2       1.1       mrg 
      3       1.1       mrg /*
      4       1.1       mrg  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5       1.1       mrg  * All rights reserved.
      6       1.1       mrg  *
      7       1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8       1.1       mrg  * modification, are permitted provided that the following conditions
      9       1.1       mrg  * are met:
     10       1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11       1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12       1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15       1.1       mrg  *
     16       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1       mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1       mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1       mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1       mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1       mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1       mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1       mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1       mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1       mrg  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1       mrg  */
     28       1.1       mrg 
     29       1.6  jmcneill #include <assert.h>
     30  1.7.26.2    martin #include <err.h>
     31       1.1       mrg #include <errno.h>
     32       1.1       mrg #include <fcntl.h>
     33       1.1       mrg #include <limits.h>
     34       1.1       mrg #include <stdio.h>
     35       1.1       mrg #include <stdlib.h>
     36       1.1       mrg #include <unistd.h>
     37       1.1       mrg 
     38       1.1       mrg #include "audiodev.h"
     39       1.1       mrg #include "drvctl.h"
     40       1.1       mrg 
     41       1.7     joerg __dead static void
     42  1.7.26.2    martin usage(void)
     43       1.1       mrg {
     44  1.7.26.2    martin 	const char *p = getprogname();
     45  1.7.26.2    martin 
     46  1.7.26.1  christos 	fprintf(stderr, "usage: %s list [<index>]\n", p);
     47       1.3       wiz 	fprintf(stderr, "       %s default <index>\n", p);
     48  1.7.26.1  christos 	fprintf(stderr, "       %s set  <index> [p|r] <enc> <prec> <ch> <freq>\n",
     49  1.7.26.1  christos 	    p);
     50       1.3       wiz 	fprintf(stderr, "       %s test <index>\n", p);
     51       1.1       mrg 	exit(EXIT_FAILURE);
     52       1.1       mrg }
     53       1.1       mrg 
     54  1.7.26.1  christos const char *encoding_names[] = {
     55  1.7.26.1  christos 	"none",
     56  1.7.26.1  christos 	AudioEmulaw,
     57  1.7.26.1  christos 	AudioEalaw,
     58  1.7.26.1  christos 	"pcm16",
     59  1.7.26.1  christos 	"pcm8",
     60  1.7.26.1  christos 	AudioEadpcm,
     61  1.7.26.1  christos 	AudioEslinear_le,
     62  1.7.26.1  christos 	AudioEslinear_be,
     63  1.7.26.1  christos 	AudioEulinear_le,
     64  1.7.26.1  christos 	AudioEulinear_be,
     65  1.7.26.1  christos 	AudioEslinear,
     66  1.7.26.1  christos 	AudioEulinear,
     67  1.7.26.1  christos 	AudioEmpeg_l1_stream,
     68  1.7.26.1  christos 	AudioEmpeg_l1_packets,
     69  1.7.26.1  christos 	AudioEmpeg_l1_system,
     70  1.7.26.1  christos 	AudioEmpeg_l2_stream,
     71  1.7.26.1  christos 	AudioEmpeg_l2_packets,
     72  1.7.26.1  christos 	AudioEmpeg_l2_system,
     73  1.7.26.1  christos 	AudioEac3,
     74  1.7.26.1  christos };
     75  1.7.26.1  christos u_int encoding_max = __arraycount(encoding_names);
     76  1.7.26.1  christos 
     77       1.6  jmcneill static void
     78       1.6  jmcneill print_audiodev(struct audiodev *adev, int i)
     79       1.6  jmcneill {
     80  1.7.26.1  christos 	struct audiofmt *f;
     81  1.7.26.1  christos 	int j;
     82  1.7.26.1  christos 
     83       1.6  jmcneill 	assert(adev != NULL);
     84       1.6  jmcneill 
     85       1.6  jmcneill 	printf("%u: [%c] %s @ %s: ",
     86       1.6  jmcneill 	    i, adev->defaultdev ? '*' : ' ',
     87       1.6  jmcneill 	    adev->xname, adev->pxname);
     88       1.6  jmcneill 	printf("%s", adev->audio_device.name);
     89       1.6  jmcneill 	if (strlen(adev->audio_device.version) > 0)
     90       1.6  jmcneill 		printf(" %s", adev->audio_device.version);
     91  1.7.26.1  christos 	printf("\n");
     92  1.7.26.1  christos 	printf("       playback: ");
     93  1.7.26.2    martin 	if ((adev->hwinfo.mode & AUMODE_PLAY)) {
     94  1.7.26.1  christos 		printf("%uch, %uHz\n",
     95  1.7.26.2    martin 		    adev->hwinfo.play.channels,
     96  1.7.26.2    martin 		    adev->hwinfo.play.sample_rate);
     97  1.7.26.2    martin 	} else {
     98  1.7.26.1  christos 		printf("unavailable\n");
     99  1.7.26.2    martin 	}
    100  1.7.26.1  christos 	printf("       record:   ");
    101  1.7.26.2    martin 	if ((adev->hwinfo.mode & AUMODE_RECORD)) {
    102  1.7.26.1  christos 		printf("%uch, %uHz\n",
    103  1.7.26.2    martin 		    adev->hwinfo.record.channels,
    104  1.7.26.2    martin 		    adev->hwinfo.record.sample_rate);
    105  1.7.26.2    martin 	} else {
    106  1.7.26.1  christos 		printf("unavailable\n");
    107  1.7.26.2    martin 	}
    108  1.7.26.1  christos 
    109  1.7.26.1  christos 	TAILQ_FOREACH(f, &adev->formats, next) {
    110  1.7.26.1  christos 		printf("       ");
    111  1.7.26.1  christos 		if (f->fmt.priority < 0)
    112  1.7.26.1  christos 			printf("(  ) ");
    113  1.7.26.1  christos 		else if ((f->fmt.mode & (AUMODE_PLAY | AUMODE_RECORD))
    114  1.7.26.1  christos 		    == (AUMODE_PLAY | AUMODE_RECORD))
    115  1.7.26.1  christos 			printf("(PR) ");
    116  1.7.26.1  christos 		else if ((f->fmt.mode & AUMODE_PLAY))
    117  1.7.26.1  christos 			printf("(P-) ");
    118  1.7.26.1  christos 		else if ((f->fmt.mode & AUMODE_RECORD))
    119  1.7.26.1  christos 			printf("(-R) ");
    120  1.7.26.1  christos 
    121  1.7.26.1  christos 		if (f->fmt.encoding < encoding_max)
    122  1.7.26.1  christos 			printf("%s", encoding_names[f->fmt.encoding]);
    123  1.7.26.1  christos 		else
    124  1.7.26.1  christos 			printf("unknown_encoding_%d", f->fmt.encoding);
    125  1.7.26.1  christos 		printf(" %d/%d, %dch, ",
    126  1.7.26.1  christos 		    f->fmt.validbits,
    127  1.7.26.1  christos 		    f->fmt.precision,
    128  1.7.26.1  christos 		    f->fmt.channels);
    129  1.7.26.1  christos 		if (f->fmt.frequency_type == 0) {
    130  1.7.26.1  christos 			printf("%d-%dHz",
    131  1.7.26.1  christos 			    f->fmt.frequency[0],
    132  1.7.26.1  christos 			    f->fmt.frequency[1]);
    133  1.7.26.1  christos 		} else {
    134  1.7.26.1  christos 			for (j = 0; j < (int)f->fmt.frequency_type; j++) {
    135  1.7.26.1  christos 				printf("%s%d",
    136  1.7.26.1  christos 				    (j == 0) ? "{ " : ", ",
    137  1.7.26.1  christos 				    f->fmt.frequency[j]);
    138  1.7.26.1  christos 			}
    139  1.7.26.1  christos 			printf(" }");
    140  1.7.26.1  christos 		}
    141  1.7.26.1  christos 		printf("\n");
    142  1.7.26.1  christos 	}
    143       1.6  jmcneill }
    144       1.6  jmcneill 
    145       1.1       mrg int
    146       1.1       mrg main(int argc, char *argv[])
    147       1.1       mrg {
    148       1.1       mrg 	struct audiodev *adev;
    149       1.1       mrg 	unsigned int n, i;
    150  1.7.26.1  christos 	unsigned int j;
    151  1.7.26.1  christos 	const char *enc;
    152  1.7.26.1  christos 	unsigned int prec;
    153  1.7.26.1  christos 	unsigned int ch;
    154  1.7.26.1  christos 	unsigned int freq;
    155  1.7.26.1  christos 	int mode;
    156       1.1       mrg 
    157       1.1       mrg 	if (audiodev_refresh() == -1)
    158       1.1       mrg 		return EXIT_FAILURE;
    159       1.1       mrg 
    160       1.2  jmcneill 	if (argc < 2)
    161  1.7.26.2    martin 		usage();
    162       1.2  jmcneill 		/* NOTREACHED */
    163       1.2  jmcneill 
    164  1.7.26.1  christos 	if (strcmp(argv[1], "list") == 0 && argc == 2) {
    165       1.1       mrg 		n = audiodev_count();
    166       1.6  jmcneill 		for (i = 0; i < n; i++)
    167       1.6  jmcneill 			print_audiodev(audiodev_get(i), i);
    168  1.7.26.1  christos 	} else if (strcmp(argv[1], "list") == 0 && argc == 3) {
    169  1.7.26.2    martin 		if (*argv[2] < '0' || *argv[2] > '9')
    170  1.7.26.2    martin 			usage();
    171  1.7.26.2    martin 			/* NOTREACHED */
    172  1.7.26.1  christos 		errno = 0;
    173  1.7.26.1  christos 		i = strtoul(argv[2], NULL, 10);
    174  1.7.26.1  christos 		if (errno)
    175  1.7.26.2    martin 			usage();
    176  1.7.26.1  christos 			/* NOTREACHED */
    177  1.7.26.2    martin 		adev = audiodev_get(i);
    178  1.7.26.2    martin 		if (adev == NULL) {
    179  1.7.26.2    martin 			errx(EXIT_FAILURE, "no such device");
    180  1.7.26.2    martin 		}
    181  1.7.26.2    martin 		print_audiodev(adev, i);
    182       1.2  jmcneill 	} else if (strcmp(argv[1], "default") == 0 && argc == 3) {
    183       1.2  jmcneill 		if (*argv[2] < '0' || *argv[2] > '9')
    184  1.7.26.2    martin 			usage();
    185       1.1       mrg 			/* NOTREACHED */
    186       1.1       mrg 		errno = 0;
    187       1.2  jmcneill 		i = strtoul(argv[2], NULL, 10);
    188       1.1       mrg 		if (errno)
    189  1.7.26.2    martin 			usage();
    190       1.1       mrg 			/* NOTREACHED */
    191       1.1       mrg 		adev = audiodev_get(i);
    192       1.1       mrg 		if (adev == NULL) {
    193  1.7.26.2    martin 			errx(EXIT_FAILURE, "no such device");
    194       1.1       mrg 		}
    195       1.1       mrg 		printf("setting default audio device to %s\n", adev->xname);
    196       1.1       mrg 		if (audiodev_set_default(adev) == -1) {
    197  1.7.26.2    martin 			errx(EXIT_FAILURE, "couldn't set default device");
    198       1.1       mrg 		}
    199  1.7.26.1  christos 	} else if (strcmp(argv[1], "set") == 0 && argc == 8) {
    200  1.7.26.1  christos 		/* XXX bad commandline... */
    201  1.7.26.1  christos 		/* audiocfg set <index> [p|r] <enc> <prec> <ch> <freq> */
    202  1.7.26.1  christos 		if (*argv[2] < '0' || *argv[2] > '9')
    203  1.7.26.2    martin 			usage();
    204  1.7.26.1  christos 			/* NOTREACHED */
    205  1.7.26.1  christos 		errno = 0;
    206  1.7.26.1  christos 		i = strtoul(argv[2], NULL, 10);
    207  1.7.26.1  christos 		if (errno)
    208  1.7.26.2    martin 			usage();
    209  1.7.26.1  christos 			/* NOTREACHED */
    210  1.7.26.1  christos 		adev = audiodev_get(i);
    211  1.7.26.1  christos 		if (adev == NULL) {
    212  1.7.26.2    martin 			errx(EXIT_FAILURE, "no such device");
    213  1.7.26.1  christos 		}
    214  1.7.26.1  christos 
    215  1.7.26.1  christos 		mode = 0;
    216  1.7.26.1  christos 		for (j = 0; j < strlen(argv[3]); j++) {
    217  1.7.26.1  christos 			if (argv[3][j] == 'p')
    218  1.7.26.1  christos 				mode |= AUMODE_PLAY;
    219  1.7.26.1  christos 			else if (argv[3][j] == 'r')
    220  1.7.26.1  christos 				mode |= AUMODE_RECORD;
    221  1.7.26.1  christos 			else
    222  1.7.26.2    martin 				usage();
    223  1.7.26.1  christos 		}
    224  1.7.26.2    martin 		if (mode == 0)
    225  1.7.26.2    martin 			usage();
    226  1.7.26.2    martin 			/* NOTREACHED */
    227  1.7.26.1  christos 		enc = argv[4];
    228  1.7.26.1  christos 		prec = strtoul(argv[5], NULL, 10);
    229  1.7.26.1  christos 		if (errno)
    230  1.7.26.2    martin 			usage();
    231  1.7.26.1  christos 		errno = 0;
    232  1.7.26.1  christos 		ch = strtoul(argv[6], NULL, 10);
    233  1.7.26.1  christos 		if (errno)
    234  1.7.26.2    martin 			usage();
    235  1.7.26.1  christos 			/* NOTREACHED */
    236  1.7.26.1  christos 		errno = 0;
    237  1.7.26.1  christos 		freq = strtoul(argv[7], NULL, 10);
    238  1.7.26.1  christos 		if (errno)
    239  1.7.26.2    martin 			usage();
    240  1.7.26.1  christos 			/* NOTREACHED */
    241  1.7.26.1  christos 
    242  1.7.26.1  christos 		if (audiodev_set_param(adev, mode, enc, prec, ch, freq) == -1) {
    243  1.7.26.2    martin 			errx(EXIT_FAILURE, "couldn't set parameter");
    244  1.7.26.1  christos 		}
    245       1.2  jmcneill 	} else if (strcmp(argv[1], "test") == 0 && argc == 3) {
    246       1.2  jmcneill 		if (*argv[2] < '0' || *argv[2] > '9')
    247  1.7.26.2    martin 			usage();
    248       1.2  jmcneill 			/* NOTREACHED */
    249       1.2  jmcneill 		errno = 0;
    250       1.2  jmcneill 		i = strtoul(argv[2], NULL, 10);
    251       1.2  jmcneill 		if (errno)
    252  1.7.26.2    martin 			usage();
    253       1.2  jmcneill 			/* NOTREACHED */
    254       1.2  jmcneill 		adev = audiodev_get(i);
    255       1.2  jmcneill 		if (adev == NULL) {
    256  1.7.26.2    martin 			errx(EXIT_FAILURE, "no such device");
    257       1.2  jmcneill 		}
    258       1.6  jmcneill 		print_audiodev(adev, i);
    259  1.7.26.2    martin 		if (audiodev_test(adev) == -1)
    260  1.7.26.2    martin 			return EXIT_FAILURE;
    261       1.2  jmcneill 	} else
    262  1.7.26.2    martin 		usage();
    263       1.1       mrg 		/* NOTREACHED */
    264       1.1       mrg 
    265       1.1       mrg 	return EXIT_SUCCESS;
    266       1.1       mrg }
    267