2 * Copyright (c) 1998 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * This code is derived from software contributed to The DragonFly Project
6 * by Sascha Wildner <saw@online.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
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.
29 * $FreeBSD: src/sys/dev/syscons/scvidctl.c,v 1.19.2.2 2000/05/05 09:16:08 nyan Exp $
30 * $DragonFly: src/sys/dev/misc/syscons/scvidctl.c,v 1.9 2005/02/18 16:38:23 swildner Exp $
33 #include "opt_syscons.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/signalvar.h>
40 #include <sys/kernel.h>
42 #include <machine/console.h>
44 #include <dev/video/fb/fbreg.h>
47 SET_DECLARE(scrndr_set, const sc_renderer_t);
50 sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize,
59 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
62 /* adjust argument values */
64 fontsize = info.vi_cheight;
67 #ifndef SC_NO_FONT_LOADING
68 if (!(scp->sc->fonts_loaded & FONT_8))
70 font = scp->sc->font_8;
74 } else if (fontsize >= 16) {
76 #ifndef SC_NO_FONT_LOADING
77 if (!(scp->sc->fonts_loaded & FONT_16))
79 font = scp->sc->font_16;
85 #ifndef SC_NO_FONT_LOADING
86 if (!(scp->sc->fonts_loaded & FONT_14))
88 font = scp->sc->font_14;
93 if ((xsize <= 0) || (xsize > info.vi_width))
94 xsize = info.vi_width;
95 if ((ysize <= 0) || (ysize > info.vi_height))
96 ysize = info.vi_height;
98 /* stop screen saver, etc */
100 if ((error = sc_clean_up(scp))) {
105 if (sc_render_match(scp, scp->sc->adp->va_name, 0) == NULL) {
111 #ifndef SC_NO_HISTORY
112 if (scp->history != NULL)
115 prev_ysize = scp->ysize;
117 * This is a kludge to fend off scrn_update() while we
118 * muck around with scp. XXX
120 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
121 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE | MOUSE_VISIBLE);
127 scp->xpixel = scp->xsize*8;
128 scp->ypixel = scp->ysize*fontsize;
130 scp->font_size = fontsize;
132 /* allocate buffers */
133 sc_alloc_scr_buffer(scp, TRUE, TRUE);
134 sc_init_emulator(scp, NULL);
135 #ifndef SC_NO_CUTPASTE
136 sc_alloc_cut_buffer(scp, FALSE);
138 #ifndef SC_NO_HISTORY
139 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
143 if (scp == scp->sc->cur_scp)
145 scp->status &= ~UNKNOWN_MODE;
149 DPRINTF(5, ("ws_*size (%d,%d), size (%d,%d)\n",
150 tp->t_winsize.ws_col, tp->t_winsize.ws_row, scp->xsize, scp->ysize));
151 if (tp->t_winsize.ws_col != scp->xsize
152 || tp->t_winsize.ws_row != scp->ysize) {
153 tp->t_winsize.ws_col = scp->xsize;
154 tp->t_winsize.ws_row = scp->ysize;
155 pgsignal(tp->t_pgrp, SIGWINCH, 1);
162 sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
164 #ifdef SC_NO_MODE_CHANGE
171 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
174 /* stop screen saver, etc */
176 if ((error = sc_clean_up(scp))) {
181 if (sc_render_match(scp, scp->sc->adp->va_name, GRAPHICS_MODE) == NULL) {
187 scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
188 scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
191 * Don't change xsize and ysize; preserve the previous vty
192 * and history buffers.
196 scp->xpixel = info.vi_width;
197 scp->ypixel = info.vi_height;
200 #ifndef SC_NO_SYSMOUSE
201 /* move the mouse cursor at the center of the screen */
202 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
204 sc_init_emulator(scp, NULL);
207 if (scp == scp->sc->cur_scp)
209 /* clear_graphics();*/
210 refresh_ega_palette(scp);
211 scp->status &= ~UNKNOWN_MODE;
215 if (tp->t_winsize.ws_xpixel != scp->xpixel
216 || tp->t_winsize.ws_ypixel != scp->ypixel) {
217 tp->t_winsize.ws_xpixel = scp->xpixel;
218 tp->t_winsize.ws_ypixel = scp->ypixel;
219 pgsignal(tp->t_pgrp, SIGWINCH, 1);
223 #endif /* SC_NO_MODE_CHANGE */
227 sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
230 #ifndef SC_PIXEL_MODE
239 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
240 return ENODEV; /* this shouldn't happen */
242 /* adjust argument values */
244 fontsize = info.vi_cheight;
247 #ifndef SC_NO_FONT_LOADING
248 if (!(scp->sc->fonts_loaded & FONT_8))
250 font = scp->sc->font_8;
254 } else if (fontsize >= 16) {
256 #ifndef SC_NO_FONT_LOADING
257 if (!(scp->sc->fonts_loaded & FONT_16))
259 font = scp->sc->font_16;
265 #ifndef SC_NO_FONT_LOADING
266 if (!(scp->sc->fonts_loaded & FONT_14))
268 font = scp->sc->font_14;
274 xsize = info.vi_width/8;
276 ysize = info.vi_height/fontsize;
278 if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
282 * We currently support the following graphic modes:
284 * - 4 bpp planar modes whose memory size does not exceed 64K
285 * - 15, 16, 24 and 32 bpp linear modes
288 if (info.vi_mem_model == V_INFO_MM_PLANAR) {
289 if (info.vi_planes != 4)
293 * A memory size >64K requires bank switching to access the entire
297 if (info.vi_width * info.vi_height / 8 > info.vi_window_size)
299 } else if (info.vi_mem_model == V_INFO_MM_DIRECT) {
300 if ((info.vi_depth != 15) && (info.vi_depth != 16) &&
301 (info.vi_depth != 24) && (info.vi_depth != 32))
306 /* stop screen saver, etc */
308 if ((error = sc_clean_up(scp))) {
313 if (sc_render_match(scp, scp->sc->adp->va_name, PIXEL_MODE) == NULL) {
320 (*scp->tsw->te_term)(scp, scp->ts);
326 #ifndef SC_NO_HISTORY
327 if (scp->history != NULL)
330 prev_ysize = scp->ysize;
331 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
332 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
335 scp->xoff = (scp->xpixel/8 - xsize)/2;
336 scp->yoff = (scp->ypixel/fontsize - ysize)/2;
338 scp->font_size = fontsize;
340 /* allocate buffers */
341 sc_alloc_scr_buffer(scp, TRUE, TRUE);
342 sc_init_emulator(scp, NULL);
343 #ifndef SC_NO_CUTPASTE
344 sc_alloc_cut_buffer(scp, FALSE);
346 #ifndef SC_NO_HISTORY
347 sc_alloc_history_buffer(scp, 0, prev_ysize, FALSE);
351 if (scp == scp->sc->cur_scp) {
352 sc_set_border(scp, scp->border);
353 sc_set_cursor_image(scp);
356 scp->status &= ~UNKNOWN_MODE;
360 if (tp->t_winsize.ws_col != scp->xsize
361 || tp->t_winsize.ws_row != scp->ysize) {
362 tp->t_winsize.ws_col = scp->xsize;
363 tp->t_winsize.ws_row = scp->ysize;
364 pgsignal(tp->t_pgrp, SIGWINCH, 1);
368 #endif /* SC_PIXEL_MODE */
371 #define fb_ioctl(a, c, d) \
372 (((a) == NULL) ? ENODEV : \
373 (*vidsw[(a)->va_index]->ioctl)((a), (c), (caddr_t)(d)))
376 sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td)
379 video_adapter_t *adp;
384 scp = SC_STAT(tp->t_dev);
385 if (scp == NULL) /* tp == SC_MOUSE */
388 if (adp == NULL) /* shouldn't happen??? */
393 case CONS_CURRENTADP: /* get current adapter index */
395 return fb_ioctl(adp, FBIO_ADAPTER, data);
397 case CONS_CURRENT: /* get current adapter type */
399 return fb_ioctl(adp, FBIO_ADPTYPE, data);
401 case CONS_ADPINFO: /* adapter information */
403 if (((video_adapter_info_t *)data)->va_index >= 0) {
404 adp = vid_get_adapter(((video_adapter_info_t *)data)->va_index);
408 return fb_ioctl(adp, FBIO_ADPINFO, data);
410 case CONS_GET: /* get current video mode */
412 *(int *)data = scp->mode;
415 #ifndef SC_NO_MODE_CHANGE
416 case FBIO_SETMODE: /* set video mode */
417 if (!(adp->va_flags & V_ADP_MODECHANGE))
419 info.vi_mode = *(int *)data;
420 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
423 if (info.vi_flags & V_INFO_GRAPHICS)
424 return sc_set_graphics_mode(scp, tp, *(int *)data);
426 return sc_set_text_mode(scp, tp, *(int *)data, 0, 0, 0);
427 #endif /* SC_NO_MODE_CHANGE */
429 case CONS_MODEINFO: /* get mode information */
431 return fb_ioctl(adp, FBIO_MODEINFO, data);
433 case CONS_FINDMODE: /* find a matching video mode */
435 return fb_ioctl(adp, FBIO_FINDMODE, data);
437 case CONS_SETWINORG: /* set frame buffer window origin */
439 if (scp != scp->sc->cur_scp)
440 return ENODEV; /* XXX */
441 return fb_ioctl(adp, FBIO_SETWINORG, data);
443 case FBIO_GETWINORG: /* get frame buffer window origin */
444 if (scp != scp->sc->cur_scp)
445 return ENODEV; /* XXX */
446 return fb_ioctl(adp, FBIO_GETWINORG, data);
448 case FBIO_GETDISPSTART:
449 case FBIO_SETDISPSTART:
450 case FBIO_GETLINEWIDTH:
451 case FBIO_SETLINEWIDTH:
452 if (scp != scp->sc->cur_scp)
453 return ENODEV; /* XXX */
454 return fb_ioctl(adp, cmd, data);
456 case FBIO_GETPALETTE:
457 case FBIO_SETPALETTE:
469 if (scp != scp->sc->cur_scp)
470 return ENODEV; /* XXX */
471 return fb_ioctl(adp, cmd, data);
473 #ifndef SC_NO_MODE_CHANGE
474 /* generic text modes */
475 case SW_TEXT_80x25: case SW_TEXT_80x30:
476 case SW_TEXT_80x43: case SW_TEXT_80x50:
482 case SW_VGA_C80x25: case SW_VGA_M80x25:
483 case SW_VGA_C80x30: case SW_VGA_M80x30:
484 case SW_VGA_C80x50: case SW_VGA_M80x50:
485 case SW_VGA_C80x60: case SW_VGA_M80x60:
486 case SW_VGA_C90x25: case SW_VGA_M90x25:
487 case SW_VGA_C90x30: case SW_VGA_M90x30:
488 case SW_VGA_C90x43: case SW_VGA_M90x43:
489 case SW_VGA_C90x50: case SW_VGA_M90x50:
490 case SW_VGA_C90x60: case SW_VGA_M90x60:
491 case SW_B40x25: case SW_C40x25:
492 case SW_B80x25: case SW_C80x25:
493 case SW_ENH_B40x25: case SW_ENH_C40x25:
494 case SW_ENH_B80x25: case SW_ENH_C80x25:
495 case SW_ENH_B80x43: case SW_ENH_C80x43:
496 case SW_EGAMONO80x25:
497 if (!(adp->va_flags & V_ADP_MODECHANGE))
499 return sc_set_text_mode(scp, tp, cmd & 0xff, 0, 0, 0);
502 case SW_BG320: case SW_BG640:
503 case SW_CG320: case SW_CG320_D: case SW_CG640_E:
504 case SW_CG640x350: case SW_ENH_CG640:
505 case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
507 if (!(adp->va_flags & V_ADP_MODECHANGE))
509 return sc_set_graphics_mode(scp, tp, cmd & 0xff);
510 #endif /* SC_NO_MODE_CHANGE */
512 case KDSETMODE: /* set current mode of this (virtual) console */
513 switch (*(int *)data) {
514 case KD_TEXT: /* switch to TEXT (known) mode */
516 * If scp->mode is of graphics modes, we don't know which
517 * text mode to switch back to...
519 if (scp->status & GRAPHICS_MODE)
521 /* restore fonts & palette ! */
523 #ifndef SC_NO_FONT_LOADING
524 if (ISFONTAVAIL(adp->va_flags)
525 && !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
528 * Don't load fonts for now... XXX
530 if (scp->sc->fonts_loaded & FONT_8)
531 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
532 if (scp->sc->fonts_loaded & FONT_14)
533 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
534 if (scp->sc->fonts_loaded & FONT_16)
535 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
537 #endif /* SC_NO_FONT_LOADING */
540 #ifndef SC_NO_PALETTE_LOADING
541 load_palette(adp, scp->sc->palette);
544 /* move hardware cursor out of the way */
545 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
548 case KD_TEXT1: /* switch to TEXT (known) mode */
550 * If scp->mode is of graphics modes, we don't know which
551 * text/pixel mode to switch back to...
553 if (scp->status & GRAPHICS_MODE)
556 if ((error = sc_clean_up(scp))) {
560 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
562 /* no restore fonts & palette */
563 if (scp == scp->sc->cur_scp)
565 sc_clear_screen(scp);
566 scp->status &= ~UNKNOWN_MODE;
570 case KD_PIXEL: /* pixel (raster) display */
571 if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
573 if (scp->status & GRAPHICS_MODE)
574 return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
577 if ((error = sc_clean_up(scp))) {
581 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
583 if (scp == scp->sc->cur_scp) {
585 #ifndef SC_NO_PALETTE_LOADING
586 load_palette(adp, scp->sc->palette);
589 sc_clear_screen(scp);
590 scp->status &= ~UNKNOWN_MODE;
592 #endif /* SC_PIXEL_MODE */
594 case KD_GRAPHICS: /* switch to GRAPHICS (unknown) mode */
596 if ((error = sc_clean_up(scp))) {
600 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
610 case KDRASTER: /* set pixel (raster) display mode */
611 if (ISUNKNOWNSC(scp) || ISTEXTSC(scp))
613 return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
615 #endif /* SC_PIXEL_MODE */
617 case KDGETMODE: /* get current mode of this (virtual) console */
619 * From the user program's point of view, KD_PIXEL is the same
622 *data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
625 case KDSBORDER: /* set border color of this (virtual) console */
627 if (scp == scp->sc->cur_scp)
628 sc_set_border(scp, scp->border);
635 static LIST_HEAD(, sc_renderer) sc_rndr_list =
636 LIST_HEAD_INITIALIZER(sc_rndr_list);
639 sc_render_add(sc_renderer_t *rndr)
641 LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
646 sc_render_remove(sc_renderer_t *rndr)
649 LIST_REMOVE(rndr, link);
651 return EBUSY; /* XXX */
655 *sc_render_match(scr_stat *scp, char *name, int mode)
657 const sc_renderer_t **list;
658 const sc_renderer_t *p;
660 if (!LIST_EMPTY(&sc_rndr_list)) {
661 LIST_FOREACH(p, &sc_rndr_list, link) {
662 if ((strcmp(p->name, name) == 0)
663 && (mode == p->mode)) {
665 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
670 SET_FOREACH(list, scrndr_set) {
672 if ((strcmp(p->name, name) == 0)
673 && (mode == p->mode)) {
675 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);