简体   繁体   English

为什么每次我调整终端大小时函数tgetnum(“ co”)都不更新?

[英]Why the function tgetnum(“co”) does not update every time I resize the terminal?

I have a global variable called g_data that holds a pointer to a structure which contains, among many, two variables to hold the height and width of the terminal. 我有一个名为g_data的全局变量,该变量持有一个指向结构的指针,该结构在许多变量中包含两个变量,以容纳终端的​​高度和宽度。 The function signalhandler below checks whether the terminal has been resized. 下面的功能信号处理程序检查终端是否已调整大小。 In that case, the function update_data changes the values of those two variables to the current height and width of the terminal. 在那种情况下,函数update_data将这两个变量的值更改为终端的当前高度和宽度。

My problem is that the function tgetnum, whenever it's being called, it doesn't seem to get the current terminal size (after the resizing). 我的问题是,无论何时调用函数tgetnum,它似乎都不会获得当前的终端大小(在调整大小之后)。

I'm using Ubuntu 18.04LTS 我正在使用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);
    }
}

Looking up tgetnum in Linux man pages, it says: 在Linux手册页中查找tgetnum ,它说:

These routines are included as a conversion aid for programs that use the termcap library. 这些例程作为使用termcap库的程序的转换帮助而包括在内。 Their parameters are the same and the routines are emulated using the terminfo database. 它们的参数相同,并且使用terminfo数据库模拟例程。 Thus, they can only be used to query the capabilities of entries for which a terminfo entry has been compiled. 因此,它们只能用于查询已为其编译terminfo条目的条目的功能。

Looking up terminfo in man, it says: 在人中查找terminfo时,它说:

Terminfo is a data base describing terminals, used by screen-oriented programs... Terminfo是一个描述终端的数据库,供面向屏幕的程序使用...

Because it is a database, there is no dynamic updating. 因为它是数据库,所以没有动态更新。 It only gets the statically defined information from the database for your current terminal. 它仅从数据库中获取当前终端的静态定义的信息。


Update: 更新:

Googling around I found http://man7.org/tlpi/code/online/dist/tty/demo_SIGWINCH.c.html which sets a SIGWINCH handler and then uses ioctl to get the updated size, roughly: 到处搜寻,我发现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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM