rcons_subr.c revision 1.15.2.1 1 /* $NetBSD: rcons_subr.c,v 1.15.2.1 2006/02/01 14:52:20 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)rcons_subr.c 8.1 (Berkeley) 6/11/93
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: rcons_subr.c,v 1.15.2.1 2006/02/01 14:52:20 yamt Exp $");
45
46 #include <sys/param.h>
47 #ifdef _KERNEL
48 #include <sys/device.h>
49 #include <sys/systm.h>
50 #else
51 #include "myfbdevice.h"
52 #endif
53
54 #include <dev/rcons/rcons.h>
55 #include <dev/wscons/wsdisplayvar.h>
56
57 extern void rcons_bell(struct rconsole *);
58
59 #if 0
60 #define RCONS_ISPRINT(c) ((((c) >= ' ') && ((c) <= '~')) || ((c) > 160))
61 #else
62 #define RCONS_ISPRINT(c) (((((c) >= ' ') && ((c) <= '~'))) || ((c) > 127))
63 #endif
64 #define RCONS_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
65
66 /* Initialize our operations set */
67 void
68 rcons_init_ops(rc)
69 struct rconsole *rc;
70 {
71 long tmp;
72 int i, m;
73
74 m = sizeof(rc->rc_charmap) / sizeof(rc->rc_charmap[0]);
75
76 for (i = 0; i < m; i++)
77 rc->rc_ops->mapchar(rc->rc_cookie, i, rc->rc_charmap + i);
78
79 /* Determine which attributes the device supports. */
80 #ifdef RASTERCONSOLE_FGCOL
81 rc->rc_deffgcolor = RASTERCONSOLE_FGCOL;
82 #endif
83 #ifdef RASTERCONSOLE_BGCOL
84 rc->rc_defbgcolor = RASTERCONSOLE_BGCOL;
85 #endif
86 rc->rc_fgcolor = rc->rc_deffgcolor;
87 rc->rc_bgcolor = rc->rc_defbgcolor;
88 rc->rc_supwsflg = 0;
89
90 for (i = 1; i < 256; i <<= 1)
91 if (rc->rc_ops->allocattr(rc->rc_cookie, 0, 0, i, &tmp) == 0)
92 rc->rc_supwsflg |= i;
93
94 /* Allocate kernel output attribute */
95 rc->rc_wsflg = WSATTR_HILIT;
96 rcons_setcolor(rc, rc->rc_deffgcolor, rc->rc_defbgcolor);
97 rc->rc_kern_attr = rc->rc_attr;
98
99 rc->rc_wsflg = 0;
100 rcons_setcolor(rc, rc->rc_deffgcolor, rc->rc_defbgcolor);
101 rc->rc_defattr = rc->rc_attr;
102 }
103
104 /* Output (or at least handle) a string sent to the console */
105 void
106 rcons_puts(rc, str, n)
107 struct rconsole *rc;
108 const unsigned char *str;
109 int n;
110 {
111 int c, i, j;
112 const unsigned char *cp;
113
114 /* Jump scroll */
115 /* XXX maybe this should be an option? */
116 if ((rc->rc_bits & FB_INESC) == 0) {
117 /* Count newlines up to an escape sequence */
118 i = 0;
119 j = 0;
120 for (cp = str; j++ < n && *cp != '\033'; ++cp) {
121 if (*cp == '\n')
122 ++i;
123 else if (*cp == '\013')
124 --i;
125 }
126
127 /* Only jump scroll two or more rows */
128 if (rc->rc_row + i > rc->rc_maxrow + 1) {
129 /* Erase the cursor (if necessary) */
130 if (rc->rc_bits & FB_CURSOR)
131 rcons_cursor(rc);
132
133 rcons_scroll(rc, i);
134 }
135 }
136
137 /* Process characters */
138 while (--n >= 0) {
139 c = *str;
140 if (c == '\033') {
141 /* Start an escape (perhaps aborting one in progress) */
142 rc->rc_bits |= FB_INESC | FB_P0_DEFAULT | FB_P1_DEFAULT;
143 rc->rc_bits &= ~(FB_P0 | FB_P1);
144
145 /* Most parameters default to 1 */
146 rc->rc_p0 = rc->rc_p1 = 1;
147 } else if (rc->rc_bits & FB_INESC) {
148 rcons_esc(rc, c);
149 } else {
150 /* Erase the cursor (if necessary) */
151 if (rc->rc_bits & FB_CURSOR)
152 rcons_cursor(rc);
153
154 /* Display the character */
155 if (RCONS_ISPRINT(c)) {
156 /* Try to output as much as possible */
157 j = rc->rc_maxcol - rc->rc_col;
158 if (j > n)
159 j = n;
160 for (i = 1; i < j && RCONS_ISPRINT(str[i]); ++i)
161 continue;
162 rcons_text(rc, str, i);
163 --i;
164 str += i;
165 n -= i;
166 } else
167 rcons_pctrl(rc, c);
168 }
169 ++str;
170 }
171 /* Redraw the cursor (if necessary) */
172 if ((rc->rc_bits & FB_CURSOR) == 0)
173 rcons_cursor(rc);
174 }
175
176
177 /* Handle a control character sent to the console */
178 void
179 rcons_pctrl(rc, c)
180 struct rconsole *rc;
181 int c;
182 {
183
184 switch (c) {
185 case '\r': /* Carriage return */
186 rc->rc_col = 0;
187 break;
188
189 case '\b': /* Backspace */
190 if (rc->rc_col > 0)
191 (rc->rc_col)--;
192 break;
193
194 case '\v': /* Vertical tab */
195 if (rc->rc_row > 0)
196 (rc->rc_row)--;
197 break;
198
199 case '\f': /* Formfeed */
200 rc->rc_row = rc->rc_col = 0;
201 rcons_clear2eop(rc);
202 break;
203
204 case '\n': /* Linefeed */
205 (rc->rc_row)++;
206 if (rc->rc_row >= rc->rc_maxrow)
207 rcons_scroll(rc, 1);
208 break;
209
210 case '\a': /* Bell */
211 rcons_bell(rc);
212 break;
213
214 case '\t': /* Horizontal tab */
215 rc->rc_col = (rc->rc_col + 8) & ~7;
216 if (rc->rc_col >= rc->rc_maxcol)
217 rc->rc_col = rc->rc_maxcol;
218 break;
219 }
220 }
221
222 /* Handle the next character in an escape sequence */
223 void
224 rcons_esc(rc, c)
225 struct rconsole *rc;
226 int c;
227 {
228
229 if (c == '[') {
230 /* Parameter 0 */
231 rc->rc_bits &= ~FB_P1;
232 rc->rc_bits |= FB_P0;
233 } else if (c == ';') {
234 /* Parameter 1 */
235 rc->rc_bits &= ~FB_P0;
236 rc->rc_bits |= FB_P1;
237 } else if (RCONS_ISDIGIT(c)) {
238 /* Add a digit to a parameter */
239 if (rc->rc_bits & FB_P0) {
240 /* Parameter 0 */
241 if (rc->rc_bits & FB_P0_DEFAULT) {
242 rc->rc_bits &= ~FB_P0_DEFAULT;
243 rc->rc_p0 = 0;
244 }
245 rc->rc_p0 *= 10;
246 rc->rc_p0 += c - '0';
247 } else if (rc->rc_bits & FB_P1) {
248 /* Parameter 1 */
249 if (rc->rc_bits & FB_P1_DEFAULT) {
250 rc->rc_bits &= ~FB_P1_DEFAULT;
251 rc->rc_p1 = 0;
252 }
253 rc->rc_p1 *= 10;
254 rc->rc_p1 += c - '0';
255 }
256 } else {
257 /* Erase the cursor (if necessary) */
258 if (rc->rc_bits & FB_CURSOR)
259 rcons_cursor(rc);
260
261 /* Process the completed escape sequence */
262 rcons_doesc(rc, c);
263 rc->rc_bits &= ~FB_INESC;
264 }
265 }
266
267
268 /* Handle an SGR (Select Graphic Rendition) escape */
269 void
270 rcons_sgresc(rc, c)
271 struct rconsole *rc;
272 int c;
273 {
274
275 switch (c) {
276 /* Clear all attributes || End underline */
277 case 0:
278 rc->rc_wsflg = 0;
279 rc->rc_fgcolor = rc->rc_deffgcolor;
280 rc->rc_bgcolor = rc->rc_defbgcolor;
281 rc->rc_attr = rc->rc_defattr;
282 break;
283
284 /* ANSI foreground color */
285 case 30: case 31: case 32: case 33:
286 case 34: case 35: case 36: case 37:
287 rcons_setcolor(rc, c - 30, rc->rc_bgcolor);
288 break;
289
290 /* ANSI background color */
291 case 40: case 41: case 42: case 43:
292 case 44: case 45: case 46: case 47:
293 rcons_setcolor(rc, rc->rc_fgcolor, c - 40);
294 break;
295
296 /* Begin reverse */
297 case 7:
298 rc->rc_wsflg |= WSATTR_REVERSE;
299 rcons_setcolor(rc, rc->rc_fgcolor, rc->rc_bgcolor);
300 break;
301
302 /* Begin bold */
303 case 1:
304 rc->rc_wsflg |= WSATTR_HILIT;
305 rcons_setcolor(rc, rc->rc_fgcolor, rc->rc_bgcolor);
306 break;
307
308 /* Begin underline */
309 case 4:
310 rc->rc_wsflg |= WSATTR_UNDERLINE;
311 rcons_setcolor(rc, rc->rc_fgcolor, rc->rc_bgcolor);
312 break;
313 }
314 }
315
316
317 /* Process a complete escape sequence */
318 void
319 rcons_doesc(rc, c)
320 struct rconsole *rc;
321 int c;
322 {
323
324 #ifdef notdef
325 /* XXX add escape sequence to enable visual (and audible) bell */
326 rc->rc_bits = FB_VISBELL;
327 #endif
328
329 switch (c) {
330
331 case '@':
332 /* Insert Character (ICH) */
333 rcons_insertchar(rc, rc->rc_p0);
334 break;
335
336 case 'A':
337 /* Cursor Up (CUU) */
338 if (rc->rc_row >= rc->rc_p0)
339 rc->rc_row -= rc->rc_p0;
340 else
341 rc->rc_row = 0;
342 break;
343
344 case 'B':
345 /* Cursor Down (CUD) */
346 rc->rc_row += rc->rc_p0;
347 if (rc->rc_row >= rc->rc_maxrow)
348 rc->rc_row = rc->rc_maxrow - 1;
349 break;
350
351 case 'C':
352 /* Cursor Forward (CUF) */
353 rc->rc_col += rc->rc_p0;
354 if (rc->rc_col >= rc->rc_maxcol)
355 rc->rc_col = rc->rc_maxcol - 1;
356 break;
357
358 case 'D':
359 /* Cursor Backward (CUB) */
360 if (rc->rc_col >= rc->rc_p0)
361 rc->rc_col -= rc->rc_p0;
362 else
363 rc->rc_col = 0;
364 break;
365
366 case 'E':
367 /* Cursor Next Line (CNL) */
368 rc->rc_col = 0;
369 rc->rc_row += rc->rc_p0;
370 if (rc->rc_row >= rc->rc_maxrow)
371 rc->rc_row = rc->rc_maxrow - 1;
372 break;
373
374 case 'f':
375 /* Horizontal And Vertical Position (HVP) */
376 case 'H':
377 /* Cursor Position (CUP) */
378 rc->rc_col = MIN(MAX(rc->rc_p1, 1), rc->rc_maxcol) - 1;
379 rc->rc_row = MIN(MAX(rc->rc_p0, 1), rc->rc_maxrow) - 1;
380 break;
381
382 case 'J':
383 /* Erase in Display (ED) */
384 rcons_clear2eop(rc);
385 break;
386
387 case 'K':
388 /* Erase in Line (EL) */
389 rcons_clear2eol(rc);
390 break;
391
392 case 'L':
393 /* Insert Line (IL) */
394 rcons_insertline(rc, rc->rc_p0);
395 break;
396
397 case 'M':
398 /* Delete Line (DL) */
399 rcons_delline(rc, rc->rc_p0);
400 break;
401
402 case 'P':
403 /* Delete Character (DCH) */
404 rcons_delchar(rc, rc->rc_p0);
405 break;
406
407 case 'm':
408 /* Select Graphic Rendition (SGR) */
409 /* (defaults to zero) */
410 if (rc->rc_bits & FB_P0_DEFAULT)
411 rc->rc_p0 = 0;
412
413 if (rc->rc_bits & FB_P1_DEFAULT)
414 rc->rc_p1 = 0;
415
416 rcons_sgresc(rc, rc->rc_p0);
417
418 if (rc->rc_bits & FB_P1)
419 rcons_sgresc(rc, rc->rc_p1);
420
421 break;
422
423 /*
424 * XXX: setting SUNBOW and SUNWOB should probably affect
425 * deffgcolor, defbgcolor and defattr too.
426 */
427 case 'p':
428 /* Black On White (SUNBOW) */
429 rcons_setcolor(rc, WSCOL_BLACK, WSCOL_WHITE);
430 break;
431
432 case 'q':
433 /* White On Black (SUNWOB) */
434 rcons_setcolor(rc, WSCOL_WHITE, WSCOL_BLACK);
435 break;
436
437 case 'r':
438 /* Set scrolling (SUNSCRL) */
439 /* (defaults to zero) */
440 if (rc->rc_bits & FB_P0_DEFAULT)
441 rc->rc_p0 = 0;
442 /* XXX not implemented yet */
443 rc->rc_scroll = rc->rc_p0;
444 break;
445
446 case 's':
447 /* Reset terminal emulator (SUNRESET) */
448 rc->rc_wsflg = 0;
449 rc->rc_scroll = 0;
450 rc->rc_bits &= ~FB_NO_CURSOR;
451 rc->rc_fgcolor = rc->rc_deffgcolor;
452 rc->rc_bgcolor = rc->rc_defbgcolor;
453 rc->rc_attr = rc->rc_defattr;
454
455 if (rc->rc_bits & FB_INVERT)
456 rcons_invert(rc, 0);
457 break;
458 #ifdef notyet
459 /*
460 * XXX following two read \E[?25h and \E[?25l. rcons
461 * can't currently handle the '?'.
462 */
463 case 'h':
464 /* Normal/very visible cursor */
465 if (rc->rc_p0 == 25) {
466 rc->rc_bits &= ~FB_NO_CURSOR;
467
468 if (rc->rc_bits & FB_CURSOR) {
469 rc->rc_bits ^= FB_CURSOR;
470 rcons_cursor(rc);
471 }
472 }
473 break;
474
475 case 'l':
476 /* Invisible cursor */
477 if (rc->rc_p0 == 25 && (rc->rc_bits & FB_NO_CURSOR) == 0) {
478 if (rc->rc_bits & FB_CURSOR)
479 rcons_cursor(rc);
480
481 rc->rc_bits |= FB_NO_CURSOR;
482 }
483 break;
484 #endif
485 }
486 }
487
488 /* Set ANSI colors */
489 void
490 rcons_setcolor(rc, fg, bg)
491 struct rconsole *rc;
492 int fg, bg;
493 {
494 int flg;
495
496 if (fg > WSCOL_WHITE || fg < 0)
497 return;
498
499 if (bg > WSCOL_WHITE || bg < 0)
500 return;
501
502 #ifdef RASTERCONS_WONB
503 flg = bg;
504 bg = fg;
505 fg = flg;
506 #endif
507
508 /* Emulate WSATTR_REVERSE attribute if it's not supported */
509 if ((rc->rc_wsflg & WSATTR_REVERSE) &&
510 !(rc->rc_supwsflg & WSATTR_REVERSE)) {
511 flg = bg;
512 bg = fg;
513 fg = flg;
514 }
515
516 /*
517 * Mask out unsupported flags and get attribute
518 * XXX - always ask for WSCOLORS if supported (why shouldn't we?)
519 */
520 flg = (rc->rc_wsflg | WSATTR_WSCOLORS) & rc->rc_supwsflg;
521 rc->rc_bgcolor = bg;
522 rc->rc_fgcolor = fg;
523 rc->rc_ops->allocattr(rc->rc_cookie, fg, bg, flg, &rc->rc_attr);
524 }
525
526
527 /* Actually write a string to the frame buffer */
528 void
529 rcons_text(rc, str, n)
530 struct rconsole *rc;
531 const unsigned char *str;
532 int n;
533 {
534 u_int uc;
535
536 while (n--) {
537 uc = rc->rc_charmap[*str++ & 255];
538 rc->rc_ops->putchar(rc->rc_cookie, rc->rc_row, rc->rc_col++,
539 uc, rc->rc_attr);
540 }
541
542 if (rc->rc_col >= rc->rc_maxcol) {
543 rc->rc_col = 0;
544 rc->rc_row++;
545 }
546
547 if (rc->rc_row >= rc->rc_maxrow)
548 rcons_scroll(rc, 1);
549 }
550
551 /* Paint (or unpaint) the cursor */
552 void
553 rcons_cursor(rc)
554 struct rconsole *rc;
555 {
556 rc->rc_bits ^= FB_CURSOR;
557
558 if (rc->rc_bits & FB_NO_CURSOR)
559 return;
560
561 rc->rc_ops->cursor(rc->rc_cookie, rc->rc_bits & FB_CURSOR,
562 rc->rc_row, rc->rc_col);
563 }
564
565 /* Possibly change to SUNWOB or SUNBOW mode */
566 void
567 rcons_invert(rc, wob)
568 struct rconsole *rc;
569 int wob;
570 {
571
572 rc->rc_bits ^= FB_INVERT;
573 /* XXX how do we do we invert the framebuffer?? */
574 }
575
576 /* Clear to the end of the page */
577 void
578 rcons_clear2eop(rc)
579 struct rconsole *rc;
580 {
581 if (rc->rc_col || rc->rc_row) {
582 rcons_clear2eol(rc);
583
584 if (rc->rc_row < (rc->rc_maxrow - 1))
585 rc->rc_ops->eraserows(rc->rc_cookie, rc->rc_row + 1,
586 rc->rc_maxrow, rc->rc_attr);
587 } else
588 rc->rc_ops->eraserows(rc->rc_cookie, 0, rc->rc_maxrow,
589 rc->rc_attr);
590 }
591
592 /* Clear to the end of the line */
593 void
594 rcons_clear2eol(rc)
595 struct rconsole *rc;
596 {
597 rc->rc_ops->erasecols(rc->rc_cookie, rc->rc_row, rc->rc_col,
598 rc->rc_maxcol - rc->rc_col, rc->rc_attr);
599 }
600
601
602 /* Scroll up */
603 void
604 rcons_scroll(rc, n)
605 struct rconsole *rc;
606 int n;
607 {
608 /* Can't scroll more than the whole screen */
609 if (n > rc->rc_maxrow)
610 n = rc->rc_maxrow;
611
612 /* Calculate new row */
613 if (rc->rc_row >= n)
614 rc->rc_row -= n;
615 else
616 rc->rc_row = 0;
617
618 rc->rc_ops->copyrows(rc->rc_cookie, n, 0, rc->rc_maxrow - n);
619 rc->rc_ops->eraserows(rc->rc_cookie, rc->rc_maxrow - n, n, rc->rc_attr);
620 }
621
622 /* Delete characters */
623 void
624 rcons_delchar(rc, n)
625 struct rconsole *rc;
626 int n;
627 {
628 /* Can't delete more chars than there are */
629 if (n > rc->rc_maxcol - rc->rc_col)
630 n = rc->rc_maxcol - rc->rc_col;
631
632 rc->rc_ops->copycols(rc->rc_cookie, rc->rc_row, rc->rc_col + n,
633 rc->rc_col, rc->rc_maxcol - rc->rc_col - n);
634
635 rc->rc_ops->erasecols(rc->rc_cookie, rc->rc_row,
636 rc->rc_maxcol - n, n, rc->rc_attr);
637 }
638
639 /* Delete a number of lines */
640 void
641 rcons_delline(rc, n)
642 struct rconsole *rc;
643 int n;
644 {
645 /* Can't delete more lines than there are */
646 if (n > rc->rc_maxrow - rc->rc_row)
647 n = rc->rc_maxrow - rc->rc_row;
648
649 rc->rc_ops->copyrows(rc->rc_cookie, rc->rc_row + n, rc->rc_row,
650 rc->rc_maxrow - rc->rc_row - n);
651
652 rc->rc_ops->eraserows(rc->rc_cookie, rc->rc_maxrow - n, n,
653 rc->rc_attr);
654 }
655
656 /* Insert some characters */
657 void
658 rcons_insertchar(rc, n)
659 struct rconsole *rc;
660 int n;
661 {
662 /* Can't insert more chars than can fit */
663 if (n > rc->rc_maxcol - rc->rc_col)
664 n = rc->rc_maxcol - rc->rc_col - 1;
665
666 rc->rc_ops->copycols(rc->rc_cookie, rc->rc_row, rc->rc_col,
667 rc->rc_col + n, rc->rc_maxcol - rc->rc_col - n - 1);
668
669 rc->rc_ops->erasecols(rc->rc_cookie, rc->rc_row, rc->rc_col,
670 n, rc->rc_attr);
671 }
672
673 /* Insert some lines */
674 void
675 rcons_insertline(rc, n)
676 struct rconsole *rc;
677 int n;
678 {
679 /* Can't insert more lines than can fit */
680 if (n > rc->rc_maxrow - rc->rc_row)
681 n = rc->rc_maxrow - rc->rc_row;
682
683 rc->rc_ops->copyrows(rc->rc_cookie, rc->rc_row, rc->rc_row + n,
684 rc->rc_maxrow - rc->rc_row - n);
685
686 rc->rc_ops->eraserows(rc->rc_cookie, rc->rc_row, n,
687 rc->rc_attr);
688 }
689
690 /* end of rcons_subr.c */
691