Home | History | Annotate | Line # | Download | only in dev
amidisplaycc.c revision 1.2
      1 /*-
      2  * Copyright (c) 2000 Jukka Andberg.
      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  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. The name of the author may not be used to endorse or promote products
     14  *    derived from this software without specific prior written permission
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * wscons interface to amiga custom chips. Contains the necessary functions
     30  * to render text on bitmapped screens. Uses the functions defined in
     31  * grfabs_reg.h for display creation/destruction and low level setup.
     32  *
     33  * For each virtual terminal a new screen ('view') is allocated.
     34  * Also one more is allocated for the mapped screen on demand.
     35  */
     36 
     37 #include "amidisplaycc.h"
     38 #include "grfcc.h"
     39 #include "view.h"
     40 #include "opt_amigaccgrf.h"
     41 
     42 #if NAMIDISPLAYCC>0
     43 
     44 #include <sys/param.h>
     45 #include <sys/types.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 #include <sys/systm.h>
     49 
     50 #include <sys/conf.h>
     51 
     52 #include <amiga/dev/grfabs_reg.h>
     53 #include <amiga/dev/viewioctl.h>
     54 #include <amiga/amiga/device.h>
     55 #include <dev/wscons/wsconsio.h>
     56 #include <dev/rcons/raster.h>
     57 #include <dev/wscons/wscons_raster.h>
     58 #include <dev/wscons/wsdisplayvar.h>
     59 #include <dev/cons.h>
     60 #include <dev/wsfont/wsfont.h>
     61 
     62 #include <machine/stdarg.h>
     63 
     64 #define AMIDISPLAYCC_MAXFONTS 8
     65 
     66 /* These can be lowered if you are sure you dont need that much colors. */
     67 #define MAXDEPTH 8
     68 #define MAXROWS 128
     69 #define MAXCOLUMNS 80
     70 
     71 #define ADJUSTCOLORS
     72 
     73 #define MAXCOLORS (1<<MAXDEPTH)
     74 
     75 struct amidisplaycc_screen;
     76 struct amidisplaycc_softc
     77 {
     78 	struct device dev;
     79 
     80 	/* runtime-loaded fonts */
     81 	struct wsdisplay_font         fonts[AMIDISPLAYCC_MAXFONTS];
     82 
     83 	struct amidisplaycc_screen  * currentscreen;
     84 
     85 	/* display turned on? */
     86 	int       ison;
     87 
     88 	/* stuff relating to the mapped screen */
     89 	view_t  * gfxview;
     90 	int       gfxwidth;
     91 	int       gfxheight;
     92 	int       gfxdepth;
     93 	int       gfxon;
     94 };
     95 
     96 
     97 /*
     98  * Configuration stuff.
     99  */
    100 
    101 static int  amidisplaycc_match  __P((struct device *,
    102 				     struct cfdata *,
    103 				     void *));
    104 static void amidisplaycc_attach __P((struct device *,
    105 				     struct device *,
    106 				     void *));
    107 
    108 struct cfattach amidisplaycc_ca = {
    109 	sizeof(struct amidisplaycc_softc),
    110 	amidisplaycc_match,
    111 	amidisplaycc_attach
    112 };
    113 
    114 cons_decl(amidisplaycc_);
    115 
    116 /* end of configuration stuff */
    117 
    118 /* private utility functions */
    119 
    120 static int  amidisplaycc_setvideo    __P((struct amidisplaycc_softc *,
    121 					 int ));
    122 
    123 static int  amidisplaycc_setemulcmap __P((struct amidisplaycc_screen *,
    124 					  struct wsdisplay_cmap *));
    125 
    126 static int  amidisplaycc_cmapioctl   __P((view_t *, u_long,
    127 					  struct wsdisplay_cmap *));
    128 static int  amidisplaycc_setcmap     __P((view_t *, struct wsdisplay_cmap *));
    129 static int  amidisplaycc_getcmap     __P((view_t *, struct wsdisplay_cmap *));
    130 static int  amidisplaycc_gfxscreen   __P((struct amidisplaycc_softc *, int));
    131 
    132 static int
    133 amidisplaycc_setnamedfont            __P((struct amidisplaycc_screen *,
    134 					 char *));
    135 static void
    136 amidisplaycc_setfont                 __P((struct amidisplaycc_screen *,
    137 					 struct wsdisplay_font *,
    138 					 int));
    139 static struct wsdisplay_font *
    140 amidisplaycc_findfont                __P((struct amidisplaycc_softc *,
    141 					 const char *, int, int));
    142 
    143 static void dprintf(const char *fmt, ...);
    144 
    145 /* end of private utility functions */
    146 
    147 /* emulops for wscons */
    148 void amidisplaycc_cursor       __P(( void *, int, int, int         ));
    149 int  amidisplaycc_mapchar      __P(( void *, int, unsigned int *   ));
    150 void amidisplaycc_putchar      __P(( void *, int, int, u_int, long ));
    151 void amidisplaycc_copycols     __P(( void *, int, int, int, int    ));
    152 void amidisplaycc_erasecols    __P(( void *, int, int, int, long   ));
    153 void amidisplaycc_copyrows     __P(( void *, int, int, int         ));
    154 void amidisplaycc_eraserows    __P(( void *, int, int, long        ));
    155 int  amidisplaycc_alloc_attr   __P(( void *, int, int, int, long * ));
    156 /* end of emulops for wscons */
    157 
    158 
    159 /* accessops for wscons */
    160 int      amidisplaycc_ioctl        __P(( void *, u_long, caddr_t,
    161 					 int, struct proc *              ));
    162 paddr_t  amidisplaycc_mmap         __P(( void *, off_t, int              ));
    163 int      amidisplaycc_alloc_screen __P(( void *,
    164 					 const struct wsscreen_descr *,
    165 					 void **, int *, int *, long *   ));
    166 
    167 void     amidisplaycc_free_screen  __P(( void *, void *                  ));
    168 int      amidisplaycc_show_screen  __P(( void *, void *, int,
    169 					 void (*) (void *, int, int),
    170 					 void *                          ));
    171 int      amidisplaycc_load_font    __P(( void *, void *,
    172 					 struct wsdisplay_font *         ));
    173 void     amidisplaycc_pollc        __P(( void *, int ));
    174 /* end of accessops for wscons */
    175 
    176 /*
    177  * These structures are passed to wscons, and they contain the
    178  * display-specific callback functions.
    179  */
    180 
    181 const struct wsdisplay_accessops amidisplaycc_accessops = {
    182 	amidisplaycc_ioctl,
    183 	amidisplaycc_mmap,
    184 	amidisplaycc_alloc_screen,
    185 	amidisplaycc_free_screen,
    186 	amidisplaycc_show_screen,
    187 	amidisplaycc_load_font,
    188 	amidisplaycc_pollc
    189 };
    190 
    191 const struct wsdisplay_emulops amidisplaycc_emulops = {
    192 	amidisplaycc_cursor,
    193 	amidisplaycc_mapchar,
    194 	amidisplaycc_putchar,
    195 	amidisplaycc_copycols,
    196 	amidisplaycc_erasecols,
    197 	amidisplaycc_copyrows,
    198 	amidisplaycc_eraserows,
    199 	amidisplaycc_alloc_attr
    200 };
    201 
    202 /* add some of our own data to the wsscreen_descr */
    203 struct amidisplaycc_screen_descr {
    204 	struct wsscreen_descr  wsdescr;
    205 	int                    depth;
    206 	char                   name[16];
    207 };
    208 
    209 /*
    210  * List of supported screenmodes. Almost anything can be given here.
    211  */
    212 
    213 #define ADCC_SCREEN(name, width, height, depth, fontwidth, fontheight) \
    214     /* CONSTCOND */ \
    215     {{ \
    216     name, \
    217     width / fontwidth, \
    218     height / fontheight, \
    219     &amidisplaycc_emulops, fontwidth, fontheight, \
    220     (depth > 1 ? WSSCREEN_WSCOLORS : 0) | WSSCREEN_REVERSE | \
    221     WSSCREEN_HILIT | WSSCREEN_UNDERLINE }, \
    222     depth }
    223 
    224 struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
    225 	/* name, width, height, depth, fontwidth==8, fontheight */
    226 	ADCC_SCREEN("80x50", 640, 400, 3, 8, 8),
    227 	ADCC_SCREEN("80x40", 640, 400, 3, 8, 10),
    228 	ADCC_SCREEN("80x25", 640, 400, 3, 8, 16),
    229 	ADCC_SCREEN("80x24", 640, 384, 3, 8, 16),
    230 
    231 	ADCC_SCREEN("640x400x1", 640, 400, 1, 8, 8),
    232 	ADCC_SCREEN("640x400x2", 640, 400, 2, 8, 8),
    233 	ADCC_SCREEN("640x400x3", 640, 400, 3, 8, 8),
    234 
    235 	ADCC_SCREEN("640x200x1", 640, 200, 1, 8, 8),
    236 	ADCC_SCREEN("640x200x1", 640, 200, 2, 8, 8),
    237 	ADCC_SCREEN("640x200x1", 640, 200, 3, 8, 8),
    238 };
    239 
    240 #define ADCC_SCREENPTR(index) &amidisplaycc_screentab[index].wsdescr
    241 const struct wsscreen_descr *amidisplaycc_screens[] = {
    242 	ADCC_SCREENPTR(0),
    243 	ADCC_SCREENPTR(1),
    244 	ADCC_SCREENPTR(2),
    245 	ADCC_SCREENPTR(3),
    246 	ADCC_SCREENPTR(4),
    247 	ADCC_SCREENPTR(5),
    248 	ADCC_SCREENPTR(6),
    249 	ADCC_SCREENPTR(7),
    250 	ADCC_SCREENPTR(8),
    251 	ADCC_SCREENPTR(9),
    252 };
    253 
    254 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
    255 
    256 /*
    257  * This structure also is passed to wscons. It contains pointers
    258  * to the available display modes.
    259  */
    260 
    261 const struct wsscreen_list amidisplaycc_screenlist = {
    262 	sizeof(amidisplaycc_screens)/sizeof(amidisplaycc_screens[0]),
    263 	amidisplaycc_screens
    264 };
    265 
    266 /*
    267  * Our own screen structure. One will be created for each screen.
    268  */
    269 
    270 struct amidisplaycc_screen
    271 {
    272 	struct amidisplaycc_softc *device;
    273 
    274 	int       isconsole;
    275 	int       isvisible;
    276 	view_t  * view;
    277 
    278 	int       ncols;
    279 	int       nrows;
    280 
    281 	int       cursorrow;
    282 	int       cursorcol;
    283 
    284 	/* Active bitplanes for each character row. */
    285 	int       rowmasks[MAXROWS];
    286 
    287 	/* Mapping of colors to screen colors. */
    288 	int       colormap[MAXCOLORS];
    289 
    290 	/* Copies of display parameters for convenience */
    291 	int       width;
    292 	int       height;
    293 	int       depth;
    294 
    295 	int       widthbytes; /* bytes_per_row           */
    296 	int       linebytes;  /* widthbytes + row_mod    */
    297 	int       rowbytes;   /* linebytes * fontheight  */
    298 
    299 	u_char  * planes[MAXDEPTH];
    300 
    301 	u_char  * savedscreen;
    302 
    303 	/*
    304 	 * The font is either one we loaded ourselves, or
    305 	 * one gotten using the wsfont system.
    306 	 *
    307 	 * wsfontcookie differentiates between them:
    308 	 * For fonts loaded by ourselves it is -1.
    309 	 * For wsfonts it contains a cookie for that system.
    310 	 */
    311 	struct wsdisplay_font  * font;
    312 	int                      wsfontcookie;
    313 	int                      fontwidth;
    314 	int                      fontheight;
    315 };
    316 
    317 typedef struct amidisplaycc_screen adccscr_t;
    318 
    319 /*
    320  * Need one statically allocated screen for early init.
    321  * The rest are mallocated when needed.
    322  */
    323 adccscr_t amidisplaycc_consolescreen;
    324 
    325 
    326 /*
    327  * Bring in the one or two builtin fonts.
    328  */
    329 
    330 extern unsigned char kernel_font_8x8[];
    331 extern unsigned char kernel_font_lo_8x8;
    332 extern unsigned char kernel_font_hi_8x8;
    333 
    334 /*
    335  * Default palettes for 2, 4 and 8 color emulation displays.
    336  */
    337 
    338 /* black, grey */
    339 static u_char pal2red[] = { 0x00, 0xaa };
    340 static u_char pal2grn[] = { 0x00, 0xaa };
    341 static u_char pal2blu[] = { 0x00, 0xaa };
    342 
    343 /* black, red, green, grey */
    344 static u_char pal4red[] = { 0x00, 0xaa, 0x00, 0xaa };
    345 static u_char pal4grn[] = { 0x00, 0x00, 0xaa, 0xaa };
    346 static u_char pal4blu[] = { 0x00, 0x00, 0x00, 0xaa };
    347 
    348 /* black, red, green, brown, blue, magenta, cyan, grey */
    349 static u_char pal8red[] = { 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa};
    350 static u_char pal8grn[] = { 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0xaa, 0xaa};
    351 static u_char pal8blu[] = { 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa};
    352 
    353 static struct wsdisplay_cmap pal2 = { 0, 2, pal2red, pal2grn, pal2blu };
    354 static struct wsdisplay_cmap pal4 = { 0, 4, pal4red, pal4grn, pal4blu };
    355 static struct wsdisplay_cmap pal8 = { 0, 8, pal8red, pal8grn, pal8blu };
    356 
    357 #ifdef GRF_AGA
    358 extern int aga_enable;
    359 #else
    360 static int aga_enable = 0;
    361 #endif
    362 
    363 /*
    364  * This gets called at console init to determine the priority of
    365  * this console device.
    366  *
    367  * Of course pointers to this and other functions must present
    368  * in constab[] in conf.c for this to work.
    369  */
    370 void
    371 amidisplaycc_cnprobe(cd)
    372 	struct consdev *cd;
    373 {
    374 	cd->cn_pri = CN_INTERNAL;
    375 
    376 	/*
    377 	 * Yeah, real nice. But if we win the console then the wscons system
    378 	 * does the proper initialization.
    379 	 */
    380 	cd->cn_dev = NODEV;
    381 }
    382 
    383 /*
    384  * This gets called if this device is used as the console.
    385  */
    386 void
    387 amidisplaycc_cninit(cd)
    388 	struct consdev  * cd;
    389 {
    390 	void  * cookie;
    391 	long    attr;
    392 	int     x;
    393 	int     y;
    394 
    395 	/* Yeah, we got the console! */
    396 
    397 	/*
    398 	 * This will do the basic stuff we also need.
    399 	 */
    400 	config_console();
    401 
    402 	/*
    403 	 * Call the init function in grfabs.c if we have
    404 	 * no grfcc to do it.
    405 	 * If grfcc is present it will call grfcc_probe()
    406 	 * during config_console() above.
    407 	 */
    408 #if NGRFCC==0
    409 	grfcc_probe();
    410 #endif
    411 
    412 #if NVIEW>0
    413 	viewprobe();
    414 #endif
    415 
    416 	/*
    417 	 * Set up wscons to handle the details.
    418 	 * It will then call us back when it needs something
    419 	 * display-specific. It will also set up cn_tab properly,
    420 	 * something which we failed to do at amidisplaycc_cnprobe().
    421 	 */
    422 
    423 	/*
    424 	 * The alloc_screen knows to allocate the first screen statically.
    425 	 */
    426 	amidisplaycc_alloc_screen(NULL, &amidisplaycc_screentab[0].wsdescr,
    427 				  &cookie, &x, &y, &attr);
    428 	wsdisplay_cnattach(&amidisplaycc_screentab[0].wsdescr,
    429 			   cookie, x, y, attr);
    430 }
    431 
    432 static int
    433 amidisplaycc_match(pdp, cfp, auxp)
    434 	struct device *pdp;
    435 	struct cfdata *cfp;
    436 	void *auxp;
    437 {
    438 	char *name = auxp;
    439 
    440 	if (matchname("amidisplaycc", name) == 0)
    441 		return (0);
    442 
    443 	/* Allow only one of us now. Not sure about that. */
    444 	if (cfp->cf_unit != 0)
    445 		return (0);
    446 
    447 	return 1;
    448 }
    449 
    450 /* ARGSUSED */
    451 static void
    452 amidisplaycc_attach(pdp, dp, auxp)
    453 	struct device  * pdp;
    454 	struct device  * dp;
    455 	void           * auxp;
    456 {
    457 	struct wsemuldisplaydev_attach_args    waa;
    458 	struct amidisplaycc_softc            * adp;
    459 
    460 	adp = (struct amidisplaycc_softc*)dp;
    461 
    462 	/*
    463 	 * Attach only at real configuration time. Console init is done at
    464 	 * the amidisplaycc_cninit function above.
    465 	 */
    466 	if (adp) {
    467 		printf(": Amiga custom chip graphics %s",
    468 		       aga_enable ? "(AGA)" : "");
    469 
    470 		if (amidisplaycc_consolescreen.isconsole) {
    471 			adp->currentscreen = &amidisplaycc_consolescreen;
    472 			printf(" (console)");
    473 		} else
    474 			adp->currentscreen = NULL;
    475 
    476 		printf("\n");
    477 
    478 		adp->ison = 1;
    479 
    480 		/*
    481 		 * Mapped screen properties.
    482 		 * Would need a way to configure.
    483 		 */
    484 		adp->gfxview = NULL;
    485 		adp->gfxon = 0;
    486 		adp->gfxwidth = 640;
    487 		adp->gfxheight = 480;
    488 
    489 		if (aga_enable)
    490 			adp->gfxdepth = 8;
    491 		else
    492 			adp->gfxdepth = 4;
    493 
    494 		if (NELEMS(amidisplaycc_screentab) !=
    495 		    NELEMS(amidisplaycc_screens))
    496 			panic("invalid screen definitions");
    497 
    498 		waa.scrdata = &amidisplaycc_screenlist;
    499 		waa.console = amidisplaycc_consolescreen.isconsole;
    500 		waa.accessops = &amidisplaycc_accessops;
    501 		waa.accesscookie = dp;
    502 		config_found(dp, &waa, wsemuldisplaydevprint);
    503 
    504 		bzero(adp->fonts, sizeof(adp->fonts));
    505 
    506 		/* Initialize an alternate system for finding fonts. */
    507 		wsfont_init();
    508 	}
    509 }
    510 
    511 
    512 /*
    513  * Color, bgcolor and style are packed into one long attribute.
    514  * These macros are used to create/split the attribute
    515  */
    516 
    517 #define MAKEATTR(fg, bg, mode) (((fg)<<16) | ((bg)<<8) | (mode))
    518 #define ATTRFG(attr) (((attr)>>16) & 255)
    519 #define ATTRBG(attr) (((attr)>>8) & 255)
    520 #define ATTRMO(attr) ((attr) & 255)
    521 
    522 /*
    523  * Called by wscons to draw/clear the cursor.
    524  * We do this by xorring the block to the screen.
    525  *
    526  * This simple implementation will break if the screen is modified
    527  * under the cursor before clearing it.
    528  */
    529 void
    530 amidisplaycc_cursor(screen, on, row, col)
    531 	void  * screen;
    532 	int     on;
    533 	int     row;
    534 	int     col;
    535 {
    536 	adccscr_t  * scr;
    537 	u_char     * dst;
    538 	int  i;
    539 
    540 	scr = screen;
    541 
    542 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
    543 		return;
    544 
    545 	if (!on && scr->cursorrow==-1 && scr->cursorcol==-1)
    546 		return;
    547 
    548 	if (!on) {
    549 		row = scr->cursorrow;
    550 		col = scr->cursorcol;
    551 	}
    552 
    553 	dst = scr->planes[0];
    554 	dst += row * scr->rowbytes;
    555 	dst += col;
    556 
    557 	if (on) {
    558 		scr->cursorrow = row;
    559 		scr->cursorcol = col;
    560 	} else {
    561 		scr->cursorrow = -1;
    562 		scr->cursorcol = -1;
    563 	}
    564 
    565 	for (i = scr->fontheight ; i > 0 ; i--) {
    566 		*dst ^= 255;
    567 		dst += scr->linebytes;
    568 	}
    569 }
    570 
    571 
    572 /*
    573  * This obviously does something important, don't ask me what.
    574  */
    575 int
    576 amidisplaycc_mapchar(screen, ch, chp)
    577 	void          * screen;
    578 	int             ch;
    579 	unsigned int  * chp;
    580 {
    581 	if (ch > 0 && ch < 256) {
    582 		*chp = ch;
    583 		return (5);
    584 	}
    585 	*chp = ' ';
    586 	return (0);
    587 }
    588 
    589 /*
    590  * Write a character to screen with color / bgcolor / hilite(bold) /
    591  * underline / reverse.
    592  * Surely could be made faster but I'm not sure if its worth the
    593  * effort as scrolling is at least a magnitude slower.
    594  */
    595 void
    596 amidisplaycc_putchar(screen, row, col, ch, attr)
    597 	void  * screen;
    598 	int     row;
    599 	int     col;
    600 	u_int   ch;
    601 	long    attr;
    602 {
    603 	adccscr_t  * scr;
    604 	u_char     * dst;
    605 	u_char     * font;
    606 
    607 	int         fontheight;
    608 	u_int8_t  * fontreal;
    609 	int         fontlow;
    610 	int         fonthigh;
    611 
    612 	int     bmapoffset;
    613 	int     linebytes;
    614 	int     underline;
    615 	int     fgcolor;
    616 	int     bgcolor;
    617 	int     plane;
    618 	int     depth;
    619 	int     mode;
    620 	int     bold;
    621 	u_char  f;
    622 	int     j;
    623 
    624 	scr = screen;
    625 
    626 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
    627 		return;
    628 
    629 	/* Extract the colors from the attribute */
    630 	fgcolor = ATTRFG(attr);
    631 	bgcolor = ATTRBG(attr);
    632 	mode    = ATTRMO(attr);
    633 
    634 	/* Translate to screen colors */
    635 	fgcolor = scr->colormap[fgcolor];
    636 	bgcolor = scr->colormap[bgcolor];
    637 
    638 	if (mode & WSATTR_REVERSE) {
    639 		j = fgcolor;
    640 		fgcolor = bgcolor;
    641 		bgcolor = j;
    642 	}
    643 
    644 	bold      = (mode & WSATTR_HILIT) > 0;
    645 	underline = (mode & WSATTR_UNDERLINE) > 0;
    646 
    647 	/* If we have loaded a font use it otherwise the builtin font */
    648 	if (scr->font) {
    649 		fontreal = scr->font->data;
    650 		fontlow  = scr->font->firstchar;
    651 		fonthigh = fontlow + scr->font->numchars - 1;
    652 	} else {
    653 		fontreal = kernel_font_8x8;
    654 		fontlow  = kernel_font_lo_8x8;
    655 		fonthigh = kernel_font_hi_8x8;
    656 	}
    657 
    658 	fontheight = scr->fontheight;
    659 	depth      = scr->depth;
    660 	linebytes  = scr->linebytes;
    661 
    662 	if (ch < fontlow || ch > fonthigh)
    663 		ch = fontlow;
    664 
    665 	/* Find the location where the wanted char is in the font data */
    666 	fontreal += scr->fontheight * (ch - fontlow);
    667 
    668 	bmapoffset = row * scr->rowbytes + col;
    669 
    670 	scr->rowmasks[row] |= fgcolor | bgcolor;
    671 
    672 	for (plane = 0 ; plane < depth ; plane++) {
    673 		dst = scr->planes[plane] + bmapoffset;
    674 
    675 		if (fgcolor & 1) {
    676 			if (bgcolor & 1) {
    677 				/* fg=on bg=on (fill) */
    678 
    679 				for (j = 0 ; j < fontheight ; j++) {
    680 					*dst = 255;
    681 					dst += linebytes;
    682 				}
    683 			} else {
    684 				/* fg=on bg=off (normal) */
    685 
    686 				font = fontreal;
    687 				for (j = 0 ; j < fontheight ; j++) {
    688 					f = *(font++);
    689 					f |= f >> bold;
    690 					*dst = f;
    691 					dst += linebytes;
    692 				}
    693 
    694 				if (underline)
    695 					*(dst - linebytes) = 255;
    696 			}
    697 		} else {
    698 			if (bgcolor & 1) {
    699 				/* fg=off bg=on (inverted) */
    700 
    701 				font = fontreal;
    702 				for (j = 0 ; j < fontheight ; j++) {
    703 					f = *(font++);
    704 					f |= f >> bold;
    705 					*dst = ~f;
    706 					dst += linebytes;
    707 				}
    708 
    709 				if (underline)
    710 					*(dst - linebytes) = 0;
    711 			} else {
    712 				/* fg=off bg=off (clear) */
    713 
    714 				for (j = 0 ; j < fontheight ; j++) {
    715 					*dst = 0;
    716 					dst += linebytes;
    717 				}
    718 			}
    719 		}
    720 		fgcolor >>= 1;
    721 		bgcolor >>= 1;
    722 	}
    723 }
    724 
    725 /*
    726  * Copy characters on a row to another position on the same row.
    727  */
    728 
    729 void
    730 amidisplaycc_copycols(screen, row, srccol, dstcol, ncols)
    731 	void  * screen;
    732 	int     row;
    733 	int     srccol;
    734 	int     dstcol;
    735 	int     ncols;
    736 {
    737 	adccscr_t  * scr;
    738 	u_char     * src;
    739 	u_char     * dst;
    740 
    741 	int  bmapoffset;
    742 	int  linebytes;
    743 	int  depth;
    744 	int  plane;
    745 	int  i;
    746 	int  j;
    747 
    748 	scr = screen;
    749 
    750 	if (srccol < 0 || srccol + ncols > scr->ncols ||
    751 	    dstcol < 0 || dstcol + ncols > scr->ncols ||
    752 	    row < 0 || row >= scr->nrows)
    753 		return;
    754 
    755 	depth = scr->depth;
    756 	linebytes = scr->linebytes;
    757 	bmapoffset = row * scr->rowbytes;
    758 
    759 	for (plane = 0 ; plane < depth ; plane++) {
    760 		src = scr->planes[plane] + bmapoffset;
    761 
    762 		for (j = 0 ; j < scr->fontheight ; j++) {
    763 			dst = src;
    764 
    765 			if (srccol < dstcol) {
    766 
    767 				for (i = ncols - 1 ; i >= 0 ; i--)
    768 					dst[dstcol + i] = src[srccol + i];
    769 
    770 			} else {
    771 
    772 				for (i = 0 ; i < ncols ; i++)
    773 					dst[dstcol + i] = src[srccol + i];
    774 
    775 			}
    776 			src += linebytes;
    777 		}
    778 	}
    779 }
    780 
    781 /*
    782  * Erase part of a row.
    783  */
    784 
    785 void
    786 amidisplaycc_erasecols(screen, row, startcol, ncols, attr)
    787 	void  * screen;
    788 	int     row;
    789 	int     startcol;
    790 	int     ncols;
    791 	long    attr;
    792 {
    793 	adccscr_t  * scr;
    794 	u_char     * dst;
    795 
    796 	int  bmapoffset;
    797 	int  linebytes;
    798 	int  bgcolor;
    799 	int  depth;
    800 	int  plane;
    801 	int  fill;
    802 	int  j;
    803 
    804 	scr = screen;
    805 
    806 	if (row < 0 || row >= scr->nrows ||
    807 	    startcol < 0 || startcol + ncols > scr->ncols)
    808 		return;
    809 
    810 	depth = scr->depth;
    811 	linebytes = scr->linebytes;
    812 	bmapoffset = row * scr->rowbytes + startcol;
    813 
    814 	/* Erase will be done using the set background color. */
    815 	bgcolor = ATTRBG(attr);
    816 	bgcolor = scr->colormap[bgcolor];
    817 
    818 	for(plane = 0 ; plane < depth ; plane++) {
    819 
    820 		fill = (bgcolor & 1) ? 255 : 0;
    821 
    822 		dst = scr->planes[plane] + bmapoffset;
    823 
    824 		for (j = 0 ; j < scr->fontheight ; j++) {
    825 			memset(dst, fill, ncols);
    826 			dst += linebytes;
    827 		}
    828 	}
    829 }
    830 
    831 /*
    832  * Copy a number of rows to another location on the screen.
    833  * Combined with eraserows it can be used to perform operation
    834  * also known as 'scrolling'.
    835  */
    836 
    837 void
    838 amidisplaycc_copyrows(screen, srcrow, dstrow, nrows)
    839 	void  * screen;
    840 	int     srcrow;
    841 	int     dstrow;
    842 	int     nrows;
    843 {
    844 	adccscr_t  * scr;
    845 	u_char     * src;
    846 	u_char     * dst;
    847 
    848 	int  srcbmapoffset;
    849 	int  dstbmapoffset;
    850 	int  widthbytes;
    851 	int  fontheight;
    852 	int  linebytes;
    853 	u_int copysize;
    854 	int  rowdelta;
    855 	int  rowbytes;
    856 	int  srcmask;
    857 	int  dstmask;
    858 	int  bmdelta;
    859 	int  depth;
    860 	int  plane;
    861 	int  i;
    862 	int  j;
    863 
    864 	scr = screen;
    865 
    866 	if (srcrow < 0 || srcrow + nrows > scr->nrows ||
    867 	    dstrow < 0 || dstrow + nrows > scr->nrows)
    868 		return;
    869 
    870 	depth = scr->depth;
    871 
    872 	widthbytes = scr->widthbytes;
    873 	rowbytes   = scr->rowbytes;
    874 	linebytes  = scr->linebytes;
    875 	fontheight = scr->fontheight;
    876 
    877 	srcbmapoffset = rowbytes * srcrow;
    878 	dstbmapoffset = rowbytes * dstrow;
    879 
    880 	if (srcrow < dstrow) {
    881 		/* Move data downwards, need to copy from down to up */
    882 		bmdelta = -rowbytes;
    883 		rowdelta = -1;
    884 
    885 		srcbmapoffset += rowbytes * (nrows - 1);
    886 		srcrow += nrows - 1;
    887 
    888 		dstbmapoffset += rowbytes * (nrows - 1);
    889 		dstrow += nrows - 1;
    890 	} else {
    891 		/* Move data upwards, copy up to down */
    892 		bmdelta = rowbytes;
    893 		rowdelta = 1;
    894 	}
    895 
    896 	if (widthbytes == linebytes)
    897 		copysize = rowbytes;
    898 	else
    899 		copysize = 0;
    900 
    901 	for (j = 0 ; j < nrows ; j++) {
    902 		/* Need to copy only planes that have data on src or dst */
    903 		srcmask = scr->rowmasks[srcrow];
    904 		dstmask = scr->rowmasks[dstrow];
    905 		scr->rowmasks[dstrow] = srcmask;
    906 
    907 		for (plane = 0 ; plane < depth ; plane++) {
    908 
    909 			if (srcmask & 1) {
    910 				/*
    911 				 * Source row has data on this
    912 				 * plane, copy it.
    913 				 */
    914 
    915 				src = scr->planes[plane] + srcbmapoffset;
    916 				dst = scr->planes[plane] + dstbmapoffset;
    917 
    918 				if (copysize > 0) {
    919 
    920 					memcpy(dst, src, copysize);
    921 
    922 				} else {
    923 
    924 					/*
    925 					 * Data not continuous,
    926 					 * must do in pieces
    927 					 */
    928 					for (i=0 ; i < fontheight ; i++) {
    929 						memcpy(dst, src, widthbytes);
    930 
    931 						src += linebytes;
    932 						dst += linebytes;
    933 					}
    934 				}
    935 			} else if (dstmask & 1) {
    936 				/*
    937 				 * Source plane is empty, but dest is not.
    938 				 * so all we need to is clear it.
    939 				 */
    940 
    941 				dst = scr->planes[plane] + dstbmapoffset;
    942 
    943 				if (copysize > 0) {
    944 					/* Do it all */
    945 					bzero(dst, copysize);
    946 				} else {
    947 					for (i = 0 ; i < fontheight ; i++) {
    948 						bzero(dst, widthbytes);
    949 						dst += linebytes;
    950 					}
    951 				}
    952 			}
    953 
    954 			srcmask >>= 1;
    955 			dstmask >>= 1;
    956 		}
    957 		srcbmapoffset += bmdelta;
    958 		dstbmapoffset += bmdelta;
    959 
    960 		srcrow += rowdelta;
    961 		dstrow += rowdelta;
    962 	}
    963 }
    964 
    965 /*
    966  * Erase some rows.
    967  */
    968 
    969 void
    970 amidisplaycc_eraserows(screen, row, nrows, attr)
    971 	void  * screen;
    972 	int     row;
    973 	int     nrows;
    974 	long    attr;
    975 {
    976 	adccscr_t  * scr;
    977 	u_char     * dst;
    978 
    979 	int  bmapoffset;
    980 	int  fillsize;
    981 	int  bgcolor;
    982 	int  depth;
    983 	int  plane;
    984 	int  fill;
    985 	int  j;
    986 
    987 	int  widthbytes;
    988 	int  linebytes;
    989 	int  rowbytes;
    990 
    991 
    992 	scr = screen;
    993 
    994 	if (row < 0 || row + nrows > scr->nrows)
    995 		return;
    996 
    997 	depth      = scr->depth;
    998 	widthbytes = scr->widthbytes;
    999 	linebytes  = scr->linebytes;
   1000 	rowbytes   = scr->rowbytes;
   1001 
   1002 	bmapoffset = row * rowbytes;
   1003 
   1004 	if (widthbytes == linebytes)
   1005 		fillsize = rowbytes * nrows;
   1006 	else
   1007 		fillsize = 0;
   1008 
   1009 	bgcolor = ATTRBG(attr);
   1010 	bgcolor = scr->colormap[bgcolor];
   1011 
   1012 	for (j = 0 ; j < nrows ; j++)
   1013 		scr->rowmasks[row+j] = bgcolor;
   1014 
   1015 	for (plane = 0 ; plane < depth ; plane++) {
   1016 		dst = scr->planes[plane] + bmapoffset;
   1017 		fill = (bgcolor & 1) ? 255 : 0;
   1018 
   1019 		if (fillsize > 0) {
   1020 			/* If the rows are continuous, write them all. */
   1021 			memset(dst, fill, fillsize);
   1022 		} else {
   1023 			for (j = 0 ; j < scr->fontheight * nrows ; j++) {
   1024 				memset(dst, fill, widthbytes);
   1025 				dst += linebytes;
   1026 			}
   1027 		}
   1028 		bgcolor >>= 1;
   1029 	}
   1030 }
   1031 
   1032 
   1033 /*
   1034  * Compose an attribute value from foreground color,
   1035  * background color, and flags.
   1036  */
   1037 int
   1038 amidisplaycc_alloc_attr(screen, fg, bg, flags, attrp)
   1039 	void  * screen;
   1040 	int     fg;
   1041 	int     bg;
   1042 	int     flags;
   1043 	long  * attrp;
   1044 {
   1045 	adccscr_t  * scr;
   1046 	int          maxcolor;
   1047 	int          newfg;
   1048 	int          newbg;
   1049 
   1050 	scr = screen;
   1051 	maxcolor = (1 << scr->view->bitmap->depth) - 1;
   1052 
   1053 	/* Ensure the colors are displayable. */
   1054 	newfg = fg & maxcolor;
   1055 	newbg = bg & maxcolor;
   1056 
   1057 #ifdef ADJUSTCOLORS
   1058 	/*
   1059 	 * Hack for low-color screens, if background color is nonzero
   1060 	 * but would be displayed as one, adjust it.
   1061 	 */
   1062 	if (bg > 0 && newbg == 0)
   1063 		newbg = maxcolor;
   1064 
   1065 	/*
   1066 	 * If foreground and background colors are different but would
   1067 	 * display the same fix them by modifying the foreground.
   1068 	 */
   1069 	if (fg != bg && newfg == newbg) {
   1070 		if (newbg > 0)
   1071 			newfg = 0;
   1072 		else
   1073 			newfg = maxcolor;
   1074 	}
   1075 #endif
   1076 	*attrp = MAKEATTR(newfg, newbg, flags);
   1077 
   1078 	return (0);
   1079 }
   1080 
   1081 int
   1082 amidisplaycc_ioctl(dp, cmd, data, flag, p)
   1083 	void         * dp;
   1084 	u_long         cmd;
   1085 	caddr_t        data;
   1086 	int            flag;
   1087 	struct proc  * p;
   1088 {
   1089 	struct amidisplaycc_softc *adp;
   1090 
   1091 	adp = dp;
   1092 
   1093 	if (adp == NULL) {
   1094 		printf("amidisplaycc_ioctl: adp==NULL\n");
   1095 		return (EINVAL);
   1096 	}
   1097 
   1098 #define UINTDATA (*(u_int*)data)
   1099 #define INTDATA (*(int*)data)
   1100 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
   1101 
   1102 	switch (cmd)
   1103 	{
   1104 	case WSDISPLAYIO_GTYPE:
   1105 		UINTDATA = WSDISPLAY_TYPE_AMIGACC;
   1106 		return (0);
   1107 
   1108 	case WSDISPLAYIO_SVIDEO:
   1109 		dprintf("amidisplaycc: WSDISPLAYIO_SVIDEO %s\n",
   1110 			UINTDATA ? "On" : "Off");
   1111 
   1112 		return (amidisplaycc_setvideo(adp, UINTDATA));
   1113 
   1114 	case WSDISPLAYIO_GVIDEO:
   1115 		dprintf("amidisplaycc: WSDISPLAYIO_GVIDEO\n");
   1116 		UINTDATA = adp->ison ?
   1117 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
   1118 
   1119 		return (0);
   1120 
   1121 	case WSDISPLAYIO_SMODE:
   1122 		if (INTDATA == WSDISPLAYIO_MODE_EMUL)
   1123 			return amidisplaycc_gfxscreen(adp, 0);
   1124 		if (INTDATA == WSDISPLAYIO_MODE_MAPPED)
   1125 			return amidisplaycc_gfxscreen(adp, 1);
   1126 		return (-1);
   1127 
   1128 	case WSDISPLAYIO_GINFO:
   1129 		FBINFO.width  = adp->gfxwidth;
   1130 		FBINFO.height = adp->gfxheight;
   1131 		FBINFO.depth  = adp->gfxdepth;
   1132 		FBINFO.cmsize = 1 << FBINFO.depth;
   1133 		return (0);
   1134 
   1135 	case WSDISPLAYIO_PUTCMAP:
   1136 	case WSDISPLAYIO_GETCMAP:
   1137 		return (amidisplaycc_cmapioctl(adp->gfxview,
   1138 					       cmd,
   1139 					       (struct wsdisplay_cmap*)data));
   1140 	}
   1141 
   1142 	dprintf("amidisplaycc: unknown ioctl %lx (grp:'%c' num:%d)\n",
   1143 		(long)cmd,
   1144 		(char)((cmd&0xff00)>>8),
   1145 		(int)(cmd&0xff));
   1146 
   1147 	return (-1);
   1148 
   1149 #undef UINTDATA
   1150 #undef INTDATA
   1151 #undef FBINFO
   1152 }
   1153 
   1154 
   1155 /*
   1156  * Switch to either emulation (text) or mapped (graphics) mode
   1157  * We keep an extra screen for mapped mode so it does not
   1158  * interfere with emulation screens.
   1159  *
   1160  * Once the extra screen is created, it never goes away.
   1161  */
   1162 
   1163 static int
   1164 amidisplaycc_gfxscreen(adp, on)
   1165 	struct amidisplaycc_softc  * adp;
   1166 	int                          on;
   1167 {
   1168 	dimen_t  dimension;
   1169 
   1170 	dprintf("amidisplaycc: switching to %s mode.\n",
   1171 		on ? "mapped" : "emul");
   1172 
   1173 	/* Current mode same as requested mode? */
   1174 	if ( (on > 0) == (adp->gfxon > 0) )
   1175 		return (0);
   1176 
   1177 	if (!on) {
   1178 		/*
   1179 		 * Switch away from mapped mode. If there is
   1180 		 * a emulation screen, switch to it, otherwise
   1181 		 * just try to hide the mapped screen.
   1182 		 */
   1183 		adp->gfxon = 0;
   1184 		if (adp->currentscreen)
   1185 			grf_display_view(adp->currentscreen->view);
   1186 		else if (adp->gfxview)
   1187 			grf_remove_view(adp->gfxview);
   1188 
   1189 		return (0);
   1190 	}
   1191 
   1192 	/* switch to mapped mode then */
   1193 
   1194 	if (adp->gfxview == NULL) {
   1195 		/* First time here, create the screen */
   1196 
   1197 		dimension.width = adp->gfxwidth;
   1198 		dimension.height = adp->gfxheight;
   1199 
   1200 		dprintf("amidisplaycc: preparing mapped screen %dx%dx%d\n",
   1201 			dimension.width,
   1202 			dimension.height,
   1203 			adp->gfxdepth);
   1204 
   1205 		adp->gfxview = grf_alloc_view(NULL,
   1206 					      &dimension,
   1207 					      adp->gfxdepth);
   1208 	}
   1209 
   1210 	if (adp->gfxview) {
   1211 		adp->gfxon = 1;
   1212 
   1213 		grf_display_view(adp->gfxview);
   1214 	} else {
   1215 		printf("amidisplaycc: failed to make mapped screen\n");
   1216 		return (ENOMEM);
   1217 	}
   1218 	return (0);
   1219 }
   1220 
   1221 /*
   1222  * Map the graphics screen. It must have been created before
   1223  * by switching to mapped mode by using an ioctl.
   1224  */
   1225 paddr_t
   1226 amidisplaycc_mmap(dp, off, prot)
   1227 	void   * dp;
   1228 	off_t    off;
   1229 	int      prot;
   1230 {
   1231 	struct amidisplaycc_softc  * adp;
   1232 	bmap_t                     * bm;
   1233 	paddr_t                      rv;
   1234 
   1235 	adp = (struct amidisplaycc_softc*)dp;
   1236 
   1237 	/* Check we are in mapped mode */
   1238 	if (adp->gfxon == 0 || adp->gfxview == NULL) {
   1239 		dprintf("amidisplaycc_mmap: Not in mapped mode\n");
   1240 		return (paddr_t)(-1);
   1241 	}
   1242 
   1243 	/*
   1244 	 * As we all know by now, we are mapping our special
   1245 	 * screen here so our pretty text consoles are left
   1246 	 * untouched.
   1247 	 */
   1248 
   1249 	bm = adp->gfxview->bitmap;
   1250 
   1251 	/* Check that the offset is valid */
   1252 	if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
   1253 		dprintf("amidisplaycc_mmap: Offset out of range\n");
   1254 		return (paddr_t)(-1);
   1255 	}
   1256 
   1257 	rv = (paddr_t)bm->hardware_address;
   1258 	rv += off;
   1259 
   1260 	return (rv >> PGSHIFT);
   1261 }
   1262 
   1263 
   1264 /*
   1265  * Create a new screen.
   1266  * NULL dp signifies console and then memory is allocated statically
   1267  * and the screen is automatically displayed.
   1268  *
   1269  * A font with suitable size is searched and if not found
   1270  * the builtin 8x8 font is used.
   1271  *
   1272  * There are separate default palettes for 2, 4 and 8+ color
   1273  * screens.
   1274  */
   1275 
   1276 int
   1277 amidisplaycc_alloc_screen(dp, screenp, cookiep, curxp, curyp, defattrp)
   1278 	void   * dp;
   1279 	const struct wsscreen_descr * screenp;
   1280 	void  ** cookiep;
   1281 	int    * curxp;
   1282 	int    * curyp;
   1283 	long   * defattrp;
   1284 {
   1285 	const struct amidisplaycc_screen_descr  * adccscreenp;
   1286 	struct amidisplaycc_screen              * scr;
   1287 	struct amidisplaycc_softc               * adp;
   1288 	view_t                                  * view;
   1289 
   1290 	dimen_t  dimension;
   1291 	int      fontheight;
   1292 	int      fontwidth;
   1293 	int      maxcolor;
   1294 	int      depth;
   1295 	int      i;
   1296 	int      j;
   1297 
   1298 	adccscreenp = (const struct amidisplaycc_screen_descr *)screenp;
   1299 	depth = adccscreenp->depth;
   1300 
   1301 	adp = dp;
   1302 
   1303 	maxcolor = (1 << depth) - 1;
   1304 
   1305 	/* Sanity checks because of fixed buffers */
   1306 	if (depth > MAXDEPTH || maxcolor >= MAXCOLORS)
   1307 		return (ENOMEM);
   1308 	if (screenp->nrows > MAXROWS)
   1309 		return (ENOMEM);
   1310 
   1311 	fontwidth = screenp->fontwidth;
   1312 	fontheight = screenp->fontheight;
   1313 
   1314 	if (fontwidth != 8) {
   1315 		dprintf("amidisplaycc_alloc_screen: fontwidth %d invalid.\n",
   1316 		       fontwidth);
   1317 		return (EINVAL);
   1318 	}
   1319 
   1320 	/*
   1321 	 * The screen size is defined in characters.
   1322 	 * Calculate the pixel size using the font size.
   1323 	 */
   1324 
   1325 	dimension.width = screenp->ncols * fontwidth;
   1326 	dimension.height = screenp->nrows * fontheight;
   1327 
   1328 	view = grf_alloc_view(NULL, &dimension, depth);
   1329 	if (view == NULL)
   1330 		return (ENOMEM);
   1331 
   1332 	/*
   1333 	 * First screen gets the statically allocated console screen.
   1334 	 * Others are allocated dynamically.
   1335 	 */
   1336 	if (adp == NULL) {
   1337 		scr = &amidisplaycc_consolescreen;
   1338 		if (scr->isconsole)
   1339 			panic("more than one console?");
   1340 
   1341 		scr->isconsole = 1;
   1342 	} else {
   1343 		scr = malloc(sizeof(adccscr_t), M_DEVBUF, M_WAITOK);
   1344 		bzero(scr, sizeof(adccscr_t));
   1345 	}
   1346 
   1347 	scr->view = view;
   1348 
   1349 	scr->ncols = screenp->ncols;
   1350 	scr->nrows = screenp->nrows;
   1351 
   1352 	/* Copies of most used values */
   1353 	scr->width  = dimension.width;
   1354 	scr->height = dimension.height;
   1355 	scr->depth  = depth;
   1356 	scr->widthbytes = view->bitmap->bytes_per_row;
   1357 	scr->linebytes  = scr->widthbytes + view->bitmap->row_mod;
   1358 	scr->rowbytes   = scr->linebytes * fontheight;
   1359 
   1360 	scr->device = adp;
   1361 
   1362 
   1363 	/* --- LOAD FONT --- */
   1364 
   1365 	/* these need to be initialized befory trying to set font */
   1366 	scr->font         = NULL;
   1367 	scr->wsfontcookie = -1;
   1368 	scr->fontwidth    = fontwidth;
   1369 	scr->fontheight   = fontheight;
   1370 
   1371 	/*
   1372 	 * Note that dont try to load font for the console (adp==NULL)
   1373 	 *
   1374 	 * Here we dont care which font we get as long as it is the
   1375 	 * right size so pass NULL.
   1376 	 */
   1377 	if (adp)
   1378 		amidisplaycc_setnamedfont(scr, NULL);
   1379 
   1380 	/*
   1381 	 * If no font found, use the builtin one.
   1382 	 * It will look stupid if the wanted size was different.
   1383 	 */
   1384 	if (scr->font == NULL) {
   1385 		scr->fontwidth = 8;
   1386 		scr->fontheight = min(8, fontheight);
   1387 	}
   1388 
   1389 	/* --- LOAD FONT END --- */
   1390 
   1391 
   1392 	for (i = 0 ; i < depth ; i++) {
   1393 		scr->planes[i] = view->bitmap->plane[i];
   1394 	}
   1395 
   1396 	for (i = 0 ; i < MAXROWS ; i++)
   1397 		scr->rowmasks[i] = 0;
   1398 
   1399 	/* Simple one-to-one mapping for most colors */
   1400 	for (i = 0 ; i < MAXCOLORS ; i++)
   1401 		scr->colormap[i] = i;
   1402 
   1403 	/*
   1404 	 * Arrange the most used pens to quickest colors.
   1405 	 * The default color for given depth is (1<<depth)-1.
   1406 	 * It is assumed it is used most and it is mapped to
   1407 	 * color that can be drawn by writing data to one bitplane
   1408 	 * only.
   1409 	 * So map colors 3->2, 7->4, 15->8 and so on.
   1410 	 */
   1411 	for (i = 2 ; i < MAXCOLORS ; i *= 2) {
   1412 		j = i * 2 - 1;
   1413 
   1414 		if (j < MAXCOLORS) {
   1415 			scr->colormap[i] = j;
   1416 			scr->colormap[j] = i;
   1417 		}
   1418 	}
   1419 
   1420 	/*
   1421 	 * Set the default colormap.
   1422 	 */
   1423 	if (depth == 1)
   1424 		amidisplaycc_setemulcmap(scr, &pal2);
   1425 	else if (depth == 2)
   1426 		amidisplaycc_setemulcmap(scr, &pal4);
   1427 	else
   1428 		amidisplaycc_setemulcmap(scr, &pal8);
   1429 
   1430 	*cookiep = scr;
   1431 
   1432 	*curxp = 0;
   1433 	*curyp = 0;
   1434 	amidisplaycc_cursor(scr, 1, *curxp, *curyp);
   1435 
   1436 	*defattrp = MAKEATTR(maxcolor, 0, 0);
   1437 
   1438 	/* Show the console automatically */
   1439 	if (adp == NULL)
   1440 		grf_display_view(scr->view);
   1441 
   1442 	if (adp) {
   1443 		dprintf("amidisplaycc: allocated screen; %dx%dx%d\n",
   1444 			dimension.width,
   1445 			dimension.height,
   1446 			depth);
   1447 	}
   1448 
   1449 	return (0);
   1450 }
   1451 
   1452 
   1453 /*
   1454  * Destroy a screen.
   1455  */
   1456 
   1457 void
   1458 amidisplaycc_free_screen(dp, screen)
   1459 	void  * dp;
   1460 	void  * screen;
   1461 {
   1462 	struct amidisplaycc_screen  * scr;
   1463 	struct amidisplaycc_softc   * adp;
   1464 
   1465 	scr = screen;
   1466 	adp = (struct amidisplaycc_softc*)adp;
   1467 
   1468 	if (scr == NULL)
   1469 		return;
   1470 
   1471 	/* Free the used font */
   1472 	amidisplaycc_setfont(scr, NULL, -1);
   1473 
   1474 	if (adp->currentscreen == scr)
   1475 		adp->currentscreen = NULL;
   1476 
   1477 	if (scr->view)
   1478 		grf_free_view(scr->view);
   1479 	scr->view = NULL;
   1480 
   1481 	/* Take care not to free the statically allocated console screen */
   1482 	if (scr != &amidisplaycc_consolescreen) {
   1483 		free(scr, M_DEVBUF);
   1484 	}
   1485 }
   1486 
   1487 /*
   1488  * Switch to another vt. Switch is always made immediately.
   1489  */
   1490 
   1491 /* ARGSUSED2 */
   1492 int
   1493 amidisplaycc_show_screen(dp, screen, waitok, cb, cbarg)
   1494 	void  * dp;
   1495 	void  * screen;
   1496 	int     waitok;
   1497 	void (* cb) (void *, int, int);
   1498 	void  * cbarg;
   1499 {
   1500 	adccscr_t *scr;
   1501 	struct amidisplaycc_softc *adp;
   1502 
   1503 	adp = (struct amidisplaycc_softc*)dp;
   1504 	scr = screen;
   1505 
   1506 	if (adp == NULL) {
   1507 		dprintf("amidisplaycc_show_screen: adp==NULL\n");
   1508 		return (EINVAL);
   1509 	}
   1510 	if (scr == NULL) {
   1511 		dprintf("amidisplaycc_show_screen: scr==NULL\n");
   1512 		return (EINVAL);
   1513 	}
   1514 
   1515 	if (adp->gfxon) {
   1516 		dprintf("amidisplaycc: Screen shift while in gfx mode?");
   1517 		adp->gfxon = 0;
   1518 	}
   1519 
   1520 	adp->currentscreen = scr;
   1521 	adp->ison = 1;
   1522 
   1523 	grf_display_view(scr->view);
   1524 
   1525 	return (0);
   1526 }
   1527 
   1528 /*
   1529  * Internal. Finds the font in our softc that has the desired attributes.
   1530  * Or, if name is NULL, finds a free location for a new font.
   1531  * Returns a pointer to font structure in softc or NULL for failure.
   1532  *
   1533  * Three possible forms:
   1534  * findfont(adp, NULL, 0, 0)  -- find first empty location
   1535  * findfont(adp, NULL, x, y)  -- find last font with given size
   1536  * findfont(adp, name, x, y)  -- find last font with given name and size
   1537  *
   1538  * Note that when finding an empty location first one found is returned,
   1539  * however when finding an existing font, the last one matching is
   1540  * returned. This is because fonts cannot be unloaded and the last
   1541  * font on the list is the one added latest and thus probably preferred.
   1542  *
   1543  * Note also that this is the only function which makes assumptions
   1544  * about the storage location for the fonts.
   1545  */
   1546 static struct wsdisplay_font *
   1547 amidisplaycc_findfont(adp, name, width, height)
   1548 	struct amidisplaycc_softc  * adp;
   1549 	const char                 * name;
   1550 	int                          width;
   1551 	int                          height;
   1552 {
   1553 	struct wsdisplay_font  * font;
   1554 
   1555 	int  findempty;
   1556 	int  f;
   1557 
   1558 	if (adp == NULL) {
   1559 		dprintf("amidisplaycc_findfont: NULL adp\n");
   1560 		return NULL;
   1561 	}
   1562 
   1563 	findempty = (name == NULL) && (width == 0) && (height == 0);
   1564 
   1565 	font = NULL;
   1566 
   1567 	for (f = 0 ; f < AMIDISPLAYCC_MAXFONTS ; f++) {
   1568 
   1569 		if (findempty && adp->fonts[f].name == NULL)
   1570 			return &adp->fonts[f];
   1571 
   1572 		if (!findempty && name == NULL && adp->fonts[f].name &&
   1573 		    adp->fonts[f].fontwidth == width &&
   1574 		    adp->fonts[f].fontheight == height)
   1575 			font = &adp->fonts[f];
   1576 
   1577 		if (name && adp->fonts[f].name &&
   1578 		    strcmp(name, adp->fonts[f].name) == 0 &&
   1579 		    width == adp->fonts[f].fontwidth &&
   1580 		    height == adp->fonts[f].fontheight)
   1581 			font = &adp->fonts[f];
   1582 	}
   1583 
   1584 	return (font);
   1585 }
   1586 
   1587 
   1588 /*
   1589  * Set the font on a screen and free the old one.
   1590  * Can be called with font of NULL to just free the
   1591  * old one.
   1592  * NULL font cannot be accompanied by valid cookie (!= -1)
   1593  */
   1594 static void
   1595 amidisplaycc_setfont(scr, font, wsfontcookie)
   1596 	struct amidisplaycc_screen * scr;
   1597 	struct wsdisplay_font      * font;
   1598 	int                          wsfontcookie;
   1599 {
   1600 	if (scr == NULL)
   1601 		panic("amidisplaycc_setfont: scr==NULL");
   1602 	if (font == NULL && wsfontcookie != -1)
   1603 		panic("amidisplaycc_setfont: no font but eat cookie");
   1604 	if (scr->font == NULL && scr->wsfontcookie != -1)
   1605 		panic("amidisplaycc_setfont: no font but eat old cookie");
   1606 
   1607 	scr->font = font;
   1608 
   1609 	if (scr->wsfontcookie != -1)
   1610 		wsfont_unlock(scr->wsfontcookie);
   1611 
   1612 	scr->wsfontcookie = wsfontcookie;
   1613 }
   1614 
   1615 /*
   1616  * Try to find the named font and set the screen to use it.
   1617  * Check both the fonts we have loaded with load_font and
   1618  * fonts from wsfont system.
   1619  *
   1620  * Returns 0 on success.
   1621  */
   1622 
   1623 static int
   1624 amidisplaycc_setnamedfont(scr, fontname)
   1625 	struct amidisplaycc_screen  * scr;
   1626 	char                        * fontname;
   1627 {
   1628 	struct wsdisplay_font  * font;
   1629 	int  wsfontcookie;
   1630 
   1631 	wsfontcookie = -1;
   1632 
   1633 	if (scr == NULL || scr->device == NULL) {
   1634 		dprintf("amidisplaycc_setnamedfont: invalid\n");
   1635 		return (EINVAL);
   1636 	}
   1637 
   1638 	/* Try first our dynamically loaded fonts. */
   1639 	font = amidisplaycc_findfont(scr->device,
   1640 				     fontname,
   1641 				     scr->fontwidth,
   1642 				     scr->fontheight);
   1643 
   1644 	if (font == NULL) {
   1645 		/*
   1646 		 * Ok, no dynamically loaded font found.
   1647 		 * Try the wsfont system then.
   1648 		 */
   1649 		wsfontcookie = wsfont_find(fontname,
   1650 					   scr->fontwidth,
   1651 					   scr->fontheight,
   1652 					   1);
   1653 
   1654 		if (wsfontcookie == -1)
   1655 			return (EINVAL);
   1656 
   1657 		/* So, found a suitable font. Now lock it. */
   1658 		if (wsfont_lock(wsfontcookie,
   1659 				&font,
   1660 				WSDISPLAY_FONTORDER_L2R,
   1661 				WSDISPLAY_FONTORDER_L2R) == -1)
   1662 			return (EINVAL);
   1663 
   1664 		/* Ok here we have the font successfully. */
   1665 	}
   1666 
   1667 	amidisplaycc_setfont(scr, font, wsfontcookie);
   1668 	return (0);
   1669 }
   1670 
   1671 /*
   1672  * Load a font. This is used both to load a font and set it to screen.
   1673  * The function depends on the parameters.
   1674  * If the font has no data we must set a previously loaded
   1675  * font with the same name. If it has data, then just load
   1676  * the font but don't use it.
   1677  */
   1678 int
   1679 amidisplaycc_load_font(dp, cookie, font)
   1680 	void                   * dp;
   1681 	void                   * cookie;
   1682 	struct wsdisplay_font  * font;
   1683 {
   1684 	struct amidisplaycc_softc   * adp;
   1685 	struct amidisplaycc_screen  * scr;
   1686 	struct wsdisplay_font       * myfont;
   1687 
   1688 	u_int8_t  * c;
   1689 	void      * olddata;
   1690 	char      * name;
   1691 
   1692 	u_int       size;
   1693 	u_int       i;
   1694 
   1695 
   1696 	adp = dp;
   1697 	scr = cookie;
   1698 
   1699 	/*
   1700 	 * If font has no data it means we have to find the
   1701 	 * named font and use it.
   1702 	 */
   1703 	if (scr && font && font->name && !font->data)
   1704 		return amidisplaycc_setnamedfont(scr, font->name);
   1705 
   1706 
   1707 	/* Pre-load the font it is */
   1708 
   1709 	if (font->stride != 1) {
   1710 		dprintf("amidisplaycc_load_font: stride %d != 1\n",
   1711 		       font->stride);
   1712 		return (-1);
   1713 	}
   1714 
   1715 	if (font->fontwidth != 8) {
   1716 		dprintf("amidisplaycc_load_font: width %d not supported\n",
   1717 		       font->fontwidth);
   1718 		return (-1);
   1719 	}
   1720 
   1721 	/* Size of the font in bytes... Assuming stride==1 */
   1722 	size = font->fontheight * font->numchars;
   1723 
   1724 	/* Check if the same font was loaded before */
   1725 	myfont = amidisplaycc_findfont(adp,
   1726 				       font->name,
   1727 				       font->fontwidth,
   1728 				       font->fontheight);
   1729 
   1730 	olddata = NULL;
   1731 	if (myfont) {
   1732 		/* Old font found, we will replace */
   1733 
   1734 		if (myfont->name == NULL || myfont->data == NULL)
   1735 			panic("replacing NULL font/data");
   1736 
   1737 		/*
   1738 		 * Store the old data pointer. We'll free it later
   1739 		 * when the new one is in place. Reallocation is needed
   1740 		 * because the new font may have a different number
   1741 		 * of characters in it than the last time it was loaded.
   1742 		 */
   1743 
   1744 		olddata = myfont->data;
   1745 
   1746 	} else {
   1747 		/* Totally brand new font */
   1748 
   1749 		/* Try to find empty slot for the font */
   1750 		myfont = amidisplaycc_findfont(adp, NULL, 0, 0);
   1751 
   1752 		if (myfont == NULL)
   1753 			return (ENOMEM);
   1754 
   1755 		bzero(myfont, sizeof(struct wsdisplay_font));
   1756 
   1757 		myfont->fontwidth = font->fontwidth;
   1758 		myfont->fontheight = font->fontheight;
   1759 		myfont->stride = font->stride;
   1760 
   1761 		name = malloc(strlen(font->name)+1,
   1762 			      M_DEVBUF,
   1763 			      M_WAITOK);
   1764 		strcpy(name, font->name);
   1765 		myfont->name = name;
   1766 	}
   1767 	myfont->firstchar = font->firstchar;
   1768 	myfont->numchars  = font->numchars;
   1769 
   1770 	myfont->data = malloc(size,
   1771 			      M_DEVBUF,
   1772 			      M_WAITOK);
   1773 
   1774 	if (olddata)
   1775 		free(olddata, M_DEVBUF);
   1776 
   1777 
   1778 	memcpy(myfont->data, font->data, size);
   1779 
   1780 	if (font->bitorder == WSDISPLAY_FONTORDER_R2L) {
   1781 		/* Reverse the characters. */
   1782 		c = myfont->data;
   1783 		for (i = 0 ; i < size ; i++) {
   1784 			*c = ((*c & 0x0f) << 4) | ((*c & 0xf0) >> 4);
   1785 			*c = ((*c & 0x33) << 2) | ((*c & 0xcc) >> 2);
   1786 			*c = ((*c & 0x55) << 1) | ((*c & 0xaa) >> 1);
   1787 
   1788 			c++;
   1789 		}
   1790 	}
   1791 
   1792 	/* Yeah, we made it */
   1793 	return (0);
   1794 }
   1795 
   1796 /*
   1797  * Set display on/off.
   1798  */
   1799 static int
   1800 amidisplaycc_setvideo(adp, mode)
   1801 	struct amidisplaycc_softc  * adp;
   1802 	int                          mode;
   1803 {
   1804         view_t * view;
   1805 
   1806 	if (adp == NULL) {
   1807 		dprintf("amidisplaycc_setvideo: adp==NULL\n");
   1808 		return (EINVAL);
   1809 	}
   1810 	if (adp->currentscreen == NULL) {
   1811 		dprintf("amidisplaycc_setvideo: adp->currentscreen==NULL\n");
   1812 		return (EINVAL);
   1813 	}
   1814 
   1815 	/* select graphics or emulation screen */
   1816 	if (adp->gfxon && adp->gfxview)
   1817 		view = adp->gfxview;
   1818 	else
   1819 		view = adp->currentscreen->view;
   1820 
   1821 	if (mode) {
   1822 		/* on */
   1823 
   1824 		grf_display_view(view);
   1825 		dprintf("amidisplaycc: video is now on\n");
   1826 		adp->ison = 1;
   1827 
   1828 	} else {
   1829 		/* off */
   1830 
   1831 		grf_remove_view(view);
   1832 		dprintf("amidisplaycc: video is now off\n");
   1833 		adp->ison = 0;
   1834 	}
   1835 
   1836 	return (0);
   1837 }
   1838 
   1839 /*
   1840  * Handle the WSDISPLAY_[PUT/GET]CMAP ioctls.
   1841  * Just handle the copying of data to/from userspace and
   1842  * let the functions amidisplaycc_setcmap and amidisplaycc_putcmap
   1843  * do the real work.
   1844  */
   1845 
   1846 static int
   1847 amidisplaycc_cmapioctl(view, cmd, cmap)
   1848 	view_t                 * view;
   1849 	u_long                   cmd;
   1850 	struct wsdisplay_cmap  * cmap;
   1851 {
   1852 	struct wsdisplay_cmap  tmpcmap;
   1853 	u_char                 cmred[MAXCOLORS];
   1854 	u_char                 cmgrn[MAXCOLORS];
   1855 	u_char                 cmblu[MAXCOLORS];
   1856 
   1857 	int                    err;
   1858 
   1859 	if (cmap->index >= MAXCOLORS ||
   1860 	    cmap->count > MAXCOLORS ||
   1861 	    cmap->index + cmap->count > MAXCOLORS)
   1862 		return (EINVAL);
   1863 
   1864 	if (cmap->count == 0)
   1865 		return (0);
   1866 
   1867 	tmpcmap.index = cmap->index;
   1868 	tmpcmap.count = cmap->count;
   1869 	tmpcmap.red   = cmred;
   1870 	tmpcmap.green = cmgrn;
   1871 	tmpcmap.blue  = cmblu;
   1872 
   1873 	if (cmd == WSDISPLAYIO_PUTCMAP) {
   1874 		/* copy the color data to kernel space */
   1875 
   1876 		err = copyin(cmap->red, cmred, cmap->count);
   1877 		if (err)
   1878 			return (err);
   1879 
   1880 		err = copyin(cmap->green, cmgrn, cmap->count);
   1881 		if (err)
   1882 			return (err);
   1883 
   1884 		err = copyin(cmap->blue, cmblu, cmap->count);
   1885 		if (err)
   1886 			return (err);
   1887 
   1888 		return amidisplaycc_setcmap(view, &tmpcmap);
   1889 
   1890 	} else if (cmd == WSDISPLAYIO_GETCMAP) {
   1891 
   1892 		err = amidisplaycc_getcmap(view, &tmpcmap);
   1893 		if (err)
   1894 			return (err);
   1895 
   1896 		/* copy data to user space */
   1897 
   1898 		err = copyout(cmred, cmap->red, cmap->count);
   1899 		if (err)
   1900 			return (err);
   1901 
   1902 		err = copyout(cmgrn, cmap->green, cmap->count);
   1903 		if (err)
   1904 			return (err);
   1905 
   1906 		err = copyout(cmblu, cmap->blue, cmap->count);
   1907 		if (err)
   1908 			return (err);
   1909 
   1910 		return (0);
   1911 
   1912 	} else
   1913 		return (-1);
   1914 }
   1915 
   1916 /*
   1917  * Set the palette of a emulation screen.
   1918  * Here we do only color remapping and then call
   1919  * amidisplaycc_setcmap to do the work.
   1920  */
   1921 
   1922 static int
   1923 amidisplaycc_setemulcmap(scr, cmap)
   1924 	struct amidisplaycc_screen  * scr;
   1925 	struct wsdisplay_cmap       * cmap;
   1926 {
   1927 	struct wsdisplay_cmap  tmpcmap;
   1928 
   1929 	u_char                 red [MAXCOLORS];
   1930 	u_char                 grn [MAXCOLORS];
   1931 	u_char                 blu [MAXCOLORS];
   1932 
   1933 	int                    rc;
   1934 	int                    i;
   1935 
   1936 	/*
   1937 	 * Get old palette first.
   1938 	 * Because of the color mapping going on in the emulation
   1939 	 * screen the color range may not be contiguous in the real
   1940 	 * palette.
   1941 	 * So get the whole palette, insert the new colors
   1942 	 * at the appropriate places and then set the whole
   1943 	 * palette back.
   1944 	 */
   1945 
   1946 	tmpcmap.index = 0;
   1947 	tmpcmap.count = 1 << scr->depth;
   1948 	tmpcmap.red   = red;
   1949 	tmpcmap.green = grn;
   1950 	tmpcmap.blue  = blu;
   1951 
   1952 	rc = amidisplaycc_getcmap(scr->view, &tmpcmap);
   1953 	if (rc)
   1954 		return (rc);
   1955 
   1956 	for (i = cmap->index ; i < cmap->index + cmap->count ; i++) {
   1957 
   1958 		tmpcmap.red   [ scr->colormap[ i ] ] = cmap->red   [ i ];
   1959 		tmpcmap.green [ scr->colormap[ i ] ] = cmap->green [ i ];
   1960 		tmpcmap.blue  [ scr->colormap[ i ] ] = cmap->blue  [ i ];
   1961 	}
   1962 
   1963 	rc = amidisplaycc_setcmap(scr->view, &tmpcmap);
   1964 	if (rc)
   1965 		return (rc);
   1966 
   1967 	return (0);
   1968 }
   1969 
   1970 
   1971 /*
   1972  * Set the colormap for the given screen.
   1973  */
   1974 
   1975 static int
   1976 amidisplaycc_setcmap(view, cmap)
   1977 	view_t                 * view;
   1978 	struct wsdisplay_cmap  * cmap;
   1979 {
   1980 	u_long      cmentries [MAXCOLORS];
   1981 
   1982 	int         green_div;
   1983 	int         blue_div;
   1984 	int         grey_div;
   1985 	int         red_div;
   1986 	u_int       colors;
   1987 	int         index;
   1988 	int         count;
   1989 	int         err;
   1990 	colormap_t  cm;
   1991 	int         c;
   1992 
   1993 	if (view == NULL)
   1994 		return (EINVAL);
   1995 
   1996 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue) {
   1997 		dprintf("amidisplaycc_setcmap: other==NULL\n");
   1998 		return (EINVAL);
   1999 	}
   2000 
   2001 	index  = cmap->index;
   2002 	count  = cmap->count;
   2003 	colors = (1 << view->bitmap->depth);
   2004 
   2005 	if (count > colors || index >= colors || index + count > colors)
   2006 		return (EINVAL);
   2007 
   2008 	if (count == 0)
   2009 		return (0);
   2010 
   2011 	cm.entry = cmentries;
   2012 	cm.first = index;
   2013 	cm.size  = count;
   2014 
   2015 	/*
   2016 	 * Get the old colormap. We need to do this at least to know
   2017 	 * how many bits to use with the color values.
   2018 	 */
   2019 
   2020 	err = grf_get_colormap(view, &cm);
   2021 	if (err)
   2022 		return (err);
   2023 
   2024 	/*
   2025 	 * The palette entries from wscons contain 8 bits per gun.
   2026 	 * We need to convert them to the number of bits the view
   2027 	 * expects. That is typically 4 or 8. Here we calculate the
   2028 	 * conversion constants with which we divide the color values.
   2029 	 */
   2030 
   2031 	if (cm.type == CM_COLOR) {
   2032 		red_div = 256 / (cm.red_mask + 1);
   2033 		green_div = 256 / (cm.green_mask + 1);
   2034 		blue_div = 256 / (cm.blue_mask + 1);
   2035 	} else if (cm.type == CM_GREYSCALE)
   2036 		grey_div = 256 / (cm.grey_mask + 1);
   2037 	else
   2038 		return (EINVAL); /* Hmhh */
   2039 
   2040 	/* Copy our new values to the current colormap */
   2041 
   2042 	for (c = 0 ; c < count ; c++) {
   2043 
   2044 		if (cm.type == CM_COLOR) {
   2045 
   2046 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
   2047 				cmap->red[c] / red_div,
   2048 				cmap->green[c] / green_div,
   2049 				cmap->blue[c] / blue_div);
   2050 
   2051 		} else if (cm.type == CM_GREYSCALE) {
   2052 
   2053 			/* Generate grey from average of r-g-b (?) */
   2054 
   2055 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
   2056 				0,
   2057 				0,
   2058 				(cmap->red[c] +
   2059 				 cmap->green[c] +
   2060 				 cmap->blue[c]) / 3 / grey_div);
   2061 		}
   2062 	}
   2063 
   2064 
   2065 	/*
   2066 	 * Now we have a new colormap that contains all the entries. Set
   2067 	 * it to the view.
   2068 	 */
   2069 
   2070 	err = grf_use_colormap(view, &cm);
   2071 	if (err)
   2072 		return err;
   2073 
   2074 	return (0);
   2075 }
   2076 
   2077 /*
   2078  * Return the colormap of the given screen.
   2079  */
   2080 
   2081 static int
   2082 amidisplaycc_getcmap(view, cmap)
   2083 	view_t                 * view;
   2084 	struct wsdisplay_cmap  * cmap;
   2085 {
   2086 	u_long      cmentries [MAXCOLORS];
   2087 
   2088 	int         green_mul;
   2089 	int         blue_mul;
   2090 	int         grey_mul;
   2091 	int         red_mul;
   2092 	u_int       colors;
   2093 	int         index;
   2094 	int         count;
   2095 	int         err;
   2096 	colormap_t  cm;
   2097 	int         c;
   2098 
   2099 	if (view == NULL)
   2100 		return (EINVAL);
   2101 
   2102 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue)
   2103 		return (EINVAL);
   2104 
   2105 	index  = cmap->index;
   2106 	count  = cmap->count;
   2107 	colors = (1 << view->bitmap->depth);
   2108 
   2109 	if (count > colors || index >= colors || index + count > colors)
   2110 		return (EINVAL);
   2111 
   2112 	if (count == 0)
   2113 		return (0);
   2114 
   2115 	cm.entry = cmentries;
   2116 	cm.first = index;
   2117 	cm.size  = count;
   2118 
   2119 
   2120 	err = grf_get_colormap(view, &cm);
   2121 	if (err)
   2122 		return (err);
   2123 
   2124 	if (cm.type == CM_COLOR) {
   2125 		red_mul   = 256 / (cm.red_mask + 1);
   2126 		green_mul = 256 / (cm.green_mask + 1);
   2127 		blue_mul  = 256 / (cm.blue_mask + 1);
   2128 	} else if (cm.type == CM_GREYSCALE) {
   2129 		grey_mul  = 256 / (cm.grey_mask + 1);
   2130 	} else
   2131 		return (EINVAL);
   2132 
   2133 	/*
   2134 	 * Copy color data to wscons-style structure. Translate to
   2135 	 * 8 bits/gun from whatever resolution the color natively is.
   2136 	 */
   2137 
   2138 	for (c = 0 ; c < count ; c++) {
   2139 
   2140 		if (cm.type == CM_COLOR) {
   2141 
   2142 			cmap->red[c]   = CM_GET_RED(cm.entry[index+c]);
   2143 			cmap->green[c] = CM_GET_GREEN(cm.entry[index+c]);
   2144 			cmap->blue[c]  = CM_GET_BLUE(cm.entry[index+c]);
   2145 
   2146 			cmap->red[c]   *= red_mul;
   2147 			cmap->green[c] *= green_mul;
   2148 			cmap->blue[c]  *= blue_mul;
   2149 
   2150 		} else if (cm.type == CM_GREYSCALE) {
   2151 			cmap->red[c]   = CM_GET_GREY(cm.entry[index+c]);
   2152 			cmap->red[c]  *= grey_mul;
   2153 
   2154 			cmap->green[c] = cmap->red[c];
   2155 			cmap->blue[c]  = cmap->red[c];
   2156 		}
   2157 	}
   2158 
   2159 	return (0);
   2160 }
   2161 
   2162 /* ARGSUSED */
   2163 void amidisplaycc_pollc(cookie, on)
   2164 	void *  cookie;
   2165 	int     on;
   2166 {
   2167 }
   2168 
   2169 /*
   2170  * These dummy functions are here just so that we can compete of
   2171  * the console at init.
   2172  * If we win the console then the wscons system will provide the
   2173  * real ones which in turn will call the apropriate wskbd device.
   2174  * These should never be called.
   2175  */
   2176 
   2177 /* ARGSUSED */
   2178 void
   2179 amidisplaycc_cnputc(cd,ch)
   2180 	dev_t cd;
   2181 	int ch;
   2182 {
   2183 }
   2184 
   2185 /* ARGSUSED */
   2186 int
   2187 amidisplaycc_cngetc(cd)
   2188 	dev_t cd;
   2189 {
   2190 	return (0);
   2191 }
   2192 
   2193 /* ARGSUSED */
   2194 void
   2195 amidisplaycc_cnpollc(cd,on)
   2196 	dev_t cd;
   2197 	int on;
   2198 {
   2199 }
   2200 
   2201 
   2202 /*
   2203  * Prints stuff if DEBUG is turned on.
   2204  */
   2205 
   2206 /* ARGSUSED */
   2207 static void
   2208 dprintf(const char *fmt,...)
   2209 {
   2210 #ifdef DEBUG
   2211 	va_list ap;
   2212 
   2213 	va_start(ap, fmt);
   2214 	vprintf(fmt, ap);
   2215 	va_end(ap);
   2216 #endif
   2217 }
   2218 
   2219 #endif /* AMIDISPLAYCC */
   2220 
   2221 
   2222 
   2223 
   2224 
   2225