Thread: Ncurses: how can I exit without pressing key?

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    15

    Ncurses: how can I exit without pressing key?

    I'm trying to get my program to exit when ctrl-c is pressed or the window close button is activated, but before the program exits, it needs to run a function. In the original program, it's a function to save a bookmark, so the user can just quit, and the position will be saved.

    It works, but with one problem: it needs additional mouse or keyboard input to get past the ch function and exit the loop. I would like the program to just exit without the need for further interaction.

    Is there away to do this?

    exitnow.c:
    Code:
    #include <ncurses.h>
    #include <assert.h>
    
    #include <stdbool.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    
    // gcc exitnow.c -o exitnow -lncursesw && ./exitnow
    
    
    static bool shutdownNow = false;
    
    
    void handler(int num) {
    
    
       write(STDOUT_FILENO, "\n\rCall to exit. Bye.", 20);
    
    
       shutdownNow = true;
    }
    
    
    void IneedToBeCalledOnExit(int i) {
    
    
       printf("\nThanks. Did my last task.\n");
    }
    
    
    int main (int argc, char* argv[]){
    
    
       signal(SIGINT, handler);
       signal(SIGKILL, handler);
       signal(SIGTERM, handler);
    
    
       MEVENT event;
    
    
       initscr();
       raw();
       keypad(stdscr, TRUE);
       noecho();
       clear();
       cbreak();
       mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
       mouseinterval(0);
    
    
       int ch, i = 0;
    
    
       do {
    
    
          mvprintw(0,0, "Blah, blah. Stuff happening here: %i\nPress ctrl-c to exit", i);
          
          i++;
    
    
          ch = getch(); // We're stuck here 
    
    
       } while (shutdownNow == false); 
    
    
       refresh();
       endwin();
    
    
       // RUN ME BEFORE YOU EXIT!
       IneedToBeCalledOnExit(i);
    
    
       return 0;
    }
    Any ideas?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    15
    *** SOLVED ***

    Just putting a simple timeout(3000) right before the ch = getch() seems to have solved it.

    From timeout(3): curses input options - Linux man page:

    The timeout and wtimeout routines set blocking or non-blocking read for a given window. If delay is negative, blocking read is used (i.e., waits indefinitely for input). If delay is zero, then non-blocking read is used (i.e., read returns ERR if no input is waiting). If delay is positive, then read blocks for delay milliseconds, and returns ERR if there is still no input. Hence, these routines provide the same functionality as nodelay, plus the additional capability of being able to block for only delay milliseconds (where delay is positive).


    The curses library does ''line-breakout optimization'' by looking for typeahead periodically while updating the screen. If input is found, and it is coming from a tty, the current update is postponed until refresh or doupdate is called again. This allows faster response to commands typed in advance. Normally, the input FILE pointer passed to newterm, or stdin in the case that initscr was used, will be used to do this typeahead checking. The typeahead routine specifies that the file descriptor fd is to be used to check for typeahead instead. If fd is -1, then no typeahead checking is done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pressing a key to display a new bmp
    By batman123 in forum Game Programming
    Replies: 2
    Last Post: 01-10-2005, 05:08 PM
  2. Pressing A Button on Dos..
    By yakabod in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-30-2003, 07:46 AM
  3. GetAsyncKeyState() (Key pressing)
    By Xterria in forum Game Programming
    Replies: 4
    Last Post: 07-04-2002, 09:36 AM
  4. How to tell when pressing escape?
    By Shadow12345 in forum C++ Programming
    Replies: 16
    Last Post: 05-25-2002, 07:24 AM
  5. Pressing a button in another app
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2002, 12:53 PM

Tags for this Thread