16 #define MODE_NOCURSOR 0x01
17 #define MODE_NOWRAP 0x02
18 #define MODE_ORIGIN 0x04
19 #define MODE_NOAUTOCR 0x08
20 #define BIT_SET(i, b, val) ((val) ? ((i) | (b)) : ((i) & ~(b)))
21 #define SQRADDR(r, c) (&screen[(r) * pad_cols() + (c)])
23 static struct term
*term
;
24 static struct square
*screen
;
31 #define MAXLINES (1 << 13)
32 static int dirty
[MAXLINES
];
35 static void _term_show(int r
, int c
, int cursor
)
37 struct square
*sqr
= SQRADDR(r
, c
);
38 int fgcolor
= sqr
->c
? sqr
->fg
: fg
;
39 int bgcolor
= sqr
->c
? sqr
->bg
: bg
;
40 if (cursor
&& !(mode
& MODE_NOCURSOR
)) {
46 pad_put(sqr
->c
, r
, c
, fgcolor
, bgcolor
);
49 static void _draw_row(int r
)
52 for (i
= 0; i
< pad_cols(); i
++)
56 static void lazy_draw(int sr
, int er
)
61 for (i
= sr
; i
< er
; i
++) {
69 static void lazy_drawcols(int r
, int sc
, int ec
)
77 for (i
= sc
; i
< ec
; i
++)
82 static void lazy_put(int ch
, int r
, int c
)
84 struct square
*sqr
= SQRADDR(r
, c
);
96 static void lazy_cursor(int put
)
103 _term_show(row
, col
, put
);
106 static void lazy_clean()
109 memset(dirty
, 0, sizeof(*dirty
) * MAXLINES
);
112 static void lazy_flush()
117 _term_show(row
, col
, 0);
118 for (i
= 0; i
< pad_rows(); i
++)
122 _term_show(row
, col
, 1);
125 static int origin(void)
127 return mode
& MODE_ORIGIN
;
130 static void setsize(void)
133 size
.ws_col
= pad_cols();
134 size
.ws_row
= pad_rows();
137 ioctl(term
->fd
, TIOCSWINSZ
, &size
);
140 #define PTYBUFSIZE (1 << 13)
141 static char ptybuf
[PTYBUFSIZE
];
144 static void waitpty(void)
146 struct pollfd ufds
[1];
147 ufds
[0].fd
= term
->fd
;
148 ufds
[0].events
= POLLIN
;
152 static int readpty(void)
155 return ptybuf
[ptycur
++];
156 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
157 if (ptylen
== -1 && errno
== EAGAIN
) {
159 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
168 static void empty_rows(int sr
, int er
)
170 memset(SQRADDR(sr
, 0), 0, (er
- sr
) * sizeof(*screen
) * pad_cols());
173 static void blank_rows(int sr
, int er
)
180 static void scroll_screen(int sr
, int nr
, int n
)
183 memmove(SQRADDR(sr
+ n
, 0), SQRADDR(sr
, 0),
184 nr
* pad_cols() * sizeof(*screen
));
186 empty_rows(sr
, sr
+ n
);
188 empty_rows(sr
+ nr
+ n
, sr
+ nr
);
189 lazy_draw(MIN(sr
, sr
+ n
), MAX(sr
+ nr
, sr
+ nr
+ n
));
193 static void insert_lines(int n
)
195 int sr
= MAX(top
, row
);
196 int nr
= bot
- row
- n
;
198 scroll_screen(sr
, nr
, n
);
201 static void delete_lines(int n
)
203 int r
= MAX(top
, row
);
205 int nr
= bot
- r
- n
;
207 scroll_screen(sr
, nr
, -n
);
210 static void move_cursor(int r
, int c
)
214 t
= origin() ? top
: 0;
215 b
= origin() ? bot
: pad_rows();
216 row
= MAX(t
, MIN(r
, b
- 1));
217 col
= MAX(0, MIN(c
, pad_cols() - 1));
221 static void advance(int dr
, int dc
, int scrl
)
225 if (c
>= pad_cols()) {
226 if (!scrl
|| (mode
& MODE_NOWRAP
)) {
233 if (r
>= bot
&& scrl
) {
235 int nr
= (bot
- top
) + n
;
236 scroll_screen(top
+ -n
, nr
, n
);
238 if (r
< top
&& scrl
) {
240 int nr
= (bot
- top
) - n
;
241 scroll_screen(top
, nr
, n
);
243 r
= MIN(bot
- 1, MAX(top
, r
));
244 move_cursor(r
, MAX(0, c
));
247 void term_send(int c
)
249 unsigned char b
= (unsigned char) c
;
251 write(term
->fd
, &b
, 1);
254 void term_sendstr(char *s
)
257 write(term
->fd
, s
, strlen(s
));
260 static void setmode(int m
)
273 if (m
>= 30 && m
<= 37)
275 if (m
>= 40 && m
<= 47)
279 static void kill_chars(int sc
, int ec
)
282 for (i
= sc
; i
< ec
; i
++)
283 lazy_put(' ', row
, i
);
287 static void move_chars(int sc
, int nc
, int n
)
290 memmove(SQRADDR(row
, sc
+ n
), SQRADDR(row
, sc
),
291 nc
* sizeof(*screen
));
293 memset(SQRADDR(row
, sc
), 0, n
* sizeof(*screen
));
295 memset(SQRADDR(row
, pad_rows() + n
), 0, -n
* sizeof(*screen
));
296 lazy_drawcols(row
, MIN(sc
, sc
+ n
), pad_cols());
300 static void delete_chars(int n
)
302 move_chars(col
+ n
, pad_cols(), -n
);
305 static void insert_chars(int n
)
307 int nc
= pad_cols() - col
- n
;
308 move_chars(col
, nc
, n
);
311 static void term_blank(void)
313 memset(screen
, 0, MAXCHARS
* sizeof(*screen
));
320 static void ctlseq(void);
325 while (ptycur
< ptylen
)
331 void term_exec(char *cmd
)
334 if ((term
->pid
= forkpty(&term
->fd
, NULL
, NULL
, NULL
)) == -1) {
335 perror("failed to fork");
340 setenv("TERM", "linux", 1);
341 execlp(cmd
, cmd
, NULL
);
344 fcntl(term
->fd
, F_SETFD
, fcntl(term
->fd
, F_GETFD
) | FD_CLOEXEC
);
345 fcntl(term
->fd
, F_SETFL
, fcntl(term
->fd
, F_GETFL
) | O_NONBLOCK
);
351 static void misc_save(struct term_state
*state
)
362 static void misc_load(struct term_state
*state
)
373 void term_save(struct term
*term
)
376 misc_save(&term
->cur
);
379 void term_load(struct term
*t
, int flags
)
382 misc_load(&term
->cur
);
383 screen
= term
->screen
;
385 if (term
->fd
&& flags
== TERM_REDRAW
) {
386 lazy_draw(0, pad_rows());
389 if (!term
->fd
&& flags
)
402 void set_region(int t
, int b
)
404 top
= MIN(pad_rows(), MAX(0, t
- 1));
405 bot
= MIN(pad_rows(), MAX(0, b
? b
: pad_rows()));