中断(n)干扰输入信号 [英] Interrupt (n)curses getch on incoming signal

查看:121
本文介绍了中断(n)干扰输入信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个程序使用ncurses绘制一个小表针.我的目标之一是使其更易于移植到其他curses实现中.这意味着我想自己在大小调整操作上捕获由终端仿真器发出的SIGWINCH,并更新我的tui以坚持更改的几何形状(而不依赖于ncurses的大小调整功能).由于POSIX(据我所知)仅允许访问信号处理程序中的sig_atomic_t变量,因此我将其设置为另一种状态.在主循环中,我的程序检查状态是否已更改,并在必要时更新tui.

One of my programs uses ncurses for drawing a small tui. One of my goals is to make it rather portable to other curses implementations. This means that I want to catch a SIGWINCH issued by the terminal emulator on a resize operation myself and update my tui to adhere the changed geometry (and not depend on the resizing facilities of ncurses). Since POSIX (as far as I know) only allows access to sig_atomic_t variables within the signal handler, I set one to a different state. In the main loop, my program checks whether the state has changed and updates the tui if necessary.

但是现在,我有一个问题,当信号到达时,我的程序挂在getch中. ncurses文档指出,已处理信号永远不会中断它.这意味着在按下输入键之前,不会更改tui的大小.

But now, I have the problem that my program hangs in getch, when an signal arrives. The ncurses documentation states that handled signals never interrupt it. This means the size of the tui is not updated until an input key is pressed.

是否有任何可移植的方式来中断getch?我当前的方法是在信号处理程序中ungetch一个虚拟密钥,但是我不确定是否允许这样做.实际上,我找不到有关是否可以在信号处理程序中使用curses函数的事实的任何文档.知道如何正确处理此事吗?

Is there any portable way to interrupt getch? My current approach is to ungetch a dummy key in the signal handler but I'm not sure if this is allowed. Actually I could not find any documentation regarding the fact whether an curses function may be used in a signal handler or not. Any idea how to correctly handle this matter?

致谢

推荐答案

来自FreeBSD文档获取,中断获取取决于您使用的系统:

From the FreeBSD documentation getch, interrupt getch depends of the system you are using:

关注便携性的程序员应为其中的任何一个做好准备 在两种情况下:(a)信号接收不会中断获取; (b)信号 收据会中断getch,并使它返回errno置为ERR 到EINTR.在ncurses实施下,处理信号 永远不要打断getch.

Programmers concerned about portability should be prepared for either of two cases: (a) signal receipt does not interrupt getch; (b) signal receipt interrupts getch and causes it to return ERR with errno set to EINTR. Under the ncurses implementation, handled signals never interrupt getch.

我认为您应该考虑使用线程,一个线程负责使用getch()并将数据传递给处理它们的主线程.发送信号SIGWINCH时,您将能够杀死使用getch()的线程,并在需要时重新启动它.但是杀死使用getch()的线程可能没用,因为主线程没有被getch()阻塞.

I think you should consider using threads, one thread charged to use getch() and to pass datas to the main thread that treat them. You will be able to kill the thread that uses getch() when the signal SIGWINCH is sent, and to relauch it if you want. But killing the thread that uses getch() maybe useless, because the main thread isn't blocked by getch().

You can get non-blocking inputs without ncurses, as described on this page. But you can use read(STDIN_FILENO, &c, sizeof(char)) to read the input instead of fgetc() in the example, because read return a value <= 0 if it has failed.

这篇关于中断(n)干扰输入信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