dorne/cursetree/ncrswrap.c

112 lines
2.9 KiB
C
Raw Normal View History

2025-09-10 22:49:05 +10:00
#include <locale.h>
#include <unistd.h>
/* libncurses with wide-character support. */
#include <ncursesw/ncurses.h>
#include "ncrswrap.h"
2025-09-10 22:49:05 +10:00
#define CRS_LOCALE "en_US.UTF-8"
/* Set ncurses terminal mode (buffering, character processing,
* Key->SIG handling, and other termios(3) functionality).
*/
2025-09-11 17:37:01 +10:00
int termmode(const enum crs_termmode mode) {
2025-09-10 22:49:05 +10:00
switch (mode) {
2025-09-10 23:05:07 +10:00
case TMODE_CBREAK:
2025-09-10 22:49:05 +10:00
return cbreak();
2025-09-10 23:05:07 +10:00
case TMODE_RAW:
2025-09-10 22:49:05 +10:00
return raw();
2025-09-10 23:05:07 +10:00
case TMODE_NOCBREAK:
2025-09-10 22:49:05 +10:00
return nocbreak();
2025-09-10 23:05:07 +10:00
case TMODE_NORAW:
2025-09-10 22:49:05 +10:00
return noraw();
2025-09-11 17:37:01 +10:00
default:
2025-09-10 22:49:05 +10:00
/* defaulting is not possible. */
2025-09-11 17:37:01 +10:00
return 1;
2025-09-10 22:49:05 +10:00
}
}
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);
}
2025-09-10 22:49:05 +10:00
/* Initialise ncurses for our usecase.
* WARNING: This function should only be called once.
*/
2025-09-11 17:37:01 +10:00
void init_ncurses(void) {
2025-09-10 22:49:05 +10:00
// ncurses expects a locale for consistent behaviour
setlocale(LC_ALL, CRS_LOCALE);
/* NCurses Initialisation */
initscr();
2025-09-12 01:53:20 +10:00
/* WARNING: no you shouldn't delwin(stdscr) it breaks everything... */
__conf_window(stdscr);
2025-09-10 22:49:05 +10:00
start_color();
/* Screen Configuration */
2025-09-10 23:05:07 +10:00
termmode(TMODE_CBREAK);
noecho(); // manually echo from getch(3x)
2025-09-10 22:49:05 +10:00
curs_set(0); // hide cursor
}
2025-09-11 17:37:01 +10:00
/* Initialise (with default IO configuration) a new ncurses WINDOW.
2025-09-10 23:05:07 +10:00
*/
2025-09-11 17:37:01 +10:00
WINDOW *new_window(const int x, const int y, const int width,
const int height) {
2025-09-10 23:05:07 +10:00
WINDOW *win = newwin(height, width, y, x);
__conf_window(win);
return win;
}
/* Initialise (with default IO configuration) a fullscreen ncurses WINDOW.
2025-09-10 23:05:07 +10:00
*/
WINDOW *new_window_fs(void) {
WINDOW *rootwin = new_window(0, 0, COLS, LINES);
2025-09-10 23:05:07 +10:00
__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(const int x, const int y, const int width, const int height,
WINDOW *const win) {
return wresize(win, height, width) || mvwin(win, y, x);
}