Home | History | Annotate | Line # | Download | only in aiomixer
      1 /* $NetBSD: parse.c,v 1.4 2025/10/19 15:59:19 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 		/* put "master" controls first */
     68 		if (strstr(a->info.label.name, AudioNmaster) != NULL) {
     69 			if (strstr(b->info.label.name, AudioNmaster) == NULL) {
     70 				return -1;
     71 			}
     72 		} else if (strstr(b->info.label.name, AudioNmaster) != NULL) {
     73 			return 1;
     74 		}
     75 		return strcmp(a->info.label.name, b->info.label.name);
     76 	}
     77 	return 0;
     78 }
     79 
     80 static int
     81 compare_class(const void *pa, const void *pb)
     82 {
     83 	const struct aiomixer_class *a = (const struct aiomixer_class *)pa;
     84 	const struct aiomixer_class *b = (const struct aiomixer_class *)pb;
     85 
     86 	if (strcmp(a->name, AudioCoutputs) == 0)
     87 		return -1;
     88 	if (strcmp(b->name, AudioCoutputs) == 0)
     89 		return 1;
     90 
     91 	return strcmp(a->name, b->name);
     92 }
     93 
     94 int
     95 aiomixer_parse(struct aiomixer *aio)
     96 {
     97 	size_t i;
     98 	struct mixer_devinfo info;
     99 	struct aiomixer_class *class;
    100 	struct aiomixer_control *control;
    101 
    102 	for (info.index = 0;
    103 	    ioctl(aio->fd, AUDIO_MIXER_DEVINFO, &info) != -1; ++info.index) {
    104 		if (info.type != AUDIO_MIXER_CLASS)
    105 			continue;
    106 		if (aio->numclasses >= __arraycount(aio->classes))
    107 			break;
    108 		class = &aio->classes[aio->numclasses++];
    109 		memcpy(class->name, info.label.name, MAX_AUDIO_DEV_LEN);
    110 		class->index = info.index;
    111 		class->numcontrols = 0;
    112 	}
    113 	for (info.index = 0;
    114 	    ioctl(aio->fd, AUDIO_MIXER_DEVINFO, &info) != -1; ++info.index) {
    115 		if (info.type == AUDIO_MIXER_CLASS)
    116 			continue;
    117 		if (info.type == AUDIO_MIXER_VALUE) {
    118 			/* XXX workaround for hdaudio(4) bugs */
    119 			if (info.un.v.delta > AUDIO_MAX_GAIN)
    120 				continue;
    121 			if (info.un.v.num_channels == 0)
    122 				continue;
    123 		}
    124 		class = get_class(aio, info.mixer_class);
    125 		if (class == NULL)
    126 			break;
    127 		if (class->numcontrols >= __arraycount(class->controls))
    128 			break;
    129 		control = &class->controls[class->numcontrols++];
    130 		control->info = info;
    131 	}
    132 	qsort(aio->classes, aio->numclasses,
    133 	    sizeof(struct aiomixer_class),
    134 	    compare_class);
    135 	for (i = 0; i < aio->numclasses; ++i) {
    136 		class = &aio->classes[i];
    137 		qsort(class->controls, class->numcontrols,
    138 		    sizeof(struct aiomixer_control),
    139 		    compare_control);
    140 	}
    141 	return 0;
    142 }
    143 
    144