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>
27 #include <signal.h>
29 static int sigwinch_pipes[2];
31 static gboolean
32 handle_quit_signal(gpointer data)
33 {
34 GMainLoop *main_loop = data;
36 g_main_loop_quit(main_loop);
37 return false;
38 }
40 static gboolean
41 sigwinch_event(gcc_unused GIOChannel *source,
42 gcc_unused GIOCondition condition, gpointer data)
43 {
44 struct mpdclient *c = data;
46 char ignoreme[64];
47 if (1 > read(sigwinch_pipes[0], ignoreme, 64))
48 exit(EXIT_FAILURE);
50 endwin();
51 refresh();
52 screen_resize(c);
54 return TRUE;
55 }
57 static void
58 catch_sigwinch(gcc_unused int sig)
59 {
60 if (1 != write(sigwinch_pipes[1], "", 1))
61 exit(EXIT_FAILURE);
62 }
64 void
65 signals_init(GMainLoop *main_loop, struct mpdclient *c)
66 {
67 /* setup quit signals */
68 g_unix_signal_add(SIGTERM, handle_quit_signal, main_loop);
69 g_unix_signal_add(SIGINT, handle_quit_signal, main_loop);
70 g_unix_signal_add(SIGHUP, handle_quit_signal, main_loop);
72 /* setup signal behavior - SIGCONT */
74 struct sigaction act;
75 sigemptyset(&act.sa_mask);
76 act.sa_flags = 0;
78 act.sa_handler = catch_sigwinch;
79 if (sigaction(SIGCONT, &act, NULL) < 0) {
80 perror("sigaction(SIGCONT)");
81 exit(EXIT_FAILURE);
82 }
84 /* setup SIGWINCH */
86 act.sa_flags = SA_RESTART;
87 act.sa_handler = catch_sigwinch;
88 if (sigaction(SIGWINCH, &act, NULL) < 0) {
89 perror("sigaction(SIGWINCH)");
90 exit(EXIT_FAILURE);
91 }
93 #ifndef WIN32
94 if (!pipe(sigwinch_pipes) &&
95 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
96 GIOChannel *sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
97 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, c);
98 g_io_channel_unref(sigwinch_channel);
99 }
100 else {
101 perror("sigwinch pipe creation failed");
102 exit(EXIT_FAILURE);
103 }
104 #endif
106 /* ignore SIGPIPE */
108 act.sa_handler = SIG_IGN;
109 if (sigaction(SIGPIPE, &act, NULL) < 0) {
110 perror("sigaction(SIGPIPE)");
111 exit(EXIT_FAILURE);
112 }
113 }
115 void
116 signals_deinit(void)
117 {
118 close(sigwinch_pipes[0]);
119 close(sigwinch_pipes[1]);
120 }