notcurses_setrgb(): plug into direct mode when RGB flag is set

This commit is contained in:
nick black 2019-11-19 06:26:44 -05:00
parent 8fe936ca4c
commit 0f6e19dd92
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
4 changed files with 50 additions and 19 deletions

View File

@ -1,6 +1,8 @@
#ifndef NOTCURSES_NOTCURSES
#define NOTCURSES_NOTCURSES
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -9,7 +11,14 @@ const char* notcurses_version(void);
struct notcurses;
struct notcurses* notcurses_init(void);
// Initialize a notcurses context. While a program can have more than one
// context, it usually doesn't make much sense, as they're all dealing with
// the same screen(s). Returns NULL on error, including any failure to
// initialize the terminfo library. If termtype is NULL, the environment
// variable TERM is used.
struct notcurses* notcurses_init(const char* termtype);
// Destroy a notcurses context.
int notcurses_stop(struct notcurses* nc);
// Make the physical screen match the virtual screen. Changes made to the
@ -17,8 +26,15 @@ int notcurses_stop(struct notcurses* nc);
// successful call to notcurses_render().
int notcurses_render(struct notcurses* nc);
// Refresh the object's concept of the terminal size, and return it via *rows
// and *cols. On error, -1 is returned, and the params are untouched.
int notcurses_term_dimensions(struct notcurses* n, int* rows, int* cols);
// Set the current color using RGB specifications. If the terminal does not
// support 16M colors (indicated by the "RGB" terminfo capability), the
// provided values will be interpreted in some fashion.
int notcurses_setrgb(struct notcurses* nc, uint32_t r, uint32_t g, uint32_t b);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -4,7 +4,10 @@
int main(void){
struct notcurses* nc;
if((nc = notcurses_init()) == NULL){
if((nc = notcurses_init(NULL)) == NULL){
return EXIT_FAILURE;
}
if(notcurses_setrgb(nc, 200, 0, 200)){
return EXIT_FAILURE;
}
if(notcurses_render(nc)){

View File

@ -35,14 +35,16 @@ typedef struct cell {
} cell;
typedef struct notcurses {
int ttyfd; // file descriptor for controlling tty (takes stdin)
int ttyfd; // file descriptor for controlling tty (takes stdin)
int colors;
int rows, cols; // most recently measured values
char* smcup; // enter alternate mode
char* rmcup; // restore primary mode
char* smcup; // enter alternate mode
char* rmcup; // restore primary mode
char* setaf; // set foreground
char* setab; // set background
struct termios tpreserved; // terminal state upon entry
bool RGBflag; // terminfo reported "RGB" flag for 24bpc directcolor
cell* plane; // the contents of our bottommost plane
bool RGBflag; // terminfo reported "RGB" flag for 24bpc directcolor
cell* plane; // the contents of our bottommost plane
} notcurses;
static const char NOTCURSES_VERSION[] =
@ -128,13 +130,13 @@ alloc_plane(notcurses* nc, cell* oldplane, int* rows, int* cols){
// FIXME should probably register a SIGWINCH handler here
// FIXME install other sighandlers to clean things up
notcurses* notcurses_init(void){
notcurses* notcurses_init(const char* termtype){
struct termios modtermios;
notcurses* ret = malloc(sizeof(*ret));
if(ret == NULL){
return ret;
}
ret->ttyfd = STDIN_FILENO; // FIXME use others if stderr is redirected?
ret->ttyfd = STDOUT_FILENO; // FIXME use others if stdout is redirected?
if(tcgetattr(ret->ttyfd, &ret->tpreserved)){
fprintf(stderr, "Couldn't preserve terminal state for %d (%s)\n",
ret->ttyfd, strerror(errno));
@ -149,7 +151,7 @@ notcurses* notcurses_init(void){
goto err;
}
int termerr;
if(setupterm(NULL, ret->ttyfd, &termerr) != OK){
if(setupterm(termtype, ret->ttyfd, &termerr) != OK){
fprintf(stderr, "Terminfo error %d (see terminfo(3ncurses))\n", termerr);
goto err;
}
@ -167,6 +169,8 @@ notcurses* notcurses_init(void){
// Neither of these is supported on e.g. the "linux" virtual console.
term_get_seq(&ret->smcup, "smcup");
term_get_seq(&ret->rmcup, "rmcup");
term_get_seq(&ret->setaf, "setaf");
term_get_seq(&ret->setab, "setab");
ret->rows = ret->cols = 0;
if((ret->plane = alloc_plane(ret, NULL, &ret->rows, &ret->cols)) == NULL){
goto err;
@ -211,18 +215,26 @@ erpchar(int c){
#define tparm2(a,b) tparm(a,b,0,0,0,0,0,0,0,0)
int notcurses_setrgb(notcurses* nc, uint32_t r, uint32_t g, uint32_t b){
static char rgbesc[] = "\x1b[38;2;200;0;200m";
if(nc->RGBflag){
if(write(nc->ttyfd, rgbesc, sizeof(rgbesc)) != sizeof(rgbesc)){
return -1;
}
}else{
// For 256-color indexed mode, start constructing a palette based off
// the inputs. If more than 256 are used on a single screen, start...
// combining close ones? For 8-color mode, simple interpolation. I have no
// idea what to do for 88 colors. FIXME
return -1;
}
return 0;
}
// FIXME this needs to keep an invalidation bitmap, rather than blitting the
// world every time
int notcurses_render(notcurses* nc){
int ret = 0;
char* tstr = tparm2(set_a_foreground, 1/*, 255, 255*/);
if(tstr == NULL){
fprintf(stderr, "couldn't get string\n");
return -1;
}
if(tputs(tstr, 1, erpchar) != OK){
fprintf(stderr, "couldn't write string\n");
}
strout("make it happen!\n");
// FIXME mariahv("make it happen!");
return ret;

View File

@ -6,7 +6,7 @@
class NotcursesTest : public :: testing::Test {
protected:
void SetUp() override {
nc_ = notcurses_init();
nc_ = notcurses_init(nullptr);
std::cerr << (void*)nc_ << std::endl;
ASSERT_NE(nullptr, nc_);
if(getenv("TERM") == nullptr){