From 76d8e2dfce8dff514e174c06a755e0c5e93086cc Mon Sep 17 00:00:00 2001 From: nick black Date: Mon, 28 Dec 2020 13:38:05 -0500 Subject: [PATCH] ncdirect_{fg, bg}_default() -> ncdirect_set_{fg, bg}_default() #1261 --- NEWS.md | 2 + include/ncpp/Direct.hh | 4 +- include/notcurses/direct.h | 10 +++- src/fetch/main.c | 2 +- src/lib/direct.cpp | 97 +++++++++++++++++++++++++------------- src/lib/internal.h | 3 +- src/lib/render.c | 6 +-- src/poc/dirgb.c | 4 +- src/poc/dirlines.c | 8 ++-- 9 files changed, 87 insertions(+), 49 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6d9a62b0c..7f52980ca 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ rearrangements of Notcurses. * `ncdirect_styles_{set, on, off}()` have been deprecated in favor of `ncdirect_{set, on, off}_styles()`, to match `ncplane_` equivalents. * `ncdirect_raster_frame()` no longer requires `blitter` nor `scale`. + * `ncdirect_{fg, bg}_default()` have been deprecated in favor of + `ncdirect_set_{fg, bg}_default()`, to match `ncplane`. * 2.1.2 (2020-12-25) * Add `notcurses_linesigs_enable()` and `notcurses_linesigs_disable()`. diff --git a/include/ncpp/Direct.hh b/include/ncpp/Direct.hh index 64126da9d..e6e93fe91 100644 --- a/include/ncpp/Direct.hh +++ b/include/ncpp/Direct.hh @@ -35,7 +35,7 @@ namespace ncpp bool set_fg_default () const NOEXCEPT_MAYBE { - return error_guard (ncdirect_fg_default (direct), -1); + return error_guard (ncdirect_set_fg_default (direct), -1); } bool set_fg_rgb (unsigned rgb) const NOEXCEPT_MAYBE @@ -55,7 +55,7 @@ namespace ncpp bool set_bg_default () const NOEXCEPT_MAYBE { - return error_guard (ncdirect_bg_default (direct), -1); + return error_guard (ncdirect_set_bg_default (direct), -1); } bool set_bg_rgb (unsigned rgb) const NOEXCEPT_MAYBE diff --git a/include/notcurses/direct.h b/include/notcurses/direct.h index 060539cd9..da8bfc544 100644 --- a/include/notcurses/direct.h +++ b/include/notcurses/direct.h @@ -78,8 +78,14 @@ ncdirect_fg_rgb8(struct ncdirect* nc, unsigned r, unsigned g, unsigned b){ return ncdirect_fg_rgb(nc, (r << 16u) + (g << 8u) + b); } -API int ncdirect_fg_default(struct ncdirect* nc); -API int ncdirect_bg_default(struct ncdirect* nc); +API int ncdirect_set_fg_default(struct ncdirect* nc); +API int ncdirect_set_bg_default(struct ncdirect* nc); + +API int ncdirect_fg_default(struct ncdirect* nc) + __attribute__ ((deprecated)); + +API int ncdirect_bg_default(struct ncdirect* nc) + __attribute__ ((deprecated)); // Get the current number of columns/rows. API int ncdirect_dim_x(const struct ncdirect* nc); diff --git a/src/fetch/main.c b/src/fetch/main.c index a637c2f39..f635566ff 100644 --- a/src/fetch/main.c +++ b/src/fetch/main.c @@ -276,7 +276,7 @@ drawpalette(struct ncdirect* nc){ return -1; } } - if(ncdirect_bg_default(nc)){ + if(ncdirect_set_bg_default(nc)){ return -1; } if(putchar('\n') == EOF){ diff --git a/src/lib/direct.cpp b/src/lib/direct.cpp index ab5563ceb..6c6f1744e 100644 --- a/src/lib/direct.cpp +++ b/src/lib/direct.cpp @@ -9,16 +9,31 @@ #include "notcurses/direct.h" #include "internal.h" +static inline uint64_t +ncdirect_channels(const ncdirect* nc){ + return nc->channels; +} + +static inline bool +ncdirect_fg_default_p(const struct ncdirect* nc){ + return channels_fg_default_p(ncdirect_channels(nc)); +} + +static inline bool +ncdirect_bg_default_p(const struct ncdirect* nc){ + return channels_bg_default_p(ncdirect_channels(nc)); +} + int ncdirect_putstr(ncdirect* nc, uint64_t channels, const char* utf8){ if(channels_fg_default_p(channels)){ - if(ncdirect_fg_default(nc)){ + if(ncdirect_fg_default_p(nc)){ return -1; } }else if(ncdirect_fg_rgb(nc, channels_fg_rgb(channels))){ return -1; } if(channels_bg_default_p(channels)){ - if(ncdirect_bg_default(nc)){ + if(ncdirect_set_bg_default(nc)){ return -1; } }else if(ncdirect_bg_rgb(nc, channels_bg_rgb(channels))){ @@ -372,8 +387,10 @@ ncdirect_dump_plane(ncdirect* n, const ncplane* np, int xoff){ ncplane_dim_yx(np, &dimy, &dimx); //fprintf(stderr, "rasterizing %dx%d+%d\n", dimy, dimx, xoff); // save the existing style and colors - bool fgdefault = n->fgdefault, bgdefault = n->bgdefault; - uint32_t fgrgb = n->fgrgb, bgrgb = n->bgrgb; + const bool fgdefault = ncdirect_fg_default_p(n); + const bool bgdefault = ncdirect_bg_default_p(n); + const uint32_t fgrgb = channels_fg_rgb(n->channels); + const uint32_t bgrgb = channels_bg_rgb(n->channels); for(int y = 0 ; y < dimy ; ++y){ if(xoff){ if(ncdirect_cursor_move_yx(n, -1, xoff)){ @@ -388,12 +405,12 @@ ncdirect_dump_plane(ncdirect* n, const ncplane* np, int xoff){ return -1; } if(channels_fg_alpha(channels) == CELL_ALPHA_TRANSPARENT){ - ncdirect_fg_default(n); + ncdirect_set_fg_default(n); }else{ ncdirect_fg_rgb(n, channels_fg_rgb(channels)); } if(channels_bg_alpha(channels) == CELL_ALPHA_TRANSPARENT){ - ncdirect_bg_default(n); + ncdirect_set_bg_default(n); }else{ ncdirect_bg_rgb(n, channels_bg_rgb(channels)); } @@ -408,8 +425,8 @@ ncdirect_dump_plane(ncdirect* n, const ncplane* np, int xoff){ // each line of output; this is necessary if our output is lifted out and // used in something e.g. paste(1). // FIXME replace with a SGR clear - ncdirect_fg_default(n); - ncdirect_bg_default(n); + ncdirect_set_fg_default(n); + ncdirect_set_bg_default(n); if(putc('\n', n->ttyfp) == EOF){ return -1; } @@ -421,12 +438,12 @@ ncdirect_dump_plane(ncdirect* n, const ncplane* np, int xoff){ } // restore the previous colors if(fgdefault){ - ncdirect_fg_default(n); + ncdirect_set_fg_default(n); }else{ ncdirect_fg_rgb(n, fgrgb); } if(bgdefault){ - ncdirect_bg_default(n); + ncdirect_set_bg_default(n); }else{ ncdirect_bg_rgb(n, bgrgb); } @@ -610,8 +627,7 @@ ncdirect* ncdirect_init(const char* termtype, FILE* outfp, uint64_t flags){ if(interrogate_terminfo(&ret->tcache, shortname_term)){ goto err; } - ret->fgdefault = ret->bgdefault = true; - ret->fgrgb = ret->bgrgb = 0; + ret->channels = 0; ncdirect_set_styles(ret, 0); return ret; @@ -669,11 +685,12 @@ ncdirect_style_emit(ncdirect* n, unsigned stylebits, FILE* out){ } // sgr resets colors, so set them back up if not defaults if(r == 0){ - if(!n->fgdefault){ - r |= ncdirect_fg_rgb(n, n->fgrgb); + // FIXME need to handle palette-indexed colors + if(!ncdirect_fg_default_p(n)){ + r |= ncdirect_fg_rgb(n, channels_fg_rgb(n->channels)); } - if(!n->bgdefault){ - r |= ncdirect_bg_rgb(n, n->bgrgb); + if(!ncdirect_bg_default_p(n)){ + r |= ncdirect_bg_rgb(n, channels_bg_rgb(n->channels)); } } return r; @@ -750,26 +767,42 @@ unsigned ncdirect_palette_size(const ncdirect* nc){ return nc->tcache.colors; } -int ncdirect_fg_default(ncdirect* nc){ - if(term_emit("op", nc->tcache.op, nc->ttyfp, false) == 0){ - nc->fgdefault = true; - if(nc->bgdefault){ - return 0; - } - return ncdirect_bg_rgb(nc, nc->bgrgb); +int ncdirect_set_fg_default(ncdirect* nc){ + if(ncdirect_fg_default_p(nc)){ + return 0; } - return -1; + if(nc->tcache.fgop){ + if(term_emit("fgop", nc->tcache.fgop, nc->ttyfp, false)){ + return -1; + } + }else if(term_emit("op", nc->tcache.op, nc->ttyfp, false) == 0){ + if(!ncdirect_bg_default_p(nc)){ + if(ncdirect_bg_rgb(nc, channels_bg_rgb(nc->channels))){ + return -1; + } + } + } + channels_set_fg_default(&nc->channels); + return 0; } -int ncdirect_bg_default(ncdirect* nc){ - if(term_emit("op", nc->tcache.op, nc->ttyfp, false) == 0){ - nc->bgdefault = true; - if(nc->fgdefault){ - return 0; - } - return ncdirect_fg_rgb(nc, nc->fgrgb); +int ncdirect_set_bg_default(ncdirect* nc){ + if(ncdirect_bg_default_p(nc)){ + return 0; } - return -1; + if(nc->tcache.bgop){ + if(term_emit("bgop", nc->tcache.bgop, nc->ttyfp, false)){ + return -1; + } + }else if(term_emit("op", nc->tcache.op, nc->ttyfp, false) == 0){ + if(!ncdirect_fg_default_p(nc)){ + if(ncdirect_fg_rgb(nc, channels_fg_rgb(nc->channels))){ + return -1; + } + } + } + channels_set_bg_default(&nc->channels); + return 0; } int ncdirect_hline_interp(ncdirect* n, const char* egc, int len, diff --git a/src/lib/internal.h b/src/lib/internal.h index dfb89e3a5..479718e25 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -304,10 +304,9 @@ typedef struct ncdirect { FILE* ttyfp; // FILE* for output tty int ctermfd; // fd for controlling terminal tinfo tcache; // terminfo cache - unsigned fgrgb, bgrgb; // last RGB values of foreground/background + uint64_t channels; // current channels uint16_t stylemask; // current styles ncinputlayer input; // input layer; we're in cbreak mode - bool fgdefault, bgdefault; // are FG/BG currently using default colors? bool utf8; // are we using utf-8 encoding, as hoped? struct termios tpreserved; // terminal state upon entry // some terminals (e.g. kmscon) return cursor coordinates inverted from the diff --git a/src/lib/render.c b/src/lib/render.c index 84dbda535..e3ee00292 100644 --- a/src/lib/render.c +++ b/src/lib/render.c @@ -1241,8 +1241,7 @@ int ncdirect_bg_rgb(ncdirect* nc, unsigned rgb){ nc->tcache.bg_collides_default)){ return -1; } - nc->bgdefault = false; - nc->bgrgb = rgb; + channels_set_bg_rgb(&nc->channels, rgb); return 0; } @@ -1254,8 +1253,7 @@ int ncdirect_fg_rgb(ncdirect* nc, unsigned rgb){ (rgb & 0xff0000u) >> 16u, (rgb & 0xff00u) >> 8u, rgb & 0xffu)){ return -1; } - nc->fgdefault = false; - nc->fgrgb = rgb; + channels_set_fg_rgb(&nc->channels, rgb); return 0; } diff --git a/src/poc/dirgb.c b/src/poc/dirgb.c index 4af6f020f..ea6709b61 100644 --- a/src/poc/dirgb.c +++ b/src/poc/dirgb.c @@ -107,8 +107,8 @@ int main(void){ int leny = ncdirect_dim_y(nc); int lenx = ncdirect_dim_x(nc); - ncdirect_fg_default(nc); - ncdirect_bg_default(nc); + ncdirect_set_fg_default(nc); + ncdirect_set_bg_default(nc); if(ncdirect_cursor_move_yx(nc, leny / 2, (lenx - 4) / 2)){ goto err; } diff --git a/src/poc/dirlines.c b/src/poc/dirlines.c index 5691257c7..3cf3bdd0a 100644 --- a/src/poc/dirlines.c +++ b/src/poc/dirlines.c @@ -15,8 +15,8 @@ int main(void){ if(ncdirect_hline_interp(n, "-", i, c1, c2) < i){ return EXIT_FAILURE; } - ncdirect_fg_default(n); - ncdirect_bg_default(n); + ncdirect_set_fg_default(n); + ncdirect_set_bg_default(n); putchar('\n'); } for(int i = 0 ; i < 15 ; ++i){ @@ -26,8 +26,8 @@ int main(void){ if(ncdirect_vline_interp(n, "|", i, c1, c2) < i){ return EXIT_FAILURE; } - ncdirect_fg_default(n); - ncdirect_bg_default(n); + ncdirect_set_fg_default(n); + ncdirect_set_bg_default(n); if(i < 14){ if(ncdirect_cursor_up(n, i)){ return EXIT_FAILURE;