From 8724a12dc1aec23a16bc3d95d5b64476bc2b4391 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 3 Dec 2012 18:50:44 +0100 Subject: [PATCH] NCurses: do not resize directly in signal handler, malloc may not be working there --- src/ncurses.cc | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/ncurses.cc b/src/ncurses.cc index 2f23229bb..253bcf7c7 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -75,20 +75,9 @@ static void set_color(Color fg_color, Color bg_color) } } -static NCursesUI* signal_ui = nullptr; void on_term_resize(int) { - if (not signal_ui) - return; - - int fd = open("/dev/tty", O_RDWR); - winsize ws; - if (fd == -1 or ioctl(fd, TIOCGWINSZ, (void*)&ws) != 0) - return; - close(fd); - resizeterm(ws.ws_row, ws.ws_col); ungetch(KEY_RESIZE); - signal_ui->update_dimensions(); EventManager::instance().force_signal(0); } @@ -115,19 +104,17 @@ NCursesUI::NCursesUI() m_menu_fg = get_color_pair(Color::Blue, Color::Cyan); m_menu_bg = get_color_pair(Color::Cyan, Color::Blue); - update_dimensions(); - - assert(signal_ui == nullptr); - signal_ui = this; signal(SIGWINCH, on_term_resize); signal(SIGINT, on_sigint); + + update_dimensions(); } NCursesUI::~NCursesUI() { endwin(); - assert(signal_ui == this); - signal_ui = nullptr; + signal(SIGWINCH, SIG_DFL); + signal(SIGINT, SIG_DFL); } static void redraw(WINDOW* menu_win) @@ -238,10 +225,10 @@ bool NCursesUI::is_key_available() Key NCursesUI::get_key() { - const unsigned c = getch(); + const int c = getch(); if (c > 0 and c < 27) { - return {Key::Modifiers::Control, c - 1 + 'a'}; + return {Key::Modifiers::Control, Codepoint(c) - 1 + 'a'}; } else if (c == 27) { @@ -253,6 +240,18 @@ Key NCursesUI::get_key() else return Key::Escape; } + else if (c == KEY_RESIZE) + { + int fd = open("/dev/tty", O_RDWR); + winsize ws; + if (fd != -1 and ioctl(fd, TIOCGWINSZ, (void*)&ws) == 0) + { + close(fd); + resizeterm(ws.ws_row, ws.ws_col); + update_dimensions(); + } + return Key::Invalid; + } else switch (c) { case KEY_BACKSPACE: return Key::Backspace; @@ -265,7 +264,7 @@ Key NCursesUI::get_key() case KEY_BTAB: return Key::BackTab; } - if (c < 256) + if (c >= 0 and c < 256) { ungetch(c); return utf8::codepoint(getch_iterator{});