Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
termdesc.h File Reference
#include "version.h"
#include "builddef.h"
#include <stdint.h>
#include <pthread.h>
#include <stdbool.h>
#include <notcurses/notcurses.h>
#include "sprite.h"
#include "blit.h"
#include "fbuf.h"
#include "in.h"
Include dependency graph for termdesc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  cursorreport
 
struct  tinfo
 

Macros

#define KKEYBOARD_POP   "\x1b[<u"
 
#define XTMODKEYSUNDO   "\x1b[>2m\x1b[>4m"
 

Typedefs

typedef struct cursorreport cursorreport
 
typedef struct tinfo tinfo
 

Enumerations

enum  escape_e {
  ESCAPE_CUP , ESCAPE_HPA , ESCAPE_VPA , ESCAPE_SETAF ,
  ESCAPE_SETAB , ESCAPE_OP , ESCAPE_FGOP , ESCAPE_BGOP ,
  ESCAPE_SGR0 , ESCAPE_CIVIS , ESCAPE_CNORM , ESCAPE_OC ,
  ESCAPE_SITM , ESCAPE_RITM , ESCAPE_CUU , ESCAPE_CUB ,
  ESCAPE_CUF , ESCAPE_BOLD , ESCAPE_NOBOLD , ESCAPE_CUD ,
  ESCAPE_SMKX , ESCAPE_RMKX , ESCAPE_EL , ESCAPE_SMCUP ,
  ESCAPE_RMCUP , ESCAPE_SMXX , ESCAPE_SMUL , ESCAPE_RMUL ,
  ESCAPE_SMULX , ESCAPE_SMULNOX , ESCAPE_RMXX , ESCAPE_IND ,
  ESCAPE_INDN , ESCAPE_SC , ESCAPE_RC , ESCAPE_CLEAR ,
  ESCAPE_INITC , ESCAPE_U7 , ESCAPE_BSUM , ESCAPE_ESUM ,
  ESCAPE_SAVECOLORS , ESCAPE_RESTORECOLORS , ESCAPE_DECERA , ESCAPE_MAX
}
 

Functions

 if (idx)
 
