X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/d23763dae4f649cc29a923298e0232ffdf608744..98ff9295493ed2b990f30768e11b18b6bc65eaa4:/ui/tty.h?ds=sidebyside diff --git a/ui/tty.h b/ui/tty.h index a82804d..e026c15 100644 --- a/ui/tty.h +++ b/ui/tty.h @@ -40,28 +40,38 @@ # include "bits.h" #endif -/*----- Data structures ---------------------------------------------------*/ +#ifndef MLIB_GPRINTF_H +# include "gprintf.h" +#endif + +/*----- Attribute flags ---------------------------------------------------*/ -/* Attributes. */ +/* Line style attributes. */ #define TTAF_LNMASK 0x0003u /* line style mask */ #define TTAF_LNSHIFT 0 /* line style shift */ enum { TTLN_NONE, /* no line */ TTLN_ULINE, /* underline */ TTLN_UULINE, /* double underline */ - TTLN_STRIKE, /* strikeout */ TTLN_LIMIT }; + +/* Weight attributes. */ #define TTAF_WTMASK 0x000cu /* weight mask */ -#define TTAG_WTSHIFT 2 /* weight shift */ +#define TTAF_WTSHIFT 2 /* weight shift */ enum { TTWT_MED, /* medium */ TTWT_BOLD, /* bold/bright */ TTWT_DIM, /* light/dim */ TTWT_LIMIT }; -#define TTAF_ITAL 0x0010u /* italic/oblique */ -#define TTAF_INVV 0x0020u /* inverse video */ + +/* Miscellaneous attributes. */ +#define TTAF_INVV 0x0010u /* inverse video */ +#define TTAF_STRIKE 0x0020u /* strike out */ +#define TTAF_ITAL 0x0040u /* italic/oblique */ + +/* Colour space attributes. */ #define TTAF_FGSPCMASK 0x1c00u /* foreground space mask */ #define TTAF_FGSPCSHIFT 10 /* foreground space shift */ #define TTAF_BGSPCMASK 0xe000u /* background space mask */ @@ -70,133 +80,111 @@ enum { TTCSPC_NONE, /* no colour */ TTCSPC_1BPC, /* one bit per channel */ TTCSPC_1BPCBR, /* one bit per channel, brighter */ - TTCSPC_2BPC, /* two bits levels per channel */ + TTCSPC_4LPC, /* four levels per channel */ TTCSPC_8LGS, /* eight levels greyscale */ TTCSPC_6LPC, /* six levels per channel */ TTCSPC_24LGS, /* 24 levels greyscale */ - TTCSPC_8BPC /* eight bits per channel */ + TTCSPC_8BPC, /* eight bits per channel */ TTCSPC_LIMIT }; -/* Colours. */ -#define TT1BC_RED 1u -#define TT1BC_GREEN 2u -#define TT1BC_BLUE 4u -#define TT1BC_BRIGHT 8u -#define TTCOL_BLACK 0u -#define TTCOL_RED (TT1BC_RED) -#define TTCOL_GREEN (TT1BC_GREEN) -#define TTCOL_YELLOW (TT1BC_RED | TT1BC_GREEN) -#define TTCOL_BLUE (TT1BC_BLUE) -#define TTCOL_MAGENTA (TT1BC_RED | TT1BC_BLUE) -#define TTCOL_CYAN (TT1BC_GREEN | TT1BC_BLUE) -#define TTCOL_WHITE (TT1BC_RED | TT1BC_GREEN | TT1BC_BLUE) -#define TTCOL_BRBLACK (TTCOL_BLACK | TT1BC_BRIGHT) -#define TTCOL_BRRED (TTCOL_RED | TT1BC_BRIGHT) -#define TTCOL_BRGREEN (TTCOL_GREEN | TT1BC_BRIGHT) -#define TTCOL_BRYELLOW (TTCOL_YELLOW | TT1BC_BRIGHT) -#define TTCOL_BRBLUE (TTCOL_BLUE | TT1BC_BRIGHT) -#define TTCOL_BRMAGENTA (TTCOL_MAGENTA | TT1BC_BRIGHT) -#define TTCOL_BRCYAN (TTCOL_CYAN | TT1BC_BRIGHT) -#define TTCOL_BRWHITE (TTCOL_WHITE | TT1BC_BRIGHT) - +/*----- Specifying colours ------------------------------------------------*/ + +/* One-bit-per-channel colours. */ +#define TT1BPC_RED 1u +#define TT1BPC_GRN 2u +#define TT1BPC_BLU 4u +#define TT1BPC_BRI 8u +#define TTCOL_BLK 0u +#define TTCOL_RED (TT1BPC_RED) +#define TTCOL_GRN (TT1BPC_GRN) +#define TTCOL_YLW (TT1BPC_RED | TT1BPC_GRN) +#define TTCOL_BLU (TT1BPC_BLU) +#define TTCOL_MGN (TT1BPC_RED | TT1BPC_BLU) +#define TTCOL_CYN (TT1BPC_GRN | TT1BPC_BLU) +#define TTCOL_WHT (TT1BPC_RED | TT1BPC_GRN | TT1BPC_BLU) +#define TTCOL_BRBLK (TTCOL_BLK | TT1BPC_BRI) +#define TTCOL_BRRED (TTCOL_RED | TT1BPC_BRI) +#define TTCOL_BRGRN (TTCOL_GRN | TT1BPC_BRI) +#define TTCOL_BRYLW (TTCOL_YLW | TT1BPC_BRI) +#define TTCOL_BRBLU (TTCOL_BLU | TT1BPC_BRI) +#define TTCOL_BRMGN (TTCOL_MGN | TT1BPC_BRI) +#define TTCOL_BRCYN (TTCOL_CYN | TT1BPC_BRI) +#define TTCOL_BRWHT (TTCOL_WHT | TT1BPC_BRI) + +/* Two-bits-per-channel colours. */ #define TTCOL_MK2B(r, g, b) (((r) << 4) | ((g) << 2) | ((b) << 0)) #define TTCOL_2BR(col) (((col) >> 4)&0x03) #define TTCOL_2BG(col) (((col) >> 2)&0x03) #define TTCOL_2BB(col) (((col) >> 0)&0x03) +/* Six-levels-per-channel colours. */ #define TTCOL_MK6L(r, g, b) (36*(r) + 6*(g) + (b)) #define TTCOL_6LR(col) ((col)/36) #define TTCOL_6LG(col) (((col)/6)%6) #define TTCOL_6LB(col) ((col)%6) -#define TTCOL_MK8B(r, g, b) (((r) << 16) | ((g) << 8) | ((b) << 0)) +/* Eight-bits-per-channel colours. */ +#define TTCOL_MK8B(r, g, b) \ + (((uint32)(r) << 16) | ((g) << 8) | ((b) << 0)) #define TTCOL_8BR(col) (((col) >> 16)&0xff) #define TTCOL_8BG(col) (((col) >> 8)&0xff) #define TTCOL_8BB(col) (((col) >> 0)&0xff) -struct tty_attr { - uint32 f, _res0; /* attribute flags, reserved */ - uint32 fg, bg; /* foreground/background colours */ -}; - -/* Mode settings. */ -#define TTMF_AUTOM 0x00000001u /* automatic margins */ -#define TTMF_FSCRM 0x00000002u /* full-screen mode */ -#define TTMF_INS 0x00000004u /* insert mode */ +/*----- Terminal capabilities ---------------------------------------------*/ /* Attribute capabilities. */ #define TTACF_ULINE 0x00000001u /* underline */ #define TTACF_UULINE 0x00000002u /* double underline */ -#define TTACF_STRIKE 0x00000004u /* strikeout */ -#define TTACF_BOLD 0x00000008u /* bold/bright */ -#define TTACF_DIM 0x00000010u /* light/dim */ -#define TTACF_ITAL 0x00000020u /* italic/oblique */ -#define TTACF_INVV 0x00000040u /* inverse video */ +#define TTACF_BOLD 0x00000004u /* bold/bright */ +#define TTACF_DIM 0x00000008u /* light/dim */ +#define TTACF_INVV 0x00000010u /* inverse video */ +#define TTACF_STRIKE 0x00000020u /* strikeout */ +#define TTACF_ITAL 0x00000040u /* italic/oblique */ #define TTACF_FG 0x00000080u /* set foreground colour */ #define TTACF_BG 0x00000100u /* set background colour */ -#define TTACF_1BPC 0x00000200u /* one-bit-per-channel space */ -#define TTACF_1BPCBR 0x00000400u /* one-bit-per-channel bright space */ -#define TTACF_2BPC 0x00000800u /* two-bits-per-channel space */ -#define TTACF_8LGS 0x00001000u /* 8-levels-greyscale space */ -#define TTACF_6LPC 0x00002000u /* six-levels-per-channel space */ -#define TTACF_24LGS 0x00004000u /* 24-levels-greyscale space */ -#define TTACF_8BPC 0x00008000u /* eight-bits-per-channel space */ +#define TTACF_1BPC 0x00100000u /* one-bit-per-channel space */ +#define TTACF_1BPCBR 0x00200000u /* one-bit-per-channel bright space */ +#define TTACF_4LPC 0x00400000u /* four-levels-per-channel space */ +#define TTACF_8LGS 0x00800000u /* 8-levels-greyscale space */ +#define TTACF_6LPC 0x01000000u /* six-levels-per-channel space */ +#define TTACF_24LGS 0x02000000u /* 24-levels-greyscale space */ +#define TTACF_8BPC 0x04000000u /* eight-bits-per-channel space */ +#define TTACF_CSPCMASK 0xfff00000u + +/* Mode settings. */ +#define TTMF_AUTOM 0x00000001u /* automatic margins */ +#define TTMF_FSCRN 0x00000002u /* full-screen mode */ +#define TTMF_CVIS 0x00000004u /* visible cursor */ +#define TTMF_INS 0x00000008u /* insert mode */ +#define TTMF_DEL 0x00000010u /* delete mode */ /* Other capabilities. */ #define TTCF_RELMV 0x00000100u /* relative cursor motion */ #define TTCF_ABSMV 0x00000200u /* absolute cursor motion */ #define TTCF_MIXMV 0x00000400u /* mixed cursor motion */ -#define TTCF_MVINS 0x00000800u /* motion preserves insert mode */ -#define TTCF_MVATTR 0x00001000u /* motion preserves attributes */ -#define TTCF_MMARG 0x00002000u /* proper magic margins */ -#define TTCF_SCRGN 0x00004000u /* scroll regions */ -#define TTCF_SCROLL 0x00008000u /* explicit scrolling */ -#define TTCF_BGER 0x00010000u /* erasure uses background colour */ -#define TTCF_ERCH 0x00020000u /* erase characters */ -#define TTCF_ERBOL 0x00040000u /* erase from beginning of line */ -#define TTCF_EREOL 0x00080000u /* erase to end of line */ -#define TTCF_ERBOD 0x00100000u /* erase from beginning of display */ -#define TTCF_EREOD 0x00200000u /* erase to end of display */ -#define TTCF_ERDSP 0x00400000u /* erase display */ -#define TTCF_INSCH 0x00800000u /* insert character */ -#define TTCF_INSLN 0x01000000u /* insert line */ -#define TTCF_DELCH 0x02000000u /* delete character */ -#define TTCF_DELLN 0x04000000u /* delete line */ - -struct tty_attrlist { - uint32 cap_mask, cap_eq; /* capabilities to select */ - struct tty_attr attr; /* attributes to set */ -}; -#define TTY_ATTRLIST_END { 0, 0, { 0, 0, 0, 0 } } - -/* Terminal capability backend libraries. */ -enum { - TTLIB_TERMCAP, /* traditional `termcap' */ - TTLIB_TERMINFO, /* standard `terminfo' */ - TTLIB_UNIBILIUM, /* `Unibiliium' */ - TTLIB_XTERM, /* assume modern terminal conventions */ - TTLIB_LIMIT -}; - -struct tty_state { - uint32 modes; - struct tty_attr attr; -}; - -struct tty { - const struct tty_ops *ops; - FILE *fp; - unsigned baud, ht, wd; - uint32 acaps, ocaps; - struct tty_state st; -}; +#define TTCF_MMARG 0x00000800u /* proper magic margins */ +#define TTCF_BGER 0x00001000u /* erasure uses background colour */ +#define TTCF_ERCH 0x00002000u /* erase characters */ +#define TTCF_ERBOL 0x00004000u /* erase from beginning of line */ +#define TTCF_EREOL 0x00008000u /* erase to end of line */ +#define TTCF_ERBOD 0x00010000u /* erase from beginning of display */ +#define TTCF_EREOD 0x00020000u /* erase to end of display */ +#define TTCF_ERDSP 0x00040000u /* erase display */ +#define TTCF_INSCH 0x00080000u /* insert character */ +#define TTCF_INSLN 0x00100000u /* insert line */ +#define TTCF_DELCH 0x00200000u /* delete character */ +#define TTCF_DELLN 0x00400000u /* delete line */ + +/*----- Other constants ---------------------------------------------------*/ /* Motion origin. */ #define TTOF_XHOME 0u /* absolute horizontal motion */ #define TTOF_XCUR 1u /* relative horizontal motion */ #define TTOF_YHOME 0u /* absolute vertical motion */ #define TTOF_YCUR 2u /* relative vertical motion */ +#define TTORG_HOME (TTOF_XHOME | TTOF_YHOME) +#define TTORG_CUR (TTOF_XCUR | TTOF_YCUR) /* Erasure scope. */ #define TTEF_LINE 0u /* erase within line */ @@ -208,11 +196,54 @@ struct tty { #define TTIDF_CH 0u /* insert/delete characters */ #define TTIDF_LN 1u /* insert/delete lines */ +/* Backend implementations. */ +enum { + TTBK_END, /* list end marker */ + TTBK_TERMCAP, /* traditional `termcap' */ + TTBK_TERMINFO, /* standard `terminfo' */ + TTBK_UNIBI, /* modern `Unibilium' */ + TTBK_ANSI, /* assume modern terminal conventions */ + TTBK_LIMIT +}; + +/*----- Data structures ---------------------------------------------------*/ + +/* An attribute setting. */ +struct tty_attr { + uint32 f, _res0; /* attribute flags, reserved */ + uint32 fg, bg; /* foreground/background colours */ +}; +#define TTY_ATTR_INIT { 0, 0, 0, 0 } + +/* A menu of attribute requests. */ +struct tty_attrlist { + uint32 cap_mask, cap_eq; /* capabilities to select */ + struct tty_attr attr; /* attributes to set */ +}; +#define TTY_ATTRLIST_CLEAR { 0, 0, TTY_ATTR_INIT } +#define TTY_ATTRLIST_END { 0, 1, TTY_ATTR_INIT } + +/* A terminal logical state. */ +struct tty_state { + uint32 modes; + struct tty_attr attr; +}; + +/* Terminal control state. */ +struct tty { + const struct tty_ops *ops; + FILE *fpin, *fpout; + unsigned baud, ht, wd, f; +#define TTF_BORROW 1u + uint32 acaps, ocaps; + struct tty_state st; +}; + struct tty_ops { + void (*release)(struct tty */*tty*/); int (*setattr)(struct tty */*tty*/, const struct gprintf_ops */*gops*/, void */*go*/, - const struct tty_attr */*old*/, - const struct tty_attr */*new*/); + const struct tty_attr */*a*/); int (*setmodes)(struct tty */*tty*/, const struct gprintf_ops */*gops*/, void */*go*/, uint32 /*modes_bic*/, uint32 /*modes_xor*/); @@ -231,42 +262,104 @@ struct tty_ops { int (*ins)(struct tty */*tty*/, const struct gprintf_ops */*gops*/, void */*go*/, unsigned /*f*/, unsigned /*n*/); + int (*inch)(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + int /*ch*/); int (*del)(struct tty */*tty*/, const struct gprintf_ops */*gops*/, void */*go*/, unsigned /*f*/, unsigned /*n*/); - int (*setscrgn)(struct tty */*tty*/, - const struct gprintf_ops */*gops*/, void */*go*/, - unsigned /*y0*/, unsigned /*y1*/); - int (*scroll)(struct tty */*tty*/, - const struct gprintf_ops */*gops*/, void */*go*/, - int /*n*/); + int (*_res0)(void), (*_res1)(void), (*_res2)(void), (*_res3)(void); }; /*----- Functions provided ------------------------------------------------*/ -/* caps +/* --- @tty_clampattr@ --- * + * + * Arguments: @struct tty_attr *a_out@ = selected attributes + * @const struct tty_attr *a@ = requested attributes + * @uint32 acaps@ = terminal's attribute capability mask + * + * Returns: --- * - * RA auto-wrap off - * SA auto-wrap on + * Use: Select the closest approximation to the requested attributes + * which can be accommodated by the terminal. + */ - * AB ansi background - * AF ansi foreground - * md bold - * me clear text highlighting - * mr inverse video - * op default colour pair +extern void tty_clampattr(struct tty_attr */*a_out*/, + const struct tty_attr */*a*/, uint32 /*acaps*/); - * up cursor up 1 line - * cd clear to end-of-display - * ce clear to end-of-line - * cr carriage return - * nw newline +extern int tty_resized(struct tty */*tty*/); - * pc pad character - */ +#define TTF_OPEN 256u +extern struct tty *tty_open(FILE */*fp*/, unsigned /*f*/, + const unsigned */*backends*/); + +extern void tty_close(struct tty */*tty*/); + +extern int tty_setattrg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + const struct tty_attr */*a*/); + +extern int tty_setattr(struct tty */*tty*/, const struct tty_attr */*a*/); + +extern int tty_setattrlistg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + const struct tty_attrlist */*aa*/); + +extern int tty_setattrlist(struct tty */*tty*/, + const struct tty_attrlist */*aa*/); + +extern int tty_setmodesg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + uint32 /*modes_bic*/, uint32 /*modes_xor*/); + +extern int tty_setmodes(struct tty */*tty*/, + uint32 /*modes_bic*/, uint32 /*modes_xor*/); + +extern int tty_moveg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + unsigned /*orig*/, int /*y*/, int /*x*/); + +extern int tty_move(struct tty */*tty*/, + unsigned /*orig*/, int /*y*/, int /*x*/); + +extern int tty_repeatg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + int /*ch*/, unsigned /*n*/); + +extern int tty_repeat(struct tty */*tty*/, int /*ch*/, unsigned /*n*/); + +extern int tty_eraseg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + unsigned /*f*/); + +extern int tty_erase(struct tty */*tty*/, unsigned /*f*/); + +extern int tty_erchg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + unsigned /*n*/); + +extern int tty_erch(struct tty */*tty*/, unsigned /*n*/); + +extern int tty_insg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + unsigned /*f*/, unsigned /*n*/); + +extern int tty_ins(struct tty */*tty*/, unsigned /*f*/, unsigned /*n*/); + +extern int tty_inchg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + int /*ch*/); + +extern int tty_inch(struct tty */*tty*/, int /*ch*/); + +extern int tty_delg(struct tty */*tty*/, + const struct gprintf_ops */*gops*/, void */*go*/, + unsigned /*f*/, unsigned /*n*/); +extern int tty_del(struct tty */*tty*/, unsigned /*f*/, unsigned /*n*/); /*----- That's all, folks -------------------------------------------------*/