xref: /dragonfly/sys/dev/misc/syscons/sctermvar.h (revision ef3ac1d1)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/syscons/sctermvar.h,v 1.1.2.2 2001/07/28 12:51:47 yokota Exp $
27  * $DragonFly: src/sys/dev/misc/syscons/sctermvar.h,v 1.4 2005/05/27 20:57:40 swildner Exp $
28  */
29 
30 #ifndef _DEV_SYSCONS_SCTERMVAR_H_
31 #define _DEV_SYSCONS_SCTERMVAR_H_
32 
33 /*
34  * building blocks for terminal emulator modules.
35  */
36 
37 static __inline void	sc_term_ins_line(scr_stat *scp, int y, int n, int ch,
38 					 int attr, int tail);
39 static __inline void	sc_term_del_line(scr_stat *scp, int y, int n, int ch,
40 					 int attr, int tail);
41 static __inline void	sc_term_ins_char(scr_stat *scp, int n, int ch,
42 					 int attr);
43 static __inline void	sc_term_del_char(scr_stat *scp, int n, int ch,
44 					 int attr);
45 static __inline void	sc_term_col(scr_stat *scp, int n);
46 static __inline void	sc_term_row(scr_stat *scp, int n);
47 static __inline void	sc_term_up(scr_stat *scp, int n, int head);
48 static __inline void	sc_term_down(scr_stat *scp, int n, int tail);
49 static __inline void	sc_term_left(scr_stat *scp, int n);
50 static __inline void	sc_term_right(scr_stat *scp, int n);
51 static __inline void	sc_term_up_scroll(scr_stat *scp, int n, int ch,
52 					  int attr, int head, int tail);
53 static __inline void	sc_term_down_scroll(scr_stat *scp, int n, int ch,
54 					    int attr, int head, int tail);
55 static __inline void	sc_term_clr_eos(scr_stat *scp, int n, int ch, int attr);
56 static __inline void	sc_term_clr_eol(scr_stat *scp, int n, int ch, int attr);
57 static __inline void	sc_term_tab(scr_stat *scp, int n);
58 static __inline void	sc_term_backtab(scr_stat *scp, int n);
59 static __inline void	sc_term_respond(scr_stat *scp, u_char *s);
60 static __inline void	sc_term_gen_print(scr_stat *scp, u_char **buf, int *len,
61 					  int attr);
62 static __inline void	sc_term_gen_scroll(scr_stat *scp, int ch, int attr);
63 
64 static __inline void
65 sc_term_ins_line(scr_stat *scp, int y, int n, int ch, int attr, int tail)
66 {
67 	if (tail <= 0)
68 		tail = scp->ysize;
69 	if (n < 1)
70 		n = 1;
71 	if (n > tail - y)
72 		n = tail - y;
73 	sc_vtb_ins(&scp->vtb, y*scp->xsize, n*scp->xsize, ch, attr);
74 	mark_for_update(scp, y*scp->xsize);
75 	mark_for_update(scp, scp->xsize*tail - 1);
76 }
77 
78 static __inline void
79 sc_term_del_line(scr_stat *scp, int y, int n, int ch, int attr, int tail)
80 {
81 	if (tail <= 0)
82 		tail = scp->ysize;
83 	if (n < 1)
84 		n = 1;
85 	if (n > tail - y)
86 		n = tail - y;
87 	sc_vtb_delete(&scp->vtb, y*scp->xsize, n*scp->xsize, ch, attr);
88 	mark_for_update(scp, y*scp->xsize);
89 	mark_for_update(scp, scp->xsize*tail - 1);
90 }
91 
92 static __inline void
93 sc_term_ins_char(scr_stat *scp, int n, int ch, int attr)
94 {
95 	int count;
96 
97 	if (n < 1)
98 		n = 1;
99 	if (n > scp->xsize - scp->xpos)
100 		n = scp->xsize - scp->xpos;
101 	count = scp->xsize - (scp->xpos + n);
102 	sc_vtb_move(&scp->vtb, scp->cursor_pos, scp->cursor_pos + n, count);
103 	sc_vtb_erase(&scp->vtb, scp->cursor_pos, n, ch, attr);
104 	mark_for_update(scp, scp->cursor_pos);
105 	mark_for_update(scp, scp->cursor_pos + n + count - 1);
106 }
107 
108 static __inline void
109 sc_term_del_char(scr_stat *scp, int n, int ch, int attr)
110 {
111 	int count;
112 
113 	if (n < 1)
114 		n = 1;
115 	if (n > scp->xsize - scp->xpos)
116 		n = scp->xsize - scp->xpos;
117 	count = scp->xsize - (scp->xpos + n);
118 	sc_vtb_move(&scp->vtb, scp->cursor_pos + n, scp->cursor_pos, count);
119 	sc_vtb_erase(&scp->vtb, scp->cursor_pos + count, n, ch, attr);
120 	mark_for_update(scp, scp->cursor_pos);
121 	mark_for_update(scp, scp->cursor_pos + n + count - 1);
122 }
123 
124 static __inline void
125 sc_term_col(scr_stat *scp, int n)
126 {
127 	if (n < 1)
128 		n = 1;
129 	sc_move_cursor(scp, n - 1, scp->ypos);
130 }
131 
132 static __inline void
133 sc_term_row(scr_stat *scp, int n)
134 {
135 	if (n < 1)
136 		n = 1;
137 	sc_move_cursor(scp, scp->xpos, n - 1);
138 }
139 
140 static __inline void
141 sc_term_up(scr_stat *scp, int n, int head)
142 {
143 	if (n < 1)
144 		n = 1;
145 	n = imin(n, scp->ypos - head);
146 	if (n <= 0)
147 		return;
148 	sc_move_cursor(scp, scp->xpos, scp->ypos - n);
149 }
150 
151 static __inline void
152 sc_term_down(scr_stat *scp, int n, int tail)
153 {
154 	if (tail <= 0)
155 		tail = scp->ysize;
156 	if (n < 1)
157 		n = 1;
158 	n = imin(n, tail - scp->ypos - 1);
159 	if (n <= 0)
160 		return;
161 	sc_move_cursor(scp, scp->xpos, scp->ypos + n);
162 }
163 
164 static __inline void
165 sc_term_left(scr_stat *scp, int n)
166 {
167 	if (n < 1)
168 		n = 1;
169 	sc_move_cursor(scp, scp->xpos - n, scp->ypos);
170 }
171 
172 static __inline void
173 sc_term_right(scr_stat *scp, int n)
174 {
175 	if (n < 1)
176 		n = 1;
177 	sc_move_cursor(scp, scp->xpos + n, scp->ypos);
178 }
179 
180 static __inline void
181 sc_term_up_scroll(scr_stat *scp, int n, int ch, int attr, int head, int tail)
182 {
183 	if (tail <= 0)
184 		tail = scp->ysize;
185 	if (n < 1)
186 		n = 1;
187 	if (n <= scp->ypos - head) {
188 		sc_move_cursor(scp, scp->xpos, scp->ypos - n);
189 	} else {
190 		sc_term_ins_line(scp, head, n - (scp->ypos - head),
191 				 ch, attr, tail);
192 		sc_move_cursor(scp, scp->xpos, head);
193 	}
194 }
195 
196 static __inline void
197 sc_term_down_scroll(scr_stat *scp, int n, int ch, int attr, int head, int tail)
198 {
199 	if (tail <= 0)
200 		tail = scp->ysize;
201 	if (n < 1)
202 		n = 1;
203 	if (n < tail - scp->ypos) {
204 		sc_move_cursor(scp, scp->xpos, scp->ypos + n);
205 	} else {
206 		sc_term_del_line(scp, head, n - (tail - scp->ypos) + 1,
207 				 ch, attr, tail);
208 		sc_move_cursor(scp, scp->xpos, tail - 1);
209 	}
210 }
211 
212 static __inline void
213 sc_term_clr_eos(scr_stat *scp, int n, int ch, int attr)
214 {
215 	switch (n) {
216 	case 0: /* clear form cursor to end of display */
217 		sc_vtb_erase(&scp->vtb, scp->cursor_pos,
218 			     scp->xsize*scp->ysize - scp->cursor_pos,
219 			     ch, attr);
220 		mark_for_update(scp, scp->cursor_pos);
221 		mark_for_update(scp, scp->xsize*scp->ysize - 1);
222 		sc_remove_cutmarking(scp);
223 		break;
224 	case 1: /* clear from beginning of display to cursor */
225 		sc_vtb_erase(&scp->vtb, 0, scp->cursor_pos + 1, ch, attr);
226 		mark_for_update(scp, 0);
227 		mark_for_update(scp, scp->cursor_pos);
228 		sc_remove_cutmarking(scp);
229 		break;
230 	case 2: /* clear entire display */
231 		sc_vtb_erase(&scp->vtb, 0, scp->xsize*scp->ysize, ch, attr);
232 		mark_for_update(scp, 0);
233 		mark_for_update(scp, scp->xsize*scp->ysize - 1);
234 		sc_remove_cutmarking(scp);
235 		break;
236 	}
237 }
238 
239 static __inline void
240 sc_term_clr_eol(scr_stat *scp, int n, int ch, int attr)
241 {
242 	switch (n) {
243 	case 0: /* clear form cursor to end of line */
244 		sc_vtb_erase(&scp->vtb, scp->cursor_pos,
245 			     scp->xsize - scp->xpos, ch, attr);
246 		mark_for_update(scp, scp->cursor_pos);
247 		mark_for_update(scp, scp->cursor_pos +
248 				scp->xsize - 1 - scp->xpos);
249 		break;
250 	case 1: /* clear from beginning of line to cursor */
251 		sc_vtb_erase(&scp->vtb, scp->cursor_pos - scp->xpos,
252 			     scp->xpos + 1, ch, attr);
253 		mark_for_update(scp, scp->ypos*scp->xsize);
254 		mark_for_update(scp, scp->cursor_pos);
255 		break;
256 	case 2: /* clear entire line */
257 		sc_vtb_erase(&scp->vtb, scp->cursor_pos - scp->xpos,
258 			     scp->xsize, ch, attr);
259 		mark_for_update(scp, scp->ypos*scp->xsize);
260 		mark_for_update(scp, (scp->ypos + 1)*scp->xsize - 1);
261 		break;
262 	}
263 }
264 
265 static __inline void
266 sc_term_tab(scr_stat *scp, int n)
267 {
268 	int i;
269 
270 	if (n < 1)
271 		n = 1;
272 	i = (scp->xpos & ~7) + 8*n;
273 	if (i >= scp->xsize) {
274 		if (scp->ypos >= scp->ysize - 1) {
275 			scp->xpos = 0;
276 			scp->ypos++;
277 			scp->cursor_pos = scp->ypos*scp->xsize;
278 		} else
279 			sc_move_cursor(scp, 0, scp->ypos + 1);
280 	} else
281 		sc_move_cursor(scp, i, scp->ypos);
282 }
283 
284 static __inline void
285 sc_term_backtab(scr_stat *scp, int n)
286 {
287 	int i;
288 
289 	if (n < 1)
290 		n = 1;
291 	if ((i = scp->xpos & ~7) == scp->xpos)
292 		i -= 8*n;
293 	else
294 		i -= 8*(n - 1);
295 	if (i < 0)
296 		i = 0;
297 	sc_move_cursor(scp, i, scp->ypos);
298 }
299 
300 static __inline void
301 sc_term_respond(scr_stat *scp, u_char *s)
302 {
303 	sc_paste(scp, s, strlen(s));	/* XXX: not correct, don't use rmap */
304 }
305 
306 static __inline void
307 sc_term_gen_print(scr_stat *scp, u_char **buf, int *len, int attr)
308 {
309 	uint16_t *p;
310 	u_char *ptr;
311 	u_char *map;
312  	int cnt;
313 	int l;
314 	int i;
315 
316 	ptr = *buf;
317 	l = *len;
318 
319 	if (PRINTABLE(*ptr)) {
320 		p = scp->vtb.vtb_buffer + scp->cursor_pos;
321 		map = scp->sc->scr_map;
322 
323 		cnt = imin(l, scp->xsize - scp->xpos);
324 		i = cnt;
325 		do {
326 			p = sc_vtb_putchar(&scp->vtb, p, map[*ptr], attr);
327 			++ptr;
328 			--i;
329 		} while ((i > 0) && PRINTABLE(*ptr));
330 
331 		l -= cnt - i;
332 		mark_for_update(scp, scp->cursor_pos);
333 		scp->cursor_pos += cnt - i;
334 		mark_for_update(scp, scp->cursor_pos - 1);
335 		scp->xpos += cnt - i;
336 
337 		if (scp->xpos >= scp->xsize) {
338 			scp->xpos = 0;
339 			scp->ypos++;
340 			/* we may have to scroll the screen */
341 		}
342 	} else {
343 		switch(*ptr) {
344 		case 0x07:
345 			sc_bell(scp, scp->bell_pitch, scp->bell_duration);
346 			break;
347 
348 		case 0x08:	/* non-destructive backspace */
349 			/* XXX */
350 			if (scp->cursor_pos > 0) {
351 #if 0
352 				mark_for_update(scp, scp->cursor_pos);
353 				scp->cursor_pos--;
354 				mark_for_update(scp, scp->cursor_pos);
355 #else
356 				scp->cursor_pos--;
357 #endif
358 				if (scp->xpos > 0) {
359 					scp->xpos--;
360 				} else {
361 					scp->xpos += scp->xsize - 1;
362 					scp->ypos--;
363 				}
364 			}
365 			break;
366 
367 		case 0x09:	/* non-destructive tab */
368 			sc_term_tab(scp, 1);
369 			/* we may have to scroll the screen */
370 #if 0
371 			mark_for_update(scp, scp->cursor_pos);
372 			scp->cursor_pos += (8 - scp->xpos % 8u);
373 			mark_for_update(scp, scp->cursor_pos);
374 			scp->xpos += (8 - scp->xpos % 8u);
375 			if (scp->xpos >= scp->xsize) {
376 				scp->xpos = 0;
377 				scp->ypos++;
378 			}
379 #endif
380 			break;
381 
382 		case 0x0a:	/* newline, same pos */
383 #if 0
384 			mark_for_update(scp, scp->cursor_pos);
385 			scp->cursor_pos += scp->xsize;
386 			mark_for_update(scp, scp->cursor_pos);
387 #else
388 			scp->cursor_pos += scp->xsize;
389 			/* we may have to scroll the screen */
390 #endif
391 			scp->ypos++;
392 			break;
393 
394 		case 0x0c:	/* form feed, clears screen */
395 			sc_clear_screen(scp);
396 			break;
397 
398 		case 0x0d:	/* return, return to pos 0 */
399 #if 0
400 			mark_for_update(scp, scp->cursor_pos);
401 			scp->cursor_pos -= scp->xpos;
402 			mark_for_update(scp, scp->cursor_pos);
403 #else
404 			scp->cursor_pos -= scp->xpos;
405 #endif
406 			scp->xpos = 0;
407 			break;
408 		}
409 		ptr++; l--;
410 	}
411 
412 	*buf = ptr;
413 	*len = l;
414 }
415 
416 static __inline void
417 sc_term_gen_scroll(scr_stat *scp, int ch, int attr)
418 {
419 	/* do we have to scroll ?? */
420 	if (scp->cursor_pos >= scp->ysize*scp->xsize) {
421 		sc_remove_cutmarking(scp);		/* XXX */
422 #ifndef SC_NO_HISTORY
423 		if (scp->history != NULL)
424 			sc_hist_save_one_line(scp, 0);	/* XXX */
425 #endif
426 		sc_vtb_delete(&scp->vtb, 0, scp->xsize, ch, attr);
427 		scp->cursor_pos -= scp->xsize;
428 		scp->ypos--;
429 		mark_all(scp);
430 	}
431 }
432 
433 #endif /* _DEV_SYSCONS_SCTERMVAR_H_ */
434