int interrogate_terminfo (tinfo *ti, FILE *out, unsigned utf8, unsigned noaltscreen, unsigned nocbreak, unsigned nonewfonts, int *cursor_y, int *cursor_x, struct ncsharedstats *stats, int lmargin, int tmargin, int rmargin, int bmargin, unsigned draininput) __attribute__((nonnull(1
 
int void free_terminfo_cache (tinfo *ti)
 
char * termdesc_longterm (const tinfo *ti)
 
int locate_cursor (tinfo *ti, unsigned *cursor_y, unsigned *cursor_x)
 
int grow_esc_table (tinfo *ti, const char *tstr, escape_e esc, size_t *tlen, size_t *tused)
 
int enter_alternate_screen (int ttyfd, FILE *ttyfp, tinfo *ti, unsigned drain)
 
int leave_alternate_screen (int ttyfd, FILE *ttyfp, tinfo *ti, unsigned drain)
 
int cbreak_mode (tinfo *ti)
 
int tiocgwinsz (int fd, struct winsize *ws)
 

Variables

static escape_e e
 
return NULL
 

Macro Definition Documentation

◆ KKEYBOARD_POP

#define KKEYBOARD_POP   "\x1b[<u"

Definition at line 22 of file termdesc.h.

◆ XTMODKEYSUNDO

#define XTMODKEYSUNDO   "\x1b[>2m\x1b[>4m"

Definition at line 27 of file termdesc.h.

Typedef Documentation

◆ cursorreport

typedef struct cursorreport cursorreport

◆ tinfo

typedef struct tinfo tinfo

Enumeration Type Documentation

◆ escape_e

enum escape_e
Enumerator
ESCAPE_CUP 
ESCAPE_HPA 
ESCAPE_VPA 
ESCAPE_SETAF 
ESCAPE_SETAB 
ESCAPE_OP 
ESCAPE_FGOP 
ESCAPE_BGOP 
ESCAPE_SGR0 
ESCAPE_CIVIS 
ESCAPE_CNORM 
ESCAPE_OC 
ESCAPE_SITM 
ESCAPE_RITM 
ESCAPE_CUU 
ESCAPE_CUB 
ESCAPE_CUF 
ESCAPE_BOLD 
ESCAPE_NOBOLD 
ESCAPE_CUD 
ESCAPE_SMKX 
ESCAPE_RMKX 
ESCAPE_EL 
ESCAPE_SMCUP 
ESCAPE_RMCUP 
ESCAPE_SMXX 
ESCAPE_SMUL 
ESCAPE_RMUL 
ESCAPE_SMULX 
ESCAPE_SMULNOX 
ESCAPE_RMXX 
ESCAPE_IND 
ESCAPE_INDN 
ESCAPE_SC 
ESCAPE_RC 
ESCAPE_CLEAR 
ESCAPE_INITC 
ESCAPE_U7 
ESCAPE_BSUM 
ESCAPE_ESUM 
ESCAPE_SAVECOLORS 
ESCAPE_RESTORECOLORS 
ESCAPE_DECERA 
ESCAPE_MAX 

Definition at line 44 of file termdesc.h.

44 {
45 ESCAPE_CUP, // "cup" move cursor to absolute x, y position
46 ESCAPE_HPA, // "hpa" move cursor to absolute horizontal position
47 ESCAPE_VPA, // "vpa" move cursor to absolute vertical position
48 ESCAPE_SETAF, // "setaf" set foreground color
49 ESCAPE_SETAB, // "setab" set background color
50 ESCAPE_OP, // "op" set foreground and background color to defaults
51 ESCAPE_FGOP, // set foreground only to default
52 ESCAPE_BGOP, // set background only to default
53 ESCAPE_SGR0, // "sgr0" turn off all styles
54 ESCAPE_CIVIS, // "civis" make the cursor invisiable
55 ESCAPE_CNORM, // "cnorm" restore the cursor to normal
56 ESCAPE_OC, // "oc" restore original colors
57 ESCAPE_SITM, // "sitm" start italics
58 ESCAPE_RITM, // "ritm" end italics
59 ESCAPE_CUU, // "cuu" move n cells up
60 ESCAPE_CUB, // "cub" move n cells back (left)
61 ESCAPE_CUF, // "cuf" move n cells forward (right)
62 ESCAPE_BOLD, // "bold" enter bold mode
63 ESCAPE_NOBOLD, // disable bold (ANSI but not terminfo, SGR 22)
64 ESCAPE_CUD, // "cud" move n cells down
65 ESCAPE_SMKX, // "smkx" keypad_xmit (keypad transmit mode)
66 ESCAPE_RMKX, // "rmkx" keypad_local
67 ESCAPE_EL, // "el" clear to end of line, inclusive
68 ESCAPE_SMCUP, // "smcup" enter alternate screen
69 ESCAPE_RMCUP, // "rmcup" leave alternate screen
70 ESCAPE_SMXX, // "smxx" start struckout
71 ESCAPE_SMUL, // "smul" start underline
72 ESCAPE_RMUL, // "rmul" end underline
73 ESCAPE_SMULX, // "Smulx" deparameterized: start extended underline
74 ESCAPE_SMULNOX, // "Smulx" deparameterized: kill underline
75 ESCAPE_RMXX, // "rmxx" end struckout
76 ESCAPE_IND, // "ind" scroll 1 line up
77 ESCAPE_INDN, // "indn" scroll n lines up
78 ESCAPE_SC, // "sc" push the cursor onto the stack
79 ESCAPE_RC, // "rc" pop the cursor off the stack
80 ESCAPE_CLEAR, // "clear" clear screen and home cursor
81 ESCAPE_INITC, // "initc" set up palette entry
82 ESCAPE_U7, // "u7" cursor position report
83 // Application synchronized updates, not present in terminfo
84 // (https://gitlab.com/gnachman/iterm2/-/wikis/synchronized-updates-spec)
85 ESCAPE_BSUM, // Begin Synchronized Update Mode
86 ESCAPE_ESUM, // End Synchronized Update Mode
87 ESCAPE_SAVECOLORS, // XTPUSHCOLORS (push palette/fg/bg)
88 ESCAPE_RESTORECOLORS, // XTPOPCOLORS (pop palette/fg/bg)
89 ESCAPE_DECERA, // rectangular erase
91} escape_e;
escape_e
Definition termdesc.h:44
@ ESCAPE_CUU
Definition termdesc.h:59
@ ESCAPE_RMKX
Definition termdesc.h:66
@ ESCAPE_CIVIS
Definition termdesc.h:54
@ ESCAPE_SC
Definition termdesc.h:78
@ ESCAPE_BOLD
Definition termdesc.h:62
@ ESCAPE_CUP
Definition termdesc.h:45
@ ESCAPE_CUD
Definition termdesc.h:64
@ ESCAPE_CNORM
Definition termdesc.h:55
@ ESCAPE_MAX
Definition termdesc.h:90
@ ESCAPE_SMULX
Definition termdesc.h:73
@ ESCAPE_SETAB
Definition termdesc.h:49
@ ESCAPE_RESTORECOLORS
Definition termdesc.h:88
@ ESCAPE_CUF
Definition termdesc.h:61
@ ESCAPE_IND
Definition termdesc.h:76
@ ESCAPE_NOBOLD
Definition termdesc.h:63
@ ESCAPE_VPA
Definition termdesc.h:47
@ ESCAPE_SMXX
Definition termdesc.h:70
@ ESCAPE_ESUM
Definition termdesc.h:86
@ 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_SMKX
Definition termdesc.h:65
@ ESCAPE_FGOP
Definition termdesc.h:51
@ ESCAPE_SITM
Definition termdesc.h:57
@ ESCAPE_SAVECOLORS
Definition termdesc.h:87
@ ESCAPE_RMXX
Definition termdesc.h:75
@ ESCAPE_DECERA
Definition termdesc.h:89
@ ESCAPE_INDN
Definition termdesc.h:77
@ ESCAPE_BSUM
Definition termdesc.h:85
@ ESCAPE_SMUL
Definition termdesc.h:71
@ ESCAPE_CLEAR
Definition termdesc.h:80
@ ESCAPE_INITC
Definition termdesc.h:81
@ ESCAPE_RITM
Definition termdesc.h:58
@ ESCAPE_SETAF
Definition termdesc.h:48
@ ESCAPE_U7
Definition termdesc.h:82
@ ESCAPE_BGOP
Definition termdesc.h:52
@ ESCAPE_HPA
Definition termdesc.h:46
@ ESCAPE_RMUL
Definition termdesc.h:72
@ ESCAPE_RC
Definition termdesc.h:79
@ ESCAPE_SMULNOX
Definition termdesc.h:74
@ ESCAPE_EL
Definition termdesc.h:67
@ ESCAPE_CUB
Definition termdesc.h:60
@ ESCAPE_SGR0
Definition termdesc.h:53

Function Documentation

◆ cbreak_mode()

int cbreak_mode ( tinfo ti)

Definition at line 1595 of file termdesc.c.

1595 {
1596#ifndef __MINGW32__
1597 int ttyfd = ti->ttyfd;
1598 if(ttyfd < 0){
1599 return 0;
1600 }
1601 // assume it's not a true terminal (e.g. we might be redirected to a file)
1602 struct termios modtermios;
1603 memcpy(&modtermios, ti->tpreserved, sizeof(modtermios));
1604 // see termios(3). disabling ECHO and ICANON means input will not be echoed
1605 // to the screen, input is made available without enter-based buffering, and
1606 // line editing is disabled. since we have not gone into raw mode, ctrl+c
1607 // etc. still have their typical effects. ICRNL maps return to 13 (Ctrl+M)
1608 // instead of 10 (Ctrl+J).
1609 modtermios.c_lflag &= (~ECHO & ~ICANON);
1610 modtermios.c_iflag &= ~ICRNL;
1611 if(tcsetattr(ttyfd, TCSANOW, &modtermios)){
1612 logerror("error disabling echo / canonical on %d (%s)", ttyfd, strerror(errno));
1613 return -1;
1614 }
1615#else
1616 // we don't yet have a way to take Cygwin/MSYS2 out of canonical mode FIXME.
1617 DWORD mode;
1618 if(!GetConsoleMode(ti->inhandle, &mode)){
1619 logerror("error acquiring input mode");
1620 return -1;
1621 }
1622 mode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
1623 if(!SetConsoleMode(ti->inhandle, mode)){
1624 logerror("error setting input mode");
1625 return -1;
1626 }
1627#endif
1628 return 0;
1629}
#define logerror(fmt,...)
Definition logging.h:32
struct termios * tpreserved
Definition termdesc.h:180
int ttyfd
Definition termdesc.h:109
Here is the caller graph for this function:

◆ enter_alternate_screen()

int enter_alternate_screen ( int  ttyfd,
FILE *  ttyfp,
tinfo ti,
unsigned  drain 
)

Definition at line 559 of file termdesc.c.

559 {
560 if(ti->in_alt_screen){
561 return 0;
562 }
563 const char* popcolors = get_escape(ti, ESCAPE_RESTORECOLORS);
564 if(popcolors){
565 if(term_emit(popcolors, ttyfp, true)){
566 return -1;
567 }
568 }
569 const char* smcup = get_escape(ti, ESCAPE_SMCUP);
570 if(smcup == NULL){
571 logerror("alternate screen is unavailable");
572 return -1;
573 }
574 if(!drain){
575 if(ti->kbdlevel){
576 if(tty_emit(KKEYBOARD_POP, fd)){
577 return -1;
578 }
579 }else{
580 if(tty_emit(XTMODKEYSUNDO, fd)){
581 return -1;
582 }
583 }
584 }
585 if(tty_emit(smcup, fd) < 0){
586 return -1;
587 }
588 if(!drain){
589 if(ti->kbdlevel){
590 if(tty_emit(KKBDENTER, fd)){
591 return -1;
592 }
593 }else{
594 if(tty_emit(XTMODKEYS, fd)){
595 return -1;
596 }
597 }
598 }
599 const char* pushcolors = get_escape(ti, ESCAPE_SAVECOLORS);
600 if(pushcolors){
601 if(term_emit(pushcolors, ttyfp, true)){
602 return -1;
603 }
604 }
605 ti->in_alt_screen = true;
606 return 0;
607}
unsigned kbdlevel
Definition termdesc.h:216
bool in_alt_screen
Definition termdesc.h:219
#define KKBDENTER
Definition termdesc.c:465
#define XTMODKEYS
Definition termdesc.c:400
return NULL
Definition termdesc.h:229
#define XTMODKEYSUNDO
Definition termdesc.h:27
#define KKEYBOARD_POP
Definition termdesc.h:22
Here is the caller graph for this function:

◆ free_terminfo_cache()

int void free_terminfo_cache ( tinfo ti)

Definition at line 197 of file termdesc.c.

197 {
198 stop_inputlayer(ti);
199 loginfo("brought down input layer");
200 if(ti->pixel_cleanup){
201 ti->pixel_cleanup(ti);
202 }
203 free(ti->termversion);
204 free(ti->esctable);
205#ifdef __linux__
206 if(ti->linux_fb_fd >= 0){
207 close(ti->linux_fb_fd);
208 }
209 free(ti->linux_fb_dev);
210 if(ti->linux_fbuffer != MAP_FAILED){
211 munmap(ti->linux_fbuffer, ti->linux_fb_len);
212 }
213#endif
214 free(ti->tpreserved);
215 loginfo("destroyed terminfo cache");
216}
free(duplicated)
int stop_inputlayer(tinfo *ti)
Definition in.c:2644
#define loginfo(fmt,...)
Definition logging.h:42
void(* pixel_cleanup)(struct tinfo *)
Definition termdesc.h:158
char * esctable
Definition termdesc.h:110
char * termversion
Definition termdesc.h:177
Here is the call graph for this function:
Here is the caller graph for this function:

◆ grow_esc_table()

int grow_esc_table ( tinfo ti,
const char *  tstr,
escape_e  esc,
size_t *  tlen,
size_t *  tused 
)

Definition at line 14 of file termdesc.c.

15 {
16 // the actual table can grow past 64KB, but we can't start there, as
17 // we only have 16-bit indices.
18 if(*tused >= 65535){
19 fprintf(stderr, "Can't add escape %d to full table\n", esc);
20 return -1;
21 }
22 if(get_escape(ti, esc)){
23 fprintf(stderr, "Already defined escape %d (%s)\n",
24 esc, get_escape(ti, esc));
25 return -1;
26 }
27 size_t slen = strlen(tstr) + 1; // count the nul term
28 if(*tlen - *tused < slen){
29 // guaranteed to give us enough space to add tstr (and then some)
30 size_t newsize = *tlen + 4020 + slen; // don't pull two pages ideally
31 char* tmp = realloc(ti->esctable, newsize);
32 if(tmp == NULL){
33 return -1;
34 }
35 ti->esctable = tmp;
36 *tlen = newsize;
37 }
38 // we now are guaranteed sufficient space to copy tstr
39 memcpy(ti->esctable + *tused, tstr, slen);
40 ti->escindices[esc] = *tused + 1; // one-bias
41 *tused += slen;
42 return 0;
43}
uint16_t escindices[ESCAPE_MAX]
Definition termdesc.h:108
Here is the caller graph for this function:

◆ if()

if ( idx  )

Definition at line 226 of file termdesc.h.

226 {
227 return tdesc->esctable + idx - 1;
228 }
uint32_t idx
Definition egcpool.h:298

◆ interrogate_terminfo()

int interrogate_terminfo ( tinfo ti,
FILE *  out,
unsigned  utf8,
unsigned  noaltscreen,
unsigned  nocbreak,
unsigned  nonewfonts,
int *  cursor_y,
int *  cursor_x,
struct ncsharedstats stats,
int  lmargin,
int  tmargin,
int  rmargin,
int  bmargin,
unsigned  draininput 
)

◆ leave_alternate_screen()

int leave_alternate_screen ( int  ttyfd,
FILE *  ttyfp,
tinfo ti,
unsigned  drain 
)

Definition at line 611 of file termdesc.c.

611 {
612 if(!ti->in_alt_screen){
613 return 0;
614 }
615 const char* rmcup = get_escape(ti, ESCAPE_RMCUP);
616 if(rmcup == NULL){
617 logerror("can't leave alternate screen");
618 return -1;
619 }
620 if(!drain){
621 if(ti->kbdlevel){
622 if(tty_emit(KKEYBOARD_POP, fd)){
623 return -1;
624 }
625 }else{
626 if(tty_emit(XTMODKEYSUNDO, fd)){
627 return -1;
628 }
629 }
630 }
631 const char* popcolors = get_escape(ti, ESCAPE_RESTORECOLORS);
632 if(popcolors){
633 if(term_emit(popcolors, fp, true)){
634 return -1;
635 }
636 }
637 if(tty_emit(rmcup, fd)){
638 return -1;
639 }
640 if(!drain){
641 if(ti->kbdlevel){
642 if(tty_emit(KKBDENTER, fd)){
643 return -1;
644 }
645 }else{
646 if(tty_emit(XTMODKEYS, fd)){
647 return -1;
648 }
649 }
650 }
651 const char* pushcolors = get_escape(ti, ESCAPE_SAVECOLORS);
652 if(pushcolors){
653 if(term_emit(popcolors, fp, true)){
654 return -1;
655 }
656 }
657 ti->in_alt_screen = false;
658 return 0;
659}
Here is the caller graph for this function:

◆ locate_cursor()

int locate_cursor ( tinfo ti,
unsigned *  cursor_y,
unsigned *  cursor_x 
)

Definition at line 1558 of file termdesc.c.

1558 {
1559 const char* u7 = get_escape(ti, ESCAPE_U7);
1560 if(u7 == NULL){
1561 logwarn("no support in terminfo");
1562 return -1;
1563 }
1564 if(ti->ttyfd < 0){
1565 logwarn("no valid path for cursor report");
1566 return -1;
1567 }
1568 int fd = ti->ttyfd;
1569 if(get_cursor_location(ti->ictx, u7, cursor_y, cursor_x)){
1570 return -1;
1571 }
1572 loginfo("got a report from %d %d/%d", fd, *cursor_y, *cursor_x);
1573 return 0;
1574}
int get_cursor_location(inputctx *ictx, const char *u7, unsigned *y, unsigned *x)
Definition in.c:2785
#define logwarn(fmt,...)
Definition logging.h:37
struct inputctx * ictx
Definition termdesc.h:181
Here is the call graph for this function:
Here is the caller graph for this function:

◆ termdesc_longterm()

char * termdesc_longterm ( const tinfo ti)

Definition at line 1535 of file termdesc.c.

1535 {
1536 size_t tlen = strlen(ti->termname) + 1;
1537 size_t slen = tlen;
1538 if(ti->termversion){
1539 slen += strlen(ti->termversion) + 1;
1540 }
1541 char* ret = malloc(slen);
1542 if(ret){
1543 memcpy(ret, ti->termname, tlen);
1544 if(ti->termversion){
1545 ret[tlen - 1] = ' ';
1546 strcpy(ret + tlen, ti->termversion);
1547 }
1548 }
1549 return ret;
1550}
const char * termname
Definition termdesc.h:176
Here is the caller graph for this function:

◆ tiocgwinsz()

int tiocgwinsz ( int  fd,
struct winsize *  ws 
)

Definition at line 1576 of file termdesc.c.

1576 {
1577#ifndef __MINGW32__
1578 int i = ioctl(fd, TIOCGWINSZ, ws);
1579 if(i < 0){
1580 logerror("TIOCGWINSZ failed on %d (%s)", fd, strerror(errno));
1581 return -1;
1582 }
1583 if(ws->ws_row <= 0 || ws->ws_col <= 0){
1584 logerror("bogon from TIOCGWINSZ on %d (%d/%d)",
1585 fd, ws->ws_row, ws->ws_col);
1586 return -1;
1587 }
1588#else
1589 (void)fd;
1590 (void)ws;
1591#endif
1592 return 0;
1593}
Here is the caller graph for this function:

Variable Documentation

◆ e

Initial value:
{
unsigned idx = tdesc->escindices[e]
static escape_e e
Definition termdesc.h:224

Definition at line 224 of file termdesc.h.

◆ NULL

return NULL

Definition at line 229 of file termdesc.h.