hide <ncursesw/ncurses.h> behind ncrswrap.*

isolate ct_dims implementation to dims.*
This commit is contained in:
Emile Clark-Boman 2025-09-13 12:44:58 +10:00
parent 1dd5dd79c8
commit b04f0b4aa3
8 changed files with 142 additions and 113 deletions

View file

@ -1,6 +1,9 @@
#include <locale.h>
#include <unistd.h>
/* libncurses with wide-character support. */
#include <ncursesw/ncurses.h>
#include "ncrswrap.h"
#define CRS_LOCALE "en_US.UTF-8"
@ -24,6 +27,18 @@ int termmode(const enum crs_termmode mode) {
}
}
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) {
@ -40,7 +55,7 @@ void init_ncurses(void) {
// ncurses expects a locale for consistent behaviour
setlocale(LC_ALL, CRS_LOCALE);
/* NCurses Init */
/* NCurses Initialisation */
initscr();
/* WARNING: no you shouldn't delwin(stdscr) it breaks everything... */
__conf_window(stdscr);
@ -63,15 +78,28 @@ WINDOW *new_window(const int x, const int y, const int width,
return win;
}
/* Initialise (with default IO configuration) the root ncurses WINDOW.
* NOTE: This is typically done via initscr(3x) and called "stdscr".
/* Initialise (with default IO configuration) a fullscreen ncurses WINDOW.
*/
WINDOW *root_window(void) {
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,