draw.c revision 1.4 1 /* $NetBSD: draw.c,v 1.4 2021/05/08 13:28:45 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 wprintw(control->widgetpad, "\n");
118 }
119
120 void
121 draw_screen(struct aiomixer *aio)
122 {
123 int i, max_y;
124
125 /* Clear any leftovers if the screen changed. */
126 if (aio->widgets_resized) {
127 aio->widgets_resized = false;
128 max_y = getmaxy(stdscr);
129 for (i = 3; i < max_y; ++i) {
130 mvprintw(i, 0, "\n");
131 }
132 }
133
134 wnoutrefresh(stdscr);
135 wnoutrefresh(aio->header);
136 wnoutrefresh(aio->classbar);
137 pnoutrefresh(aio->classes[aio->curclass].widgetpad,
138 aio->class_scroll_y, 0,
139 3, 0,
140 1 + aio->classes[aio->curclass].height, getmaxx(stdscr));
141 doupdate();
142 }
143
144 static int
145 get_enum_color(const char *name)
146 {
147 if (strcmp(name, AudioNon) == 0) {
148 return COLOR_ENUM_ON;
149 }
150 if (strcmp(name, AudioNoff) == 0) {
151 return COLOR_ENUM_OFF;
152 }
153
154 return COLOR_ENUM_MISC;
155 }
156
157 static void
158 draw_enum(struct aiomixer_control *control, int ord, bool selected)
159 {
160 struct audio_mixer_enum *e;
161 int color = COLOR_ENUM_MISC;
162 int i;
163
164 for (i = 0; i < control->info.un.e.num_mem; ++i) {
165 e = &control->info.un.e;
166 if (ord == e->member[i].ord && selected) {
167 if (termattrs() & A_BOLD)
168 wattron(control->widgetpad, A_BOLD);
169 else
170 wattron(control->widgetpad, A_STANDOUT);
171 }
172 waddch(control->widgetpad, '[');
173 if (ord == e->member[i].ord) {
174 if (has_colors()) {
175 color = get_enum_color(e->member[i].label.name);
176 wattron(control->widgetpad,
177 COLOR_PAIR(color));
178 } else {
179 waddch(control->widgetpad, '*');
180 }
181 }
182 wprintw(control->widgetpad, "%s", e->member[i].label.name);
183 if (ord == control->info.un.e.member[i].ord) {
184 if (has_colors()) {
185 wattroff(control->widgetpad,
186 COLOR_PAIR(color));
187 }
188 }
189 waddch(control->widgetpad, ']');
190 if (ord == e->member[i].ord && selected) {
191 if (termattrs() & A_BOLD)
192 wattroff(control->widgetpad, A_BOLD);
193 else
194 wattroff(control->widgetpad, A_STANDOUT);
195 }
196 if (i != (e->num_mem - 1))
197 waddstr(control->widgetpad, ", ");
198 }
199 waddch(control->widgetpad, '\n');
200 }
201
202 static void
203 draw_set(struct aiomixer_control *control, int mask)
204 {
205 int i;
206
207 for (i = 0; i < control->info.un.s.num_mem; ++i) {
208 waddch(control->widgetpad, '[');
209 if (mask & control->info.un.s.member[i].mask) {
210 if (has_colors()) {
211 wattron(control->widgetpad,
212 COLOR_PAIR(COLOR_SET_SELECTED));
213 }
214 waddch(control->widgetpad, '*');
215 if (has_colors()) {
216 wattroff(control->widgetpad,
217 COLOR_PAIR(COLOR_SET_SELECTED));
218 }
219 } else {
220 waddch(control->widgetpad, ' ');
221 }
222 waddstr(control->widgetpad, "] ");
223 if (control->setindex == i) {
224 if (termattrs() & A_BOLD)
225 wattron(control->widgetpad, A_BOLD);
226 else
227 wattron(control->widgetpad, A_STANDOUT);
228 }
229 wprintw(control->widgetpad, "%s",
230 control->info.un.s.member[i].label.name);
231 if (control->setindex == i) {
232 if (termattrs() & A_BOLD)
233 wattroff(control->widgetpad, A_BOLD);
234 else
235 wattroff(control->widgetpad, A_STANDOUT);
236 }
237 if (i != (control->info.un.s.num_mem - 1))
238 waddstr(control->widgetpad, ", ");
239 }
240 }
241
242 static void
243 draw_levels(struct aiomixer_control *control,
244 const struct mixer_level *levels, bool channels_unlocked, bool selected)
245 {
246 int i;
247 int j, nchars;
248
249 for (i = 0; i < control->info.un.v.num_channels; ++i) {
250 if ((selected && !channels_unlocked) ||
251 (control->setindex == i && channels_unlocked)) {
252 if (termattrs() & A_BOLD)
253 wattron(control->widgetpad, A_BOLD);
254 else
255 wattron(control->widgetpad, A_STANDOUT);
256 }
257 wprintw(control->widgetpad, "[%3u/%3u ",
258 levels->level[i], AUDIO_MAX_GAIN);
259 if (has_colors()) {
260 wattron(control->widgetpad,
261 COLOR_PAIR(COLOR_LEVELS));
262 }
263 nchars = (levels->level[i] *
264 (getmaxx(control->widgetpad) - 11)) / AUDIO_MAX_GAIN;
265 for (j = 0; j < nchars; ++j)
266 waddch(control->widgetpad, '*');
267 if (has_colors()) {
268 wattroff(control->widgetpad,
269 COLOR_PAIR(COLOR_LEVELS));
270 }
271 nchars = getmaxx(control->widgetpad) - 11 - nchars;
272 for (j = 0; j < nchars; ++j)
273 waddch(control->widgetpad, ' ');
274 wprintw(control->widgetpad, "]\n");
275 if ((selected && !channels_unlocked) ||
276 (control->setindex == i && channels_unlocked)) {
277 if (termattrs() & A_BOLD)
278 wattroff(control->widgetpad, A_BOLD);
279 else
280 wattroff(control->widgetpad, A_STANDOUT);
281 }
282 }
283 }
284
285 void
286 draw_classbar(struct aiomixer *aio)
287 {
288 unsigned int i;
289
290 wmove(aio->classbar, 0, 0);
291
292 for (i = 0; i < aio->numclasses; ++i) {
293 if (aio->curclass == i)
294 wattron(aio->classbar, A_STANDOUT);
295 wprintw(aio->classbar, "[%u:%s]",
296 i + 1, aio->classes[i].name);
297 if (aio->curclass == i)
298 wattroff(aio->classbar, A_STANDOUT);
299 waddch(aio->classbar, ' ');
300 }
301
302 wprintw(aio->classbar, "\n\n");
303 }
304
305 void
306 draw_header(struct aiomixer *aio)
307 {
308 wprintw(aio->header, "\n");
309 mvwaddstr(aio->header, 0,
310 getmaxx(aio->header) - (int)sizeof("NetBSD audio mixer") + 1,
311 "NetBSD audio mixer");
312
313 if (aio->mixerdev.version[0] != '\0') {
314 mvwprintw(aio->header, 0, 0, "%s %s",
315 aio->mixerdev.name, aio->mixerdev.version);
316 } else {
317 mvwprintw(aio->header, 0, 0, "%s", aio->mixerdev.name);
318 }
319 }
320
321 void
322 create_widgets(struct aiomixer *aio)
323 {
324 size_t i, j;
325 struct aiomixer_class *class;
326 struct aiomixer_control *control;
327
328 aio->header = newwin(1, getmaxx(stdscr), 0, 0);
329 if (aio->header == NULL)
330 errx(EXIT_FAILURE, "failed to create window");
331
332 aio->classbar = newwin(2, getmaxx(stdscr), 1, 0);
333 if (aio->classbar == NULL)
334 errx(EXIT_FAILURE, "failed to create window");
335
336 for (i = 0; i < aio->numclasses; ++i) {
337 class = &aio->classes[i];
338 class->height = 0;
339 class->widgetpad = newpad(4 * __arraycount(class->controls),
340 getmaxx(stdscr));
341 if (class->widgetpad == NULL)
342 errx(EXIT_FAILURE, "failed to create curses pad");
343 for (j = 0; j < class->numcontrols; ++j) {
344 control = &class->controls[j];
345 switch (control->info.type) {
346 case AUDIO_MIXER_VALUE:
347 control->height = 2 +
348 control->info.un.v.num_channels;
349 break;
350 case AUDIO_MIXER_ENUM:
351 case AUDIO_MIXER_SET:
352 control->height = 3;
353 break;
354 }
355 control->widgetpad = subpad(class->widgetpad,
356 control->height, getmaxx(stdscr),
357 class->height, 0);
358 if (control->widgetpad == NULL)
359 errx(EXIT_FAILURE, "failed to create curses pad");
360 control->widget_y = class->height;
361 class->height += control->height;
362 }
363 wresize(class->widgetpad, class->height, getmaxx(stdscr));
364 }
365
366 aio->last_max_x = getmaxx(stdscr);
367 }
368
369 void
370 resize_widgets(struct aiomixer *aio)
371 {
372 size_t i, j;
373 struct aiomixer_class *class;
374 struct aiomixer_control *control;
375 int max_x;
376
377 max_x = getmaxx(stdscr);
378
379 if (aio->last_max_x != max_x) {
380 aio->last_max_x = max_x;
381 wresize(aio->header, 1, max_x);
382 wresize(aio->classbar, 2, max_x);
383
384 for (i = 0; i < aio->numclasses; ++i) {
385 class = &aio->classes[i];
386 wresize(class->widgetpad, class->height, max_x);
387 for (j = 0; j < class->numcontrols; ++j) {
388 control = &class->controls[j];
389 wresize(control->widgetpad,
390 control->height, max_x);
391 }
392 }
393 }
394
395 aio->widgets_resized = true;
396 }
397