Xinqi Bao's Git

backport of local changes
[st.git] / st.h
1 /* See LICENSE for licence details. */
2
3 #define _XOPEN_SOURCE
4 #include <ctype.h>
5 #include <fcntl.h>
6 #include <locale.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/select.h>
15 #include <sys/ioctl.h>
16 #include <X11/Xlib.h>
17 #include <X11/keysym.h>
18 #include <X11/Xutil.h>
19
20 /* special keys */
21 #define KEYDELETE "\033[3~"
22 #define KEYHOME "\033[1~"
23 #define KEYEND "\033[4~"
24 #define KEYPREV "\033[5~"
25 #define KEYNEXT "\033[6~"
26
27 #define TNAME "st"
28 #define SHELL "/bin/bash"
29 #define TAB 8
30
31 #define FONT "-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*"
32 #define BORDER 3
33 #define LINESPACE 1 /* additional pixel between each line */
34
35 /* Default colors */
36 #define DefaultFG 7
37 #define DefaultBG 0
38 #define DefaultCS 1
39 #define BellCol DefaultFG /* visual bell color */
40
41 static char* colorname[] = {
42 "black",
43 "red",
44 "green",
45 "yellow",
46 "blue",
47 "magenta",
48 "cyan",
49 "white",
50 };
51
52
53 /* Arbitrary sizes */
54 #define ESCSIZ 256
55 #define ESCARG 16
56
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 #define MAX(a, b) ((a) < (b) ? (b) : (a))
59 #define LEN(a) (sizeof(a) / sizeof(a[0]))
60 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
61 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
62 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
63
64
65 enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 }; /* Attribute */
66 enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload }; /* Cursor */
67 enum { CRset=1 , CRupdate=2 }; /* Character state */
68 enum { TMwrap=1 , TMinsert=2 }; /* Terminal mode */
69 enum { SCupdate, SCredraw }; /* screen draw mode */
70
71 #ifdef TRUECOLOR
72 #error Truecolor not implemented yet
73 typedef int Color;
74 #else
75 typedef char Color;
76 #endif
77
78
79 typedef struct {
80 char c; /* character code */
81 char mode; /* attribute flags */
82 Color fg; /* foreground */
83 Color bg; /* background */
84 char state; /* state flag */
85 } Glyph;
86
87 typedef Glyph* Line;
88
89 typedef struct {
90 Glyph attr; /* current char attributes */
91 char hidden;
92 int x;
93 int y;
94 } TCursor;
95
96 /* Escape sequence structs */
97 typedef struct {
98 char buf[ESCSIZ+1]; /* raw string */
99 int len; /* raw string length */
100 /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
101 char pre;
102 char priv;
103 int arg[ESCARG+1];
104 int narg; /* nb of args */
105 char mode;
106 } Escseq;
107
108 /* Internal representation of the screen */
109 typedef struct {
110 int row; /* nb row */
111 int col; /* nb col */
112 Line* line; /* screen */
113 TCursor c; /* cursor */
114 int top; /* top scroll limit */
115 int bot; /* bottom scroll limit */
116 int mode; /* terminal mode */
117 } Term;
118
119 /* Purely graphic info */
120 typedef struct {
121 Display* dis;
122 Window win;
123 int scr;
124 int w; /* window width */
125 int h; /* window height */
126 int ch; /* char height */
127 int cw; /* char width */
128 } XWindow;
129
130 /* Drawing Context */
131 typedef struct {
132 unsigned long col[LEN(colorname)];
133 XFontStruct* font;
134 GC gc;
135 } DC;
136
137
138 void die(const char *errstr, ...);
139 void draw(int);
140 void execsh(void);
141 void kpress(XKeyEvent *);
142 void resize(XEvent *);
143 void run(void);
144
145 int escaddc(char);
146 int escfinal(char);
147 void escdump(void);
148 void eschandle(void);
149 void escparse(void);
150 void escreset(void);
151
152 void tclearregion(int, int, int, int);
153 void tcpos(int);
154 void tcursor(int);
155 void tdeletechar(int);
156 void tdeleteline(int);
157 void tdump(void);
158 void tinsertblank(int);
159 void tinsertblankline(int);
160 void tmoveto(int, int);
161 void tnew(int, int);
162 void tnewline(void);
163 void tputc(char);
164 void tputs(char*, int);
165 void tresize(int, int);
166 void tscroll(void);
167 void tsetattr(int*, int);
168 void tsetchar(char);
169 void tsetscroll(int, int);
170
171 void ttynew(void);
172 void ttyread(void);
173 void ttyresize(int, int);
174 void ttywrite(char *, size_t);
175
176 unsigned long xgetcol(const char *);
177 void xclear(int, int, int, int);
178 void xcursor(int);
179 void xdrawc(int, int, Glyph);
180 void xinit(void);
181 void xscroll(void);