0

this is the code:

#include <curses.h>
int main() {
  initscr();
  curs_set(2);
  for(int i=0;i<COLS;i++){ //COLS NUMERO MASSIMO DI COLONNE
      mvaddch(0,i,'*');
      mvaddch(LINES-1,i,'*'); //LINES NUMERO MASSIMO RIGHE SE PARTO DALL'ULTIMO RIGA = (LINES -1)
      }
  refresh();
  endwin(); 
  return 0; 
}

All is ok but if i resize the window of cygwin,the screen become empty. I've cygwin with mintty.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
CHIRAQA
  • 33
  • 5

2 Answers2

1

The example is misleading:

  • if ncurses cleared the screen due to resizing, then you would have had a getch to keep the program from exiting (and catching the KEY_RESIZE),
  • but the example writes some characters on the screen and exits.

If your terminal (such as mintty) uses the xterm terminal description, it will temporarily switch to the alternate screen (where those characters get written), and when exiting ncurses (following the instructions in the terminal description) switches back to the normal screen (and those characters go away, leaving your screen blank.

The terminal capabilities which switch to/from the alternate screen are smcup and rmcup. You could use a different (or modified) terminal description. Or you could tell mintty to not switch to/from the alternate screen. According to its manual page it can do that in the settings dialog:

Alternate screen (NoAltScreen=false)
With this setting, the alternate screen can be disabled.

The manual indicates that these settings correspond to menu entries, which you may find simpler than editing the configuration file:

Mintty also adds a couple of items to the window menu, which can be accessed by clicking on the program icon or pressing Alt+Space.

Both menus have an entry that leads to the options dialog for changing mintty’s configuration.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • I have paste this NoAltScreen=false in the /mintty/config, ~/.minttyrc file but it doesn't work – CHIRAQA Nov 18 '17 at 23:08
0

ncurses gets the window size by three methods:

  • in terminals that support the TIOCGWINSZ ioctl(2), by a call to get the window dimensions at initscr(3) call.
  • through the info provided by the terminfo database. Many terminal types have a fixed size dimensions and the appropiate values for lines and columns is obtained from this database.
  • by getting the value of the LINES and COLUMNS environment variables.

Well, bash(1) normally adjusts those environment variable values to correct ones when a program exits and prior to calling the next, if this is available to bash(1), but if your program doesn't support the SIGWINCH signal (or does not handle it), most probably the ncurses library won't know that the windows size has changed during program run (by default I have not seen ncurses detecting this, anyway). Even in the case you run on a system that supports it, if you don't install a signal handler for that signal, ncurses library cannot afford by itself the window dimensions change, as it has to resize the stdscr contents and several criteria can be used to deal with old contents and new free space.

My observed criterion is that ncurses does not handle a window change by itself (at least by default, you have to consider also this is the same thing as what happens to ncurses that doesn't detect if another process has written to your tty, and the screen gets garbled). To handle this, you have to install a signal handler for the SIGWINCH signal (this signal is sent to the whole process group when a change in terminal dimensions has occurred, normally from the pty master side), and make the appropiate ioctl(2) to the tty device to get the new window dimensions and call the appropiate low level ncurses routines to change the new geometry of stdsrc and stdwin buffers, as that will need to reallocate memory for them. This means you are free to decide what happens to window contents on a terminal resize, but makes you to do all the work to deal with that. I have a program that, on a SIGWINCH just exec(2)s to itself and relaunchs itself, so it gets the new screen dimensions on the initscr(3) call. If you cannot auto-exec(2) your program, just dig in the documentation about how to reset the stdscr dimensions and to change the LINS and COLS global variables correctly. (If I find doc about how to do it, I'll edit this answer with the appropiate info.)

NOTE

I've run your program and it works as expected... filling the fist line and the last with asterisks. Just put a call to getch() before endwin() to be able to see the screen drawn.

Community
  • 1
  • 1
Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
  • Some indication about why the downvote would be polite. The question talks about window resize, and the program runs correct from beginning to end. Have I misunderstood something? – Luis Colorado Nov 20 '17 at 09:57