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.16 2007/08/19 11:39:11 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>
41 #include <sys/thread2.h>
43 #include <machine/console.h>
45 #include <dev/video/fb/fbreg.h>
48 SET_DECLARE(scrndr_set, const sc_renderer_t);
51 sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize,
60 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
63 /* adjust argument values */
65 fontsize = info.vi_cheight;
68 #ifndef SC_NO_FONT_LOADING
69 if (!(scp->sc->fonts_loaded & FONT_8))
71 font = scp->sc->font_8;
75 } else if (fontsize >= 16) {
77 #ifndef SC_NO_FONT_LOADING
78 if (!(scp->sc->fonts_loaded & FONT_16))
80 font = scp->sc->font_16;
86 #ifndef SC_NO_FONT_LOADING
87 if (!(scp->sc->fonts_loaded & FONT_14))
89 font = scp->sc->font_14;
94 if ((xsize <= 0) || (xsize > info.vi_width))
95 xsize = info.vi_width;
96 if ((ysize <= 0) || (ysize > info.vi_height))
97 ysize = info.vi_height;
99 /* stop screen saver, etc */
101 if ((error = sc_clean_up(scp))) {
106 if (sc_render_match(scp, scp->sc->adp->va_name, 0) == NULL) {
113 #ifndef SC_NO_HISTORY
114 if (scp->history != NULL) {
116 new_ysize = sc_vtb_rows(scp->history);
119 prev_ysize = scp->ysize;
121 * This is a kludge to fend off scrn_update() while we
122 * muck around with scp. XXX
124 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
125 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE | MOUSE_VISIBLE);
131 scp->xpixel = scp->xsize*8;
132 scp->ypixel = scp->ysize*fontsize;
134 scp->font_size = fontsize;
136 /* allocate buffers */
137 sc_alloc_scr_buffer(scp, TRUE, TRUE);
138 sc_init_emulator(scp, NULL);
139 #ifndef SC_NO_CUTPASTE
140 sc_alloc_cut_buffer(scp, FALSE);
142 #ifndef SC_NO_HISTORY
143 sc_alloc_history_buffer(scp, new_ysize, prev_ysize, FALSE);
147 if (scp == scp->sc->cur_scp)
149 scp->status &= ~UNKNOWN_MODE;
153 DPRINTF(5, ("ws_*size (%d,%d), size (%d,%d)\n",
154 tp->t_winsize.ws_col, tp->t_winsize.ws_row, scp->xsize, scp->ysize));
155 if (tp->t_winsize.ws_col != scp->xsize
156 || tp->t_winsize.ws_row != scp->ysize) {
157 tp->t_winsize.ws_col = scp->xsize;
158 tp->t_winsize.ws_row = scp->ysize;
159 pgsignal(tp->t_pgrp, SIGWINCH, 1);
166 sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
168 #ifdef SC_NO_MODE_CHANGE
174 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
177 /* stop screen saver, etc */
179 if ((error = sc_clean_up(scp))) {
184 if (sc_render_match(scp, scp->sc->adp->va_name, GRAPHICS_MODE) == NULL) {
190 scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
191 scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
194 * Don't change xsize and ysize; preserve the previous vty
195 * and history buffers.
199 scp->xpixel = info.vi_width;
200 scp->ypixel = info.vi_height;
203 #ifndef SC_NO_SYSMOUSE
204 /* move the mouse cursor at the center of the screen */
205 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
207 sc_init_emulator(scp, NULL);
210 if (scp == scp->sc->cur_scp)
212 /* clear_graphics();*/
213 refresh_ega_palette(scp);
214 scp->status &= ~UNKNOWN_MODE;
218 if (tp->t_winsize.ws_xpixel != scp->xpixel
219 || tp->t_winsize.ws_ypixel != scp->ypixel) {
220 tp->t_winsize.ws_xpixel = scp->xpixel;
221 tp->t_winsize.ws_ypixel = scp->ypixel;
222 pgsignal(tp->t_pgrp, SIGWINCH, 1);
226 #endif /* SC_NO_MODE_CHANGE */
230 sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
233 #ifndef SC_PIXEL_MODE
242 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
243 return ENODEV; /* this shouldn't happen */
245 /* adjust argument values */
247 fontsize = info.vi_cheight;
250 #ifndef SC_NO_FONT_LOADING
251 if (!(scp->sc->fonts_loaded & FONT_8))
253 font = scp->sc->font_8;
257 } else if (fontsize >= 16) {
259 #ifndef SC_NO_FONT_LOADING
260 if (!(scp->sc->fonts_loaded & FONT_16))
262 font = scp->sc->font_16;
268 #ifndef SC_NO_FONT_LOADING
269 if (!(scp->sc->fonts_loaded & FONT_14))
271 font = scp->sc->font_14;
277 xsize = info.vi_width/8;
279 ysize = info.vi_height/fontsize;
281 if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
285 * We currently support the following graphic modes:
287 * - 4 bpp planar modes whose memory size does not exceed 64K
288 * - 15, 16, 24 and 32 bpp direct modes with linear frame buffer
291 if (info.vi_mem_model == V_INFO_MM_PLANAR) {
292 if (info.vi_planes != 4)
296 * A memory size >64K requires bank switching to access the entire
300 if (info.vi_width * info.vi_height / 8 > info.vi_window_size)
302 } else if (info.vi_mem_model == V_INFO_MM_DIRECT) {
303 if (!(info.vi_flags & V_INFO_LINEAR) &&
304 (info.vi_depth != 15) && (info.vi_depth != 16) &&
305 (info.vi_depth != 24) && (info.vi_depth != 32))
310 /* stop screen saver, etc */
312 if ((error = sc_clean_up(scp))) {
317 if (sc_render_match(scp, scp->sc->adp->va_name, PIXEL_MODE) == NULL) {
324 (*scp->tsw->te_term)(scp, scp->ts);
331 #ifndef SC_NO_HISTORY
332 if (scp->history != NULL) {
334 new_ysize = sc_vtb_rows(scp->history);
337 prev_ysize = scp->ysize;
338 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
339 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
342 scp->xoff = (scp->xpixel/8 - xsize)/2;
343 scp->yoff = (scp->ypixel/fontsize - ysize)/2;
345 scp->font_size = fontsize;
347 /* allocate buffers */
348 sc_alloc_scr_buffer(scp, TRUE, TRUE);
349 sc_init_emulator(scp, NULL);
350 #ifndef SC_NO_CUTPASTE
351 sc_alloc_cut_buffer(scp, FALSE);
353 #ifndef SC_NO_HISTORY
354 sc_alloc_history_buffer(scp, new_ysize, prev_ysize, FALSE);
358 if (scp == scp->sc->cur_scp) {
359 sc_set_border(scp, scp->border);
360 sc_set_cursor_image(scp);
363 scp->status &= ~UNKNOWN_MODE;
367 if (tp->t_winsize.ws_col != scp->xsize
368 || tp->t_winsize.ws_row != scp->ysize) {
369 tp->t_winsize.ws_col = scp->xsize;
370 tp->t_winsize.ws_row = scp->ysize;
371 pgsignal(tp->t_pgrp, SIGWINCH, 1);
375 #endif /* SC_PIXEL_MODE */
378 #define fb_ioctl(a, c, d) \
379 (((a) == NULL) ? ENODEV : \
380 (*vidsw[(a)->va_index]->ioctl)((a), (c), (caddr_t)(d)))
383 sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag)
386 video_adapter_t *adp;
392 scp = SC_STAT(tp->t_dev);
393 if (scp == NULL) /* tp == SC_MOUSE */
396 if (adp == NULL) /* shouldn't happen??? */
401 case CONS_CURRENTADP: /* get current adapter index */
403 return fb_ioctl(adp, FBIO_ADAPTER, data);
405 case CONS_CURRENT: /* get current adapter type */
407 return fb_ioctl(adp, FBIO_ADPTYPE, data);
409 case CONS_ADPINFO: /* adapter information */
411 if (((video_adapter_info_t *)data)->va_index >= 0) {
412 adp = vid_get_adapter(((video_adapter_info_t *)data)->va_index);
416 return fb_ioctl(adp, FBIO_ADPINFO, data);
418 case CONS_GET: /* get current video mode */
420 *(int *)data = scp->mode;
423 #ifndef SC_NO_MODE_CHANGE
425 case FBIO_SETMODE: /* set video mode */
426 if (!(adp->va_flags & V_ADP_MODECHANGE))
428 info.vi_mode = *(int *)data;
429 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
432 if (info.vi_flags & V_INFO_GRAPHICS)
433 return sc_set_graphics_mode(scp, tp, *(int *)data);
435 return sc_set_text_mode(scp, tp, *(int *)data, 0, 0, 0);
436 #endif /* SC_NO_MODE_CHANGE */
438 case CONS_MODEINFO: /* get mode information */
440 return fb_ioctl(adp, FBIO_MODEINFO, data);
442 case CONS_FINDMODE: /* find a matching video mode */
444 return fb_ioctl(adp, FBIO_FINDMODE, data);
446 case CONS_SETWINORG: /* set frame buffer window origin */
448 if (scp != scp->sc->cur_scp)
449 return ENODEV; /* XXX */
450 return fb_ioctl(adp, FBIO_SETWINORG, data);
452 case FBIO_GETWINORG: /* get frame buffer window origin */
453 if (scp != scp->sc->cur_scp)
454 return ENODEV; /* XXX */
455 return fb_ioctl(adp, FBIO_GETWINORG, data);
457 case FBIO_GETDISPSTART:
458 case FBIO_SETDISPSTART:
459 case FBIO_GETLINEWIDTH:
460 case FBIO_SETLINEWIDTH:
461 if (scp != scp->sc->cur_scp)
462 return ENODEV; /* XXX */
463 return fb_ioctl(adp, cmd, data);
465 case FBIO_GETPALETTE:
466 case FBIO_SETPALETTE:
478 if (scp != scp->sc->cur_scp)
479 return ENODEV; /* XXX */
480 return fb_ioctl(adp, cmd, data);
482 case KDSETMODE: /* set current mode of this (virtual) console */
483 switch (*(int *)data) {
484 case KD_TEXT: /* switch to TEXT (known) mode */
486 * If scp->mode is of graphics modes, we don't know which
487 * text mode to switch back to...
489 if (scp->status & GRAPHICS_MODE)
491 /* restore fonts & palette ! */
493 #ifndef SC_NO_FONT_LOADING
494 if (ISFONTAVAIL(adp->va_flags)
495 && !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
498 * Don't load fonts for now... XXX
500 if (scp->sc->fonts_loaded & FONT_8)
501 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
502 if (scp->sc->fonts_loaded & FONT_14)
503 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
504 if (scp->sc->fonts_loaded & FONT_16)
505 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
507 #endif /* SC_NO_FONT_LOADING */
510 #ifndef SC_NO_PALETTE_LOADING
511 load_palette(adp, scp->sc->palette);
514 /* move hardware cursor out of the way */
515 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
518 case KD_TEXT1: /* switch to TEXT (known) mode */
520 * If scp->mode is of graphics modes, we don't know which
521 * text/pixel mode to switch back to...
523 if (scp->status & GRAPHICS_MODE)
526 if ((error = sc_clean_up(scp))) {
530 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
532 /* no restore fonts & palette */
533 if (scp == scp->sc->cur_scp)
535 sc_clear_screen(scp);
536 scp->status &= ~UNKNOWN_MODE;
540 case KD_PIXEL: /* pixel (raster) display */
541 if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
543 if (scp->status & GRAPHICS_MODE)
544 return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
547 if ((error = sc_clean_up(scp))) {
551 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
553 if (scp == scp->sc->cur_scp) {
555 #ifndef SC_NO_PALETTE_LOADING
556 load_palette(adp, scp->sc->palette);
559 sc_clear_screen(scp);
560 scp->status &= ~UNKNOWN_MODE;
562 #endif /* SC_PIXEL_MODE */
564 case KD_GRAPHICS: /* switch to GRAPHICS (unknown) mode */
566 if ((error = sc_clean_up(scp))) {
570 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
580 case KDRASTER: /* set pixel (raster) display mode */
581 if (ISUNKNOWNSC(scp) || ISTEXTSC(scp))
583 return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
585 #endif /* SC_PIXEL_MODE */
587 case KDGETMODE: /* get current mode of this (virtual) console */
589 * From the user program's point of view, KD_PIXEL is the same
592 *data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
595 case KDSBORDER: /* set border color of this (virtual) console */
597 if (scp == scp->sc->cur_scp)
598 sc_set_border(scp, scp->border);
605 static LIST_HEAD(, sc_renderer) sc_rndr_list =
606 LIST_HEAD_INITIALIZER(sc_rndr_list);
609 sc_render_add(sc_renderer_t *rndr)
611 LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
616 sc_render_remove(sc_renderer_t *rndr)
619 LIST_REMOVE(rndr, link);
621 return EBUSY; /* XXX */
625 sc_render_match(scr_stat *scp, char *name, int mode)
627 const sc_renderer_t **list;
628 const sc_renderer_t *p;
630 if (!LIST_EMPTY(&sc_rndr_list)) {
631 LIST_FOREACH(p, &sc_rndr_list, link) {
632 if ((strcmp(p->name, name) == 0)
633 && (mode == p->mode)) {
635 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
640 SET_FOREACH(list, scrndr_set) {
642 if ((strcmp(p->name, name) == 0)
643 && (mode == p->mode)) {
645 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);