[英]Why the function tgetnum(“co”) does not update every time I resize the terminal?
我有一個名為g_data的全局變量,該變量持有一個指向結構的指針,該結構在許多變量中包含兩個變量,以容納終端的高度和寬度。 下面的功能信號處理程序檢查終端是否已調整大小。 在那種情況下,函數update_data將這兩個變量的值更改為終端的當前高度和寬度。
我的問題是,無論何時調用函數tgetnum,它似乎都不會獲得當前的終端大小(在調整大小之后)。
我正在使用Ubuntu 18.04LTS
typedef struct s_data
{
t_lst *lst;
t_ldim ldim;
t_pos pos;
int height;
int width;
int max;
int lstsize;
} t_data;
int i = 0;
void signalhandler(int sig)
{
if (sig == SIGWINCH)
{
update_data(g_data);
if (g_data == NULL)
exit(EXIT_FAILURE);
enable_cap("ti");
print_args(g_data);
printf("%d - %d\n", i++, tgetnum("co"));
signal(SIGWINCH, signalhandler);
}
else if (sig == SIGTSTP)
{
signal(SIGTSTP, SIG_DFL);
modify_main_caps(SET);
enable_cap("te");
ioctl(0, TIOCSTI, "\032");
}
else if (sig == SIGCONT)
{
signal(SIGTSTP, signalhandler);
modify_main_caps(UNSET);
update_data(g_data);
enable_cap("ti");
print_args(g_data);
}
}
在Linux手冊頁中查找tgetnum
,它說:
這些例程作為使用termcap庫的程序的轉換幫助而包括在內。 它們的參數相同,並且使用terminfo數據庫模擬例程。 因此,它們只能用於查詢已為其編譯terminfo條目的條目的功能。
在人中查找terminfo時,它說:
Terminfo是一個描述終端的數據庫,供面向屏幕的程序使用...
因為它是數據庫,所以沒有動態更新。 它僅從數據庫中獲取當前終端的靜態定義的信息。
到處搜尋,我發現http://man7.org/tlpi/code/online/dist/tty/demo_SIGWINCH.c.html設置了SIGWINCH
處理程序,然后使用ioctl
大致獲取了更新的大小:
struct winsize ws; ioctl(STDIN_FILENO, TIOCGWINSZ, &ws); printf("Caught SIGWINCH, new window size: " "%d rows * %d columns\\n", ws.ws_row, ws.ws_col);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.