int main(int argc, char *argv[]) { initscr(); noecho(); cbreak(); keypad(stdscr,TRUE); curs_set(FALSE); install_handler( SIGHUP ); install_handler( SIGUSR1 ); // set up initial windows WINDOW* display = newwin(1, 1, 0, 0 ); WINDOW* edit = newwin(1,1, 0, 0 ); int dispheight = size_display( display, edit ); int d = 0; char buf[BUFLEN]; int ch; while((ch = getch()) != KEY_F(1)) { switch (ch) { case KEY_RESIZE: dispheight = size_display( display, edit ); d = 0; strncpy( buf, "KEY_RESIZE", BUFLEN ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; if( sighup_received ) { snprintf( buf, BUFLEN, "Received SIGHUP %lu", (unsigned long)time(NULL) ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; sighup_received = 0; } if( sigusr1_received ) { snprintf( buf, BUFLEN, "Received SIGUSR1 %lu", (unsigned long)time(NULL) ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; sigusr1_received = 0; } wrefresh(display); break; case KEY_LEFT: strncpy( buf, "KEY_LEFT", BUFLEN ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; wrefresh(display); break; case KEY_F(2): strncpy( buf, "KEY_F(2)", BUFLEN ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; wrefresh(display); break; default : if ( isprint(ch) ) { snprintf( buf, BUFLEN, "%c", ch ); mvwprintw( edit, 1, 2, buf ); wrefresh(edit); } else { snprintf( buf, BUFLEN, "Unprintable %04x", ch ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; wrefresh(display); } break; } } // close curses lib, reset terminal endwin(); return 0; }
int main(int argc, char *argv[]) { initscr(); noecho(); cbreak(); keypad(stdscr,TRUE); // For arrows and enter/backspace for menu navigation and input curs_set(FALSE); install_handler( SIGHUP ); install_handler( SIGUSR1 ); // set up initial windows WINDOW* display = newwin(1, 1, 0, 0 ); WINDOW* edit = newwin(1,1, 0, 0 ); int dispheight = size_display( display, edit ); int d = 0; char buf[BUFLEN]; char inputstring[BUFLEN]; int ch; int highlight = 1; int choice = 0; //mvwprintw(display, 1, 2, "Use arrow keys to go up and down, Press enter to select a choice"); print_menu( display, highlight ); // wrefresh(display); while((ch = getch()) != KEY_F(10)) { switch (ch) { case KEY_RESIZE: dispheight = size_display( display, edit ); d = 0; strncpy( buf, "KEY_RESIZE", BUFLEN ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; if( sighup_received ) { snprintf( buf, BUFLEN, "Received SIGHUP %lu", (unsigned long)time(NULL) ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; sighup_received = 0; } if( sigusr1_received ) { snprintf( buf, BUFLEN, "Received SIGUSR1 %lu", (unsigned long)time(NULL) ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; sigusr1_received = 0; } wrefresh(display); break; case KEY_UP: if(highlight == 1) highlight = n_choices; else --highlight; strncpy( buf, "KEY_UP", BUFLEN ); mvwprintw( edit, 1, 2, buf ); wrefresh(edit); break; // mvwprintw( display, d++ + 2, 2, buf ); // d = d % dispheight; break; case KEY_DOWN: if(highlight == n_choices) highlight = 1; else ++highlight; strncpy( buf, "KEY_DOWN", BUFLEN ); mvwprintw( edit, 1, 2, buf ); wrefresh(edit); break; // mvwprintw( display, d++ + 2, 2, buf ); // d = d % dispheight; // wrefresh(display); // break; case 10: choice = highlight; strncpy( buf, "KEY_ENTER", BUFLEN ); mvwprintw( edit, 1, 2, buf ); wrefresh(edit); break; // mvwprintw( display, d++ + 2, 2, buf ); // d = d % dispheight; // wrefresh(display); // break; case KEY_BACKSPACE: // choice = highlight; strncpy( buf, "KEY_BACKSPACE", BUFLEN ); mvwprintw( edit, 1, 2, buf ); wrefresh(edit); break; default : if ( isprint(ch) ) { snprintf( buf, BUFLEN, "%c", ch ); clear_line( edit, 1 ); mvwprintw( edit, 1, 2, buf ); wrefresh(edit); } else { snprintf( buf, BUFLEN, "Unprintable %04x", ch ); mvwprintw( display, d++ + 2, 2, buf ); d = d % dispheight; wrefresh(display); } break; } //print_menu( display, highlight ); if( choice != 0 && choice != n_choices ) { dispheight = size_display( display, edit ); //clear_line( display, 8 ); //snprintf( buf, BUFLEN, "You chose choice %d with choice string %s", choice, choices[choice-1] ); strncpy( buf, "Enter a string", BUFLEN ); mvwprintw( display, 2, 2, buf ); wrefresh( display ); get_string_input( edit, inputstring ); if( strlen( inputstring ) == 0 ) strncpy( buf, "You didn't type anything", BUFLEN ); else snprintf( buf, BUFLEN, "You typed string: %s", inputstring ); mvwprintw( display, 3, 2, buf ); wrefresh( display ); getch(); choice = 0; } print_menu( display, highlight ); } // close curses lib, reset terminal endwin(); return 0; }