Unable to determine terminal size with ncurses

I am trying to process a resize signal (SIGWINCH)

void Server::resizeSignalHandler(int a)
{
signal(SIGWINCH, SIG_IGN);

endwin();
initscr();
refresh();
clear();

int x,y;
getmaxyx(stdscr, y, x);

wmove(upScreen, 0, 0);
wmove(downScreen, y/2, 0);
wresize(upScreen, y/2, x);
wresize(downScreen, y/2, x);
wclear(upScreen);
wclear(downScreen);
waddstr(upScreen, "test1");
waddstr(downScreen, "test2");
wrefresh(upScreen);
wrefresh(downScreen);
refresh();

signal(SIGWINCH, Server::resizeSignalHandler);

}
Server::Server()
{
//ncurses screen initialization
initscr();

if (!upScreen) {
    upScreen = newwin(0, 0, 1, 1);
}
if (!downScreen) {
    downScreen = newwin(0, 0, 1, 1);
}
//adjusting screen when user resize terminal
signal(SIGWINCH, Server::resizeSignalHandler);

//configuring screens
Server::resizeSignalHandler(0);

waddstr(Server::upScreen, "lalfasdfsafd as");
waddstr(downScreen, "supreme!");
wrefresh(Server::upScreen);
wrefresh(downScreen);
}

When I debugged this code, in resizeSignalHandler var x, y were always the same (the size did not change). I also tried sizing using ioctl, but nothing changed.

I realized that many people in front of me had this problem http://www.mail-archive.com/ arch@archlinux.org /msg11253.html Sometimes they solved it (changing / etc / profile; O (sic!)), But sometimes this is not so. Is there an alternative to mac os x terminal, maybe ncurses is dedicated for xterm and the like.

+5
source share
4 answers

mac, . , bash $LINES $COLUMNS, , . , , WINCH , . "/usr/X11/bin/resize"/"/usr/bin/resize" . , $LINES $COLUMNS.

-1

, , getmaxyx, , SIGWINCH. - :

old_callback = signal(SIGWINCH, Server::resizeSignalHandler);

resizeSignalHandler:

old_callback(a);
+1

getmaxyx(...) ( ) - SIGWINCH.

ioctl(fileno(stdout), TIOCGWINSZ, struct winsize*) . , getmaxyx(...).

getmaxyx(...), resize_term(size.ws_row, size.ws_col)

Thus, the signal handler code should look like this:

void on_terminal_resize(int n) {
    struct winsize size;

    if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
        resize_term(size.ws_row, size.ws_col);
    }
    // Your code goes here...
    signal(SIGWINCH, on_terminal_resize);
}
+1
source

I'm not sure, but I think you need to call ioctl(1, TIOCGWINSZ, struct winsize*)to get updated terminal settings. See man tty_ioctl.

0
source

All Articles