Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
notcurses.c
Go to the documentation of this file.
1#include "linux.h"
2#include "version.h"
3#include "egcpool.h"
4#include "internal.h"
5#include <time.h>
6#include <fcntl.h>
7#include <errno.h>
8#include <stdio.h>
9#include <limits.h>
10#include <string.h>
11#include <unistd.h>
12#include <stdlib.h>
13#include <unistr.h>
14#include <locale.h>
15#include <uniwbrk.h>
16#include <inttypes.h>
17#include "compat/compat.h"
18#include "unixsig.h"
19#include "banner.h"
20
21#define ESC "\x1b"
22#define TABSTOP 8
23
24void notcurses_version_components(int* major, int* minor, int* patch, int* tweak){
25 *major = NOTCURSES_VERNUM_MAJOR;
26 *minor = NOTCURSES_VERNUM_MINOR;
27 *patch = NOTCURSES_VERNUM_PATCH;
28 *tweak = atoi(NOTCURSES_VERSION_TWEAK);
29}
30
32 if(nc->tcache.ttyfd < 0){
33 return -1;
34 }
36 return -1;
37 }
39 return 0;
40}
41
43 if(nc->tcache.ttyfd < 0){
44 return -1;
45 }
47 &nc->tcache, nc->flags & NCOPTION_DRAIN_INPUT)){
48 return -1;
49 }
50 // move to the end of our output
51 if(nc->rstate.logendy < 0){
52 return 0;
53 }
55 return 0;
56}
57
58// reset the current colors, styles, and palette. called on startup (to purge
59// any preexisting styling) and shutdown (to not affect further programs).
60int reset_term_attributes(const tinfo* ti, fbuf* f){
61 int ret = 0;
62 const char* esc;
63 if((esc = get_escape(ti, ESCAPE_OP)) && fbuf_emit(f, esc)){
64 ret = -1;
65 }
66 if((esc = get_escape(ti, ESCAPE_SGR0)) && fbuf_emit(f, esc)){
67 ret = -1;
68 }
69 return ret;
70}
71
72// attempt to restore the palette. if XT{PUSH,POP}COLORS is supported, use
73// XTPOPCOLORS. if we can program individual colors, and we read the palette,
74// reload it from our initial capture. otherwise, use "oc" if available; this
75// will blow away any preexisting palette in favor of the default. if we've
76// never touched the palette, don't bother trying to restore it (unless we're
77// using XTPOPCOLORS, since in that case we always used XTPUSHCOLORS).
78int reset_term_palette(const tinfo* ti, fbuf* f, unsigned touchedpalette){
79 int ret = 0;
80 const char* esc;
81 if((esc = get_escape(ti, ESCAPE_RESTORECOLORS))){
82 loginfo("restoring palette via xtpopcolors");
83 if(fbuf_emit(f, esc)){
84 ret = -1;
85 }
86 return ret;
87 }
88 if(!touchedpalette){
89 return 0;
90 }
91 if(ti->caps.can_change_colors && ti->maxpaletteread > -1){
92 loginfo("restoring saved palette (%d)", ti->maxpaletteread + 1);
93 esc = get_escape(ti, ESCAPE_INITC);
94 for(int z = 0 ; z < ti->maxpaletteread ; ++z){
95 unsigned r, g, b;
96 ncchannel_rgb8(ti->originalpalette.chans[z], &r, &g, &b);
97 // Need convert RGB values [0..256) to [0..1000], ugh
98 r = r * 1000 / 255;
99 g = g * 1000 / 255;
100 b = b * 1000 / 255;
101 if(fbuf_emit(f, tiparm(esc, z, r, g, b)) < 0){
102 return -1;
103 }
104 }
105 }else if((esc = get_escape(ti, ESCAPE_OC))){
106 loginfo("resetting palette");
107 if(fbuf_emit(f, esc)){
108 ret = -1;
109 }
110 }else{
111 logwarn("no method known to restore palette");
112 }
113 return ret;
114}
115
116// Do the minimum necessary stuff to restore the terminal, then return. This is
117// the end of the line for fatal signal handlers. notcurses_stop() will go on
118// to tear down and account for internal structures. note that we do lots of
119// shit here that is unsafe within a signal handler =[ FIXME.
120static int
121notcurses_stop_minimal(void* vnc, void** altstack){
122 notcurses* nc = vnc;
123 int ret = 0;
124 ret |= drop_signals(nc, altstack);
125 // collect output into the memstream buffer, and then dump it directly using
126 // blocking_write(), to avoid problems with unreliable fflush().
127 fbuf* f = &nc->rstate.f;
128 fbuf_reset(f);
129 // be sure to write the restoration sequences *prior* to running rmcup, as
130 // they apply to the screen (alternate or otherwise) we're actually using.
131 const char* esc;
132 ret |= reset_term_palette(&nc->tcache, f, nc->touched_palette);
133 ret |= reset_term_attributes(&nc->tcache, f);
134 if((esc = get_escape(&nc->tcache, ESCAPE_RMKX)) && fbuf_emit(f, esc)){
135 ret = -1;
136 }
137 const char* cnorm = get_escape(&nc->tcache, ESCAPE_CNORM);
138 if(cnorm && fbuf_emit(f, cnorm)){
139 ret = -1;
140 }
141 if(fbuf_flush(f, nc->ttyfp)){
142 ret = -1;
143 }
144 if(nc->tcache.ttyfd >= 0){
145 ret |= notcurses_mice_disable(nc);
146 if(nc->tcache.tpreserved){
147 ret |= tcsetattr(nc->tcache.ttyfd, TCSAFLUSH, nc->tcache.tpreserved);
148 }
149 // don't use leave_alternate_screen() here; we need pop the keyboard
150 // whether we're in regular or alternate screen, and we need it done
151 // before returning to the regular screen if we're in the alternate. if
152 // we drained input, we never sent a keyboard modifier; send none now.
153 if(!(nc->flags & NCOPTION_DRAIN_INPUT)){
154 if(nc->tcache.kbdlevel){
155 if(tty_emit(KKEYBOARD_POP, nc->tcache.ttyfd)){
156 ret = -1;
157 }
158 }else{
159 if(tty_emit(XTMODKEYSUNDO, nc->tcache.ttyfd)){
160 ret = -1;
161 }
162 }
163 }
164 if(nc->tcache.in_alt_screen){
165 if((esc = get_escape(&nc->tcache, ESCAPE_RMCUP))){
166 if(tty_emit(esc, nc->tcache.ttyfd)){
167 ret = -1;
168 }
169 nc->tcache.in_alt_screen = 0;
170 }
171 }
172 }
173 logdebug("restored terminal, returning %d", ret);
174 return ret;
175}
176
177static const char NOTCURSES_VERSION[] =
178 NOTCURSES_VERSION_MAJOR "."
179 NOTCURSES_VERSION_MINOR "."
180 NOTCURSES_VERSION_PATCH;
181
182const char* notcurses_version(void){
183 return NOTCURSES_VERSION;
184}
185
186void* ncplane_set_userptr(ncplane* n, void* opaque){
187 void* ret = n->userptr;
188 n->userptr = opaque;
189 return ret;
190}
191
193 return n->userptr;
194}
195
196const void* ncplane_userptr_const(const ncplane* n){
197 return n->userptr;
198}
199
200// is the cursor in an invalid position? it never should be, but it's probably
201// better to make sure (it's cheap) than to read from/write to random crap.
202static bool
203cursor_invalid_p(const ncplane* n){
204 if(n->y >= n->leny || n->x >= n->lenx){
205 return true;
206 }
207 return false;
208}
209
210char* ncplane_at_cursor(const ncplane* n, uint16_t* stylemask, uint64_t* channels){
211 return ncplane_at_yx(n, n->y, n->x, stylemask, channels);
212}
213
214char* ncplane_at_yx(const ncplane* n, int y, int x, uint16_t* stylemask, uint64_t* channels){
215 if(y < 0){
216 if(y != -1){
217 logerror("invalid y: %d", y);
218 return NULL;
219 }
220 y = n->y;
221 }
222 if(x < 0){
223 if(x != -1){
224 logerror("invalid x: %d", x);
225 return NULL;
226 }
227 x = n->x;
228 }
229 if((unsigned)y >= n->leny || (unsigned)x >= n->lenx){
230 logerror("invalid coordinates: %d/%d", y, x);
231 return NULL;
232 }
233 if(n->sprite){
234 if(stylemask){
235 *stylemask = 0;
236 }
237 if(channels){
238 *channels = 0;
239 }
240 return strdup(n->sprite->glyph.buf);
241 }
242 const nccell* yx = &n->fb[nfbcellidx(n, y, x)];
243 // if we're the right side of a wide glyph, we return the main glyph
244 if(nccell_wide_right_p(yx)){
245 return ncplane_at_yx(n, y, x - 1, stylemask, channels);
246 }
247 char* ret = nccell_extract(n, yx, stylemask, channels);
248 if(ret == NULL){
249 return NULL;
250 }
251//fprintf(stderr, "GOT [%s]\n", ret);
252 if(strcmp(ret, "") == 0){
253 free(ret);
254 ret = nccell_strdup(n, &n->basecell);
255 if(ret == NULL){
256 return NULL;
257 }
258 if(stylemask){
259 *stylemask = n->basecell.stylemask;
260 }
261 }
262 // FIXME load basecell channels if appropriate
263 return ret;
264}
265
267 return ncplane_at_yx_cell(n, n->y, n->x, c);
268}
269
271 if(n->sprite){
272 logerror("invoked on a sprixel plane");
273 return -1;
274 }
275 if(y < 0){
276 if(y != -1){
277 logerror("invalid y: %d", y);
278 return -1;
279 }
280 y = n->y;
281 }
282 if(x < 0){
283 if(x != -1){
284 logerror("invalid x: %d", x);
285 return -1;
286 }
287 x = n->x;
288 }
289 if((unsigned)y >= n->leny || (unsigned)x >= n->lenx){
290 logerror("invalid coordinates: %d/%d", y, x);
291 return -1;
292 }
293 nccell* targ = ncplane_cell_ref_yx(n, y, x);
294 if(nccell_duplicate(n, c, targ)){
295 return -1;
296 }
297 // FIXME take base cell into account where necessary!
298 return strlen(nccell_extended_gcluster(n, targ));
299}
300
301void ncplane_dim_yx(const ncplane* n, unsigned* rows, unsigned* cols){
302 if(rows){
303 *rows = n->leny;
304 }
305 if(cols){
306 *cols = n->lenx;
307 }
308}
309
310// anyone calling this needs ensure the ncplane's framebuffer is updated
311// to reflect changes in geometry. also called at startup for standard plane.
312// sets |cgeo_changed| high iff the cell geometry changed (will happen on a
313// resize, and on a font resize if the pixel geometry does not change).
314// sets |pgeo_changed| high iff the cell-pixel geometry changed (will happen
315// on a font resize).
316int update_term_dimensions(unsigned* rows, unsigned* cols, tinfo* tcache,
317 int margin_b, unsigned* cgeo_changed, unsigned* pgeo_changed){
318 *pgeo_changed = 0;
319 *cgeo_changed = 0;
320 // if we're not a real tty, we presumably haven't changed geometry, return
321 if(tcache->ttyfd < 0){
322 if(rows){
323 *rows = tcache->default_rows;
324 }
325 if(cols){
326 *cols = tcache->default_cols;
327 }
328 tcache->cellpxy = 0;
329 tcache->cellpxx = 0;
330 return 0;
331 }
332 unsigned rowsafe, colsafe;
333 if(rows == NULL){
334 rows = &rowsafe;
335 rowsafe = tcache->dimy;
336 }
337 if(cols == NULL){
338 cols = &colsafe;
339 colsafe = tcache->dimx;
340 }
341#ifndef __MINGW32__
342 struct winsize ws;
343 if(tiocgwinsz(tcache->ttyfd, &ws)){
344 return -1;
345 }
346 *rows = ws.ws_row;
347 *cols = ws.ws_col;
348 unsigned cpixy;
349 unsigned cpixx;
350#ifdef __linux__
351 if(tcache->linux_fb_fd >= 0){
352 get_linux_fb_pixelgeom(tcache, &tcache->pixy, &tcache->pixx);
353 cpixy = tcache->pixy / *rows;
354 cpixx = tcache->pixx / *cols;
355 }else{
356#else
357 {
358#endif
359 // we might have the pixel geometry from CSI14t, so don't override a valid
360 // earlier response with 0s from the ioctl. we do want to fire off a fresh
361 // CSI14t in this case, though FIXME.
362 if(ws.ws_ypixel){
363 tcache->pixy = ws.ws_ypixel;
364 tcache->pixx = ws.ws_xpixel;
365 }
366 // update even if we didn't get values just now, because we need set
367 // cellpx{y,x} up from an initial CSI14n, which set only pix{y,x}.
368 cpixy = ws.ws_row ? tcache->pixy / ws.ws_row : 0;
369 cpixx = ws.ws_col ? tcache->pixx / ws.ws_col : 0;
370 }
371 if(tcache->cellpxy != cpixy){
372 tcache->cellpxy = cpixy;
373 *pgeo_changed = 1;
374 }
375 if(tcache->cellpxx != cpixx){
376 tcache->cellpxx = cpixx;
377 *pgeo_changed = 1;
378 }
379 if(tcache->cellpxy == 0 || tcache->cellpxx == 0){
380 tcache->pixel_draw = NULL; // disable support
381 }
382#else
383 CONSOLE_SCREEN_BUFFER_INFO csbi;
384 // There is the buffer itself, which is similar in function to the scrollback
385 // buffer in a Linux terminal, and there is the display window, which is the
386 // visible view of that buffer. The addressable area (from a VT point of
387 // view) spans the width of the buffer, but the height of the display window.
388 //
389 // +--------------------------+ ^
390 // | | |
391 // | | |
392 // +-----+--------------+-----+ ^ w | b
393 // |XXXXX|XXXXXXXXXXXXXX|XXXXX| | i | u
394 // |XXXXX|XXXXXXXXXXXXXX|XXXXX| | n | f
395 // |XXXXX|XXXXXXXXXXXXXX|XXXXX| | d | f
396 // |XXXXX|XXXXXXXXXXXXXX|XXXXX| | o | e
397 // +-----+--------------+-----+ v w | r
398 // | | |
399 // | | |
400 // +--------------------------+ v
401 //
402 // <--- window --->
403 //
404 //<--------- buffer --------->
405 //
406 // Because the buffer extends past the bottom of the display window, a user
407 // can potentially scroll down beyond what would normally be thought of as the
408 // end of the buffer. Because the buffer can be wider than the display
409 // window, the user can scroll horizontally to view parts of the addressable
410 // area that aren't currently visible.
411 if(GetConsoleScreenBufferInfo(tcache->outhandle, &csbi)){
412 *cols = csbi.dwSize.X;
413 *rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
414 }else{
415 *rows = tcache->default_rows;
416 *cols = tcache->default_cols;
417 }
418#endif
419 if(tcache->dimy != *rows){
420 tcache->dimy = *rows;
421 *cgeo_changed = 1;
422 }
423 if(tcache->dimx != *cols){
424 tcache->dimx = *cols;
425 *cgeo_changed = 1;
426 }
427 if(tcache->sixel_maxy_pristine){
428 int sixelrows = *rows - 1;
429 // if the bottom margin is at least one row, we can draw into the last
430 // row of our visible area. we must leave the true bottom row alone.
431 if(margin_b){
432 ++sixelrows;
433 }
434 tcache->sixel_maxy = sixelrows * tcache->cellpxy;
435 if(tcache->sixel_maxy > tcache->sixel_maxy_pristine){
436 tcache->sixel_maxy = tcache->sixel_maxy_pristine;
437 }
438 }
439 return 0;
440}
441
442// destroy the sprixels of an ncpile (this will not hide the sprixels)
443static void
444free_sprixels(ncpile* n){
445 while(n->sprixelcache){
446 sprixel* tmp = n->sprixelcache->next;
447 sprixel_free(n->sprixelcache);
448 n->sprixelcache = tmp;
449 }
450}
451
452// destroy an empty ncpile. only call with pilelock held.
453static void
454ncpile_destroy(ncpile* pile){
455 if(pile){
456 pile->prev->next = pile->next;
457 pile->next->prev = pile->prev;
458 free_sprixels(pile);
459 free(pile->crender);
460 free(pile);
461 }
462}
463
465 if(p){
466 // ncdirect fakes an ncplane with no ->pile
467 if(ncplane_pile(p)){
469 pthread_mutex_lock(&nc->stats.lock);
471 ncplane_notcurses(p)->stats.s.fbbytes -= sizeof(*p->fb) * p->leny * p->lenx;
472 pthread_mutex_unlock(&nc->stats.lock);
473 if(p->above == NULL && p->below == NULL){
474 pthread_mutex_lock(&nc->pilelock);
475 ncpile_destroy(ncplane_pile(p));
476 pthread_mutex_unlock(&nc->pilelock);
477 }
478 }
479 if(p->widget){
480 void* w = p->widget;
481 void (*wdestruct)(void*) = p->wdestruct;
482 p->widget = NULL;
483 p->wdestruct = NULL;
484 logdebug("calling widget destructor %p(%p)", wdestruct, w);
485 wdestruct(w);
486 logdebug("got the widget");
487 }
488 if(p->sprite){
490 }
491 destroy_tam(p);
492 egcpool_dump(&p->pool);
493 free(p->name);
494 free(p->fb);
495 free(p);
496 }
497}
498
499// create a new ncpile. only call with pilelock held. the return value
500// was assigned to n->pile.
502static ncpile*
503make_ncpile(notcurses* nc, ncplane* n){
504 ncpile* ret = malloc(sizeof(*ret));
505 if(ret){
506 ret->nc = nc;
507 ret->top = n;
508 ret->bottom = n;
509 ret->roots = n;
510 n->bprev = &ret->roots;
511 if(nc->stdplane){ // stdplane (and thus stdpile) has already been created
512 ret->prev = ncplane_pile(nc->stdplane)->prev;
513 ncplane_pile(nc->stdplane)->prev->next = ret;
514 ret->next = ncplane_pile(nc->stdplane);
515 ncplane_pile(nc->stdplane)->prev = ret;
516 }else{
517 ret->prev = ret;
518 ret->next = ret;
519 }
520 n->above = NULL;
521 n->below = NULL;
522 ret->dimy = nc->tcache.dimy;
523 ret->dimx = nc->tcache.dimx;
524 ret->cellpxy = nc->tcache.cellpxy;
525 ret->cellpxx = nc->tcache.cellpxx;
526 ret->crender = NULL;
527 ret->crenderlen = 0;
528 ret->sprixelcache = NULL;
529 ret->scrolls = 0;
530 }
531 n->pile = ret;
532 return ret;
533}
534
535
536static inline size_t ncplane_sizeof_cellarray( size_t rows, size_t cols)
537{
538 // check if multiplication would overflow
539 // calloc will deal with overflow due to sizeof(nccell)
540 if( ! rows || (cols > SIZE_MAX / rows))
541 return 0;
542 return rows * cols;
543}
544
545// create a new ncplane at the specified location (relative to the true screen,
546// having origin at 0,0), having the specified size, and put it at the top of
547// the planestack. its cursor starts at its origin; its style starts as null.
548// a plane may exceed the boundaries of the screen, but must have positive
549// size in both dimensions. bind the plane to 'n', which may be NULL to create
550// a new pile. if bound to a plane instead, this plane moves when that plane
551// moves, and coordinates to move to are relative to that plane.
552// there are two denormalized case we also must handle, that of the "fake"
553// isolated ncplane created by ncdirect for rendering visuals. in that case
554// (and only in that case), nc is NULL (as is n). there's also creation of the
555// initial standard plane, in which case nc is not NULL, but nc->stdplane *is*
556// (as once more is n).
558 const ncplane_options* nopts){
559 if(nopts->flags >= (NCPLANE_OPTION_FIXED << 1u)){
560 logwarn("provided unsupported flags %016" PRIx64, nopts->flags);
561 }
563 if(n == NULL){
564 logerror("alignment requires a parent plane");
565 return NULL;
566 }
567 }
569 if(nopts->rows != 0 || nopts->cols != 0){
570 logerror("geometry specified with margins (r=%u, c=%u)",
571 nopts->rows, nopts->cols);
572 return NULL;
573 }
574 }
575 ncplane* p = malloc(sizeof(*p));
576 if(p == NULL){
577 return NULL;
578 }
582 p->widget = NULL;
583 p->wdestruct = NULL;
585 p->margin_b = nopts->margin_b;
586 p->margin_r = nopts->margin_r;
587 if(n){ // use parent size
588 p->leny = ncplane_dim_y(n);
589 p->lenx = ncplane_dim_x(n);
590 }else{ // use pile size
591 notcurses_term_dim_yx(nc, &p->leny, &p->lenx);
592 }
593 if((p->leny -= p->margin_b) <= 0){
594 p->leny = 1;
595 }
596 if((p->lenx -= p->margin_r) <= 0){
597 p->lenx = 1;
598 }
599 }else{
600 p->leny = nopts->rows;
601 p->lenx = nopts->cols;
602 }
603
604 size_t fbsize = ncplane_sizeof_cellarray(p->leny, p->lenx);
605 if(!fbsize || (p->fb = calloc(fbsize, sizeof(struct nccell))) == NULL){
606 logerror("error allocating cellmatrix (r=%u, c=%u)",
607 p->leny, p->lenx);
608 free(p);
609 return NULL;
610 }
611 p->x = p->y = 0;
612 p->logrow = 0;
613 p->sprite = NULL;
614 p->blist = NULL;
615 p->name = strdup(nopts->name ? nopts->name : "");
618 p->tam = NULL;
619 if(!n){ // new root/standard plane
620 p->absy = nopts->y;
621 p->absx = nopts->x;
622 p->bnext = NULL;
623 p->bprev = NULL;
624 p->boundto = p;
625 }else{ // bound to preexisting pile
626 if(nopts->flags & NCPLANE_OPTION_HORALIGNED){
627 p->absx = ncplane_halign(n, nopts->x, nopts->cols);
628 p->halign = nopts->x;
629 }else{
630 p->absx = nopts->x;
631 }
632 p->absx += n->absx;
633 if(nopts->flags & NCPLANE_OPTION_VERALIGNED){
634 p->absy = ncplane_valign(n, nopts->y, nopts->rows);
635 p->valign = nopts->y;
636 }else{
637 p->absy = nopts->y;
638 }
639 p->absy += n->absy;
640 if( (p->bnext = n->blist) ){
641 n->blist->bprev = &p->bnext;
642 }
643 p->bprev = &n->blist;
644 *p->bprev = p;
645 p->boundto = n;
646 }
647 // FIXME handle top/left margins
648 p->resizecb = nopts->resizecb;
649 p->stylemask = 0;
650 p->channels = 0;
651 egcpool_init(&p->pool);
652 nccell_init(&p->basecell);
653 p->userptr = nopts->userptr;
654 if(nc == NULL){ // fake ncplane backing ncdirect object
655 p->above = NULL;
656 p->below = NULL;
657 p->pile = NULL;
658 }else{
659 pthread_mutex_lock(&nc->pilelock);
660 ncpile* pile = n ? ncplane_pile(n) : NULL;
661 if( (p->pile = pile) ){ // existing pile
662 p->above = NULL;
663 if( (p->below = pile->top) ){ // always happens save initial plane
664 pile->top->above = p;
665 }else{
666 pile->bottom = p;
667 }
668 pile->top = p;
669 }else{ // new pile
670 make_ncpile(nc, p);
671 }
672 pthread_mutex_lock(&nc->stats.lock);
673 nc->stats.s.fbbytes += fbsize;
674 ++nc->stats.s.planes;
675 pthread_mutex_unlock(&nc->stats.lock);
676 pthread_mutex_unlock(&nc->pilelock);
677 }
678 loginfo("created new %ux%u plane \"%s\" @ %dx%d",
679 p->leny, p->lenx, p->name ? p->name : "", p->absy, p->absx);
680 return p;
681}
682
683// create an ncplane of the specified dimensions, but do not yet place it in
684// the z-buffer. clear out all cells. this is for a wholly new context.
685static ncplane*
686create_initial_ncplane(notcurses* nc, int dimy, int dimx){
687 ncplane_options nopts = {
688 .y = 0, .x = 0,
689 .rows = dimy - (nc->margin_t + nc->margin_b),
690 .cols = dimx - (nc->margin_l + nc->margin_r),
691 .userptr = NULL,
692 .name = "std",
693 .resizecb = NULL,
694 .flags = 0,
695 };
696 return nc->stdplane = ncplane_new_internal(nc, NULL, &nopts);
697}
698
700 return nc->stdplane;
701}
702
704 return nc->stdplane;
705}
706
710
712 return ncplane_new_internal(nc, NULL, nopts);
713}
714
716 n->x = 0;
717 n->y = 0;
718}
719
721 if(x < 0){
722 if(x < -1){
723 logerror("negative target x %d", x);
724 return -1;
725 }
726 }else if((unsigned)x >= n->lenx){
727 logerror("target x %d >= width %u", x, n->lenx);
728 return -1;
729 }else{
730 n->x = x;
731 }
732 if(y < 0){
733 if(y < -1){
734 logerror("negative target y %d", y);
735 return -1;
736 }
737 }else if((unsigned)y >= n->leny){
738 logerror("target y %d >= height %u", y, n->leny);
739 return -1;
740 }else{
741 n->y = y;
742 }
743 if(cursor_invalid_p(n)){
744 logerror("invalid cursor following move (%d/%d)", n->y, n->x);
745 return -1;
746 }
747 return 0;
748}
749
751 if((int)n->y + y == -1){
752 logerror("invalid target y -1");
753 return -1;
754 }else if((int)n->x + x == -1){
755 logerror("invalid target x -1");
756 return -1;
757 }else return ncplane_cursor_move_yx(n, n->y + y, n->x + x);
758}
759
760ncplane* ncplane_dup(const ncplane* n, void* opaque){
761 int dimy = n->leny;
762 int dimx = n->lenx;
763 const int placey = n->absy;
764 const int placex = n->absx;
765 struct ncplane_options nopts = {
766 .y = placey,
767 .x = placex,
768 .rows = dimy,
769 .cols = dimx,
770 .userptr = opaque,
771 .name = n->name,
772 .resizecb = ncplane_resizecb(n),
773 .flags = 0,
774 };
775 ncplane* newn = ncplane_create(n->boundto, &nopts);
776 if(newn == NULL){
777 return NULL;
778 }
779 // we don't duplicate sprites...though i'm unsure why not
780 size_t fbsize = sizeof(*n->fb) * dimx * dimy;
781 if(egcpool_dup(&newn->pool, &n->pool)){
783 return NULL;
784 }
785 memmove(newn->fb, n->fb, fbsize);
786 // don't use ncplane_cursor_move_yx() here; the cursor could be in an
787 // invalid location, which will be disallowed, failing out.
788 newn->y = n->y;
789 newn->x = n->x;
790 newn->halign = n->halign;
791 newn->stylemask = ncplane_styles(n);
792 newn->channels = ncplane_channels(n);
793 // we dupd the egcpool, so just dup the goffset
794 newn->basecell = n->basecell;
795 return newn;
796}
797
798// call the resize callback for each bound child in turn. we only need to do
799// the first generation; if they resize, they'll invoke
800// ncplane_resize_internal(), leading to this function being called anew.
802 int ret = 0;
803 for(struct ncplane* child = n->blist ; child ; child = child->bnext){
804 if(child->resizecb){
805 ret |= child->resizecb(child);
806 }
807 }
808 return ret;
809}
810
811// basic consistency checks on resize requests
812static int
813ncplane_resize_internal_check(const ncplane* n, int keepy, int keepx,
814 unsigned keepleny, unsigned keeplenx,
815 int yoff, int xoff, unsigned ylen, unsigned xlen,
816 unsigned* rows, unsigned* cols){
817 if(keepy < 0 || keepx < 0){ // can't start at negative origin
818 logerror("can't retain negative offset %dx%d", keepy, keepx);
819 return -1;
820 }
821 if((!keepleny && keeplenx) || (keepleny && !keeplenx)){ // both must be 0
822 logerror("can't retain null dimension %ux%u", keepleny, keeplenx);
823 return -1;
824 }
825 // can't be smaller than keep length
826 if(ylen < keepleny){
827 logerror("can't map in y dimension: %u < %u", ylen, keepleny);
828 return -1;
829 }
830 if(xlen < keeplenx){
831 logerror("can't map in x dimension: %u < %u", xlen, keeplenx);
832 return -1;
833 }
834 if(ylen <= 0 || xlen <= 0){ // can't resize to trivial or negative size
835 logerror("can't achieve meaningless size %ux%u", ylen, xlen);
836 return -1;
837 }
839 if(keepleny + keepy > *rows){
840 logerror("can't keep %u@%d rows from %u", keepleny, keepy, *rows);
841 return -1;
842 }
843 if(keeplenx + keepx > *cols){
844 logerror("can't keep %u@%d cols from %u", keeplenx, keepx, *cols);
845 return -1;
846 }
847 loginfo("%ux%u @ %d/%d → %u/%u @ %d/%d (want %ux%u@%d/%d)", *rows, *cols,
848 n->absy, n->absx, ylen, xlen, n->absy + keepy + yoff, n->absx + keepx + xoff,
849 keepleny, keeplenx, keepy, keepx);
850 return 0;
851}
852
853// can be used on stdplane, unlike ncplane_resize() which prohibits it.
854int ncplane_resize_internal(ncplane* n, int keepy, int keepx,
855 unsigned keepleny, unsigned keeplenx,
856 int yoff, int xoff,
857 unsigned ylen, unsigned xlen){
858 unsigned rows, cols;
859 if(ncplane_resize_internal_check(n, keepy, keepx, keepleny, keeplenx,
860 yoff, xoff, ylen, xlen,
861 &rows, &cols)){
862 return -1;
863 }
864 if(n->absy == n->absy + keepy && n->absx == n->absx + keepx &&
865 rows == ylen && cols == xlen){
866 return 0;
867 }
869 if(n->sprite){
870 sprixel_hide(n->sprite);
871 }
872 // we're good to resize. we'll need alloc up a new framebuffer, and copy in
873 // those elements we're retaining, zeroing out the rest. alternatively, if
874 // we've shrunk, we will be filling the new structure.
875 int oldarea = rows * cols;
876 int keptarea = keepleny * keeplenx;
877 int newarea = ylen * xlen;
878 size_t fbsize = sizeof(nccell) * newarea;
879 nccell* fb;
880 // there are two cases worth optimizing:
881 //
882 // * nothing is kept. we malloc() a new cellmatrix, dump the EGCpool in
883 // toto, and zero out the matrix. no copies, one memset.
884 // * old and new x dimensions match, and we're keeping the full width.
885 // we release any cells we're about to lose, realloc() the cellmatrix,
886 // and zero out any new cells. so long as the realloc() doesn't move
887 // us, there are no copies, one memset, one iteration (since this is
888 // most often due to autogrowth by a single line, the likelihood that
889 // we remain where we are is pretty high).
890 // * otherwise, we malloc() a new cellmatrix, zero out any new cells,
891 // copy over any reused cells, and release any lost cells. one
892 // gigantic iteration.
893 // we might realloc instead of mallocing, in which case we NULL out
894 // |preserved|. it must otherwise be free()d at the end.
895 nccell* preserved = n->fb;
896 if(cols == xlen && cols == keeplenx && keepleny && !keepy){
897 // we need release the cells that we're losing, lest we leak EGCpool
898 // memory. unfortunately, this means we mutate the plane on the error case.
899 // any solution would involve copying them out first. we only do this if
900 // we're keeping some, as we otherwise drop the EGCpool in toto.
901 if(n->leny > keepleny){
902 for(unsigned y = keepleny ; y < n->leny ; ++y){
903 for(unsigned x = 0 ; x < n->lenx ; ++x){
904 nccell_release(n, ncplane_cell_ref_yx(n, y, x));
905 }
906 }
907 }
908 if((fb = realloc(n->fb, fbsize)) == NULL){
909 return -1;
910 }
911 preserved = NULL;
912 }else{
913 if((fb = malloc(fbsize)) == NULL){
914 return -1;
915 }
916 }
917 if(n->tam){
918 loginfo("tam realloc to %d entries", newarea);
919 // FIXME first, free any disposed auxiliary vectors!
920 tament* tmptam = realloc(n->tam, sizeof(*tmptam) * newarea);
921 if(tmptam == NULL){
922 if(preserved){
923 free(fb);
924 }
925 return -1;
926 }
927 n->tam = tmptam;
928 if(newarea > oldarea){
929 memset(n->tam + oldarea, 0, sizeof(*n->tam) * (newarea - oldarea));
930 }
931 }
932 // update the cursor, if it would otherwise be off-plane
933 if(n->y >= ylen){
934 n->y = ylen - 1;
935 }
936 if(n->x >= xlen){
937 n->x = xlen - 1;
938 }
939 pthread_mutex_lock(&nc->stats.lock);
940 ncplane_notcurses(n)->stats.s.fbbytes -= sizeof(*fb) * (rows * cols);
941 ncplane_notcurses(n)->stats.s.fbbytes += fbsize;
942 pthread_mutex_unlock(&nc->stats.lock);
943 const int oldabsy = n->absy;
944 // go ahead and move. we can no longer fail at this point. but don't yet
945 // resize, because n->len[xy] are used in fbcellidx() in the loop below. we
946 // don't use ncplane_move_yx(), because we want to planebinding-invariant.
947 n->absy += keepy + yoff;
948 n->absx += keepx + xoff;
949//fprintf(stderr, "absx: %d keepx: %d xoff: %d\n", n->absx, keepx, xoff);
950 if(keptarea == 0){ // keep nothing, resize/move only.
951 // if we're keeping nothing, dump the old egcspool. otherwise, we go ahead
952 // and keep it. perhaps we ought compact it?
953 memset(fb, 0, sizeof(*fb) * newarea);
954 egcpool_dump(&n->pool);
955 }else if(!preserved){
956 // the x dimensions are equal, and we're keeping across the width. only the
957 // y dimension changed. if we grew, we need zero out the new cells (if we
958 // shrunk, we already released the old cells prior to the realloc).
959 unsigned tozorch = (ylen - keepleny) * xlen * sizeof(*fb);
960 if(tozorch){
961 unsigned zorchoff = keepleny * xlen;
962 memset(fb + zorchoff, 0, tozorch);
963 }
964 }else{
965 // we currently have maxy rows of maxx cells each. we will be keeping rows
966 // keepy..keepy + keepleny - 1 and columns keepx..keepx + keeplenx - 1.
967 // anything else is zerod out. itery is the row we're writing *to*, and we
968 // must write to each (and every cell in each).
969 for(unsigned itery = 0 ; itery < ylen ; ++itery){
970 int truey = itery + n->absy;
971 int sourceoffy = truey - oldabsy;
972//fprintf(stderr, "sourceoffy: %d keepy: %d ylen: %d\n", sourceoffy, keepy, ylen);
973 // if we have nothing copied to this line, zero it out in one go
974 if(sourceoffy < keepy || sourceoffy >= keepy + (int)keepleny){
975//fprintf(stderr, "writing 0s to line %d of %d\n", itery, ylen);
976 memset(fb + (itery * xlen), 0, sizeof(*fb) * xlen);
977 }else{
978 int copyoff = itery * xlen; // our target at any given time
979 // we do have something to copy, and zero, one, or two regions to zero out
980 unsigned copied = 0;
981 if(xoff < 0){
982 memset(fb + copyoff, 0, sizeof(*fb) * -xoff);
983 copyoff += -xoff;
984 copied += -xoff;
985 }
986 const int sourceidx = nfbcellidx(n, sourceoffy, keepx);
987//fprintf(stderr, "copying line %d (%d) to %d (%d)\n", sourceoffy, sourceidx, copyoff / xlen, copyoff);
988 memcpy(fb + copyoff, preserved + sourceidx, sizeof(*fb) * keeplenx);
989 copyoff += keeplenx;
990 copied += keeplenx;
991 unsigned perline = xlen - copied;
992 for(unsigned x = copyoff ; x < n->lenx ; ++x){
993 nccell_release(n, ncplane_cell_ref_yx(n, sourceoffy, x));
994 }
995 memset(fb + copyoff, 0, sizeof(*fb) * perline);
996 }
997 }
998 }
999 n->fb = fb;
1000 n->lenx = xlen;
1001 n->leny = ylen;
1002 free(preserved);
1004}
1005
1006int ncplane_resize(ncplane* n, int keepy, int keepx,
1007 unsigned keepleny, unsigned keeplenx,
1008 int yoff, int xoff,
1009 unsigned ylen, unsigned xlen){
1010 if(n == ncplane_notcurses(n)->stdplane){
1011//fprintf(stderr, "Can't resize standard plane\n");
1012 return -1;
1013 }
1014 return ncplane_resize_internal(n, keepy, keepx, keepleny, keeplenx,
1015 yoff, xoff, ylen, xlen);
1016}
1017
1019 if(ncp == NULL){
1020 return 0;
1021 }
1022 if(ncplane_notcurses(ncp)->stdplane == ncp){
1023 logerror("won't destroy standard plane");
1024 return -1;
1025 }
1026//notcurses_debug(ncplane_notcurses(ncp), stderr);
1027 loginfo("destroying %dx%d plane \"%s\" @ %dx%d",
1028 ncp->leny, ncp->lenx, ncp->name ? ncp->name : NULL, ncp->absy, ncp->absx);
1029 int ret = 0;
1030 // dissolve our binding from behind (->bprev is either NULL, or its
1031 // predecessor on the bound list's ->bnext, or &ncp->boundto->blist)
1032 if(ncp->bprev){
1033 if( (*ncp->bprev = ncp->bnext) ){
1034 ncp->bnext->bprev = ncp->bprev;
1035 }
1036 }else if(ncp->bnext){
1037 //assert(ncp->boundto->blist == ncp);
1038 ncp->bnext->bprev = NULL;
1039 }
1040 // recursively reparent our children to the plane to which we are bound.
1041 // this will extract each one from the sibling list.
1042 struct ncplane* bound = ncp->blist;
1043 while(bound){
1044 struct ncplane* tmp = bound->bnext;
1045 ncplane* bindto = ((ncp == ncp->boundto) ? bound : ncp->boundto);
1046 if(ncplane_reparent_family(bound, bindto) == NULL){
1047 ret = -1;
1048 }
1049 bound = tmp;
1050 }
1051 // extract ourselves from the z-axis. do this *after* reparenting, in case
1052 // reparenting shifts up the z-axis somehow (though i don't think it can,
1053 // at least not within a pile?).
1054 if(ncp->above){
1055 ncp->above->below = ncp->below;
1056 }else{
1057 ncplane_pile(ncp)->top = ncp->below;
1058 }
1059 if(ncp->below){
1060 ncp->below->above = ncp->above;
1061 }else{
1062 ncplane_pile(ncp)->bottom = ncp->above;
1063 }
1064 free_plane(ncp);
1065 return ret;
1066}
1067
1069 if(ncp == NULL){
1070 return 0;
1071 }
1072 if(ncplane_notcurses(ncp)->stdplane == ncp){
1073 logerror("won't destroy standard plane");
1074 return -1;
1075 }
1076 int ret = 0;
1077 while(ncp->blist){
1078 ret |= ncplane_destroy_family(ncp->blist);
1079 }
1080 ret |= ncplane_destroy(ncp);
1081 return ret;
1082}
1083
1084// it's critical that we're using UTF-8 encoding if at all possible. since the
1085// client might not have called setlocale(2) (if they weren't reading the
1086// directions...), go ahead and try calling setlocale(LC_ALL, "") and then
1087// setlocale(LC_CTYPE, "C.UTF-8") ourselves *iff* we're not using UTF-8 *and*
1088// LANG is not explicitly set to "C" nor "POSIX". this still requires the user
1089// to have a proper locale generated and available on disk. either way, they're
1090// going to get a diagnostic (unless the user has explicitly configured a LANG
1091// of "C" or "POSIX"). recommended practice is for the client code to have
1092// called setlocale() themselves, and set the NCOPTION_INHIBIT_SETLOCALE flag.
1093// if that flag is set, we take the locale and encoding as we get them.
1094void init_lang(void){
1095#ifdef __MINGW32__
1096 if(setlocale(LC_ALL, ".UTF8") == NULL){
1097 logwarn("couldn't set LC_ALL to utf8");
1098 }
1099#endif
1100 const char* encoding = nl_langinfo(CODESET);
1101 if(encoding && encoding_is_utf8(encoding)){
1102 return; // already utf-8, great!
1103 }
1104 const char* lang = getenv("LANG");
1105 // if LANG was explicitly set to C/POSIX, life sucks, roll with it
1106 if(lang && (!strcmp(lang, "C") || !strcmp(lang, "POSIX"))){
1107 loginfo("LANG was explicitly set to %s, not changing locale", lang);
1108 return;
1109 }
1110#ifndef __MINGW32__
1111 if(setlocale(LC_ALL, "") == NULL){
1112 logwarn("setting locale based on LANG failed");
1113 }
1114#endif
1115 encoding = nl_langinfo(CODESET);
1116 if(encoding && encoding_is_utf8(encoding)){
1117 loginfo("set locale from LANG; client should call setlocale(2)!");
1118 return;
1119 }
1120 setlocale(LC_CTYPE, "C.UTF-8");
1121 encoding = nl_langinfo(CODESET);
1122 if(encoding && encoding_is_utf8(encoding)){
1123 loginfo("forced UTF-8 encoding; client should call setlocale(2)!");
1124 return;
1125 }
1126}
1127
1128// initialize a recursive mutex lock in a way that works on both glibc + musl
1129static int
1130recursive_lock_init(pthread_mutex_t *lock){
1131#ifndef __GLIBC__
1132#define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
1133#endif
1134 pthread_mutexattr_t attr;
1135 if(pthread_mutexattr_init(&attr)){
1136 return -1;
1137 }
1138 if(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP)){
1139 pthread_mutexattr_destroy(&attr);
1140 return -1;
1141 }
1142 if(pthread_mutex_init(lock, &attr)){
1143 pthread_mutexattr_destroy(&attr);
1144 return -1;
1145 }
1146 pthread_mutexattr_destroy(&attr);
1147 return 0;
1148#ifndef __GLIBC__
1149#undef PTHREAD_MUTEX_RECURSIVE_NP
1150#endif
1151}
1152
1154 if(nc->tcache.cellpxy == 0 || nc->tcache.cellpxx == 0){
1155 return NCPIXEL_NONE;
1156 }
1157 return nc->tcache.pixel_implementation;
1158}
1159
1160// the earliest stage of initialization. logging does not yet work; all
1161// diagostics should be emitted with fprintf(stderr).
1162// * validates |opts|, if not NULL
1163// * creates and zeroes out the notcurses struct
1164// * ensures that we're using ASCII or UTF8 and calls setlocale(3)
1165// * checks the environment for NOTCURSES_LOGLEVEL and sets ret->loglevel
1166// * writes TERM to the environment, if specified via opts->termtype
1167//
1168// iff we're using UTF8, |utf8| will be set to 1. it is otherwise set to 0.
1169__attribute__ ((nonnull (2))) static notcurses*
1170notcurses_early_init(const struct notcurses_options* opts, FILE* fp, unsigned* utf8){
1171 if(fwide(fp, 0) > 0){
1172 fprintf(stderr, "error: output stream is wide-oriented");
1173 return NULL;
1174 }
1175 notcurses* ret = malloc(sizeof(*ret));
1176 if(ret == NULL){
1177 return ret;
1178 }
1179 memset(ret, 0, sizeof(*ret));
1180 if(opts){
1181 if(opts->flags >= (NCOPTION_SCROLLING << 1u)){
1182 fprintf(stderr, "warning: unknown Notcurses options %016" PRIu64, opts->flags);
1183 }
1184 if(opts->termtype){
1185 if(putenv_term(opts->termtype)){
1186 free(ret);
1187 return NULL;
1188 }
1189 }
1190 ret->flags = opts->flags;
1191 ret->margin_t = opts->margin_t;
1192 ret->margin_b = opts->margin_b;
1193 ret->margin_l = opts->margin_l;
1194 ret->margin_r = opts->margin_r;
1195 ret->loglevel = opts->loglevel;
1196 }
1198 if(!(ret->flags & NCOPTION_INHIBIT_SETLOCALE)){
1199 init_lang();
1200 }
1201//fprintf(stderr, "getenv LC_ALL: %s LC_CTYPE: %s\n", getenv("LC_ALL"), getenv("LC_CTYPE"));
1202 const char* encoding = nl_langinfo(CODESET);
1203 if(encoding && encoding_is_utf8(encoding)){
1204 *utf8 = true;
1205 }else{
1206 *utf8 = false;
1207 if(encoding && (strcmp(encoding, "ANSI_X3.4-1968") &&
1208 strcmp(encoding, "US-ASCII") &&
1209 strcmp(encoding, "ASCII"))){
1210 fprintf(stderr, "encoding (\"%s\") was neither ANSI_X3.4-1968 nor UTF-8, refusing to start\n did you call setlocale()?\n",
1211 encoding ? encoding : "none found");
1212 free(ret);
1213 return NULL;
1214 }
1215 }
1216 ret->cursory = ret->cursorx = -1;
1217 reset_stats(&ret->stats.s);
1219 ret->ttyfp = fp;
1220 egcpool_init(&ret->pool);
1222 fprintf(stderr, "invalid loglevel %d", ret->loglevel);
1223 free(ret);
1224 return NULL;
1225 }
1226 if(recursive_lock_init(&ret->pilelock)){
1227 fprintf(stderr, "couldn't initialize pile mutex");
1228 free(ret);
1229 return NULL;
1230 }
1231 if(pthread_mutex_init(&ret->stats.lock, NULL)){
1232 pthread_mutex_destroy(&ret->pilelock);
1233 free(ret);
1234 return NULL;
1235 }
1236 if(*utf8){
1238 }
1239 return ret;
1240}
1241
1243 if(outfp == NULL){
1244 outfp = stdout;
1245 }
1246 unsigned utf8;
1247 // ret comes out entirely zero-initialized
1248 notcurses* ret = notcurses_early_init(opts, outfp, &utf8);
1249 if(ret == NULL){
1250 return NULL;
1251 }
1252 // the fbuf is needed by notcurses_stop_minimal, so this must be done
1253 // before registering fatal signal handlers.
1254 if(fbuf_init(&ret->rstate.f)){
1255 pthread_mutex_destroy(&ret->pilelock);
1256 pthread_mutex_destroy(&ret->stats.lock);
1257 free(ret);
1258 return NULL;
1259 }
1262 notcurses_stop_minimal)){
1263 fbuf_free(&ret->rstate.f);
1264 pthread_mutex_destroy(&ret->pilelock);
1265 pthread_mutex_destroy(&ret->stats.lock);
1266 free(ret);
1267 return NULL;
1268 }
1269 // don't set loglevel until we've acquired the signal handler, lest we
1270 // change the loglevel out from under a running instance
1271 loglevel = ret->loglevel;
1272 ret->rstate.logendy = -1;
1273 ret->rstate.logendx = -1;
1274 ret->rstate.x = ret->rstate.y = -1;
1275 int fakecursory = ret->rstate.logendy;
1276 int fakecursorx = ret->rstate.logendx;
1277 int* cursory = ret->flags & NCOPTION_PRESERVE_CURSOR ?
1278 &ret->rstate.logendy : &fakecursory;
1279 int* cursorx = ret->flags & NCOPTION_PRESERVE_CURSOR ?
1280 &ret->rstate.logendx : &fakecursorx;
1281 if(interrogate_terminfo(&ret->tcache, ret->ttyfp, utf8,
1284 cursory, cursorx, &ret->stats,
1285 ret->margin_l, ret->margin_t,
1286 ret->margin_r, ret->margin_b,
1287 ret->flags & NCOPTION_DRAIN_INPUT)){
1288 void* altstack;
1289 fbuf_free(&ret->rstate.f);
1290 pthread_mutex_destroy(&ret->pilelock);
1291 pthread_mutex_destroy(&ret->stats.lock);
1292 drop_signals(ret, &altstack);
1293 free(ret);
1294 free(altstack);
1295 return NULL;
1296 }
1297 if(ret->tcache.maxpaletteread > -1){
1298 memcpy(ret->palette.chans, ret->tcache.originalpalette.chans,
1299 sizeof(*ret->palette.chans) * (ret->tcache.maxpaletteread + 1));
1300 }
1301 if((ret->flags & NCOPTION_PRESERVE_CURSOR) ||
1302 (!(ret->flags & NCOPTION_SUPPRESS_BANNERS))){
1303 // the u7 led the queries so that we would get a cursor position
1304 // unaffected by any query spill (unconsumed control sequences). move
1305 // us back to that location, in case there was any such spillage.
1306 if(*cursory < 0 || *cursorx < 0){
1307 unsigned cy, cx;
1308 if(locate_cursor(&ret->tcache, &cy, &cx)){
1309 logwarn("couldn't preserve cursor");
1310 }else{
1311 *cursory = cy;
1312 *cursorx = cx;
1313 }
1314 }
1315 if(*cursory >= 0 && *cursorx >= 0){
1316 if(goto_location(ret, &ret->rstate.f, *cursory, *cursorx, NULL)){
1317 goto err;
1318 }
1319 }
1320 }
1321 unsigned dimy, dimx, cgeo, pgeo; // latter two are don't-cares
1322 if(update_term_dimensions(&dimy, &dimx, &ret->tcache, ret->margin_b, &cgeo, &pgeo)){
1323 goto err;
1324 }
1325 if(ncvisual_init(ret->loglevel)){
1326 goto err;
1327 }
1328 ret->stdplane = NULL;
1329 if((ret->stdplane = create_initial_ncplane(ret, dimy, dimx)) == NULL){
1330 logpanic("couldn't create the initial plane (bad margins?)");
1331 goto err;
1332 }
1333 if(ret->flags & NCOPTION_SCROLLING){
1334 ncplane_set_scrolling(ret->stdplane, true);
1335 }
1336 reset_term_attributes(&ret->tcache, &ret->rstate.f);
1337 const char* cinvis = get_escape(&ret->tcache, ESCAPE_CIVIS);
1338 if(cinvis && fbuf_emit(&ret->rstate.f, cinvis) < 0){
1339 free_plane(ret->stdplane);
1340 goto err;
1341 }
1342 const char* pushcolors = get_escape(&ret->tcache, ESCAPE_SAVECOLORS);
1343 if(pushcolors && fbuf_emit(&ret->rstate.f, pushcolors)){
1344 free_plane(ret->stdplane);
1345 goto err;
1346 }
1347 init_banner(ret, &ret->rstate.f);
1348 if(fbuf_flush(&ret->rstate.f, ret->ttyfp) < 0){
1349 free_plane(ret->stdplane);
1350 goto err;
1351 }
1352 if(ret->rstate.logendy >= 0){ // if either is set, both are
1353 if(!(ret->flags & NCOPTION_SUPPRESS_BANNERS) && ret->tcache.ttyfd >= 0){
1354 unsigned uendy, uendx;
1355 if(locate_cursor(&ret->tcache, &uendy, &uendx)){
1356 free_plane(ret->stdplane);
1357 goto err;
1358 }
1359 ret->rstate.logendy = uendy;
1360 ret->rstate.logendx = uendx;
1361 }
1364 }
1365 }
1366 if(!(ret->flags & NCOPTION_NO_ALTERNATE_SCREEN)){
1367 // perform an explicit clear since the alternate screen was requested
1368 // (smcup *might* clear, but who knows? and it might not have been
1369 // available in any case).
1370 if(clear_and_home(ret, &ret->tcache, &ret->rstate.f)){
1371 goto err;
1372 }
1373 // no need to reestablish a preserved cursor -- that only affects the
1374 // standard plane, not the physical cursor that was just disrupted.
1375 }
1376 // the sprite clear ought take place within the alternate screen, if it's
1377 // being used.
1378 if(!(ret->flags & NCOPTION_NO_CLEAR_BITMAPS)){
1379 if(sprite_clear_all(&ret->tcache, &ret->rstate.f)){
1380 goto err;
1381 }
1382 }
1383 if(ret->rstate.f.used){
1384 if(fbuf_flush(&ret->rstate.f, ret->ttyfp) < 0){
1385 goto err;
1386 }
1387 }
1388 return ret;
1389
1390err:{
1391 void* altstack;
1392 logpanic("alas, you will not be going to space today.");
1393 notcurses_stop_minimal(ret, &altstack);
1394 fbuf_free(&ret->rstate.f);
1395 if(ret->tcache.ttyfd >= 0 && ret->tcache.tpreserved){
1396 (void)tcsetattr(ret->tcache.ttyfd, TCSAFLUSH, ret->tcache.tpreserved);
1397 free(ret->tcache.tpreserved);
1398 }
1399 del_curterm(cur_term);
1400 pthread_mutex_destroy(&ret->stats.lock);
1401 pthread_mutex_destroy(&ret->pilelock);
1402 free(ret);
1403 free(altstack);
1404 return NULL;
1405 }
1406}
1407
1408// updates *pile to point at (*pile)->next, frees all but standard pile/plane
1409static void
1410ncpile_drop(notcurses* nc, ncpile** pile){
1411 bool sawstdplane = false;
1412 ncpile* next = (*pile)->next;
1413 ncplane* p = (*pile)->top;
1414 while(p){
1415 ncplane* tmp = p->below;
1416 logdebug("killing plane %p, next is %p", p, tmp);
1417 if(nc->stdplane != p){
1418 free_plane(p);
1419 }else{
1420 sawstdplane = true;
1421 }
1422 p = tmp;
1423 }
1424 *pile = next;
1425 if(sawstdplane){
1426 ncplane_pile(nc->stdplane)->top = nc->stdplane;
1427 ncplane_pile(nc->stdplane)->bottom = nc->stdplane;
1428 nc->stdplane->above = nc->stdplane->below = NULL;
1429 nc->stdplane->blist = NULL;
1430 }
1431}
1432
1433// drop all piles and all planes, save the standard plane and its pile
1435 logdebug("we have some planes");
1436 pthread_mutex_lock(&nc->pilelock);
1437 ncpile* p = ncplane_pile(nc->stdplane);
1438 ncpile* p0 = p;
1439 do{
1440 ncpile_drop(nc, &p);
1441 }while(p0 != p);
1442 pthread_mutex_unlock(&nc->pilelock);
1443 logdebug("all planes dropped");
1444}
1445
1447 logdebug("stopping notcurses");
1448//notcurses_debug(nc, stderr);
1449 int ret = 0;
1450 if(nc){
1451 void* altstack;
1452 ret |= notcurses_stop_minimal(nc, &altstack);
1453 // if we were not using the alternate screen, our cursor's wherever we last
1454 // wrote. move it to the furthest place to which it advanced.
1455 if(!get_escape(&nc->tcache, ESCAPE_SMCUP)){
1456 fbuf_reset(&nc->rstate.f);
1457//fprintf(stderr, "CLOSING TO %d/%d\n", nc->rstate.logendy, nc->rstate.logendx);
1458 goto_location(nc, &nc->rstate.f, nc->rstate.logendy, nc->rstate.logendx, NULL);
1459//fprintf(stderr, "***"); fflush(stderr);
1460 fbuf_finalize(&nc->rstate.f, stdout);
1461 }
1462 if(nc->stdplane){
1464 free_plane(nc->stdplane);
1465 }
1466 if(nc->tcache.ttyfd >= 0){
1467 ret |= close(nc->tcache.ttyfd);
1468 }
1469 egcpool_dump(&nc->pool);
1470 free(nc->lastframe);
1471 // perhaps surprisingly, this stops the input thread
1473 // get any current stats loaded into stash_stats
1475 if(!(nc->flags & NCOPTION_SUPPRESS_BANNERS)){
1476 summarize_stats(nc);
1477 }
1478#ifndef __MINGW32__
1479 del_curterm(cur_term);
1480#endif
1481 ret |= pthread_mutex_destroy(&nc->stats.lock);
1482 ret |= pthread_mutex_destroy(&nc->pilelock);
1483 fbuf_free(&nc->rstate.f);
1484 free(nc);
1485 free(altstack);
1486 }
1487 return ret;
1488}
1489
1490uint64_t ncplane_channels(const ncplane* n){
1491 return ncchannels_channels(n->channels);
1492}
1493
1495 ncchannels_set_channels(&n->channels, channels);
1496}
1497
1498uint16_t ncplane_styles(const ncplane* n){
1499 return n->stylemask;
1500}
1501
1503 ncchannels_set_fg_default(&n->channels);
1504}
1505
1507 ncchannels_set_bg_default(&n->channels);
1508}
1509
1510void ncplane_set_bg_rgb8_clipped(ncplane* n, int r, int g, int b){
1511 ncchannels_set_bg_rgb8_clipped(&n->channels, r, g, b);
1512}
1513
1514int ncplane_set_bg_rgb8(ncplane* n, unsigned r, unsigned g, unsigned b){
1515 return ncchannels_set_bg_rgb8(&n->channels, r, g, b);
1516}
1517
1518void ncplane_set_fg_rgb8_clipped(ncplane* n, int r, int g, int b){
1519 ncchannels_set_fg_rgb8_clipped(&n->channels, r, g, b);
1520}
1521
1522int ncplane_set_fg_rgb8(ncplane* n, unsigned r, unsigned g, unsigned b){
1523 return ncchannels_set_fg_rgb8(&n->channels, r, g, b);
1524}
1525
1526int ncplane_set_fg_rgb(ncplane* n, unsigned channel){
1527 return ncchannels_set_fg_rgb(&n->channels, channel);
1528}
1529
1530uint64_t ncplane_set_bchannel(ncplane* n, uint32_t channel){
1531 return ncchannels_set_bchannel(&n->channels, channel);
1532}
1533
1534uint64_t ncplane_set_fchannel(ncplane* n, uint32_t channel){
1535 return ncchannels_set_fchannel(&n->channels, channel);
1536}
1537
1538int ncplane_set_bg_rgb(ncplane* n, unsigned channel){
1539 return ncchannels_set_bg_rgb(&n->channels, channel);
1540}
1541
1543 return ncchannels_set_fg_alpha(&n->channels, alpha);
1544}
1545
1547 return ncchannels_set_bg_alpha(&n->channels, alpha);
1548}
1549
1551 return ncchannels_set_fg_palindex(&n->channels, idx);
1552}
1553
1555 return ncchannels_set_bg_palindex(&n->channels, idx);
1556}
1557
1559 if(nccell_wide_right_p(c)){
1560 return -1;
1561 }
1562 return nccell_duplicate(ncp, &ncp->basecell, c);
1563}
1564
1565int ncplane_set_base(ncplane* ncp, const char* egc, uint16_t stylemask, uint64_t channels){
1566 return nccell_prime(ncp, &ncp->basecell, egc, stylemask, channels);
1567}
1568
1570 return nccell_duplicate(ncp, c, &ncp->basecell);
1571}
1572
1573const char* nccell_extended_gcluster(const ncplane* n, const nccell* c){
1574 if(cell_simple_p(c)){
1575 return (const char*)&c->gcluster;
1576 }
1577 return egcpool_extended_gcluster(&n->pool, c);
1578}
1579
1580// 'n' ends up above 'above'
1581int ncplane_move_above(ncplane* restrict n, ncplane* restrict above){
1582 if(n == above){ // probably gets optimized out =/
1583 return -1;
1584 }
1585 ncpile* p = ncplane_pile(n);
1586 if(above == NULL){
1587 if(n->below){
1588 if( (n->below->above = n->above) ){
1589 n->above->below = n->below;
1590 }else{
1591 p->top = n->below;
1592 }
1593 n->below = NULL;
1594 if( (n->above = p->bottom) ){
1595 n->above->below = n;
1596 }
1597 p->bottom = n;
1598 }
1599 return 0;
1600 }
1601 if(n->below != above){
1602 if(p != ncplane_pile(above)){ // can't move among piles
1603 return -1;
1604 }
1605 // splice out 'n'
1606 if(n->below){
1607 n->below->above = n->above;
1608 }else{
1609 p->bottom = n->above;
1610 }
1611 if(n->above){
1612 n->above->below = n->below;
1613 }else{
1614 p->top = n->below;
1615 }
1616 if( (n->above = above->above) ){
1617 above->above->below = n;
1618 }else{
1619 p->top = n;
1620 }
1621 above->above = n;
1622 n->below = above;
1623 }
1624 return 0;
1625}
1626
1627// 'n' ends up below 'below', or on top if 'below' == NULL
1628int ncplane_move_below(ncplane* restrict n, ncplane* restrict below){
1629 if(n == below){ // probably gets optimized out =/
1630 return -1;
1631 }
1632 ncpile* p = ncplane_pile(n);
1633 if(below == NULL){
1634 if(n->above){
1635 if( (n->above->below = n->below) ){
1636 n->below->above = n->above;
1637 }else{
1638 p->bottom = n->above;
1639 }
1640 n->above = NULL;
1641 if( (n->below = p->top) ){
1642 n->below->above = n;
1643 }
1644 p->top = n;
1645 }
1646 return 0;
1647 }
1648 if(n->above != below){
1649 if(p != ncplane_pile(below)){ // can't move among piles
1650 return -1;
1651 }
1652 if(n->below){
1653 n->below->above = n->above;
1654 }else{
1655 p->bottom = n->above;
1656 }
1657 if(n->above){
1658 n->above->below = n->below;
1659 }else{
1660 p->top = n->below;
1661 }
1662 if( (n->below = below->below) ){
1663 below->below->above = n;
1664 }else{
1665 p->bottom = n;
1666 }
1667 below->below = n;
1668 n->above = below;
1669 }
1670 return 0;
1671}
1672
1673// if above is NULL, we're moving to the bottom
1674int ncplane_move_family_above(ncplane* restrict n, ncplane* restrict bpoint){
1677 if(ncplane_move_above(n, bpoint)){
1678 return -1;
1679 }
1680 // traverse the planes above n, until we hit NULL. do the planes above n
1681 // first, so that we know the topmost element of our new ensplicification.
1682 // at this point, n is the bottommost plane, and we're inserting above it.
1683 ncplane* targ = n;
1684 while(above && above != n){
1685 ncplane* tmp = ncplane_above(above);
1686 if(ncplane_descendant_p(above, n)){
1688 targ = above;
1689 }
1690 above = tmp;
1691 }
1692 // n remains the topmost plane, and we're inserting above it. we have to be
1693 // careful this time not to cross into any we moved below n.
1694 const ncplane* topmost = targ;
1695 targ = n;
1696 while(below && below != topmost){
1697 ncplane* tmp = ncplane_below(below);
1698 if(ncplane_descendant_p(below, n)){
1700 targ = below;
1701 }
1702 below = tmp;
1703 }
1704 return 0;
1705}
1706
1707// if below is NULL, we're moving to the top
1708int ncplane_move_family_below(ncplane* restrict n, ncplane* restrict bpoint){
1711 if(ncplane_move_below(n, bpoint)){
1712 return -1;
1713 }
1714 // traverse the planes below n, until we hit NULL. do the planes below n
1715 // first, so that we know the bottommost element of our new ensplicification.
1716 // we're inserting below n...
1717 ncplane* targ = n;
1718 while(below && below != n){
1719 ncplane* tmp = ncplane_below(below);
1720 if(ncplane_descendant_p(below, n)){
1722 targ = below;
1723 }
1724 below = tmp;
1725 }
1726 // n remains the topmost plane, and we're inserting above it. we have to be
1727 // careful this time not to cross into any we moved below n.
1728 const ncplane* bottommost = targ;
1729 targ = n;
1730 while(above && above != bottommost){
1731 ncplane* tmp = ncplane_above(above);
1732 if(ncplane_descendant_p(above, n)){
1734 targ = above;
1735 }
1736 above = tmp;
1737 }
1738 return 0;
1739}
1740
1741void ncplane_cursor_yx(const ncplane* n, unsigned* y, unsigned* x){
1742 if(y){
1743 *y = n->y;
1744 }
1745 if(x){
1746 *x = n->x;
1747 }
1748}
1749
1750static inline void
1751nccell_obliterate(ncplane* n, nccell* c){
1752 nccell_release(n, c);
1753 nccell_init(c);
1754}
1755
1756// increment y by 1 and rotate the framebuffer up one line. x moves to 0. any
1757// non-fixed bound planes move up 1 line if they intersect the plane.
1759//fprintf(stderr, "pre-scroll: %d/%d %d/%d log: %d scrolling: %u\n", n->y, n->x, n->leny, n->lenx, n->logrow, n->scrolling);
1760 n->x = 0;
1761 if(n->y == n->leny - 1){
1762 // we're on the last line of the plane
1763 if(n->autogrow){
1764 ncplane_resize_simple(n, n->leny + 1, n->lenx);
1765 ncplane_cursor_move_yx(n, n->leny - 1, 0);
1766 return;
1767 }
1768 // we'll actually be scrolling material up and out, and making a new line.
1769 // if this is the standard plane, that means a "physical" scroll event is
1770 // called for.
1772 ncplane_pile(n)->scrolls++;
1773 }
1774 n->logrow = (n->logrow + 1) % n->leny;
1775 nccell* row = n->fb + nfbcellidx(n, n->y, 0);
1776 for(unsigned clearx = 0 ; clearx < n->lenx ; ++clearx){
1777 nccell_release(n, &row[clearx]);
1778 }
1779 memset(row, 0, sizeof(*row) * n->lenx);
1780 for(struct ncplane* c = n->blist ; c ; c = c->bnext){
1781 if(!c->fixedbound){
1782 if(ncplanes_intersect_p(n, c)){
1783 ncplane_move_rel(c, -1, 0);
1784 }
1785 }
1786 }
1787 }else{
1788 ++n->y;
1789 }
1790}
1791
1793 if(!ncplane_scrolling_p(n)){
1794 logerror("can't scroll %d on non-scrolling plane", r);
1795 return -1;
1796 }
1797 if(r < 0){
1798 logerror("can't scroll %d lines", r);
1799 return -1;
1800 }
1801 while(r-- > 0){
1802 scroll_down(n);
1803 }
1805 notcurses_render(ncplane_notcurses(n));
1806 }
1807 return 0;
1808}
1809
1810// Scroll |n| up until |child| is no longer hidden beneath it. Returns an
1811// error if |child| is not a child of |n|, or |n| is not scrolling, or |child|
1812// is fixed. Returns the number of scrolling events otherwise (might be 0).
1814 if(!ncplane_descendant_p(child, n)){
1815 logerror("not a descendant of specified plane");
1816 return -1;
1817 }
1818 if(child->fixedbound){
1819 logerror("child plane is fixed");
1820 return -1;
1821 }
1822 int parend = ncplane_abs_y(n) + ncplane_dim_y(n) - 1; // where parent ends
1823 int chend = ncplane_abs_y(child) + ncplane_dim_y(child) - 1; // where child ends
1824 if(chend <= parend){
1825 return 0;
1826 }
1827 int r = chend - parend; // how many rows we need scroll parent
1828 ncplane_cursor_move_yx(n, ncplane_dim_y(n) - 1, 0);
1829 int ret = ncplane_scrollup(n, r);
1830 return ret;
1831}
1832
1833int nccell_load(ncplane* n, nccell* c, const char* gcluster){
1834 int cols;
1835 int bytes = utf8_egc_len(gcluster, &cols);
1836 return pool_load_direct(&n->pool, c, gcluster, bytes, cols);
1837}
1838
1839// where the magic happens. write the single EGC completely described by |egc|,
1840// occupying |cols| columns, to the ncplane |n| at the coordinate |y|, |x|. if
1841// either or both of |y|/|x| is -1, the current cursor location for that
1842// dimension will be used. if the glyph cannot fit on the current line, it is
1843// an error unless scrolling is enabled.
1844static int
1845ncplane_put(ncplane* n, int y, int x, const char* egc, int cols,
1846 uint16_t stylemask, uint64_t channels, int bytes){
1847 if(n->sprite){
1848 logerror("can't write [%s] to sprixelated plane", egc);
1849 return -1;
1850 }
1851 // reject any control character for output other than newline (and then only
1852 // on a scrolling plane) and tab.
1853 if(is_control_egc((const unsigned char*)egc, bytes)){
1854 if(*egc == '\n'){
1855 // if we're not scrolling, autogrow would be to the right (as opposed to
1856 // down), and thus it still wouldn't apply to the case of a newline.
1857 if(!n->scrolling){
1858 logerror("rejecting newline on non-scrolling plane");
1859 return -1;
1860 }
1861 }else if(*egc != '\t'){
1862 logerror("rejecting %dB control character", bytes);
1863 return -1;
1864 }
1865 }
1866 // check *before ncplane_cursor_move_yx()* whether we're past the end of the
1867 // line. if scrolling is enabled, move to the next line if so. if x or y are
1868 // specified, we must always try to print at exactly that location, and
1869 // there's no need to check the present location in that dimension.
1870 bool linesend = false;
1871 if(x < 0){
1872 // we checked x for all negatives, but only -1 is valid (our else clause is
1873 // predicated on a non-negative x).
1874 if(x == -1){
1875 if(n->x + cols - 1 >= n->lenx){
1876 linesend = true;
1877 }
1878 }
1879 }else{
1880 if((unsigned)x + cols - 1 >= n->lenx){
1881 linesend = true;
1882 }
1883 }
1884 bool scrolled = false;
1885 if(linesend){
1886 if(n->scrolling){
1887 scroll_down(n);
1888 scrolled = true;
1889 }else if(n->autogrow){
1890 ncplane_resize_simple(n, n->leny, n->lenx + cols);
1891 }else{
1892 logerror("target x %d [%.*s] > length %d", n->x, bytes, egc, n->lenx);
1893 return -1;
1894 }
1895 }
1896 // targets outside the plane will be rejected here.
1897 if(ncplane_cursor_move_yx(n, y, x)){
1898 return -1;
1899 }
1900 if(*egc == '\n'){
1901 scroll_down(n);
1902 scrolled = true;
1903 }
1904 // A wide character obliterates anything to its immediate right (and marks
1905 // that cell as wide). Any character placed atop one cell of a wide character
1906 // obliterates all cells. Note that a two-cell glyph can thus obliterate two
1907 // other two-cell glyphs, totalling four columns.
1908 nccell* targ = ncplane_cell_ref_yx(n, n->y, n->x);
1909 // we're always starting on the leftmost cell of our output glyph. check the
1910 // target, and find the leftmost cell of the glyph it will be displacing.
1911 // obliterate as we go along.
1912 int idx = n->x;
1913 nccell* lmc = targ;
1914 while(nccell_wide_right_p(lmc)){
1915 nccell_obliterate(n, &n->fb[nfbcellidx(n, n->y, idx)]);
1916 lmc = ncplane_cell_ref_yx(n, n->y, --idx);
1917 }
1918 // we're now on the leftmost cell of the target glyph.
1919 int twidth = nccell_cols(lmc);
1920 nccell_release(n, lmc);
1921 twidth -= n->x - idx;
1922 while(--twidth > 0){
1923 nccell_obliterate(n, &n->fb[nfbcellidx(n, n->y, n->x + twidth)]);
1924 }
1925 targ->stylemask = stylemask;
1926 targ->channels = channels;
1927 // tabs get replaced with spaces, up to the next tab stop. we always try to
1928 // write at least one. if there are no more tabstops on the current line, if
1929 // autogrowing to the right, extend as necessary. otherwise, if scrolling,
1930 // move to the next line. otherwise, simply fill any spaces we can. this has
1931 // already taken place by the time we get here, if it ought have happened.
1932 if(*egc == '\t'){
1933 cols = TABSTOP - (n->x % TABSTOP);
1934 if(n->x + 1 >= n->lenx){
1935 if(!n->scrolling && n->autogrow){
1936 ncplane_resize_simple(n, n->leny, n->lenx + (cols ? cols - 1 : TABSTOP - 1));
1937 // must refresh targ; resize invalidated it
1938 targ = ncplane_cell_ref_yx(n, n->y, n->x);
1939 }
1940 }
1941 if(cell_load_direct(n, targ, " ", bytes, 1) < 0){
1942 return -1;
1943 }
1944 }else{
1945 if(cell_load_direct(n, targ, egc, bytes, cols) < 0){
1946 return -1;
1947 }
1948 }
1949//fprintf(stderr, "%08x %016lx %c %d %d\n", targ->gcluster, targ->channels, nccell_double_wide_p(targ) ? 'D' : 'd', bytes, cols);
1950 if(*egc != '\n'){
1951 ++n->x;
1952 // if wide, set our right hand columns wide, and check for further damage
1953 for(int i = 1 ; i < cols ; ++i){
1954 nccell* candidate = &n->fb[nfbcellidx(n, n->y, n->x)];
1955 int off = nccell_cols(candidate);
1956 nccell_release(n, &n->fb[nfbcellidx(n, n->y, n->x)]);
1957 while(--off > 0){
1958 nccell_obliterate(n, &n->fb[nfbcellidx(n, n->y, n->x + off)]);
1959 }
1960 if(*egc != '\t'){
1961 candidate->channels = targ->channels;
1962 candidate->stylemask = targ->stylemask;
1963 candidate->width = targ->width;
1964 }else{
1965 if(cell_load_direct(n, candidate, " ", bytes, 1) < 0){
1966 return -1;
1967 }
1968 }
1969 ++n->x;
1970 }
1971 }
1972 if(scrolled){
1974 notcurses_render(ncplane_notcurses(n));
1975 }
1976 }
1977 return cols;
1978}
1979
1980int ncplane_putc_yx(ncplane* n, int y, int x, const nccell* c){
1981 const int cols = nccell_cols(c);
1982 // unfortunately, |c| comes from |n|. the act of writing |c| to |n| could
1983 // grow |n|'s egcpool, invalidating the reference represented by
1984 // nccell_extended_gcluster(). so we must copy and free it.
1985 char* egc = nccell_strdup(n, c);
1986 if(egc == NULL){
1987 logerror("couldn't duplicate cell");
1988 return -1;
1989 }
1990 int r = ncplane_put(n, y, x, egc, cols, c->stylemask, c->channels, strlen(egc));
1991 free(egc);
1992 return r;
1993}
1994
1995int ncplane_putegc_yx(ncplane* n, int y, int x, const char* gclust, size_t* sbytes){
1996 int cols;
1997 int bytes = utf8_egc_len(gclust, &cols);
1998 if(bytes < 0){
1999 return -1;
2000 }
2001 if(sbytes){
2002 *sbytes = bytes;
2003 }
2004//fprintf(stderr, "glust: %s cols: %d wcs: %d\n", gclust, cols, bytes);
2005 return ncplane_put(n, y, x, gclust, cols, n->stylemask, n->channels, bytes);
2006}
2007
2009 uint64_t channels = n->channels;
2010 uint16_t stylemask = n->stylemask;
2011 const nccell* targ = &n->fb[nfbcellidx(n, n->y, n->x)];
2012 n->channels = targ->channels;
2013 n->stylemask = targ->stylemask;
2014 int ret = ncplane_putchar(n, c);
2015 n->channels = channels;
2016 n->stylemask = stylemask;
2017 return ret;
2018}
2019
2020int ncplane_putwegc_stained(ncplane* n, const wchar_t* gclust, size_t* sbytes){
2021 uint64_t channels = n->channels;
2022 uint16_t stylemask = n->stylemask;
2023 const nccell* targ = &n->fb[nfbcellidx(n, n->y, n->x)];
2024 n->channels = targ->channels;
2025 n->stylemask = targ->stylemask;
2026 int ret = ncplane_putwegc(n, gclust, sbytes);
2027 n->channels = channels;
2028 n->stylemask = stylemask;
2029 return ret;
2030}
2031
2032int ncplane_putegc_stained(ncplane* n, const char* gclust, size_t* sbytes){
2033 uint64_t channels = n->channels;
2034 uint16_t stylemask = n->stylemask;
2035 const nccell* targ = &n->fb[nfbcellidx(n, n->y, n->x)];
2036 n->channels = targ->channels;
2037 n->stylemask = targ->stylemask;
2038 int ret = ncplane_putegc(n, gclust, sbytes);
2039 n->channels = channels;
2040 n->stylemask = stylemask;
2041 return ret;
2042}
2043
2044int ncplane_cursor_at(const ncplane* n, nccell* c, char** gclust){
2045 if(n->y >= n->leny || n->x >= n->lenx){
2046 return -1;
2047 }
2048 const nccell* src = &n->fb[nfbcellidx(n, n->y, n->x)];
2049 memcpy(c, src, sizeof(*src));
2050 if(cell_simple_p(c)){
2051 *gclust = NULL;
2052 }else if((*gclust = strdup(nccell_extended_gcluster(n, src))) == NULL){
2053 return -1;
2054 }
2055 return 0;
2056}
2057
2059 return term_supported_styles(&nc->tcache);
2060}
2061
2063 return nc->tcache.caps.colors;
2064}
2065
2067 return termdesc_longterm(&nc->tcache);
2068}
2069
2070// conform to the specified stylebits
2071void ncplane_set_styles(ncplane* n, unsigned stylebits){
2072 n->stylemask = (stylebits & NCSTYLE_MASK);
2073}
2074
2075// turn on any specified stylebits
2076void ncplane_on_styles(ncplane* n, unsigned stylebits){
2077 n->stylemask |= (stylebits & NCSTYLE_MASK);
2078}
2079
2080// turn off any specified stylebits
2081void ncplane_off_styles(ncplane* n, unsigned stylebits){
2082 n->stylemask &= ~(stylebits & NCSTYLE_MASK);
2083}
2084
2085// i hate the big allocation and two copies here, but eh what you gonna do?
2086// well, for one, we don't need the huge allocation FIXME
2087char* ncplane_vprintf_prep(const char* format, va_list ap){
2088 const size_t size = BUFSIZ; // healthy estimate, can embiggen below
2089 char* buf = malloc(size);
2090 if(buf == NULL){
2091 return NULL;
2092 }
2093 va_list vacopy;
2094 va_copy(vacopy, ap);
2095 int ret = vsnprintf(buf, size, format, ap);
2096 if(ret < 0){
2097 free(buf);
2098 va_end(vacopy);
2099 return NULL;
2100 }
2101 if((size_t)ret >= size){
2102 char* tmp = realloc(buf, ret + 1);
2103 if(tmp == NULL){
2104 free(buf);
2105 va_end(vacopy);
2106 return NULL;
2107 }
2108 buf = tmp;
2109 vsprintf(buf, format, vacopy);
2110 }
2111 va_end(vacopy);
2112 return buf;
2113}
2114
2115int ncplane_vprintf_yx(ncplane* n, int y, int x, const char* format, va_list ap){
2116 char* r = ncplane_vprintf_prep(format, ap);
2117 if(r == NULL){
2118 return -1;
2119 }
2120 int ret = ncplane_putstr_yx(n, y, x, r);
2121 free(r);
2122 return ret;
2123}
2124
2126 const char* format, va_list ap){
2127 char* r = ncplane_vprintf_prep(format, ap);
2128 if(r == NULL){
2129 return -1;
2130 }
2131 int ret = ncplane_putstr_aligned(n, y, align, r);
2132 free(r);
2133 return ret;
2134}
2135
2136int ncplane_vprintf_stained(struct ncplane* n, const char* format, va_list ap){
2137 char* r = ncplane_vprintf_prep(format, ap);
2138 if(r == NULL){
2139 return -1;
2140 }
2141 int ret = ncplane_putstr_stained(n, r);
2142 free(r);
2143 return ret;
2144}
2145
2146int ncplane_putnstr_aligned(struct ncplane* n, int y, ncalign_e align, size_t s, const char* str){
2147 char* chopped = strndup(str, s);
2148 int ret = ncplane_putstr_aligned(n, y, align, chopped);
2149 free(chopped);
2150 return ret;
2151}
2152
2153int ncplane_hline_interp(ncplane* n, const nccell* c, unsigned len,
2154 uint64_t c1, uint64_t c2){
2155 if(len <= 0){
2156 logerror("passed invalid length %u", len);
2157 return -1;
2158 }
2159 unsigned ur, ug, ub;
2160 int r1, g1, b1, r2, g2, b2;
2161 int br1, bg1, bb1, br2, bg2, bb2;
2162 ncchannels_fg_rgb8(c1, &ur, &ug, &ub);
2163 r1 = ur; g1 = ug; b1 = ub;
2164 ncchannels_fg_rgb8(c2, &ur, &ug, &ub);
2165 r2 = ur; g2 = ug; b2 = ub;
2166 ncchannels_bg_rgb8(c1, &ur, &ug, &ub);
2167 br1 = ur; bg1 = ug; bb1 = ub;
2168 ncchannels_bg_rgb8(c2, &ur, &ug, &ub);
2169 br2 = ur; bg2 = ug; bb2 = ub;
2170 int deltr = r2 - r1;
2171 int deltg = g2 - g1;
2172 int deltb = b2 - b1;
2173 int deltbr = br2 - br1;
2174 int deltbg = bg2 - bg1;
2175 int deltbb = bb2 - bb1;
2176 unsigned ret;
2178 if(nccell_duplicate(n, &dupc, c) < 0){
2179 return -1;
2180 }
2181 bool fgdef = false, bgdef = false;
2182 if(ncchannels_fg_default_p(c1) && ncchannels_fg_default_p(c2)){
2183 fgdef = true;
2184 }
2185 if(ncchannels_bg_default_p(c1) && ncchannels_bg_default_p(c2)){
2186 bgdef = true;
2187 }
2188 for(ret = 0 ; ret < len ; ++ret){
2189 int r = (deltr * (int)ret) / (int)len + r1;
2190 int g = (deltg * (int)ret) / (int)len + g1;
2191 int b = (deltb * (int)ret) / (int)len + b1;
2192 int br = (deltbr * (int)ret) / (int)len + br1;
2193 int bg = (deltbg * (int)ret) / (int)len + bg1;
2194 int bb = (deltbb * (int)ret) / (int)len + bb1;
2195 if(!fgdef){
2196 nccell_set_fg_rgb8(&dupc, r, g, b);
2197 }
2198 if(!bgdef){
2199 nccell_set_bg_rgb8(&dupc, br, bg, bb);
2200 }
2201 if(ncplane_putc(n, &dupc) <= 0){
2202 return -1;
2203 }
2204 }
2205 nccell_release(n, &dupc);
2206 return ret;
2207}
2208
2209int ncplane_vline_interp(ncplane* n, const nccell* c, unsigned len,
2210 uint64_t c1, uint64_t c2){
2211 if(len <= 0){
2212 logerror("passed invalid length %u", len);
2213 return -1;
2214 }
2215 unsigned ur, ug, ub;
2216 int r1, g1, b1, r2, g2, b2;
2217 int br1, bg1, bb1, br2, bg2, bb2;
2218 ncchannels_fg_rgb8(c1, &ur, &ug, &ub);
2219 r1 = ur; g1 = ug; b1 = ub;
2220 ncchannels_fg_rgb8(c2, &ur, &ug, &ub);
2221 r2 = ur; g2 = ug; b2 = ub;
2222 ncchannels_bg_rgb8(c1, &ur, &ug, &ub);
2223 br1 = ur; bg1 = ug; bb1 = ub;
2224 ncchannels_bg_rgb8(c2, &ur, &ug, &ub);
2225 br2 = ur; bg2 = ug; bb2 = ub;
2226 int deltr = (r2 - r1) / ((int)len + 1);
2227 int deltg = (g2 - g1) / ((int)len + 1);
2228 int deltb = (b2 - b1) / ((int)len + 1);
2229 int deltbr = (br2 - br1) / ((int)len + 1);
2230 int deltbg = (bg2 - bg1) / ((int)len + 1);
2231 int deltbb = (bb2 - bb1) / ((int)len + 1);
2232 unsigned ypos, xpos;
2233 unsigned ret;
2234 ncplane_cursor_yx(n, &ypos, &xpos);
2236 if(nccell_duplicate(n, &dupc, c) < 0){
2237 return -1;
2238 }
2239 bool fgdef = false, bgdef = false;
2240 if(ncchannels_fg_default_p(c1) && ncchannels_fg_default_p(c2)){
2241 fgdef = true;
2242 }
2243 if(ncchannels_bg_default_p(c1) && ncchannels_bg_default_p(c2)){
2244 bgdef = true;
2245 }
2246 for(ret = 0 ; ret < len ; ++ret){
2247 if(ncplane_cursor_move_yx(n, ypos + ret, xpos)){
2248 return -1;
2249 }
2250 r1 += deltr;
2251 g1 += deltg;
2252 b1 += deltb;
2253 br1 += deltbr;
2254 bg1 += deltbg;
2255 bb1 += deltbb;
2256 if(!fgdef){
2257 nccell_set_fg_rgb8(&dupc, r1, g1, b1);
2258 }
2259 if(!bgdef){
2260 nccell_set_bg_rgb8(&dupc, br1, bg1, bb1);
2261 }
2262 if(ncplane_putc(n, &dupc) <= 0){
2263 return -1;
2264 }
2265 }
2266 nccell_release(n, &dupc);
2267 return ret;
2268}
2269
2270// we must have at least 2x2, or it's an error
2271int ncplane_box(ncplane* n, const nccell* ul, const nccell* ur,
2272 const nccell* ll, const nccell* lr, const nccell* hl,
2273 const nccell* vl, unsigned ystop, unsigned xstop,
2274 unsigned ctlword){
2275 unsigned yoff, xoff;
2276 ncplane_cursor_yx(n, &yoff, &xoff);
2277 // must be at least 2x2, with its upper-left corner at the current cursor
2278 if(ystop < yoff + 1){
2279 logerror("ystop (%u) insufficient for yoff (%d)", ystop, yoff);
2280 return -1;
2281 }
2282 if(xstop < xoff + 1){
2283 logerror("xstop (%u) insufficient for xoff (%d)", xstop, xoff);
2284 return -1;
2285 }
2286 unsigned ymax, xmax;
2287 ncplane_dim_yx(n, &ymax, &xmax);
2288 // must be within the ncplane
2289 if(xstop >= xmax || ystop >= ymax){
2290 logerror("boundary (%ux%u) beyond plane (%dx%d)", ystop, xstop, ymax, xmax);
2291 return -1;
2292 }
2293 unsigned edges;
2294 edges = !(ctlword & NCBOXMASK_TOP) + !(ctlword & NCBOXMASK_LEFT);
2295 if(edges >= box_corner_needs(ctlword)){
2296 if(ncplane_putc(n, ul) < 0){
2297 return -1;
2298 }
2299 }
2300 if(!(ctlword & NCBOXMASK_TOP)){ // draw top border, if called for
2301 if(xstop - xoff >= 2){
2302 if(ncplane_cursor_move_yx(n, yoff, xoff + 1)){
2303 return -1;
2304 }
2305 if(!(ctlword & NCBOXGRAD_TOP)){ // cell styling, hl
2306 if(ncplane_hline(n, hl, xstop - xoff - 1) < 0){
2307 return -1;
2308 }
2309 }else{ // gradient, ul -> ur
2310 if(ncplane_hline_interp(n, hl, xstop - xoff - 1, ul->channels, ur->channels) < 0){
2311 return -1;
2312 }
2313 }
2314 }
2315 }
2316 edges = !(ctlword & NCBOXMASK_TOP) + !(ctlword & NCBOXMASK_RIGHT);
2317 if(edges >= box_corner_needs(ctlword)){
2318 if(ncplane_cursor_move_yx(n, yoff, xstop)){
2319 return -1;
2320 }
2321 if(ncplane_putc(n, ur) < 0){
2322 return -1;
2323 }
2324 }
2325 ++yoff;
2326 // middle rows (vertical lines)
2327 if(yoff < ystop){
2328 if(!(ctlword & NCBOXMASK_LEFT)){
2329 if(ncplane_cursor_move_yx(n, yoff, xoff)){
2330 return -1;
2331 }
2332 if((ctlword & NCBOXGRAD_LEFT)){ // grad styling, ul->ll
2333 if(ncplane_vline_interp(n, vl, ystop - yoff, ul->channels, ll->channels) < 0){
2334 return -1;
2335 }
2336 }else{
2337 if(ncplane_vline(n, vl, ystop - yoff) < 0){
2338 return -1;
2339 }
2340 }
2341 }
2342 if(!(ctlword & NCBOXMASK_RIGHT)){
2343 if(ncplane_cursor_move_yx(n, yoff, xstop)){
2344 return -1;
2345 }
2346 if((ctlword & NCBOXGRAD_RIGHT)){ // grad styling, ur->lr
2347 if(ncplane_vline_interp(n, vl, ystop - yoff, ur->channels, lr->channels) < 0){
2348 return -1;
2349 }
2350 }else{
2351 if(ncplane_vline(n, vl, ystop - yoff) < 0){
2352 return -1;
2353 }
2354 }
2355 }
2356 }
2357 // bottom line
2358 yoff = ystop;
2359 edges = !(ctlword & NCBOXMASK_BOTTOM) + !(ctlword & NCBOXMASK_LEFT);
2360 if(edges >= box_corner_needs(ctlword)){
2361 if(ncplane_cursor_move_yx(n, yoff, xoff)){
2362 return -1;
2363 }
2364 if(ncplane_putc(n, ll) < 0){
2365 return -1;
2366 }
2367 }
2368 if(!(ctlword & NCBOXMASK_BOTTOM)){
2369 if(xstop - xoff >= 2){
2370 if(ncplane_cursor_move_yx(n, yoff, xoff + 1)){
2371 return -1;
2372 }
2373 if(!(ctlword & NCBOXGRAD_BOTTOM)){ // cell styling, hl
2374 if(ncplane_hline(n, hl, xstop - xoff - 1) < 0){
2375 return -1;
2376 }
2377 }else{
2378 if(ncplane_hline_interp(n, hl, xstop - xoff - 1, ll->channels, lr->channels) < 0){
2379 return -1;
2380 }
2381 }
2382 }
2383 }
2384 edges = !(ctlword & NCBOXMASK_BOTTOM) + !(ctlword & NCBOXMASK_RIGHT);
2385 if(edges >= box_corner_needs(ctlword)){
2386 if(ncplane_cursor_move_yx(n, yoff, xstop)){
2387 return -1;
2388 }
2389 if(ncplane_putc(n, lr) < 0){
2390 return -1;
2391 }
2392 }
2393 return 0;
2394}
2395
2396// takes the head of a list of bound planes. performs a DFS on all planes bound
2397// to 'n', and all planes down-list from 'n', moving all *by* 'dy' and 'dx'.
2398static void
2399move_bound_planes(ncplane* n, int dy, int dx){
2400 while(n){
2401 if(n->sprite){
2402 sprixel_movefrom(n->sprite, n->absy, n->absx);
2403 }
2404 n->absy += dy;
2405 n->absx += dx;
2406 move_bound_planes(n->blist, dy, dx);
2407 n = n->bnext;
2408 }
2409}
2410
2411int ncplane_move_yx(ncplane* n, int y, int x){
2412 if(n == ncplane_notcurses(n)->stdplane){
2413 return -1;
2414 }
2415 int dy, dx; // amount moved
2416 if(n->boundto == n){
2417 dy = y - n->absy;
2418 dx = x - n->absx;
2419 }else{
2420 dy = (n->boundto->absy + y) - n->absy;
2421 dx = (n->boundto->absx + x) - n->absx;
2422 }
2423 if(dy || dx){ // don't want to trigger sprixel_movefrom() if unneeded
2424 if(n->sprite){
2425 sprixel_movefrom(n->sprite, n->absy, n->absx);
2426 }
2427 n->absx += dx;
2428 n->absy += dy;
2429 move_bound_planes(n->blist, dy, dx);
2430 }
2431 return 0;
2432}
2433
2434int ncplane_y(const ncplane* n){
2435 if(n->boundto == n){
2436 return n->absy;
2437 }
2438 return n->absy - n->boundto->absy;
2439}
2440
2441int ncplane_x(const ncplane* n){
2442 if(n->boundto == n){
2443 return n->absx;
2444 }
2445 return n->absx - n->boundto->absx;
2446}
2447
2448void ncplane_yx(const ncplane* n, int* y, int* x){
2449 if(y){
2450 *y = ncplane_y(n);
2451 }
2452 if(x){
2453 *x = ncplane_x(n);
2454 }
2455}
2456
2457// special case of ncplane_erase_region()
2459 loginfo("erasing %dx%d plane", n->leny, n->lenx);
2460 if(n->sprite){
2461 sprixel_hide(n->sprite);
2462 destroy_tam(n);
2463 }
2464 // we must preserve the background, but a pure nccell_duplicate() would be
2465 // wiped out by the egcpool_dump(). do a duplication (to get the stylemask
2466 // and channels), and then reload.
2467 char* egc = nccell_strdup(n, &n->basecell);
2468 memset(n->fb, 0, sizeof(*n->fb) * n->leny * n->lenx);
2469 egcpool_dump(&n->pool);
2470 egcpool_init(&n->pool);
2471 // we need to zero out the EGC before handing this off to nccell_load, but
2472 // we don't want to lose the channels/attributes, so explicit gcluster load.
2473 n->basecell.gcluster = 0;
2474 nccell_load(n, &n->basecell, egc);
2475 free(egc);
2476 n->y = n->x = 0;
2477}
2478
2479int ncplane_erase_region(ncplane* n, int ystart, int xstart, int ylen, int xlen){
2480 if(ystart == -1){
2481 ystart = n->y;
2482 }
2483 if(xstart == -1){
2484 xstart = n->x;
2485 }
2486 if(ystart < 0 || xstart < 0){
2487 logerror("illegal start of erase (%d, %d)", ystart, xstart);
2488 return -1;
2489 }
2490 if(ystart >= (int)ncplane_dim_y(n) || xstart >= (int)ncplane_dim_x(n)){
2491 logerror("illegal start of erase (%d, %d)", ystart, xstart);
2492 return -1;
2493 }
2494 if(xlen < 0){
2495 if(xlen + 1 < -xstart){
2496 xlen = -xstart - 1;
2497 }
2498 xstart = xstart + xlen + 1;
2499 xlen = -xlen;
2500 }else if(xlen == 0){
2501 xstart = 0;
2502 xlen = ncplane_dim_x(n);
2503 }
2504 if(xlen > (int)ncplane_dim_x(n) || xstart + xlen > (int)ncplane_dim_x(n)){
2505 xlen = ncplane_dim_x(n) - xstart;
2506 }
2507 if(ylen < 0){
2508 if(ylen + 1 < -ystart){
2509 ylen = -ystart - 1;
2510 }
2511 ystart = ystart + ylen + 1;
2512 ylen = -ylen;
2513 }else if(ylen == 0){
2514 ystart = 0;
2515 ylen = ncplane_dim_y(n);
2516 }
2517 if(ylen > (int)ncplane_dim_y(n) || ystart + ylen > (int)ncplane_dim_y(n)){
2518 ylen = ncplane_dim_y(n) - ystart;
2519 }
2520 // special-case the full plane erasure, as it's powerfully optimized (O(1))
2521 if(ystart == 0 && xstart == 0 &&
2522 ylen == (int)ncplane_dim_y(n) && xlen == (int)ncplane_dim_x(n)){
2523 int tmpy = n->y; // preserve cursor location
2524 int tmpx = n->x;
2526 n->y = tmpy;
2527 n->x = tmpx;
2528 return 0;
2529 }
2530 loginfo("erasing %d/%d - %d/%d", ystart, xstart, ystart + ylen, xstart + xlen);
2531 for(int y = ystart ; y < ystart + ylen ; ++y){
2532 for(int x = xstart ; x < xstart + xlen ; ++x){
2533 nccell_release(n, &n->fb[nfbcellidx(n, y, x)]);
2534 nccell_init(&n->fb[nfbcellidx(n, y, x)]);
2535 }
2536 }
2537 return 0;
2538}
2539
2541 return ncplane_pile(n)->top;
2542}
2543
2545 return ncplane_pile(n)->bottom;
2546}
2547
2549 return n->below;
2550}
2551
2553 return n->above;
2554}
2555
2556int notcurses_mice_enable(notcurses* n, unsigned eventmask){
2557 if(mouse_setup(&n->tcache, eventmask)){
2558 return -1;
2559 }
2560 return 0;
2561}
2562
2564 ncpalette* p = malloc(sizeof(*p));
2565 if(p){
2566 memcpy(p, &nc->palette, sizeof(*p));
2567 }
2568 return p;
2569}
2570
2572 int ret = -1;
2573 if(!notcurses_canchangecolor(nc)){
2574 return -1;
2575 }
2576 for(size_t z = 0 ; z < sizeof(p->chans) / sizeof(*p->chans) ; ++z){
2577 if(nc->palette.chans[z] != p->chans[z]){
2578 nc->palette.chans[z] = p->chans[z];
2579 nc->palette_damage[z] = true;
2580 }
2581 }
2582 ret = 0;
2583 return ret;
2584}
2585
2587 free(p);
2588}
2589
2590bool ncplane_translate_abs(const ncplane* n, int* restrict y, int* restrict x){
2591 ncplane_translate(ncplane_stdplane_const(n), n, y, x);
2592 if(y){
2593 if(*y < 0){
2594 return false;
2595 }
2596 unsigned yval = *y;
2597 if(yval >= n->leny){
2598 return false;
2599 }
2600 }
2601 if(x){
2602 if(*x < 0){
2603 return false;
2604 }
2605 unsigned xval = *x;
2606 if(xval >= n->lenx){
2607 return false;
2608 }
2609 }
2610 return true;
2611}
2612
2613void ncplane_translate(const ncplane* src, const ncplane* dst,
2614 int* restrict y, int* restrict x){
2615 if(dst == NULL){
2616 dst = ncplane_stdplane_const(src);
2617 }
2618 if(y){
2619 *y = src->absy - dst->absy + *y;
2620 }
2621 if(x){
2622 *x = src->absx - dst->absx + *x;
2623 }
2624}
2625
2627 return ncplane_pile(n)->nc;
2628}
2629
2631 return ncplane_pile_const(n)->nc;
2632}
2633
2635 return n->absy;
2636}
2637
2639 return n->absx;
2640}
2641
2642void ncplane_abs_yx(const ncplane* n, int* RESTRICT y, int* RESTRICT x){
2643 if(y){
2644 *y = ncplane_abs_y(n);
2645 }
2646 if(x){
2647 *x = ncplane_abs_x(n);
2648 }
2649}
2650
2652 return n->boundto;
2653}
2654
2656 return n->boundto;
2657}
2658
2659int ncplane_set_name(ncplane* n, const char* name){
2660 char* copy = name ? strdup(name) : NULL;
2661 if(copy == NULL && name != NULL){
2662 return -1;
2663 }
2664 free(n->name);
2665 n->name = copy;
2666 return 0;
2667}
2668
2669char* ncplane_name(const ncplane* n){
2670 return n->name ? strdup(n->name) : NULL;
2671}
2672
2674 n->resizecb = resizecb;
2675}
2676
2678 return n->resizecb;
2679}
2680
2682 if(n->boundto == n){
2683 return 0;
2684 }
2685 int absy = ncplane_abs_y(n);
2686 int absx = ncplane_abs_x(n);
2687 int ret = 0;
2688 if(absy + ncplane_dim_y(n) > ncplane_dim_y(n->boundto)){
2689 const int dy = (absy + ncplane_dim_y(n)) - ncplane_dim_y(n->boundto);
2690 logdebug("moving up %d", dy);
2691 if(ncplane_move_rel(n, -dy, 0)){
2692 ret = -1;
2693 }
2694 absy = ncplane_abs_y(n);
2695 }
2696 if(absx + ncplane_dim_x(n) > ncplane_dim_x(n->boundto)){
2697 const int dx = ncplane_dim_x(n->boundto) - (absx + ncplane_dim_x(n));
2698 logdebug("moving left %d", dx);
2699 if(ncplane_move_rel(n, 0, dx)){
2700 ret = -1;
2701 }
2702 absx = ncplane_abs_x(n);
2703 }
2704 // this will prefer upper-left material if the child plane is larger than
2705 // the parent. we might want a smarter rule, one based on origin?
2706 if(absy < 0){
2707 logdebug("moving down %d", -absy);
2708 // we're at least partially above our parent
2709 if(ncplane_move_rel(n, -absy, 0)){
2710 ret = -1;
2711 }
2712 }
2713 if(absx < 0){
2714 logdebug("moving right %d", -absx);
2715 // we're at least partially to the left of our parent
2716 if(ncplane_move_rel(n, 0, -absx)){
2717 ret = -1;
2718 }
2719 }
2720 return ret;
2721}
2722
2724 const ncplane* parent = ncplane_parent_const(n);
2725 // a marginalized plane cannot be larger than its oppressor plane =]
2726 unsigned maxy, maxx;
2727 if(parent == n){ // root plane, need to use pile size
2728 ncpile* p = ncplane_pile(n);
2729 maxy = p->dimy;
2730 maxx = p->dimx;
2731 }else{
2732 ncplane_dim_yx(parent, &maxy, &maxx);
2733 }
2734 if((maxy -= (n->margin_b + (n->absy - n->boundto->absy))) < 1){
2735 maxy = 1;
2736 }
2737 if((maxx -= (n->margin_r + (n->absx - n->boundto->absx))) < 1){
2738 maxx = 1;
2739 }
2740 unsigned oldy, oldx;
2741 ncplane_dim_yx(n, &oldy, &oldx); // current dimensions of 'n'
2742 unsigned keepleny = oldy > maxy ? maxy : oldy;
2743 unsigned keeplenx = oldx > maxx ? maxx : oldx;
2744 if(ncplane_resize_internal(n, 0, 0, keepleny, keeplenx, 0, 0, maxy, maxx)){
2745 return -1;
2746 }
2747 int targy = maxy - n->margin_b;
2748 int targx = maxx - n->margin_b;
2749 loginfo("marg %d/%d, pdim %d/%d, move %d/%d", n->margin_b, n->margin_r, maxy, maxx, targy, targx);
2750 return ncplane_move_yx(n, targy, targx);
2751}
2752
2754 const ncpile* pile = ncplane_pile(n); // FIXME should be taken against parent
2755 const unsigned rows = pile->dimy;
2756 const unsigned cols = pile->dimx;
2757 unsigned oldy, oldx;
2758 ncplane_dim_yx(n, &oldy, &oldx); // current dimensions of 'n'
2759 unsigned keepleny = oldy > rows ? rows : oldy;
2760 unsigned keeplenx = oldx > cols ? cols : oldx;
2761 return ncplane_resize_internal(n, 0, 0, keepleny, keeplenx, 0, 0, rows, cols);
2762}
2763
2765 const ncplane* parent = ncplane_parent_const(n);
2766 if(parent == n){
2767 logerror("can't realign a root plane");
2768 return 0;
2769 }
2770 if(n->halign == NCALIGN_UNALIGNED && n->valign == NCALIGN_UNALIGNED){
2771 logerror("passed a non-aligned plane");
2772 return -1;
2773 }
2774 int xpos = ncplane_x(n);
2775 if(n->halign != NCALIGN_UNALIGNED){
2776 xpos = ncplane_halign(parent, n->halign, ncplane_dim_x(n));
2777 }
2778 int ypos = ncplane_y(n);
2779 if(n->valign != NCALIGN_UNALIGNED){
2780 ypos = ncplane_valign(parent, n->valign, ncplane_dim_y(n));
2781 }
2782 return ncplane_move_yx(n, ypos, xpos);
2783}
2784
2785// The standard plane cannot be reparented; we return NULL in that case.
2786// If provided |newparent|==|n|, we are moving |n| to its own pile. If |n|
2787// is already bound to |newparent|, this is a no-op, and we return |n|.
2788// This is essentially a wrapper around ncplane_reparent_family() that first
2789// reparents any children to the parent of 'n', or makes them root planes if
2790// 'n' is a root plane.
2792 const notcurses* nc = ncplane_notcurses_const(n);
2793 if(n == nc->stdplane){
2794 logerror("won't reparent standard plane");
2795 return NULL; // can't reparent standard plane
2796 }
2797 if(n->boundto == newparent){
2798 loginfo("won't reparent plane to itself");
2799 return n; // don't return error, just a no-op
2800 }
2801//notcurses_debug(ncplane_notcurses(n), stderr);
2802 if(n->blist){
2803 if(n->boundto == n){ // children become new root planes
2804 ncplane* lastlink;
2805 ncplane* child = n->blist;
2806 do{
2807 child->boundto = child;
2808 lastlink = child;
2809 child = child->bnext;
2810 }while(child); // n->blist != NULL -> lastlink != NULL
2811 if( (lastlink->bnext = ncplane_pile(n)->roots) ){
2812 lastlink->bnext->bprev = &lastlink->bnext;
2813 }
2814 n->blist->bprev = &ncplane_pile(n)->roots;
2815 ncplane_pile(n)->roots = n->blist;
2816 }else{ // children are rebound to current parent
2817 ncplane* lastlink;
2818 ncplane* child = n->blist;
2819 do{
2820 child->boundto = n->boundto;
2821 lastlink = child;
2822 child = child->bnext;
2823 }while(child); // n->blist != NULL -> lastlink != NULL
2824 if( (lastlink->bnext = n->boundto->blist) ){
2825 lastlink->bnext->bprev = &lastlink->bnext;
2826 }
2827 n->blist->bprev = &n->boundto->blist;
2828 n->boundto->blist = n->blist;
2829 }
2830 n->blist = NULL;
2831 }
2832 // FIXME would be nice to skip ncplane_descendant_p() on this call...:/
2833 return ncplane_reparent_family(n, newparent);
2834}
2835
2836// unsplice self from the z-axis, and then unsplice all children, recursively.
2837// to be called before unbinding 'n' from old pile.
2838static void
2839unsplice_zaxis_recursive(ncplane* n){
2840 // might already have been unspliced, in which case ->above/->below are NULL
2841 if(ncplane_pile(n)->top == n){
2842 ncplane_pile(n)->top = n->below;
2843 }else if(n->above){
2844 n->above->below = n->below;
2845 }
2846 if(ncplane_pile(n)->bottom == n){
2847 ncplane_pile(n)->bottom = n->above;
2848 }else if(n->below){
2849 n->below->above = n->above;
2850 }
2851 for(ncplane* child = n->blist ; child ; child = child->bnext){
2852 unsplice_zaxis_recursive(child);
2853 }
2854 n->below = n->above = NULL;
2855}
2856
2857// unsplice our sprixel from the pile's sprixellist, and then unsplice all
2858// children, recursively. call before unbinding. returns a doubly-linked
2859// list of any sprixels found.
2860static sprixel*
2861unsplice_sprixels_recursive(ncplane* n, sprixel* prev){
2862 sprixel* s = n->sprite;
2863 if(s){
2864 if(s->prev){
2865 s->prev->next = s->next;
2866 }else{
2867 ncplane_pile(n)->sprixelcache = s->next;
2868 }
2869 if(s->next){
2870 s->next->prev = s->prev;
2871 }
2872 if( (s->prev = prev) ){
2873 prev->next = s;
2874 }
2875 s->next = NULL;
2876 prev = s;
2877 }
2878 for(ncplane* child = n->blist ; child ; child = child->bnext){
2879 unsplice_sprixels_recursive(child, prev);
2880 while(prev && prev->next){ // FIXME lame
2881 prev = prev->next;
2882 }
2883 }
2884 return prev;
2885}
2886
2887// recursively splice 'n' and children into the z-axis, above 'n->boundto'.
2888// handles 'n' == 'n->boundto'. to be called after binding 'n' into new pile.
2889static void
2890splice_zaxis_recursive(ncplane* n, ncpile* p, unsigned ocellpxy, unsigned ocellpxx,
2891 unsigned ncellpxy, unsigned ncellpxx){
2892 n->pile = p;
2893 if(n != n->boundto){
2894 if((n->above = n->boundto->above) == NULL){
2895 n->pile->top = n;
2896 }else{
2897 n->boundto->above->below = n;
2898 }
2899 n->below = n->boundto;
2900 n->boundto->above = n;
2901 }
2902 if(n->sprite){
2903 if(ocellpxy != ncellpxy || ocellpxx != ncellpxx){
2904 sprixel_rescale(n->sprite, ncellpxy, ncellpxx);
2905 // FIXME do what on error?
2906 }
2907 }
2908 for(ncplane* child = n->blist ; child ; child = child->bnext){
2909 splice_zaxis_recursive(child, p, ocellpxy, ocellpxx, ncellpxy, ncellpxx);
2910 }
2911}
2912
2914 // ncplane_notcurses() goes through ncplane_pile(). since we're possibly
2915 // destroying piles below, get the notcurses reference early on.
2917 if(n == nc->stdplane){
2918 return NULL; // can't reparent standard plane
2919 }
2920 if(n->boundto == newparent){ // no-op
2921 return n;
2922 }
2923 if(ncplane_descendant_p(newparent, n)){
2924 return NULL;
2925 }
2926//notcurses_debug(ncplane_notcurses(n), stderr);
2927 if(n->bprev){ // extract from sibling list
2928 if( (*n->bprev = n->bnext) ){
2929 n->bnext->bprev = n->bprev;
2930 }
2931 }else if(n->bnext){
2932 n->bnext->bprev = NULL;
2933 }
2934 n->bprev = NULL;
2935 n->bnext = NULL;
2936 // if leaving a pile, extract n from the old zaxis, and also any sprixel
2937 sprixel* s = NULL;
2938 if(n == newparent || ncplane_pile(n) != ncplane_pile(newparent)){
2939 unsplice_zaxis_recursive(n);
2940 s = unsplice_sprixels_recursive(n, NULL);
2941 }
2942 const unsigned ocellpxy = ncplane_pile(n)->cellpxy;
2943 const unsigned ocellpxx = ncplane_pile(n)->cellpxx;
2944 n->boundto = newparent;
2945 if(n == n->boundto){ // we're a new root plane
2946 logdebug("reparenting new root plane %p", n);
2947 unsplice_zaxis_recursive(n);
2948 n->bnext = NULL;
2949 n->bprev = NULL;
2950 pthread_mutex_lock(&nc->pilelock);
2951 if(ncplane_pile(n)->top == NULL){ // did we just empty our pile?
2952 ncpile_destroy(ncplane_pile(n));
2953 }
2954 make_ncpile(nc, n);
2955 unsigned ncellpxy = ncplane_pile(n)->cellpxy;
2956 unsigned ncellpxx = ncplane_pile(n)->cellpxx;
2957 pthread_mutex_unlock(&nc->pilelock);
2958 if(ncplane_pile(n)){ // FIXME otherwise, we've got a problem...!
2959 splice_zaxis_recursive(n, ncplane_pile(n), ocellpxy, ocellpxx, ncellpxy, ncellpxx);
2960 }
2961 }else{ // establish ourselves as a sibling of new parent's children
2962 if( (n->bnext = newparent->blist) ){
2963 n->bnext->bprev = &n->bnext;
2964 }
2965 n->bprev = &newparent->blist;
2966 newparent->blist = n;
2967 // place it immediately above the new binding plane if crossing piles
2968 if(ncplane_pile(n) != ncplane_pile(n->boundto)){
2969 unsigned ncellpxy = ncplane_pile(n->boundto)->cellpxy;
2970 unsigned ncellpxx = ncplane_pile(n->boundto)->cellpxx;
2971 pthread_mutex_lock(&nc->pilelock);
2972 if(ncplane_pile(n)->top == NULL){ // did we just empty our pile?
2973 ncpile_destroy(ncplane_pile(n));
2974 }
2975 n->pile = ncplane_pile(n->boundto);
2976 pthread_mutex_unlock(&nc->pilelock);
2977 splice_zaxis_recursive(n, ncplane_pile(n), ocellpxy, ocellpxx, ncellpxy, ncellpxx);
2978 }
2979 }
2980 if(s){ // must be on new plane, with sprixels to donate
2981 sprixel* lame = s;
2982 while(lame->next){
2983 lame = lame->next;
2984 }
2985 if( (lame->next = n->pile->sprixelcache) ){
2986 n->pile->sprixelcache->prev = lame;
2987 }
2988 n->pile->sprixelcache = s;
2989 }
2990 return n;
2991}
2992
2993bool ncplane_set_scrolling(ncplane* n, unsigned scrollp){
2994 bool old = n->scrolling;
2995 n->scrolling = scrollp;
2996 return old;
2997}
2998
3000 return n->scrolling;
3001}
3002
3003bool ncplane_set_autogrow(ncplane* n, unsigned growp){
3005 logerror("can't set the standard plane autogrow");
3006 return false;
3007 }
3008 bool old = n->autogrow;
3009 n->autogrow = growp;
3010 return old;
3011}
3012
3014 return n->autogrow;
3015}
3016
3017// extract an integer, which must be non-negative, and followed by either a
3018// comma or a NUL terminator.
3019static int
3020lex_ulong(const char* op, unsigned* i, char** endptr){
3021 errno = 0;
3022 long l = strtol(op, endptr, 10);
3023 if(l < 0 || (l == LONG_MAX && errno == ERANGE) || (l > INT_MAX)){
3024 fprintf(stderr, "invalid margin: %s", op);
3025 return -1;
3026 }
3027 if((**endptr != ',' && **endptr) || *endptr == op){
3028 fprintf(stderr, "invalid margin: %s", op);
3029 return -1;
3030 }
3031 *i = l;
3032 return 0;
3033}
3034
3035int notcurses_lex_scalemode(const char* op, ncscale_e* scalemode){
3036 if(strcasecmp(op, "stretch") == 0){
3037 *scalemode = NCSCALE_STRETCH;
3038 }else if(strcasecmp(op, "scalehi") == 0){
3039 *scalemode = NCSCALE_SCALE_HIRES;
3040 }else if(strcasecmp(op, "hires") == 0){
3041 *scalemode = NCSCALE_NONE_HIRES;
3042 }else if(strcasecmp(op, "scale") == 0){
3043 *scalemode = NCSCALE_SCALE;
3044 }else if(strcasecmp(op, "none") == 0){
3045 *scalemode = NCSCALE_NONE;
3046 }else{
3047 return -1;
3048 }
3049 return 0;
3050}
3051
3052const char* notcurses_str_scalemode(ncscale_e scalemode){
3053 if(scalemode == NCSCALE_STRETCH){
3054 return "stretch";
3055 }else if(scalemode == NCSCALE_SCALE){
3056 return "scale";
3057 }else if(scalemode == NCSCALE_NONE){
3058 return "none";
3059 }else if(scalemode == NCSCALE_NONE_HIRES){
3060 return "hires";
3061 }else if(scalemode == NCSCALE_SCALE_HIRES){
3062 return "scalehi";
3063 }
3064 return NULL;
3065}
3066
3068 char* eptr;
3069 if(lex_ulong(op, &opts->margin_t, &eptr)){
3070 return -1;
3071 }
3072 if(!*eptr){ // allow a single value to be specified for all four margins
3073 opts->margin_r = opts->margin_l = opts->margin_b = opts->margin_t;
3074 return 0;
3075 }
3076 op = ++eptr; // once here, we require four values
3077 if(lex_ulong(op, &opts->margin_r, &eptr) || !*eptr){
3078 return -1;
3079 }
3080 op = ++eptr;
3081 if(lex_ulong(op, &opts->margin_b, &eptr) || !*eptr){
3082 return -1;
3083 }
3084 op = ++eptr;
3085 if(lex_ulong(op, &opts->margin_l, &eptr) || *eptr){ // must end in NUL
3086 return -1;
3087 }
3088 return 0;
3089}
3090
3092 return inputready_fd(n->tcache.ictx);
3093}
3094
3096 return inputready_fd(n->tcache.ictx);
3097}
3098
3099// FIXME speed this up, PoC
3100// given an egc, get its index in the blitter's EGC set
3101static int
3102get_blitter_egc_idx(const struct blitset* bset, const char* egc){
3103 wchar_t wc;
3104 mbstate_t mbs = {0};
3105 size_t sret = mbrtowc(&wc, egc, strlen(egc), &mbs);
3106 if(sret == (size_t)-1 || sret == (size_t)-2){
3107 return -1;
3108 }
3109 wchar_t* wptr = wcsrchr(bset->egcs, wc);
3110 if(wptr == NULL){
3111//fprintf(stderr, "FAILED TO FIND [%s] (%lc) in [%ls]\n", egc, wc, bset->egcs);
3112 return -1;
3113 }
3114//fprintf(stderr, "FOUND [%s] (%lc) in [%ls] (%zu)\n", egc, wc, bset->egcs, wptr - bset->egcs);
3115 return wptr - bset->egcs;
3116}
3117
3118static bool
3119is_bg_p(int idx, int py, int px, int width){
3120 // bit increases to the right, and down
3121 const int bpos = py * width + px; // bit corresponding to pixel, 0..|egcs|-1
3122 const unsigned mask = 1u << bpos;
3123 if(idx & mask){
3124 return false;
3125 }
3126 return true;
3127}
3128
3129static inline uint32_t*
3130ncplane_as_rgba_internal(const ncplane* nc, ncblitter_e blit,
3131 int begy, int begx, unsigned leny, unsigned lenx,
3132 unsigned* pxdimy, unsigned* pxdimx){
3133 const notcurses* ncur = ncplane_notcurses_const(nc);
3134 unsigned ystart, xstart;
3135 if(check_geometry_args(nc, begy, begx, &leny, &lenx, &ystart, &xstart)){
3136 return NULL;
3137 }
3138 if(blit == NCBLIT_PIXEL){ // FIXME extend this to support sprixels
3139 logerror("pixel blitter %d not yet supported", blit);
3140 return NULL;
3141 }
3142 if(blit == NCBLIT_DEFAULT){
3143 logerror("must specify exact blitter, not NCBLIT_DEFAULT");
3144 return NULL;
3145 }
3146 const struct blitset* bset = lookup_blitset(&ncur->tcache, blit, false);
3147 if(bset == NULL){
3148 logerror("blitter %d invalid in current environment", blit);
3149 return NULL;
3150 }
3151//fprintf(stderr, "ALLOCATING %u %d %d %p\n", 4u * lenx * leny * 2, leny, lenx, bset);
3152 if(pxdimy){
3153 *pxdimy = leny * bset->height;
3154 }
3155 if(pxdimx){
3156 *pxdimx = lenx * bset->width;
3157 }
3158 uint32_t* ret = malloc(sizeof(*ret) * lenx * bset->width * leny * bset->height);
3159//fprintf(stderr, "GEOM: %d/%d %d/%d ret: %p\n", bset->height, bset->width, *pxdimy, *pxdimx, ret);
3160 if(ret){
3161 for(unsigned y = ystart, targy = 0 ; y < ystart + leny ; ++y, targy += bset->height){
3162 for(unsigned x = xstart, targx = 0 ; x < xstart + lenx ; ++x, targx += bset->width){
3163 uint16_t stylemask;
3164 uint64_t channels;
3165 char* c = ncplane_at_yx(nc, y, x, &stylemask, &channels);
3166 if(c == NULL){
3167 free(ret);
3168 return NULL;
3169 }
3170 int idx = get_blitter_egc_idx(bset, c);
3171 if(idx < 0){
3172 free(ret);
3173 free(c);
3174 return NULL;
3175 }
3176 unsigned fr, fg, fb, br, bg, bb, fa, ba;
3177 ncchannels_fg_rgb8(channels, &fr, &fb, &fg);
3178 fa = ncchannels_fg_alpha(channels);
3179 ncchannels_bg_rgb8(channels, &br, &bb, &bg);
3180 ba = ncchannels_bg_alpha(channels);
3181 // handle each destination pixel from this cell
3182 for(unsigned py = 0 ; py < bset->height ; ++py){
3183 for(unsigned px = 0 ; px < bset->width ; ++px){
3184 uint32_t* p = &ret[(targy + py) * (lenx * bset->width) + (targx + px)];
3185 bool background = is_bg_p(idx, py, px, bset->width);
3186 if(background){
3187 if(ba){
3188 *p = 0;
3189 }else{
3190 ncpixel_set_a(p, 0xff);
3191 ncpixel_set_r(p, br);
3192 ncpixel_set_g(p, bb);
3193 ncpixel_set_b(p, bg);
3194 }
3195 }else{
3196 if(fa){
3197 *p = 0;
3198 }else{
3199 ncpixel_set_a(p, 0xff);
3200 ncpixel_set_r(p, fr);
3201 ncpixel_set_g(p, fb);
3202 ncpixel_set_b(p, fg);
3203 }
3204 }
3205 }
3206 }
3207 free(c);
3208 }
3209 }
3210 }
3211 return ret;
3212}
3213
3215 int begy, int begx, unsigned leny,
3216 unsigned lenx, unsigned* pxdimy, unsigned* pxdimx){
3217 unsigned px, py;
3218 if(!pxdimy){
3219 pxdimy = &py;
3220 }
3221 if(!pxdimx){
3222 pxdimx = &px;
3223 }
3224 return ncplane_as_rgba_internal(nc, blit, begy, begx, leny, lenx, pxdimy, pxdimx);
3225}
3226
3227// return a heap-allocated copy of the contents
3228char* ncplane_contents(ncplane* nc, int begy, int begx, unsigned leny, unsigned lenx){
3229 unsigned ystart, xstart;
3230 if(check_geometry_args(nc, begy, begx, &leny, &lenx, &ystart, &xstart)){
3231 return NULL;
3232 }
3233 size_t retlen = 1;
3234 char* ret = malloc(retlen);
3235 if(ret){
3236 for(unsigned y = ystart, targy = 0 ; y < ystart + leny ; ++y, targy += 2){
3237 for(unsigned x = xstart, targx = 0 ; x < xstart + lenx ; ++x, ++targx){
3239 // we need ncplane_at_yx_cell() here instead of ncplane_at_yx(),
3240 // because we should only have one copy of each wide EGC.
3241 int clen;
3242 if((clen = ncplane_at_yx_cell(nc, y, x, &ncl)) < 0){
3243 free(ret);
3244 return NULL;
3245 }
3246 const char* c = nccell_extended_gcluster(nc, &ncl);
3247 if(clen){
3248 char* tmp = realloc(ret, retlen + clen);
3249 if(!tmp){
3250 free(ret);
3251 return NULL;
3252 }
3253 ret = tmp;
3254 memcpy(ret + retlen - 1, c, clen);
3255 retlen += clen;
3256 }
3257 }
3258 }
3259 ret[retlen - 1] = '\0';
3260 }
3261 return ret;
3262}
3263
3264// find the center coordinate of a plane, preferring the top/left in the
3265// case of an even number of rows/columns (in such a case, there will be one
3266// more cell to the bottom/right of the center than the top/left). the
3267// center is then modified relative to the plane's origin.
3269 ncplane_center(n, y, x);
3270 if(y){
3271 *y += n->absy;
3272 }
3273 if(x){
3274 *x += n->absx;
3275 }
3276}
3277
3278int ncplane_putwstr_stained(ncplane* n, const wchar_t* gclustarr){
3279 mbstate_t ps = {0};
3280 const wchar_t** wset = &gclustarr;
3281 size_t mbytes = wcsrtombs(NULL, wset, 0, &ps);
3282 if(mbytes == (size_t)-1){
3283 logerror("error converting wide string");
3284 return -1;
3285 }
3286 ++mbytes;
3287 char* mbstr = malloc(mbytes);
3288 if(mbstr == NULL){
3289 return -1;
3290 }
3291 size_t s = wcsrtombs(mbstr, wset, mbytes, &ps);
3292 if(s == (size_t)-1){
3293 free(mbstr);
3294 return -1;
3295 }
3296 int r = ncplane_putstr_stained(n, mbstr);
3297 free(mbstr);
3298 return r;
3299}
3300
3301int notcurses_ucs32_to_utf8(const uint32_t* ucs32, unsigned ucs32count,
3302 unsigned char* resultbuf, size_t buflen){
3303 if(u32_to_u8(ucs32, ucs32count, resultbuf, &buflen) == NULL){
3304 return -1;
3305 }
3306 return buflen;
3307}
3308
3309int ncstrwidth(const char* egcs, int* validbytes, int* validwidth){
3310 int cols;
3311 if(validwidth == NULL){
3312 validwidth = &cols;
3313 }
3314 *validwidth = 0;
3315 int bytes;
3316 if(validbytes == NULL){
3317 validbytes = &bytes;
3318 }
3319 *validbytes = 0;
3320 do{
3321 int thesecols, thesebytes;
3322 thesebytes = utf8_egc_len(egcs, &thesecols);
3323 if(thesebytes < 0){
3324 return -1;
3325 }
3326 egcs += thesebytes;
3327 *validbytes += thesebytes;
3328 *validwidth += thesecols;
3329 }while(*egcs);
3330 return *validwidth;
3331}
3332
3334 unsigned* RESTRICT pxy, unsigned* RESTRICT pxx,
3335 unsigned* RESTRICT celldimy, unsigned* RESTRICT celldimx,
3336 unsigned* RESTRICT maxbmapy, unsigned* RESTRICT maxbmapx){
3337 const notcurses* nc = ncplane_notcurses_const(n);
3338 const ncpile* p = ncplane_pile_const(n);
3339 if(celldimy){
3340 *celldimy = p->cellpxy;
3341 }
3342 if(celldimx){
3343 *celldimx = p->cellpxx;
3344 }
3345 if(pxy){
3346 *pxy = p->cellpxy * ncplane_dim_y(n);
3347 }
3348 if(pxx){
3349 *pxx = p->cellpxx * ncplane_dim_x(n);
3350 }
3352 if(maxbmapy){
3353 *maxbmapy = p->cellpxy * ncplane_dim_y(n);
3354 if(*maxbmapy > nc->tcache.sixel_maxy && nc->tcache.sixel_maxy){
3355 *maxbmapy = nc->tcache.sixel_maxy;
3356 }
3357 }
3358 if(maxbmapx){
3359 *maxbmapx = p->cellpxx * ncplane_dim_x(n);
3360 if(*maxbmapx > nc->tcache.sixel_maxx && nc->tcache.sixel_maxx){
3361 *maxbmapx = nc->tcache.sixel_maxx;
3362 }
3363 }
3364 }else{
3365 if(maxbmapy){
3366 *maxbmapy = 0;
3367 }
3368 if(maxbmapx){
3369 *maxbmapx = 0;
3370 }
3371 }
3372}
3373
3375 return &n->tcache.caps;
3376}
int init_banner(const notcurses *nc, fbuf *f)
Definition banner.c:27
const struct blitset * lookup_blitset(const tinfo *tcache, ncblitter_e setid, bool may_degrade)
Definition blit.c:1297
ncloglevel_e loglevel
Definition debug.c:3
API int API int API int uint64_t uint64_t uint64_t uint64_t lr
Definition direct.h:216
API int API int API int uint64_t uint64_t uint64_t uint64_t unsigned unsigned unsigned ctlword
Definition direct.h:217
API int API int API int uint64_t uint64_t uint64_t ll
Definition direct.h:216
API int API int API int uint64_t uint64_t uint64_t uint64_t unsigned unsigned xlen
Definition direct.h:217
API int API int API int uint64_t uint64_t uint64_t uint64_t unsigned ylen
Definition direct.h:217
API int API int API int uint64_t ul
Definition direct.h:215
API int API int API int uint64_t uint64_t ur
Definition direct.h:215
const char * egc
Definition egcpool.h:173
const nccell * c
Definition egcpool.h:296
uint32_t idx
Definition egcpool.h:298
free(duplicated)
va_end(va)
int r
Definition fbuf.h:226
int inputready_fd(const inputctx *ictx)
Definition in.c:2661
int putenv_term(const char *termname) __attribute__((nonnull(1)))
Definition termdesc.c:1632
void sprixel_movefrom(sprixel *s, int y, int x)
Definition sprite.c:68
void sprixel_hide(sprixel *s)
Definition sprite.c:83
int mouse_setup(tinfo *ti, unsigned eventmask)
Definition mice.c:3
void reset_stats(ncstats *stats)
Definition stats.c:74
int ncvisual_init(int loglevel)
Definition visual.c:23
int set_loglevel_from_env(ncloglevel_e *loglevel) __attribute__((nonnull(1)))
Definition util.c:19
void summarize_stats(notcurses *nc)
Definition stats.c:166
void sprixel_free(sprixel *s)
Definition sprite.c:38
void ncmetric_use_utf8(void)
Definition metric.c:24
int sprite_clear_all(const tinfo *t, fbuf *f)
Definition sprite.c:204
int clear_and_home(notcurses *nc, tinfo *ti, fbuf *f)
Definition render.c:1399
int get_linux_fb_pixelgeom(tinfo *ti, unsigned *ypix, unsigned *xpix)
Definition linux.c:813
#define logerror(fmt,...)
Definition logging.h:32
#define loginfo(fmt,...)
Definition logging.h:42
#define logdebug(fmt,...)
Definition logging.h:52
#define logwarn(fmt,...)
Definition logging.h:37
#define logpanic(fmt,...)
Definition logging.h:22
ncplane * ncpile_top(ncplane *n)
Definition notcurses.c:2540
int ncplane_abs_x(const ncplane *n)
Definition notcurses.c:2638
int ncplane_putwstr_stained(ncplane *n, const wchar_t *gclustarr)
Definition notcurses.c:3278
__attribute__((malloc))
Definition notcurses.c:501
void ncplane_on_styles(ncplane *n, unsigned stylebits)
Definition notcurses.c:2076
void ncplane_set_fg_default(ncplane *n)
Definition notcurses.c:1502
void ncpalette_free(ncpalette *p)
Definition notcurses.c:2586
const char * notcurses_version(void)
Definition notcurses.c:182
ncplane * ncplane_new_internal(notcurses *nc, ncplane *n, const ncplane_options *nopts)
Definition notcurses.c:557
int ncplane_set_fg_rgb(ncplane *n, unsigned channel)
Definition notcurses.c:1526
int ncplane_cursor_move_yx(ncplane *n, int y, int x)
Definition notcurses.c:720
void ncplane_set_fg_rgb8_clipped(ncplane *n, int r, int g, int b)
Definition notcurses.c:1518
void ncplane_home(ncplane *n)
Definition notcurses.c:715
int ncplane_x(const ncplane *n)
Definition notcurses.c:2441
int ncplane_cursor_move_rel(ncplane *n, int y, int x)
Definition notcurses.c:750
void * ncplane_userptr(ncplane *n)
Definition notcurses.c:192
int ncplane_y(const ncplane *n)
Definition notcurses.c:2434
char * ncplane_contents(ncplane *nc, int begy, int begx, unsigned leny, unsigned lenx)
Definition notcurses.c:3228
void ncplane_cursor_yx(const ncplane *n, unsigned *y, unsigned *x)
Definition notcurses.c:1741
ncplane * notcurses_stdplane(notcurses *nc)
Definition notcurses.c:699
#define PTHREAD_MUTEX_RECURSIVE_NP
int ncplane_erase_region(ncplane *n, int ystart, int xstart, int ylen, int xlen)
Definition notcurses.c:2479
ncplane * ncpile_create(notcurses *nc, const struct ncplane_options *nopts)
Definition notcurses.c:711
char * notcurses_detected_terminal(const notcurses *nc)
Definition notcurses.c:2066
bool ncplane_autogrow_p(const ncplane *n)
Definition notcurses.c:3013
int update_term_dimensions(unsigned *rows, unsigned *cols, tinfo *tcache, int margin_b, unsigned *cgeo_changed, unsigned *pgeo_changed)
Definition notcurses.c:316
int ncplane_scrollup(ncplane *n, int r)
Definition notcurses.c:1792
void ncplane_set_channels(ncplane *n, uint64_t channels)
Definition notcurses.c:1494
int notcurses_ucs32_to_utf8(const uint32_t *ucs32, unsigned ucs32count, unsigned char *resultbuf, size_t buflen)
Definition notcurses.c:3301
void ncplane_abs_yx(const ncplane *n, int *RESTRICT y, int *RESTRICT x)
Definition notcurses.c:2642
ncplane * ncplane_dup(const ncplane *n, void *opaque)
Definition notcurses.c:760
void ncplane_set_bg_rgb8_clipped(ncplane *n, int r, int g, int b)
Definition notcurses.c:1510
int ncplane_putchar_stained(ncplane *n, char c)
Definition notcurses.c:2008
int ncplane_putnstr_aligned(struct ncplane *n, int y, ncalign_e align, size_t s, const char *str)
Definition notcurses.c:2146
int ncplane_vprintf_aligned(ncplane *n, int y, ncalign_e align, const char *format, va_list ap)
Definition notcurses.c:2125
int ncplane_set_bg_alpha(ncplane *n, int alpha)
Definition notcurses.c:1546
void ncplane_set_styles(ncplane *n, unsigned stylebits)
Definition notcurses.c:2071
const char * notcurses_str_scalemode(ncscale_e scalemode)
Definition notcurses.c:3052
int ncplane_destroy(ncplane *ncp)
Definition notcurses.c:1018
int ncplane_resize_marginalized(ncplane *n)
Definition notcurses.c:2723
int ncplane_scrollup_child(ncplane *n, const ncplane *child)
Definition notcurses.c:1813
void ncplane_pixel_geom(const ncplane *n, unsigned *RESTRICT pxy, unsigned *RESTRICT pxx, unsigned *RESTRICT celldimy, unsigned *RESTRICT celldimx, unsigned *RESTRICT maxbmapy, unsigned *RESTRICT maxbmapx)
Definition notcurses.c:3333
void notcurses_version_components(int *major, int *minor, int *patch, int *tweak)
Definition notcurses.c:24
const char * nccell_extended_gcluster(const ncplane *n, const nccell *c)
Definition notcurses.c:1573
void ncplane_off_styles(ncplane *n, unsigned stylebits)
Definition notcurses.c:2081
void * ncplane_set_userptr(ncplane *n, void *opaque)
Definition notcurses.c:186
char * ncplane_at_yx(const ncplane *n, int y, int x, uint16_t *stylemask, uint64_t *channels)
Definition notcurses.c:214
int ncplane_set_fg_palindex(ncplane *n, unsigned idx)
Definition notcurses.c:1550
int ncplane_putegc_stained(ncplane *n, const char *gclust, size_t *sbytes)
Definition notcurses.c:2032
int ncplane_abs_y(const ncplane *n)
Definition notcurses.c:2634
int ncplane_resize_placewithin(ncplane *n)
Definition notcurses.c:2681
uint16_t ncplane_styles(const ncplane *n)
Definition notcurses.c:1498
int ncplane_move_family_above(ncplane *restrict n, ncplane *restrict bpoint)
Definition notcurses.c:1674
int ncplane_set_fg_alpha(ncplane *n, int alpha)
Definition notcurses.c:1542
int ncpalette_use(notcurses *nc, const ncpalette *p)
Definition notcurses.c:2571
int ncplane_set_bg_rgb(ncplane *n, unsigned channel)
Definition notcurses.c:1538
int ncplane_destroy_family(ncplane *ncp)
Definition notcurses.c:1068
const notcurses * ncplane_notcurses_const(const ncplane *n)
Definition notcurses.c:2630
const nccapabilities * notcurses_capabilities(const notcurses *n)
Definition notcurses.c:3374
ncplane * ncplane_reparent_family(ncplane *n, ncplane *newparent)
Definition notcurses.c:2913
uint64_t ncplane_set_fchannel(ncplane *n, uint32_t channel)
Definition notcurses.c:1534
int ncplane_move_yx(ncplane *n, int y, int x)
Definition notcurses.c:2411
int ncplane_base(ncplane *ncp, nccell *c)
Definition notcurses.c:1569
int reset_term_palette(const tinfo *ti, fbuf *f, unsigned touchedpalette)
Definition notcurses.c:78
void free_plane(ncplane *p)
Definition notcurses.c:464
int ncplane_resize_maximize(ncplane *n)
Definition notcurses.c:2753
int nccell_load(ncplane *n, nccell *c, const char *gcluster)
Definition notcurses.c:1833
int ncdirect_inputready_fd(ncdirect *n)
Definition notcurses.c:3095
int ncplane_putwegc_stained(ncplane *n, const wchar_t *gclust, size_t *sbytes)
Definition notcurses.c:2020
int notcurses_inputready_fd(notcurses *n)
Definition notcurses.c:3091
void ncplane_translate(const ncplane *src, const ncplane *dst, int *restrict y, int *restrict x)
Definition notcurses.c:2613
int ncplane_putegc_yx(ncplane *n, int y, int x, const char *gclust, size_t *sbytes)
Definition notcurses.c:1995
int ncplane_set_base(ncplane *ncp, const char *egc, uint16_t stylemask, uint64_t channels)
Definition notcurses.c:1565
int ncplane_set_bg_rgb8(ncplane *n, unsigned r, unsigned g, unsigned b)
Definition notcurses.c:1514
int ncplane_move_family_below(ncplane *restrict n, ncplane *restrict bpoint)
Definition notcurses.c:1708
int notcurses_mice_enable(notcurses *n, unsigned eventmask)
Definition notcurses.c:2556
int ncplane_resize_internal(ncplane *n, int keepy, int keepx, unsigned keepleny, unsigned keeplenx, int yoff, int xoff, unsigned ylen, unsigned xlen)
Definition notcurses.c:854
const ncplane * notcurses_stdplane_const(const notcurses *nc)
Definition notcurses.c:703
uint64_t ncplane_set_bchannel(ncplane *n, uint32_t channel)
Definition notcurses.c:1530
int ncplane_vprintf_stained(struct ncplane *n, const char *format, va_list ap)
Definition notcurses.c:2136
const ncplane * ncplane_parent_const(const ncplane *n)
Definition notcurses.c:2655
int ncplane_vprintf_yx(ncplane *n, int y, int x, const char *format, va_list ap)
Definition notcurses.c:2115
ncplane * ncplane_above(ncplane *n)
Definition notcurses.c:2552
int(*)(ncplane *) ncplane_resizecb(const ncplane *n)
Definition notcurses.c:2677
notcurses * notcurses_core_init(const notcurses_options *opts, FILE *outfp)
Definition notcurses.c:1242
void ncplane_set_resizecb(ncplane *n, int(*resizecb)(ncplane *))
Definition notcurses.c:2673
bool ncplane_set_scrolling(ncplane *n, unsigned scrollp)
Definition notcurses.c:2993
ncplane * ncplane_below(ncplane *n)
Definition notcurses.c:2548
uint32_t * ncplane_as_rgba(const ncplane *nc, ncblitter_e blit, int begy, int begx, unsigned leny, unsigned lenx, unsigned *pxdimy, unsigned *pxdimx)
Definition notcurses.c:3214
bool ncplane_scrolling_p(const ncplane *n)
Definition notcurses.c:2999
int notcurses_stop(notcurses *nc)
Definition notcurses.c:1446
char * ncplane_at_cursor(const ncplane *n, uint16_t *stylemask, uint64_t *channels)
Definition notcurses.c:210
void ncplane_set_bg_default(ncplane *n)
Definition notcurses.c:1506
void ncplane_yx(const ncplane *n, int *y, int *x)
Definition notcurses.c:2448
int ncplane_set_bg_palindex(ncplane *n, unsigned idx)
Definition notcurses.c:1554
void init_lang(void)
Definition notcurses.c:1094
void scroll_down(ncplane *n)
Definition notcurses.c:1758
int ncplane_hline_interp(ncplane *n, const nccell *c, unsigned len, uint64_t c1, uint64_t c2)
Definition notcurses.c:2153
ncplane * ncplane_reparent(ncplane *n, ncplane *newparent)
Definition notcurses.c:2791
int ncplane_set_name(ncplane *n, const char *name)
Definition notcurses.c:2659
ncpixelimpl_e notcurses_check_pixel_support(const notcurses *nc)
Definition notcurses.c:1153
int ncplane_at_cursor_cell(ncplane *n, nccell *c)
Definition notcurses.c:266
int notcurses_lex_scalemode(const char *op, ncscale_e *scalemode)
Definition notcurses.c:3035
int ncplane_cursor_at(const ncplane *n, nccell *c, char **gclust)
Definition notcurses.c:2044
int resize_callbacks_children(ncplane *n)
Definition notcurses.c:801
int ncplane_resize_realign(ncplane *n)
Definition notcurses.c:2764
int reset_term_attributes(const tinfo *ti, fbuf *f)
Definition notcurses.c:60
char * ncplane_vprintf_prep(const char *format, va_list ap)
Definition notcurses.c:2087
unsigned notcurses_palette_size(const notcurses *nc)
Definition notcurses.c:2062
int notcurses_leave_alternate_screen(notcurses *nc)
Definition notcurses.c:42
ncplane * ncpile_bottom(ncplane *n)
Definition notcurses.c:2544
int ncplane_move_below(ncplane *restrict n, ncplane *restrict below)
Definition notcurses.c:1628
int ncplane_at_yx_cell(ncplane *n, int y, int x, nccell *c)
Definition notcurses.c:270
int ncstrwidth(const char *egcs, int *validbytes, int *validwidth)
Definition notcurses.c:3309
int ncplane_set_fg_rgb8(ncplane *n, unsigned r, unsigned g, unsigned b)
Definition notcurses.c:1522
int ncplane_resize(ncplane *n, int keepy, int keepx, unsigned keepleny, unsigned keeplenx, int yoff, int xoff, unsigned ylen, unsigned xlen)
Definition notcurses.c:1006
bool ncplane_translate_abs(const ncplane *n, int *restrict y, int *restrict x)
Definition notcurses.c:2590
int ncplane_vline_interp(ncplane *n, const nccell *c, unsigned len, uint64_t c1, uint64_t c2)
Definition notcurses.c:2209
uint64_t ncplane_channels(const ncplane *n)
Definition notcurses.c:1490
notcurses * ncplane_notcurses(const ncplane *n)
Definition notcurses.c:2626
ncplane * ncplane_create(ncplane *n, const ncplane_options *nopts)
Definition notcurses.c:707
const void * ncplane_userptr_const(const ncplane *n)
Definition notcurses.c:196
bool ncplane_set_autogrow(ncplane *n, unsigned growp)
Definition notcurses.c:3003
void ncplane_erase(ncplane *n)
Definition notcurses.c:2458
char * ncplane_name(const ncplane *n)
Definition notcurses.c:2669
ncplane * ncplane_parent(ncplane *n)
Definition notcurses.c:2651
uint16_t notcurses_supported_styles(const notcurses *nc)
Definition notcurses.c:2058
int ncplane_move_above(ncplane *restrict n, ncplane *restrict above)
Definition notcurses.c:1581
#define TABSTOP
Definition notcurses.c:22
int ncplane_box(ncplane *n, const nccell *ul, const nccell *ur, const nccell *ll, const nccell *lr, const nccell *hl, const nccell *vl, unsigned ystop, unsigned xstop, unsigned ctlword)
Definition notcurses.c:2271
int notcurses_enter_alternate_screen(notcurses *nc)
Definition notcurses.c:31
void notcurses_drop_planes(notcurses *nc)
Definition notcurses.c:1434
int ncplane_putc_yx(ncplane *n, int y, int x, const nccell *c)
Definition notcurses.c:1980
int notcurses_lex_margins(const char *op, notcurses_options *opts)
Definition notcurses.c:3067
void ncplane_center_abs(const ncplane *n, int *RESTRICT y, int *RESTRICT x)
Definition notcurses.c:3268
int ncplane_set_base_cell(ncplane *ncp, const nccell *c)
Definition notcurses.c:1558
ncpalette * ncpalette_new(notcurses *nc)
Definition notcurses.c:2563
void ncplane_dim_yx(const ncplane *n, unsigned *rows, unsigned *cols)
Definition notcurses.c:301
ncscale_e
Definition notcurses.h:96
@ NCSCALE_SCALE_HIRES
Definition notcurses.h:101
@ NCSCALE_STRETCH
Definition notcurses.h:99
@ NCSCALE_SCALE
Definition notcurses.h:98
@ NCSCALE_NONE
Definition notcurses.h:97
@ NCSCALE_NONE_HIRES
Definition notcurses.h:100
int y
Definition notcurses.h:1905
#define NCBOXMASK_TOP
Definition notcurses.h:2601
#define NCBOXGRAD_RIGHT
Definition notcurses.h:2606
return newn
Definition notcurses.h:3508
#define NCOPTION_SUPPRESS_BANNERS
Definition notcurses.h:1015
#define NCOPTION_INHIBIT_SETLOCALE
Definition notcurses.h:990
#define NCPLANE_OPTION_HORALIGNED
Definition notcurses.h:1441
#define NCPLANE_OPTION_AUTOGROW
Definition notcurses.h:1458
#define NCOPTION_NO_CLEAR_BITMAPS
Definition notcurses.h:996
#define NCSTYLE_MASK
Definition notcurses.h:769
#define NCPLANE_OPTION_MARGINALIZED
Definition notcurses.h:1450
#define NCPLANE_OPTION_VSCROLL
Definition notcurses.h:1462
const struct ncplane_options * opts
Definition notcurses.h:3483
ncalign_e
Definition notcurses.h:80
@ NCALIGN_UNALIGNED
Definition notcurses.h:81
#define NCOPTION_DRAIN_INPUT
Definition notcurses.h:1030
#define NCPLANE_OPTION_VERALIGNED
Definition notcurses.h:1443
#define NCOPTION_PRESERVE_CURSOR
Definition notcurses.h:1011
#define NCOPTION_SCROLLING
Definition notcurses.h:1034
#define NCOPTION_NO_QUIT_SIGHANDLERS
Definition notcurses.h:1005
#define NCBOXMASK_LEFT
Definition notcurses.h:2604
ncblitter_e
Definition notcurses.h:65
@ NCBLIT_PIXEL
Definition notcurses.h:73
@ NCBLIT_DEFAULT
Definition notcurses.h:66
#define NCOPTION_NO_WINCH_SIGHANDLER
Definition notcurses.h:1000
#define NCBOXGRAD_BOTTOM
Definition notcurses.h:2607
vopts n
Definition notcurses.h:3502
#define NCBOXGRAD_LEFT
Definition notcurses.h:2608
#define RESTRICT
Definition notcurses.h:24
#define NCBOXMASK_BOTTOM
Definition notcurses.h:2603
@ NCLOGLEVEL_SILENT
Definition notcurses.h:969
@ NCLOGLEVEL_TRACE
Definition notcurses.h:977
int int x
Definition notcurses.h:1905
#define NCCELL_TRIVIAL_INITIALIZER
Definition notcurses.h:737
ncpixelimpl_e
Definition notcurses.h:1672
@ NCPIXEL_NONE
Definition notcurses.h:1673
#define NCBOXMASK_RIGHT
Definition notcurses.h:2602
#define NCPLANE_OPTION_FIXED
Definition notcurses.h:1454
#define NCOPTION_NO_ALTERNATE_SCREEN
Definition notcurses.h:1019
API int API int const nccell unsigned len
Definition notcurses.h:2588
#define NCBOXGRAD_TOP
Definition notcurses.h:2605
#define NCOPTION_NO_FONT_CHANGES
Definition notcurses.h:1025
void nccell_release(ncplane *n, nccell *c)
Definition render.c:128
int nccell_duplicate(ncplane *n, nccell *targ, const nccell *c)
Definition render.c:133
int sprixel_rescale(sprixel *spx, unsigned ncellpxy, unsigned ncellpxx)
Definition sprite.c:225
void notcurses_stats_reset(notcurses *nc, ncstats *stats)
Definition stats.c:100
ncblitter blit
Definition internal.h:410
unsigned width
Definition internal.h:400
unsigned height
Definition internal.h:401
const wchar_t * egcs
Definition internal.h:404
Definition fbuf.h:25
uint64_t used
Definition fbuf.h:27
unsigned colors
Definition notcurses.h:1637
bool can_change_colors
Definition notcurses.h:1640
uint8_t width
Definition notcurses.h:702
uint64_t channels
Definition notcurses.h:723
uint16_t stylemask
Definition notcurses.h:703
uint32_t gcluster
Definition notcurses.h:693
uint32_t chans[NCPALETTESIZE]
Definition notcurses.h:1585
unsigned cellpxx
Definition internal.h:326
struct ncpile * next
Definition internal.h:323
ncplane * bottom
Definition internal.h:319
sprixel * sprixelcache
Definition internal.h:328
unsigned dimx
Definition internal.h:325
struct ncpile * prev
Definition internal.h:323
size_t crenderlen
Definition internal.h:324
struct notcurses * nc
Definition internal.h:322
ncplane * roots
Definition internal.h:320
unsigned dimy
Definition internal.h:325
struct crender * crender
Definition internal.h:321
int scrolls
Definition internal.h:327
unsigned cellpxy
Definition internal.h:326
ncplane * top
Definition internal.h:318
unsigned margin_r
Definition notcurses.h:1473
const char * name
Definition notcurses.h:1470
int(* resizecb)(struct ncplane *)
Definition notcurses.h:1471
unsigned margin_b
Definition notcurses.h:1473
int logrow
Definition internal.h:78
struct ncplane * below
Definition internal.h:94
ncalign_e valign
Definition internal.h:113
sprixel * sprite
Definition internal.h:105
bool fixedbound
Definition internal.h:117
unsigned lenx
Definition internal.h:86
bool scrolling
Definition internal.h:116
nccell basecell
Definition internal.h:110
bool autogrow
Definition internal.h:118
struct ncplane * above
Definition internal.h:93
struct ncpile * pile
Definition internal.h:92
struct ncplane * blist
Definition internal.h:102
char * name
Definition internal.h:111
uint64_t channels
Definition internal.h:88
ncalign_e halign
Definition internal.h:112
struct ncplane * boundto
Definition internal.h:103
struct ncplane * bnext
Definition internal.h:100
unsigned x
Definition internal.h:79
struct ncplane ** bprev
Definition internal.h:101
void(* wdestruct)(void *)
Definition internal.h:124
int absy
Definition internal.h:83
unsigned leny
Definition internal.h:86
uint16_t stylemask
Definition internal.h:114
void * userptr
Definition internal.h:108
tament * tam
Definition internal.h:106
int(* resizecb)(struct ncplane *)
Definition internal.h:109
unsigned y
Definition internal.h:79
int absx
Definition internal.h:83
int margin_b
Definition internal.h:115
void * widget
Definition internal.h:123
egcpool pool
Definition internal.h:87
int margin_r
Definition internal.h:115
nccell * fb
Definition internal.h:77
ncstats s
Definition internal.h:247
pthread_mutex_t lock
Definition internal.h:246
unsigned planes
Definition notcurses.h:1820
uint64_t fbbytes
Definition notcurses.h:1819
rasterstate rstate
Definition internal.h:336
ncstats stashed_stats
Definition internal.h:357
ncpalette palette
Definition internal.h:366
bool palette_damage[NCPALETTESIZE]
Definition internal.h:367
pthread_mutex_t pilelock
Definition internal.h:361
nccell * lastframe
Definition internal.h:341
int cursorx
Definition internal.h:354
uint64_t flags
Definition internal.h:369
int margin_l
Definition internal.h:364
ncsharedstats stats
Definition internal.h:356
tinfo tcache
Definition internal.h:360
int cursory
Definition internal.h:353
bool touched_palette
Definition internal.h:368
FILE * ttyfp
Definition internal.h:359
ncplane * stdplane
Definition internal.h:333
int margin_t
Definition internal.h:364
int margin_b
Definition internal.h:364
egcpool pool
Definition internal.h:348
int margin_r
Definition internal.h:364
int loglevel
Definition internal.h:365
struct sprixel * prev
Definition sprite.h:145
struct sprixel * next
Definition sprite.h:144
int maxpaletteread
Definition termdesc.h:193
ncpixelimpl_e pixel_implementation
Definition termdesc.h:134
unsigned cellpxx
Definition termdesc.h:117
unsigned pixx
Definition termdesc.h:113
unsigned pixy
Definition termdesc.h:112
unsigned kbdlevel
Definition termdesc.h:216
unsigned dimx
Definition termdesc.h:118
ncpalette originalpalette
Definition termdesc.h:192
nccapabilities caps
Definition termdesc.h:111
struct termios * tpreserved
Definition termdesc.h:180
bool in_alt_screen
Definition termdesc.h:219
unsigned sixel_maxx
Definition termdesc.h:165
int default_rows
Definition termdesc.h:189
int(* pixel_draw)(const struct tinfo *, const struct ncpile *p, struct sprixel *s, fbuf *f, int y, int x)
Definition termdesc.h:147
unsigned dimy
Definition termdesc.h:118
unsigned sixel_maxy
Definition termdesc.h:172
unsigned sixel_maxy_pristine
Definition termdesc.h:173
int default_cols
Definition termdesc.h:190
unsigned cellpxy
Definition termdesc.h:116
int ttyfd
Definition termdesc.h:109
int enter_alternate_screen(int fd, FILE *ttyfp, tinfo *ti, unsigned drain)
Definition termdesc.c:559
int locate_cursor(tinfo *ti, unsigned *cursor_y, unsigned *cursor_x)
Definition termdesc.c:1558
void free_terminfo_cache(tinfo *ti)
Definition termdesc.c:197
int interrogate_terminfo(tinfo *ti, FILE *out, unsigned utf8, unsigned noaltscreen, unsigned nocbreak, unsigned nonewfonts, int *cursor_y, int *cursor_x, ncsharedstats *stats, int lmargin, int tmargin, int rmargin, int bmargin, unsigned draininput)
Definition termdesc.c:1294
int leave_alternate_screen(int fd, FILE *fp, tinfo *ti, unsigned drain)
Definition termdesc.c:611
char * termdesc_longterm(const tinfo *ti)
Definition termdesc.c:1535
int tiocgwinsz(int fd, struct winsize *ws)
Definition termdesc.c:1576
return NULL
Definition termdesc.h:229
@ ESCAPE_RMKX
Definition termdesc.h:66
@ ESCAPE_CIVIS
Definition termdesc.h:54
@ ESCAPE_CNORM
Definition termdesc.h:55
@ ESCAPE_RESTORECOLORS
Definition termdesc.h:88
@ ESCAPE_OP
Definition termdesc.h:50
@ ESCAPE_OC
Definition termdesc.h:56
@ ESCAPE_RMCUP
Definition termdesc.h:69
@ ESCAPE_SMCUP
Definition termdesc.h:68
@ ESCAPE_SAVECOLORS
Definition termdesc.h:87
@ ESCAPE_INITC
Definition termdesc.h:81
@ ESCAPE_SGR0
Definition termdesc.h:53
#define XTMODKEYSUNDO
Definition termdesc.h:27
#define KKEYBOARD_POP
Definition termdesc.h:22
int setup_signals(void *vnc, bool no_quit_sigs, bool no_winch_sigs, int(*handler)(void *, void **))
Definition unixsig.c:188
int drop_signals(void *nc, void **altstack)
Definition unixsig.c:100