1 /*
2  * mainloop.c - main loop
3  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "aconfig.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <poll.h>
24 #include <panel.h>
25 #include <alsa/asoundlib.h>
26 #include "mem.h"
27 #include "die.h"
28 #include "colors.h"
29 #include "widget.h"
30 #include "mixer_widget.h"
31 #include "mixer_display.h"
32 #include "mixer_controls.h"
33 #include "mainloop.h"
34 
35 static WINDOW *curses_initialized;
36 
black_hole_error_handler(const char *file ATTRIBUTE_UNUSED, int line ATTRIBUTE_UNUSED, const char *function ATTRIBUTE_UNUSED, int err ATTRIBUTE_UNUSED, const char *fmt ATTRIBUTE_UNUSED, ...)37 static void black_hole_error_handler(const char *file ATTRIBUTE_UNUSED,
38 				     int line ATTRIBUTE_UNUSED,
39 				     const char *function ATTRIBUTE_UNUSED,
40 				     int err ATTRIBUTE_UNUSED,
41 				     const char *fmt ATTRIBUTE_UNUSED, ...)
42 {
43 }
44 
initialize_curses(bool use_color, bool use_mouse)45 void initialize_curses(bool use_color, bool use_mouse)
46 {
47 	curses_initialized = initscr();
48 	cbreak();
49 	noecho();
50 #ifdef HAVE_CURSES_ESCDELAY
51 	set_escdelay(100);
52 #endif
53 	window_size_changed(); /* update screen_lines/cols */
54 	init_colors(use_color);
55 	if (use_mouse)
56 		mousemask(ALL_MOUSE_EVENTS, NULL);
57 
58 	snd_lib_error_set_handler(black_hole_error_handler);
59 }
60 
app_shutdown(void)61 void app_shutdown(void)
62 {
63 	if (curses_initialized) {
64 		clear();
65 		refresh();
66 		curs_set(1);
67 		endwin();
68 	}
69 	mixer_shutdown();
70 }
71 
mainloop(void)72 void mainloop(void)
73 {
74 	struct pollfd *pollfds = NULL;
75 	int nfds = 0, n;
76 	const struct widget *active_widget;
77 	unsigned short revents;
78 	int key;
79 	int err;
80 
81 	for (;;) {
82 		update_panels();
83 		doupdate();
84 
85 		active_widget = get_active_widget();
86 		if (!active_widget)
87 			break;
88 
89 		n = 1 + snd_mixer_poll_descriptors_count(mixer);
90 		if (n != nfds) {
91 			free(pollfds);
92 			nfds = n;
93 			pollfds = ccalloc(nfds, sizeof *pollfds);
94 			pollfds[0].fd = fileno(stdin);
95 			pollfds[0].events = POLLIN;
96 		}
97 		err = snd_mixer_poll_descriptors(mixer, &pollfds[1], nfds - 1);
98 		if (err < 0)
99 			fatal_alsa_error("cannot get poll descriptors", err);
100 		n = poll(pollfds, nfds, -1);
101 		if (n < 0) {
102 			if (errno == EINTR) {
103 				pollfds[0].revents = 0;
104 				doupdate(); /* handle SIGWINCH */
105 			} else {
106 				fatal_error("poll error");
107 			}
108 		}
109 		if (pollfds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
110 			break;
111 		if (pollfds[0].revents & POLLIN)
112 			--n;
113 		if (n > 0) {
114 			err = snd_mixer_poll_descriptors_revents(mixer, &pollfds[1], nfds - 1, &revents);
115 			if (err < 0)
116 				fatal_alsa_error("cannot get poll events", err);
117 			if (revents & (POLLERR | POLLNVAL))
118 				close_mixer_device();
119 			else if (revents & POLLIN)
120 				snd_mixer_handle_events(mixer);
121 		}
122 		key = wgetch(active_widget->window);
123 		while (key != ERR) {
124 #ifdef KEY_RESIZE
125 			if (key == KEY_RESIZE)
126 				window_size_changed();
127 			else
128 #endif
129 				active_widget->handle_key(key);
130 			active_widget = get_active_widget();
131 			if (!active_widget)
132 				break;
133 			key = wgetch(active_widget->window);
134 		}
135 		if (!active_widget)
136 			break;
137 		if (controls_changed) {
138 			controls_changed = FALSE;
139 			create_controls();
140 			control_values_changed = FALSE;
141 			display_controls();
142 		} else if (control_values_changed) {
143 			control_values_changed = FALSE;
144 			display_controls();
145 		}
146 	}
147 	free(pollfds);
148 }
149