Home | History | Annotate | Line # | Download | only in aiomixer
      1 /* $NetBSD: parse.c,v 1.3 2021/05/08 13:03:40 nia Exp $ */
      2 /*-
      3  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Nia Alarie.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 #include <sys/audioio.h>
     31 #include <sys/ioctl.h>
     32 #include <curses.h>
     33 #include <stdlib.h>
     34 #include "app.h"
     35 #include "parse.h"
     36 
     37 static struct aiomixer_class *get_class(struct aiomixer *, int);
     38 static int compare_control(const void *, const void *);
     39 static int compare_class(const void *, const void *);
     40 
     41 static struct aiomixer_class *
     42 get_class(struct aiomixer *aio, int class)
     43 {
     44 	size_t i;
     45 
     46 	for (i = 0; i < aio->numclasses; ++i) {
     47 		if (aio->classes[i].index == class) {
     48 			return &aio->classes[i];
     49 		}
     50 	}
     51 	return NULL;
     52 }
     53 
     54 static int
     55 compare_control(const void *pa, const void *pb)
     56 {
     57 	const struct aiomixer_control *a = (const struct aiomixer_control *)pa;
     58 	const struct aiomixer_control *b = (const struct aiomixer_control *)pb;
     59 
     60 	if (a->info.prev != AUDIO_MIXER_LAST ||
     61 	    b->info.prev != AUDIO_MIXER_LAST) {
     62 		if (b->info.prev == a->info.index)
     63 			return -1;
     64 		if (a->info.prev == b->info.index)
     65 			return 1;
     66 	} else {
     67 		return strcmp(a->info.label.name, b->info.label.name);
     68 	}
     69 	return 0;
     70 }
     71 
     72 static int
     73 compare_class(const void *pa, const void *pb)
     74 {
     75 	const struct aiomixer_class *a = (const struct aiomixer_class *)pa;
     76 	const struct aiomixer_class *b = (const struct aiomixer_class *)pb;
     77 
     78 	if (strcmp(a->name, AudioCoutputs) == 0)
     79 		return -1;
     80 	if (strcmp(b->name, AudioCoutputs) == 0)
     81 		return 1;
     82 
     83 	return strcmp(a->name, b->name);
     84 }
     85 
     86 int
     87 aiomixer_parse(struct aiomixer *aio)
     88 {
     89 	size_t i;
     90 	struct mixer_devinfo info;
     91 	struct aiomixer_class *class;
     92 	struct aiomixer_control *control;
     93 
     94 	for (info.index = 0;
     95 	    ioctl(aio->fd, AUDIO_MIXER_DEVINFO, &info) != -1; ++info.index) {
     96 		if (info.type != AUDIO_MIXER_CLASS)
     97 			continue;
     98 		if (aio->numclasses >= __arraycount(aio->classes))
     99 			break;
    100 		class = &aio->classes[aio->numclasses++];
    101 		memcpy(class->name, info.label.name, MAX_AUDIO_DEV_LEN);
    102 		class->index = info.index;
    103 		class->numcontrols = 0;
    104 	}
    105 	for (info.index = 0;
    106 	    ioctl(aio->fd, AUDIO_MIXER_DEVINFO, &info) != -1; ++info.index) {
    107 		if (info.type == AUDIO_MIXER_CLASS)
    108 			continue;
    109 		if (info.type == AUDIO_MIXER_VALUE) {
    110 			/* XXX workaround for hdaudio(4) bugs */
    111 			if (info.un.v.delta > AUDIO_MAX_GAIN)
    112 				continue;
    113 			if (info.un.v.num_channels == 0)
    114 				continue;
    115 		}
    116 		class = get_class(aio, info.mixer_class);
    117 		if (class == NULL)
    118 			break;
    119 		if (class->numcontrols >= __arraycount(class->controls))
    120 			break;
    121 		control = &class->controls[class->numcontrols++];
    122 		control->info = info;
    123 	}
    124 	qsort(aio->classes, aio->numclasses,
    125 	    sizeof(struct aiomixer_class),
    126 	    compare_class);
    127 	for (i = 0; i < aio->numclasses; ++i) {
    128 		class = &aio->classes[i];
    129 		qsort(class->controls, class->numcontrols,
    130 		    sizeof(struct aiomixer_control),
    131 		    compare_control);
    132 	}
    133 	return 0;
    134 }
    135 
    136