0

I am using ncurses to build my own terminal. My main window contains 5 other windows. The window which I have positioned below, is the prompt panel. I would like text to be entered there and as well I want the ability to input text in there. I have positioned the cursor using wmove but the cursor remains at the top left part of the window. As well, when I enter text, everything shifts.

This is part of my code;

WINDOW *mainwin, *date_time, *alarm, *colour, *output, *prompt_win;

/* Initialize ncurses */
mainwin = initscr();
if (!mainwin) {
    fprintf(stderr, "Error initialising ncurses.\n");
    exit(EXIT_FAILURE);
}

/* Make our child window, and add a border and some text to it. */
date_time = subwin(mainwin, 5, 35, 0, 0);
box(date_time, 0, 0);

alarm = subwin(mainwin, 5, 35, 0, 35);
box(alarm, 0, 0);
colour = subwin(mainwin, 5, 5, 0, 70);
box(colour, 0, 0);
output = subwin(mainwin, 10, 75, 5, 0);
box(output, 0, 0);
prompt_win = subwin(mainwin, 7, 75, 15, 0);
box(prompt_win, 0, 0);

pid_t pid = fork();

if (pid < 0) {
    perror("Error: Fork Failed");
} else if (pid == 0) {
    /* Alarm & date */
    pid_t child_pid = getpid();
    pid_t pid2 = fork();

    if (pid2 < 0) {
        perror("Error: Fork Failed");
    } else if (pid2 == 0) {
        /* Date */
        pid_t child_pid2 = getpid();

        while (1) {
            wnoutrefresh(date_time);
            getTime(date_time);
            /* doupdate() */
        }
    } else {
        /* Alarm */
        wnoutrefresh(alarm);
        wnoutrefresh(colour);
        /* doupdate() */
    }

    /* _exit(0); */
} else {
    /* Output & prompt */
    /* Display and enter text */

    /* prints "OK>" */
    mvwprintw(prompt_win,curse_loc,1,prompt_arr[0]);
    wnoutrefresh(prompt_win);

    /* gets input */
    getstr(buffer[curse_loc]);
    strcpy(current_cmd,buffer[curse_loc]);
    refresh();

    while (strcmp(current_cmd,"exit")) {
        wmove(prompt_win, curse_loc, sizeof(prompt_arr));
        refresh();
        mvwin(prompt_win, curse_loc, curse_loc);
        wnoutrefresh(prompt_win);
        curse_loc++;
        /* again displays "OK>" */
        mvwprintw(prompt_win, curse_loc, 1, prompt_arr[0]);
        /* gets input */
        getstr(buffer[curse_loc]);
        strcpy(current_cmd, buffer[curse_loc]);
        doupdate();
    }
    endwin();
    delwin(prompt_win);
    delwin(output);
    delwin(colour);
    delwin(alarm);
    delwin(date_time);
    delwin(mainwin);
    refresh();
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
lfarr
  • 17
  • 6

1 Answers1

1

There are a few problems:

  • the only call to read characters (from the keyboard) is getstr, which (using stdscr as its window) stops, with the cursor at the current (e.g., wmove) position in stdscr. If you want the cursor to stop in another window, you should use wgetstr, passing that window pointer as a parameter.
  • the program is attempting to write to the screen from two different processes. Since the two processes do not know what changes the other has done (nor what the other sent to the screen), the results will be unpredictable.
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • in the case I want the user to keep entering stuff in the prompt, while the time is infinitely updating. At the moment the system wont allow the user to enter cause the cursor has moved at the top. How can I fix this error. I have tried using mvwprintw and for that reason I think the cursor is getting mixed up. – lfarr Apr 29 '17 at 05:34