The C++ Linux code below tries to write the row number on each line of /dev/tty1 using ncurses. When executed from e.g. /dev/tty2, the output is correct and each line of tty1 is written.
stderr output:
Window size of std in:200 x 75
Window size of opened terminal: 200 x 75
Ncurses cols x lines: 200 x 75
When executed from a remote terminal (ssh), the stdin terminal size differs, but ncurses configures itself correctly and the output is correct:
Window size of std in:80 x 24
Window size of opened terminal: 200 x 75
Ncurses cols x lines: 200 x 75
When executed in gdb from an ssh terminal (we discovered the issue with Visual Studio remote debugging, but it works also with generic ssh connections) ncurses fails to configure itself correctly.
Window size of std in:80 x 24
Window size of opened terminal: 200 x 75
Ncurses cols x lines: 80 x 24
Without the resizeterm call, tty1 shows the numbers 0-23, the other lines are blank.
With the resizeterm call, tty1 shows the numbers 51-74 on the top 24 rows of the screen, the other numbers appear to have scrolled out of sight, and the other lines are blank. I.e. Ncurses internally reconfigured itself to use the new sizes, but the output is still mapped on the original (smaller) section of the tty.
Question: How can I initialize ncurses such that it uses the correct terminal size while debugging my application with gdb from a remote ssh connection ?
#include <cstdio>
#include <curses.h>
#include <cstdint>
#include <unistd.h>
#include <sys/ioctl.h>
#include <iostream>
int main()
{
FILE *term = fopen("/dev/tty1", "r+");
fputs("\033c", term);
struct winsize appTermSize, openedTermSize;
ioctl(0, TIOCGWINSZ, &appTermSize); // Window size of calling terminal
std::cerr << "Window size of std in:" << appTermSize.ws_col << " x " << appTermSize.ws_row << std::endl;
ioctl(fileno(term), TIOCGWINSZ, &openedTermSize); // Actual terminal size, the size ncurses should use.
std::cerr << "Window size of opened terminal: " << openedTermSize.ws_col << " x " << openedTermSize.ws_row << std::endl;
auto screen = newterm("linux", term, term);
std::cerr << "Ncurses cols x lines: " << COLS << " x " << LINES << std::endl;
set_term(screen);
resizeterm(openedTermSize.ws_row, openedTermSize.ws_col);
clear();
noecho();
curs_set(0);
for (uint32_t y = 0; y <= openedTermSize.ws_col; y++)
{
mvaddch(y, y, (y/10)+'0');
mvaddch(y, y+1, (y%10)+'0');
}
refresh();
while ('x' != getch());
endwin();
delscreen (screen);
}