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
++) {
55 struct square
*s
= SQRADDR(r
, i
);
56 if (s
->c
&& (s
->c
!= ' ' || s
->bg
!= bg
))
61 static void lazy_draw(int sr
, int er
)
66 for (i
= sr
; i
< er
; i
++) {
74 static void lazy_drawcols(int r
, int sc
, int ec
)
82 for (i
= sc
; i
< ec
; i
++)
87 static void lazy_put(int ch
, int r
, int c
)
89 struct square
*sqr
= SQRADDR(r
, c
);
101 static void lazy_cursor(int put
)
108 _term_show(row
, col
, put
);
111 static void lazy_clean()
114 memset(dirty
, 0, sizeof(*dirty
) * MAXLINES
);
117 static void lazy_flush()
122 _term_show(row
, col
, 0);
123 for (i
= 0; i
< pad_rows(); i
++)
127 _term_show(row
, col
, 1);
130 static void term_redraw(void)
133 for (i
= 0; i
< pad_rows(); i
++)
135 _term_show(row
, col
, 1);
138 static int origin(void)
140 return mode
& MODE_ORIGIN
;
143 static void setsize(void)
146 size
.ws_col
= pad_cols();
147 size
.ws_row
= pad_rows();
150 ioctl(term
->fd
, TIOCSWINSZ
, &size
);
153 #define PTYBUFSIZE (1 << 13)
154 static char ptybuf
[PTYBUFSIZE
];
157 static void waitpty(void)
159 struct pollfd ufds
[1];
160 ufds
[0].fd
= term
->fd
;
161 ufds
[0].events
= POLLIN
;
165 static int readpty(void)
168 return ptybuf
[ptycur
++];
171 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
172 if (ptylen
== -1 && errno
== EAGAIN
) {
174 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
183 static void empty_rows(int sr
, int er
)
185 memset(SQRADDR(sr
, 0), 0, (er
- sr
) * sizeof(*screen
) * pad_cols());
188 static void blank_rows(int sr
, int er
)
195 static void scroll_screen(int sr
, int nr
, int n
)
198 memmove(SQRADDR(sr
+ n
, 0), SQRADDR(sr
, 0),
199 nr
* pad_cols() * sizeof(*screen
));
201 empty_rows(sr
, sr
+ n
);
203 empty_rows(sr
+ nr
+ n
, sr
+ nr
);
204 lazy_draw(MIN(sr
, sr
+ n
), MAX(sr
+ nr
, sr
+ nr
+ n
));
208 static void insert_lines(int n
)
210 int sr
= MAX(top
, row
);
211 int nr
= bot
- row
- n
;
213 scroll_screen(sr
, nr
, n
);
216 static void delete_lines(int n
)
218 int r
= MAX(top
, row
);
220 int nr
= bot
- r
- n
;
222 scroll_screen(sr
, nr
, -n
);
225 static void move_cursor(int r
, int c
)
229 t
= origin() ? top
: 0;
230 b
= origin() ? bot
: pad_rows();
231 row
= MAX(t
, MIN(r
, b
- 1));
232 col
= MAX(0, MIN(c
, pad_cols() - 1));
236 static void advance(int dr
, int dc
, int scrl
)
240 if (c
>= pad_cols()) {
241 if (scrl
&& mode
& MODE_WRAP
) {
248 if (r
>= bot
&& scrl
) {
250 int nr
= (bot
- top
) + n
;
251 scroll_screen(top
+ -n
, nr
, n
);
253 if (r
< top
&& scrl
) {
255 int nr
= (bot
- top
) - n
;
256 scroll_screen(top
, nr
, n
);
258 r
= MIN(bot
- 1, MAX(top
, r
));
259 move_cursor(r
, MAX(0, c
));
262 void term_send(int c
)
264 unsigned char b
= (unsigned char) c
;
266 write(term
->fd
, &b
, 1);
269 static void term_sendstr(char *s
)
272 write(term
->fd
, s
, strlen(s
));
275 static void setattr(int m
)
289 fg
= m
> 37 ? FGCOLOR
: m
- 30;
291 bg
= m
> 47 ? BGCOLOR
: m
- 40;
294 static void kill_chars(int sc
, int ec
)
297 for (i
= sc
; i
< ec
; i
++)
298 lazy_put(' ', row
, i
);
302 static void move_chars(int sc
, int nc
, int n
)
305 memmove(SQRADDR(row
, sc
+ n
), SQRADDR(row
, sc
), nc
* sizeof(*screen
));
307 memset(SQRADDR(row
, sc
), 0, n
* sizeof(*screen
));
309 memset(SQRADDR(row
, pad_cols() + n
), 0, -n
* sizeof(*screen
));
310 lazy_drawcols(row
, MIN(sc
, sc
+ n
), pad_cols());
314 static void delete_chars(int n
)
317 int nc
= pad_cols() - sc
;
318 move_chars(sc
, nc
, -n
);
321 static void insert_chars(int n
)
323 int nc
= pad_cols() - col
- n
;
324 move_chars(col
, nc
, n
);
327 static void term_blank(void)
329 memset(screen
, 0, MAXCHARS
* sizeof(*screen
));
336 static void ctlseq(void);
341 while (ptycur
< ptylen
)
347 void term_exec(char *cmd
)
349 memset(term
, 0, sizeof(*term
));
350 term
->cur
.bot
= term
->sav
.bot
= pad_rows();
351 term
->cur
.mode
= MODE_DEFAULT
;
352 term_load(term
, visible
);
353 if ((term
->pid
= forkpty(&term
->fd
, NULL
, NULL
, NULL
)) == -1) {
354 perror("failed to fork");
359 setenv("TERM", "linux", 1);
360 execlp(cmd
, cmd
, NULL
);
363 fcntl(term
->fd
, F_SETFD
, fcntl(term
->fd
, F_GETFD
) | FD_CLOEXEC
);
364 fcntl(term
->fd
, F_SETFL
, fcntl(term
->fd
, F_GETFL
) | O_NONBLOCK
);
370 static void misc_save(struct term_state
*state
)
381 static void misc_load(struct term_state
*state
)
392 void term_save(struct term
*term
)
395 misc_save(&term
->cur
);
398 void term_load(struct term
*t
, int flags
)
401 misc_load(&term
->cur
);
402 screen
= term
->screen
;
404 if (flags
== TERM_REDRAW
) {
423 static void set_region(int t
, int b
)
425 top
= MIN(pad_rows(), MAX(0, t
- 1));
426 bot
= MIN(pad_rows(), MAX(0, b
? b
: pad_rows()));