66f46b69fbd75d474d187df53aa104cb4ed28619
[dragonfly.git] / sys / dev / misc / syscons / scterm-sc.c
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * Copyright (c) 1992-1998 Søren Schmidt
4  * 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 as
11  *    the first lines of this file unmodified.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/syscons/scterm-sc.c,v 1.4.2.10 2001/06/11 09:05:39 phk Exp $
28  * $DragonFly: src/sys/dev/misc/syscons/scterm-sc.c,v 1.4 2003/08/07 21:16:59 dillon Exp $
29  */
30
31 #include "opt_syscons.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/consio.h>
37
38 #include <machine/pc/display.h>
39
40 #include "syscons.h"
41 #include "sctermvar.h"
42
43 #ifndef SC_DUMB_TERMINAL
44
45 #define MAX_ESC_PAR     5
46
47 /* attribute flags */
48 typedef struct {
49         u_short         fg;                     /* foreground color */
50         u_short         bg;                     /* background color */
51 } color_t;
52
53 typedef struct {
54         int             flags;
55 #define SCTERM_BUSY     (1 << 0)
56         int             esc;
57         int             num_param;
58         int             last_param;
59         int             param[MAX_ESC_PAR];
60         int             saved_xpos;
61         int             saved_ypos;
62         int             attr_mask;              /* current logical attr mask */
63 #define NORMAL_ATTR     0x00
64 #define BLINK_ATTR      0x01
65 #define BOLD_ATTR       0x02
66 #define UNDERLINE_ATTR  0x04
67 #define REVERSE_ATTR    0x08
68 #define FG_CHANGED      0x10
69 #define BG_CHANGED      0x20
70         int             cur_attr;               /* current hardware attr word */
71         color_t         cur_color;              /* current hardware color */
72         color_t         std_color;              /* normal hardware color */
73         color_t         rev_color;              /* reverse hardware color */
74         color_t         dflt_std_color;         /* default normal color */
75         color_t         dflt_rev_color;         /* default reverse color */
76 } term_stat;
77
78 static sc_term_init_t   scterm_init;
79 static sc_term_term_t   scterm_term;
80 static sc_term_puts_t   scterm_puts;
81 static sc_term_ioctl_t  scterm_ioctl;
82 static sc_term_reset_t  scterm_reset;
83 static sc_term_default_attr_t   scterm_default_attr;
84 static sc_term_clear_t  scterm_clear;
85 static sc_term_notify_t scterm_notify;
86 static sc_term_input_t  scterm_input;
87
88 static sc_term_sw_t sc_term_sc = {
89         { NULL, NULL },
90         "sc",                                   /* emulator name */
91         "syscons terminal",                     /* description */
92         "*",                                    /* matching renderer, any :-) */
93         sizeof(term_stat),                      /* softc size */
94         0,
95         scterm_init,
96         scterm_term,
97         scterm_puts,
98         scterm_ioctl,
99         scterm_reset,
100         scterm_default_attr,
101         scterm_clear,
102         scterm_notify,
103         scterm_input,
104 };
105
106 SCTERM_MODULE(sc, sc_term_sc);
107
108 static term_stat        reserved_term_stat;
109 static void             scterm_scan_esc(scr_stat *scp, term_stat *tcp,
110                                         u_char c);
111 static int              mask2attr(term_stat *tcp);
112
113 static int
114 scterm_init(scr_stat *scp, void **softc, int code)
115 {
116         term_stat *tcp;
117
118         if (*softc == NULL) {
119                 if (reserved_term_stat.flags & SCTERM_BUSY)
120                         return EINVAL;
121                 *softc = &reserved_term_stat;
122         }
123         tcp = *softc;
124
125         switch (code) {
126         case SC_TE_COLD_INIT:
127                 bzero(tcp, sizeof(*tcp));
128                 tcp->flags = SCTERM_BUSY;
129                 tcp->esc = 0;
130                 tcp->saved_xpos = -1;
131                 tcp->saved_ypos = -1;
132                 tcp->attr_mask = NORMAL_ATTR;
133                 /* XXX */
134                 tcp->dflt_std_color.fg = SC_NORM_ATTR & 0x0f;
135                 tcp->dflt_std_color.bg = (SC_NORM_ATTR >> 4) & 0x0f;
136                 tcp->dflt_rev_color.fg = SC_NORM_REV_ATTR & 0x0f;
137                 tcp->dflt_rev_color.bg = (SC_NORM_REV_ATTR >> 4) & 0x0f;
138                 tcp->std_color = tcp->dflt_std_color;
139                 tcp->rev_color = tcp->dflt_rev_color;
140                 tcp->cur_color = tcp->std_color;
141                 tcp->cur_attr = mask2attr(tcp);
142                 ++sc_term_sc.te_refcount;
143                 break;
144
145         case SC_TE_WARM_INIT:
146                 tcp->esc = 0;
147                 tcp->saved_xpos = -1;
148                 tcp->saved_ypos = -1;
149 #if 0
150                 tcp->std_color = tcp->dflt_std_color;
151                 tcp->rev_color = tcp->dflt_rev_color;
152 #endif
153                 tcp->cur_color = tcp->std_color;
154                 tcp->cur_attr = mask2attr(tcp);
155                 break;
156         }
157
158         return 0;
159 }
160
161 static int
162 scterm_term(scr_stat *scp, void **softc)
163 {
164         if (*softc == &reserved_term_stat) {
165                 *softc = NULL;
166                 bzero(&reserved_term_stat, sizeof(reserved_term_stat));
167         }
168         --sc_term_sc.te_refcount;
169         return 0;
170 }
171
172 static void
173 scterm_scan_esc(scr_stat *scp, term_stat *tcp, u_char c)
174 {
175         static u_char ansi_col[16] = {
176 #ifdef __alpha__
177                 /*
178                  * DEC is evil.  They switch the red and blue attributes in
179                  * the palette in the system console.  As a simple work-around,
180                  * re-map the ANSI colors appropriately.
181                  */
182                 FG_BLACK,     FG_BLUE,         FG_GREEN,      FG_CYAN,
183                 FG_RED,       FG_MAGENTA,      FG_BROWN,      FG_LIGHTGREY,
184                 FG_DARKGREY,  FG_LIGHTBLUE,    FG_LIGHTGREEN, FG_LIGHTCYAN,
185                 FG_LIGHTRED,  FG_LIGHTMAGENTA, FG_YELLOW,     FG_WHITE
186 #else
187                 FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
188                 FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
189                 FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
190                 FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE
191 #endif
192         };
193         sc_softc_t *sc;
194         int i, n;
195
196         i = n = 0;
197         sc = scp->sc; 
198         if (tcp->esc == 1) {    /* seen ESC */
199                 switch (c) {
200
201                 case '7':       /* Save cursor position */
202                         tcp->saved_xpos = scp->xpos;
203                         tcp->saved_ypos = scp->ypos;
204                         break;
205
206                 case '8':       /* Restore saved cursor position */
207                         if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
208                                 sc_move_cursor(scp, tcp->saved_xpos,
209                                                tcp->saved_ypos);
210                         break;
211
212                 case '[':       /* Start ESC [ sequence */
213                         tcp->esc = 2;
214                         tcp->last_param = -1;
215                         for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
216                                 tcp->param[i] = 1;
217                         tcp->num_param = 0;
218                         return;
219
220                 case 'M':       /* Move cursor up 1 line, scroll if at top */
221                         sc_term_up_scroll(scp, 1, sc->scr_map[0x20],
222                                           tcp->cur_attr, 0, 0);
223                         break;
224 #if notyet
225                 case 'Q':
226                         tcp->esc = 4;
227                         return;
228 #endif
229                 case 'c':       /* reset */
230                         tcp->attr_mask = NORMAL_ATTR;
231                         tcp->cur_color = tcp->std_color
232                                        = tcp->dflt_std_color;
233                         tcp->rev_color = tcp->dflt_rev_color;
234                         tcp->cur_attr = mask2attr(tcp);
235                         sc_clear_screen(scp);
236                         break;
237
238                 case '(':       /* iso-2022: designate 94 character set to G0 */
239                         tcp->esc = 5;
240                         return;
241                 }
242         } else if (tcp->esc == 2) {     /* seen ESC [ */
243                 if (c >= '0' && c <= '9') {
244                         if (tcp->num_param < MAX_ESC_PAR) {
245                                 if (tcp->last_param != tcp->num_param) {
246                                         tcp->last_param = tcp->num_param;
247                                         tcp->param[tcp->num_param] = 0;
248                                 } else {
249                                         tcp->param[tcp->num_param] *= 10;
250                                 }
251                                 tcp->param[tcp->num_param] += c - '0';
252                                 return;
253                         }
254                 }
255                 tcp->num_param = tcp->last_param + 1;
256                 switch (c) {
257
258                 case ';':
259                         if (tcp->num_param < MAX_ESC_PAR)
260                                 return;
261                         break;
262
263                 case '=':
264                         tcp->esc = 3;
265                         tcp->last_param = -1;
266                         for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
267                                 tcp->param[i] = 1;
268                         tcp->num_param = 0;
269                         return;
270
271                 case 'A':       /* up n rows */
272                         sc_term_up(scp, tcp->param[0], 0);
273                         break;
274
275                 case 'B':       /* down n rows */
276                         sc_term_down(scp, tcp->param[0], 0);
277                         break;
278
279                 case 'C':       /* right n columns */
280                         sc_term_right(scp, tcp->param[0]);
281                         break;
282
283                 case 'D':       /* left n columns */
284                         sc_term_left(scp, tcp->param[0]);
285                         break;
286
287                 case 'E':       /* cursor to start of line n lines down */
288                         n = tcp->param[0];
289                         if (n < 1)
290                                 n = 1;
291                         sc_move_cursor(scp, 0, scp->ypos + n);
292                         break;
293
294                 case 'F':       /* cursor to start of line n lines up */
295                         n = tcp->param[0];
296                         if (n < 1)
297                                 n = 1;
298                         sc_move_cursor(scp, 0, scp->ypos - n);
299                         break;
300
301                 case 'f':       /* Cursor move */
302                 case 'H':
303                         if (tcp->num_param == 0)
304                                 sc_move_cursor(scp, 0, 0);
305                         else if (tcp->num_param == 2)
306                                 sc_move_cursor(scp, tcp->param[1] - 1,
307                                                tcp->param[0] - 1);
308                         break;
309
310                 case 'J':       /* Clear all or part of display */
311                         if (tcp->num_param == 0)
312                                 n = 0;
313                         else
314                                 n = tcp->param[0];
315                         sc_term_clr_eos(scp, n, sc->scr_map[0x20],
316                                         tcp->cur_attr);
317                         break;
318
319                 case 'K':       /* Clear all or part of line */
320                         if (tcp->num_param == 0)
321                                 n = 0;
322                         else
323                                 n = tcp->param[0];
324                         sc_term_clr_eol(scp, n, sc->scr_map[0x20],
325                                         tcp->cur_attr);
326                         break;
327
328                 case 'L':       /* Insert n lines */
329                         sc_term_ins_line(scp, scp->ypos, tcp->param[0],
330                                          sc->scr_map[0x20], tcp->cur_attr, 0);
331                         break;
332
333                 case 'M':       /* Delete n lines */
334                         sc_term_del_line(scp, scp->ypos, tcp->param[0],
335                                          sc->scr_map[0x20], tcp->cur_attr, 0);
336                         break;
337
338                 case 'P':       /* Delete n chars */
339                         sc_term_del_char(scp, tcp->param[0],
340                                          sc->scr_map[0x20], tcp->cur_attr);
341                         break;
342
343                 case '@':       /* Insert n chars */
344                         sc_term_ins_char(scp, tcp->param[0],
345                                          sc->scr_map[0x20], tcp->cur_attr);
346                         break;
347
348                 case 'S':       /* scroll up n lines */
349                         sc_term_del_line(scp, 0, tcp->param[0],
350                                          sc->scr_map[0x20], tcp->cur_attr, 0);
351                         break;
352
353                 case 'T':       /* scroll down n lines */
354                         sc_term_ins_line(scp, 0, tcp->param[0],
355                                          sc->scr_map[0x20], tcp->cur_attr, 0);
356                         break;
357
358                 case 'X':       /* erase n characters in line */
359                         n = tcp->param[0];
360                         if (n < 1)
361                                 n = 1;
362                         if (n > scp->xsize - scp->xpos)
363                                 n = scp->xsize - scp->xpos;
364                         sc_vtb_erase(&scp->vtb, scp->cursor_pos, n,
365                                      sc->scr_map[0x20], tcp->cur_attr);
366                         mark_for_update(scp, scp->cursor_pos);
367                         mark_for_update(scp, scp->cursor_pos + n - 1);
368                         break;
369
370                 case 'Z':       /* move n tabs backwards */
371                         sc_term_backtab(scp, tcp->param[0]);
372                         break;
373
374                 case '`':       /* move cursor to column n */
375                         sc_term_col(scp, tcp->param[0]);
376                         break;
377
378                 case 'a':       /* move cursor n columns to the right */
379                         sc_term_right(scp, tcp->param[0]);
380                         break;
381
382                 case 'd':       /* move cursor to row n */
383                         sc_term_row(scp, tcp->param[0]);
384                         break;
385
386                 case 'e':       /* move cursor n rows down */
387                         sc_term_down(scp, tcp->param[0], 0);
388                         break;
389
390                 case 'm':       /* change attribute */
391                         if (tcp->num_param == 0) {
392                                 tcp->attr_mask = NORMAL_ATTR;
393                                 tcp->cur_color = tcp->std_color;
394                                 tcp->cur_attr = mask2attr(tcp);
395                                 break;
396                         }
397                         for (i = 0; i < tcp->num_param; i++) {
398                                 switch (n = tcp->param[i]) {
399                                 case 0: /* back to normal */
400                                         tcp->attr_mask = NORMAL_ATTR;
401                                         tcp->cur_color = tcp->std_color;
402                                         tcp->cur_attr = mask2attr(tcp);
403                                         break;
404                                 case 1: /* bold */
405                                         tcp->attr_mask |= BOLD_ATTR;
406                                         tcp->cur_attr = mask2attr(tcp);
407                                         break;
408                                 case 4: /* underline */
409                                         tcp->attr_mask |= UNDERLINE_ATTR;
410                                         tcp->cur_attr = mask2attr(tcp);
411                                         break;
412                                 case 5: /* blink */
413                                         tcp->attr_mask |= BLINK_ATTR;
414                                         tcp->cur_attr = mask2attr(tcp);
415                                         break;
416                                 case 7: /* reverse */
417                                         tcp->attr_mask |= REVERSE_ATTR;
418                                         tcp->cur_attr = mask2attr(tcp);
419                                         break;
420                                 case 22: /* remove bold (or dim) */
421                                         tcp->attr_mask &= ~BOLD_ATTR;
422                                         tcp->cur_attr = mask2attr(tcp);
423                                         break;
424                                 case 24: /* remove underline */
425                                         tcp->attr_mask &= ~UNDERLINE_ATTR;
426                                         tcp->cur_attr = mask2attr(tcp);
427                                         break;
428                                 case 25: /* remove blink */
429                                         tcp->attr_mask &= ~BLINK_ATTR;
430                                         tcp->cur_attr = mask2attr(tcp);
431                                         break;
432                                 case 27: /* remove reverse */
433                                         tcp->attr_mask &= ~REVERSE_ATTR;
434                                         tcp->cur_attr = mask2attr(tcp);
435                                         break;
436                                 case 30: case 31: /* set ansi fg color */
437                                 case 32: case 33: case 34:
438                                 case 35: case 36: case 37:
439                                         tcp->attr_mask |= FG_CHANGED;
440                                         tcp->cur_color.fg = ansi_col[n - 30];
441                                         tcp->cur_attr = mask2attr(tcp);
442                                         break;
443                                 case 39: /* restore fg color back to normal */
444                                         tcp->attr_mask &= ~(FG_CHANGED|BOLD_ATTR);
445                                         tcp->cur_color.fg = tcp->std_color.fg;
446                                         tcp->cur_attr = mask2attr(tcp);
447                                         break;
448                                 case 40: case 41: /* set ansi bg color */
449                                 case 42: case 43: case 44:
450                                 case 45: case 46: case 47:
451                                         tcp->attr_mask |= BG_CHANGED;
452                                         tcp->cur_color.bg = ansi_col[n - 40];
453                                         tcp->cur_attr = mask2attr(tcp);
454                                         break;
455                                 case 49: /* restore bg color back to normal */
456                                         tcp->attr_mask &= ~BG_CHANGED;
457                                         tcp->cur_color.bg = tcp->std_color.bg;
458                                         tcp->cur_attr = mask2attr(tcp);
459                                         break;
460                                 }
461                         }
462                         break;
463
464                 case 's':       /* Save cursor position */
465                         tcp->saved_xpos = scp->xpos;
466                         tcp->saved_ypos = scp->ypos;
467                         break;
468
469                 case 'u':       /* Restore saved cursor position */
470                         if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
471                                 sc_move_cursor(scp, tcp->saved_xpos,
472                                                tcp->saved_ypos);
473                         break;
474
475                 case 'x':
476                         if (tcp->num_param == 0)
477                                 n = 0;
478                         else
479                                 n = tcp->param[0];
480                         switch (n) {
481                         case 0: /* reset colors and attributes back to normal */
482                                 tcp->attr_mask = NORMAL_ATTR;
483                                 tcp->cur_color = tcp->std_color
484                                                = tcp->dflt_std_color;
485                                 tcp->rev_color = tcp->dflt_rev_color;
486                                 tcp->cur_attr = mask2attr(tcp);
487                                 break;
488                         case 1: /* set ansi background */
489                                 tcp->attr_mask &= ~BG_CHANGED;
490                                 tcp->cur_color.bg = tcp->std_color.bg
491                                                   = ansi_col[tcp->param[1] & 0x0f];
492                                 tcp->cur_attr = mask2attr(tcp);
493                                 break;
494                         case 2: /* set ansi foreground */
495                                 tcp->attr_mask &= ~FG_CHANGED;
496                                 tcp->cur_color.fg = tcp->std_color.fg
497                                                   = ansi_col[tcp->param[1] & 0x0f];
498                                 tcp->cur_attr = mask2attr(tcp);
499                                 break;
500                         case 3: /* set adapter attribute directly */
501                                 tcp->attr_mask &= ~(FG_CHANGED | BG_CHANGED);
502                                 tcp->cur_color.fg = tcp->std_color.fg
503                                                   = tcp->param[1] & 0x0f;
504                                 tcp->cur_color.bg = tcp->std_color.bg
505                                                   = (tcp->param[1] >> 4) & 0x0f;
506                                 tcp->cur_attr = mask2attr(tcp);
507                                 break;
508                         case 5: /* set ansi reverse background */
509                                 tcp->rev_color.bg = ansi_col[tcp->param[1] & 0x0f];
510                                 tcp->cur_attr = mask2attr(tcp);
511                                 break;
512                         case 6: /* set ansi reverse foreground */
513                                 tcp->rev_color.fg = ansi_col[tcp->param[1] & 0x0f];
514                                 tcp->cur_attr = mask2attr(tcp);
515                                 break;
516                         case 7: /* set adapter reverse attribute directly */
517                                 tcp->rev_color.fg = tcp->param[1] & 0x0f;
518                                 tcp->rev_color.bg = (tcp->param[1] >> 4) & 0x0f;
519                                 tcp->cur_attr = mask2attr(tcp);
520                                 break;
521                         }
522                         break;
523
524                 case 'z':       /* switch to (virtual) console n */
525                         if (tcp->num_param == 1)
526                                 sc_switch_scr(sc, tcp->param[0]);
527                         break;
528                 }
529         } else if (tcp->esc == 3) {     /* seen ESC [0-9]+ = */
530                 if (c >= '0' && c <= '9') {
531                         if (tcp->num_param < MAX_ESC_PAR) {
532                                 if (tcp->last_param != tcp->num_param) {
533                                         tcp->last_param = tcp->num_param;
534                                         tcp->param[tcp->num_param] = 0;
535                                 } else {
536                                         tcp->param[tcp->num_param] *= 10;
537                                 }
538                                 tcp->param[tcp->num_param] += c - '0';
539                                 return;
540                         }
541                 }
542                 tcp->num_param = tcp->last_param + 1;
543                 switch (c) {
544
545                 case ';':
546                         if (tcp->num_param < MAX_ESC_PAR)
547                                 return;
548                         break;
549
550                 case 'A':   /* set display border color */
551                         if (tcp->num_param == 1) {
552                                 scp->border=tcp->param[0] & 0xff;
553                                 if (scp == sc->cur_scp)
554                                         sc_set_border(scp, scp->border);
555                         }
556                         break;
557
558                 case 'B':   /* set bell pitch and duration */
559                         if (tcp->num_param == 2) {
560                                 scp->bell_pitch = tcp->param[0];
561                                 scp->bell_duration = 
562                                     (tcp->param[1] * hz + 99) / 100;
563                         }
564                         break;
565
566                 case 'C':   /* set cursor type & shape */
567                         i = spltty();
568                         if (!ISGRAPHSC(sc->cur_scp))
569                                 sc_remove_cursor_image(sc->cur_scp);
570                         if (tcp->num_param == 1) {
571                                 if (tcp->param[0] & 0x01)
572                                         sc->flags |= SC_BLINK_CURSOR;
573                                 else
574                                         sc->flags &= ~SC_BLINK_CURSOR;
575                                 if (tcp->param[0] & 0x02) 
576                                         sc->flags |= SC_CHAR_CURSOR;
577                                 else
578                                         sc->flags &= ~SC_CHAR_CURSOR;
579                         } else if (tcp->num_param == 2) {
580                                 sc->cursor_base = scp->font_size 
581                                                 - (tcp->param[1] & 0x1F) - 1;
582                                 sc->cursor_height = (tcp->param[1] & 0x1F) 
583                                                 - (tcp->param[0] & 0x1F) + 1;
584                         }
585                         /* 
586                          * The cursor shape is global property; 
587                          * all virtual consoles are affected. 
588                          * Update the cursor in the current console...
589                          */
590                         if (!ISGRAPHSC(sc->cur_scp)) {
591                                 sc_set_cursor_image(sc->cur_scp);
592                                 sc_draw_cursor_image(sc->cur_scp);
593                         }
594                         splx(i);
595                         break;
596
597                 case 'F':   /* set adapter foreground */
598                         if (tcp->num_param == 1) {
599                                 tcp->attr_mask &= ~FG_CHANGED;
600                                 tcp->cur_color.fg = tcp->std_color.fg
601                                                   = tcp->param[0] & 0x0f;
602                                 tcp->cur_attr = mask2attr(tcp);
603                         }
604                         break;
605
606                 case 'G':   /* set adapter background */
607                         if (tcp->num_param == 1) {
608                                 tcp->attr_mask &= ~BG_CHANGED;
609                                 tcp->cur_color.bg = tcp->std_color.bg
610                                                   = tcp->param[0] & 0x0f;
611                                 tcp->cur_attr = mask2attr(tcp);
612                         }
613                         break;
614
615                 case 'H':   /* set adapter reverse foreground */
616                         if (tcp->num_param == 1) {
617                                 tcp->rev_color.fg = tcp->param[0] & 0x0f;
618                                 tcp->cur_attr = mask2attr(tcp);
619                         }
620                         break;
621
622                 case 'I':   /* set adapter reverse background */
623                         if (tcp->num_param == 1) {
624                                 tcp->rev_color.bg = tcp->param[0] & 0x0f;
625                                 tcp->cur_attr = mask2attr(tcp);
626                         }
627                         break;
628                 }
629 #if notyet
630         } else if (tcp->esc == 4) {     /* seen ESC Q */
631                 /* to be filled */
632 #endif
633         } else if (tcp->esc == 5) {     /* seen ESC ( */
634                 switch (c) {
635                 case 'B':   /* iso-2022: desginate ASCII into G0 */
636                         break;
637                 /* other items to be filled */
638                 default:
639                         break;
640                 }
641         }
642         tcp->esc = 0;
643 }
644
645 static void
646 scterm_puts(scr_stat *scp, u_char *buf, int len)
647 {
648         term_stat *tcp;
649
650         tcp = scp->ts;
651 outloop:
652         scp->sc->write_in_progress++;
653
654         if (tcp->esc) {
655                 scterm_scan_esc(scp, tcp, *buf);
656                 buf++;
657                 len--;
658         } else {
659                 switch (*buf) {
660                 case 0x1b:
661                         tcp->esc = 1;
662                         tcp->num_param = 0;
663                         buf++;
664                         len--;
665                         break;
666                 default:
667                         sc_term_gen_print(scp, &buf, &len, tcp->cur_attr);
668                         break;
669                 }
670         }
671
672         sc_term_gen_scroll(scp, scp->sc->scr_map[0x20], tcp->cur_attr);
673
674         scp->sc->write_in_progress--;
675         if (len)
676                 goto outloop;
677 }
678
679 static int
680 scterm_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
681              int flag, struct thread *td)
682 {
683         term_stat *tcp = scp->ts;
684         vid_info_t *vi;
685
686         switch (cmd) {
687         case GIO_ATTR:          /* get current attributes */
688                 /* FIXME: */
689                 *(int*)data = (tcp->cur_attr >> 8) & 0xff;
690                 return 0;
691         case CONS_GETINFO:      /* get current (virtual) console info */
692                 vi = (vid_info_t *)data;
693                 if (vi->size != sizeof(struct vid_info))
694                         return EINVAL;
695                 vi->mv_norm.fore = tcp->std_color.fg;
696                 vi->mv_norm.back = tcp->std_color.bg;
697                 vi->mv_rev.fore = tcp->rev_color.fg;
698                 vi->mv_rev.back = tcp->rev_color.bg;
699                 /*
700                  * The other fields are filled by the upper routine. XXX
701                  */
702                 return ENOIOCTL;
703         }
704         return ENOIOCTL;
705 }
706
707 static int
708 scterm_reset(scr_stat *scp, int code)
709 {
710         /* FIXME */
711         return 0;
712 }
713
714 static void
715 scterm_default_attr(scr_stat *scp, int color, int rev_color)
716 {
717         term_stat *tcp = scp->ts;
718
719         tcp->dflt_std_color.fg = color & 0x0f;
720         tcp->dflt_std_color.bg = (color >> 4) & 0x0f;
721         tcp->dflt_rev_color.fg = rev_color & 0x0f;
722         tcp->dflt_rev_color.bg = (rev_color >> 4) & 0x0f;
723         tcp->std_color = tcp->dflt_std_color;
724         tcp->rev_color = tcp->dflt_rev_color;
725         tcp->cur_color = tcp->std_color;
726         tcp->cur_attr = mask2attr(tcp);
727 }
728
729 static void
730 scterm_clear(scr_stat *scp)
731 {
732         term_stat *tcp = scp->ts;
733
734         sc_move_cursor(scp, 0, 0);
735         sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], tcp->cur_attr);
736         mark_all(scp);
737 }
738
739 static void
740 scterm_notify(scr_stat *scp, int event)
741 {
742         switch (event) {
743         case SC_TE_NOTIFY_VTSWITCH_IN:
744                 break;
745         case SC_TE_NOTIFY_VTSWITCH_OUT:
746                 break;
747         }
748 }
749
750 static int
751 scterm_input(scr_stat *scp, int c, struct tty *tp)
752 {
753         return FALSE;
754 }
755
756 /*
757  * Calculate hardware attributes word using logical attributes mask and
758  * hardware colors
759  */
760
761 /* FIXME */
762 static int
763 mask2attr(term_stat *tcp)
764 {
765         int attr, mask = tcp->attr_mask;
766
767         if (mask & REVERSE_ATTR) {
768                 attr = ((mask & FG_CHANGED) ?
769                         tcp->cur_color.bg : tcp->rev_color.fg) |
770                         (((mask & BG_CHANGED) ?
771                         tcp->cur_color.fg : tcp->rev_color.bg) << 4);
772         } else
773                 attr = tcp->cur_color.fg | (tcp->cur_color.bg << 4);
774
775         /* XXX: underline mapping for Hercules adapter can be better */
776         if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
777                 attr ^= 0x08;
778         if (mask & BLINK_ATTR)
779                 attr ^= 0x80;
780
781         return (attr << 8);
782 }
783
784 #endif /* SC_DUMB_TERMINAL */