16 #define MODE_CURSOR 0x01
17 #define MODE_WRAP 0x02
18 #define MODE_ORIGIN 0x04
19 #define MODE_AUTOCR 0x08
20 #define MODE_DEFAULT (MODE_CURSOR | MODE_WRAP)
21 #define BIT_SET(i, b, val) ((val) ? ((i) | (b)) : ((i) & ~(b)))
22 #define SQRADDR(r, c) (screen + (r) * pad_cols() + (c))
24 static struct term
*term
;
25 static struct square
*screen
;
32 #define MAXLINES (1 << 13)
33 static int dirty
[MAXLINES
];
36 static void _term_show(int r
, int c
, int cursor
)
38 struct square
*sqr
= SQRADDR(r
, c
);
39 int fgcolor
= sqr
->c
? sqr
->fg
: fg
;
40 int bgcolor
= sqr
->c
? sqr
->bg
: bg
;
41 if (cursor
&& mode
& MODE_CURSOR
) {
47 pad_put(sqr
->c
, r
, c
, fgcolor
, bgcolor
);
50 static void _draw_row(int r
)
54 for (i
= 0; i
< pad_cols(); i
++)
59 static void lazy_draw(int sr
, int er
)
64 for (i
= sr
; i
< er
; i
++) {
72 static void lazy_drawcols(int r
, int sc
, int ec
)
80 for (i
= sc
; i
< ec
; i
++)
85 static void lazy_put(int ch
, int r
, int c
)
87 struct square
*sqr
= SQRADDR(r
, c
);
99 static void lazy_cursor(int put
)
106 _term_show(row
, col
, put
);
109 static void lazy_clean()
112 memset(dirty
, 0, sizeof(*dirty
) * MAXLINES
);
115 static void lazy_flush()
120 _term_show(row
, col
, 0);
121 for (i
= 0; i
< pad_rows(); i
++)
125 _term_show(row
, col
, 1);
128 static void term_redraw(void)
131 for (i
= 0; i
< pad_rows(); i
++)
133 _term_show(row
, col
, 1);
136 static int origin(void)
138 return mode
& MODE_ORIGIN
;
141 static void setsize(void)
144 size
.ws_col
= pad_cols();
145 size
.ws_row
= pad_rows();
148 ioctl(term
->fd
, TIOCSWINSZ
, &size
);
151 #define PTYBUFSIZE (1 << 13)
152 static char ptybuf
[PTYBUFSIZE
];
155 static void waitpty(void)
157 struct pollfd ufds
[1];
158 ufds
[0].fd
= term
->fd
;
159 ufds
[0].events
= POLLIN
;
163 static int readpty(void)
166 return ptybuf
[ptycur
++];
169 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
170 if (ptylen
== -1 && errno
== EAGAIN
) {
172 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
181 static void empty_rows(int sr
, int er
)
183 memset(SQRADDR(sr
, 0), 0, (er
- sr
) * sizeof(*screen
) * pad_cols());
186 static void blank_rows(int sr
, int er
)
193 static void scroll_screen(int sr
, int nr
, int n
)
196 memmove(SQRADDR(sr
+ n
, 0), SQRADDR(sr
, 0),
197 nr
* pad_cols() * sizeof(*screen
));
199 empty_rows(sr
, sr
+ n
);
201 empty_rows(sr
+ nr
+ n
, sr
+ nr
);
202 lazy_draw(MIN(sr
, sr
+ n
), MAX(sr
+ nr
, sr
+ nr
+ n
));
206 static void insert_lines(int n
)
208 int sr
= MAX(top
, row
);
209 int nr
= bot
- row
- n
;
211 scroll_screen(sr
, nr
, n
);
214 static void delete_lines(int n
)
216 int r
= MAX(top
, row
);
218 int nr
= bot
- r
- n
;
220 scroll_screen(sr
, nr
, -n
);
223 static void move_cursor(int r
, int c
)
227 t
= origin() ? top
: 0;
228 b
= origin() ? bot
: pad_rows();
229 row
= MAX(t
, MIN(r
, b
- 1));
230 col
= MAX(0, MIN(c
, pad_cols() - 1));
234 static void advance(int dr
, int dc
, int scrl
)
238 if (c
>= pad_cols()) {
239 if (scrl
&& mode
& MODE_WRAP
) {
246 if (r
>= bot
&& scrl
) {
248 int nr
= (bot
- top
) + n
;
249 scroll_screen(top
+ -n
, nr
, n
);
251 if (r
< top
&& scrl
) {
253 int nr
= (bot
- top
) - n
;
254 scroll_screen(top
, nr
, n
);
256 r
= MIN(bot
- 1, MAX(top
, r
));
257 move_cursor(r
, MAX(0, c
));
260 void term_send(int c
)
262 unsigned char b
= (unsigned char) c
;
264 write(term
->fd
, &b
, 1);
267 static void term_sendstr(char *s
)
270 write(term
->fd
, s
, strlen(s
));
273 static void setmode(int m
)
286 if (m
>= 30 && m
<= 37)
288 if (m
>= 40 && m
<= 47)
292 static void kill_chars(int sc
, int ec
)
295 for (i
= sc
; i
< ec
; i
++)
296 lazy_put(' ', row
, i
);
300 static void move_chars(int sc
, int nc
, int n
)
303 memmove(SQRADDR(row
, sc
+ n
), SQRADDR(row
, sc
), nc
* sizeof(*screen
));
305 memset(SQRADDR(row
, sc
), 0, n
* sizeof(*screen
));
307 memset(SQRADDR(row
, pad_cols() + n
), 0, -n
* sizeof(*screen
));
308 lazy_drawcols(row
, MIN(sc
, sc
+ n
), pad_cols());
312 static void delete_chars(int n
)
315 int nc
= pad_cols() - sc
;
316 move_chars(sc
, nc
, -n
);
319 static void insert_chars(int n
)
321 int nc
= pad_cols() - col
- n
;
322 move_chars(col
, nc
, n
);
325 static void term_blank(void)
327 memset(screen
, 0, MAXCHARS
* sizeof(*screen
));
334 static void ctlseq(void);
339 while (ptycur
< ptylen
)
345 void term_exec(char *cmd
)
347 memset(term
, 0, sizeof(*term
));
348 term
->cur
.bot
= term
->sav
.bot
= pad_rows();
349 term
->cur
.mode
= MODE_DEFAULT
;
350 term_load(term
, visible
);
351 if ((term
->pid
= forkpty(&term
->fd
, NULL
, NULL
, NULL
)) == -1) {
352 perror("failed to fork");
357 setenv("TERM", "linux", 1);
358 execlp(cmd
, cmd
, NULL
);
361 fcntl(term
->fd
, F_SETFD
, fcntl(term
->fd
, F_GETFD
) | FD_CLOEXEC
);
362 fcntl(term
->fd
, F_SETFL
, fcntl(term
->fd
, F_GETFL
) | O_NONBLOCK
);
368 static void misc_save(struct term_state
*state
)
379 static void misc_load(struct term_state
*state
)
390 void term_save(struct term
*term
)
393 misc_save(&term
->cur
);
396 void term_load(struct term
*t
, int flags
)
399 misc_load(&term
->cur
);
400 screen
= term
->screen
;
402 if (flags
== TERM_REDRAW
) {
421 static void set_region(int t
, int b
)
423 top
= MIN(pad_rows(), MAX(0, t
- 1));
424 bot
= MIN(pad_rows(), MAX(0, b
? b
: pad_rows()));