tarina

git clone https://git.tarina.org/tarina
Log | Files | Refs | README | LICENSE

mainloop.c (3469B)


      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 
     37 static void black_hole_error_handler(const char *file, int line,
     38 				     const char *function, int err,
     39 				     const char *fmt, ...)
     40 {
     41 }
     42 
     43 void initialize_curses(bool use_color)
     44 {
     45 	curses_initialized = initscr();
     46 	cbreak();
     47 	noecho();
     48 #ifdef HAVE_CURSES_ESCDELAY
     49 	set_escdelay(100);
     50 #endif
     51 	window_size_changed(); /* update screen_lines/cols */
     52 	init_colors(use_color);
     53 	snd_lib_error_set_handler(black_hole_error_handler);
     54 }
     55 
     56 void app_shutdown(void)
     57 {
     58 	if (curses_initialized) {
     59 		clear();
     60 		refresh();
     61 		curs_set(1);
     62 		endwin();
     63 	}
     64 	mixer_shutdown();
     65 }
     66 
     67 void mainloop(void)
     68 {
     69 	struct pollfd *pollfds = NULL;
     70 	int nfds = 0, n;
     71 	struct widget *active_widget;
     72 	unsigned short revents;
     73 	int key;
     74 	int err;
     75 
     76 	for (;;) {
     77 		update_panels();
     78 		doupdate();
     79 
     80 		active_widget = get_active_widget();
     81 		if (!active_widget)
     82 			break;
     83 
     84 		n = 1 + snd_mixer_poll_descriptors_count(mixer);
     85 		if (n != nfds) {
     86 			free(pollfds);
     87 			nfds = n;
     88 			pollfds = ccalloc(nfds, sizeof *pollfds);
     89 			pollfds[0].fd = fileno(stdin);
     90 			pollfds[0].events = POLLIN;
     91 		}
     92 		err = snd_mixer_poll_descriptors(mixer, &pollfds[1], nfds - 1);
     93 		if (err < 0)
     94 			fatal_alsa_error("cannot get poll descriptors", err);
     95 		n = poll(pollfds, nfds, -1);
     96 		if (n < 0) {
     97 			if (errno == EINTR) {
     98 				pollfds[0].revents = 0;
     99 				doupdate(); /* handle SIGWINCH */
    100 			} else {
    101 				fatal_error("poll error");
    102 			}
    103 		}
    104 		if (pollfds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
    105 			break;
    106 		if (pollfds[0].revents & POLLIN)
    107 			--n;
    108 		if (n > 0) {
    109 			err = snd_mixer_poll_descriptors_revents(mixer, &pollfds[1], nfds - 1, &revents);
    110 			if (err < 0)
    111 				fatal_alsa_error("cannot get poll events", err);
    112 			if (revents & (POLLERR | POLLNVAL))
    113 				close_mixer_device();
    114 			else if (revents & POLLIN)
    115 				snd_mixer_handle_events(mixer);
    116 		}
    117 		key = wgetch(active_widget->window);
    118 		while (key != ERR) {
    119 #ifdef KEY_RESIZE
    120 			if (key == KEY_RESIZE)
    121 				window_size_changed();
    122 			else
    123 #endif
    124 				active_widget->handle_key(key);
    125 			active_widget = get_active_widget();
    126 			if (!active_widget)
    127 				break;
    128 			key = wgetch(active_widget->window);
    129 		}
    130 		if (!active_widget)
    131 			break;
    132 		if (controls_changed) {
    133 			controls_changed = FALSE;
    134 			create_controls();
    135 			control_values_changed = FALSE;
    136 			display_controls();
    137 		} else if (control_values_changed) {
    138 			control_values_changed = FALSE;
    139 			display_controls();
    140 		}
    141 	}
    142 	free(pollfds);
    143 }