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
)
53 for (i
= 0; i
< pad_cols(); i
++)
57 static void lazy_draw(int sr
, int er
)
62 for (i
= sr
; i
< er
; i
++) {
70 static void lazy_drawcols(int r
, int sc
, int ec
)
78 for (i
= sc
; i
< ec
; i
++)
83 static void lazy_put(int ch
, int r
, int c
)
85 struct square
*sqr
= SQRADDR(r
, c
);
97 static void lazy_cursor(int put
)
104 _term_show(row
, col
, put
);
107 static void lazy_clean()
110 memset(dirty
, 0, sizeof(*dirty
) * MAXLINES
);
113 static void lazy_flush()
118 _term_show(row
, col
, 0);
119 for (i
= 0; i
< pad_rows(); i
++)
123 _term_show(row
, col
, 1);
126 static int origin(void)
128 return mode
& MODE_ORIGIN
;
131 static void setsize(void)
134 size
.ws_col
= pad_cols();
135 size
.ws_row
= pad_rows();
138 ioctl(term
->fd
, TIOCSWINSZ
, &size
);
141 #define PTYBUFSIZE (1 << 13)
142 static char ptybuf
[PTYBUFSIZE
];
145 static void waitpty(void)
147 struct pollfd ufds
[1];
148 ufds
[0].fd
= term
->fd
;
149 ufds
[0].events
= POLLIN
;
153 static int readpty(void)
156 return ptybuf
[ptycur
++];
159 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
160 if (ptylen
== -1 && errno
== EAGAIN
) {
162 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
171 static void empty_rows(int sr
, int er
)
173 memset(SQRADDR(sr
, 0), 0, (er
- sr
) * sizeof(*screen
) * pad_cols());
176 static void blank_rows(int sr
, int er
)
183 static void scroll_screen(int sr
, int nr
, int n
)
186 memmove(SQRADDR(sr
+ n
, 0), SQRADDR(sr
, 0),
187 nr
* pad_cols() * sizeof(*screen
));
189 empty_rows(sr
, sr
+ n
);
191 empty_rows(sr
+ nr
+ n
, sr
+ nr
);
192 lazy_draw(MIN(sr
, sr
+ n
), MAX(sr
+ nr
, sr
+ nr
+ n
));
196 static void insert_lines(int n
)
198 int sr
= MAX(top
, row
);
199 int nr
= bot
- row
- n
;
201 scroll_screen(sr
, nr
, n
);
204 static void delete_lines(int n
)
206 int r
= MAX(top
, row
);
208 int nr
= bot
- r
- n
;
210 scroll_screen(sr
, nr
, -n
);
213 static void move_cursor(int r
, int c
)
217 t
= origin() ? top
: 0;
218 b
= origin() ? bot
: pad_rows();
219 row
= MAX(t
, MIN(r
, b
- 1));
220 col
= MAX(0, MIN(c
, pad_cols() - 1));
224 static void advance(int dr
, int dc
, int scrl
)
228 if (c
>= pad_cols()) {
229 if (scrl
&& mode
& MODE_WRAP
) {
236 if (r
>= bot
&& scrl
) {
238 int nr
= (bot
- top
) + n
;
239 scroll_screen(top
+ -n
, nr
, n
);
241 if (r
< top
&& scrl
) {
243 int nr
= (bot
- top
) - n
;
244 scroll_screen(top
, nr
, n
);
246 r
= MIN(bot
- 1, MAX(top
, r
));
247 move_cursor(r
, MAX(0, c
));
250 void term_send(int c
)
252 unsigned char b
= (unsigned char) c
;
254 write(term
->fd
, &b
, 1);
257 static void term_sendstr(char *s
)
260 write(term
->fd
, s
, strlen(s
));
263 static void setmode(int m
)
276 if (m
>= 30 && m
<= 37)
278 if (m
>= 40 && m
<= 47)
282 static void kill_chars(int sc
, int ec
)
285 for (i
= sc
; i
< ec
; i
++)
286 lazy_put(' ', row
, i
);
290 static void move_chars(int sc
, int nc
, int n
)
293 memmove(SQRADDR(row
, sc
+ n
), SQRADDR(row
, sc
), nc
* sizeof(*screen
));
295 memset(SQRADDR(row
, sc
), 0, n
* sizeof(*screen
));
297 memset(SQRADDR(row
, pad_cols() + n
), 0, -n
* sizeof(*screen
));
298 lazy_drawcols(row
, MIN(sc
, sc
+ n
), pad_cols());
302 static void delete_chars(int n
)
305 int nc
= pad_cols() - sc
;
306 move_chars(sc
, nc
, -n
);
309 static void insert_chars(int n
)
311 int nc
= pad_cols() - col
- n
;
312 move_chars(col
, nc
, n
);
315 static void term_blank(void)
317 memset(screen
, 0, MAXCHARS
* sizeof(*screen
));
324 static void ctlseq(void);
329 while (ptycur
< ptylen
)
335 void term_exec(char *cmd
)
337 memset(term
, 0, sizeof(*term
));
338 term
->cur
.bot
= term
->sav
.bot
= pad_rows();
339 term
->cur
.mode
= MODE_DEFAULT
;
340 term_load(term
, visible
);
341 if ((term
->pid
= forkpty(&term
->fd
, NULL
, NULL
, NULL
)) == -1) {
342 perror("failed to fork");
347 setenv("TERM", "linux", 1);
348 execlp(cmd
, cmd
, NULL
);
351 fcntl(term
->fd
, F_SETFD
, fcntl(term
->fd
, F_GETFD
) | FD_CLOEXEC
);
352 fcntl(term
->fd
, F_SETFL
, fcntl(term
->fd
, F_GETFL
) | O_NONBLOCK
);
358 static void misc_save(struct term_state
*state
)
369 static void misc_load(struct term_state
*state
)
380 void term_save(struct term
*term
)
383 misc_save(&term
->cur
);
386 void term_load(struct term
*t
, int flags
)
389 misc_load(&term
->cur
);
390 screen
= term
->screen
;
392 if (flags
== TERM_REDRAW
) {
394 lazy_draw(0, pad_rows());
413 static void set_region(int t
, int b
)
415 top
= MIN(pad_rows(), MAX(0, t
- 1));
416 bot
= MIN(pad_rows(), MAX(0, b
? b
: pad_rows()));