xref: /openbsd-src/regress/lib/libsndio/cap/cap.c (revision 06f016bf3b63f9eab843dc061ef3fb4975a29f44)
1f153e440Sratchov #include <errno.h>
2f153e440Sratchov #include <fcntl.h>
3f153e440Sratchov #include <poll.h>
4f153e440Sratchov #include <stdio.h>
5f153e440Sratchov #include <string.h>
6f153e440Sratchov #include <stdlib.h>
7f153e440Sratchov #include <unistd.h>
8f153e440Sratchov #include <sndio.h>
978ccc913Sratchov #include "tools.h"
10f153e440Sratchov 
11f153e440Sratchov struct sio_par par;
12f153e440Sratchov struct sio_cap cap;
13f153e440Sratchov 
14f153e440Sratchov void
pr_enc(struct sio_enc * enc)15f153e440Sratchov pr_enc(struct sio_enc *enc)
16f153e440Sratchov {
17f153e440Sratchov 	fprintf(stderr, "%s%d", enc->sig ? "s" : "u", enc->bits);
18f153e440Sratchov 	if (enc->bps > 1)
19f153e440Sratchov 		fprintf(stderr, "%s", enc->le ? "le" : "be");
20f153e440Sratchov 	if (enc->bps != SIO_BPS(enc->bits))
21f153e440Sratchov 		fprintf(stderr, "%d%s", enc->bps, enc->msb ? "msb" : "lsb");
22f153e440Sratchov }
23f153e440Sratchov 
24f153e440Sratchov void
cap_pr(struct sio_cap * cap)25f153e440Sratchov cap_pr(struct sio_cap *cap)
26f153e440Sratchov {
27f153e440Sratchov 	unsigned n, i;
28f153e440Sratchov 
29f153e440Sratchov 	for (n = 0; n < cap->nconf; n++) {
30f153e440Sratchov 		fprintf(stderr, "config %d\n", n);
31f153e440Sratchov 		fprintf(stderr, "\tenc:");
32f153e440Sratchov 		for (i = 0; i < SIO_NENC; i++) {
33f153e440Sratchov 			if (cap->confs[n].enc & (1 << i)) {
34f153e440Sratchov 				fprintf(stderr, " ");
35f153e440Sratchov 				pr_enc(&cap->enc[i]);
36f153e440Sratchov 			}
37f153e440Sratchov 		}
38f153e440Sratchov 		fprintf(stderr, "\n\tpchan:");
39f153e440Sratchov 		for (i = 0; i < SIO_NCHAN; i++) {
40f153e440Sratchov 			if (cap->confs[n].pchan & (1 << i))
41f153e440Sratchov 				fprintf(stderr, " %d", cap->pchan[i]);
42f153e440Sratchov 		}
43f153e440Sratchov 		fprintf(stderr, "\n\trchan:");
44f153e440Sratchov 		for (i = 0; i < SIO_NCHAN; i++) {
45f153e440Sratchov 			if (cap->confs[n].rchan & (1 << i))
46f153e440Sratchov 				fprintf(stderr, " %d", cap->rchan[i]);
47f153e440Sratchov 		}
48f153e440Sratchov 		fprintf(stderr, "\n\trate:");
49f153e440Sratchov 		for (i = 0; i < SIO_NRATE; i++) {
50f153e440Sratchov 			if (cap->confs[n].rate & (1 << i))
51f153e440Sratchov 				fprintf(stderr, " %d", cap->rate[i]);
52f153e440Sratchov 		}
53f153e440Sratchov 		fprintf(stderr, "\n");
54f153e440Sratchov 	}
55f153e440Sratchov }
56f153e440Sratchov 
57f153e440Sratchov void
usage(void)58f153e440Sratchov usage(void) {
59f153e440Sratchov 	fprintf(stderr, "usage: cap [-pr]\n");
60f153e440Sratchov }
61f153e440Sratchov 
62f153e440Sratchov int
main(int argc,char ** argv)63f153e440Sratchov main(int argc, char **argv) {
64f153e440Sratchov 	int ch;
65f153e440Sratchov 	unsigned mode = SIO_PLAY | SIO_REC;
66f153e440Sratchov 	struct sio_hdl *hdl;
67f153e440Sratchov 
68f153e440Sratchov 	while ((ch = getopt(argc, argv, "pr")) != -1) {
69f153e440Sratchov 		switch(ch) {
70f153e440Sratchov 		case 'p':
71f153e440Sratchov 			mode &= ~SIO_REC;
72f153e440Sratchov 			break;
73f153e440Sratchov 		case 'r':
74f153e440Sratchov 			mode &= ~SIO_PLAY;
75f153e440Sratchov 			break;
76f153e440Sratchov 		default:
77f153e440Sratchov 			usage();
78f153e440Sratchov 			exit(1);
79f153e440Sratchov 			break;
80f153e440Sratchov 		}
81f153e440Sratchov 	}
82f153e440Sratchov 	if (mode == 0) {
83*06f016bfSratchov 		fprintf(stderr, "-p and -r flags are mutually exclusive\n");
84f153e440Sratchov 		exit(1);
85f153e440Sratchov 	}
86f153e440Sratchov 	hdl = sio_open(NULL, mode, 0);
87f153e440Sratchov 	if (hdl == NULL) {
88f153e440Sratchov 		fprintf(stderr, "sio_open() failed\n");
89f153e440Sratchov 		exit(1);
90f153e440Sratchov 	}
91f153e440Sratchov 	if (!sio_getcap(hdl, &cap)) {
92*06f016bfSratchov 		fprintf(stderr, "sio_getcap() failed\n");
93f153e440Sratchov 		exit(1);
94f153e440Sratchov 	}
95f153e440Sratchov 	cap_pr(&cap);
96f153e440Sratchov 	sio_close(hdl);
97f153e440Sratchov 	return 0;
98f153e440Sratchov }
99