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