curses: fixup SIGWINCH handler mess

Don't run code in the signal handler, only set a flag.
Use sigaction(2) to avoid non-portable signal(2) semantics.
Make #ifdefs less messy.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Message-id: 1385130903-20531-1-git-send-email-kraxel@redhat.com
Signed-off-by: Anthony Liguori <aliguori@amazon.com>
This commit is contained in:
Gerd Hoffmann 2013-11-22 15:35:03 +01:00 committed by Anthony Liguori
parent 16f4e8fa73
commit 032ac6f8bf

View File

@ -106,9 +106,9 @@ static void curses_resize(DisplayChangeListener *dcl,
curses_calc_pad(); curses_calc_pad();
} }
#ifndef _WIN32 #if !defined(_WIN32) && defined(SIGWINCH) && defined(KEY_RESIZE)
#if defined(SIGWINCH) && defined(KEY_RESIZE) static volatile sig_atomic_t got_sigwinch;
static void curses_winch_handler(int signum) static void curses_winch_check(void)
{ {
struct winsize { struct winsize {
unsigned short ws_row; unsigned short ws_row;
@ -117,18 +117,34 @@ static void curses_winch_handler(int signum)
unsigned short ws_ypixel; /* unused */ unsigned short ws_ypixel; /* unused */
} ws; } ws;
/* terminal size changed */ if (!got_sigwinch) {
if (ioctl(1, TIOCGWINSZ, &ws) == -1)
return; return;
}
got_sigwinch = false;
if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
return;
}
resize_term(ws.ws_row, ws.ws_col); resize_term(ws.ws_row, ws.ws_col);
curses_calc_pad();
invalidate = 1; invalidate = 1;
/* some systems require this */
signal(SIGWINCH, curses_winch_handler);
} }
#endif
static void curses_winch_handler(int signum)
{
got_sigwinch = true;
}
static void curses_winch_init(void)
{
struct sigaction old, winch = {
.sa_handler = curses_winch_handler,
};
sigaction(SIGWINCH, &winch, &old);
}
#else
static void curses_winch_check(void) {}
static void curses_winch_init(void) {}
#endif #endif
static void curses_cursor_position(DisplayChangeListener *dcl, static void curses_cursor_position(DisplayChangeListener *dcl,
@ -163,6 +179,8 @@ static void curses_refresh(DisplayChangeListener *dcl)
{ {
int chr, nextchr, keysym, keycode, keycode_alt; int chr, nextchr, keysym, keycode, keycode_alt;
curses_winch_check();
if (invalidate) { if (invalidate) {
clear(); clear();
refresh(); refresh();
@ -349,13 +367,7 @@ void curses_display_init(DisplayState *ds, int full_screen)
curses_keyboard_setup(); curses_keyboard_setup();
atexit(curses_atexit); atexit(curses_atexit);
#ifndef _WIN32 curses_winch_init();
#if defined(SIGWINCH) && defined(KEY_RESIZE)
/* some curses implementations provide a handler, but we
* want to be sure this is handled regardless of the library */
signal(SIGWINCH, curses_winch_handler);
#endif
#endif
dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener)); dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
dcl->ops = &dcl_ops; dcl->ops = &dcl_ops;