The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/syscons.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1992-1997 Søren Schmidt
    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
   10  *    in this position and unchanged.
   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. The name of the author may not be used to endorse or promote products
   15  *    derived from this software withough specific prior written permission
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  *
   28  * $FreeBSD: src/sys/i386/isa/syscons.c,v 1.182.2.41 1999/09/05 08:13:35 peter Exp $
   29  */
   30 
   31 #include "sc.h"
   32 #include "apm.h"
   33 #include "opt_ddb.h"
   34 #include "opt_syscons.h"
   35 
   36 #if NSC > 0
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/conf.h>
   40 #include <sys/ioctl.h>
   41 #include <sys/proc.h>
   42 #include <sys/signalvar.h>
   43 #include <sys/tty.h>
   44 #include <sys/uio.h>
   45 #include <sys/callout.h>
   46 #include <sys/kernel.h>
   47 #include <sys/syslog.h>
   48 #include <sys/errno.h>
   49 #include <sys/malloc.h>
   50 #ifdef  DEVFS
   51 #include <sys/devfsext.h>
   52 #endif
   53 
   54 #include <machine/clock.h>
   55 #include <machine/cons.h>
   56 #include <machine/console.h>
   57 #include <machine/mouse.h>
   58 #include <machine/md_var.h>
   59 #include <machine/psl.h>
   60 #include <machine/frame.h>
   61 #include <machine/pc/display.h>
   62 #include <machine/apm_bios.h>
   63 #include <machine/random.h>
   64 
   65 #include <vm/vm.h>
   66 #include <vm/vm_param.h>
   67 #include <vm/pmap.h>
   68 
   69 #include <i386/isa/isa.h>
   70 #include <i386/isa/isa_device.h>
   71 #include <i386/isa/timerreg.h>
   72 #include <i386/isa/kbdtables.h>
   73 #include <i386/isa/kbdio.h>
   74 #include <i386/isa/syscons.h>
   75 
   76 #if !defined(MAXCONS)
   77 #define MAXCONS 16
   78 #endif
   79 
   80 #if !defined(SC_MAX_HISTORY_SIZE)
   81 #define SC_MAX_HISTORY_SIZE     (1000 * MAXCONS)
   82 #endif
   83 
   84 #if !defined(SC_HISTORY_SIZE)
   85 #define SC_HISTORY_SIZE         (ROW * 4)
   86 #endif
   87 
   88 #if (SC_HISTORY_SIZE * MAXCONS) > SC_MAX_HISTORY_SIZE
   89 #undef SC_MAX_HISTORY_SIZE
   90 #define SC_MAX_HISTORY_SIZE     (SC_HISTORY_SIZE * MAXCONS)
   91 #endif
   92 
   93 #if !defined(SC_MOUSE_CHAR)
   94 #define SC_MOUSE_CHAR           (0xd0)
   95 #endif
   96 
   97 #define COLD 0
   98 #define WARM 1
   99 
  100 #define MODE_MAP_SIZE           (M_VGA_CG320 + 1)
  101 #define MODE_PARAM_SIZE         64
  102 
  103 #define MAX_BLANKTIME           (7*24*60*60)    /* 7 days!? */
  104 
  105 /* for backward compatibility */
  106 #define OLD_CONS_MOUSECTL       _IOWR('c', 10, old_mouse_info_t)
  107 
  108 typedef struct old_mouse_data {
  109     int x;
  110     int y;
  111     int buttons;
  112 } old_mouse_data_t;
  113 
  114 typedef struct old_mouse_info {
  115     int operation;
  116     union {
  117         struct old_mouse_data data;
  118         struct mouse_mode mode;
  119     } u;
  120 } old_mouse_info_t;
  121 
  122 /* XXX use sc_bcopy where video memory is concerned */
  123 #define sc_bcopy generic_bcopy
  124 extern void generic_bcopy(const void *, void *, size_t);
  125 
  126 static default_attr user_default = {
  127     (FG_LIGHTGREY | BG_BLACK) << 8,
  128     (FG_BLACK | BG_LIGHTGREY) << 8
  129 };
  130 
  131 static default_attr kernel_default = {
  132     (FG_WHITE | BG_BLACK) << 8,
  133     (FG_BLACK | BG_LIGHTGREY) << 8
  134 };
  135 
  136 static  scr_stat        main_console;
  137 static  scr_stat        *console[MAXCONS];
  138 #ifdef DEVFS
  139 static  void            *sc_devfs_token[MAXCONS];
  140 #endif
  141         scr_stat        *cur_console;
  142 static  scr_stat        *new_scp, *old_scp;
  143 static  term_stat       kernel_console;
  144 static  default_attr    *current_default;
  145 static  int             flags = 0;
  146 static  int             sc_port = IO_KBD;
  147 static  KBDC            sc_kbdc = NULL;
  148 static  char            init_done = COLD;
  149 static  u_short         sc_buffer[ROW*COL];
  150 static  char            shutdown_in_progress = FALSE;
  151 static  char            font_loading_in_progress = FALSE;
  152 static  char            switch_in_progress = FALSE;
  153 static  char            write_in_progress = FALSE;
  154 static  char            blink_in_progress = FALSE;
  155 static  int             blinkrate = 0;
  156         u_int           crtc_addr = MONO_BASE;
  157         char            crtc_type = KD_MONO;
  158         char            crtc_vga = FALSE;
  159 static  u_char          shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
  160 static  u_char          accents = 0;
  161 static  u_char          nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
  162 static  const u_int     n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
  163 static  int             delayed_next_scr = FALSE;
  164 static  long            scrn_blank_time = 0;    /* screen saver timeout value */
  165         int             scrn_blanked = 0;       /* screen saver active flag */
  166 static  long            scrn_time_stamp;
  167         u_char          scr_map[256];
  168         u_char          scr_rmap[256];
  169         char            *video_mode_ptr = NULL;
  170         int             fonts_loaded = 0;
  171         char            font_8[256*8];
  172         char            font_14[256*14];
  173         char            font_16[256*16];
  174         char            palette[256*3];
  175 static  char            *mode_map[MODE_MAP_SIZE];
  176 static  char            vgaregs[MODE_PARAM_SIZE];
  177 static  char            vgaregs2[MODE_PARAM_SIZE];
  178 static  int             rows_offset = 1;
  179 static  char            *cut_buffer;
  180 static  int             cut_buffer_size;
  181 static  int             mouse_level = 0;        /* sysmouse protocol level */
  182 static  mousestatus_t   mouse_status = { 0, 0, 0, 0, 0, 0 };
  183 static  u_short         mouse_and_mask[16] = {
  184                                 0xc000, 0xe000, 0xf000, 0xf800,
  185                                 0xfc00, 0xfe00, 0xff00, 0xff80,
  186                                 0xfe00, 0x1e00, 0x1f00, 0x0f00,
  187                                 0x0f00, 0x0000, 0x0000, 0x0000
  188                         };
  189 static  u_short         mouse_or_mask[16] = {
  190                                 0x0000, 0x4000, 0x6000, 0x7000,
  191                                 0x7800, 0x7c00, 0x7e00, 0x6800,
  192                                 0x0c00, 0x0c00, 0x0600, 0x0600,
  193                                 0x0000, 0x0000, 0x0000, 0x0000
  194                         };
  195 
  196 static int              extra_history_size = 
  197                             SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE * MAXCONS;
  198 
  199 static void             none_saver(int blank) { }
  200 static void             (*current_saver)(int blank) = none_saver;
  201 int                     (*sc_user_ioctl)(dev_t dev, int cmd, caddr_t data,
  202                                          int flag, struct proc *p) = NULL;
  203 
  204 /* OS specific stuff */
  205 #ifdef not_yet_done
  206 #define VIRTUAL_TTY(x)  (sccons[x] = ttymalloc(sccons[x]))
  207 struct  CONSOLE_TTY     (sccons[MAXCONS] = ttymalloc(sccons[MAXCONS]))
  208 struct  MOUSE_TTY       (sccons[MAXCONS+1] = ttymalloc(sccons[MAXCONS+1]))
  209 struct  tty             *sccons[MAXCONS+2];
  210 #else
  211 #define VIRTUAL_TTY(x)  &sccons[x]
  212 #define CONSOLE_TTY     &sccons[MAXCONS]
  213 #define MOUSE_TTY       &sccons[MAXCONS+1]
  214 static struct tty       sccons[MAXCONS+2];
  215 #endif
  216 #define SC_MOUSE        128
  217 #define SC_CONSOLE      255
  218 #define MONO_BUF        pa_to_va(0xB0000)
  219 #define CGA_BUF         pa_to_va(0xB8000)
  220 u_short                 *Crtat;
  221 static const int        nsccons = MAXCONS+2;
  222 
  223 #define WRAPHIST(scp, pointer, offset)\
  224     ((scp->history) + ((((pointer) - (scp->history)) + (scp->history_size)\
  225     + (offset)) % (scp->history_size)))
  226 #define ISSIGVALID(sig) ((sig) > 0 && (sig) < NSIG)
  227 
  228 /* this should really be in `rtc.h' */
  229 #define RTC_EQUIPMENT           0x14
  230 
  231 /* prototypes */
  232 static int scattach(struct isa_device *dev);
  233 static int scparam(struct tty *tp, struct termios *t);
  234 static int scprobe(struct isa_device *dev);
  235 static int scvidprobe(int unit, int flags);
  236 static int sckbdprobe(int unit, int flags);
  237 static void scstart(struct tty *tp);
  238 static void scmousestart(struct tty *tp);
  239 static void scinit(void);
  240 static void scshutdown(int howto, void *arg);
  241 static void map_mode_table(char *map[], char *table, int max);
  242 static u_char map_mode_num(u_char mode);
  243 static char *get_mode_param(scr_stat *scp, u_char mode);
  244 static u_int scgetc(u_int flags);
  245 #define SCGETC_CN       1
  246 #define SCGETC_NONBLOCK 2
  247 static void sccnupdate(scr_stat *scp);
  248 static scr_stat *get_scr_stat(dev_t dev);
  249 static scr_stat *alloc_scp(void);
  250 static void init_scp(scr_stat *scp);
  251 static int get_scr_num(void);
  252 static timeout_t scrn_timer;
  253 static void scrn_update(scr_stat *scp, int show_cursor);
  254 static void stop_scrn_saver(void (*saver)(int));
  255 static int wait_scrn_saver_stop(void);
  256 static void clear_screen(scr_stat *scp);
  257 static int switch_scr(scr_stat *scp, u_int next_scr);
  258 static void exchange_scr(void);
  259 static __inline void move_crsr(scr_stat *scp, int x, int y);
  260 static void scan_esc(scr_stat *scp, u_char c);
  261 static void draw_cursor_image(scr_stat *scp); 
  262 static void remove_cursor_image(scr_stat *scp); 
  263 static void ansi_put(scr_stat *scp, u_char *buf, int len);
  264 static u_char *get_fstr(u_int c, u_int *len);
  265 static void history_to_screen(scr_stat *scp);
  266 static int history_up_line(scr_stat *scp);
  267 static int history_down_line(scr_stat *scp);
  268 static int mask2attr(struct term_stat *term);
  269 static void set_keyboard(int command, int data);
  270 static void update_leds(int which);
  271 static void set_vgaregs(char *modetable);
  272 static void read_vgaregs(char *buf);
  273 #define COMP_IDENTICAL  0
  274 #define COMP_SIMILAR    1
  275 #define COMP_DIFFERENT  2
  276 static int comp_vgaregs(u_char *buf1, u_char *buf2);
  277 static void dump_vgaregs(u_char *buf);
  278 #define PARAM_BUFSIZE   6
  279 static void set_font_mode(u_char *buf);
  280 static void set_normal_mode(u_char *buf);
  281 static void set_destructive_cursor(scr_stat *scp);
  282 static void set_mouse_pos(scr_stat *scp);
  283 static int skip_spc_right(scr_stat *scp, u_short *p);
  284 static int skip_spc_left(scr_stat *scp, u_short *p);
  285 static void mouse_cut(scr_stat *scp);
  286 static void mouse_cut_start(scr_stat *scp);
  287 static void mouse_cut_end(scr_stat *scp);
  288 static void mouse_cut_word(scr_stat *scp);
  289 static void mouse_cut_line(scr_stat *scp);
  290 static void mouse_cut_extend(scr_stat *scp);
  291 static void mouse_paste(scr_stat *scp);
  292 static void draw_mouse_image(scr_stat *scp); 
  293 static void remove_mouse_image(scr_stat *scp); 
  294 static void draw_cutmarking(scr_stat *scp); 
  295 static void remove_cutmarking(scr_stat *scp); 
  296 static void save_palette(void);
  297 static void do_bell(scr_stat *scp, int pitch, int duration);
  298 static timeout_t blink_screen;
  299 #ifdef SC_SPLASH_SCREEN
  300 static void toggle_splash_screen(scr_stat *scp);
  301 #endif
  302 
  303 struct  isa_driver scdriver = {
  304     scprobe, scattach, "sc", 1
  305 };
  306 
  307 static  d_open_t        scopen;
  308 static  d_close_t       scclose;
  309 static  d_read_t        scread;
  310 static  d_write_t       scwrite;
  311 static  d_ioctl_t       scioctl;
  312 static  d_devtotty_t    scdevtotty;
  313 static  d_mmap_t        scmmap;
  314 
  315 #define CDEV_MAJOR 12
  316 static  struct cdevsw   scdevsw = {
  317         scopen,         scclose,        scread,         scwrite,
  318         scioctl,        nullstop,       noreset,        scdevtotty,
  319         ttselect,       scmmap,         nostrategy,     "sc",   NULL,   -1 };
  320 
  321 /*
  322  * These functions need to be before calls to them so they can be inlined.
  323  */
  324 static __inline void
  325 draw_cursor_image(scr_stat *scp)
  326 {
  327     u_short cursor_image, *ptr = Crtat + (scp->cursor_pos - scp->scr_buf);
  328     u_short prev_image;
  329 
  330     /* do we have a destructive cursor ? */
  331     if (flags & CHAR_CURSOR) {
  332         prev_image = scp->cursor_saveunder;
  333         cursor_image = *ptr & 0x00ff;
  334         if (cursor_image == DEAD_CHAR) 
  335             cursor_image = prev_image & 0x00ff;
  336         cursor_image |= *(scp->cursor_pos) & 0xff00;
  337         scp->cursor_saveunder = cursor_image;
  338         /* update the cursor bitmap if the char under the cursor has changed */
  339         if (prev_image != cursor_image) 
  340             set_destructive_cursor(scp);
  341         /* modify cursor_image */
  342         if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
  343             /* 
  344              * When the mouse pointer is at the same position as the cursor,
  345              * the cursor bitmap needs to be updated even if the char under 
  346              * the cursor hasn't changed, because the mouse pionter may 
  347              * have moved by a few dots within the cursor cel.
  348              */
  349             if ((prev_image == cursor_image) 
  350                     && (cursor_image != *(scp->cursor_pos)))
  351                 set_destructive_cursor(scp);
  352             cursor_image &= 0xff00;
  353             cursor_image |= DEAD_CHAR;
  354         }
  355     }
  356     else {
  357         cursor_image = (*(ptr) & 0x00ff) | *(scp->cursor_pos) & 0xff00;
  358         scp->cursor_saveunder = cursor_image;
  359         if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
  360             if ((cursor_image & 0x7000) == 0x7000) {
  361                 cursor_image &= 0x8fff;
  362                 if(!(cursor_image & 0x0700))
  363                     cursor_image |= 0x0700;
  364             } else {
  365                 cursor_image |= 0x7000;
  366                 if ((cursor_image & 0x0700) == 0x0700)
  367                     cursor_image &= 0xf0ff;
  368             }
  369         }
  370     }
  371     *ptr = cursor_image;
  372 }
  373 
  374 static __inline void
  375 remove_cursor_image(scr_stat *scp)
  376 {
  377     *(Crtat + (scp->cursor_oldpos - scp->scr_buf)) = scp->cursor_saveunder;
  378 }
  379 
  380 static __inline void
  381 move_crsr(scr_stat *scp, int x, int y)
  382 {
  383     if (x < 0)
  384         x = 0;
  385     if (y < 0)
  386         y = 0;
  387     if (x >= scp->xsize)
  388         x = scp->xsize-1;
  389     if (y >= scp->ysize)
  390         y = scp->ysize-1;
  391     scp->xpos = x;
  392     scp->ypos = y;
  393     scp->cursor_pos = scp->scr_buf + scp->ypos * scp->xsize + scp->xpos;
  394 }
  395 
  396 static int
  397 scprobe(struct isa_device *dev)
  398 {
  399     if (!scvidprobe(dev->id_unit, dev->id_flags)) {
  400         if (bootverbose)
  401             printf("sc%d: no video adapter is found.\n", dev->id_unit);
  402         return (0);
  403     }
  404 
  405     sc_port = dev->id_iobase;
  406     if (sckbdprobe(dev->id_unit, dev->id_flags))
  407         return (IO_KBDSIZE);
  408     else
  409         return ((dev->id_flags & DETECT_KBD) ? 0 : IO_KBDSIZE);
  410 }
  411 
  412 /* probe video adapters, return TRUE if found */ 
  413 static int
  414 scvidprobe(int unit, int flags)
  415 {
  416     /* 
  417      * XXX don't try to `printf' anything here, the console may not have 
  418      * been configured yet. 
  419      */
  420     u_short volatile *cp;
  421     u_short was;
  422     u_long  pa;
  423     u_long  segoff;
  424 
  425     /* do this test only once */
  426     if (init_done != COLD)
  427         return (Crtat != 0);
  428 
  429     /*
  430      * Finish defaulting crtc variables for a mono screen.  Crtat is a
  431      * bogus common variable so that it can be shared with pcvt, so it
  432      * can't be statically initialized.  XXX.
  433      */
  434     Crtat = (u_short *)MONO_BUF;
  435     crtc_type = KD_MONO;
  436     /* If CGA memory seems to work, switch to color.  */
  437     cp = (u_short *)CGA_BUF;
  438     was = *cp;
  439     *cp = (u_short) 0xA55A;
  440     if (*cp == 0xA55A) {
  441         Crtat = (u_short *)CGA_BUF;
  442         crtc_addr = COLOR_BASE;
  443         crtc_type = KD_CGA;
  444     } else {
  445         cp = Crtat;
  446         was = *cp;
  447         *cp = (u_short) 0xA55A;
  448         if (*cp != 0xA55A) {
  449             /* no screen at all, bail out */
  450             Crtat = 0;
  451             return FALSE;
  452         }
  453     }
  454     *cp = was;
  455 
  456     /* 
  457      * Check rtc and BIOS date area.
  458      * XXX: don't use BIOSDATA_EQUIPMENT, it is not a dead copy
  459      * of RTC_EQUIPMENT. The bit 4 and 5 of the ETC_EQUIPMENT are
  460      * zeros for EGA and VGA. However, the EGA/VGA BIOS will set 
  461      * these bits in BIOSDATA_EQUIPMENT according to the monitor
  462      * type detected.
  463      */
  464     switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) {  /* bit 4 and 5 */
  465     case 0: /* EGA/VGA, or nothing */
  466         crtc_type = KD_EGA;
  467         /* the color adapter may be in the 40x25 mode... XXX */
  468         break;
  469     case 1: /* CGA 40x25 */
  470         /* switch to the 80x25 mode? XXX */
  471         /* FALL THROUGH */
  472     case 2: /* CGA 80x25 */
  473         /* `crtc_type' has already been set... */
  474         /* crtc_type = KD_CGA; */
  475         break;
  476     case 3: /* MDA */
  477         /* `crtc_type' has already been set... */
  478         /* crtc_type = KD_MONO; */
  479         break;
  480     }
  481 
  482     /* is this a VGA or higher ? */
  483     outb(crtc_addr, 7);
  484     if (inb(crtc_addr) == 7) {
  485 
  486         crtc_type = KD_VGA;
  487         crtc_vga = TRUE;
  488         read_vgaregs(vgaregs);
  489 
  490         /* Get the BIOS video mode pointer */
  491         segoff = *(u_long *)pa_to_va(0x4a8);
  492         pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
  493         if (ISMAPPED(pa, sizeof(u_long))) {
  494             segoff = *(u_long *)pa_to_va(pa);
  495             pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
  496             if (ISMAPPED(pa, MODE_PARAM_SIZE))
  497                 video_mode_ptr = (char *)pa_to_va(pa);
  498         }
  499     }
  500 
  501     return TRUE;
  502 }
  503 
  504 /* probe the keyboard, return TRUE if found */
  505 static int
  506 sckbdprobe(int unit, int flags)
  507 {
  508     int codeset;
  509     int c = -1;
  510     int m;
  511 
  512     sc_kbdc = kbdc_open(sc_port);
  513 
  514     if (!kbdc_lock(sc_kbdc, TRUE)) {
  515         /* driver error? */
  516         printf("sc%d: unable to lock the controller.\n", unit);
  517         return ((flags & DETECT_KBD) ? FALSE : TRUE);
  518     }
  519 
  520     /* discard anything left after UserConfig */
  521     empty_both_buffers(sc_kbdc, 10);
  522 
  523     /* save the current keyboard controller command byte */
  524     m = kbdc_get_device_mask(sc_kbdc) & ~KBD_KBD_CONTROL_BITS;
  525     c = get_controller_command_byte(sc_kbdc);
  526     if (c == -1) {
  527         /* CONTROLLER ERROR */
  528         printf("sc%d: unable to get the current command byte value.\n", unit);
  529         goto fail;
  530     }
  531     if (bootverbose)
  532         printf("sc%d: the current keyboard controller command byte %04x\n",
  533             unit, c);
  534 #if 0
  535     /* override the keyboard lock switch */
  536     c |= KBD_OVERRIDE_KBD_LOCK;
  537 #endif
  538 
  539     /* 
  540      * The keyboard may have been screwed up by the boot block.
  541      * We may just be able to recover from error by testing the controller
  542      * and the keyboard port. The controller command byte needs to be saved
  543      * before this recovery operation, as some controllers seem to set 
  544      * the command byte to particular values.
  545      */
  546     test_controller(sc_kbdc);
  547     test_kbd_port(sc_kbdc);
  548 
  549     /* enable the keyboard port, but disable the keyboard intr. */
  550     if (!set_controller_command_byte(sc_kbdc,
  551             KBD_KBD_CONTROL_BITS,
  552             KBD_ENABLE_KBD_PORT | KBD_DISABLE_KBD_INT)) {
  553         /* CONTROLLER ERROR 
  554          * there is very little we can do...
  555          */
  556         printf("sc%d: unable to set the command byte.\n", unit);
  557         goto fail;
  558      }
  559 
  560      /* 
  561       * Check if we have an XT keyboard before we attempt to reset it. 
  562       * The procedure assumes that the keyboard and the controller have 
  563       * been set up properly by BIOS and have not been messed up 
  564       * during the boot process.
  565       */
  566      codeset = -1;
  567      if (flags & XT_KEYBD)
  568          /* the user says there is a XT keyboard */
  569          codeset = 1;
  570 #ifdef DETECT_XT_KEYBOARD
  571      else if ((c & KBD_TRANSLATION) == 0) {
  572          /* SET_SCANCODE_SET is not always supported; ignore error */
  573          if (send_kbd_command_and_data(sc_kbdc, KBDC_SET_SCANCODE_SET, 0)
  574                  == KBD_ACK) 
  575              codeset = read_kbd_data(sc_kbdc);
  576      }
  577      if (bootverbose)
  578          printf("sc%d: keyboard scancode set %d\n", unit, codeset);
  579 #endif /* DETECT_XT_KEYBOARD */
  580  
  581     if (flags & KBD_NORESET) {
  582         write_kbd_command(sc_kbdc, KBDC_ECHO);
  583         if (read_kbd_data(sc_kbdc) != KBD_ECHO) {
  584             empty_both_buffers(sc_kbdc, 10);
  585             test_controller(sc_kbdc);
  586             test_kbd_port(sc_kbdc);
  587             if (bootverbose)
  588                 printf("sc%d: failed to get response from the keyboard.\n", 
  589                     unit);
  590             goto fail;
  591         }
  592     } else {
  593         /* reset keyboard hardware */
  594         if (!reset_kbd(sc_kbdc)) {
  595             /* KEYBOARD ERROR
  596              * Keyboard reset may fail either because the keyboard doen't
  597              * exist, or because the keyboard doesn't pass the self-test,
  598              * or the keyboard controller on the motherboard and the keyboard
  599              * somehow fail to shake hands. It is just possible, particularly
  600              * in the last case, that the keyoard controller may be left 
  601              * in a hung state. test_controller() and test_kbd_port() appear
  602              * to bring the keyboard controller back (I don't know why and
  603              * how, though.)
  604              */
  605             empty_both_buffers(sc_kbdc, 10);
  606             test_controller(sc_kbdc);
  607             test_kbd_port(sc_kbdc);
  608             /* We could disable the keyboard port and interrupt... but, 
  609              * the keyboard may still exist (see above). 
  610              */
  611             if (bootverbose)
  612                 printf("sc%d: failed to reset the keyboard.\n", unit);
  613             goto fail;
  614         }
  615     }
  616 
  617     /*
  618      * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
  619      * such as those on the IBM ThinkPad laptop computers can be used
  620      * with the standard console driver.
  621      */
  622     if (codeset == 1) {
  623         if (send_kbd_command_and_data(
  624                 sc_kbdc, KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
  625             /* XT kbd doesn't need scan code translation */
  626             c &= ~KBD_TRANSLATION;
  627         } else {
  628             /* KEYBOARD ERROR 
  629              * The XT kbd isn't usable unless the proper scan code set
  630              * is selected. 
  631              */
  632             printf("sc%d: unable to set the XT keyboard mode.\n", unit);
  633             goto fail;
  634         }
  635     }
  636     /* enable the keyboard port and intr. */
  637     if (!set_controller_command_byte(sc_kbdc, 
  638             KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
  639             (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
  640                 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
  641         /* CONTROLLER ERROR 
  642          * This is serious; we are left with the disabled keyboard intr. 
  643          */
  644         printf("sc%d: unable to enable the keyboard port and intr.\n", unit);
  645         goto fail;
  646     }
  647 
  648     kbdc_set_device_mask(sc_kbdc, m | KBD_KBD_CONTROL_BITS),
  649     kbdc_lock(sc_kbdc, FALSE);
  650     return TRUE;
  651 
  652 fail:
  653     if (c != -1)
  654         /* try to restore the command byte as before, if possible */
  655         set_controller_command_byte(sc_kbdc, 0xff, c);
  656     kbdc_set_device_mask(sc_kbdc, 
  657         (flags & DETECT_KBD) ? m : m | KBD_KBD_CONTROL_BITS);
  658     kbdc_lock(sc_kbdc, FALSE);
  659     return FALSE;
  660 }
  661 
  662 #if NAPM > 0
  663 static int
  664 scresume(void *dummy)
  665 {
  666         shfts = ctls = alts = agrs = metas = accents = 0; 
  667         return 0;
  668 }
  669 #endif
  670 
  671 static int
  672 scattach(struct isa_device *dev)
  673 {
  674     scr_stat *scp;
  675     dev_t cdev = makedev(CDEV_MAJOR, 0);
  676     char *p;
  677 #ifdef DEVFS
  678     int vc;
  679 #endif
  680 
  681     scinit();
  682     flags = dev->id_flags;
  683     if (!crtc_vga)
  684         flags &= ~CHAR_CURSOR;
  685 
  686     scp = console[0];
  687 
  688     if (crtc_vga) {
  689         cut_buffer_size = scp->xsize * scp->ysize + 1;
  690         cut_buffer = (char *)malloc(cut_buffer_size, M_DEVBUF, M_NOWAIT);
  691         if (cut_buffer != NULL)
  692             cut_buffer[0] = '\0';
  693     }
  694 
  695     scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
  696                                      M_DEVBUF, M_NOWAIT);
  697 
  698     /* copy temporary buffer to final buffer */
  699     bcopy(sc_buffer, scp->scr_buf, scp->xsize * scp->ysize * sizeof(u_short));
  700 
  701     scp->cursor_pos = scp->cursor_oldpos =
  702         scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
  703     scp->mouse_pos = scp->mouse_oldpos = 
  704         scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
  705                         scp->mouse_xpos/8);
  706 
  707     /* initialize history buffer & pointers */
  708     scp->history_head = scp->history_pos = 
  709         (u_short *)malloc(scp->history_size*sizeof(u_short),
  710                           M_DEVBUF, M_NOWAIT);
  711     if (scp->history_head != NULL)
  712         bzero(scp->history_head, scp->history_size*sizeof(u_short));
  713     scp->history = scp->history_head;
  714 
  715     /* initialize cursor stuff */
  716     if (!(scp->status & UNKNOWN_MODE))
  717         draw_cursor_image(scp);
  718 
  719     /* get screen update going */
  720     scrn_timer(NULL);
  721 
  722     update_leds(scp->status);
  723 
  724     if ((crtc_type == KD_VGA) && bootverbose) {
  725         printf("sc%d: BIOS video mode:%d\n", 
  726             dev->id_unit, *(u_char *)pa_to_va(0x449));
  727         printf("sc%d: VGA registers upon power-up\n", dev->id_unit);
  728         dump_vgaregs(vgaregs);
  729         printf("sc%d: video mode:%d\n", dev->id_unit, scp->mode);
  730         printf("sc%d: VGA registers in BIOS for mode:%d\n", 
  731                 dev->id_unit, scp->mode);
  732         dump_vgaregs(vgaregs2);
  733         p = get_mode_param(scp, scp->mode);
  734         if (p != NULL) {
  735             printf("sc%d: VGA registers to be used for mode:%d\n", 
  736                 dev->id_unit, scp->mode);
  737             dump_vgaregs(p);
  738         }
  739         printf("sc%d: rows_offset:%d\n", dev->id_unit, rows_offset);
  740     }
  741     if ((crtc_type == KD_VGA) && (video_mode_ptr == NULL))
  742         printf("sc%d: WARNING: video mode switching is only partially supported\n",
  743                 dev->id_unit); 
  744 
  745     printf("sc%d: ", dev->id_unit);
  746     switch(crtc_type) {
  747     case KD_VGA:
  748         if (crtc_addr == MONO_BASE)
  749             printf("VGA mono");
  750         else
  751             printf("VGA color");
  752         break;
  753     case KD_EGA:
  754         if (crtc_addr == MONO_BASE)
  755             printf("EGA mono");
  756         else
  757             printf("EGA color");
  758         break;
  759     case KD_CGA:
  760         printf("CGA");
  761         break;
  762     case KD_MONO:
  763     case KD_HERCULES:
  764     default:
  765         printf("MDA/hercules");
  766         break;
  767     }
  768     printf(" <%d virtual consoles, flags=0x%x>\n", MAXCONS, flags);
  769 
  770 #if NAPM > 0
  771     scp->r_hook.ah_fun = scresume;
  772     scp->r_hook.ah_arg = NULL;
  773     scp->r_hook.ah_name = "system keyboard";
  774     scp->r_hook.ah_order = APM_MID_ORDER;
  775     apm_hook_establish(APM_HOOK_RESUME , &scp->r_hook);
  776 #endif
  777 
  778     at_shutdown(scshutdown, NULL, SHUTDOWN_PRE_SYNC);
  779 
  780     cdevsw_add(&cdev, &scdevsw, NULL);
  781 
  782 #ifdef DEVFS
  783     for (vc = 0; vc < MAXCONS; vc++)
  784         sc_devfs_token[vc] = devfs_add_devswf(&scdevsw, vc, DV_CHR, UID_ROOT,
  785                                               GID_WHEEL, 0600, "ttyv%n", vc);
  786 #endif
  787     return 0;
  788 }
  789 
  790 struct tty
  791 *scdevtotty(dev_t dev)
  792 {
  793     int unit = minor(dev);
  794 
  795     if (init_done == COLD)
  796         return(NULL);
  797     if (unit == SC_CONSOLE)
  798         return CONSOLE_TTY;
  799     if (unit == SC_MOUSE)
  800         return MOUSE_TTY;
  801     if (unit >= MAXCONS || unit < 0)
  802         return(NULL);
  803     return VIRTUAL_TTY(unit);
  804 }
  805 
  806 int
  807 scopen(dev_t dev, int flag, int mode, struct proc *p)
  808 {
  809     struct tty *tp = scdevtotty(dev);
  810 
  811     if (!tp)
  812         return(ENXIO);
  813 
  814     tp->t_oproc = (minor(dev) == SC_MOUSE) ? scmousestart : scstart;
  815     tp->t_param = scparam;
  816     tp->t_dev = dev;
  817     if (!(tp->t_state & TS_ISOPEN)) {
  818         ttychars(tp);
  819         /* Use the current setting of the <-- key as default VERASE. */  
  820         /* If the Delete key is preferable, an stty is necessary     */
  821         tp->t_cc[VERASE] = key_map.key[0x0e].map[0];
  822         tp->t_iflag = TTYDEF_IFLAG;
  823         tp->t_oflag = TTYDEF_OFLAG;
  824         tp->t_cflag = TTYDEF_CFLAG;
  825         tp->t_lflag = TTYDEF_LFLAG;
  826         tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
  827         scparam(tp, &tp->t_termios);
  828         ttsetwater(tp);
  829         (*linesw[tp->t_line].l_modem)(tp, 1);
  830         if (minor(dev) == SC_MOUSE)
  831             mouse_level = 0;            /* XXX */
  832     }
  833     else
  834         if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
  835             return(EBUSY);
  836     if (minor(dev) < MAXCONS && !console[minor(dev)]) {
  837         console[minor(dev)] = alloc_scp();
  838     }
  839     if (minor(dev)<MAXCONS && !tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
  840         tp->t_winsize.ws_col = console[minor(dev)]->xsize;
  841         tp->t_winsize.ws_row = console[minor(dev)]->ysize;
  842     }
  843     return ((*linesw[tp->t_line].l_open)(dev, tp));
  844 }
  845 
  846 int
  847 scclose(dev_t dev, int flag, int mode, struct proc *p)
  848 {
  849     struct tty *tp = scdevtotty(dev);
  850     struct scr_stat *scp;
  851 
  852     if (!tp)
  853         return(ENXIO);
  854     if (minor(dev) < MAXCONS) {
  855         scp = get_scr_stat(tp->t_dev);
  856         if (scp->status & SWITCH_WAIT_ACQ)
  857             wakeup((caddr_t)&scp->smode);
  858 #if not_yet_done
  859         if (scp == &main_console) {
  860             scp->pid = 0;
  861             scp->proc = NULL;
  862             scp->smode.mode = VT_AUTO;
  863         }
  864         else {
  865             free(scp->scr_buf, M_DEVBUF);
  866             if (scp->history != NULL) {
  867                 free(scp->history, M_DEVBUF);
  868                 if (scp->history_size / scp->xsize
  869                         > imax(SC_HISTORY_SIZE, scp->ysize))
  870                     extra_history_size += scp->history_size / scp->xsize 
  871                         - imax(SC_HISTORY_SIZE, scp->ysize);
  872             }
  873             free(scp, M_DEVBUF);
  874             console[minor(dev)] = NULL;
  875         }
  876 #else
  877         scp->pid = 0;
  878         scp->proc = NULL;
  879         scp->smode.mode = VT_AUTO;
  880 #endif
  881     }
  882     spltty();
  883     (*linesw[tp->t_line].l_close)(tp, flag);
  884     ttyclose(tp);
  885     spl0();
  886     return(0);
  887 }
  888 
  889 int
  890 scread(dev_t dev, struct uio *uio, int flag)
  891 {
  892     struct tty *tp = scdevtotty(dev);
  893 
  894     if (!tp)
  895         return(ENXIO);
  896     return((*linesw[tp->t_line].l_read)(tp, uio, flag));
  897 }
  898 
  899 int
  900 scwrite(dev_t dev, struct uio *uio, int flag)
  901 {
  902     struct tty *tp = scdevtotty(dev);
  903 
  904     if (!tp)
  905         return(ENXIO);
  906     return((*linesw[tp->t_line].l_write)(tp, uio, flag));
  907 }
  908 
  909 void
  910 scintr(int unit)
  911 {
  912     static struct tty *cur_tty;
  913     int c, len;
  914     u_char *cp;
  915 
  916     /* 
  917      * Loop while there is still input to get from the keyboard.
  918      * I don't think this is nessesary, and it doesn't fix
  919      * the Xaccel-2.1 keyboard hang, but it can't hurt.         XXX
  920      */
  921     while ((c = scgetc(SCGETC_NONBLOCK)) != NOKEY) {
  922 
  923         cur_tty = VIRTUAL_TTY(get_scr_num());
  924         if (!(cur_tty->t_state & TS_ISOPEN))
  925             if (!((cur_tty = CONSOLE_TTY)->t_state & TS_ISOPEN))
  926                 continue;
  927 
  928         switch (c & 0xff00) {
  929         case 0x0000: /* normal key */
  930             (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
  931             break;
  932         case FKEY:  /* function key, return string */
  933             if (cp = get_fstr((u_int)c, (u_int *)&len)) {
  934                 while (len-- >  0)
  935                     (*linesw[cur_tty->t_line].l_rint)(*cp++ & 0xFF, cur_tty);
  936             }
  937             break;
  938         case MKEY:  /* meta is active, prepend ESC */
  939             (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
  940             (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
  941             break;
  942         case BKEY:  /* backtab fixed sequence (esc [ Z) */
  943             (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
  944             (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
  945             (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
  946             break;
  947         }
  948     }
  949 
  950     if (cur_console->status & MOUSE_ENABLED) {
  951         cur_console->status &= ~MOUSE_VISIBLE;
  952         remove_mouse_image(cur_console);
  953     }
  954 }
  955 
  956 static int
  957 scparam(struct tty *tp, struct termios *t)
  958 {
  959     tp->t_ispeed = t->c_ispeed;
  960     tp->t_ospeed = t->c_ospeed;
  961     tp->t_cflag = t->c_cflag;
  962     return 0;
  963 }
  964 
  965 int
  966 scioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
  967 {
  968     int error;
  969     u_int i;
  970     struct tty *tp;
  971     struct trapframe *fp;
  972     scr_stat *scp;
  973     u_short *usp;
  974     char *mp;
  975     int s;
  976 
  977     tp = scdevtotty(dev);
  978     if (!tp)
  979         return ENXIO;
  980     scp = get_scr_stat(tp->t_dev);
  981 
  982     /* If there is a user_ioctl function call that first */
  983     if (sc_user_ioctl) {
  984         if (error = (*sc_user_ioctl)(dev, cmd, data, flag, p))
  985             return error;
  986     }
  987 
  988     switch (cmd) {              /* process console hardware related ioctl's */
  989 
  990     case GIO_ATTR:              /* get current attributes */
  991         *(int*)data = (scp->term.cur_attr >> 8) & 0xFF;
  992         return 0;
  993 
  994     case GIO_COLOR:             /* is this a color console ? */
  995         if (crtc_addr == COLOR_BASE)
  996             *(int*)data = 1;
  997         else
  998             *(int*)data = 0;
  999         return 0;
 1000 
 1001     case CONS_CURRENT:          /* get current adapter type */
 1002         *(int *)data = crtc_type;
 1003         return 0;
 1004 
 1005     case CONS_GET:              /* get current video mode */
 1006         *(int*)data = scp->mode;
 1007         return 0;
 1008 
 1009     case CONS_BLANKTIME:        /* set screen saver timeout (0 = no saver) */
 1010         if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
 1011             return EINVAL;
 1012         scrn_blank_time = *(int *)data;
 1013         if (scrn_blank_time == 0)
 1014             scrn_time_stamp = mono_time.tv_sec;
 1015         return 0;
 1016 
 1017     case CONS_CURSORTYPE:       /* set cursor type blink/noblink */
 1018         if ((*(int*)data) & 0x01)
 1019             flags |= BLINK_CURSOR;
 1020         else
 1021             flags &= ~BLINK_CURSOR;
 1022         if ((*(int*)data) & 0x02) {
 1023             if (!crtc_vga)
 1024                 return ENXIO;
 1025             flags |= CHAR_CURSOR;
 1026         } else
 1027             flags &= ~CHAR_CURSOR;
 1028         /* 
 1029          * The cursor shape is global property; all virtual consoles
 1030          * are affected. Update the cursor in the current console...
 1031          */
 1032         if (!(cur_console->status & UNKNOWN_MODE)) {
 1033             s = spltty();
 1034             remove_cursor_image(cur_console);
 1035             if (flags & CHAR_CURSOR)
 1036                 set_destructive_cursor(cur_console);
 1037             draw_cursor_image(cur_console);
 1038             splx(s);
 1039         }
 1040         return 0;
 1041 
 1042     case CONS_BELLTYPE:         /* set bell type sound/visual */
 1043         if ((*(int *)data) & 0x01)
 1044             flags |= VISUAL_BELL;
 1045         else
 1046             flags &= ~VISUAL_BELL;
 1047         if ((*(int *)data) & 0x02)
 1048             flags |= QUIET_BELL;
 1049         else
 1050             flags &= ~QUIET_BELL;
 1051         return 0;
 1052 
 1053     case CONS_HISTORY:          /* set history size */
 1054         if (*(int *)data > 0) {
 1055             int lines;  /* buffer size to allocate */
 1056             int lines0; /* current buffer size */
 1057 
 1058             lines = imax(*(int *)data, scp->ysize);
 1059             lines0 = (scp->history != NULL) ? 
 1060                       scp->history_size / scp->xsize : scp->ysize;
 1061             /*
 1062              * syscons unconditionally allocates buffers upto SC_HISTORY_SIZE
 1063              * lines or scp->ysize lines, whichever is larger. A value 
 1064              * greater than that is allowed, subject to extra_history_size.
 1065              */
 1066             if (lines > imax(lines0, SC_HISTORY_SIZE) + extra_history_size)
 1067                 return EINVAL;
 1068             if (cur_console->status & BUFFER_SAVED)
 1069                 return EBUSY;
 1070             usp = scp->history;
 1071             scp->history = NULL;
 1072             if (usp != NULL)
 1073                 free(usp, M_DEVBUF);
 1074             scp->history_size = lines * scp->xsize;
 1075             /*
 1076              * extra_history_size += 
 1077              *    (lines0 > imax(SC_HISTORY_SIZE, scp->ysize)) ? 
 1078              *     lines0 - imax(SC_HISTORY_SIZE, scp->ysize)) : 0;
 1079              * extra_history_size -= 
 1080              *    (lines > imax(SC_HISTORY_SIZE, scp->ysize)) ? 
 1081              *     lines - imax(SC_HISTORY_SIZE, scp->ysize)) : 0;
 1082              * lines0 >= ysize && lines >= ysize... Hey, the above can be 
 1083              * reduced to the following...
 1084              */
 1085             extra_history_size += 
 1086                 imax(lines0, SC_HISTORY_SIZE) - imax(lines, SC_HISTORY_SIZE);
 1087             usp = (u_short *)malloc(scp->history_size * sizeof(u_short),
 1088                                     M_DEVBUF, M_WAITOK);
 1089             bzero(usp, scp->history_size * sizeof(u_short));
 1090             scp->history_head = scp->history_pos = usp;
 1091             scp->history = usp;
 1092             return 0;
 1093         }
 1094         else
 1095             return EINVAL;
 1096 
 1097     case CONS_MOUSECTL:         /* control mouse arrow */
 1098     case OLD_CONS_MOUSECTL:
 1099     {
 1100         /* MOUSE_BUTTON?DOWN -> MOUSE_MSC_BUTTON?UP */
 1101         static butmap[8] = {
 1102             MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP 
 1103                 | MOUSE_MSC_BUTTON3UP,
 1104             MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
 1105             MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
 1106             MOUSE_MSC_BUTTON3UP,
 1107             MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
 1108             MOUSE_MSC_BUTTON2UP,
 1109             MOUSE_MSC_BUTTON1UP,
 1110             0,
 1111         };
 1112         mouse_info_t *mouse = (mouse_info_t*)data;
 1113         mouse_info_t buf;
 1114 
 1115         if (!crtc_vga)
 1116             return ENODEV;
 1117         
 1118         if (cmd == OLD_CONS_MOUSECTL) {
 1119             static unsigned char swapb[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
 1120             old_mouse_info_t *old_mouse = (old_mouse_info_t *)data;
 1121 
 1122             mouse = &buf;
 1123             mouse->operation = old_mouse->operation;
 1124             switch (mouse->operation) {
 1125             case MOUSE_MODE:
 1126                 mouse->u.mode = old_mouse->u.mode;
 1127                 break;
 1128             case MOUSE_SHOW:
 1129             case MOUSE_HIDE:
 1130                 break;
 1131             case MOUSE_MOVEABS:
 1132             case MOUSE_MOVEREL:
 1133             case MOUSE_ACTION:
 1134                 mouse->u.data.x = old_mouse->u.data.x;
 1135                 mouse->u.data.y = old_mouse->u.data.y;
 1136                 mouse->u.data.z = 0;
 1137                 mouse->u.data.buttons = swapb[old_mouse->u.data.buttons & 0x7];
 1138                 break;
 1139             case MOUSE_GETINFO:
 1140                 old_mouse->u.data.x = scp->mouse_xpos;
 1141                 old_mouse->u.data.y = scp->mouse_ypos;
 1142                 old_mouse->u.data.buttons = swapb[scp->mouse_buttons & 0x7];
 1143                 break;
 1144             default:
 1145                 return EINVAL;
 1146             }
 1147         }
 1148 
 1149         switch (mouse->operation) {
 1150         case MOUSE_MODE:
 1151             if (ISSIGVALID(mouse->u.mode.signal)) {
 1152                 scp->mouse_signal = mouse->u.mode.signal;
 1153                 scp->mouse_proc = p;
 1154                 scp->mouse_pid = p->p_pid;
 1155             }
 1156             else {
 1157                 scp->mouse_signal = 0;
 1158                 scp->mouse_proc = NULL;
 1159                 scp->mouse_pid = 0;
 1160             }
 1161             break;
 1162 
 1163         case MOUSE_SHOW:
 1164             if (!(scp->status & MOUSE_ENABLED)) {
 1165                 scp->status |= (MOUSE_ENABLED | MOUSE_VISIBLE);
 1166                 scp->mouse_oldpos = scp->mouse_pos;
 1167                 mark_all(scp);
 1168             }
 1169             else
 1170                 return EINVAL;
 1171             break;
 1172 
 1173         case MOUSE_HIDE:
 1174             if (scp->status & MOUSE_ENABLED) {
 1175                 scp->status &= ~(MOUSE_ENABLED | MOUSE_VISIBLE);
 1176                 mark_all(scp);
 1177             }
 1178             else
 1179                 return EINVAL;
 1180             break;
 1181 
 1182         case MOUSE_MOVEABS:
 1183             scp->mouse_xpos = mouse->u.data.x;
 1184             scp->mouse_ypos = mouse->u.data.y;
 1185             set_mouse_pos(scp);
 1186             break;
 1187 
 1188         case MOUSE_MOVEREL:
 1189             scp->mouse_xpos += mouse->u.data.x;
 1190             scp->mouse_ypos += mouse->u.data.y;
 1191             set_mouse_pos(scp);
 1192             break;
 1193 
 1194         case MOUSE_GETINFO:
 1195             mouse->u.data.x = scp->mouse_xpos;
 1196             mouse->u.data.y = scp->mouse_ypos;
 1197             mouse->u.data.z = 0;
 1198             mouse->u.data.buttons = scp->mouse_buttons;
 1199             break;
 1200 
 1201         case MOUSE_ACTION:
 1202         case MOUSE_MOTION_EVENT:
 1203             /* this should maybe only be settable from /dev/consolectl SOS */
 1204             /* send out mouse event on /dev/sysmouse */
 1205 
 1206             mouse_status.dx += mouse->u.data.x;
 1207             mouse_status.dy += mouse->u.data.y;
 1208             mouse_status.dz += mouse->u.data.z;
 1209             if (mouse->operation == MOUSE_ACTION)
 1210                 mouse_status.button = mouse->u.data.buttons;
 1211             mouse_status.flags |= 
 1212                 ((mouse->u.data.x || mouse->u.data.y || mouse->u.data.z) ? 
 1213                     MOUSE_POSCHANGED : 0)
 1214                 | (mouse_status.obutton ^ mouse_status.button);
 1215 
 1216             if (cur_console->status & MOUSE_ENABLED)
 1217                 cur_console->status |= MOUSE_VISIBLE;
 1218 
 1219             if ((MOUSE_TTY)->t_state & TS_ISOPEN) {
 1220                 u_char buf[MOUSE_SYS_PACKETSIZE];
 1221                 int j;
 1222 
 1223                 /* the first five bytes are compatible with MouseSystems' */
 1224                 buf[0] = MOUSE_MSC_SYNC
 1225                     | butmap[mouse_status.button & MOUSE_STDBUTTONS];
 1226                 j = imax(imin(mouse->u.data.x, 255), -256);
 1227                 buf[1] = j >> 1;
 1228                 buf[3] = j - buf[1];
 1229                 j = -imax(imin(mouse->u.data.y, 255), -256);
 1230                 buf[2] = j >> 1;
 1231                 buf[4] = j - buf[2];
 1232                 for (j = 0; j < MOUSE_MSC_PACKETSIZE; j++)
 1233                         (*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[j],MOUSE_TTY);
 1234                 if (mouse_level >= 1) {         /* extended part */
 1235                     j = imax(imin(mouse->u.data.z, 127), -128);
 1236                     buf[5] = (j >> 1) & 0x7f;
 1237                     buf[6] = (j - (j >> 1)) & 0x7f;
 1238                     /* buttons 4-10 */
 1239                     buf[7] = (~mouse_status.button >> 3) & 0x7f;
 1240                     for (j = MOUSE_MSC_PACKETSIZE; 
 1241                          j < MOUSE_SYS_PACKETSIZE; j++)
 1242                         (*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[j],MOUSE_TTY);
 1243                 }
 1244             }
 1245 
 1246             if (cur_console->mouse_signal) {
 1247                 cur_console->mouse_buttons = mouse->u.data.buttons;
 1248                 /* has controlling process died? */
 1249                 if (cur_console->mouse_proc && 
 1250                     (cur_console->mouse_proc != pfind(cur_console->mouse_pid))){
 1251                         cur_console->mouse_signal = 0;
 1252                         cur_console->mouse_proc = NULL;
 1253                         cur_console->mouse_pid = 0;
 1254                 }
 1255                 else
 1256                     psignal(cur_console->mouse_proc, cur_console->mouse_signal);
 1257             }
 1258             else if (mouse->operation == MOUSE_ACTION && cut_buffer != NULL) {
 1259                 /* process button presses */
 1260                 if ((cur_console->mouse_buttons ^ mouse->u.data.buttons) && 
 1261                     !(cur_console->status & UNKNOWN_MODE)) {
 1262                     cur_console->mouse_buttons = mouse->u.data.buttons;
 1263                     if (cur_console->mouse_buttons & MOUSE_BUTTON1DOWN)
 1264                         mouse_cut_start(cur_console);
 1265                     else
 1266                         mouse_cut_end(cur_console);
 1267                     if (cur_console->mouse_buttons & MOUSE_BUTTON2DOWN ||
 1268                         cur_console->mouse_buttons & MOUSE_BUTTON3DOWN)
 1269                         mouse_paste(cur_console);
 1270                 }
 1271             }
 1272 
 1273             if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
 1274                 cur_console->mouse_xpos += mouse->u.data.x;
 1275                 cur_console->mouse_ypos += mouse->u.data.y;
 1276                 set_mouse_pos(cur_console);
 1277             }
 1278 
 1279             break;
 1280 
 1281         case MOUSE_BUTTON_EVENT:
 1282             if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
 1283                 return EINVAL;
 1284             if (mouse->u.event.value < 0)
 1285                 return EINVAL;
 1286 
 1287             if (mouse->u.event.value > 0) {
 1288                 cur_console->mouse_buttons |= mouse->u.event.id;
 1289                 mouse_status.button |= mouse->u.event.id;
 1290             } else {
 1291                 cur_console->mouse_buttons &= ~mouse->u.event.id;
 1292                 mouse_status.button &= ~mouse->u.event.id;
 1293             }
 1294             mouse_status.flags |= mouse_status.obutton ^ mouse_status.button;
 1295 
 1296             if (cur_console->status & MOUSE_ENABLED)
 1297                 cur_console->status |= MOUSE_VISIBLE;
 1298 
 1299             if ((MOUSE_TTY)->t_state & TS_ISOPEN) {
 1300                 u_char buf[8];
 1301                 int i;
 1302 
 1303                 buf[0] = MOUSE_MSC_SYNC 
 1304                          | butmap[mouse_status.button & MOUSE_STDBUTTONS];
 1305                 buf[7] = (~mouse_status.button >> 3) & 0x7f;
 1306                 buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
 1307                 for (i = 0; 
 1308                      i < ((mouse_level >= 1) ? MOUSE_SYS_PACKETSIZE 
 1309                                              : MOUSE_MSC_PACKETSIZE); i++)
 1310                     (*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[i],MOUSE_TTY);
 1311             }
 1312 
 1313             if (cur_console->mouse_signal) {
 1314                 if (cur_console->mouse_proc && 
 1315                     (cur_console->mouse_proc != pfind(cur_console->mouse_pid))){
 1316                         cur_console->mouse_signal = 0;
 1317                         cur_console->mouse_proc = NULL;
 1318                         cur_console->mouse_pid = 0;
 1319                 }
 1320                 else
 1321                     psignal(cur_console->mouse_proc, cur_console->mouse_signal);
 1322                 break;
 1323             }
 1324 
 1325             if ((cur_console->status & UNKNOWN_MODE) || (cut_buffer == NULL))
 1326                 break;
 1327 
 1328             switch (mouse->u.event.id) {
 1329             case MOUSE_BUTTON1DOWN:
 1330                 switch (mouse->u.event.value % 4) {
 1331                 case 0: /* up */
 1332                     mouse_cut_end(cur_console);
 1333                     break;
 1334                 case 1:
 1335                     mouse_cut_start(cur_console);
 1336                     break;
 1337                 case 2:
 1338                     mouse_cut_word(cur_console);
 1339                     break;
 1340                 case 3:
 1341                     mouse_cut_line(cur_console);
 1342                     break;
 1343                 }
 1344                 break;
 1345             case MOUSE_BUTTON2DOWN:
 1346                 switch (mouse->u.event.value) {
 1347                 case 0: /* up */
 1348                     break;
 1349                 default:
 1350                     mouse_paste(cur_console);
 1351                     break;
 1352                 }
 1353                 break;
 1354             case MOUSE_BUTTON3DOWN:
 1355                 switch (mouse->u.event.value) {
 1356                 case 0: /* up */
 1357                     if (!(cur_console->mouse_buttons & MOUSE_BUTTON1DOWN))
 1358                         mouse_cut_end(cur_console);
 1359                     break;
 1360                 default:
 1361                     mouse_cut_extend(cur_console);
 1362                     break;
 1363                 }
 1364                 break;
 1365             }
 1366             break;
 1367 
 1368         default:
 1369             return EINVAL;
 1370         }
 1371         /* make screensaver happy */
 1372         scrn_time_stamp = mono_time.tv_sec;
 1373         return 0;
 1374     }
 1375 
 1376     /* MOUSE_XXX: /dev/sysmouse ioctls */
 1377     case MOUSE_GETHWINFO:       /* get device information */
 1378     {
 1379         mousehw_t *hw = (mousehw_t *)data;
 1380 
 1381         if (tp != MOUSE_TTY)
 1382             return ENOTTY;
 1383         hw->buttons = 10;               /* XXX unknown */
 1384         hw->iftype = MOUSE_IF_SYSMOUSE;
 1385         hw->type = MOUSE_MOUSE;
 1386         hw->model = MOUSE_MODEL_GENERIC;
 1387         hw->hwid = 0;
 1388         return 0;
 1389     }
 1390 
 1391     case MOUSE_GETMODE:         /* get protocol/mode */
 1392     {
 1393         mousemode_t *mode = (mousemode_t *)data;
 1394 
 1395         if (tp != MOUSE_TTY)
 1396             return ENOTTY;
 1397         mode->level = mouse_level;
 1398         switch (mode->level) {
 1399         case 0:
 1400             /* at this level, sysmouse emulates MouseSystems protocol */
 1401             mode->protocol = MOUSE_PROTO_MSC;
 1402             mode->rate = -1;            /* unknown */
 1403             mode->resolution = -1;      /* unknown */
 1404             mode->accelfactor = 0;      /* disabled */
 1405             mode->packetsize = MOUSE_MSC_PACKETSIZE;
 1406             mode->syncmask[0] = MOUSE_MSC_SYNCMASK;
 1407             mode->syncmask[1] = MOUSE_MSC_SYNC;
 1408             break;
 1409 
 1410         case 1:
 1411             /* at this level, sysmouse uses its own protocol */
 1412             mode->protocol = MOUSE_PROTO_SYSMOUSE;
 1413             mode->rate = -1;
 1414             mode->resolution = -1;
 1415             mode->accelfactor = 0;
 1416             mode->packetsize = MOUSE_SYS_PACKETSIZE;
 1417             mode->syncmask[0] = MOUSE_SYS_SYNCMASK;
 1418             mode->syncmask[1] = MOUSE_SYS_SYNC;
 1419             break;
 1420         }
 1421         return 0;
 1422     }
 1423 
 1424     case MOUSE_SETMODE:         /* set protocol/mode */
 1425     {
 1426         mousemode_t *mode = (mousemode_t *)data;
 1427 
 1428         if (tp != MOUSE_TTY)
 1429             return ENOTTY;
 1430         if ((mode->level < 0) || (mode->level > 1))
 1431             return EINVAL;
 1432         mouse_level = mode->level;
 1433         return 0;
 1434     }
 1435 
 1436     case MOUSE_GETLEVEL:        /* get operation level */
 1437         if (tp != MOUSE_TTY)
 1438             return ENOTTY;
 1439         *(int *)data = mouse_level;
 1440         return 0;
 1441 
 1442     case MOUSE_SETLEVEL:        /* set operation level */
 1443         if (tp != MOUSE_TTY)
 1444             return ENOTTY;
 1445         if ((*(int *)data  < 0) || (*(int *)data > 1))
 1446             return EINVAL;
 1447         mouse_level = *(int *)data;
 1448         return 0;
 1449 
 1450     case MOUSE_GETSTATUS:       /* get accumulated mouse events */
 1451         if (tp != MOUSE_TTY)
 1452             return ENOTTY;
 1453         s = spltty();
 1454         *(mousestatus_t *)data = mouse_status;
 1455         mouse_status.flags = 0;
 1456         mouse_status.obutton = mouse_status.button;
 1457         mouse_status.dx = 0;
 1458         mouse_status.dy = 0;
 1459         mouse_status.dz = 0;
 1460         splx(s);
 1461         return 0;
 1462 
 1463 #if notyet
 1464     case MOUSE_GETVARS:         /* get internal mouse variables */
 1465     case MOUSE_SETVARS:         /* set internal mouse variables */
 1466         if (tp != MOUSE_TTY)
 1467             return ENOTTY;
 1468         return ENODEV;
 1469 #endif
 1470 
 1471     case MOUSE_READSTATE:       /* read status from the device */
 1472     case MOUSE_READDATA:        /* read data from the device */
 1473         if (tp != MOUSE_TTY)
 1474             return ENOTTY;
 1475         return ENODEV;
 1476 
 1477     case CONS_GETINFO:          /* get current (virtual) console info */
 1478     {
 1479         vid_info_t *ptr = (vid_info_t*)data;
 1480         if (ptr->size == sizeof(struct vid_info)) {
 1481             ptr->m_num = get_scr_num();
 1482             ptr->mv_col = scp->xpos;
 1483             ptr->mv_row = scp->ypos;
 1484             ptr->mv_csz = scp->xsize;
 1485             ptr->mv_rsz = scp->ysize;
 1486             ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
 1487             ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
 1488             ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
 1489             ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
 1490             ptr->mv_grfc.fore = 0;      /* not supported */
 1491             ptr->mv_grfc.back = 0;      /* not supported */
 1492             ptr->mv_ovscan = scp->border;
 1493             ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
 1494             return 0;
 1495         }
 1496         return EINVAL;
 1497     }
 1498 
 1499     case CONS_GETVERS:          /* get version number */
 1500         *(int*)data = 0x200;    /* version 2.0 */
 1501         return 0;
 1502 
 1503     /* VGA TEXT MODES */
 1504     case SW_VGA_C40x25:
 1505     case SW_VGA_C80x25: case SW_VGA_M80x25:
 1506     case SW_VGA_C80x30: case SW_VGA_M80x30:
 1507     case SW_VGA_C80x50: case SW_VGA_M80x50:
 1508     case SW_VGA_C80x60: case SW_VGA_M80x60:
 1509     case SW_B40x25:     case SW_C40x25:
 1510     case SW_B80x25:     case SW_C80x25:
 1511     case SW_ENH_B40x25: case SW_ENH_C40x25:
 1512     case SW_ENH_B80x25: case SW_ENH_C80x25:
 1513     case SW_ENH_B80x43: case SW_ENH_C80x43:
 1514     case SW_EGAMONO80x25:
 1515 
 1516         if (!crtc_vga)
 1517             return ENODEV;
 1518         mp = get_mode_param(scp, cmd & 0xff);
 1519         if (mp == NULL)
 1520             return ENODEV;
 1521  
 1522         s = spltty();
 1523         if ((error = wait_scrn_saver_stop())) {
 1524             splx(s);
 1525             return error;
 1526         }
 1527 
 1528         scp->status &= ~MOUSE_VISIBLE;
 1529         remove_cutmarking(scp);
 1530         if (scp->history != NULL)
 1531             i = imax(scp->history_size / scp->xsize 
 1532                      - imax(SC_HISTORY_SIZE, scp->ysize), 0);
 1533         else
 1534             i = 0;
 1535         switch (cmd & 0xff) {
 1536         case M_VGA_C80x60: case M_VGA_M80x60:
 1537             if (!(fonts_loaded & FONT_8)) {
 1538                 splx(s);
 1539                 return EINVAL;
 1540             }
 1541             /*
 1542              * This is a kludge to fend off scrn_update() while we
 1543              * muck around with scp. XXX
 1544              */
 1545             scp->status |= UNKNOWN_MODE;
 1546             scp->xsize = 80;
 1547             scp->ysize = 60;
 1548             scp->font_size = 8;
 1549             break;
 1550         case M_VGA_C80x50: case M_VGA_M80x50:
 1551             if (!(fonts_loaded & FONT_8)) {
 1552                 splx(s);
 1553                 return EINVAL;
 1554             }
 1555             scp->status |= UNKNOWN_MODE;
 1556             scp->xsize = 80;
 1557             scp->ysize = 50;
 1558             scp->font_size = 8;
 1559             break;
 1560         case M_ENH_B80x43: case M_ENH_C80x43:
 1561             if (!(fonts_loaded & FONT_8)) {
 1562                 splx(s);
 1563                 return EINVAL;
 1564             }
 1565             scp->status |= UNKNOWN_MODE;
 1566             scp->xsize = 80;
 1567             scp->ysize = 43;
 1568             scp->font_size = 8;
 1569             break;
 1570         case M_VGA_C80x30: case M_VGA_M80x30:
 1571             scp->status |= UNKNOWN_MODE;
 1572             scp->xsize = 80;
 1573             scp->ysize = 30;
 1574             scp->font_size = mp[2];
 1575             break;
 1576         case M_ENH_C40x25: case M_ENH_B40x25:
 1577         case M_ENH_C80x25: case M_ENH_B80x25:
 1578         case M_EGAMONO80x25:
 1579             if (!(fonts_loaded & FONT_14)) {
 1580                 splx(s);
 1581                 return EINVAL;
 1582             }
 1583             /* FALL THROUGH */
 1584         default:
 1585             if ((cmd & 0xff) > M_VGA_CG320) {
 1586                 splx(s);
 1587                 return EINVAL;
 1588             }
 1589             scp->status |= UNKNOWN_MODE;
 1590             scp->xsize = mp[0];
 1591             scp->ysize = mp[1] + rows_offset;
 1592             scp->font_size = mp[2];
 1593             break;
 1594         }
 1595 
 1596         scp->mode = cmd & 0xff;
 1597         scp->xpixel = scp->xsize * 8;
 1598         scp->ypixel = scp->ysize * scp->font_size;
 1599         free(scp->scr_buf, M_DEVBUF);
 1600         scp->scr_buf = (u_short *)
 1601             malloc(scp->xsize*scp->ysize*sizeof(u_short), M_DEVBUF, M_WAITOK);
 1602         /* move the text cursor to the home position */
 1603         move_crsr(scp, 0, 0);
 1604         /* move the mouse cursor at the center of the screen */
 1605         scp->mouse_xpos = scp->xpixel / 2;
 1606         scp->mouse_ypos = scp->ypixel / 2;
 1607         scp->mouse_pos = scp->mouse_oldpos = 
 1608             scp->scr_buf + (scp->mouse_ypos / scp->font_size) * scp->xsize 
 1609             + scp->mouse_xpos / 8;
 1610         /* allocate a larger cut buffer if necessary */
 1611         if ((cut_buffer == NULL)
 1612             || (cut_buffer_size < scp->xsize * scp->ysize + 1)) {
 1613             if (cut_buffer != NULL)
 1614                 free(cut_buffer, M_DEVBUF);
 1615             cut_buffer_size = scp->xsize * scp->ysize + 1;
 1616             cut_buffer = (char *)malloc(cut_buffer_size, M_DEVBUF, M_NOWAIT);
 1617             if (cut_buffer != NULL)
 1618                 cut_buffer[0] = '\0';
 1619         }
 1620         splx(s);
 1621 
 1622         usp = scp->history;
 1623         scp->history = NULL;
 1624         if (usp != NULL) {
 1625             free(usp, M_DEVBUF);
 1626             extra_history_size += i;
 1627         }
 1628         scp->history_size = imax(SC_HISTORY_SIZE, scp->ysize) * scp->xsize;
 1629         usp = (u_short *)malloc(scp->history_size * sizeof(u_short), 
 1630                                 M_DEVBUF, M_NOWAIT);
 1631         if (usp != NULL)
 1632             bzero(usp, scp->history_size * sizeof(u_short));
 1633         scp->history_head = scp->history_pos = usp;
 1634         scp->history = usp;
 1635         if (scp == cur_console)
 1636             set_mode(scp);
 1637         clear_screen(scp);
 1638         scp->status &= ~UNKNOWN_MODE;
 1639 
 1640         if (tp->t_winsize.ws_col != scp->xsize
 1641             || tp->t_winsize.ws_row != scp->ysize) {
 1642             tp->t_winsize.ws_col = scp->xsize;
 1643             tp->t_winsize.ws_row = scp->ysize;
 1644             pgsignal(tp->t_pgrp, SIGWINCH, 1);
 1645         }
 1646         return 0;
 1647 
 1648     /* GRAPHICS MODES */
 1649     case SW_BG320:     case SW_BG640:
 1650     case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
 1651     case SW_CG640x350: case SW_ENH_CG640:
 1652     case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
 1653 
 1654         if (!crtc_vga)
 1655             return ENODEV;
 1656         mp = get_mode_param(scp, cmd & 0xff);
 1657         if (mp == NULL)
 1658             return ENODEV;
 1659 
 1660         s = spltty();
 1661         if ((error = wait_scrn_saver_stop())) {
 1662             splx(s);
 1663             return error;
 1664         }
 1665 
 1666         scp->status &= ~MOUSE_VISIBLE;
 1667         remove_cutmarking(scp);
 1668         scp->status |= UNKNOWN_MODE;    /* graphics mode */
 1669         scp->mode = cmd & 0xFF;
 1670         scp->xpixel = mp[0] * 8;
 1671         scp->ypixel = (mp[1] + rows_offset) * mp[2];
 1672         scp->font_size = FONT_NONE;
 1673         /* move the mouse cursor at the center of the screen */
 1674         scp->mouse_xpos = scp->xpixel / 2;
 1675         scp->mouse_ypos = scp->ypixel / 2;
 1676         splx(s);
 1677 
 1678         if (scp == cur_console)
 1679             set_mode(scp);
 1680         /* clear_graphics();*/
 1681 
 1682         if (tp->t_winsize.ws_xpixel != scp->xpixel
 1683             || tp->t_winsize.ws_ypixel != scp->ypixel) {
 1684             tp->t_winsize.ws_xpixel = scp->xpixel;
 1685             tp->t_winsize.ws_ypixel = scp->ypixel;
 1686             pgsignal(tp->t_pgrp, SIGWINCH, 1);
 1687         }
 1688         return 0;
 1689 
 1690     case SW_VGA_MODEX:
 1691         if (!crtc_vga)
 1692             return ENODEV;
 1693         mp = get_mode_param(scp, cmd & 0xff);
 1694         if (mp == NULL)
 1695             return ENODEV;
 1696 
 1697         s = spltty();
 1698         if ((error = wait_scrn_saver_stop())) {
 1699             splx(s);
 1700             return error;
 1701         }
 1702 
 1703         scp->status &= ~MOUSE_VISIBLE;
 1704         remove_cutmarking(scp);
 1705         scp->status |= UNKNOWN_MODE;    /* graphics mode */
 1706         scp->mode = cmd & 0xFF;
 1707         scp->xpixel = 320;
 1708         scp->ypixel = 240;
 1709         scp->font_size = FONT_NONE;
 1710         splx(s);
 1711 
 1712         if (scp == cur_console)
 1713             set_mode(scp);
 1714         /* clear_graphics();*/
 1715         if (tp->t_winsize.ws_xpixel != scp->xpixel
 1716             || tp->t_winsize.ws_ypixel != scp->ypixel) {
 1717             tp->t_winsize.ws_xpixel = scp->xpixel;
 1718             tp->t_winsize.ws_ypixel = scp->ypixel;
 1719             pgsignal(tp->t_pgrp, SIGWINCH, 1);
 1720         }
 1721         return 0;
 1722 
 1723     case VT_SETMODE:            /* set screen switcher mode */
 1724     {
 1725         struct vt_mode *mode;
 1726 
 1727         mode = (struct vt_mode *)data;
 1728         if (ISSIGVALID(mode->relsig) && ISSIGVALID(mode->acqsig) &&
 1729             ISSIGVALID(mode->frsig)) {
 1730             bcopy(data, &scp->smode, sizeof(struct vt_mode));
 1731             if (scp->smode.mode == VT_PROCESS) {
 1732                 scp->proc = p;
 1733                 scp->pid = scp->proc->p_pid;
 1734             }
 1735             return 0;
 1736         } else
 1737             return EINVAL;
 1738     }
 1739 
 1740     case VT_GETMODE:            /* get screen switcher mode */
 1741         bcopy(&scp->smode, data, sizeof(struct vt_mode));
 1742         return 0;
 1743 
 1744     case VT_RELDISP:            /* screen switcher ioctl */
 1745         switch(*data) {
 1746         case VT_FALSE:          /* user refuses to release screen, abort */
 1747             if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
 1748                 old_scp->status &= ~SWITCH_WAIT_REL;
 1749                 switch_in_progress = FALSE;
 1750                 return 0;
 1751             }
 1752             return EINVAL;
 1753 
 1754         case VT_TRUE:           /* user has released screen, go on */
 1755             if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
 1756                 scp->status &= ~SWITCH_WAIT_REL;
 1757                 exchange_scr();
 1758                 if (new_scp->smode.mode == VT_PROCESS) {
 1759                     new_scp->status |= SWITCH_WAIT_ACQ;
 1760                     psignal(new_scp->proc, new_scp->smode.acqsig);
 1761                 }
 1762                 else
 1763                     switch_in_progress = FALSE;
 1764                 return 0;
 1765             }
 1766             return EINVAL;
 1767 
 1768         case VT_ACKACQ:         /* acquire acknowledged, switch completed */
 1769             if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
 1770                 scp->status &= ~SWITCH_WAIT_ACQ;
 1771                 switch_in_progress = FALSE;
 1772                 return 0;
 1773             }
 1774             return EINVAL;
 1775 
 1776         default:
 1777             return EINVAL;
 1778         }
 1779         /* NOT REACHED */
 1780 
 1781     case VT_OPENQRY:            /* return free virtual console */
 1782         for (i = 0; i < MAXCONS; i++) {
 1783             tp = VIRTUAL_TTY(i);
 1784             if (!(tp->t_state & TS_ISOPEN)) {
 1785                 *data = i + 1;
 1786                 return 0;
 1787             }
 1788         }
 1789         return EINVAL;
 1790 
 1791     case VT_ACTIVATE:           /* switch to screen *data */
 1792         return switch_scr(scp, (*data) - 1);
 1793 
 1794     case VT_WAITACTIVE:         /* wait for switch to occur */
 1795         if (*data > MAXCONS || *data < 0)
 1796             return EINVAL;
 1797         if (minor(dev) == (*data) - 1)
 1798             return 0;
 1799         if (*data == 0) {
 1800             if (scp == cur_console)
 1801                 return 0;
 1802         }
 1803         else
 1804             scp = console[(*data) - 1];
 1805         while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
 1806                              "waitvt", 0)) == ERESTART) ;
 1807         return error;
 1808 
 1809     case VT_GETACTIVE:
 1810         *data = get_scr_num()+1;
 1811         return 0;
 1812 
 1813     case KDENABIO:              /* allow io operations */
 1814         error = suser(p->p_ucred, &p->p_acflag);
 1815         if (error != 0)
 1816             return error;
 1817         if (securelevel > 0)
 1818             return EPERM;
 1819         fp = (struct trapframe *)p->p_md.md_regs;
 1820         fp->tf_eflags |= PSL_IOPL;
 1821         return 0;
 1822 
 1823     case KDDISABIO:             /* disallow io operations (default) */
 1824         fp = (struct trapframe *)p->p_md.md_regs;
 1825         fp->tf_eflags &= ~PSL_IOPL;
 1826         return 0;
 1827 
 1828     case KDSETMODE:             /* set current mode of this (virtual) console */
 1829         switch (*data) {
 1830         case KD_TEXT:           /* switch to TEXT (known) mode */
 1831             /* restore fonts & palette ! */
 1832             if (crtc_vga) {
 1833 #if 0
 1834                 /*
 1835                  * FONT KLUDGE
 1836                  * Don't load fonts for now... XXX
 1837                  */
 1838                 if (fonts_loaded & FONT_8)
 1839                     copy_font(LOAD, FONT_8, font_8);
 1840                 if (fonts_loaded & FONT_14)
 1841                     copy_font(LOAD, FONT_14, font_14);
 1842                 if (fonts_loaded & FONT_16)
 1843                     copy_font(LOAD, FONT_16, font_16);
 1844 #endif
 1845                 load_palette(palette);
 1846             }
 1847             /* FALL THROUGH */
 1848 
 1849         case KD_TEXT1:          /* switch to TEXT (known) mode */
 1850             s = spltty();
 1851             if ((error = wait_scrn_saver_stop())) {
 1852                 splx(s);
 1853                 return error;
 1854             }
 1855             scp->status &= ~MOUSE_VISIBLE;
 1856             remove_cutmarking(scp);
 1857             scp->status |= UNKNOWN_MODE;
 1858             splx(s);
 1859             /* no restore fonts & palette */
 1860             if (crtc_vga)
 1861                 set_mode(scp);
 1862             scp->status &= ~UNKNOWN_MODE;
 1863             clear_screen(scp);
 1864             return 0;
 1865 
 1866         case KD_GRAPHICS:       /* switch to GRAPHICS (unknown) mode */
 1867             s = spltty();
 1868             if ((error = wait_scrn_saver_stop())) {
 1869                 splx(s);
 1870                 return error;
 1871             }
 1872             scp->status &= ~MOUSE_VISIBLE;
 1873             remove_cutmarking(scp);
 1874             scp->status |= UNKNOWN_MODE;
 1875             splx(s);
 1876             return 0;
 1877         default:
 1878             return EINVAL;
 1879         }
 1880         /* NOT REACHED */
 1881 
 1882     case KDGETMODE:             /* get current mode of this (virtual) console */
 1883         *data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
 1884         return 0;
 1885 
 1886     case KDSBORDER:             /* set border color of this (virtual) console */
 1887         scp->border = *data;
 1888         if (scp == cur_console)
 1889             set_border(scp->border);
 1890         return 0;
 1891 
 1892     case KDSKBSTATE:            /* set keyboard state (locks) */
 1893         if (*data >= 0 && *data <= LOCK_KEY_MASK) {
 1894             scp->status &= ~LOCK_KEY_MASK;
 1895             scp->status |= *data;
 1896             if (scp == cur_console)
 1897                 update_leds(scp->status);
 1898             return 0;
 1899         }
 1900         return EINVAL;
 1901 
 1902     case KDGKBSTATE:            /* get keyboard state (locks) */
 1903         *data = scp->status & LOCK_KEY_MASK;
 1904         return 0;
 1905 
 1906     case KDSETRAD:              /* set keyboard repeat & delay rates */
 1907         if (*data & 0x80)
 1908             return EINVAL;
 1909         if (sc_kbdc != NULL) 
 1910             set_keyboard(KBDC_SET_TYPEMATIC, *data);
 1911         return 0;
 1912 
 1913     case KDSKBMODE:             /* set keyboard mode */
 1914         switch (*data) {
 1915         case K_RAW:             /* switch to RAW scancode mode */
 1916             scp->status &= ~KBD_CODE_MODE;
 1917             scp->status |= KBD_RAW_MODE;
 1918             return 0;
 1919 
 1920         case K_CODE:            /* switch to CODE mode */
 1921             scp->status &= ~KBD_RAW_MODE;
 1922             scp->status |= KBD_CODE_MODE;
 1923             return 0;
 1924 
 1925         case K_XLATE:           /* switch to XLT ascii mode */
 1926             if (scp == cur_console && scp->status & KBD_RAW_MODE)
 1927                 shfts = ctls = alts = agrs = metas = accents = 0;
 1928             scp->status &= ~(KBD_RAW_MODE | KBD_CODE_MODE);
 1929             return 0;
 1930         default:
 1931             return EINVAL;
 1932         }
 1933         /* NOT REACHED */
 1934 
 1935     case KDGKBMODE:             /* get keyboard mode */
 1936         *data = (scp->status & KBD_RAW_MODE) ? K_RAW : 
 1937                 ((scp->status & KBD_CODE_MODE) ? K_CODE : K_XLATE);
 1938         return 0;
 1939 
 1940     case KDMKTONE:              /* sound the bell */
 1941         if (*(int*)data)
 1942             do_bell(scp, (*(int*)data)&0xffff,
 1943                     (((*(int*)data)>>16)&0xffff)*hz/1000);
 1944         else
 1945             do_bell(scp, scp->bell_pitch, scp->bell_duration);
 1946         return 0;
 1947 
 1948     case KIOCSOUND:             /* make tone (*data) hz */
 1949         if (scp == cur_console) {
 1950             if (*(int*)data) {
 1951                 int pitch = timer_freq / *(int*)data;
 1952 
 1953                 /* set command for counter 2, 2 byte write */
 1954                 if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE))
 1955                     return EBUSY;
 1956 
 1957                 /* set pitch */
 1958                 outb(TIMER_CNTR2, pitch);
 1959                 outb(TIMER_CNTR2, (pitch>>8));
 1960 
 1961                 /* enable counter 2 output to speaker */
 1962                 outb(IO_PPI, inb(IO_PPI) | 3);
 1963             }
 1964             else {
 1965                 /* disable counter 2 output to speaker */
 1966                 outb(IO_PPI, inb(IO_PPI) & 0xFC);
 1967                 release_timer2();
 1968             }
 1969         }
 1970         return 0;
 1971 
 1972     case KDGKBTYPE:             /* get keyboard type */
 1973         *data = 0;              /* type not known (yet) */
 1974         return 0;
 1975 
 1976     case KDSETLED:              /* set keyboard LED status */
 1977         if (*data >= 0 && *data <= LED_MASK) {
 1978             scp->status &= ~LED_MASK;
 1979             scp->status |= *data;
 1980             if (scp == cur_console)
 1981                 update_leds(scp->status);
 1982             return 0;
 1983         }
 1984         return EINVAL;
 1985 
 1986     case KDGETLED:              /* get keyboard LED status */
 1987         *data = scp->status & LED_MASK;
 1988         return 0;
 1989 
 1990     case GETFKEY:               /* get functionkey string */
 1991         if (*(u_short*)data < n_fkey_tab) {
 1992             fkeyarg_t *ptr = (fkeyarg_t*)data;
 1993             bcopy(&fkey_tab[ptr->keynum].str, ptr->keydef,
 1994                   fkey_tab[ptr->keynum].len);
 1995             ptr->flen = fkey_tab[ptr->keynum].len;
 1996             return 0;
 1997         }
 1998         else
 1999             return EINVAL;
 2000 
 2001     case SETFKEY:               /* set functionkey string */
 2002         if (*(u_short*)data < n_fkey_tab) {
 2003             fkeyarg_t *ptr = (fkeyarg_t*)data;
 2004             bcopy(ptr->keydef, &fkey_tab[ptr->keynum].str,
 2005                   min(ptr->flen, MAXFK));
 2006             fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
 2007             return 0;
 2008         }
 2009         else
 2010             return EINVAL;
 2011 
 2012     case GIO_SCRNMAP:           /* get output translation table */
 2013         bcopy(&scr_map, data, sizeof(scr_map));
 2014         return 0;
 2015 
 2016     case PIO_SCRNMAP:           /* set output translation table */
 2017         bcopy(data, &scr_map, sizeof(scr_map));
 2018         for (i=0; i<sizeof(scr_map); i++)
 2019             scr_rmap[scr_map[i]] = i;
 2020         return 0;
 2021 
 2022     case GIO_KEYMAP:            /* get keyboard translation table */
 2023         bcopy(&key_map, data, sizeof(key_map));
 2024         return 0;
 2025 
 2026     case PIO_KEYMAP:            /* set keyboard translation table */
 2027         accents = 0;
 2028         bzero(&accent_map, sizeof(accent_map));
 2029         bcopy(data, &key_map, sizeof(key_map));
 2030         return 0;
 2031 
 2032     case GIO_DEADKEYMAP:        /* get accent key translation table */
 2033         bcopy(&accent_map, data, sizeof(accent_map));
 2034         return 0;
 2035 
 2036     case PIO_DEADKEYMAP:        /* set accent key translation table */
 2037         accents = 0;
 2038         bcopy(data, &accent_map, sizeof(accent_map));
 2039         return 0;
 2040 
 2041     case PIO_FONT8x8:           /* set 8x8 dot font */
 2042         if (!crtc_vga)
 2043             return ENXIO;
 2044         bcopy(data, font_8, 8*256);
 2045         fonts_loaded |= FONT_8;
 2046         /*
 2047          * FONT KLUDGE
 2048          * Always use the font page #0. XXX
 2049          * Don't load if the current font size is not 8x8.
 2050          */
 2051         if (!(cur_console->status & UNKNOWN_MODE)
 2052             && (cur_console->font_size < 14)) {
 2053             copy_font(LOAD, FONT_8, font_8);
 2054             if (flags & CHAR_CURSOR)
 2055                 set_destructive_cursor(cur_console);
 2056         }
 2057         return 0;
 2058 
 2059     case GIO_FONT8x8:           /* get 8x8 dot font */
 2060         if (!crtc_vga)
 2061             return ENXIO;
 2062         if (fonts_loaded & FONT_8) {
 2063             bcopy(font_8, data, 8*256);
 2064             return 0;
 2065         }
 2066         else
 2067             return ENXIO;
 2068 
 2069     case PIO_FONT8x14:          /* set 8x14 dot font */
 2070         if (!crtc_vga)
 2071             return ENXIO;
 2072         bcopy(data, font_14, 14*256);
 2073         fonts_loaded |= FONT_14;
 2074         /*
 2075          * FONT KLUDGE
 2076          * Always use the font page #0. XXX
 2077          * Don't load if the current font size is not 8x14.
 2078          */
 2079         if (!(cur_console->status & UNKNOWN_MODE)
 2080             && (cur_console->font_size >= 14) && (cur_console->font_size < 16)) {
 2081             copy_font(LOAD, FONT_14, font_14);
 2082             if (flags & CHAR_CURSOR)
 2083                 set_destructive_cursor(cur_console);
 2084         }
 2085         return 0;
 2086 
 2087     case GIO_FONT8x14:          /* get 8x14 dot font */
 2088         if (!crtc_vga)
 2089             return ENXIO;
 2090         if (fonts_loaded & FONT_14) {
 2091             bcopy(font_14, data, 14*256);
 2092             return 0;
 2093         }
 2094         else
 2095             return ENXIO;
 2096 
 2097     case PIO_FONT8x16:          /* set 8x16 dot font */
 2098         if (!crtc_vga)
 2099             return ENXIO;
 2100         bcopy(data, font_16, 16*256);
 2101         fonts_loaded |= FONT_16;
 2102         /*
 2103          * FONT KLUDGE
 2104          * Always use the font page #0. XXX
 2105          * Don't load if the current font size is not 8x16.
 2106          */
 2107         if (!(cur_console->status & UNKNOWN_MODE)
 2108             && (cur_console->font_size >= 16)) {
 2109             copy_font(LOAD, FONT_16, font_16);
 2110             if (flags & CHAR_CURSOR)
 2111                 set_destructive_cursor(cur_console);
 2112         }
 2113         return 0;
 2114 
 2115     case GIO_FONT8x16:          /* get 8x16 dot font */
 2116         if (!crtc_vga)
 2117             return ENXIO;
 2118         if (fonts_loaded & FONT_16) {
 2119             bcopy(font_16, data, 16*256);
 2120             return 0;
 2121         }
 2122         else
 2123             return ENXIO;
 2124     default:
 2125         break;
 2126     }
 2127 
 2128     error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
 2129     if (error >= 0)
 2130         return(error);
 2131     error = ttioctl(tp, cmd, data, flag);
 2132     if (error >= 0)
 2133         return(error);
 2134     return(ENOTTY);
 2135 }
 2136 
 2137 static void
 2138 scstart(struct tty *tp)
 2139 {
 2140     struct clist *rbp;
 2141     int s, len;
 2142     u_char buf[PCBURST];
 2143     scr_stat *scp = get_scr_stat(tp->t_dev);
 2144 
 2145     if (scp->status & SLKED || blink_in_progress)
 2146         return; /* XXX who repeats the call when the above flags are cleared? */
 2147     s = spltty();
 2148     if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
 2149         tp->t_state |= TS_BUSY;
 2150         rbp = &tp->t_outq;
 2151         while (rbp->c_cc) {
 2152             len = q_to_b(rbp, buf, PCBURST);
 2153             splx(s);
 2154             ansi_put(scp, buf, len);
 2155             s = spltty();
 2156         }
 2157         tp->t_state &= ~TS_BUSY;
 2158         ttwwakeup(tp);
 2159     }
 2160     splx(s);
 2161 }
 2162 
 2163 static void
 2164 scmousestart(struct tty *tp)
 2165 {
 2166     struct clist *rbp;
 2167     int s;
 2168     u_char buf[PCBURST];
 2169 
 2170     s = spltty();
 2171     if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
 2172         tp->t_state |= TS_BUSY;
 2173         rbp = &tp->t_outq;
 2174         while (rbp->c_cc) {
 2175             q_to_b(rbp, buf, PCBURST);
 2176         }
 2177         tp->t_state &= ~TS_BUSY;
 2178         ttwwakeup(tp);
 2179     }
 2180     splx(s);
 2181 }
 2182 
 2183 void
 2184 sccnprobe(struct consdev *cp)
 2185 {
 2186     struct isa_device *dvp;
 2187 
 2188     /*
 2189      * Take control if we are the highest priority enabled display device.
 2190      */
 2191     dvp = find_display();
 2192     if (dvp == NULL || dvp->id_driver != &scdriver) {
 2193         cp->cn_pri = CN_DEAD;
 2194         return;
 2195     }
 2196 
 2197     if (!scvidprobe(dvp->id_unit, dvp->id_flags)) {
 2198         cp->cn_pri = CN_DEAD;
 2199         return;
 2200     }
 2201 
 2202     /* initialize required fields */
 2203     cp->cn_dev = makedev(CDEV_MAJOR, SC_CONSOLE);
 2204     cp->cn_pri = CN_INTERNAL;
 2205 
 2206     sc_kbdc = kbdc_open(sc_port);
 2207 }
 2208 
 2209 void
 2210 sccninit(struct consdev *cp)
 2211 {
 2212     scinit();
 2213 }
 2214 
 2215 void
 2216 sccnputc(dev_t dev, int c)
 2217 {
 2218     u_char buf[1];
 2219     int s;
 2220     scr_stat *scp = console[0];
 2221     term_stat save = scp->term;
 2222 
 2223     scp->term = kernel_console;
 2224     current_default = &kernel_default;
 2225     if (scp == cur_console && !(scp->status & UNKNOWN_MODE))
 2226         remove_cursor_image(scp);
 2227     buf[0] = c;
 2228     ansi_put(scp, buf, 1);
 2229     kernel_console = scp->term;
 2230     current_default = &user_default;
 2231     scp->term = save;
 2232 
 2233     s = spltty();       /* block scintr and scrn_timer */
 2234     sccnupdate(scp);
 2235     splx(s);
 2236 }
 2237 
 2238 int
 2239 sccngetc(dev_t dev)
 2240 {
 2241     int s = spltty();   /* block scintr and scrn_timer while we poll */
 2242     int c;
 2243 
 2244     /* 
 2245      * Stop the screen saver and update the screen if necessary.
 2246      * What if we have been running in the screen saver code... XXX
 2247      */
 2248     sccnupdate(cur_console);
 2249 
 2250     c = scgetc(SCGETC_CN);
 2251     splx(s);
 2252     return(c);
 2253 }
 2254 
 2255 int
 2256 sccncheckc(dev_t dev)
 2257 {
 2258     int s = spltty();   /* block scintr and scrn_timer while we poll */
 2259     int c;
 2260 
 2261     sccnupdate(cur_console);
 2262     c = scgetc(SCGETC_CN | SCGETC_NONBLOCK);
 2263     splx(s);
 2264     return(c == NOKEY ? -1 : c);        /* c == -1 can't happen */
 2265 }
 2266 
 2267 static void
 2268 sccnupdate(scr_stat *scp)
 2269 {
 2270     if (scp == cur_console && !font_loading_in_progress) {
 2271         if (scrn_blanked > 0)
 2272             stop_scrn_saver(current_saver);
 2273         if (!(scp->status & UNKNOWN_MODE) && scrn_blanked <= 0 
 2274             && !blink_in_progress && !switch_in_progress)
 2275             scrn_update(scp, TRUE);
 2276     }
 2277 }
 2278 
 2279 static scr_stat
 2280 *get_scr_stat(dev_t dev)
 2281 {
 2282     int unit = minor(dev);
 2283 
 2284     if (unit == SC_CONSOLE)
 2285         return console[0];
 2286     if (unit >= MAXCONS || unit < 0)
 2287         return(NULL);
 2288     return console[unit];
 2289 }
 2290 
 2291 static int
 2292 get_scr_num()
 2293 {
 2294     int i = 0;
 2295 
 2296     while ((i < MAXCONS) && (cur_console != console[i]))
 2297         i++;
 2298     return i < MAXCONS ? i : 0;
 2299 }
 2300 
 2301 static void
 2302 scrn_timer(void *arg)
 2303 {
 2304     scr_stat *scp = cur_console;
 2305     int s;
 2306 
 2307     /* don't do anything when we are touching font */
 2308     if (font_loading_in_progress) {
 2309         timeout(scrn_timer, NULL, hz / 10);
 2310         return;
 2311     }
 2312     s = spltty();
 2313 
 2314     /* 
 2315      * With release 2.1 of the Xaccel server, the keyboard is left
 2316      * hanging pretty often. Apparently an interrupt from the
 2317      * keyboard is lost, and I don't know why (yet).
 2318      * This ugly hack calls scintr if input is ready for the keyboard
 2319      * and conveniently hides the problem.                      XXX
 2320      */
 2321     /* Try removing anything stuck in the keyboard controller; whether
 2322      * it's a keyboard scan code or mouse data. `scintr()' doesn't
 2323      * read the mouse data directly, but `kbdio' routines will, as a
 2324      * side effect.
 2325      */
 2326     if (kbdc_lock(sc_kbdc, TRUE)) {
 2327         /*
 2328          * We have seen the lock flag is not set. Let's reset the flag early;
 2329          * otherwise `update_led()' failes which may want the lock 
 2330          * during `scintr()'.
 2331          */
 2332         kbdc_lock(sc_kbdc, FALSE);
 2333         if (kbdc_data_ready(sc_kbdc)) 
 2334             scintr(0);
 2335     }
 2336 
 2337     /* should we stop the screen saver? */
 2338     if (panicstr || shutdown_in_progress)
 2339         scrn_time_stamp = mono_time.tv_sec;
 2340     if (mono_time.tv_sec <= scrn_time_stamp + scrn_blank_time)
 2341         if (scrn_blanked > 0)
 2342             stop_scrn_saver(current_saver);
 2343 
 2344     /* should we just return ? */
 2345     if ((scp->status&UNKNOWN_MODE) || blink_in_progress || switch_in_progress) {
 2346         timeout(scrn_timer, NULL, hz / 10);
 2347         splx(s);
 2348         return;
 2349     }
 2350 
 2351     scp = cur_console;
 2352     if (scrn_blanked <= 0)
 2353         scrn_update(scp, TRUE);
 2354 
 2355     /* should we activate the screen saver? */
 2356     if ((scrn_blank_time != 0) 
 2357             && (mono_time.tv_sec > scrn_time_stamp + scrn_blank_time))
 2358         (*current_saver)(TRUE);
 2359 
 2360     timeout(scrn_timer, NULL, hz / 25);
 2361     splx(s);
 2362 }
 2363 
 2364 static void 
 2365 scrn_update(scr_stat *scp, int show_cursor)
 2366 {
 2367     /* update screen image */
 2368     if (scp->start <= scp->end) {
 2369         sc_bcopy(scp->scr_buf + scp->start, Crtat + scp->start,
 2370             (1 + scp->end - scp->start) * sizeof(u_short));
 2371     }
 2372 
 2373     /* we are not to show the cursor and the mouse pointer... */
 2374     if (!show_cursor) {
 2375         scp->end = 0;
 2376         scp->start = scp->xsize*scp->ysize;
 2377         return;
 2378     }
 2379 
 2380     /* update "pseudo" mouse pointer image */
 2381     if (scp->status & MOUSE_VISIBLE) {
 2382         /* did mouse move since last time ? */
 2383         if (scp->status & MOUSE_MOVED) {
 2384             /* do we need to remove old mouse pointer image ? */
 2385             if (scp->mouse_cut_start != NULL ||
 2386                 (scp->mouse_pos-scp->scr_buf) <= scp->start ||
 2387                 (scp->mouse_pos+scp->xsize + 1 - scp->scr_buf) >= scp->end) {
 2388                 remove_mouse_image(scp);
 2389             }
 2390             scp->status &= ~MOUSE_MOVED;
 2391             draw_mouse_image(scp);
 2392         }
 2393         else {
 2394             /* mouse didn't move, has it been overwritten ? */
 2395             if ((scp->mouse_pos+scp->xsize + 1 - scp->scr_buf) >= scp->start &&
 2396                 (scp->mouse_pos - scp->scr_buf) <= scp->end) {
 2397                 draw_mouse_image(scp);
 2398             }
 2399         }
 2400     }
 2401         
 2402     /* update cursor image */
 2403     if (scp->status & CURSOR_ENABLED) {
 2404         /* did cursor move since last time ? */
 2405         if (scp->cursor_pos != scp->cursor_oldpos) {
 2406             /* do we need to remove old cursor image ? */
 2407             if ((scp->cursor_oldpos - scp->scr_buf) < scp->start ||
 2408                 ((scp->cursor_oldpos - scp->scr_buf) > scp->end)) {
 2409                 remove_cursor_image(scp);
 2410             }
 2411             scp->cursor_oldpos = scp->cursor_pos;
 2412             draw_cursor_image(scp);
 2413         }
 2414         else {
 2415             /* cursor didn't move, has it been overwritten ? */
 2416             if (scp->cursor_pos - scp->scr_buf >= scp->start &&
 2417                 scp->cursor_pos - scp->scr_buf <= scp->end) {
 2418                 draw_cursor_image(scp);
 2419             } else {
 2420                 /* if its a blinking cursor, we may have to update it */
 2421                 if (flags & BLINK_CURSOR)
 2422                     draw_cursor_image(scp);
 2423             }
 2424         }
 2425         blinkrate++;
 2426     }
 2427 
 2428     if (scp->mouse_cut_start != NULL)
 2429         draw_cutmarking(scp);
 2430 
 2431     scp->end = 0;
 2432     scp->start = scp->xsize*scp->ysize;
 2433 }
 2434 
 2435 int
 2436 add_scrn_saver(void (*this_saver)(int))
 2437 {
 2438     if (current_saver != none_saver)
 2439         return EBUSY;
 2440     current_saver = this_saver;
 2441     return 0;
 2442 }
 2443 
 2444 int
 2445 remove_scrn_saver(void (*this_saver)(int))
 2446 {
 2447     if (current_saver != this_saver)
 2448         return EINVAL;
 2449 
 2450     /*
 2451      * In order to prevent `current_saver' from being called by
 2452      * the timeout routine `scrn_timer()' while we manipulate 
 2453      * the saver list, we shall set `current_saver' to `none_saver' 
 2454      * before stopping the current saver, rather than blocking by `splXX()'.
 2455      */
 2456     current_saver = none_saver;
 2457     if (scrn_blanked > 0)
 2458         stop_scrn_saver(this_saver);
 2459 
 2460     return 0;
 2461 }
 2462 
 2463 static void
 2464 stop_scrn_saver(void (*saver)(int))
 2465 {
 2466     (*saver)(FALSE);
 2467     scrn_time_stamp = mono_time.tv_sec;
 2468     mark_all(cur_console);
 2469     wakeup((caddr_t)&scrn_blanked);
 2470 }
 2471 
 2472 static int
 2473 wait_scrn_saver_stop(void)
 2474 {
 2475     int error = 0;
 2476 
 2477     scrn_time_stamp = mono_time.tv_sec;
 2478     while (scrn_blanked > 0) {
 2479         error = tsleep((caddr_t)&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
 2480         scrn_time_stamp = mono_time.tv_sec;
 2481         if (error != ERESTART)
 2482             break;
 2483     }
 2484     return error;
 2485 }
 2486 
 2487 static void
 2488 clear_screen(scr_stat *scp)
 2489 {
 2490     move_crsr(scp, 0, 0);
 2491     scp->cursor_oldpos = scp->cursor_pos;
 2492     fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
 2493           scp->xsize * scp->ysize);
 2494     mark_all(scp);
 2495     remove_cutmarking(scp);
 2496 }
 2497 
 2498 static int
 2499 switch_scr(scr_stat *scp, u_int next_scr)
 2500 {
 2501     if (switch_in_progress && (cur_console->proc != pfind(cur_console->pid)))
 2502         switch_in_progress = FALSE;
 2503 
 2504     if (next_scr >= MAXCONS || switch_in_progress ||
 2505         (cur_console->smode.mode == VT_AUTO
 2506          && cur_console->status & UNKNOWN_MODE)) {
 2507         do_bell(scp, BELL_PITCH, BELL_DURATION);
 2508         return EINVAL;
 2509     }
 2510 
 2511     /* is the wanted virtual console open ? */
 2512     if (next_scr) {
 2513         struct tty *tp = VIRTUAL_TTY(next_scr);
 2514         if (!(tp->t_state & TS_ISOPEN)) {
 2515             do_bell(scp, BELL_PITCH, BELL_DURATION);
 2516             return EINVAL;
 2517         }
 2518     }
 2519     /* delay switch if actively updating screen */
 2520     if (write_in_progress || blink_in_progress) {
 2521         delayed_next_scr = next_scr+1;
 2522         return 0;
 2523     }
 2524     switch_in_progress = TRUE;
 2525     old_scp = cur_console;
 2526     new_scp = console[next_scr];
 2527     wakeup((caddr_t)&new_scp->smode);
 2528     if (new_scp == old_scp) {
 2529         switch_in_progress = FALSE;
 2530         delayed_next_scr = FALSE;
 2531         return 0;
 2532     }
 2533 
 2534     /* has controlling process died? */
 2535     if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
 2536         old_scp->smode.mode = VT_AUTO;
 2537     if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
 2538         new_scp->smode.mode = VT_AUTO;
 2539 
 2540     /* check the modes and switch appropriately */
 2541     if (old_scp->smode.mode == VT_PROCESS) {
 2542         old_scp->status |= SWITCH_WAIT_REL;
 2543         psignal(old_scp->proc, old_scp->smode.relsig);
 2544     }
 2545     else {
 2546         exchange_scr();
 2547         if (new_scp->smode.mode == VT_PROCESS) {
 2548             new_scp->status |= SWITCH_WAIT_ACQ;
 2549             psignal(new_scp->proc, new_scp->smode.acqsig);
 2550         }
 2551         else
 2552             switch_in_progress = FALSE;
 2553     }
 2554     return 0;
 2555 }
 2556 
 2557 static void
 2558 exchange_scr(void)
 2559 {
 2560     move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
 2561     cur_console = new_scp;
 2562     if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE)){
 2563         if (crtc_vga)
 2564             set_mode(new_scp);
 2565     }
 2566     move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
 2567     if (!(new_scp->status & UNKNOWN_MODE) && (flags & CHAR_CURSOR))
 2568         set_destructive_cursor(new_scp);
 2569     if ((old_scp->status & UNKNOWN_MODE) && crtc_vga)
 2570         load_palette(palette);
 2571     if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE ||
 2572         old_scp->status & KBD_CODE_MODE || new_scp->status & KBD_CODE_MODE)
 2573         shfts = ctls = alts = agrs = metas = accents = 0;
 2574     set_border(new_scp->border);
 2575     update_leds(new_scp->status);
 2576     delayed_next_scr = FALSE;
 2577     mark_all(new_scp);
 2578 }
 2579 
 2580 static void
 2581 scan_esc(scr_stat *scp, u_char c)
 2582 {
 2583     static u_char ansi_col[16] =
 2584         {0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
 2585     int i, n;
 2586     u_short *src, *dst, count;
 2587 
 2588     if (scp->term.esc == 1) {   /* seen ESC */
 2589         switch (c) {
 2590 
 2591         case '7':   /* Save cursor position */
 2592             scp->saved_xpos = scp->xpos;
 2593             scp->saved_ypos = scp->ypos;
 2594             break;
 2595 
 2596         case '8':   /* Restore saved cursor position */
 2597             if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
 2598                 move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
 2599             break;
 2600 
 2601         case '[':   /* Start ESC [ sequence */
 2602             scp->term.esc = 2;
 2603             scp->term.last_param = -1;
 2604             for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
 2605                 scp->term.param[i] = 1;
 2606             scp->term.num_param = 0;
 2607             return;
 2608 
 2609         case 'M':   /* Move cursor up 1 line, scroll if at top */
 2610             if (scp->ypos > 0)
 2611                 move_crsr(scp, scp->xpos, scp->ypos - 1);
 2612             else {
 2613                 bcopy(scp->scr_buf, scp->scr_buf + scp->xsize,
 2614                        (scp->ysize - 1) * scp->xsize * sizeof(u_short));
 2615                 fillw(scp->term.cur_color | scr_map[0x20],
 2616                       scp->scr_buf, scp->xsize);
 2617                 mark_all(scp);
 2618             }
 2619             break;
 2620 #if notyet
 2621         case 'Q':
 2622             scp->term.esc = 4;
 2623             return;
 2624 #endif
 2625         case 'c':   /* Clear screen & home */
 2626             clear_screen(scp);
 2627             break;
 2628 
 2629         case '(':   /* iso-2022: designate 94 character set to G0 */
 2630             scp->term.esc = 5;
 2631             return;
 2632         }
 2633     }
 2634     else if (scp->term.esc == 2) {      /* seen ESC [ */
 2635         if (c >= '' && c <= '9') {
 2636             if (scp->term.num_param < MAX_ESC_PAR) {
 2637             if (scp->term.last_param != scp->term.num_param) {
 2638                 scp->term.last_param = scp->term.num_param;
 2639                 scp->term.param[scp->term.num_param] = 0;
 2640             }
 2641             else
 2642                 scp->term.param[scp->term.num_param] *= 10;
 2643             scp->term.param[scp->term.num_param] += c - '';
 2644             return;
 2645             }
 2646         }
 2647         scp->term.num_param = scp->term.last_param + 1;
 2648         switch (c) {
 2649 
 2650         case ';':
 2651             if (scp->term.num_param < MAX_ESC_PAR)
 2652                 return;
 2653             break;
 2654 
 2655         case '=':
 2656             scp->term.esc = 3;
 2657             scp->term.last_param = -1;
 2658             for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
 2659                 scp->term.param[i] = 1;
 2660             scp->term.num_param = 0;
 2661             return;
 2662 
 2663         case 'A':   /* up n rows */
 2664             n = scp->term.param[0]; if (n < 1) n = 1;
 2665             move_crsr(scp, scp->xpos, scp->ypos - n);
 2666             break;
 2667 
 2668         case 'B':   /* down n rows */
 2669             n = scp->term.param[0]; if (n < 1) n = 1;
 2670             move_crsr(scp, scp->xpos, scp->ypos + n);
 2671             break;
 2672 
 2673         case 'C':   /* right n columns */
 2674             n = scp->term.param[0]; if (n < 1) n = 1;
 2675             move_crsr(scp, scp->xpos + n, scp->ypos);
 2676             break;
 2677 
 2678         case 'D':   /* left n columns */
 2679             n = scp->term.param[0]; if (n < 1) n = 1;
 2680             move_crsr(scp, scp->xpos - n, scp->ypos);
 2681             break;
 2682 
 2683         case 'E':   /* cursor to start of line n lines down */
 2684             n = scp->term.param[0]; if (n < 1) n = 1;
 2685             move_crsr(scp, 0, scp->ypos + n);
 2686             break;
 2687 
 2688         case 'F':   /* cursor to start of line n lines up */
 2689             n = scp->term.param[0]; if (n < 1) n = 1;
 2690             move_crsr(scp, 0, scp->ypos - n);
 2691             break;
 2692 
 2693         case 'f':   /* Cursor move */
 2694         case 'H':
 2695             if (scp->term.num_param == 0)
 2696                 move_crsr(scp, 0, 0);
 2697             else if (scp->term.num_param == 2)
 2698                 move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
 2699             break;
 2700 
 2701         case 'J':   /* Clear all or part of display */
 2702             if (scp->term.num_param == 0)
 2703                 n = 0;
 2704             else
 2705                 n = scp->term.param[0];
 2706             switch (n) {
 2707             case 0: /* clear form cursor to end of display */
 2708                 fillw(scp->term.cur_color | scr_map[0x20],
 2709                       scp->cursor_pos,
 2710                       scp->scr_buf + scp->xsize * scp->ysize - scp->cursor_pos);
 2711                 mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 2712                 mark_for_update(scp, scp->xsize * scp->ysize);
 2713                 remove_cutmarking(scp);
 2714                 break;
 2715             case 1: /* clear from beginning of display to cursor */
 2716                 fillw(scp->term.cur_color | scr_map[0x20],
 2717                       scp->scr_buf,
 2718                       scp->cursor_pos - scp->scr_buf);
 2719                 mark_for_update(scp, 0);
 2720                 mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 2721                 remove_cutmarking(scp);
 2722                 break;
 2723             case 2: /* clear entire display */
 2724                 fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
 2725                       scp->xsize * scp->ysize);
 2726                 mark_all(scp);
 2727                 remove_cutmarking(scp);
 2728                 break;
 2729             }
 2730             break;
 2731 
 2732         case 'K':   /* Clear all or part of line */
 2733             if (scp->term.num_param == 0)
 2734                 n = 0;
 2735             else
 2736                 n = scp->term.param[0];
 2737             switch (n) {
 2738             case 0: /* clear form cursor to end of line */
 2739                 fillw(scp->term.cur_color | scr_map[0x20],
 2740                       scp->cursor_pos,
 2741                       scp->xsize - scp->xpos);
 2742                 mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 2743                 mark_for_update(scp, scp->cursor_pos - scp->scr_buf +
 2744                                 scp->xsize - scp->xpos);
 2745                 break;
 2746             case 1: /* clear from beginning of line to cursor */
 2747                 fillw(scp->term.cur_color | scr_map[0x20],
 2748                       scp->cursor_pos - scp->xpos,
 2749                       scp->xpos + 1);
 2750                 mark_for_update(scp, scp->ypos * scp->xsize);
 2751                 mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 2752                 break;
 2753             case 2: /* clear entire line */
 2754                 fillw(scp->term.cur_color | scr_map[0x20],
 2755                       scp->cursor_pos - scp->xpos,
 2756                       scp->xsize);
 2757                 mark_for_update(scp, scp->ypos * scp->xsize);
 2758                 mark_for_update(scp, (scp->ypos + 1) * scp->xsize);
 2759                 break;
 2760             }
 2761             break;
 2762 
 2763         case 'L':   /* Insert n lines */
 2764             n = scp->term.param[0]; if (n < 1) n = 1;
 2765             if (n > scp->ysize - scp->ypos)
 2766                 n = scp->ysize - scp->ypos;
 2767             src = scp->scr_buf + scp->ypos * scp->xsize;
 2768             dst = src + n * scp->xsize;
 2769             count = scp->ysize - (scp->ypos + n);
 2770             bcopy(src, dst, count * scp->xsize * sizeof(u_short));
 2771             fillw(scp->term.cur_color | scr_map[0x20], src,
 2772                   n * scp->xsize);
 2773             mark_for_update(scp, scp->ypos * scp->xsize);
 2774             mark_for_update(scp, scp->xsize * scp->ysize);
 2775             break;
 2776 
 2777         case 'M':   /* Delete n lines */
 2778             n = scp->term.param[0]; if (n < 1) n = 1;
 2779             if (n > scp->ysize - scp->ypos)
 2780                 n = scp->ysize - scp->ypos;
 2781             dst = scp->scr_buf + scp->ypos * scp->xsize;
 2782             src = dst + n * scp->xsize;
 2783             count = scp->ysize - (scp->ypos + n);
 2784             bcopy(src, dst, count * scp->xsize * sizeof(u_short));
 2785             src = dst + count * scp->xsize;
 2786             fillw(scp->term.cur_color | scr_map[0x20], src,
 2787                   n * scp->xsize);
 2788             mark_for_update(scp, scp->ypos * scp->xsize);
 2789             mark_for_update(scp, scp->xsize * scp->ysize);
 2790             break;
 2791 
 2792         case 'P':   /* Delete n chars */
 2793             n = scp->term.param[0]; if (n < 1) n = 1;
 2794             if (n > scp->xsize - scp->xpos)
 2795                 n = scp->xsize - scp->xpos;
 2796             dst = scp->cursor_pos;
 2797             src = dst + n;
 2798             count = scp->xsize - (scp->xpos + n);
 2799             bcopy(src, dst, count * sizeof(u_short));
 2800             src = dst + count;
 2801             fillw(scp->term.cur_color | scr_map[0x20], src, n);
 2802             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 2803             mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
 2804             break;
 2805 
 2806         case '@':   /* Insert n chars */
 2807             n = scp->term.param[0]; if (n < 1) n = 1;
 2808             if (n > scp->xsize - scp->xpos)
 2809                 n = scp->xsize - scp->xpos;
 2810             src = scp->cursor_pos;
 2811             dst = src + n;
 2812             count = scp->xsize - (scp->xpos + n);
 2813             bcopy(src, dst, count * sizeof(u_short));
 2814             fillw(scp->term.cur_color | scr_map[0x20], src, n);
 2815             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 2816             mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
 2817             break;
 2818 
 2819         case 'S':   /* scroll up n lines */
 2820             n = scp->term.param[0]; if (n < 1)  n = 1;
 2821             if (n > scp->ysize)
 2822                 n = scp->ysize;
 2823             bcopy(scp->scr_buf + (scp->xsize * n),
 2824                    scp->scr_buf,
 2825                    scp->xsize * (scp->ysize - n) * sizeof(u_short));
 2826             fillw(scp->term.cur_color | scr_map[0x20],
 2827                   scp->scr_buf + scp->xsize * (scp->ysize - n),
 2828                   scp->xsize * n);
 2829             mark_all(scp);
 2830             break;
 2831 
 2832         case 'T':   /* scroll down n lines */
 2833             n = scp->term.param[0]; if (n < 1)  n = 1;
 2834             if (n > scp->ysize)
 2835                 n = scp->ysize;
 2836             bcopy(scp->scr_buf,
 2837                   scp->scr_buf + (scp->xsize * n),
 2838                   scp->xsize * (scp->ysize - n) *
 2839                   sizeof(u_short));
 2840             fillw(scp->term.cur_color | scr_map[0x20],
 2841                   scp->scr_buf, scp->xsize * n);
 2842             mark_all(scp);
 2843             break;
 2844 
 2845         case 'X':   /* erase n characters in line */
 2846             n = scp->term.param[0]; if (n < 1)  n = 1;
 2847             if (n > scp->xsize - scp->xpos)
 2848                 n = scp->xsize - scp->xpos;
 2849             fillw(scp->term.cur_color | scr_map[0x20],
 2850                   scp->cursor_pos, n);
 2851             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 2852             mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n);
 2853             break;
 2854 
 2855         case 'Z':   /* move n tabs backwards */
 2856             n = scp->term.param[0]; if (n < 1)  n = 1;
 2857             if ((i = scp->xpos & 0xf8) == scp->xpos)
 2858                 i -= 8*n;
 2859             else
 2860                 i -= 8*(n-1);
 2861             if (i < 0)
 2862                 i = 0;
 2863             move_crsr(scp, i, scp->ypos);
 2864             break;
 2865 
 2866         case '`':   /* move cursor to column n */
 2867             n = scp->term.param[0]; if (n < 1)  n = 1;
 2868             move_crsr(scp, n - 1, scp->ypos);
 2869             break;
 2870 
 2871         case 'a':   /* move cursor n columns to the right */
 2872             n = scp->term.param[0]; if (n < 1)  n = 1;
 2873             move_crsr(scp, scp->xpos + n, scp->ypos);
 2874             break;
 2875 
 2876         case 'd':   /* move cursor to row n */
 2877             n = scp->term.param[0]; if (n < 1)  n = 1;
 2878             move_crsr(scp, scp->xpos, n - 1);
 2879             break;
 2880 
 2881         case 'e':   /* move cursor n rows down */
 2882             n = scp->term.param[0]; if (n < 1)  n = 1;
 2883             move_crsr(scp, scp->xpos, scp->ypos + n);
 2884             break;
 2885 
 2886         case 'm':   /* change attribute */
 2887             if (scp->term.num_param == 0) {
 2888                 scp->term.attr_mask = NORMAL_ATTR;
 2889                 scp->term.cur_attr =
 2890                     scp->term.cur_color = scp->term.std_color;
 2891                 break;
 2892             }
 2893             for (i = 0; i < scp->term.num_param; i++) {
 2894                 switch (n = scp->term.param[i]) {
 2895                 case 0: /* back to normal */
 2896                     scp->term.attr_mask = NORMAL_ATTR;
 2897                     scp->term.cur_attr =
 2898                         scp->term.cur_color = scp->term.std_color;
 2899                     break;
 2900                 case 1: /* bold */
 2901                     scp->term.attr_mask |= BOLD_ATTR;
 2902                     scp->term.cur_attr = mask2attr(&scp->term);
 2903                     break;
 2904                 case 4: /* underline */
 2905                     scp->term.attr_mask |= UNDERLINE_ATTR;
 2906                     scp->term.cur_attr = mask2attr(&scp->term);
 2907                     break;
 2908                 case 5: /* blink */
 2909                     scp->term.attr_mask |= BLINK_ATTR;
 2910                     scp->term.cur_attr = mask2attr(&scp->term);
 2911                     break;
 2912                 case 7: /* reverse video */
 2913                     scp->term.attr_mask |= REVERSE_ATTR;
 2914                     scp->term.cur_attr = mask2attr(&scp->term);
 2915                     break;
 2916                 case 30: case 31: /* set fg color */
 2917                 case 32: case 33: case 34:
 2918                 case 35: case 36: case 37:
 2919                     scp->term.attr_mask |= FOREGROUND_CHANGED;
 2920                     scp->term.cur_color =
 2921                         (scp->term.cur_color&0xF000) | (ansi_col[(n-30)&7]<<8);
 2922                     scp->term.cur_attr = mask2attr(&scp->term);
 2923                     break;
 2924                 case 40: case 41: /* set bg color */
 2925                 case 42: case 43: case 44:
 2926                 case 45: case 46: case 47:
 2927                     scp->term.attr_mask |= BACKGROUND_CHANGED;
 2928                     scp->term.cur_color =
 2929                         (scp->term.cur_color&0x0F00) | (ansi_col[(n-40)&7]<<12);
 2930                     scp->term.cur_attr = mask2attr(&scp->term);
 2931                     break;
 2932                 }
 2933             }
 2934             break;
 2935 
 2936         case 's':   /* Save cursor position */
 2937             scp->saved_xpos = scp->xpos;
 2938             scp->saved_ypos = scp->ypos;
 2939             break;
 2940 
 2941         case 'u':   /* Restore saved cursor position */
 2942             if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
 2943                 move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
 2944             break;
 2945 
 2946         case 'x':
 2947             if (scp->term.num_param == 0)
 2948                 n = 0;
 2949             else
 2950                 n = scp->term.param[0];
 2951             switch (n) {
 2952             case 0:     /* reset attributes */
 2953                 scp->term.attr_mask = NORMAL_ATTR;
 2954                 scp->term.cur_attr =
 2955                     scp->term.cur_color = scp->term.std_color =
 2956                     current_default->std_color;
 2957                 scp->term.rev_color = current_default->rev_color;
 2958                 break;
 2959             case 1:     /* set ansi background */
 2960                 scp->term.attr_mask &= ~BACKGROUND_CHANGED;
 2961                 scp->term.cur_color = scp->term.std_color =
 2962                     (scp->term.std_color & 0x0F00) |
 2963                     (ansi_col[(scp->term.param[1])&0x0F]<<12);
 2964                 scp->term.cur_attr = mask2attr(&scp->term);
 2965                 break;
 2966             case 2:     /* set ansi foreground */
 2967                 scp->term.attr_mask &= ~FOREGROUND_CHANGED;
 2968                 scp->term.cur_color = scp->term.std_color =
 2969                     (scp->term.std_color & 0xF000) |
 2970                     (ansi_col[(scp->term.param[1])&0x0F]<<8);
 2971                 scp->term.cur_attr = mask2attr(&scp->term);
 2972                 break;
 2973             case 3:     /* set ansi attribute directly */
 2974                 scp->term.attr_mask &= ~(FOREGROUND_CHANGED|BACKGROUND_CHANGED);
 2975                 scp->term.cur_color = scp->term.std_color =
 2976                     (scp->term.param[1]&0xFF)<<8;
 2977                 scp->term.cur_attr = mask2attr(&scp->term);
 2978                 break;
 2979             case 5:     /* set ansi reverse video background */
 2980                 scp->term.rev_color =
 2981                     (scp->term.rev_color & 0x0F00) |
 2982                     (ansi_col[(scp->term.param[1])&0x0F]<<12);
 2983                 scp->term.cur_attr = mask2attr(&scp->term);
 2984                 break;
 2985             case 6:     /* set ansi reverse video foreground */
 2986                 scp->term.rev_color =
 2987                     (scp->term.rev_color & 0xF000) |
 2988                     (ansi_col[(scp->term.param[1])&0x0F]<<8);
 2989                 scp->term.cur_attr = mask2attr(&scp->term);
 2990                 break;
 2991             case 7:     /* set ansi reverse video directly */
 2992                 scp->term.rev_color =
 2993                     (scp->term.param[1]&0xFF)<<8;
 2994                 scp->term.cur_attr = mask2attr(&scp->term);
 2995                 break;
 2996             }
 2997             break;
 2998 
 2999         case 'z':   /* switch to (virtual) console n */
 3000             if (scp->term.num_param == 1)
 3001                 switch_scr(scp, scp->term.param[0]);
 3002             break;
 3003         }
 3004     }
 3005     else if (scp->term.esc == 3) {      /* seen ESC [0-9]+ = */
 3006         if (c >= '' && c <= '9') {
 3007             if (scp->term.num_param < MAX_ESC_PAR) {
 3008             if (scp->term.last_param != scp->term.num_param) {
 3009                 scp->term.last_param = scp->term.num_param;
 3010                 scp->term.param[scp->term.num_param] = 0;
 3011             }
 3012             else
 3013                 scp->term.param[scp->term.num_param] *= 10;
 3014             scp->term.param[scp->term.num_param] += c - '';
 3015             return;
 3016             }
 3017         }
 3018         scp->term.num_param = scp->term.last_param + 1;
 3019         switch (c) {
 3020 
 3021         case ';':
 3022             if (scp->term.num_param < MAX_ESC_PAR)
 3023                 return;
 3024             break;
 3025 
 3026         case 'A':   /* set display border color */
 3027             if (scp->term.num_param == 1) {
 3028                 scp->border=scp->term.param[0] & 0xff;
 3029                 if (scp == cur_console)
 3030                     set_border(scp->border);
 3031             }
 3032             break;
 3033 
 3034         case 'B':   /* set bell pitch and duration */
 3035             if (scp->term.num_param == 2) {
 3036                 scp->bell_pitch = scp->term.param[0];
 3037                 scp->bell_duration = scp->term.param[1];
 3038             }
 3039             break;
 3040 
 3041         case 'C':   /* set cursor type & shape */
 3042             if (scp->term.num_param == 1) {
 3043                 if (scp->term.param[0] & 0x01)
 3044                     flags |= BLINK_CURSOR;
 3045                 else
 3046                     flags &= ~BLINK_CURSOR;
 3047                 if ((scp->term.param[0] & 0x02) && crtc_vga)
 3048                     flags |= CHAR_CURSOR;
 3049                 else
 3050                     flags &= ~CHAR_CURSOR;
 3051             }
 3052             else if (scp->term.num_param == 2) {
 3053                 scp->cursor_start = scp->term.param[0] & 0x1F;
 3054                 scp->cursor_end = scp->term.param[1] & 0x1F;
 3055             }
 3056             /* 
 3057              * The cursor shape is global property; all virtual consoles
 3058              * are affected. Update the cursor in the current console...
 3059              */
 3060             if (!(cur_console->status & UNKNOWN_MODE)) {
 3061                 i = spltty();
 3062                 remove_cursor_image(cur_console);
 3063                 if (crtc_vga && (flags & CHAR_CURSOR))
 3064                     set_destructive_cursor(cur_console);
 3065                 draw_cursor_image(cur_console);
 3066                 splx(i);
 3067             }
 3068             break;
 3069 
 3070         case 'F':   /* set ansi foreground */
 3071             if (scp->term.num_param == 1) {
 3072                 scp->term.attr_mask &= ~FOREGROUND_CHANGED;
 3073                 scp->term.cur_color = scp->term.std_color =
 3074                     (scp->term.std_color & 0xF000)
 3075                     | ((scp->term.param[0] & 0x0F) << 8);
 3076                 scp->term.cur_attr = mask2attr(&scp->term);
 3077             }
 3078             break;
 3079 
 3080         case 'G':   /* set ansi background */
 3081             if (scp->term.num_param == 1) {
 3082                 scp->term.attr_mask &= ~BACKGROUND_CHANGED;
 3083                 scp->term.cur_color = scp->term.std_color =
 3084                     (scp->term.std_color & 0x0F00)
 3085                     | ((scp->term.param[0] & 0x0F) << 12);
 3086                 scp->term.cur_attr = mask2attr(&scp->term);
 3087             }
 3088             break;
 3089 
 3090         case 'H':   /* set ansi reverse video foreground */
 3091             if (scp->term.num_param == 1) {
 3092                 scp->term.rev_color =
 3093                     (scp->term.rev_color & 0xF000)
 3094                     | ((scp->term.param[0] & 0x0F) << 8);
 3095                 scp->term.cur_attr = mask2attr(&scp->term);
 3096             }
 3097             break;
 3098 
 3099         case 'I':   /* set ansi reverse video background */
 3100             if (scp->term.num_param == 1) {
 3101                 scp->term.rev_color =
 3102                     (scp->term.rev_color & 0x0F00)
 3103                     | ((scp->term.param[0] & 0x0F) << 12);
 3104                 scp->term.cur_attr = mask2attr(&scp->term);
 3105             }
 3106             break;
 3107         }
 3108     }
 3109 #if notyet
 3110     else if (scp->term.esc == 4) {      /* seen ESC Q */
 3111         /* to be filled */
 3112     }
 3113 #endif
 3114     else if (scp->term.esc == 5) {      /* seen ESC ( */
 3115         switch (c) {
 3116         case 'B':   /* iso-2022: desginate ASCII into G0 */
 3117             break;
 3118         /* other items to be filled */
 3119         default:
 3120             break;
 3121         }
 3122     }
 3123     scp->term.esc = 0;
 3124 }
 3125 
 3126 static void
 3127 ansi_put(scr_stat *scp, u_char *buf, int len)
 3128 {
 3129     u_char *ptr = buf;
 3130 
 3131     /* make screensaver happy */
 3132     if (scp == cur_console)
 3133         scrn_time_stamp = mono_time.tv_sec;
 3134 
 3135     write_in_progress++;
 3136 outloop:
 3137     if (scp->term.esc) {
 3138         scan_esc(scp, *ptr++);
 3139         len--;
 3140     }
 3141     else if (PRINTABLE(*ptr)) {     /* Print only printables */
 3142         int cnt = len <= (scp->xsize-scp->xpos) ? len : (scp->xsize-scp->xpos);
 3143         u_short cur_attr = scp->term.cur_attr;
 3144         u_short *cursor_pos = scp->cursor_pos;
 3145         do {
 3146             /*
 3147              * gcc-2.6.3 generates poor (un)sign extension code.  Casting the
 3148              * pointers in the following to volatile should have no effect,
 3149              * but in fact speeds up this inner loop from 26 to 18 cycles
 3150              * (+ cache misses) on i486's.
 3151              */
 3152 #define UCVP(ucp)       ((u_char volatile *)(ucp))
 3153             *cursor_pos++ = UCVP(scr_map)[*UCVP(ptr)] | cur_attr;
 3154             ptr++;
 3155             cnt--;
 3156         } while (cnt && PRINTABLE(*ptr));
 3157         len -= (cursor_pos - scp->cursor_pos);
 3158         scp->xpos += (cursor_pos - scp->cursor_pos);
 3159         mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3160         mark_for_update(scp, cursor_pos - scp->scr_buf);
 3161         scp->cursor_pos = cursor_pos;
 3162         if (scp->xpos >= scp->xsize) {
 3163             scp->xpos = 0;
 3164             scp->ypos++;
 3165         }
 3166     }
 3167     else  {
 3168         switch(*ptr) {
 3169         case 0x07:
 3170             do_bell(scp, scp->bell_pitch, scp->bell_duration);
 3171             break;
 3172 
 3173         case 0x08:      /* non-destructive backspace */
 3174             if (scp->cursor_pos > scp->scr_buf) {
 3175                 mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3176                 scp->cursor_pos--;
 3177                 mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3178                 if (scp->xpos > 0)
 3179                     scp->xpos--;
 3180                 else {
 3181                     scp->xpos += scp->xsize - 1;
 3182                     scp->ypos--;
 3183                 }
 3184             }
 3185             break;
 3186 
 3187         case 0x09:  /* non-destructive tab */
 3188             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3189             scp->cursor_pos += (8 - scp->xpos % 8u);
 3190             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3191             if ((scp->xpos += (8 - scp->xpos % 8u)) >= scp->xsize) {
 3192                 scp->xpos = 0;
 3193                 scp->ypos++;
 3194             }
 3195             break;
 3196 
 3197         case 0x0a:  /* newline, same pos */
 3198             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3199             scp->cursor_pos += scp->xsize;
 3200             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3201             scp->ypos++;
 3202             break;
 3203 
 3204         case 0x0c:  /* form feed, clears screen */
 3205             clear_screen(scp);
 3206             break;
 3207 
 3208         case 0x0d:  /* return, return to pos 0 */
 3209             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3210             scp->cursor_pos -= scp->xpos;
 3211             mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
 3212             scp->xpos = 0;
 3213             break;
 3214 
 3215         case 0x1b:  /* start escape sequence */
 3216             scp->term.esc = 1;
 3217             scp->term.num_param = 0;
 3218             break;
 3219         }
 3220         ptr++; len--;
 3221     }
 3222     /* do we have to scroll ?? */
 3223     if (scp->cursor_pos >= scp->scr_buf + scp->ysize * scp->xsize) {
 3224         remove_cutmarking(scp);
 3225         if (scp->history) {
 3226             bcopy(scp->scr_buf, scp->history_head,
 3227                    scp->xsize * sizeof(u_short));
 3228             scp->history_head += scp->xsize;
 3229             if (scp->history_head + scp->xsize >
 3230                 scp->history + scp->history_size)
 3231                 scp->history_head = scp->history;
 3232         }
 3233         bcopy(scp->scr_buf + scp->xsize, scp->scr_buf,
 3234                scp->xsize * (scp->ysize - 1) * sizeof(u_short));
 3235         fillw(scp->term.cur_color | scr_map[0x20],
 3236               scp->scr_buf + scp->xsize * (scp->ysize - 1),
 3237               scp->xsize);
 3238         scp->cursor_pos -= scp->xsize;
 3239         scp->ypos--;
 3240         mark_all(scp);
 3241     }
 3242     if (len)
 3243         goto outloop;
 3244     write_in_progress--;
 3245     if (delayed_next_scr)
 3246         switch_scr(scp, delayed_next_scr - 1);
 3247 }
 3248 
 3249 static void
 3250 scinit(void)
 3251 {
 3252     u_int hw_cursor;
 3253     u_int i;
 3254 
 3255     if (init_done != COLD)
 3256         return;
 3257     init_done = WARM;
 3258 
 3259     /*
 3260      * Ensure a zero start address.  This is mainly to recover after
 3261      * switching from pcvt using userconfig().  The registers are w/o
 3262      * for old hardware so it's too hard to relocate the active screen
 3263      * memory.
 3264      */
 3265     outb(crtc_addr, 12);
 3266     outb(crtc_addr + 1, 0);
 3267     outb(crtc_addr, 13);
 3268     outb(crtc_addr + 1, 0);
 3269 
 3270     /* extract cursor location */
 3271     outb(crtc_addr, 14);
 3272     hw_cursor = inb(crtc_addr + 1) << 8;
 3273     outb(crtc_addr, 15);
 3274     hw_cursor |= inb(crtc_addr + 1);
 3275 
 3276     /*
 3277      * Validate cursor location.  It may be off the screen.  Then we must
 3278      * not use it for the initial buffer offset.
 3279      */
 3280     if (hw_cursor >= ROW * COL)
 3281         hw_cursor = (ROW - 1) * COL;
 3282 
 3283     /* move hardware cursor out of the way */
 3284     outb(crtc_addr, 14);
 3285     outb(crtc_addr + 1, 0xff);
 3286     outb(crtc_addr, 15);
 3287     outb(crtc_addr + 1, 0xff);
 3288 
 3289     /* set up the first console */
 3290     current_default = &user_default;
 3291     console[0] = &main_console;
 3292     init_scp(console[0]);
 3293     cur_console = console[0];
 3294 
 3295     /* discard the video mode table if we are not familiar with it... */
 3296     if (video_mode_ptr) {
 3297         bzero(mode_map, sizeof(mode_map));
 3298         bcopy(video_mode_ptr + MODE_PARAM_SIZE*console[0]->mode, 
 3299               vgaregs2, sizeof(vgaregs2));
 3300         switch (comp_vgaregs(vgaregs, video_mode_ptr 
 3301                     + MODE_PARAM_SIZE*console[0]->mode)) {
 3302         case COMP_IDENTICAL:
 3303             map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
 3304             /* 
 3305              * This is a kludge for Toshiba DynaBook SS433 whose BIOS video
 3306              * mode table entry has the actual # of rows at the offset 1; 
 3307              * BIOSes from other manufacturers store the # of rows - 1 there. 
 3308              * XXX
 3309              */
 3310             rows_offset = vgaregs[1] + 1 
 3311                 - video_mode_ptr[MODE_PARAM_SIZE*console[0]->mode + 1];
 3312             break;
 3313         case COMP_SIMILAR:
 3314             map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
 3315             mode_map[console[0]->mode] = vgaregs;
 3316             rows_offset = vgaregs[1] + 1 
 3317                 - video_mode_ptr[MODE_PARAM_SIZE*console[0]->mode + 1];
 3318             vgaregs[1] -= rows_offset - 1;
 3319             break;
 3320         case COMP_DIFFERENT:
 3321         default:
 3322             video_mode_ptr = NULL;
 3323             mode_map[console[0]->mode] = vgaregs;
 3324             rows_offset = 1;
 3325             break;
 3326         }
 3327     }
 3328 
 3329     /* copy screen to temporary buffer */
 3330     sc_bcopy(Crtat, sc_buffer,
 3331            console[0]->xsize * console[0]->ysize * sizeof(u_short));
 3332 
 3333     console[0]->scr_buf = console[0]->mouse_pos = console[0]->mouse_oldpos
 3334         = sc_buffer;
 3335     console[0]->cursor_pos = console[0]->cursor_oldpos = sc_buffer + hw_cursor;
 3336     console[0]->cursor_saveunder = *console[0]->cursor_pos;
 3337     console[0]->xpos = hw_cursor % COL;
 3338     console[0]->ypos = hw_cursor / COL;
 3339     for (i=1; i<MAXCONS; i++)
 3340         console[i] = NULL;
 3341     kernel_console.esc = 0;
 3342     kernel_console.attr_mask = NORMAL_ATTR;
 3343     kernel_console.cur_attr =
 3344         kernel_console.cur_color = kernel_console.std_color =
 3345         kernel_default.std_color;
 3346     kernel_console.rev_color = kernel_default.rev_color;
 3347 
 3348     /* initialize mapscrn arrays to a one to one map */
 3349     for (i=0; i<sizeof(scr_map); i++) {
 3350         scr_map[i] = scr_rmap[i] = i;
 3351     }
 3352 
 3353     /* Save font and palette if VGA */
 3354     if (crtc_vga) {
 3355         copy_font(SAVE, FONT_16, font_16);
 3356         fonts_loaded = FONT_16;
 3357         save_palette();
 3358         set_destructive_cursor(console[0]);
 3359     }
 3360 
 3361 #ifdef SC_SPLASH_SCREEN
 3362     /* 
 3363      * Now put up a graphics image, and maybe cycle a
 3364      * couble of palette entries for simple animation.
 3365      */
 3366     toggle_splash_screen(cur_console);
 3367 #endif
 3368 }
 3369 
 3370 static void
 3371 scshutdown(int howto, void *arg)
 3372 {
 3373     scrn_time_stamp = mono_time.tv_sec;
 3374     if (!cold && cur_console->smode.mode == VT_AUTO 
 3375         && console[0]->smode.mode == VT_AUTO)
 3376         switch_scr(cur_console, 0);
 3377     shutdown_in_progress = TRUE;
 3378 }
 3379 
 3380 static void
 3381 map_mode_table(char *map[], char *table, int max)
 3382 {
 3383     int i;
 3384 
 3385     for(i = 0; i < max; ++i)
 3386         map[i] = table + i*MODE_PARAM_SIZE;
 3387     for(; i < MODE_MAP_SIZE; ++i)
 3388         map[i] = NULL;
 3389 }
 3390 
 3391 static u_char
 3392 map_mode_num(u_char mode)
 3393 {
 3394     static struct {
 3395         u_char from;
 3396         u_char to;
 3397     } mode_map[] = {
 3398         { M_ENH_B80x43, M_ENH_B80x25 },
 3399         { M_ENH_C80x43, M_ENH_C80x25 },
 3400         { M_VGA_M80x30, M_VGA_M80x25 },
 3401         { M_VGA_C80x30, M_VGA_C80x25 },
 3402         { M_VGA_M80x50, M_VGA_M80x25 },
 3403         { M_VGA_C80x50, M_VGA_C80x25 },
 3404         { M_VGA_M80x60, M_VGA_M80x25 },
 3405         { M_VGA_C80x60, M_VGA_C80x25 },
 3406         { M_VGA_MODEX,  M_VGA_CG320 },
 3407     };
 3408     int i;
 3409 
 3410     for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
 3411         if (mode_map[i].from == mode)
 3412             return mode_map[i].to;
 3413     }
 3414     return mode;
 3415 }
 3416 
 3417 static char 
 3418 *get_mode_param(scr_stat *scp, u_char mode)
 3419 {
 3420     if (mode >= MODE_MAP_SIZE)
 3421         mode = map_mode_num(mode);
 3422     if (mode < MODE_MAP_SIZE)
 3423         return mode_map[mode];
 3424     else
 3425         return NULL;
 3426 }
 3427 
 3428 static scr_stat
 3429 *alloc_scp()
 3430 {
 3431     scr_stat *scp;
 3432 
 3433     scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
 3434     init_scp(scp);
 3435     scp->scr_buf = scp->cursor_pos = scp->cursor_oldpos =
 3436         (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
 3437                           M_DEVBUF, M_WAITOK);
 3438     scp->mouse_pos = scp->mouse_oldpos = 
 3439         scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
 3440                         scp->mouse_xpos/8);
 3441     scp->history_head = scp->history_pos =
 3442         (u_short *)malloc(scp->history_size*sizeof(u_short),
 3443                           M_DEVBUF, M_WAITOK);
 3444     bzero(scp->history_head, scp->history_size*sizeof(u_short));
 3445     scp->history = scp->history_head;
 3446 /* SOS
 3447     if (crtc_vga && video_mode_ptr)
 3448         set_mode(scp);
 3449 */
 3450     clear_screen(scp);
 3451     scp->cursor_saveunder = *scp->cursor_pos;
 3452     return scp;
 3453 }
 3454 
 3455 static void
 3456 init_scp(scr_stat *scp)
 3457 {
 3458     switch(crtc_type) {
 3459     case KD_VGA:
 3460         if (crtc_addr == MONO_BASE)
 3461             scp->mode = M_VGA_M80x25;
 3462         else
 3463             scp->mode = M_VGA_C80x25;
 3464         scp->font_size = 16;
 3465         break;
 3466     case KD_CGA:
 3467         if (crtc_addr == MONO_BASE)
 3468             scp->mode = M_B80x25;
 3469         else
 3470             scp->mode = M_C80x25;
 3471         scp->font_size = 8;
 3472         break;
 3473     case KD_EGA:
 3474         if (crtc_addr == MONO_BASE)
 3475             scp->mode = M_B80x25;
 3476         else
 3477             scp->mode = M_C80x25;
 3478         scp->font_size = 14;
 3479         break;
 3480     case KD_MONO:
 3481     case KD_HERCULES:
 3482     default:
 3483         scp->mode = M_EGAMONO80x25;
 3484         scp->font_size = 14;
 3485         break;
 3486     }
 3487     scp->initial_mode = scp->mode;
 3488 
 3489     scp->xsize = COL;
 3490     scp->ysize = ROW;
 3491     scp->xpixel = scp->xsize * 8;
 3492     scp->ypixel = scp->ysize * scp->font_size;
 3493     scp->xpos = scp->ypos = 0;
 3494     scp->saved_xpos = scp->saved_ypos = -1;
 3495     scp->start = scp->xsize * scp->ysize;
 3496     scp->end = 0;
 3497     scp->term.esc = 0;
 3498     scp->term.attr_mask = NORMAL_ATTR;
 3499     scp->term.cur_attr =
 3500         scp->term.cur_color = scp->term.std_color =
 3501         current_default->std_color;
 3502     scp->term.rev_color = current_default->rev_color;
 3503     scp->border = BG_BLACK;
 3504     scp->cursor_start = *(char *)pa_to_va(0x461);
 3505     scp->cursor_end = *(char *)pa_to_va(0x460);
 3506     scp->mouse_xpos = scp->xsize*8/2;
 3507     scp->mouse_ypos = scp->ysize*scp->font_size/2;
 3508     scp->mouse_cut_start = scp->mouse_cut_end = NULL;
 3509     scp->mouse_signal = 0;
 3510     scp->mouse_pid = 0;
 3511     scp->mouse_proc = NULL;
 3512     scp->bell_pitch = BELL_PITCH;
 3513     scp->bell_duration = BELL_DURATION;
 3514     scp->status = (*(char *)pa_to_va(0x417) & 0x20) ? NLKED : 0;
 3515     scp->status |= CURSOR_ENABLED;
 3516     scp->pid = 0;
 3517     scp->proc = NULL;
 3518     scp->smode.mode = VT_AUTO;
 3519     scp->history_head = scp->history_pos = scp->history = NULL;
 3520     scp->history_size = imax(SC_HISTORY_SIZE, scp->ysize) * scp->xsize;
 3521 }
 3522 
 3523 static u_char
 3524 *get_fstr(u_int c, u_int *len)
 3525 {
 3526     u_int i;
 3527 
 3528     if (!(c & FKEY))
 3529         return(NULL);
 3530     i = (c & 0xFF) - F_FN;
 3531     if (i > n_fkey_tab)
 3532         return(NULL);
 3533     *len = fkey_tab[i].len;
 3534     return(fkey_tab[i].str);
 3535 }
 3536 
 3537 static void
 3538 history_to_screen(scr_stat *scp)
 3539 {
 3540     int i;
 3541 
 3542     for (i=0; i<scp->ysize; i++)
 3543         bcopy(scp->history + (((scp->history_pos - scp->history) +
 3544                scp->history_size-((i+1)*scp->xsize))%scp->history_size),
 3545                scp->scr_buf + (scp->xsize * (scp->ysize-1 - i)),
 3546                scp->xsize * sizeof(u_short));
 3547     mark_all(scp);
 3548 }
 3549 
 3550 static int
 3551 history_up_line(scr_stat *scp)
 3552 {
 3553     if (WRAPHIST(scp, scp->history_pos, -(scp->xsize*scp->ysize)) !=
 3554         scp->history_head) {
 3555         scp->history_pos = WRAPHIST(scp, scp->history_pos, -scp->xsize);
 3556         history_to_screen(scp);
 3557         return 0;
 3558     }
 3559     else
 3560         return -1;
 3561 }
 3562 
 3563 static int
 3564 history_down_line(scr_stat *scp)
 3565 {
 3566     if (scp->history_pos != scp->history_head) {
 3567         scp->history_pos = WRAPHIST(scp, scp->history_pos, scp->xsize);
 3568         history_to_screen(scp);
 3569         return 0;
 3570     }
 3571     else
 3572         return -1;
 3573 }
 3574 
 3575 /*
 3576  * scgetc(flags) - get character from keyboard.
 3577  * If flags & SCGETC_CN, then avoid harmful side effects.
 3578  * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
 3579  * return NOKEY if there is nothing there.
 3580  */
 3581 static u_int
 3582 scgetc(u_int flags)
 3583 {
 3584     struct key_t *key;
 3585     u_char scancode, keycode;
 3586     u_int state, action;
 3587     int c;
 3588     static u_char esc_flag = 0, compose = 0;
 3589     static u_int chr = 0;
 3590 
 3591 next_code:
 3592     /* first see if there is something in the keyboard port */
 3593     if (flags & SCGETC_NONBLOCK) {
 3594         c = read_kbd_data_no_wait(sc_kbdc);
 3595         if (c == -1)
 3596             return(NOKEY);
 3597     } else {
 3598         do {
 3599             c = read_kbd_data(sc_kbdc);
 3600         } while(c == -1);
 3601     }
 3602     scancode = (u_char)c;
 3603 
 3604     /* make screensaver happy */
 3605     if (!(scancode & 0x80))
 3606         scrn_time_stamp = mono_time.tv_sec;
 3607 
 3608     if (!(flags & SCGETC_CN)) {
 3609         /* do the /dev/random device a favour */
 3610         add_keyboard_randomness(scancode);
 3611 
 3612         if (cur_console->status & KBD_RAW_MODE)
 3613             return scancode;
 3614     }
 3615 
 3616     keycode = scancode & 0x7F;
 3617     switch (esc_flag) {
 3618     case 0x00:      /* normal scancode */
 3619         switch(scancode) {
 3620         case 0xB8:  /* left alt (compose key) */
 3621             if (compose) {
 3622                 compose = 0;
 3623                 if (chr > 255) {
 3624                     do_bell(cur_console,
 3625                         BELL_PITCH, BELL_DURATION);
 3626                     chr = 0;
 3627                 }
 3628             }
 3629             break;
 3630         case 0x38:
 3631             if (!compose) {
 3632                 compose = 1;
 3633                 chr = 0;
 3634             }
 3635             break;
 3636         case 0xE0:
 3637         case 0xE1:
 3638             esc_flag = scancode;
 3639             goto next_code;
 3640         }
 3641         break;
 3642     case 0xE0:      /* 0xE0 prefix */
 3643         esc_flag = 0;
 3644         switch (keycode) {
 3645         case 0x1C:  /* right enter key */
 3646             keycode = 0x59;
 3647             break;
 3648         case 0x1D:  /* right ctrl key */
 3649             keycode = 0x5A;
 3650             break;
 3651         case 0x35:  /* keypad divide key */
 3652             keycode = 0x5B;
 3653             break;
 3654         case 0x37:  /* print scrn key */
 3655             keycode = 0x5C;
 3656             break;
 3657         case 0x38:  /* right alt key (alt gr) */
 3658             keycode = 0x5D;
 3659             break;
 3660         case 0x47:  /* grey home key */
 3661             keycode = 0x5E;
 3662             break;
 3663         case 0x48:  /* grey up arrow key */
 3664             keycode = 0x5F;
 3665             break;
 3666         case 0x49:  /* grey page up key */
 3667             keycode = 0x60;
 3668             break;
 3669         case 0x4B:  /* grey left arrow key */
 3670             keycode = 0x61;
 3671             break;
 3672         case 0x4D:  /* grey right arrow key */
 3673             keycode = 0x62;
 3674             break;
 3675         case 0x4F:  /* grey end key */
 3676             keycode = 0x63;
 3677             break;
 3678         case 0x50:  /* grey down arrow key */
 3679             keycode = 0x64;
 3680             break;
 3681         case 0x51:  /* grey page down key */
 3682             keycode = 0x65;
 3683             break;
 3684         case 0x52:  /* grey insert key */
 3685             keycode = 0x66;
 3686             break;
 3687         case 0x53:  /* grey delete key */
 3688             keycode = 0x67;
 3689             break;
 3690 
 3691         /* the following 3 are only used on the MS "Natural" keyboard */
 3692         case 0x5b:  /* left Window key */
 3693             keycode = 0x69;
 3694             break;
 3695         case 0x5c:  /* right Window key */
 3696             keycode = 0x6a;
 3697             break;
 3698         case 0x5d:  /* menu key */
 3699             keycode = 0x6b;
 3700             break;
 3701         default:    /* ignore everything else */
 3702             goto next_code;
 3703         }
 3704         break;
 3705     case 0xE1:      /* 0xE1 prefix */
 3706         esc_flag = 0;
 3707         if (keycode == 0x1D)
 3708             esc_flag = 0x1D;
 3709         goto next_code;
 3710         /* NOT REACHED */
 3711     case 0x1D:      /* pause / break */
 3712         esc_flag = 0;
 3713         if (keycode != 0x45)
 3714             goto next_code;
 3715         keycode = 0x68;
 3716         break;
 3717     }
 3718 
 3719     if (!(flags & SCGETC_CN) && (cur_console->status & KBD_CODE_MODE))
 3720         return (keycode | (scancode & 0x80));
 3721 
 3722     /* if scroll-lock pressed allow history browsing */
 3723     if (cur_console->history && cur_console->status & SLKED) {
 3724         int i;
 3725 
 3726         cur_console->status &= ~CURSOR_ENABLED;
 3727         if (!(cur_console->status & BUFFER_SAVED)) {
 3728             cur_console->status |= BUFFER_SAVED;
 3729             cur_console->history_save = cur_console->history_head;
 3730 
 3731             /* copy screen into top of history buffer */
 3732             for (i=0; i<cur_console->ysize; i++) {
 3733                 bcopy(cur_console->scr_buf + (cur_console->xsize * i),
 3734                        cur_console->history_head,
 3735                        cur_console->xsize * sizeof(u_short));
 3736                 cur_console->history_head += cur_console->xsize;
 3737                 if (cur_console->history_head + cur_console->xsize >
 3738                     cur_console->history + cur_console->history_size)
 3739                     cur_console->history_head=cur_console->history;
 3740             }
 3741             cur_console->history_pos = cur_console->history_head;
 3742             history_to_screen(cur_console);
 3743         }
 3744         switch (scancode) {
 3745         case 0x47:  /* home key */
 3746             cur_console->history_pos = cur_console->history_head;
 3747             history_to_screen(cur_console);
 3748             goto next_code;
 3749 
 3750         case 0x4F:  /* end key */
 3751             cur_console->history_pos =
 3752                 WRAPHIST(cur_console, cur_console->history_head,
 3753                          cur_console->xsize*cur_console->ysize);
 3754             history_to_screen(cur_console);
 3755             goto next_code;
 3756 
 3757         case 0x48:  /* up arrow key */
 3758             if (history_up_line(cur_console))
 3759                 do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 3760             goto next_code;
 3761 
 3762         case 0x50:  /* down arrow key */
 3763             if (history_down_line(cur_console))
 3764                 do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 3765             goto next_code;
 3766 
 3767         case 0x49:  /* page up key */
 3768             for (i=0; i<cur_console->ysize; i++)
 3769             if (history_up_line(cur_console)) {
 3770                 do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 3771                 break;
 3772             }
 3773             goto next_code;
 3774 
 3775         case 0x51:  /* page down key */
 3776             for (i=0; i<cur_console->ysize; i++)
 3777             if (history_down_line(cur_console)) {
 3778                 do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 3779                 break;
 3780             }
 3781             goto next_code;
 3782         }
 3783     }
 3784 
 3785     if (compose) {
 3786         switch (scancode) {
 3787         /* key pressed process it */
 3788         case 0x47: case 0x48: case 0x49:    /* keypad 7,8,9 */
 3789             chr = (scancode - 0x40) + chr*10;
 3790             goto next_code;
 3791         case 0x4B: case 0x4C: case 0x4D:    /* keypad 4,5,6 */
 3792             chr = (scancode - 0x47) + chr*10;
 3793             goto next_code;
 3794         case 0x4F: case 0x50: case 0x51:    /* keypad 1,2,3 */
 3795             chr = (scancode - 0x4E) + chr*10;
 3796             goto next_code;
 3797         case 0x52:              /* keypad 0 */
 3798             chr *= 10;
 3799             goto next_code;
 3800 
 3801         /* key release, no interest here */
 3802         case 0xC7: case 0xC8: case 0xC9:    /* keypad 7,8,9 */
 3803         case 0xCB: case 0xCC: case 0xCD:    /* keypad 4,5,6 */
 3804         case 0xCF: case 0xD0: case 0xD1:    /* keypad 1,2,3 */
 3805         case 0xD2:              /* keypad 0 */
 3806             goto next_code;
 3807 
 3808         case 0x38:              /* left alt key */
 3809             break;
 3810         default:
 3811             if (chr) {
 3812                 compose = chr = 0;
 3813                 do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 3814                 goto next_code;
 3815             }
 3816             break;
 3817         }
 3818     }
 3819 
 3820     state = (shfts ? 1 : 0 ) | (2 * (ctls ? 1 : 0)) | (4 * (alts ? 1 : 0));
 3821     if ((!agrs && (cur_console->status & ALKED))
 3822         || (agrs && !(cur_console->status & ALKED)))
 3823         keycode += ALTGR_OFFSET;
 3824     key = &key_map.key[keycode];
 3825     if ( ((key->flgs & FLAG_LOCK_C) && (cur_console->status & CLKED))
 3826          || ((key->flgs & FLAG_LOCK_N) && (cur_console->status & NLKED)) )
 3827         state ^= 1;
 3828 
 3829     /* Check for make/break */
 3830     action = key->map[state];
 3831     if (scancode & 0x80) {      /* key released */
 3832         if (key->spcl & (0x80>>state)) {
 3833             switch (action) {
 3834             case LSH:
 3835                 shfts &= ~1;
 3836                 break;
 3837             case RSH:
 3838                 shfts &= ~2;
 3839                 break;
 3840             case LCTR:
 3841                 ctls &= ~1;
 3842                 break;
 3843             case RCTR:
 3844                 ctls &= ~2;
 3845                 break;
 3846             case LALT:
 3847                 alts &= ~1;
 3848                 break;
 3849             case RALT:
 3850                 alts &= ~2;
 3851                 break;
 3852             case NLK:
 3853                 nlkcnt = 0;
 3854                 break;
 3855             case CLK:
 3856                 clkcnt = 0;
 3857                 break;
 3858             case SLK:
 3859                 slkcnt = 0;
 3860                 break;
 3861             case ASH:
 3862                 agrs = 0;
 3863                 break;
 3864             case ALK:
 3865                 alkcnt = 0;
 3866                 break;
 3867             case META:
 3868                 metas = 0;
 3869                 break;
 3870             }
 3871         }
 3872         if (chr && !compose) {
 3873             action = chr;
 3874             chr = 0;
 3875             return(action);
 3876         }
 3877     } else {
 3878         /* key pressed */
 3879         if (key->spcl & (0x80>>state)) {
 3880             switch (action) {
 3881             /* LOCKING KEYS */
 3882             case NLK:
 3883 #ifdef SC_SPLASH_SCREEN
 3884                 toggle_splash_screen(cur_console); /* SOS XXX */
 3885 #endif
 3886                 if (!nlkcnt) {
 3887                     nlkcnt++;
 3888                     if (cur_console->status & NLKED)
 3889                         cur_console->status &= ~NLKED;
 3890                     else
 3891                         cur_console->status |= NLKED;
 3892                     update_leds(cur_console->status);
 3893                 }
 3894                 break;
 3895             case CLK:
 3896                 if (!clkcnt) {
 3897                     clkcnt++;
 3898                     if (cur_console->status & CLKED)
 3899                         cur_console->status &= ~CLKED;
 3900                     else
 3901                         cur_console->status |= CLKED;
 3902                     update_leds(cur_console->status);
 3903                 }
 3904                 break;
 3905             case SLK:
 3906                 if (!slkcnt) {
 3907                     slkcnt++;
 3908                     if (cur_console->status & SLKED) {
 3909                         cur_console->status &= ~SLKED;
 3910                         if (cur_console->status & BUFFER_SAVED){
 3911                             int i;
 3912                             u_short *ptr = cur_console->history_save;
 3913 
 3914                             for (i=0; i<cur_console->ysize; i++) {
 3915                                 bcopy(ptr,
 3916                                        cur_console->scr_buf +
 3917                                        (cur_console->xsize*i),
 3918                                        cur_console->xsize * sizeof(u_short));
 3919                                 ptr += cur_console->xsize;
 3920                                 if (ptr + cur_console->xsize >
 3921                                     cur_console->history +
 3922                                     cur_console->history_size)
 3923                                     ptr = cur_console->history;
 3924                             }
 3925                             cur_console->status &= ~BUFFER_SAVED;
 3926                             cur_console->history_head=cur_console->history_save;
 3927                             cur_console->status |= CURSOR_ENABLED;
 3928                             mark_all(cur_console);
 3929                         }
 3930                         scstart(VIRTUAL_TTY(get_scr_num()));
 3931                     }
 3932                     else
 3933                         cur_console->status |= SLKED;
 3934                     update_leds(cur_console->status);
 3935                 }
 3936                 break;
 3937             case ALK:
 3938                 if (!alkcnt) {
 3939                     alkcnt++;
 3940                     if (cur_console->status & ALKED)
 3941                         cur_console->status &= ~ALKED;
 3942                     else
 3943                         cur_console->status |= ALKED;
 3944                     update_leds(cur_console->status);
 3945                 }
 3946                 break;
 3947 
 3948             /* NON-LOCKING KEYS */
 3949             case NOP:
 3950                 break;
 3951             case SPSC:
 3952 #ifdef SC_SPLASH_SCREEN
 3953                 accents = 0;
 3954                 toggle_splash_screen(cur_console);
 3955 #endif
 3956                 break;
 3957             case RBT:
 3958 #ifndef SC_DISABLE_REBOOT
 3959                 accents = 0;
 3960                 shutdown_nice();
 3961 #endif
 3962                 break;
 3963             case SUSP:
 3964 #if NAPM > 0
 3965                 accents = 0;
 3966                 apm_suspend();
 3967 #endif
 3968                 break;
 3969 
 3970             case DBG:
 3971 #ifdef DDB          /* try to switch to console 0 */
 3972                 accents = 0;
 3973                 if (cur_console->smode.mode == VT_AUTO &&
 3974                     console[0]->smode.mode == VT_AUTO)
 3975                     switch_scr(cur_console, 0);
 3976                 Debugger("manual escape to debugger");
 3977 #else
 3978                 printf("No debugger in kernel\n");
 3979 #endif
 3980                 break;
 3981             case LSH:
 3982                 shfts |= 1;
 3983                 break;
 3984             case RSH:
 3985                 shfts |= 2;
 3986                 break;
 3987             case LCTR:
 3988                 ctls |= 1;
 3989                 break;
 3990             case RCTR:
 3991                 ctls |= 2;
 3992                 break;
 3993             case LALT:
 3994                 alts |= 1;
 3995                 break;
 3996             case RALT:
 3997                 alts |= 2;
 3998                 break;
 3999             case ASH:
 4000                 agrs = 1;
 4001                 break;
 4002             case META:
 4003                 metas = 1;
 4004                 break;
 4005             case NEXT:
 4006                 {
 4007                 int next, this = get_scr_num();
 4008                 accents = 0;
 4009                 for (next = this+1; next != this; next = (next+1)%MAXCONS) {
 4010                     struct tty *tp = VIRTUAL_TTY(next);
 4011                     if (tp->t_state & TS_ISOPEN) {
 4012                         switch_scr(cur_console, next);
 4013                         break;
 4014                     }
 4015                 }
 4016                 }
 4017                 break;
 4018             case BTAB:
 4019                 accents = 0;
 4020                 return(BKEY);
 4021             default:
 4022                 if (action >= F_ACC && action <= L_ACC) {
 4023                     /* turn it into an index */
 4024                     action -= F_ACC - 1;
 4025                     if ((action > accent_map.n_accs) 
 4026                         || (accent_map.acc[action - 1].accchar == 0)) {
 4027                         /* 
 4028                          * The index is out of range or pointing to an 
 4029                          * empty entry.
 4030                          */
 4031                         accents = 0;
 4032                         do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 4033                     }
 4034                     /* 
 4035                      * If the same accent key has been hit twice,
 4036                      * produce the accent char itself.
 4037                      */
 4038                     if (action == accents) {
 4039                         action = accent_map.acc[accents - 1].accchar;
 4040                         accents = 0;
 4041                         if (metas)
 4042                             action |= MKEY;
 4043                         return (action);
 4044                     }
 4045                     /* remember the index and wait for the next key stroke */
 4046                     accents = action; 
 4047                     break;
 4048                 }
 4049                 if (accents > 0) {
 4050                     accents = 0;
 4051                     do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 4052                 }
 4053                 if (action >= F_SCR && action <= L_SCR) {
 4054                     switch_scr(cur_console, action - F_SCR);
 4055                     break;
 4056                 }
 4057                 if (action >= F_FN && action <= L_FN)
 4058                     action |= FKEY;
 4059                 return(action);
 4060             }
 4061         }
 4062         else {
 4063             if (accents) {
 4064                 struct acc_t *acc;
 4065                 int i;
 4066 
 4067                 acc = &accent_map.acc[accents - 1];
 4068                 accents = 0;
 4069                 /* 
 4070                  * If the accent key is followed by the space key,
 4071                  * produce the accent char itself.
 4072                  */
 4073                 if (action == ' ') {
 4074                     action = acc->accchar;
 4075                     if (metas)
 4076                         action |= MKEY;
 4077                     return (action);
 4078                 }
 4079                 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
 4080                     if (acc->map[i][0] == 0)    /* end of the map entry */
 4081                         break;
 4082                     if (acc->map[i][0] == action) {
 4083                         action = acc->map[i][1];
 4084                         if (metas)
 4085                             action |= MKEY;
 4086                         return (action);
 4087                     }
 4088                 }
 4089                 do_bell(cur_console, BELL_PITCH, BELL_DURATION);
 4090                 goto next_code;
 4091             }
 4092             if (metas)
 4093                 action |= MKEY;
 4094             return(action);
 4095         }
 4096     }
 4097     goto next_code;
 4098 }
 4099 
 4100 int
 4101 scmmap(dev_t dev, vm_offset_t offset, int nprot)
 4102 {
 4103     if (offset > 0x20000 - PAGE_SIZE)
 4104         return -1;
 4105     return i386_btop((VIDEOMEM + offset));
 4106 }
 4107 
 4108 /*
 4109  * Calculate hardware attributes word using logical attributes mask and
 4110  * hardware colors
 4111  */
 4112 
 4113 static int
 4114 mask2attr(struct term_stat *term)
 4115 {
 4116     int attr, mask = term->attr_mask;
 4117 
 4118     if (mask & REVERSE_ATTR) {
 4119         attr = ((mask & FOREGROUND_CHANGED) ?
 4120                 ((term->cur_color & 0xF000) >> 4) :
 4121                 (term->rev_color & 0x0F00)) |
 4122                ((mask & BACKGROUND_CHANGED) ?
 4123                 ((term->cur_color & 0x0F00) << 4) :
 4124                 (term->rev_color & 0xF000));
 4125     } else
 4126         attr = term->cur_color;
 4127 
 4128     /* XXX: underline mapping for Hercules adapter can be better */
 4129     if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
 4130         attr ^= 0x0800;
 4131     if (mask & BLINK_ATTR)
 4132         attr ^= 0x8000;
 4133 
 4134     return attr;
 4135 }
 4136 
 4137 static void
 4138 set_keyboard(int command, int data)
 4139 {
 4140     int s;
 4141 
 4142     if (sc_kbdc == NULL)
 4143         return;
 4144 
 4145     /* prevent the timeout routine from polling the keyboard */
 4146     if (!kbdc_lock(sc_kbdc, TRUE)) 
 4147         return;
 4148 
 4149     /* disable the keyboard and mouse interrupt */
 4150     s = spltty();
 4151 #if 0
 4152     c = get_controller_command_byte(sc_kbdc);
 4153     if ((c == -1) 
 4154         || !set_controller_command_byte(sc_kbdc, 
 4155             kbdc_get_device_mask(sc_kbdc),
 4156             KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
 4157                 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 4158         /* CONTROLLER ERROR */
 4159         kbdc_lock(sc_kbdc, FALSE);
 4160         splx(s);
 4161         return;
 4162     }
 4163     /* 
 4164      * Now that the keyboard controller is told not to generate 
 4165      * the keyboard and mouse interrupts, call `splx()' to allow 
 4166      * the other tty interrupts. The clock interrupt may also occur, 
 4167      * but the timeout routine (`scrn_timer()') will be blocked 
 4168      * by the lock flag set via `kbdc_lock()'
 4169      */
 4170     splx(s);
 4171 #endif
 4172 
 4173     if (send_kbd_command_and_data(sc_kbdc, command, data) != KBD_ACK)
 4174         send_kbd_command(sc_kbdc, KBDC_ENABLE_KBD);
 4175 
 4176 #if 0
 4177     /* restore the interrupts */
 4178     if (!set_controller_command_byte(sc_kbdc,
 4179             kbdc_get_device_mask(sc_kbdc),
 4180             c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 
 4181         /* CONTROLLER ERROR */
 4182     }
 4183 #else
 4184     splx(s);
 4185 #endif
 4186     kbdc_lock(sc_kbdc, FALSE);
 4187 }
 4188 
 4189 static void
 4190 update_leds(int which)
 4191 {
 4192     static u_char xlate_leds[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
 4193 
 4194     /* replace CAPS led with ALTGR led for ALTGR keyboards */
 4195     if (key_map.n_keys > ALTGR_OFFSET) {
 4196         if (which & ALKED)
 4197             which |= CLKED;
 4198         else
 4199             which &= ~CLKED;
 4200     }
 4201 
 4202     set_keyboard(KBDC_SET_LEDS, xlate_leds[which & LED_MASK]);
 4203 }
 4204 
 4205 void
 4206 set_mode(scr_stat *scp)
 4207 {
 4208     char special_modetable[MODE_PARAM_SIZE];
 4209     char *mp;
 4210     int s;
 4211     int i;
 4212 
 4213     if (scp != cur_console)
 4214         return;
 4215 
 4216     /* 
 4217      * even if mode switching is disabled, we can change back
 4218      * to the initial mode or the custom mode based on the initial
 4219      * mode if we have saved register values upon start-up.
 4220      */
 4221     mp = get_mode_param(scp, scp->mode);
 4222     if (mp == NULL)
 4223         return;
 4224     bcopy(mp, &special_modetable, sizeof(special_modetable));
 4225 
 4226     /* setup video hardware for the given mode */
 4227     switch (scp->mode) {
 4228     case M_VGA_C80x60: case M_VGA_M80x60:
 4229         special_modetable[2]  = 0x08;
 4230         special_modetable[19] = 0x47;
 4231         goto special_480l;
 4232 
 4233     case M_VGA_C80x30: case M_VGA_M80x30:
 4234         special_modetable[19] = 0x4f;
 4235 special_480l:
 4236         special_modetable[9] |= 0xc0;
 4237         special_modetable[16] = 0x08;
 4238         special_modetable[17] = 0x3e;
 4239         special_modetable[26] = 0xea;
 4240         special_modetable[28] = 0xdf;
 4241         special_modetable[31] = 0xe7;
 4242         special_modetable[32] = 0x04;
 4243         goto setup_mode;
 4244 
 4245     case M_ENH_C80x43: case M_ENH_B80x43:
 4246         special_modetable[28] = 87;
 4247         goto special_80x50;
 4248 
 4249     case M_VGA_C80x50: case M_VGA_M80x50:
 4250 special_80x50:
 4251         special_modetable[2] = 8;
 4252         special_modetable[19] = 7;
 4253         goto setup_mode;
 4254 
 4255     case M_VGA_C40x25: case M_VGA_C80x25:
 4256     case M_VGA_M80x25:
 4257     case M_B40x25:     case M_C40x25:
 4258     case M_B80x25:     case M_C80x25:
 4259     case M_ENH_B40x25: case M_ENH_C40x25:
 4260     case M_ENH_B80x25: case M_ENH_C80x25:
 4261     case M_EGAMONO80x25:
 4262 
 4263 setup_mode:
 4264         set_vgaregs(special_modetable);
 4265         scp->font_size = special_modetable[2];
 4266 
 4267         /* set font type (size) */
 4268         if (scp->font_size < 14) {
 4269             if (fonts_loaded & FONT_8)
 4270                 copy_font(LOAD, FONT_8, font_8);
 4271             i = 0x0a;                           /* font 2 */
 4272         } else if (scp->font_size >= 16) {
 4273             if (fonts_loaded & FONT_16)
 4274                 copy_font(LOAD, FONT_16, font_16);
 4275             i = 0x00;                           /* font 0 */
 4276         } else {
 4277             if (fonts_loaded & FONT_14)
 4278                 copy_font(LOAD, FONT_14, font_14);
 4279             i = 0x05;                           /* font 1 */
 4280         }
 4281         /*
 4282          * FONT KLUDGE:
 4283          * This is an interim kludge to display correct font.
 4284          * Always use the font page #0 on the video plane 2.
 4285          * Somehow we cannot show the font in other font pages on
 4286          * some video cards... XXX
 4287          */
 4288         i = 0x00;
 4289         s = splhigh();
 4290         outb(TSIDX, 0x00); outb(TSREG, 0x01);
 4291         outb(TSIDX, 0x03); outb(TSREG, i);
 4292         outb(TSIDX, 0x00); outb(TSREG, 0x03);
 4293         splx(s);
 4294         if (flags & CHAR_CURSOR)
 4295             set_destructive_cursor(scp);
 4296         mark_all(scp);
 4297         break;
 4298 
 4299     case M_VGA_MODEX:
 4300         /* "unchain" the VGA mode */
 4301         special_modetable[5-1+0x04] &= 0xf7;
 4302         special_modetable[5-1+0x04] |= 0x04;
 4303         /* turn off doubleword mode */
 4304         special_modetable[10+0x14] &= 0xbf;
 4305         /* turn off word adressing */
 4306         special_modetable[10+0x17] |= 0x40;
 4307         /* set logical screen width */
 4308         special_modetable[10+0x13] = 80;
 4309         /* set 240 lines */
 4310         special_modetable[10+0x11] = 0x2c;
 4311         special_modetable[10+0x06] = 0x0d;
 4312         special_modetable[10+0x07] = 0x3e;
 4313         special_modetable[10+0x10] = 0xea;
 4314         special_modetable[10+0x11] = 0xac;
 4315         special_modetable[10+0x12] = 0xdf;
 4316         special_modetable[10+0x15] = 0xe7;
 4317         special_modetable[10+0x16] = 0x06;
 4318         /* set vertical sync polarity to reflect aspect ratio */
 4319         special_modetable[9] = 0xe3;
 4320         goto setup_grmode;
 4321 
 4322     case M_BG320:     case M_CG320:     case M_BG640:
 4323     case M_CG320_D:   case M_CG640_E:
 4324     case M_CG640x350: case M_ENH_CG640:
 4325     case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
 4326 
 4327 setup_grmode:
 4328         set_vgaregs(special_modetable);
 4329         scp->font_size = FONT_NONE;
 4330         break;
 4331 
 4332     default:
 4333         /* call user defined function XXX */
 4334         break;
 4335     }
 4336 
 4337     /* set border color for this (virtual) console */
 4338     set_border(scp->border);
 4339     return;
 4340 }
 4341 
 4342 void
 4343 set_border(u_char color)
 4344 {
 4345     switch (crtc_type) {
 4346     case KD_EGA:
 4347     case KD_VGA:
 4348         inb(crtc_addr + 6);             /* reset flip-flop */
 4349         outb(ATC, 0x31); outb(ATC, color);
 4350         break;
 4351     case KD_CGA:
 4352         outb(crtc_addr + 5, color & 0x0f); /* color select register */
 4353         break;
 4354     case KD_MONO:
 4355     case KD_HERCULES:
 4356     default:
 4357         break;
 4358     }
 4359 }
 4360 
 4361 static void
 4362 set_vgaregs(char *modetable)
 4363 {
 4364     int i, s = splhigh();
 4365 
 4366     outb(TSIDX, 0x00); outb(TSREG, 0x01);       /* stop sequencer */
 4367     for (i=0; i<4; i++) {                       /* program sequencer */
 4368         outb(TSIDX, i+1);
 4369         outb(TSREG, modetable[i+5]);
 4370     }
 4371     outb(MISC, modetable[9]);                   /* set dot-clock */
 4372     outb(TSIDX, 0x00); outb(TSREG, 0x03);       /* start sequencer */
 4373     outb(crtc_addr, 0x11);
 4374     outb(crtc_addr+1, inb(crtc_addr+1) & 0x7F);
 4375     for (i=0; i<25; i++) {                      /* program crtc */
 4376         outb(crtc_addr, i);
 4377         if (i == 14 || i == 15)                 /* no hardware cursor */
 4378             outb(crtc_addr+1, 0xff);
 4379         else
 4380             outb(crtc_addr+1, modetable[i+10]);
 4381     }
 4382     inb(crtc_addr+6);                           /* reset flip-flop */
 4383     for (i=0; i<20; i++) {                      /* program attribute ctrl */
 4384         outb(ATC, i);
 4385         outb(ATC, modetable[i+35]);
 4386     }
 4387     for (i=0; i<9; i++) {                       /* program graph data ctrl */
 4388         outb(GDCIDX, i);
 4389         outb(GDCREG, modetable[i+55]);
 4390     }
 4391     inb(crtc_addr+6);                           /* reset flip-flop */
 4392     outb(ATC, 0x20);                            /* enable palette */
 4393     splx(s);
 4394 }
 4395 
 4396 static void
 4397 read_vgaregs(char *buf)
 4398 {
 4399     int i, j;
 4400     int s;
 4401 
 4402     bzero(buf, MODE_PARAM_SIZE);
 4403 
 4404     s = splhigh();
 4405 
 4406     outb(TSIDX, 0x00); outb(TSREG, 0x01);       /* stop sequencer */
 4407     for (i=0, j=5; i<4; i++) {           
 4408         outb(TSIDX, i+1);
 4409         buf[j++] = inb(TSREG);
 4410     }
 4411     buf[9] = inb(MISC + 10);                    /* dot-clock */
 4412     outb(TSIDX, 0x00); outb(TSREG, 0x03);       /* start sequencer */
 4413 
 4414     for (i=0, j=10; i<25; i++) {                /* crtc */
 4415         outb(crtc_addr, i);
 4416         buf[j++] = inb(crtc_addr+1);
 4417     }
 4418     for (i=0, j=35; i<20; i++) {                /* attribute ctrl */
 4419         inb(crtc_addr+6);                       /* reset flip-flop */
 4420         outb(ATC, i);
 4421         buf[j++] = inb(ATC + 1);
 4422     }
 4423     for (i=0, j=55; i<9; i++) {                 /* graph data ctrl */
 4424         outb(GDCIDX, i);
 4425         buf[j++] = inb(GDCREG);
 4426     }
 4427     inb(crtc_addr+6);                           /* reset flip-flop */
 4428     outb(ATC, 0x20);                            /* enable palette */
 4429 
 4430     buf[0] = *(char *)pa_to_va(0x44a);          /* COLS */
 4431     buf[1] = *(char *)pa_to_va(0x484);          /* ROWS */
 4432     buf[2] = *(char *)pa_to_va(0x485);          /* POINTS */
 4433     buf[3] = *(char *)pa_to_va(0x44c);
 4434     buf[4] = *(char *)pa_to_va(0x44d);
 4435 
 4436     splx(s);
 4437 }
 4438 
 4439 static int 
 4440 comp_vgaregs(u_char *buf1, u_char *buf2)
 4441 {
 4442     static struct {
 4443         u_char mask;
 4444     } params[MODE_PARAM_SIZE] = {
 4445         0xff, 0x00, 0xff,               /* COLS, ROWS, POINTS */
 4446         0xff, 0xff,                     /* page length */
 4447         0xfe, 0xff, 0xff, 0xff,         /* sequencer registers */
 4448         0xf3,                           /* misc register */
 4449         0xff, 0xff, 0xff, 0x7f, 0xff,   /* CRTC */
 4450         0xff, 0xff, 0xff, 0x7f, 0xff,
 4451         0x00, 0x00, 0x00, 0x00, 0x00,
 4452         0x00, 0xff, 0x7f, 0xff, 0xff,
 4453         0x7f, 0xff, 0xff, 0xef, 0xff,
 4454         0xff, 0xff, 0xff, 0xff, 0xff,   /* attribute controller registers */
 4455         0xff, 0xff, 0xff, 0xff, 0xff,
 4456         0xff, 0xff, 0xff, 0xff, 0xff,
 4457         0xff, 0xff, 0xff, 0xff, 0xf0,
 4458         0xff, 0xff, 0xff, 0xff, 0xff,   /* GDC register */
 4459         0xff, 0xff, 0xff, 0xff, 
 4460     }; 
 4461     int identical = TRUE;
 4462     int i;
 4463 
 4464     for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
 4465         if (params[i].mask == 0)        /* don't care */
 4466             continue;
 4467         if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
 4468             return COMP_DIFFERENT;
 4469         if (buf1[i] != buf2[i])
 4470             identical = FALSE;
 4471     }
 4472     return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
 4473 
 4474 #if 0
 4475     for(i = 0; i < 20; ++i) {
 4476         if (*buf1++ != *buf2++)
 4477             return COMP_DIFFERENT;
 4478     }
 4479     buf1 += 2;  /* skip the cursor shape */
 4480     buf2 += 2;
 4481     for(i = 22; i < 24; ++i) {
 4482         if (*buf1++ != *buf2++)
 4483             return COMP_DIFFERENT;
 4484     }
 4485     buf1 += 2;  /* skip the cursor position */
 4486     buf2 += 2;
 4487     for(i = 26; i < MODE_PARAM_SIZE; ++i) {
 4488         if (*buf1++ != *buf2++)
 4489             return COMP_DIFFERENT;
 4490     }
 4491     return COMP_IDENTICAL;
 4492 #endif
 4493 }
 4494 
 4495 static void
 4496 dump_vgaregs(u_char *buf)
 4497 {
 4498     int i;
 4499 
 4500     for(i = 0; i < MODE_PARAM_SIZE;) {
 4501         printf("%02x ", buf[i]);
 4502         if ((++i % 16) == 0)
 4503             printf("\n");
 4504     }
 4505 }
 4506 
 4507 static void
 4508 set_font_mode(u_char *buf)
 4509 {
 4510     int s = splhigh();
 4511 
 4512     font_loading_in_progress = TRUE;
 4513 
 4514     /* save register values */
 4515     outb(TSIDX, 0x02); buf[0] = inb(TSREG);
 4516     outb(TSIDX, 0x04); buf[1] = inb(TSREG);
 4517     outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
 4518     outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
 4519     outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
 4520     inb(crtc_addr + 6);
 4521     outb(ATC, 0x10); buf[5] = inb(ATC + 1);
 4522 
 4523     /* setup vga for loading fonts */
 4524     inb(crtc_addr+6);                           /* reset flip-flop */
 4525     outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
 4526     inb(crtc_addr+6);                           /* reset flip-flop */
 4527     outb(ATC, 0x20);                            /* enable palette */
 4528 
 4529 #if SLOW_VGA
 4530 #ifdef SC_ALT_SEQACCESS
 4531     outb(TSIDX, 0x00); outb(TSREG, 0x01);
 4532 #endif
 4533     outb(TSIDX, 0x02); outb(TSREG, 0x04);
 4534     outb(TSIDX, 0x04); outb(TSREG, 0x07);
 4535 #ifdef SC_ALT_SEQACCESS
 4536     outb(TSIDX, 0x00); outb(TSREG, 0x03);
 4537 #endif
 4538     outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
 4539     outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
 4540     outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
 4541 #else
 4542 #ifdef SC_ALT_SEQACCESS
 4543     outw(TSIDX, 0x0100);
 4544 #endif
 4545     outw(TSIDX, 0x0402);
 4546     outw(TSIDX, 0x0704);
 4547 #ifdef SC_ALT_SEQACCESS
 4548     outw(TSIDX, 0x0300);
 4549 #endif
 4550     outw(GDCIDX, 0x0204);
 4551     outw(GDCIDX, 0x0005);
 4552     outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
 4553 #endif
 4554     splx(s);
 4555 }
 4556 
 4557 static void
 4558 set_normal_mode(u_char *buf)
 4559 {
 4560     char *modetable;
 4561     int s = splhigh();
 4562 
 4563     /* setup vga for normal operation mode again */
 4564     inb(crtc_addr+6);                           /* reset flip-flop */
 4565     outb(ATC, 0x10); outb(ATC, buf[5]);
 4566     inb(crtc_addr+6);                           /* reset flip-flop */
 4567     outb(ATC, 0x20);                            /* enable palette */
 4568 
 4569 #if SLOW_VGA
 4570 #ifdef SC_ALT_SEQACCESS
 4571     outb(TSIDX, 0x00); outb(TSREG, 0x01);
 4572 #endif
 4573     outb(TSIDX, 0x02); outb(TSREG, buf[0]);
 4574     outb(TSIDX, 0x04); outb(TSREG, buf[1]);
 4575 #ifdef SC_ALT_SEQACCESS
 4576     outb(TSIDX, 0x00); outb(TSREG, 0x03);
 4577 #endif
 4578     outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
 4579     outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
 4580     if (crtc_addr == MONO_BASE) {
 4581         outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
 4582     } else {
 4583         outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
 4584     }
 4585 #else
 4586 #ifdef SC_ALT_SEQACCESS
 4587     outw(TSIDX, 0x0100);
 4588 #endif
 4589     outw(TSIDX, 0x0002 | (buf[0] << 8));
 4590     outw(TSIDX, 0x0004 | (buf[1] << 8));
 4591 #ifdef SC_ALT_SEQACCESS
 4592     outw(TSIDX, 0x0300);
 4593 #endif
 4594     outw(GDCIDX, 0x0004 | (buf[2] << 8));
 4595     outw(GDCIDX, 0x0005 | (buf[3] << 8));
 4596     if (crtc_addr == MONO_BASE)
 4597         outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
 4598     else
 4599         outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
 4600 #endif
 4601 
 4602     font_loading_in_progress = FALSE;
 4603     splx(s);
 4604 }
 4605 
 4606 void
 4607 copy_font(int operation, int font_type, char* font_image)
 4608 {
 4609     int ch, line, segment, fontsize;
 4610     u_char buf[PARAM_BUFSIZE];
 4611     u_char val;
 4612 
 4613     switch (font_type) {
 4614     default:
 4615     case FONT_8:
 4616         segment = 0x8000;
 4617         fontsize = 8;
 4618         break;
 4619     case FONT_14:
 4620         segment = 0x4000;
 4621         fontsize = 14;
 4622         break;
 4623     case FONT_16:
 4624         segment = 0x0000;
 4625         fontsize = 16;
 4626         break;
 4627     }
 4628     /*
 4629      * FONT KLUDGE
 4630      * Always use the font page #0. XXX
 4631      */
 4632     segment = 0x0000;
 4633     outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
 4634     outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
 4635     set_font_mode(buf);
 4636     for (ch=0; ch < 256; ch++)
 4637         for (line=0; line < fontsize; line++)
 4638         if (operation)
 4639             *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line) =
 4640                     font_image[(ch*fontsize)+line];
 4641         else
 4642             font_image[(ch*fontsize)+line] =
 4643             *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line);
 4644     set_normal_mode(buf);
 4645     outb(TSIDX, 0x01); outb(TSREG, val & 0xDF); /* enable screen */
 4646 }
 4647 
 4648 static void
 4649 set_destructive_cursor(scr_stat *scp)
 4650 {
 4651     u_char buf[PARAM_BUFSIZE];
 4652     u_char cursor[32];
 4653     caddr_t address;
 4654     int i;
 4655     char *font_buffer;
 4656 
 4657     if (scp->font_size < 14) {
 4658         font_buffer = font_8;
 4659         address = (caddr_t)VIDEOMEM + 0x8000;
 4660     }
 4661     else if (scp->font_size >= 16) {
 4662         font_buffer = font_16;
 4663         address = (caddr_t)VIDEOMEM;
 4664     }
 4665     else {
 4666         font_buffer = font_14;
 4667         address = (caddr_t)VIDEOMEM + 0x4000;
 4668     }
 4669     /*
 4670      * FONT KLUDGE
 4671      * Always use the font page #0. XXX
 4672      */
 4673     address = (caddr_t)VIDEOMEM;
 4674 
 4675     if (scp->status & MOUSE_VISIBLE) {
 4676         if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR)
 4677             bcopy(&scp->mouse_cursor[0], cursor, scp->font_size);
 4678         else if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR + 1)
 4679             bcopy(&scp->mouse_cursor[32], cursor, scp->font_size);
 4680         else if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR + 2)
 4681             bcopy(&scp->mouse_cursor[64], cursor, scp->font_size);
 4682         else if ((scp->cursor_saveunder & 0xff) == SC_MOUSE_CHAR + 3)
 4683             bcopy(&scp->mouse_cursor[96], cursor, scp->font_size);
 4684         else
 4685             bcopy(font_buffer+((scp->cursor_saveunder & 0xff)*scp->font_size),
 4686                    cursor, scp->font_size);
 4687     }
 4688     else
 4689         bcopy(font_buffer + ((scp->cursor_saveunder & 0xff) * scp->font_size),
 4690                cursor, scp->font_size);
 4691     for (i=0; i<32; i++)
 4692         if ((i >= scp->cursor_start && i <= scp->cursor_end) ||
 4693             (scp->cursor_start >= scp->font_size && i == scp->font_size - 1))
 4694             cursor[i] |= 0xff;
 4695 #if 1
 4696     while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
 4697 #endif
 4698     set_font_mode(buf);
 4699     sc_bcopy(cursor, (char *)pa_to_va(address) + DEAD_CHAR * 32, 32);
 4700     set_normal_mode(buf);
 4701 }
 4702 
 4703 static void
 4704 set_mouse_pos(scr_stat *scp)
 4705 {
 4706     static int last_xpos = -1, last_ypos = -1;
 4707 
 4708     if (scp->mouse_xpos < 0)
 4709         scp->mouse_xpos = 0;
 4710     if (scp->mouse_ypos < 0)
 4711         scp->mouse_ypos = 0;
 4712     if (scp->status & UNKNOWN_MODE) {
 4713         if (scp->mouse_xpos > scp->xpixel-1)
 4714             scp->mouse_xpos = scp->xpixel-1;
 4715         if (scp->mouse_ypos > scp->ypixel-1)
 4716             scp->mouse_ypos = scp->ypixel-1;
 4717         return;
 4718     }
 4719     if (scp->mouse_xpos > (scp->xsize*8)-1)
 4720         scp->mouse_xpos = (scp->xsize*8)-1;
 4721     if (scp->mouse_ypos > (scp->ysize*scp->font_size)-1)
 4722         scp->mouse_ypos = (scp->ysize*scp->font_size)-1;
 4723 
 4724     if (scp->mouse_xpos != last_xpos || scp->mouse_ypos != last_ypos) {
 4725         scp->status |= MOUSE_MOVED;
 4726 
 4727         scp->mouse_pos = scp->scr_buf + 
 4728             ((scp->mouse_ypos/scp->font_size)*scp->xsize + scp->mouse_xpos/8);
 4729 
 4730         if ((scp->status & MOUSE_VISIBLE) && (scp->status & MOUSE_CUTTING))
 4731             mouse_cut(scp);
 4732     }
 4733 }
 4734 
 4735 #define isspace(c)      (((c) & 0xff) == ' ')
 4736 
 4737 static int
 4738 skip_spc_right(scr_stat *scp, u_short *p)
 4739 {
 4740     int i;
 4741 
 4742     for (i = (p - scp->scr_buf) % scp->xsize; i < scp->xsize; ++i) {
 4743         if (!isspace(*p))
 4744             break;
 4745         ++p;
 4746     }
 4747     return i;
 4748 }
 4749 
 4750 static int
 4751 skip_spc_left(scr_stat *scp, u_short *p)
 4752 {
 4753     int i;
 4754 
 4755     for (i = (p-- - scp->scr_buf) % scp->xsize - 1; i >= 0; --i) {
 4756         if (!isspace(*p))
 4757             break;
 4758         --p;
 4759     }
 4760     return i;
 4761 }
 4762 
 4763 static void
 4764 mouse_cut(scr_stat *scp)
 4765 {
 4766     u_short *end;
 4767     u_short *p;
 4768     int i = 0;
 4769     int j = 0;
 4770 
 4771     scp->mouse_cut_end = (scp->mouse_pos >= scp->mouse_cut_start) ?
 4772         scp->mouse_pos + 1 : scp->mouse_pos;
 4773     end = (scp->mouse_cut_start > scp->mouse_cut_end) ? 
 4774         scp->mouse_cut_start : scp->mouse_cut_end;
 4775     for (p = (scp->mouse_cut_start > scp->mouse_cut_end) ?
 4776             scp->mouse_cut_end : scp->mouse_cut_start; p < end; ++p) {
 4777         cut_buffer[i] = *p & 0xff;
 4778         /* remember the position of the last non-space char */
 4779         if (!isspace(cut_buffer[i++]))
 4780             j = i;
 4781         /* trim trailing blank when crossing lines */
 4782         if (((p - scp->scr_buf) % scp->xsize) == (scp->xsize - 1)) {
 4783             cut_buffer[j++] = '\r';
 4784             i = j;
 4785         }
 4786     }
 4787     cut_buffer[i] = '\0';
 4788 
 4789     /* scan towards the end of the last line */
 4790     --p;
 4791     for (i = (p - scp->scr_buf) % scp->xsize; i < scp->xsize; ++i) {
 4792         if (!isspace(*p))
 4793             break;
 4794         ++p;
 4795     }
 4796     /* if there is nothing but blank chars, trim them, but mark towards eol */
 4797     if (i >= scp->xsize) {
 4798         if (scp->mouse_cut_start > scp->mouse_cut_end)
 4799             scp->mouse_cut_start = p;
 4800         else
 4801             scp->mouse_cut_end = p;
 4802         cut_buffer[j++] = '\r';
 4803         cut_buffer[j] = '\0';
 4804     }
 4805 
 4806     mark_for_update(scp, scp->mouse_cut_start - scp->scr_buf);
 4807     mark_for_update(scp, scp->mouse_cut_end - scp->scr_buf);
 4808 }
 4809 
 4810 static void
 4811 mouse_cut_start(scr_stat *scp) 
 4812 {
 4813     int i;
 4814 
 4815     if (scp->status & MOUSE_VISIBLE) {
 4816         if (scp->mouse_pos == scp->mouse_cut_start &&
 4817             scp->mouse_cut_start == scp->mouse_cut_end - 1) {
 4818             cut_buffer[0] = '\0';
 4819             remove_cutmarking(scp);
 4820         } else if (skip_spc_right(scp, scp->mouse_pos) >= scp->xsize) {
 4821             /* if the pointer is on trailing blank chars, mark towards eol */
 4822             i = skip_spc_left(scp, scp->mouse_pos) + 1;
 4823             scp->mouse_cut_start = scp->scr_buf +
 4824                 ((scp->mouse_pos - scp->scr_buf) / scp->xsize) * scp->xsize + i;
 4825             scp->mouse_cut_end = scp->scr_buf +
 4826                 ((scp->mouse_pos - scp->scr_buf) / scp->xsize + 1) * scp->xsize;
 4827             cut_buffer[0] = '\r';
 4828             cut_buffer[1] = '\0';
 4829             scp->status |= MOUSE_CUTTING;
 4830         } else {
 4831             scp->mouse_cut_start = scp->mouse_pos;
 4832             scp->mouse_cut_end = scp->mouse_cut_start + 1;
 4833             cut_buffer[0] = *scp->mouse_cut_start & 0xff;
 4834             cut_buffer[1] = '\0';
 4835             scp->status |= MOUSE_CUTTING;
 4836         }
 4837         mark_all(scp);
 4838         /* delete all other screens cut markings */
 4839         for (i=0; i<MAXCONS; i++) {
 4840             if (console[i] == NULL || console[i] == scp)
 4841                 continue;
 4842             remove_cutmarking(console[i]);
 4843         }
 4844     }
 4845 }
 4846 
 4847 static void
 4848 mouse_cut_end(scr_stat *scp) 
 4849 {
 4850     if (scp->status & MOUSE_VISIBLE) {
 4851         scp->status &= ~MOUSE_CUTTING;
 4852     }
 4853 }
 4854 
 4855 static void
 4856 mouse_cut_word(scr_stat *scp)
 4857 {
 4858     u_short *p;
 4859     u_short *sol;
 4860     u_short *eol;
 4861     int i;
 4862 
 4863     /*
 4864      * Because we don't have locale information in the kernel,
 4865      * we only distinguish space char and non-space chars.  Punctuation
 4866      * chars, symbols and other regular chars are all treated alike.
 4867      */
 4868     if (scp->status & MOUSE_VISIBLE) {
 4869         sol = scp->scr_buf
 4870             + ((scp->mouse_pos - scp->scr_buf) / scp->xsize) * scp->xsize;
 4871         eol = sol + scp->xsize;
 4872         if (isspace(*scp->mouse_pos)) {
 4873             for (p = scp->mouse_pos; p >= sol; --p)
 4874                 if (!isspace(*p))
 4875                     break;
 4876             scp->mouse_cut_start = ++p;
 4877             for (p = scp->mouse_pos; p < eol; ++p)
 4878                 if (!isspace(*p))
 4879                     break;
 4880             scp->mouse_cut_end = p;
 4881         } else {
 4882             for (p = scp->mouse_pos; p >= sol; --p)
 4883                 if (isspace(*p))
 4884                     break;
 4885             scp->mouse_cut_start = ++p;
 4886             for (p = scp->mouse_pos; p < eol; ++p)
 4887                 if (isspace(*p))
 4888                     break;
 4889             scp->mouse_cut_end = p;
 4890         }
 4891         for (i = 0, p = scp->mouse_cut_start; p < scp->mouse_cut_end; ++p)
 4892             cut_buffer[i++] = *p & 0xff;
 4893         cut_buffer[i] = '\0';
 4894         scp->status |= MOUSE_CUTTING;
 4895     }
 4896 }
 4897 
 4898 static void
 4899 mouse_cut_line(scr_stat *scp)
 4900 {
 4901     u_short *p;
 4902     int i;
 4903 
 4904     if (scp->status & MOUSE_VISIBLE) {
 4905         scp->mouse_cut_start = scp->scr_buf
 4906             + ((scp->mouse_pos - scp->scr_buf) / scp->xsize) * scp->xsize;
 4907         scp->mouse_cut_end = scp->mouse_cut_start + scp->xsize;
 4908         for (i = 0, p = scp->mouse_cut_start; p < scp->mouse_cut_end; ++p)
 4909             cut_buffer[i++] = *p & 0xff;
 4910         cut_buffer[i++] = '\r';
 4911         cut_buffer[i] = '\0';
 4912         scp->status |= MOUSE_CUTTING;
 4913     }
 4914 }
 4915 
 4916 static void
 4917 mouse_cut_extend(scr_stat *scp) 
 4918 {
 4919     if ((scp->status & MOUSE_VISIBLE) && !(scp->status & MOUSE_CUTTING)
 4920         && (scp->mouse_cut_start != NULL)) {
 4921         mouse_cut(scp);
 4922         scp->status |= MOUSE_CUTTING;
 4923     }
 4924 }
 4925 
 4926 static void
 4927 mouse_paste(scr_stat *scp) 
 4928 {
 4929     if (scp->status & MOUSE_VISIBLE) {
 4930         struct tty *tp;
 4931         u_char *ptr = cut_buffer;
 4932 
 4933         tp = VIRTUAL_TTY(get_scr_num());
 4934         while (*ptr)
 4935             (*linesw[tp->t_line].l_rint)(scr_rmap[*ptr++], tp);
 4936     }
 4937 }
 4938 
 4939 static void
 4940 draw_mouse_image(scr_stat *scp)
 4941 {
 4942     caddr_t address;
 4943     int i;
 4944     char *font_buffer;
 4945     u_char buf[PARAM_BUFSIZE];
 4946     u_short buffer[32];
 4947     u_short xoffset, yoffset;
 4948     u_short *crt_pos = Crtat + (scp->mouse_pos - scp->scr_buf);
 4949     int font_size = scp->font_size;
 4950 
 4951     if (font_size < 14) {
 4952         font_buffer = font_8;
 4953         address = (caddr_t)VIDEOMEM + 0x8000;
 4954     }
 4955     else if (font_size >= 16) {
 4956         font_buffer = font_16;
 4957         address = (caddr_t)VIDEOMEM;
 4958     }
 4959     else {
 4960         font_buffer = font_14;
 4961         address = (caddr_t)VIDEOMEM + 0x4000;
 4962     }
 4963     /*
 4964      * FONT KLUDGE
 4965      * Always use the font page #0. XXX
 4966      */
 4967     address = (caddr_t)VIDEOMEM;
 4968 
 4969     xoffset = scp->mouse_xpos % 8;
 4970     yoffset = scp->mouse_ypos % font_size;
 4971 
 4972     /* prepare mousepointer char's bitmaps */
 4973     bcopy(font_buffer + ((*(scp->mouse_pos) & 0xff) * font_size),
 4974            &scp->mouse_cursor[0], font_size);
 4975     bcopy(font_buffer + ((*(scp->mouse_pos+1) & 0xff) * font_size),
 4976            &scp->mouse_cursor[32], font_size);
 4977     bcopy(font_buffer + ((*(scp->mouse_pos+scp->xsize) & 0xff) * font_size),
 4978            &scp->mouse_cursor[64], font_size);
 4979     bcopy(font_buffer + ((*(scp->mouse_pos+scp->xsize+1) & 0xff) * font_size),
 4980            &scp->mouse_cursor[96], font_size);
 4981     for (i=0; i<font_size; i++) {
 4982         buffer[i] = scp->mouse_cursor[i]<<8 | scp->mouse_cursor[i+32];
 4983         buffer[i+font_size]=scp->mouse_cursor[i+64]<<8|scp->mouse_cursor[i+96];
 4984     }
 4985 
 4986     /* now and-or in the mousepointer image */
 4987     for (i=0; i<16; i++) {
 4988         buffer[i+yoffset] =
 4989             ( buffer[i+yoffset] & ~(mouse_and_mask[i] >> xoffset))
 4990             | (mouse_or_mask[i] >> xoffset);
 4991     }
 4992     for (i=0; i<font_size; i++) {
 4993         scp->mouse_cursor[i] = (buffer[i] & 0xff00) >> 8;
 4994         scp->mouse_cursor[i+32] = buffer[i] & 0xff;
 4995         scp->mouse_cursor[i+64] = (buffer[i+font_size] & 0xff00) >> 8;
 4996         scp->mouse_cursor[i+96] = buffer[i+font_size] & 0xff;
 4997     }
 4998 
 4999     scp->mouse_oldpos = scp->mouse_pos;
 5000 
 5001 #if 1
 5002     /* wait for vertical retrace to avoid jitter on some videocards */
 5003     while (!(inb(crtc_addr+6) & 0x08)) /* idle */ ;
 5004 #endif
 5005     set_font_mode(buf);
 5006     sc_bcopy(scp->mouse_cursor, (char *)pa_to_va(address) + SC_MOUSE_CHAR * 32, 
 5007              128);
 5008     set_normal_mode(buf);
 5009     *(crt_pos) = (*(scp->mouse_pos) & 0xff00) | SC_MOUSE_CHAR;
 5010     *(crt_pos+scp->xsize) = 
 5011         (*(scp->mouse_pos + scp->xsize) & 0xff00) | (SC_MOUSE_CHAR + 2);
 5012     if (scp->mouse_xpos < (scp->xsize-1)*8) {
 5013         *(crt_pos + 1) = (*(scp->mouse_pos + 1) & 0xff00) | (SC_MOUSE_CHAR + 1);
 5014         *(crt_pos+scp->xsize + 1) = 
 5015             (*(scp->mouse_pos + scp->xsize + 1) & 0xff00) | (SC_MOUSE_CHAR + 3);
 5016     }
 5017     mark_for_update(scp, scp->mouse_pos - scp->scr_buf);
 5018     mark_for_update(scp, scp->mouse_pos + scp->xsize + 1 - scp->scr_buf);
 5019 }
 5020 
 5021 static void
 5022 remove_mouse_image(scr_stat *scp)
 5023 {
 5024     u_short *crt_pos = Crtat + (scp->mouse_oldpos - scp->scr_buf);
 5025 
 5026     *(crt_pos) = *(scp->mouse_oldpos);
 5027     *(crt_pos+1) = *(scp->mouse_oldpos+1);
 5028     *(crt_pos+scp->xsize) = *(scp->mouse_oldpos+scp->xsize);
 5029     *(crt_pos+scp->xsize+1) = *(scp->mouse_oldpos+scp->xsize+1);
 5030     mark_for_update(scp, scp->mouse_oldpos - scp->scr_buf);
 5031     mark_for_update(scp, scp->mouse_oldpos + scp->xsize + 1 - scp->scr_buf);
 5032 }
 5033 
 5034 static void
 5035 draw_cutmarking(scr_stat *scp)
 5036 {
 5037     u_short *ptr;
 5038     u_short och, nch;
 5039 
 5040     for (ptr=scp->scr_buf; ptr<=(scp->scr_buf+(scp->xsize*scp->ysize)); ptr++) {
 5041         nch = och = *(Crtat + (ptr - scp->scr_buf));
 5042         /* are we outside the selected area ? */
 5043         if ( ptr < (scp->mouse_cut_start > scp->mouse_cut_end ? 
 5044                     scp->mouse_cut_end : scp->mouse_cut_start) ||
 5045              ptr >= (scp->mouse_cut_start > scp->mouse_cut_end ?
 5046                     scp->mouse_cut_start : scp->mouse_cut_end)) {
 5047             if (ptr != scp->cursor_pos)
 5048                 nch = (och & 0xff) | (*ptr & 0xff00);
 5049         }
 5050         else {
 5051             /* are we clear of the cursor image ? */
 5052             if (ptr != scp->cursor_pos)
 5053                 nch = (och & 0x88ff) | (*ptr & 0x7000)>>4 | (*ptr & 0x0700)<<4;
 5054             else {
 5055                 if (flags & CHAR_CURSOR)
 5056                     nch = (och & 0x88ff)|(*ptr & 0x7000)>>4|(*ptr & 0x0700)<<4;
 5057                 else 
 5058                     if (!(flags & BLINK_CURSOR))
 5059                         nch = (och & 0xff) | (*ptr & 0xff00);
 5060             }
 5061         }
 5062         if (nch != och)
 5063             *(Crtat + (ptr - scp->scr_buf)) = nch;
 5064     }
 5065 }
 5066 
 5067 static void
 5068 remove_cutmarking(scr_stat *scp)
 5069 {
 5070     scp->mouse_cut_start = scp->mouse_cut_end = NULL;
 5071     scp->status &= ~MOUSE_CUTTING;
 5072     mark_all(scp);
 5073 }
 5074 
 5075 static void
 5076 save_palette(void)
 5077 {
 5078     int i;
 5079 
 5080     outb(PALRADR, 0x00);
 5081     for (i=0x00; i<0x300; i++)
 5082         palette[i] = inb(PALDATA);
 5083     inb(crtc_addr+6);           /* reset flip/flop */
 5084 }
 5085 
 5086 void
 5087 load_palette(char *palette)
 5088 {
 5089     int i;
 5090 
 5091     outb(PIXMASK, 0xFF);            /* no pixelmask */
 5092     outb(PALWADR, 0x00);
 5093     for (i=0x00; i<0x300; i++)
 5094          outb(PALDATA, palette[i]);
 5095     inb(crtc_addr+6);           /* reset flip/flop */
 5096     outb(ATC, 0x20);            /* enable palette */
 5097 }
 5098 
 5099 static void
 5100 do_bell(scr_stat *scp, int pitch, int duration)
 5101 {
 5102     if (cold || shutdown_in_progress)
 5103         return;
 5104 
 5105     if (scp != cur_console && (flags & QUIET_BELL))
 5106         return;
 5107 
 5108     if (flags & VISUAL_BELL) {
 5109         if (blink_in_progress)
 5110             return;
 5111         blink_in_progress = 4;
 5112         if (scp != cur_console)
 5113             blink_in_progress += 2;
 5114         blink_screen(cur_console);
 5115     } else {
 5116         if (scp != cur_console)
 5117             pitch *= 2;
 5118         sysbeep(pitch, duration);
 5119     }
 5120 }
 5121 
 5122 static void
 5123 blink_screen(void *arg)
 5124 {
 5125     scr_stat *scp = arg;
 5126 
 5127     if ((scp->status & UNKNOWN_MODE) || (blink_in_progress <= 1)) {
 5128         blink_in_progress = FALSE;
 5129         mark_all(scp);
 5130         if (delayed_next_scr)
 5131             switch_scr(scp, delayed_next_scr - 1);
 5132     }
 5133     else {
 5134         if (blink_in_progress & 1)
 5135             fillw(kernel_default.std_color | scr_map[0x20],
 5136                   Crtat, scp->xsize * scp->ysize);
 5137         else
 5138             fillw(kernel_default.rev_color | scr_map[0x20],
 5139                   Crtat, scp->xsize * scp->ysize);
 5140         blink_in_progress--;
 5141         timeout(blink_screen, scp, hz / 10);
 5142     }
 5143 }
 5144 
 5145 #ifdef SC_SPLASH_SCREEN
 5146 static void
 5147 toggle_splash_screen(scr_stat *scp)
 5148 {
 5149     static int toggle = 0;
 5150     static u_char save_mode;
 5151     int s;
 5152 
 5153     if (video_mode_ptr == NULL)
 5154         return;
 5155 
 5156     s = splhigh();
 5157     if (toggle) {
 5158         scp->mode = save_mode;
 5159         scp->status &= ~UNKNOWN_MODE;
 5160         set_mode(scp);
 5161         load_palette(palette);
 5162         toggle = 0;
 5163     }
 5164     else {
 5165         save_mode = scp->mode;
 5166         scp->mode = M_VGA_CG320;
 5167         scp->status |= UNKNOWN_MODE;
 5168         set_mode(scp);
 5169         /* load image */
 5170         toggle = 1;
 5171     }
 5172     splx(s);
 5173 }
 5174 #endif
 5175 #endif /* NSC */

Cache object: db2f8b781e30fa6e6cbd6927e5e0b6e9


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.