0

I don't understand why the arrow keys code changes after forking in a WINDOW. The up arrow returns 259, but after the fork 65. If I run the same program on stdscr, it returns 65 already at the beginning. Thanks for the help and sorry for the english (translated by Google).

`

#include <curses.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

void openVim() {
pid_t pid = fork();
    if (pid < 0) {}
    else if (pid == 0) {
        execl("/usr/bin/vim", "/usr/bin/vim", NULL);
        exit(0);
    }
    else {
        wait(NULL);
    }

}

int main() {
    initscr();
    noecho();
    int ch = 0;
    WINDOW* mainWin = newwin(10,10,0,0);
    keypad(mainWin, TRUE);
    while ((ch = wgetch(mainWin)) != 'q') {
        wclear(mainWin);
        if (ch == 'V') openVim();
        else
        mvwprintw(mainWin, 0, 0, "%i - %c", ch, ch);
        wrefresh(mainWin);
    }
    delwin(mainWin);
    endwin();
    return 0;
}

`

I noticed that if I put a simple for loop in the fork, it doesn't happen. It probably has to do with execl?

1 Answers1

0

vim resets the terminal I/O mode; your program doesn't account for that (see reset_prog_mode and reset_shell_mode).

The NCURSES Programming HOWTO section Temporarily Leaving Curses mode also discusses this issue.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Thanks, it works! I'm starting out with c++ and ncurses and I missed that. If anyone else has the same problem, the solution is here: https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/misc.html – Iugin74 Dec 03 '22 at 10:40
  • ...which reminded me - [see link](https://invisible-island.net/ncurses/howto/NCURSES-Programming-HOWTO.html#TEMPLEAVE). But if the answer works for you, accepting it works for me. – Thomas Dickey Dec 04 '22 at 00:19