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, V_INFO_MM_TEXT) == 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);
127 scp->model = V_INFO_MM_TEXT;
132 scp->xpixel = scp->xsize*8;
133 scp->ypixel = scp->ysize*fontsize;
135 scp->font_size = fontsize;
137 /* allocate buffers */
138 sc_alloc_scr_buffer(scp, TRUE, TRUE);
139 sc_init_emulator(scp, NULL);
140 #ifndef SC_NO_CUTPASTE
141 sc_alloc_cut_buffer(scp, FALSE);
143 #ifndef SC_NO_HISTORY
144 sc_alloc_history_buffer(scp, new_ysize, prev_ysize, FALSE);
148 if (scp == scp->sc->cur_scp)
150 scp->status &= ~UNKNOWN_MODE;
154 DPRINTF(5, ("ws_*size (%d,%d), size (%d,%d)\n",
155 tp->t_winsize.ws_col, tp->t_winsize.ws_row, scp->xsize, scp->ysize));
156 if (tp->t_winsize.ws_col != scp->xsize
157 || tp->t_winsize.ws_row != scp->ysize) {
158 tp->t_winsize.ws_col = scp->xsize;
159 tp->t_winsize.ws_row = scp->ysize;
160 pgsignal(tp->t_pgrp, SIGWINCH, 1);
167 sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
169 #ifdef SC_NO_MODE_CHANGE
175 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info))
178 /* stop screen saver, etc */
180 if ((error = sc_clean_up(scp))) {
185 if (sc_render_match(scp, scp->sc->adp->va_name, V_INFO_MM_OTHER) == NULL) {
191 scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
192 scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
194 scp->model = V_INFO_MM_OTHER;
196 * Don't change xsize and ysize; preserve the previous vty
197 * and history buffers.
201 scp->xpixel = info.vi_width;
202 scp->ypixel = info.vi_height;
205 #ifndef SC_NO_SYSMOUSE
206 /* move the mouse cursor at the center of the screen */
207 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
209 sc_init_emulator(scp, NULL);
212 if (scp == scp->sc->cur_scp)
214 /* clear_graphics();*/
215 refresh_ega_palette(scp);
216 scp->status &= ~UNKNOWN_MODE;
220 if (tp->t_winsize.ws_xpixel != scp->xpixel
221 || tp->t_winsize.ws_ypixel != scp->ypixel) {
222 tp->t_winsize.ws_xpixel = scp->xpixel;
223 tp->t_winsize.ws_ypixel = scp->ypixel;
224 pgsignal(tp->t_pgrp, SIGWINCH, 1);
228 #endif /* SC_NO_MODE_CHANGE */
232 sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
235 #ifndef SC_PIXEL_MODE
244 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
245 return ENODEV; /* this shouldn't happen */
247 /* adjust argument values */
249 fontsize = info.vi_cheight;
252 #ifndef SC_NO_FONT_LOADING
253 if (!(scp->sc->fonts_loaded & FONT_8))
255 font = scp->sc->font_8;
259 } else if (fontsize >= 16) {
261 #ifndef SC_NO_FONT_LOADING
262 if (!(scp->sc->fonts_loaded & FONT_16))
264 font = scp->sc->font_16;
270 #ifndef SC_NO_FONT_LOADING
271 if (!(scp->sc->fonts_loaded & FONT_14))
273 font = scp->sc->font_14;
279 xsize = info.vi_width/8;
281 ysize = info.vi_height/fontsize;
283 if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
287 * We currently support the following graphic modes:
289 * - 4 bpp planar modes whose memory size does not exceed 64K
290 * - 15, 16, 24 and 32 bpp direct modes with linear frame buffer
293 if (info.vi_mem_model == V_INFO_MM_PLANAR) {
294 if (info.vi_planes != 4)
298 * A memory size >64K requires bank switching to access the entire
302 if (info.vi_width * info.vi_height / 8 > info.vi_window_size)
304 } else if (info.vi_mem_model == V_INFO_MM_DIRECT) {
305 if (!(info.vi_flags & V_INFO_LINEAR) &&
306 (info.vi_depth != 15) && (info.vi_depth != 16) &&
307 (info.vi_depth != 24) && (info.vi_depth != 32))
312 /* stop screen saver, etc */
314 if ((error = sc_clean_up(scp))) {
319 if (sc_render_match(scp, scp->sc->adp->va_name, info.vi_mem_model) == NULL) {
326 (*scp->tsw->te_term)(scp, scp->ts);
333 #ifndef SC_NO_HISTORY
334 if (scp->history != NULL) {
336 new_ysize = sc_vtb_rows(scp->history);
339 prev_ysize = scp->ysize;
340 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
341 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
342 scp->model = info.vi_mem_model;
345 scp->xoff = (scp->xpixel/8 - xsize)/2;
346 scp->yoff = (scp->ypixel/fontsize - ysize)/2;
348 scp->font_size = fontsize;
350 /* allocate buffers */
351 sc_alloc_scr_buffer(scp, TRUE, TRUE);
352 sc_init_emulator(scp, NULL);
353 #ifndef SC_NO_CUTPASTE
354 sc_alloc_cut_buffer(scp, FALSE);
356 #ifndef SC_NO_HISTORY
357 sc_alloc_history_buffer(scp, new_ysize, prev_ysize, FALSE);
361 if (scp == scp->sc->cur_scp) {
362 sc_set_border(scp, scp->border);
363 sc_set_cursor_image(scp);
366 scp->status &= ~UNKNOWN_MODE;
370 if (tp->t_winsize.ws_col != scp->xsize
371 || tp->t_winsize.ws_row != scp->ysize) {
372 tp->t_winsize.ws_col = scp->xsize;
373 tp->t_winsize.ws_row = scp->ysize;
374 pgsignal(tp->t_pgrp, SIGWINCH, 1);
378 #endif /* SC_PIXEL_MODE */
381 #define fb_ioctl(a, c, d) \
382 (((a) == NULL) ? ENODEV : \
383 (*vidsw[(a)->va_index]->ioctl)((a), (c), (caddr_t)(d)))
386 sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag)
389 video_adapter_t *adp;
395 scp = SC_STAT(tp->t_dev);
396 if (scp == NULL) /* tp == SC_MOUSE */
399 if (adp == NULL) /* shouldn't happen??? */
404 case CONS_CURRENTADP: /* get current adapter index */
406 return fb_ioctl(adp, FBIO_ADAPTER, data);
408 case CONS_CURRENT: /* get current adapter type */
410 return fb_ioctl(adp, FBIO_ADPTYPE, data);
412 case CONS_ADPINFO: /* adapter information */
414 if (((video_adapter_info_t *)data)->va_index >= 0) {
415 adp = vid_get_adapter(((video_adapter_info_t *)data)->va_index);
419 return fb_ioctl(adp, FBIO_ADPINFO, data);
421 case CONS_GET: /* get current video mode */
423 *(int *)data = scp->mode;
426 #ifndef SC_NO_MODE_CHANGE
428 case FBIO_SETMODE: /* set video mode */
429 if (!(adp->va_flags & V_ADP_MODECHANGE))
431 info.vi_mode = *(int *)data;
432 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
435 if (info.vi_flags & V_INFO_GRAPHICS)
436 return sc_set_graphics_mode(scp, tp, *(int *)data);
438 return sc_set_text_mode(scp, tp, *(int *)data, 0, 0, 0);
439 #endif /* SC_NO_MODE_CHANGE */
441 case CONS_MODEINFO: /* get mode information */
443 return fb_ioctl(adp, FBIO_MODEINFO, data);
445 case CONS_FINDMODE: /* find a matching video mode */
447 return fb_ioctl(adp, FBIO_FINDMODE, data);
449 case CONS_SETWINORG: /* set frame buffer window origin */
451 if (scp != scp->sc->cur_scp)
452 return ENODEV; /* XXX */
453 return fb_ioctl(adp, FBIO_SETWINORG, data);
455 case FBIO_GETWINORG: /* get frame buffer window origin */
456 if (scp != scp->sc->cur_scp)
457 return ENODEV; /* XXX */
458 return fb_ioctl(adp, FBIO_GETWINORG, data);
460 case FBIO_GETDISPSTART:
461 case FBIO_SETDISPSTART:
462 case FBIO_GETLINEWIDTH:
463 case FBIO_SETLINEWIDTH:
464 if (scp != scp->sc->cur_scp)
465 return ENODEV; /* XXX */
466 return fb_ioctl(adp, cmd, data);
468 case FBIO_GETPALETTE:
469 case FBIO_SETPALETTE:
481 if (scp != scp->sc->cur_scp)
482 return ENODEV; /* XXX */
483 return fb_ioctl(adp, cmd, data);
485 case KDSETMODE: /* set current mode of this (virtual) console */
486 switch (*(int *)data) {
487 case KD_TEXT: /* switch to TEXT (known) mode */
489 * If scp->mode is of graphics modes, we don't know which
490 * text mode to switch back to...
492 if (scp->status & GRAPHICS_MODE)
494 /* restore fonts & palette ! */
496 #ifndef SC_NO_FONT_LOADING
497 if (ISFONTAVAIL(adp->va_flags)
498 && !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
501 * Don't load fonts for now... XXX
503 if (scp->sc->fonts_loaded & FONT_8)
504 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
505 if (scp->sc->fonts_loaded & FONT_14)
506 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
507 if (scp->sc->fonts_loaded & FONT_16)
508 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
510 #endif /* SC_NO_FONT_LOADING */
513 #ifndef SC_NO_PALETTE_LOADING
514 load_palette(adp, scp->sc->palette);
517 /* move hardware cursor out of the way */
518 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
521 case KD_TEXT1: /* switch to TEXT (known) mode */
523 * If scp->mode is of graphics modes, we don't know which
524 * text/pixel mode to switch back to...
526 if (scp->status & GRAPHICS_MODE)
529 if ((error = sc_clean_up(scp))) {
533 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
535 /* no restore fonts & palette */
536 if (scp == scp->sc->cur_scp)
538 sc_clear_screen(scp);
539 scp->status &= ~UNKNOWN_MODE;
543 case KD_PIXEL: /* pixel (raster) display */
544 if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
546 if (scp->status & GRAPHICS_MODE)
547 return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
550 if ((error = sc_clean_up(scp))) {
554 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
556 if (scp == scp->sc->cur_scp) {
558 #ifndef SC_NO_PALETTE_LOADING
559 load_palette(adp, scp->sc->palette);
562 sc_clear_screen(scp);
563 scp->status &= ~UNKNOWN_MODE;
565 #endif /* SC_PIXEL_MODE */
567 case KD_GRAPHICS: /* switch to GRAPHICS (unknown) mode */
569 if ((error = sc_clean_up(scp))) {
573 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
583 case KDRASTER: /* set pixel (raster) display mode */
584 if (ISUNKNOWNSC(scp) || ISTEXTSC(scp))
586 return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
588 #endif /* SC_PIXEL_MODE */
590 case KDGETMODE: /* get current mode of this (virtual) console */
592 * From the user program's point of view, KD_PIXEL is the same
595 *data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
598 case KDSBORDER: /* set border color of this (virtual) console */
600 if (scp == scp->sc->cur_scp)
601 sc_set_border(scp, scp->border);
608 static LIST_HEAD(, sc_renderer) sc_rndr_list =
609 LIST_HEAD_INITIALIZER(sc_rndr_list);
612 sc_render_add(sc_renderer_t *rndr)
614 LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
619 sc_render_remove(sc_renderer_t *rndr)
622 LIST_REMOVE(rndr, link);
624 return EBUSY; /* XXX */
628 sc_render_match(scr_stat *scp, char *name, int model)
630 const sc_renderer_t **list;
631 const sc_renderer_t *p;
633 if (!LIST_EMPTY(&sc_rndr_list)) {
634 LIST_FOREACH(p, &sc_rndr_list, link) {
635 if ((strcmp(p->name, name) == 0) &&
636 (model == p->model)) {
638 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
643 SET_FOREACH(list, scrndr_set) {
645 if ((strcmp(p->name, name) == 0) &&
646 (model == p->model)) {
648 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);