1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2018 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "signals.hxx"
22 #include "util/Compiler.h"
24 #include <glib-unix.h>
28 static int sigwinch_pipes[2];
31 handle_quit_signal(gpointer data)
33 auto *main_loop = (GMainLoop *)data;
35 g_main_loop_quit(main_loop);
40 sigwinch_event(gcc_unused GIOChannel *source,
41 gcc_unused GIOCondition condition, gpointer data)
43 auto &screen = *(ScreenManager *)data;
46 if (1 > read(sigwinch_pipes[0], ignoreme, 64))
57 catch_sigwinch(gcc_unused int sig)
59 if (1 != write(sigwinch_pipes[1], "", 1))
64 signals_init(GMainLoop *main_loop, ScreenManager &screen)
66 /* setup quit signals */
67 g_unix_signal_add(SIGTERM, handle_quit_signal, main_loop);
68 g_unix_signal_add(SIGINT, handle_quit_signal, main_loop);
69 g_unix_signal_add(SIGHUP, handle_quit_signal, main_loop);
71 /* setup signal behavior - SIGCONT */
74 sigemptyset(&act.sa_mask);
77 act.sa_handler = catch_sigwinch;
78 if (sigaction(SIGCONT, &act, nullptr) < 0) {
79 perror("sigaction(SIGCONT)");
85 act.sa_flags = SA_RESTART;
86 act.sa_handler = catch_sigwinch;
87 if (sigaction(SIGWINCH, &act, nullptr) < 0) {
88 perror("sigaction(SIGWINCH)");
93 if (!pipe(sigwinch_pipes) &&
94 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
95 GIOChannel *sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
96 g_io_add_watch(sigwinch_channel, G_IO_IN,
97 sigwinch_event, &screen);
98 g_io_channel_unref(sigwinch_channel);
101 perror("sigwinch pipe creation failed");
108 act.sa_handler = SIG_IGN;
109 if (sigaction(SIGPIPE, &act, nullptr) < 0) {
110 perror("sigaction(SIGPIPE)");
118 close(sigwinch_pipes[0]);
119 close(sigwinch_pipes[1]);