【问题标题】:Ncurses program exits when terminal resizedNcurses 程序在终端调整大小时退出
【发布时间】:2013-03-16 14:51:13
【问题描述】:

当我调整终端窗口的大小时,以下程序会退出。为什么以及如何阻止它?

#include <ncurses.h>
#include <unistd.h>

int main () {
    initscr ();

    printw ("Some text\n");
    refresh ();

    sleep (100);
    endwin ();

    return 0;
}

【问题讨论】:

标签: c linux ncurses


【解决方案1】:

我找到了答案here

当终端调整大小时,SIGWINCH 信号会引发并且程序退出。

解决办法如下:

#include <ncurses.h>
#include <unistd.h>
#include <signal.h>

int main () {
    initscr ();

    signal (SIGWINCH, NULL);

    printw ("Some text\n");
    refresh ();

    sleep (100);
    endwin ();

    return 0;
}

【讨论】:

  • 但是对于 ncurses 程序,您需要实际处理信号以使您的布局适应新的终端尺寸。
  • 写在answer
【解决方案2】:

您需要处理 SIGWINCH 信号:

#include <signal.h>

/* resizer handler, called when the user resizes the window */
void resizeHandler(int sig) {
    // update layout, do stuff...
}

int main(int argc, char **argv) {
    signal(SIGWINCH, resizeHandler);

    // play with ncurses
    // ...
}

【讨论】:

  • 我只会从信号处理程序中调用异步信号安全函数。阅读signal(7) 手册页。通常的方法是只在信号处理程序中设置一个volatile sigatomic_t 标志,并在其他地方测试该标志......
猜你喜欢
  • 2013-10-21
  • 2016-03-21
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 2017-11-06
相关资源
最近更新 更多