114 lines
2.9 KiB
C
114 lines
2.9 KiB
C
#include <locale.h>
|
|
#include <unistd.h>
|
|
|
|
#include "_ncurses.h"
|
|
|
|
#include "ncrswrap.h"
|
|
|
|
#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;
|
|
}
|
|
}
|
|
|
|
void termsize(int *const width, int *const height) {
|
|
*width = COLS;
|
|
*height = LINES;
|
|
}
|
|
|
|
struct ct_dims *termdims(void) {
|
|
struct ct_dims *dims = new_dims(0, 0, 0, 0);
|
|
termsize(&dims->width, &dims->height);
|
|
|
|
return dims;
|
|
}
|
|
|
|
/* 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 Initialisation */
|
|
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
|
|
}
|
|
|
|
void end_ncurses(void) {
|
|
endwin();
|
|
}
|
|
|
|
/* 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) a fullscreen ncurses WINDOW.
|
|
*/
|
|
WINDOW *new_window_fs(void) {
|
|
WINDOW *rootwin = new_window(0, 0, COLS, LINES);
|
|
__conf_window(rootwin);
|
|
return rootwin;
|
|
}
|
|
|
|
// void destroy_window(WINDOW *) __attribute__((alias("delwin")));
|
|
|
|
// int winposx(WINDOW *) __attribute__((alias("getbegy")));
|
|
// int winposy(WINDOW *) __attribute__((alias("getbegy")));
|
|
// int winwidth(WINDOW *) __attribute__((alias("getmaxx")));
|
|
// int winheight(WINDOW *) __attribute__((alias("getmaxy")));
|
|
|
|
// struct ct_dims *windims(WINDOW *win) {
|
|
// int x, y, width, height;
|
|
// winpos(win, x, y);
|
|
// winsize(win, width, height);
|
|
// return new_dims(x, y, width, height);
|
|
// }
|
|
|
|
/* 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(WINDOW *const win, const int x, const int y,
|
|
const int width, const int height) {
|
|
return wresize(win, height, width) || mvwin(win, y, x);
|
|
}
|