Home | History | Annotate | Line # | Download | only in aiomixer
draw.c revision 1.3
      1 /* $NetBSD: draw.c,v 1.3 2021/05/07 19:37:03 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 <fcntl.h>
     33 #include <unistd.h>
     34 #include <curses.h>
     35 #include <err.h>
     36 #include <stdlib.h>
     37 #include "draw.h"
     38 
     39 static int get_enum_color(const char *);
     40 static void draw_enum(struct aiomixer_control *, int, bool);
     41 static void draw_set(struct aiomixer_control *, int);
     42 static void draw_levels(struct aiomixer_control *,
     43     const struct mixer_level *, bool, bool);
     44 
     45 void
     46 draw_mixer_select(unsigned int num_mixers, unsigned int selected_mixer)
     47 {
     48 	struct audio_device dev;
     49 	char mixer_path[16];
     50 	unsigned int i;
     51 	int fd;
     52 
     53 	mvprintw(0, 0, "Select a mixer device:\n");
     54 
     55 	for (i = 0; i < num_mixers; ++i) {
     56 		(void)snprintf(mixer_path, sizeof(mixer_path),
     57 		    "/dev/mixer%d", i);
     58 		fd = open(mixer_path, O_RDWR);
     59 		if (fd == -1)
     60 			break;
     61 		if (ioctl(fd, AUDIO_GETDEV, &dev) < 0) {
     62 			close(fd);
     63 			break;
     64 		}
     65 		close(fd);
     66 		if (selected_mixer == i) {
     67 			attron(A_STANDOUT);
     68 			addstr("[*] ");
     69 		} else {
     70 			addstr("[ ] ");
     71 		}
     72 		printw("%s: %s %s %s\n", mixer_path,
     73 		    dev.name, dev.version, dev.config);
     74 		if (selected_mixer == i)
     75 			attroff(A_STANDOUT);
     76 	}
     77 }
     78 
     79 void
     80 draw_control(struct aiomixer *aio,
     81     struct aiomixer_control *control, bool selected)
     82 {
     83 	struct mixer_ctrl value;
     84 
     85 	value.dev = control->info.index;
     86 	value.type = control->info.type;
     87 	if (value.type == AUDIO_MIXER_VALUE)
     88 		value.un.value.num_channels = control->info.un.v.num_channels;
     89 
     90 	if (ioctl(aio->fd, AUDIO_MIXER_READ, &value) < 0)
     91 		err(EXIT_FAILURE, "failed to read from mixer device");
     92 
     93 	wclear(control->widgetpad);
     94 	if (selected)
     95 		wattron(control->widgetpad, A_STANDOUT);
     96 	if (value.type == AUDIO_MIXER_VALUE)
     97 		wprintw(control->widgetpad, "%s (%s)\n",
     98 		    control->info.label.name, control->info.un.v.units.name);
     99 	else
    100 		wprintw(control->widgetpad, "%s\n", control->info.label.name);
    101 	if (selected)
    102 		wattroff(control->widgetpad, A_STANDOUT);
    103 
    104 	switch (value.type) {
    105 	case AUDIO_MIXER_ENUM:
    106 		draw_enum(control, value.un.ord, selected);
    107 		break;
    108 	case AUDIO_MIXER_SET:
    109 		draw_set(control, value.un.mask);
    110 		break;
    111 	case AUDIO_MIXER_VALUE:
    112 		draw_levels(control, &value.un.value,
    113 		    aio->channels_unlocked, selected);
    114 		break;
    115 	}
    116 }
    117 
    118 void
    119 draw_screen(struct aiomixer *aio)
    120 {
    121 	int i, max_y;
    122 
    123 	/* Clear any leftovers if the screen changed. */
    124 	if (aio->widgets_resized) {
    125 		aio->widgets_resized = false;
    126 		max_y = getmaxy(stdscr);
    127 		for (i = 3; i < max_y; ++i) {
    128 			mvprintw(i, 0, "\n");
    129 		}
    130 	}
    131 
    132 	wnoutrefresh(stdscr);
    133 	wnoutrefresh(aio->header);
    134 	wnoutrefresh(aio->classbar);
    135 	pnoutrefresh(aio->classes[aio->curclass].widgetpad,
    136 	    aio->class_scroll_y, 0,
    137 	    3, 0,
    138 	    1 + aio->classes[aio->curclass].height, getmaxx(stdscr));
    139 	doupdate();
    140 }
    141 
    142 static int
    143 get_enum_color(const char *name)
    144 {
    145 	if (strcmp(name, AudioNon) == 0) {
    146 		return COLOR_ENUM_ON;
    147 	}
    148 	if (strcmp(name, AudioNoff) == 0) {
    149 		return COLOR_ENUM_OFF;
    150 	}
    151 
    152 	return COLOR_ENUM_MISC;
    153 }
    154 
    155 static void
    156 draw_enum(struct aiomixer_control *control, int ord, bool selected)
    157 {
    158 	struct audio_mixer_enum *e;
    159 	int color = COLOR_ENUM_MISC;
    160 	int i;
    161 
    162 	for (i = 0; i < control->info.un.e.num_mem; ++i) {
    163 		e = &control->info.un.e;
    164 		if (ord == e->member[i].ord && selected) {
    165 			if (termattrs() & A_BOLD)
    166 				wattron(control->widgetpad, A_BOLD);
    167 			else
    168 				wattron(control->widgetpad, A_STANDOUT);
    169 		}
    170 		waddch(control->widgetpad, '[');
    171 		if (ord == e->member[i].ord) {
    172 			if (has_colors()) {
    173 				color = get_enum_color(e->member[i].label.name);
    174 				wattron(control->widgetpad,
    175 					COLOR_PAIR(color));
    176 			} else {
    177 				waddch(control->widgetpad, '*');
    178 			}
    179 		}
    180 		wprintw(control->widgetpad, "%s", e->member[i].label.name);
    181 		if (ord == control->info.un.e.member[i].ord) {
    182 			if (has_colors()) {
    183 				wattroff(control->widgetpad,
    184 					COLOR_PAIR(color));
    185 			}
    186 		}
    187 		waddch(control->widgetpad, ']');
    188 		if (ord == e->member[i].ord && selected) {
    189 			if (termattrs() & A_BOLD)
    190 				wattroff(control->widgetpad, A_BOLD);
    191 			else
    192 				wattroff(control->widgetpad, A_STANDOUT);
    193 		}
    194 		if (i != (e->num_mem - 1))
    195 			waddstr(control->widgetpad, ", ");
    196 	}
    197 	waddch(control->widgetpad, '\n');
    198 }
    199 
    200 static void
    201 draw_set(struct aiomixer_control *control, int mask)
    202 {
    203 	int i;
    204 
    205 	for (i = 0; i < control->info.un.s.num_mem; ++i) {
    206 		waddch(control->widgetpad, '[');
    207 		if (mask & control->info.un.s.member[i].mask) {
    208 			if (has_colors()) {
    209 				wattron(control->widgetpad,
    210 					COLOR_PAIR(COLOR_SET_SELECTED));
    211 			}
    212 			waddch(control->widgetpad, '*');
    213 			if (has_colors()) {
    214 				wattroff(control->widgetpad,
    215 					COLOR_PAIR(COLOR_SET_SELECTED));
    216 			}
    217 		} else {
    218 			waddch(control->widgetpad, ' ');
    219 		}
    220 		waddstr(control->widgetpad, "] ");
    221 		if (control->setindex == i) {
    222 			if (termattrs() & A_BOLD)
    223 				wattron(control->widgetpad, A_BOLD);
    224 			else
    225 				wattron(control->widgetpad, A_STANDOUT);
    226 		}
    227 		wprintw(control->widgetpad, "%s",
    228 		    control->info.un.s.member[i].label.name);
    229 		if (control->setindex == i) {
    230 			if (termattrs() & A_BOLD)
    231 				wattroff(control->widgetpad, A_BOLD);
    232 			else
    233 				wattroff(control->widgetpad, A_STANDOUT);
    234 		}
    235 		if (i != (control->info.un.s.num_mem - 1))
    236 			waddstr(control->widgetpad, ", ");
    237 	}
    238 }
    239 
    240 static void
    241 draw_levels(struct aiomixer_control *control,
    242     const struct mixer_level *levels, bool channels_unlocked, bool selected)
    243 {
    244 	int i;
    245 	int j, nchars;
    246 
    247 	for (i = 0; i < control->info.un.v.num_channels; ++i) {
    248 		if ((selected && !channels_unlocked) ||
    249 		    (control->setindex == i && channels_unlocked)) {
    250 			if (termattrs() & A_BOLD)
    251 				wattron(control->widgetpad, A_BOLD);
    252 			else
    253 				wattron(control->widgetpad, A_STANDOUT);
    254 		}
    255 		wprintw(control->widgetpad, "[%3u/%3u ",
    256 		    levels->level[i], AUDIO_MAX_GAIN);
    257 		if (has_colors()) {
    258 			wattron(control->widgetpad,
    259 				COLOR_PAIR(COLOR_LEVELS));
    260 		}
    261 		nchars = (levels->level[i] *
    262 		    (getmaxx(control->widgetpad) - 11)) / AUDIO_MAX_GAIN;
    263 		for (j = 0; j < nchars; ++j)
    264 			waddch(control->widgetpad, '*');
    265 		if (has_colors()) {
    266 			wattroff(control->widgetpad,
    267 				COLOR_PAIR(COLOR_LEVELS));
    268 		}
    269 		nchars = getmaxx(control->widgetpad) - 11 - nchars;
    270 		for (j = 0; j < nchars; ++j)
    271 			waddch(control->widgetpad, ' ');
    272 		wprintw(control->widgetpad, "]\n");
    273 		if ((selected && !channels_unlocked) ||
    274 		    (control->setindex == i && channels_unlocked)) {
    275 			if (termattrs() & A_BOLD)
    276 				wattroff(control->widgetpad, A_BOLD);
    277 			else
    278 				wattroff(control->widgetpad, A_STANDOUT);
    279 		}
    280 	}
    281 }
    282 
    283 void
    284 draw_classbar(struct aiomixer *aio)
    285 {
    286 	unsigned int i;
    287 
    288 	wmove(aio->classbar, 0, 0);
    289 
    290 	for (i = 0; i < aio->numclasses; ++i) {
    291 		if (aio->curclass == i)
    292 			wattron(aio->classbar, A_STANDOUT);
    293 		wprintw(aio->classbar, "[%u:%s]",
    294 		    i + 1, aio->classes[i].name);
    295 		if (aio->curclass == i)
    296 			wattroff(aio->classbar, A_STANDOUT);
    297 		waddch(aio->classbar, ' ');
    298 	}
    299 
    300 	wprintw(aio->classbar, "\n\n");
    301 }
    302 
    303 void
    304 draw_header(struct aiomixer *aio)
    305 {
    306 	wprintw(aio->header, "\n");
    307 	mvwaddstr(aio->header, 0,
    308 	    getmaxx(aio->header) - (int)sizeof("NetBSD audio mixer") + 1,
    309 	    "NetBSD audio mixer");
    310 
    311 	if (aio->mixerdev.version[0] != '\0') {
    312 		mvwprintw(aio->header, 0, 0, "%s %s",
    313 		    aio->mixerdev.name, aio->mixerdev.version);
    314 	} else {
    315 		mvwprintw(aio->header, 0, 0, "%s", aio->mixerdev.name);
    316 	}
    317 }
    318 
    319 void
    320 create_widgets(struct aiomixer *aio)
    321 {
    322 	size_t i, j;
    323 	struct aiomixer_class *class;
    324 	struct aiomixer_control *control;
    325 
    326 	aio->header = newwin(1, getmaxx(stdscr), 0, 0);
    327 	if (aio->header == NULL)
    328 		errx(EXIT_FAILURE, "failed to create window");
    329 
    330 	aio->classbar = newwin(2, getmaxx(stdscr), 1, 0);
    331 	if (aio->classbar == NULL)
    332 		errx(EXIT_FAILURE, "failed to create window");
    333 
    334 	for (i = 0; i < aio->numclasses; ++i) {
    335 		class = &aio->classes[i];
    336 		class->height = 0;
    337 		class->widgetpad = newpad(4 * __arraycount(class->controls),
    338 		    getmaxx(stdscr));
    339 		if (class->widgetpad == NULL)
    340 			errx(EXIT_FAILURE, "failed to create curses pad");
    341 		for (j = 0; j < class->numcontrols; ++j) {
    342 			control = &class->controls[j];
    343 			switch (control->info.type) {
    344 			case AUDIO_MIXER_VALUE:
    345 				control->height = 2 +
    346 				    control->info.un.v.num_channels;
    347 				break;
    348 			case AUDIO_MIXER_ENUM:
    349 			case AUDIO_MIXER_SET:
    350 				control->height = 3;
    351 				break;
    352 			}
    353 			control->widgetpad = subpad(class->widgetpad,
    354 			    control->height, getmaxx(stdscr),
    355 			    class->height, 0);
    356 			if (control->widgetpad == NULL)
    357 				errx(EXIT_FAILURE, "failed to create curses pad");
    358 			control->widget_y = class->height;
    359 			class->height += control->height;
    360 		}
    361 		wresize(class->widgetpad, class->height, getmaxx(stdscr));
    362 	}
    363 
    364 	aio->last_max_x = getmaxx(stdscr);
    365 }
    366 
    367 void
    368 resize_widgets(struct aiomixer *aio)
    369 {
    370 	size_t i, j;
    371 	struct aiomixer_class *class;
    372 	struct aiomixer_control *control;
    373 	int max_x;
    374 
    375 	max_x = getmaxx(stdscr);
    376 
    377 	if (aio->last_max_x != max_x) {
    378 		aio->last_max_x = max_x;
    379 		wresize(aio->header, 1, max_x);
    380 		wresize(aio->classbar, 2, max_x);
    381 
    382 		for (i = 0; i < aio->numclasses; ++i) {
    383 			class = &aio->classes[i];
    384 			wresize(class->widgetpad, class->height, max_x);
    385 			for (j = 0; j < class->numcontrols; ++j) {
    386 				control = &class->controls[j];
    387 				wresize(control->widgetpad,
    388 				    control->height, max_x);
    389 			}
    390 		}
    391 	}
    392 
    393 	aio->widgets_resized = true;
    394 }
    395