14 #define SQRADDR(r, c) (&screen[(r) * pad_cols() + (c)])
20 static struct square screen
[MAXCHARS
];
24 static void setsize(void)
27 size
.ws_col
= pad_cols();
28 size
.ws_row
= pad_rows();
31 ioctl(fd
, TIOCSWINSZ
, &size
);
34 static int readpty(void)
37 if (read(fd
, &b
, 1) > 0)
42 static void term_show(int r
, int c
, int cursor
)
44 struct square
*sqr
= SQRADDR(r
, c
);
45 int fgcolor
= sqr
->c
? sqr
->fg
: fg
;
46 int bgcolor
= sqr
->c
? sqr
->bg
: bg
;
47 if (cursor
&& !nocursor
) {
52 pad_put(sqr
->c
, r
, c
, fgcolor
, bgcolor
);
55 void term_put(int ch
, int r
, int c
)
57 struct square
*sqr
= SQRADDR(r
, c
);
64 static void empty_rows(int sr
, int er
)
66 memset(SQRADDR(sr
, 0), 0, (er
- sr
) * sizeof(screen
[0]) * pad_cols());
69 static void blank_rows(int sr
, int er
)
73 for (i
= sr
* pad_cols(); i
< er
* pad_cols(); i
++)
74 term_show(i
/ pad_cols(), i
% pad_cols(), 0);
75 term_show(row
, col
, 1);
78 static void scroll_screen(int sr
, int nr
, int n
)
80 term_show(row
, col
, 0);
81 memmove(SQRADDR(sr
+ n
, 0), SQRADDR(sr
, 0),
82 nr
* pad_cols() * sizeof(screen
[0]));
84 empty_rows(sr
, sr
+ n
);
86 empty_rows(sr
+ nr
+ n
, sr
+ nr
);
87 pad_scroll(sr
, nr
, n
, bg
);
88 term_show(row
, col
, 1);
91 static void insert_lines(int n
)
93 int sr
= MAX(top
, row
);
94 int nr
= bot
- row
- n
;
96 scroll_screen(sr
, nr
, n
);
99 static void delete_lines(int n
)
101 int r
= MAX(top
, row
);
103 int nr
= bot
- r
- n
;
105 scroll_screen(sr
, nr
, -n
);
108 static void move_cursor(int r
, int c
)
110 term_show(row
, col
, 0);
111 if (c
>= pad_cols()) {
117 int nr
= (bot
- top
) + n
;
118 scroll_screen(-n
, nr
, n
);
121 row
= MAX(0, MIN(r
, pad_rows() - 1));
122 col
= MAX(0, MIN(c
, pad_cols() - 1));
123 term_show(row
, col
, 1);
126 static void advance(int dr
, int dc
)
130 move_cursor(MAX(0, r
), MAX(0, c
));
133 void term_send(int c
)
135 unsigned char b
= (unsigned char) c
;
140 static void setmode(int m
)
153 if (m
>= 30 && m
<= 37)
155 if (m
>= 40 && m
<= 47)
159 static void kill_chars(int sc
, int ec
)
162 memmove(SQRADDR(row
, sc
), SQRADDR(row
, ec
),
163 (pad_cols() - ec
) * sizeof(screen
[0]));
164 memset(SQRADDR(row
, sc
+ pad_cols() - ec
), 0,
165 (ec
- sc
) * sizeof(screen
[0]));
166 for (i
= col
; i
< pad_cols(); i
++)
167 term_show(row
, i
, 0);
168 move_cursor(row
, col
);
171 void term_blank(void)
174 memset(screen
, 0, sizeof(screen
));
177 static void ctlseq(void);
183 void term_exec(char *cmd
)
185 if ((pid
= forkpty(&fd
, NULL
, NULL
, NULL
)) == -1)
186 xerror("failed to create a pty");
188 setenv("TERM", "linux", 1);
189 execl(cmd
, cmd
, NULL
);
197 void term_save(struct term_state
*state
)
205 memcpy(state
->screen
, screen
,
206 pad_rows() * pad_cols() * sizeof(screen
[0]));
209 void term_load(struct term_state
*state
)
218 memcpy(screen
, state
->screen
,
219 pad_rows() * pad_cols() * sizeof(screen
[0]));
220 for (i
= 0; i
< pad_rows() * pad_cols(); i
++)
221 term_show(i
/ pad_cols(), i
% pad_cols(), 0);
222 term_show(row
, col
, 1);