dorne/cursetree/curse.c

84 lines
2.2 KiB
C

#include <locale.h>
#include <unistd.h>
#include "curse.h"
#define BASE_DELAY 1000000
#define CRS_LOCALE "en_US.UTF-8"
/* Set ncurses terminal mode (buffering, character processing,
* Key->SIG handling, and other termios(3) functionality).
*/
int termmode(const enum crs_termmode mode) {
switch (mode) {
case TMODE_CBREAK:
return cbreak();
case TMODE_RAW:
return raw();
case TMODE_NOCBREAK:
return nocbreak();
case TMODE_NORAW:
return noraw();
default:
/* defaulting is not possible. */
return 1;
}
}
/* Apply a default IO configuration for an ncurses WINDOW.
*/
static inline void __conf_window(WINDOW *const win) {
nodelay(win, TRUE); // getch(3x) nonblocking IO
keypad(win, TRUE); // allow function keys
// intrflush(3x) - flush terminal input buffer on interrupt key
intrflush(win, FALSE);
}
/* Initialise ncurses for our usecase.
* WARNING: This function should only be called once.
*/
void init_ncurses(void) {
// ncurses expects a locale for consistent behaviour
setlocale(LC_ALL, CRS_LOCALE);
/* NCurses Init */
initscr();
/* WARNING: no you shouldn't delwin(stdscr) it breaks everything... */
__conf_window(stdscr);
start_color();
/* Screen Configuration */
termmode(TMODE_CBREAK);
noecho(); // manually echo from getch(3x)
curs_set(0); // hide cursor
}
/* Initialise (with default IO configuration) a new ncurses WINDOW.
*/
WINDOW *new_window(const int x, const int y, const int width,
const int height) {
WINDOW *win = newwin(height, width, y, x);
__conf_window(win);
return win;
}
/* Initialise (with default IO configuration) the root ncurses WINDOW.
* NOTE: This is typically done via initscr(3x) and called "stdscr".
*/
WINDOW *root_window(void) {
WINDOW *rootwin = new_window(0, 0, COLS, LINES);
__conf_window(rootwin);
return rootwin;
}
/* Resize and move (if resized successfully) an ncurses WINDOW.
* Returns ERR (1) on fail, and OK (0) on success.
* NOTE: Failure occurs if width or height <= 0,
* NOTE: or if the x,y coordinate is outside the screen bounds.
*/
int resizemv_window(const int x, const int y, const int width, const int height,
WINDOW *const win) {
return wresize(win, height, width) || mvwin(win, y, x);
}