wsemul_sun.c revision 1.10 1 /* $NetBSD: wsemul_sun.c,v 1.10 1999/01/17 15:44:57 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 static const char _copyright[] __attribute__ ((unused)) =
34 "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.";
35 static const char _rcsid[] __attribute__ ((unused)) =
36 "$NetBSD: wsemul_sun.c,v 1.10 1999/01/17 15:44:57 drochner Exp $";
37
38 /* XXX DESCRIPTION/SOURCE OF INFORMATION */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/time.h>
43 #include <sys/malloc.h>
44 #include <sys/fcntl.h>
45
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsdisplayvar.h>
48 #include <dev/wscons/wsemulvar.h>
49 #include <dev/wscons/wsksymdef.h>
50 #include <dev/wscons/ascii.h>
51
52 #include "opt_wskernattr.h"
53
54 void *wsemul_sun_cnattach __P((const struct wsscreen_descr *, void *,
55 int, int, long));
56 void *wsemul_sun_attach __P((int console, const struct wsscreen_descr *,
57 void *, int, int, void *, long));
58 void wsemul_sun_output __P((void *cookie, const u_char *data, u_int count,
59 int));
60 int wsemul_sun_translate __P((void *cookie, keysym_t, char **));
61 void wsemul_sun_detach __P((void *cookie, u_int *crowp, u_int *ccolp));
62 void wsemul_sun_resetop __P((void *, enum wsemul_resetops));
63
64 const struct wsemul_ops wsemul_sun_ops = {
65 "sun",
66 wsemul_sun_cnattach,
67 wsemul_sun_attach,
68 wsemul_sun_output,
69 wsemul_sun_translate,
70 wsemul_sun_detach,
71 wsemul_sun_resetop
72 };
73
74 #define SUN_EMUL_STATE_NORMAL 0 /* normal processing */
75 #define SUN_EMUL_STATE_HAVEESC 1 /* seen start of ctl seq */
76 #define SUN_EMUL_STATE_CONTROL 2 /* processing ctl seq */
77
78 #define SUN_EMUL_NARGS 2 /* max # of args to a command */
79
80 struct wsemul_sun_emuldata {
81 const struct wsdisplay_emulops *emulops;
82 void *emulcookie;
83 void *cbcookie;
84 int scrcapabilities;
85 u_int nrows, ncols, crow, ccol;
86
87 u_int state; /* processing state */
88 u_int args[SUN_EMUL_NARGS]; /* command args, if CONTROL */
89 u_int scrolldist; /* distance to scroll */
90 long defattr; /* default attribute (rendition) */
91 long bowattr; /* attribute for reversed mode */
92 int rendflags;
93 #define REND_BOW 1
94 #define REND_SO 2
95 long curattr; /* currently used attribute */
96 long kernattr; /* attribute for kernel output */
97 #ifdef DIAGNOSTIC
98 int console;
99 #endif
100 };
101
102 static u_int wsemul_sun_output_normal __P((struct wsemul_sun_emuldata *,
103 u_char, int));
104 static u_int wsemul_sun_output_haveesc __P((struct wsemul_sun_emuldata *,
105 u_char));
106 static u_int wsemul_sun_output_control __P((struct wsemul_sun_emuldata *,
107 u_char));
108 static void wsemul_sun_control __P((struct wsemul_sun_emuldata *, u_char));
109
110 struct wsemul_sun_emuldata wsemul_sun_console_emuldata;
111
112 /* some useful utility macros */
113 #define ARG(n) (edp->args[(n)])
114 #define NORMALIZE_ARG(n) (ARG(n) ? ARG(n) : 1)
115 #define COLS_LEFT (edp->ncols - edp->ccol - 1)
116 #define ROWS_LEFT (edp->nrows - edp->crow - 1)
117
118 void *
119 wsemul_sun_cnattach(type, cookie, ccol, crow, defattr)
120 const struct wsscreen_descr *type;
121 void *cookie;
122 int ccol, crow;
123 long defattr;
124 {
125 struct wsemul_sun_emuldata *edp;
126 int res;
127
128 edp = &wsemul_sun_console_emuldata;
129
130 edp->emulops = type->textops;
131 edp->emulcookie = cookie;
132 edp->scrcapabilities = type->capabilities;
133 edp->nrows = type->nrows;
134 edp->ncols = type->ncols;
135 edp->crow = crow;
136 edp->ccol = ccol;
137 edp->curattr = edp->defattr = defattr;
138 #if defined(WS_KERNEL_FG) || defined(WS_KERNEL_BG) || \
139 defined(WS_KERNEL_COLATTR) || defined(WS_KERNEL_MONOATTR)
140 #ifndef WS_KERNEL_FG
141 #define WS_KERNEL_FG WSCOL_WHITE
142 #endif
143 #ifndef WS_KERNEL_BG
144 #define WS_KERNEL_BG WSCOL_BLACK
145 #endif
146 #ifndef WS_KERNEL_COLATTR
147 #define WS_KERNEL_COLATTR 0
148 #endif
149 #ifndef WS_KERNEL_MONOATTR
150 #define WS_KERNEL_MONOATTR 0
151 #endif
152 if (type->capabilities & WSSCREEN_WSCOLORS)
153 res = (*edp->emulops->alloc_attr)(cookie,
154 WS_KERNEL_FG, WS_KERNEL_BG,
155 WS_KERNEL_COLATTR | WSATTR_WSCOLORS,
156 &edp->kernattr);
157 else
158 res = (*edp->emulops->alloc_attr)(cookie, 0, 0,
159 WS_KERNEL_MONOATTR,
160 &edp->kernattr);
161 if (res)
162 #else
163 res = 0; /* XXX gcc */
164 #endif
165 edp->kernattr = defattr;
166
167 edp->cbcookie = NULL;
168
169 edp->state = SUN_EMUL_STATE_NORMAL;
170 edp->scrolldist = 1;
171 #ifdef DIAGNOSTIC
172 edp->console = 1;
173 #endif
174 return (edp);
175 }
176
177 void *
178 wsemul_sun_attach(console, type, cookie, ccol, crow, cbcookie, defattr)
179 int console;
180 const struct wsscreen_descr *type;
181 void *cookie;
182 int ccol, crow;
183 void *cbcookie;
184 long defattr;
185 {
186 struct wsemul_sun_emuldata *edp;
187
188 if (console) {
189 edp = &wsemul_sun_console_emuldata;
190 #ifdef DIAGNOSTIC
191 KASSERT(edp->console == 1);
192 #endif
193 } else {
194 edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK);
195
196 edp->emulops = type->textops;
197 edp->emulcookie = cookie;
198 edp->scrcapabilities = type->capabilities;
199 edp->nrows = type->nrows;
200 edp->ncols = type->ncols;
201 edp->crow = crow;
202 edp->ccol = ccol;
203 edp->defattr = defattr;
204
205 edp->state = SUN_EMUL_STATE_NORMAL;
206 edp->scrolldist = 1;
207 #ifdef DIAGNOSTIC
208 edp->console = 0;
209 #endif
210 }
211
212 edp->cbcookie = cbcookie;
213
214 /* XXX This assumes that the default attribute is wob. */
215 if ((!(edp->scrcapabilities & WSSCREEN_WSCOLORS) ||
216 (*edp->emulops->alloc_attr)(edp->emulcookie,
217 WSCOL_BLACK, WSCOL_WHITE,
218 WSATTR_WSCOLORS,
219 &edp->bowattr)) &&
220 (!(edp->scrcapabilities & WSSCREEN_REVERSE) ||
221 (*edp->emulops->alloc_attr)(edp->emulcookie, 0, 0,
222 WSATTR_REVERSE,
223 &edp->bowattr)))
224 edp->bowattr = edp->defattr;
225
226 edp->curattr = edp->defattr;
227 edp->rendflags = 0;
228
229 return (edp);
230 }
231
232 static inline u_int
233 wsemul_sun_output_normal(edp, c, kernel)
234 struct wsemul_sun_emuldata *edp;
235 u_char c;
236 int kernel;
237 {
238 u_int newstate = SUN_EMUL_STATE_NORMAL;
239 u_int n;
240
241 switch (c) {
242 case ASCII_BEL: /* "Bell (BEL)" */
243 wsdisplay_emulbell(edp->cbcookie);
244 break;
245
246 case ASCII_BS: /* "Backspace (BS)" */
247 if (edp->ccol > 0)
248 edp->ccol--;
249 break;
250
251 case ASCII_CR: /* "Return (CR)" */
252 edp->ccol = 0;
253 break;
254
255 case ASCII_HT: /* "Tab (TAB)" */
256 n = min(8 - (edp->ccol & 7), COLS_LEFT);
257 (*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
258 edp->ccol, n,
259 kernel ? edp->kernattr : edp->curattr);
260 edp->ccol += n;
261 break;
262
263 case ASCII_FF: /* "Form Feed (FF)" */
264 (*edp->emulops->eraserows)(edp->emulcookie, 0, edp->nrows,
265 kernel ? edp->kernattr : edp->curattr);
266 /* XXX possible in kernel output? */
267 edp->ccol = 0;
268 edp->crow = 0;
269 break;
270
271 case ASCII_VT: /* "Reverse Line Feed" */
272 if (edp->crow > 0)
273 edp->crow--;
274 break;
275
276 case ASCII_ESC: /* "Escape (ESC)" */
277 #ifdef DIAGNOSTIC
278 if (kernel)
279 panic("ESC in kernel output");
280 #endif
281 if (edp->state == SUN_EMUL_STATE_NORMAL) {
282 newstate = SUN_EMUL_STATE_HAVEESC;
283 break;
284 }
285 /* special case: fall through, we're printing one out */
286 /* FALLTHRU */
287
288 default: /* normal character */
289 (*edp->emulops->putchar)(edp->emulcookie, edp->crow, edp->ccol,
290 c, kernel ? edp->kernattr : edp->curattr);
291 edp->ccol++;
292
293 /* if cur col is still on cur line, done. */
294 if (edp->ccol < edp->ncols)
295 break;
296
297 /* wrap the column around. */
298 edp->ccol = 0;
299
300 /* FALLTHRU */
301
302 case ASCII_LF: /* "Line Feed (LF)" */
303 /* if the cur line isn't the last, incr and leave. */
304 if (edp->crow < edp->nrows - 1) {
305 edp->crow++;
306 break;
307 }
308
309 /*
310 * if we're in wrap-around mode, go to the first
311 * line and clear it.
312 */
313 if (edp->scrolldist == 0) {
314 edp->crow = 0;
315 (*edp->emulops->eraserows)(edp->emulcookie, 0, 1,
316 edp->curattr);
317 break;
318 }
319
320 /* scroll by the scrolling distance. */
321 (*edp->emulops->copyrows)(edp->emulcookie, edp->scrolldist, 0,
322 edp->nrows - edp->scrolldist);
323 (*edp->emulops->eraserows)(edp->emulcookie,
324 edp->nrows - edp->scrolldist, edp->scrolldist,
325 edp->curattr);
326 edp->crow -= edp->scrolldist - 1;
327 break;
328 }
329
330 return (newstate);
331 }
332
333 static inline u_int
334 wsemul_sun_output_haveesc(edp, c)
335 struct wsemul_sun_emuldata *edp;
336 u_char c;
337 {
338 u_int newstate;
339
340 switch (c) {
341 case '[': /* continuation of multi-char sequence */
342 memset(edp->args, 0, sizeof (edp->args));
343 newstate = SUN_EMUL_STATE_CONTROL;
344 break;
345
346 default:
347 /* spit out the escape char (???), then the new character */
348 wsemul_sun_output_normal(edp, ASCII_ESC, 0); /* ??? */
349 newstate = wsemul_sun_output_normal(edp, c, 0);
350 break;
351 }
352
353 return (newstate);
354 }
355
356 static inline void
357 wsemul_sun_control(edp, c)
358 struct wsemul_sun_emuldata *edp;
359 u_char c;
360 {
361 u_int n, src, dst;
362
363 switch (c) {
364 case '@': /* "Insert Character (ICH)" */
365 n = min(NORMALIZE_ARG(0), COLS_LEFT + 1);
366 src = edp->ccol;
367 dst = edp->ccol + n;
368 if (dst < edp->ncols) {
369 (*edp->emulops->copycols)(edp->emulcookie, edp->crow,
370 src, dst, edp->ncols - dst);
371 }
372 (*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
373 src, dst - src, edp->curattr);
374 break;
375
376 case 'A': /* "Cursor Up (CUU)" */
377 edp->crow -= min(NORMALIZE_ARG(0), edp->crow);
378 break;
379
380 case 'E': /* "Cursor Next Line (CNL)" */
381 edp->ccol = 0;
382 /* FALLTHRU */
383 case 'B': /* "Cursor Down (CUD)" */
384 edp->crow += min(NORMALIZE_ARG(0), ROWS_LEFT);
385 break;
386
387 case 'C': /* "Cursor Forward (CUF)" */
388 edp->ccol += min(NORMALIZE_ARG(0), COLS_LEFT);
389 break;
390
391 case 'D': /* "Cursor Backward (CUB)" */
392 edp->ccol -= min(NORMALIZE_ARG(0), edp->ccol);
393 break;
394
395 case 'f': /* "Horizontal And Vertical Position (HVP)" */
396 case 'H': /* "Cursor Position (CUP)" */
397 edp->crow = min(NORMALIZE_ARG(1), edp->nrows) - 1;
398 edp->ccol = min(NORMALIZE_ARG(0), edp->ncols) - 1;
399 break;
400
401 case 'J': /* "Erase in Display (ED)" */
402 if (ROWS_LEFT > 0) {
403 (*edp->emulops->eraserows)(edp->emulcookie,
404 edp->crow + 1, ROWS_LEFT, edp->curattr);
405 }
406 /* FALLTHRU */
407 case 'K': /* "Erase in Line (EL)" */
408 (*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
409 edp->ccol, COLS_LEFT + 1, edp->curattr);
410 break;
411
412 case 'L': /* "Insert Line (IL)" */
413 n = min(NORMALIZE_ARG(0), ROWS_LEFT + 1);
414 src = edp->crow;
415 dst = edp->crow + n;
416 if (dst < edp->nrows) {
417 (*edp->emulops->copyrows)(edp->emulcookie,
418 src, dst, edp->nrows - dst);
419 }
420 (*edp->emulops->eraserows)(edp->emulcookie,
421 src, dst - src, edp->curattr);
422 break;
423
424 case 'M': /* "Delete Line (DL)" */
425 n = min(NORMALIZE_ARG(0), ROWS_LEFT + 1);
426 src = edp->crow + n;
427 dst = edp->crow;
428 if (src < edp->nrows) {
429 (*edp->emulops->copyrows)(edp->emulcookie,
430 src, dst, edp->nrows - src);
431 }
432 (*edp->emulops->eraserows)(edp->emulcookie,
433 dst + edp->nrows - src, src - dst, edp->curattr);
434 break;
435
436 case 'P': /* "Delete Character (DCH)" */
437 n = min(NORMALIZE_ARG(0), COLS_LEFT + 1);
438 src = edp->ccol + n;
439 dst = edp->ccol;
440 if (src < edp->ncols) {
441 (*edp->emulops->copycols)(edp->emulcookie, edp->crow,
442 src, dst, edp->ncols - src);
443 }
444 (*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
445 dst + edp->ncols - src, src - dst, edp->curattr);
446 break;
447
448 case 'm': /* "Select Graphic Rendition (SGR)" */
449 if (ARG(0))
450 edp->rendflags |= REND_SO;
451 else
452 edp->rendflags &= ~REND_SO;
453 goto setattr;
454
455 case 'p': /* "Black On White (SUNBOW)" */
456 edp->rendflags |= REND_BOW;
457 goto setattr;
458
459 case 'q': /* "White On Black (SUNWOB)" */
460 edp->rendflags &= ~REND_BOW;
461 goto setattr;
462
463 case 'r': /* "Set Scrolling (SUNSCRL)" */
464 edp->scrolldist = min(ARG(0), edp->nrows);
465 break;
466
467 case 's': /* "Reset Terminal Emulator (SUNRESET)" */
468 edp->scrolldist = 1;
469 edp->rendflags = 0;
470 setattr:
471 if (((edp->rendflags & REND_BOW) != 0) ^
472 ((edp->rendflags & REND_SO) != 0))
473 edp->curattr = edp->bowattr;
474 else
475 edp->curattr = edp->defattr;
476 break;
477 }
478 }
479
480 static inline u_int
481 wsemul_sun_output_control(edp, c)
482 struct wsemul_sun_emuldata *edp;
483 u_char c;
484 {
485 u_int newstate = SUN_EMUL_STATE_CONTROL;
486 u_int i;
487
488 switch (c) {
489 case '0': case '1': case '2': case '3': case '4': /* argument digit */
490 case '5': case '6': case '7': case '8': case '9':
491 edp->args[0] = (edp->args[0] * 10) + (c - '0');
492 break;
493
494 case ';': /* argument terminator */
495 for (i = 1; i < SUN_EMUL_NARGS; i++)
496 edp->args[i] = edp->args[i - 1];
497 edp->args[0] = 0;
498 break;
499
500 default: /* end of escape sequence */
501 wsemul_sun_control(edp, c);
502 newstate = SUN_EMUL_STATE_NORMAL;
503 break;
504 }
505 return (newstate);
506 }
507
508 void
509 wsemul_sun_output(cookie, data, count, kernel)
510 void *cookie;
511 const u_char *data;
512 u_int count;
513 int kernel;
514 {
515 struct wsemul_sun_emuldata *edp = cookie;
516 u_int newstate;
517
518 #ifdef DIAGNOSTIC
519 if (kernel && !edp->console)
520 panic("wsemul_sun_output: kernel output, not console");
521 #endif
522
523 /* XXX */
524 (*edp->emulops->cursor)(edp->emulcookie, 0, edp->crow, edp->ccol);
525 for (; count > 0; data++, count--) {
526 if (kernel) {
527 wsemul_sun_output_normal(edp, *data, 1);
528 continue;
529 }
530 switch (edp->state) {
531 case SUN_EMUL_STATE_NORMAL:
532 /* XXX SCAN INPUT FOR NEWLINES, DO PRESCROLLING */
533 newstate = wsemul_sun_output_normal(edp, *data, 0);
534 break;
535 case SUN_EMUL_STATE_HAVEESC:
536 newstate = wsemul_sun_output_haveesc(edp, *data);
537 break;
538 case SUN_EMUL_STATE_CONTROL:
539 newstate = wsemul_sun_output_control(edp, *data);
540 break;
541 default:
542 #ifdef DIAGNOSTIC
543 panic("wsemul_sun: invalid state %d\n", edp->state);
544 #endif
545 /* try to recover, if things get screwed up... */
546 newstate = wsemul_sun_output_normal(edp, *data, 0);
547 break;
548 }
549 edp->state = newstate;
550 }
551 /* XXX */
552 (*edp->emulops->cursor)(edp->emulcookie, 1, edp->crow, edp->ccol);
553 }
554
555 static char *sun_fkeys[] = {
556 "\033[224z", /* F1 */
557 "\033[225z",
558 "\033[226z",
559 "\033[227z",
560 "\033[228z",
561 "\033[229z",
562 "\033[230z",
563 "\033[231z",
564 "\033[232z",
565 "\033[233z", /* F10 */
566 };
567
568 int
569 wsemul_sun_translate(cookie, in, out)
570 void *cookie;
571 keysym_t in;
572 char **out;
573 {
574 static char c;
575
576 if (KS_GROUP(in) == KS_GROUP_Keypad && (in & 0x80) == 0) {
577 c = in & 0xff; /* turn into ASCII */
578 *out = &c;
579 return (1);
580 }
581
582 if (in >= KS_f1 && in <= KS_f10) {
583 *out = sun_fkeys[in - KS_f1];
584 return (6);
585 }
586 if (in >= KS_F1 && in <= KS_F10) {
587 *out = sun_fkeys[in - KS_F1];
588 return (6);
589 }
590 if (in >= KS_KP_F1 && in <= KS_KP_F4) {
591 *out = sun_fkeys[in - KS_KP_F1];
592 return (6);
593 }
594
595 switch (in) {
596 case KS_Home:
597 case KS_KP_Home:
598 case KS_KP_Begin:
599 *out = "\033[214z";
600 return (6);
601 case KS_Prior:
602 case KS_KP_Prior:
603 *out = "\033[216z";
604 return (6);
605 case KS_Next:
606 case KS_KP_Next:
607 *out = "\033[222z";
608 return (6);
609 case KS_Up:
610 case KS_KP_Up:
611 *out = "\033[A";
612 return (3);
613 case KS_Down:
614 case KS_KP_Down:
615 *out = "\033[B";
616 return (3);
617 case KS_Left:
618 case KS_KP_Left:
619 *out = "\033[D";
620 return (3);
621 case KS_Right:
622 case KS_KP_Right:
623 *out = "\033[C";
624 return (3);
625 case KS_KP_Delete:
626 *out = "\177";
627 return (1);
628 }
629 return (0);
630 }
631
632 void
633 wsemul_sun_detach(cookie, crowp, ccolp)
634 void *cookie;
635 u_int *crowp, *ccolp;
636 {
637 struct wsemul_sun_emuldata *edp = cookie;
638
639 *crowp = edp->crow;
640 *ccolp = edp->ccol;
641 if (edp != &wsemul_sun_console_emuldata)
642 free(edp, M_DEVBUF);
643 }
644
645 void
646 wsemul_sun_resetop(cookie, op)
647 void *cookie;
648 enum wsemul_resetops op;
649 {
650 struct wsemul_sun_emuldata *edp = cookie;
651
652 switch (op) {
653 case WSEMUL_RESET:
654 edp->state = SUN_EMUL_STATE_NORMAL;
655 edp->scrolldist = 1;
656 edp->rendflags = 0;
657 edp->curattr = edp->defattr;
658 break;
659 case WSEMUL_CLEARSCREEN:
660 (*edp->emulops->eraserows)(edp->emulcookie, 0, edp->nrows,
661 edp->defattr);
662 edp->ccol = edp->crow = 0;
663 (*edp->emulops->cursor)(edp->emulcookie, 1, 0, 0);
664 break;
665 default:
666 break;
667 }
668 }
669