I am writing a program to display a database to the screen(xterm) and want to allow the window resize signal to increase/decrease the amount data that is displayed. I have a signal handler function for catching the SIGWINCH event and calling a function to reallocate the space for the windows (stdscr and curscr) an reallocate the space for the windows.
The resize function looks something like:
//GLOBAL CONSTANTS, initialized elsewhere to the correct values
WINDOW* display;
int maxX;
int maxY;
// resize event function
void resizeHandler() {
WINDOW*curscrOld = curscr;
WINDOW*stdscrOld = stdscr;
endwin();
delwin(curscrOld);
delwin(stdscrOld);
display = initscr();
getmaxyx(display, maxY, maxX);
signal(SIGWINCH, resizeHandler);
}
The problem with the reallocation function seems to be that I put in another call to initscr(). Not every time, but frequently enough to be annoying, the program will have a segmentation fault during the call to initscr() when a resize event occurs. The reason I put in another call to initscr() was getmaxyx() was not updating the size of the window on a resize event. I tried to just use the endwin() and refresh() functions to reinitialize the values returned from getmaxyx(), with no luck.
Inorder to try and get around this error I have been working to remove the additional call to initscr() by deleting and then creating both stdscr and curscr WINDOWS. I managed to find out about the ioctl() function that returns the lines and columns of the term window so that I know what size I want the new windows to be. I have been having limited sucess with this approach. After I create the new windows my first call to refresh() results in yet another segmentation fault. The new code looks something like this:
//GLOBAL CONSTANTS, initialized elsewhere to the correct values
WINDOW* display;
int maxX;
int maxY;
// resize event function
void resizeHandler() {
struct winsize disp;
endwin();
delwin(curscr);
delwin(stdscr);
ioctl(STDIN_FILENO, TIOCGWINSZ, &disp)
LINES = disp.ws_lines;
COLS = disp.ws_cols;
newwin(curscr, 0, 0, 0, 0);
newwin(stdscr, 0, 0, 0, 0);
werase(curscr);
wrefresh(curscr);
wmove(curscr, 0, 0);
werase(stdscr);
wrefresh(stdscr);
wmove(stdscr, 0, 0);
signal(SIGWINCH, resizeHandler);
}
I have tried it with and without resetting the curscr variable, which seems to make no difference. Thanks in advance for your help.