Code

cc24e7d9748cb56b2d396edb3abba0a351b9f211
[ncmpc.git] / src / signals.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
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 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.
18  */
20 #include "signals.h"
21 #include "screen.h"
22 #include "Compiler.h"
24 #include <glib-unix.h>
26 #include <stdbool.h>
28 static int sigwinch_pipes[2];
30 static gboolean
31 handle_quit_signal(gpointer data)
32 {
33         GMainLoop *main_loop = data;
35         g_main_loop_quit(main_loop);
36         return false;
37 }
39 static gboolean
40 sigwinch_event(gcc_unused GIOChannel *source,
41                gcc_unused GIOCondition condition, gpointer data)
42 {
43         struct mpdclient *c = data;
45         char ignoreme[64];
46         if (1 > read(sigwinch_pipes[0], ignoreme, 64))
47                 exit(EXIT_FAILURE);
49         endwin();
50         refresh();
51         screen_resize(c);
53         return TRUE;
54 }
56 static void
57 catch_sigwinch(gcc_unused int sig)
58 {
59         if (1 != write(sigwinch_pipes[1], "", 1))
60                 exit(EXIT_FAILURE);
61 }
63 void
64 signals_init(GMainLoop *main_loop, struct mpdclient *c)
65 {
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 */
73         struct sigaction act;
74         sigemptyset(&act.sa_mask);
75         act.sa_flags = 0;
77         act.sa_handler = catch_sigwinch;
78         if (sigaction(SIGCONT, &act, NULL) < 0) {
79                 perror("sigaction(SIGCONT)");
80                 exit(EXIT_FAILURE);
81         }
83         /* setup SIGWINCH */
85         act.sa_flags = SA_RESTART;
86         act.sa_handler = catch_sigwinch;
87         if (sigaction(SIGWINCH, &act, NULL) < 0) {
88                 perror("sigaction(SIGWINCH)");
89                 exit(EXIT_FAILURE);
90         }
92 #ifndef WIN32
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, sigwinch_event, c);
97                 g_io_channel_unref(sigwinch_channel);
98         }
99         else {
100                 perror("sigwinch pipe creation failed");
101                 exit(EXIT_FAILURE);
102         }
103 #endif
105         /* ignore SIGPIPE */
107         act.sa_handler = SIG_IGN;
108         if (sigaction(SIGPIPE, &act, NULL) < 0) {
109                 perror("sigaction(SIGPIPE)");
110                 exit(EXIT_FAILURE);
111         }
114 void
115 signals_deinit(void)
117         close(sigwinch_pipes[0]);
118         close(sigwinch_pipes[1]);