This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
programming:ncurses [2022/03/19 05:41] nanodano |
programming:ncurses [2022/03/19 23:56] (current) nanodano |
||
---|---|---|---|
Line 95: | Line 95: | ||
- | ===== Hello world example | + | ===== Examples |
+ | ==== Hello world ==== | ||
<code cpp hello_curses.cpp> | <code cpp hello_curses.cpp> | ||
- | // g++ hello_curses.cpp -lcurses | + | // g++ hello_curses.cpp -lncurses |
+ | // add -static for static binary | ||
#include < | #include < | ||
Line 108: | Line 110: | ||
</ | </ | ||
- | |||
- | <code cpp sigkill.cpp> | ||
- | // Allow CTRL-C to kill app | ||
- | |||
- | // Near beginning, link signal to callback | ||
- | signal(SIGINT, | ||
- | |||
- | static void finish(int sig) | ||
- | { | ||
- | endwin(); | ||
- | |||
- | /* do your non-curses wrapup here */ | ||
- | |||
- | exit(0); | ||
- | } | ||
- | </ | ||
<code cpp example.cpp> | <code cpp example.cpp> | ||
Line 190: | Line 176: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ==== Shelling out ==== | ||
+ | |||
+ | <code cpp shell_out.cpp> | ||
+ | // From https:// | ||
+ | addstr(" | ||
+ | def_prog_mode(); | ||
+ | endwin(); | ||
+ | system(" | ||
+ | addstr(" | ||
+ | refresh(); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Test mouse input ==== | ||
+ | |||
+ | <code cpp mouse_test.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | void redrawBorder() { | ||
+ | clear(); | ||
+ | box(stdscr, | ||
+ | |||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | initscr(); | ||
+ | mousemask(ALL_MOUSE_EVENTS, | ||
+ | | ||
+ | /** | ||
+ | | ||
+ | */ | ||
+ | if (has_colors()) { start_color(); | ||
+ | // | ||
+ | init_pair(1, | ||
+ | init_pair(2, | ||
+ | | ||
+ | cbreak(); | ||
+ | keypad(stdscr, | ||
+ | nonl(); | ||
+ | noecho(); | ||
+ | curs_set(0); | ||
+ | | ||
+ | attrset(COLOR_PAIR(1)); | ||
+ | redrawBorder(); | ||
+ | // | ||
+ | | ||
+ | | ||
+ | | ||
+ | // short id; /* ID to distinguish multiple devices */ | ||
+ | // int x, y, z; /* event coordinates */ | ||
+ | // mmask_t bstate; | ||
+ | |||
+ | while (true) { | ||
+ | int c = getch(); | ||
+ | switch (c) { | ||
+ | case ' | ||
+ | endwin(); | ||
+ | exit(0); | ||
+ | case KEY_MOUSE: | ||
+ | MEVENT event; | ||
+ | if (getmouse(& | ||
+ | clear(); | ||
+ | redrawBorder(); | ||
+ | |||
+ | attrset(COLOR_PAIR(2)); | ||
+ | mvaddstr(5, | ||
+ | mvaddstr(6, | ||
+ | mvaddstr(7, | ||
+ | addstr(std:: | ||
+ | if (event.bstate & BUTTON1_PRESSED) { | ||
+ | addstr(" | ||
+ | } else if (event.bstate & BUTTON1_DOUBLE_CLICKED) { | ||
+ | addstr(" | ||
+ | } | ||
+ | attrset(COLOR_PAIR(1)); | ||
+ | |||
+ | } | ||
+ | break; | ||
+ | |||
+ | /** | ||
+ | BUTTON1_PRESSED | ||
+ | BUTTON1_RELEASED | ||
+ | BUTTON1_CLICKED | ||
+ | BUTTON1_DOUBLE_CLICKED | ||
+ | BUTTON1_TRIPLE_CLICKED | ||
+ | BUTTON2_PRESSED | ||
+ | BUTTON2_RELEASED | ||
+ | BUTTON2_CLICKED | ||
+ | BUTTON2_DOUBLE_CLICKED | ||
+ | BUTTON2_TRIPLE_CLICKED | ||
+ | BUTTON3_PRESSED | ||
+ | BUTTON3_RELEASED | ||
+ | BUTTON3_CLICKED | ||
+ | BUTTON3_DOUBLE_CLICKED | ||
+ | BUTTON3_TRIPLE_CLICKED | ||
+ | BUTTON4_PRESSED | ||
+ | BUTTON4_RELEASED | ||
+ | BUTTON4_CLICKED | ||
+ | BUTTON4_DOUBLE_CLICKED | ||
+ | BUTTON4_TRIPLE_CLICKED | ||
+ | BUTTON_SHIFT | ||
+ | BUTTON_CTRL | ||
+ | BUTTON_ALT | ||
+ | ALL_MOUSE_EVENTS | ||
+ | REPORT_MOUSE_POSITION | ||
+ | */ | ||
+ | |||
+ | case KEY_RESIZE: | ||
+ | redrawBorder(); | ||
+ | mvaddstr(10, | ||
+ | break; | ||
+ | |||
+ | default: | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * Cleanup and shutdown | ||
+ | */ | ||
+ | endwin(); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Modifier keys ==== | ||
+ | |||
+ | <code cpp> | ||
+ | |||
+ | // int c = getch(); | ||
+ | // switch (c) | ||
+ | case ctrl(' | ||
+ | addstr(" | ||
+ | break; | ||
+ | default: | ||
+ | if (c == KEY_ENTER) { | ||
+ | addstr(" | ||
+ | } | ||
+ | if (c == 27) { // Alt key was down when key pressed | ||
+ | // Get the next char that came with the alt | ||
+ | c = getch(); | ||
+ | addstr(" | ||
+ | addch(c); // could be ' | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Important variables and functions ===== | ||
+ | |||
+ | |||
+ | <code cpp> | ||
+ | initscr(); // Initialize ncurses | ||
+ | endwin(); // Cleanup for exit or shell out (refresh will bring state) | ||
+ | getmaxyx() // get terminal size | ||
+ | |||
+ | cbreak() | ||
+ | nonl() | ||
+ | noecho() | ||
+ | curs_set() | ||
+ | |||
+ | |||
+ | newwin() | ||
+ | |||
+ | // Add chars to window | ||
+ | mvaddch(y, | ||
+ | addstr() | ||
+ | printw() | ||
+ | addch() | ||
+ | |||
+ | |||
+ | // Get input | ||
+ | getch() | ||
+ | wgetstr() | ||
+ | wscanw() | ||
+ | keypad(stdscr, | ||
+ | |||
+ | if (has_colors()) { start_color(); | ||
+ | use_default_colors() | ||
+ | |||
+ | |||
+ | refresh() // Refresh from stdscr | ||
+ | wrefresh() // Refresh using non-stdscr window | ||
+ | wnoutrefresh() // Disable auto update on window update (pair with doupdate) | ||
+ | doupdate() // Manually repaint all updates | ||
+ | touchwin() | ||
+ | |||
+ | box() | ||
+ | border() | ||
+ | |||
+ | // Colors | ||
+ | init_pair() | ||
+ | COLOR_PAIR | ||
+ | |||
+ | attron() | ||
+ | attroff() | ||
+ | attrset() | ||
+ | |||
+ | move(y, x) | ||
+ | |||
+ | |||
+ | |||
+ | mousemask(ALL_MOUSE_EVENTS, | ||
+ | MEVENT // data type with bits set for specific mouse events like BUTTON1_CLICKED | ||
+ | |||
+ | // Special events from wgetch() | ||
+ | KEY_MOUSE // Mouse event (use getmouse()) | ||
+ | KEY_RESIZE // Terminal was resized and adjusted | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | scrollok() | ||
+ | leaveok() | ||
+ | |||
+ | derwin() | ||
+ | subwin() | ||
+ | delwin() | ||
+ | |||
+ | |||
+ | define_key() // function-key control sequences | ||
+ | keyok() // toggle the listening for special control sequences | ||
+ | </ | ||
+ |