chiark / gitweb /
@@@ more mess
[mLib] / ui / tty.h
1 /* -*-c-*-
2  *
3  * Terminal handling
4  *
5  * (c) 2024 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 #ifndef MLIB_TTY_H
29 #define MLIB_TTY_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdio.h>
38
39 #ifndef MLIB_BITS_H
40 #  include "bits.h"
41 #endif
42
43 /*----- Data structures ---------------------------------------------------*/
44
45 /* Attributes. */
46 #define TTAF_LNMASK     0x0003u         /* line style mask */
47 #define TTAF_LNSHIFT    0               /* line style shift */
48 enum {
49   TTLN_NONE,                            /*   no line */
50   TTLN_ULINE,                           /*   underline */
51   TTLN_UULINE,                          /*   double underline */
52   TTLN_STRIKE,                          /*   strikeout */
53   TTLN_LIMIT
54 };
55 #define TTAF_WTMASK     0x000cu         /* weight mask */
56 #define TTAG_WTSHIFT    2               /* weight shift */
57 enum {
58   TTWT_MED,                             /*   medium */
59   TTWT_BOLD,                            /*   bold/bright */
60   TTWT_DIM,                             /*   light/dim */
61   TTWT_LIMIT
62 };
63 #define TTAF_ITAL       0x0010u         /* italic/oblique */
64 #define TTAF_INVV       0x0020u         /* inverse video */
65 #define TTAF_FGSPCMASK  0x1c00u         /* foreground space mask */
66 #define TTAF_FGSPCSHIFT 10              /* foreground space shift */
67 #define TTAF_BGSPCMASK  0xe000u         /* background space mask */
68 #define TTAF_BGSPCSHIFT 13              /* background space shift */
69 enum {
70   TTCSPC_NONE,                          /* no colour */
71   TTCSPC_1BPC,                          /* one bit per channel */
72   TTCSPC_1BPCBR,                        /* one bit per channel, brighter */
73   TTCSPC_2BPC,                          /* two bits levels per channel */
74   TTCSPC_8LGS,                          /* eight levels greyscale */
75   TTCSPC_6LPC,                          /* six levels per channel */
76   TTCSPC_24LGS,                         /* 24 levels greyscale */
77   TTCSPC_8BPC                           /* eight bits per channel */
78   TTCSPC_LIMIT
79 };
80
81 /* Colours. */
82 #define TT1BC_RED       1u
83 #define TT1BC_GREEN     2u
84 #define TT1BC_BLUE      4u
85 #define TT1BC_BRIGHT    8u
86 #define TTCOL_BLACK     0u
87 #define TTCOL_RED       (TT1BC_RED)
88 #define TTCOL_GREEN     (TT1BC_GREEN)
89 #define TTCOL_YELLOW    (TT1BC_RED | TT1BC_GREEN)
90 #define TTCOL_BLUE      (TT1BC_BLUE)
91 #define TTCOL_MAGENTA   (TT1BC_RED | TT1BC_BLUE)
92 #define TTCOL_CYAN      (TT1BC_GREEN | TT1BC_BLUE)
93 #define TTCOL_WHITE     (TT1BC_RED | TT1BC_GREEN | TT1BC_BLUE)
94 #define TTCOL_BRBLACK   (TTCOL_BLACK | TT1BC_BRIGHT)
95 #define TTCOL_BRRED     (TTCOL_RED | TT1BC_BRIGHT)
96 #define TTCOL_BRGREEN   (TTCOL_GREEN | TT1BC_BRIGHT)
97 #define TTCOL_BRYELLOW  (TTCOL_YELLOW | TT1BC_BRIGHT)
98 #define TTCOL_BRBLUE    (TTCOL_BLUE | TT1BC_BRIGHT)
99 #define TTCOL_BRMAGENTA (TTCOL_MAGENTA | TT1BC_BRIGHT)
100 #define TTCOL_BRCYAN    (TTCOL_CYAN | TT1BC_BRIGHT)
101 #define TTCOL_BRWHITE   (TTCOL_WHITE | TT1BC_BRIGHT)
102
103 #define TTCOL_MK2B(r, g, b) (((r) << 4) | ((g) <<  2) | ((b) <<  0))
104 #define TTCOL_2BR(col) (((col) >> 4)&0x03)
105 #define TTCOL_2BG(col) (((col) >> 2)&0x03)
106 #define TTCOL_2BB(col) (((col) >> 0)&0x03)
107
108 #define TTCOL_MK6L(r, g, b) (36*(r) + 6*(g) + (b))
109 #define TTCOL_6LR(col) ((col)/36)
110 #define TTCOL_6LG(col) (((col)/6)%6)
111 #define TTCOL_6LB(col) ((col)%6)
112
113 #define TTCOL_MK8B(r, g, b) (((r) << 16) | ((g) <<  8) | ((b) <<  0))
114 #define TTCOL_8BR(col) (((col) >> 16)&0xff)
115 #define TTCOL_8BG(col) (((col) >>  8)&0xff)
116 #define TTCOL_8BB(col) (((col) >>  0)&0xff)
117
118 struct tty_attr {
119   uint32 f, _res0;                      /* attribute flags, reserved */
120   uint32 fg, bg;                        /* foreground/background colours */
121 };
122
123 /* Mode settings. */
124 #define TTMF_AUTOM      0x00000001u     /* automatic margins */
125 #define TTMF_FSCRM      0x00000002u     /* full-screen mode */
126 #define TTMF_INS        0x00000004u     /* insert mode */
127
128 /* Attribute capabilities. */
129 #define TTACF_ULINE     0x00000001u     /* underline */
130 #define TTACF_UULINE    0x00000002u     /* double underline */
131 #define TTACF_STRIKE    0x00000004u     /* strikeout */
132 #define TTACF_BOLD      0x00000008u     /* bold/bright */
133 #define TTACF_DIM       0x00000010u     /* light/dim */
134 #define TTACF_ITAL      0x00000020u     /* italic/oblique */
135 #define TTACF_INVV      0x00000040u     /* inverse video */
136 #define TTACF_FG        0x00000080u     /* set foreground colour */
137 #define TTACF_BG        0x00000100u     /* set background colour */
138 #define TTACF_1BPC      0x00000200u     /* one-bit-per-channel space */
139 #define TTACF_1BPCBR    0x00000400u    /* one-bit-per-channel bright space */
140 #define TTACF_2BPC      0x00000800u     /* two-bits-per-channel space */
141 #define TTACF_8LGS      0x00001000u     /* 8-levels-greyscale space */
142 #define TTACF_6LPC      0x00002000u     /* six-levels-per-channel space */
143 #define TTACF_24LGS     0x00004000u     /* 24-levels-greyscale space */
144 #define TTACF_8BPC      0x00008000u     /* eight-bits-per-channel space */
145
146 /* Other capabilities. */
147 #define TTCF_RELMV      0x00000100u     /* relative cursor motion */
148 #define TTCF_ABSMV      0x00000200u     /* absolute cursor motion */
149 #define TTCF_MIXMV      0x00000400u     /* mixed cursor motion */
150 #define TTCF_MVINS      0x00000800u     /* motion preserves insert mode */
151 #define TTCF_MVATTR     0x00001000u     /* motion preserves attributes */
152 #define TTCF_MMARG      0x00002000u     /* proper magic margins */
153 #define TTCF_SCRGN      0x00004000u     /* scroll regions */
154 #define TTCF_SCROLL     0x00008000u     /* explicit scrolling */
155 #define TTCF_BGER       0x00010000u     /* erasure uses background colour */
156 #define TTCF_ERCH       0x00020000u     /* erase characters */
157 #define TTCF_ERBOL      0x00040000u     /* erase from beginning of line */
158 #define TTCF_EREOL      0x00080000u     /* erase to end of line */
159 #define TTCF_ERBOD      0x00100000u     /* erase from beginning of display */
160 #define TTCF_EREOD      0x00200000u     /* erase to end of display */
161 #define TTCF_ERDSP      0x00400000u     /* erase display */
162 #define TTCF_INSCH      0x00800000u     /* insert character */
163 #define TTCF_INSLN      0x01000000u     /* insert line */
164 #define TTCF_DELCH      0x02000000u     /* delete character */
165 #define TTCF_DELLN      0x04000000u     /* delete line */
166
167 struct tty_attrlist {
168   uint32 cap_mask, cap_eq;              /* capabilities to select */
169   struct tty_attr attr;                 /* attributes to set */
170 };
171 #define TTY_ATTRLIST_END { 0, 0, { 0, 0, 0, 0 } }
172
173 /* Terminal capability backend libraries. */
174 enum {
175   TTLIB_TERMCAP,                        /* traditional `termcap' */
176   TTLIB_TERMINFO,                       /* standard `terminfo' */
177   TTLIB_UNIBILIUM,                      /* `Unibiliium' */
178   TTLIB_XTERM,                       /* assume modern terminal conventions */
179   TTLIB_LIMIT
180 };
181
182 struct tty_state {
183   uint32 modes;
184   struct tty_attr attr;
185 };
186
187 struct tty {
188   const struct tty_ops *ops;
189   FILE *fp;
190   unsigned baud, ht, wd;
191   uint32 acaps, ocaps;
192   struct tty_state st;
193 };
194
195 /* Motion origin. */
196 #define TTOF_XHOME 0u                   /* absolute horizontal motion */
197 #define TTOF_XCUR 1u                    /* relative horizontal motion */
198 #define TTOF_YHOME 0u                   /* absolute vertical motion */
199 #define TTOF_YCUR 2u                    /* relative vertical motion */
200
201 /* Erasure scope. */
202 #define TTEF_LINE 0u                    /* erase within line */
203 #define TTEF_DSP 1u                     /* erase whole display */
204 #define TTEF_BEGIN 2u                   /* erase from beginning */
205 #define TTEF_END 4u                     /* erase to end */
206
207 /* Insertion and deletion. */
208 #define TTIDF_CH 0u                     /* insert/delete characters */
209 #define TTIDF_LN 1u                     /* insert/delete lines */
210
211 struct tty_ops {
212   int (*setattr)(struct tty */*tty*/,
213                  const struct gprintf_ops */*gops*/, void */*go*/,
214                  const struct tty_attr */*old*/,
215                  const struct tty_attr */*new*/);
216   int (*setmodes)(struct tty */*tty*/,
217                   const struct gprintf_ops */*gops*/, void */*go*/,
218                   uint32 /*modes_bic*/, uint32 /*modes_xor*/);
219   int (*move)(struct tty */*tty*/,
220               const struct gprintf_ops */*gops*/, void */*go*/,
221               unsigned /*orig*/, int /*y*/, int /*x*/);
222   int (*repeat)(struct tty */*tty*/,
223                 const struct gprintf_ops */*gops*/, void */*go*/,
224                 int /*ch*/, unsigned /*n*/);
225   int (*erase)(struct tty */*tty*/,
226                const struct gprintf_ops */*gops*/, void */*go*/,
227                unsigned /*f*/);
228   int (*erch)(struct tty */*tty*/,
229               const struct gprintf_ops */*gops*/, void */*go*/,
230               unsigned /*n*/);
231   int (*ins)(struct tty */*tty*/,
232              const struct gprintf_ops */*gops*/, void */*go*/,
233              unsigned /*f*/, unsigned /*n*/);
234   int (*del)(struct tty */*tty*/,
235              const struct gprintf_ops */*gops*/, void */*go*/,
236              unsigned /*f*/, unsigned /*n*/);
237   int (*setscrgn)(struct tty */*tty*/,
238                   const struct gprintf_ops */*gops*/, void */*go*/,
239                   unsigned /*y0*/, unsigned /*y1*/);
240   int (*scroll)(struct tty */*tty*/,
241                 const struct gprintf_ops */*gops*/, void */*go*/,
242                 int /*n*/);
243 };
244
245 /*----- Functions provided ------------------------------------------------*/
246
247 /* caps
248  *
249  * RA   auto-wrap off
250  * SA   auto-wrap on
251
252  * AB   ansi background
253  * AF   ansi foreground
254  * md   bold
255  * me   clear text highlighting
256  * mr   inverse video
257  * op   default colour pair
258
259  * up   cursor up 1 line
260
261  * cd   clear to end-of-display
262  * ce   clear to end-of-line
263
264  * cr   carriage return
265  * nw   newline
266
267  * pc   pad character
268  */
269
270
271 /*----- That's all, folks -------------------------------------------------*/
272
273 #ifdef __cplusplus
274   }
275 #endif
276
277 #endif