4 * Copyright (c) 1998 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * This code is derived from software contributed to The DragonFly Project
8 * by Sascha Wildner <saw@online.de>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer as
15 * the first lines of this file unmodified.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $FreeBSD: src/sys/dev/syscons/scvidctl.c,v 1.19.2.2 2000/05/05 09:16:08 nyan Exp $
32 * $DragonFly: src/sys/dev/misc/syscons/scvidctl.c,v 1.16 2007/08/19 11:39:11 swildner Exp $
35 #include "opt_syscons.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/signalvar.h>
42 #include <sys/kernel.h>
43 #include <sys/thread2.h>
45 #include <machine/console.h>
47 #include <dev/video/fb/fbreg.h>
50 SET_DECLARE(scrndr_set, const sc_renderer_t);
53 sc_set_text_mode(scr_stat *scp, struct tty *tp, int mode, int xsize, int ysize,
62 lwkt_gettoken(&tty_token);
63 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info)) {
64 lwkt_reltoken(&tty_token);
67 lwkt_reltoken(&tty_token);
69 /* adjust argument values */
71 fontsize = info.vi_cheight;
74 #ifndef SC_NO_FONT_LOADING
75 if (!(scp->sc->fonts_loaded & FONT_8))
77 font = scp->sc->font_8;
81 } else if (fontsize >= 16) {
83 #ifndef SC_NO_FONT_LOADING
84 if (!(scp->sc->fonts_loaded & FONT_16))
86 font = scp->sc->font_16;
92 #ifndef SC_NO_FONT_LOADING
93 if (!(scp->sc->fonts_loaded & FONT_14))
95 font = scp->sc->font_14;
100 if ((xsize <= 0) || (xsize > info.vi_width))
101 xsize = info.vi_width;
102 if ((ysize <= 0) || (ysize > info.vi_height))
103 ysize = info.vi_height;
105 /* stop screen saver, etc */
107 if ((error = sc_clean_up(scp))) {
112 if (sc_render_match(scp, scp->sc->adp->va_name, V_INFO_MM_TEXT) == NULL) {
119 #ifndef SC_NO_HISTORY
120 if (scp->history != NULL) {
122 new_ysize = sc_vtb_rows(scp->history);
125 prev_ysize = scp->ysize;
127 * This is a kludge to fend off scrn_update() while we
128 * muck around with scp. XXX
130 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
131 scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE | MOUSE_VISIBLE);
133 scp->model = V_INFO_MM_TEXT;
138 scp->xpixel = scp->xsize*8;
139 scp->ypixel = scp->ysize*fontsize;
141 scp->font_size = fontsize;
143 /* allocate buffers */
144 sc_alloc_scr_buffer(scp, TRUE, TRUE);
145 sc_init_emulator(scp, NULL);
146 #ifndef SC_NO_CUTPASTE
147 sc_alloc_cut_buffer(scp, FALSE);
149 #ifndef SC_NO_HISTORY
150 sc_alloc_history_buffer(scp, new_ysize, prev_ysize, FALSE);
154 if (scp == scp->sc->cur_scp)
156 scp->status &= ~UNKNOWN_MODE;
160 DPRINTF(5, ("ws_*size (%d,%d), size (%d,%d)\n",
161 tp->t_winsize.ws_col, tp->t_winsize.ws_row, scp->xsize, scp->ysize));
162 if (tp->t_winsize.ws_col != scp->xsize
163 || tp->t_winsize.ws_row != scp->ysize) {
164 tp->t_winsize.ws_col = scp->xsize;
165 tp->t_winsize.ws_row = scp->ysize;
166 pgsignal(tp->t_pgrp, SIGWINCH, 1);
173 sc_set_graphics_mode(scr_stat *scp, struct tty *tp, int mode)
175 #ifdef SC_NO_MODE_CHANGE
181 lwkt_gettoken(&tty_token);
182 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, mode, &info)) {
183 lwkt_reltoken(&tty_token);
186 lwkt_reltoken(&tty_token);
188 /* stop screen saver, etc */
190 if ((error = sc_clean_up(scp))) {
195 if (sc_render_match(scp, scp->sc->adp->va_name, V_INFO_MM_OTHER) == NULL) {
201 scp->status |= (UNKNOWN_MODE | GRAPHICS_MODE | MOUSE_HIDDEN);
202 scp->status &= ~(PIXEL_MODE | MOUSE_VISIBLE);
204 scp->model = V_INFO_MM_OTHER;
206 * Don't change xsize and ysize; preserve the previous vty
207 * and history buffers.
211 scp->xpixel = info.vi_width;
212 scp->ypixel = info.vi_height;
215 #ifndef SC_NO_SYSMOUSE
216 /* move the mouse cursor at the center of the screen */
217 sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
219 sc_init_emulator(scp, NULL);
222 if (scp == scp->sc->cur_scp)
224 /* clear_graphics();*/
225 refresh_ega_palette(scp);
226 scp->status &= ~UNKNOWN_MODE;
230 if (tp->t_winsize.ws_xpixel != scp->xpixel
231 || tp->t_winsize.ws_ypixel != scp->ypixel) {
232 tp->t_winsize.ws_xpixel = scp->xpixel;
233 tp->t_winsize.ws_ypixel = scp->ypixel;
234 pgsignal(tp->t_pgrp, SIGWINCH, 1);
238 #endif /* SC_NO_MODE_CHANGE */
242 sc_set_pixel_mode(scr_stat *scp, struct tty *tp, int xsize, int ysize,
245 #ifndef SC_PIXEL_MODE
254 lwkt_gettoken(&tty_token);
255 if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info)) {
256 lwkt_reltoken(&tty_token);
257 return ENODEV; /* this shouldn't happen */
259 lwkt_reltoken(&tty_token);
261 /* adjust argument values */
263 fontsize = info.vi_cheight;
266 #ifndef SC_NO_FONT_LOADING
267 if (!(scp->sc->fonts_loaded & FONT_8))
269 font = scp->sc->font_8;
273 } else if (fontsize >= 16) {
275 #ifndef SC_NO_FONT_LOADING
276 if (!(scp->sc->fonts_loaded & FONT_16))
278 font = scp->sc->font_16;
284 #ifndef SC_NO_FONT_LOADING
285 if (!(scp->sc->fonts_loaded & FONT_14))
287 font = scp->sc->font_14;
293 xsize = info.vi_width/8;
295 ysize = info.vi_height/fontsize;
297 if ((info.vi_width < xsize*8) || (info.vi_height < ysize*fontsize))
301 * We currently support the following graphic modes:
303 * - 4 bpp planar modes whose memory size does not exceed 64K
304 * - 8 bbp packed pixel modes
305 * - 15, 16, 24 and 32 bpp direct modes with linear frame buffer
308 if (info.vi_mem_model == V_INFO_MM_PLANAR) {
309 if (info.vi_planes != 4)
313 * A memory size >64K requires bank switching to access the entire
317 if (info.vi_width * info.vi_height / 8 > info.vi_window_size)
319 } else if (info.vi_mem_model == V_INFO_MM_PACKED) {
320 if (info.vi_depth != 8)
322 } else if (info.vi_mem_model == V_INFO_MM_DIRECT) {
323 if (!(info.vi_flags & V_INFO_LINEAR) &&
324 (info.vi_depth != 15) && (info.vi_depth != 16) &&
325 (info.vi_depth != 24) && (info.vi_depth != 32))
330 /* stop screen saver, etc */
332 if ((error = sc_clean_up(scp))) {
337 if (sc_render_match(scp, scp->sc->adp->va_name, info.vi_mem_model) == NULL) {
344 (*scp->tsw->te_term)(scp, scp->ts);
351 #ifndef SC_NO_HISTORY
352 if (scp->history != NULL) {
354 new_ysize = sc_vtb_rows(scp->history);
357 prev_ysize = scp->ysize;
358 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
359 scp->status &= ~(GRAPHICS_MODE | MOUSE_VISIBLE);
360 scp->model = info.vi_mem_model;
363 scp->xoff = (scp->xpixel/8 - xsize)/2;
364 scp->yoff = (scp->ypixel/fontsize - ysize)/2;
366 scp->font_size = fontsize;
368 /* allocate buffers */
369 sc_alloc_scr_buffer(scp, TRUE, TRUE);
370 sc_init_emulator(scp, NULL);
371 #ifndef SC_NO_CUTPASTE
372 sc_alloc_cut_buffer(scp, FALSE);
374 #ifndef SC_NO_HISTORY
375 sc_alloc_history_buffer(scp, new_ysize, prev_ysize, FALSE);
379 if (scp == scp->sc->cur_scp) {
380 sc_set_border(scp, scp->border);
381 sc_set_cursor_image(scp);
384 scp->status &= ~UNKNOWN_MODE;
388 if (tp->t_winsize.ws_col != scp->xsize
389 || tp->t_winsize.ws_row != scp->ysize) {
390 tp->t_winsize.ws_col = scp->xsize;
391 tp->t_winsize.ws_row = scp->ysize;
392 pgsignal(tp->t_pgrp, SIGWINCH, 1);
396 #endif /* SC_PIXEL_MODE */
399 #define fb_ioctl(a, c, d) \
400 (((a) == NULL) ? ENODEV : \
401 (*vidsw[(a)->va_index]->ioctl)((a), (c), (caddr_t)(d)))
404 sc_vid_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag)
407 video_adapter_t *adp;
413 scp = SC_STAT(tp->t_dev);
414 if (scp == NULL) /* tp == SC_MOUSE */
417 if (adp == NULL) /* shouldn't happen??? */
420 lwkt_gettoken(&tty_token);
423 case CONS_CURRENTADP: /* get current adapter index */
425 ret = fb_ioctl(adp, FBIO_ADAPTER, data);
426 lwkt_reltoken(&tty_token);
429 case CONS_CURRENT: /* get current adapter type */
431 ret = fb_ioctl(adp, FBIO_ADPTYPE, data);
432 lwkt_reltoken(&tty_token);
435 case CONS_ADPINFO: /* adapter information */
437 if (((video_adapter_info_t *)data)->va_index >= 0) {
438 adp = vid_get_adapter(((video_adapter_info_t *)data)->va_index);
440 lwkt_reltoken(&tty_token);
444 ret = fb_ioctl(adp, FBIO_ADPINFO, data);
445 lwkt_reltoken(&tty_token);
448 case CONS_GET: /* get current video mode */
450 *(int *)data = scp->mode;
451 lwkt_reltoken(&tty_token);
454 #ifndef SC_NO_MODE_CHANGE
456 case FBIO_SETMODE: /* set video mode */
457 if (!(adp->va_flags & V_ADP_MODECHANGE)) {
458 lwkt_reltoken(&tty_token);
461 info.vi_mode = *(int *)data;
462 error = fb_ioctl(adp, FBIO_MODEINFO, &info);
464 lwkt_reltoken(&tty_token);
467 if (info.vi_flags & V_INFO_GRAPHICS) {
468 lwkt_reltoken(&tty_token);
469 return sc_set_graphics_mode(scp, tp, *(int *)data);
471 lwkt_reltoken(&tty_token);
472 return sc_set_text_mode(scp, tp, *(int *)data, 0, 0, 0);
474 #endif /* SC_NO_MODE_CHANGE */
476 case CONS_MODEINFO: /* get mode information */
478 ret = fb_ioctl(adp, FBIO_MODEINFO, data);
479 lwkt_reltoken(&tty_token);
482 case CONS_FINDMODE: /* find a matching video mode */
484 ret = fb_ioctl(adp, FBIO_FINDMODE, data);
485 lwkt_reltoken(&tty_token);
488 case CONS_SETWINORG: /* set frame buffer window origin */
490 if (scp != scp->sc->cur_scp) {
491 lwkt_reltoken(&tty_token);
492 return ENODEV; /* XXX */
494 ret = fb_ioctl(adp, FBIO_SETWINORG, data);
495 lwkt_reltoken(&tty_token);
498 case FBIO_GETWINORG: /* get frame buffer window origin */
499 if (scp != scp->sc->cur_scp) {
500 lwkt_reltoken(&tty_token);
501 return ENODEV; /* XXX */
503 ret = fb_ioctl(adp, FBIO_GETWINORG, data);
504 lwkt_reltoken(&tty_token);
507 case FBIO_GETDISPSTART:
508 case FBIO_SETDISPSTART:
509 case FBIO_GETLINEWIDTH:
510 case FBIO_SETLINEWIDTH:
511 if (scp != scp->sc->cur_scp) {
512 lwkt_reltoken(&tty_token);
513 return ENODEV; /* XXX */
515 ret = fb_ioctl(adp, cmd, data);
516 lwkt_reltoken(&tty_token);
519 case FBIO_GETPALETTE:
520 case FBIO_SETPALETTE:
532 if (scp != scp->sc->cur_scp) {
533 lwkt_reltoken(&tty_token);
534 return ENODEV; /* XXX */
536 ret = fb_ioctl(adp, cmd, data);
537 lwkt_reltoken(&tty_token);
540 case KDSETMODE: /* set current mode of this (virtual) console */
541 switch (*(int *)data) {
542 case KD_TEXT: /* switch to TEXT (known) mode */
544 * If scp->mode is of graphics modes, we don't know which
545 * text mode to switch back to...
547 if (scp->status & GRAPHICS_MODE) {
548 lwkt_reltoken(&tty_token);
551 /* restore fonts & palette ! */
553 #ifndef SC_NO_FONT_LOADING
554 if (ISFONTAVAIL(adp->va_flags)
555 && !(scp->status & (GRAPHICS_MODE | PIXEL_MODE)))
558 * Don't load fonts for now... XXX
560 if (scp->sc->fonts_loaded & FONT_8)
561 sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
562 if (scp->sc->fonts_loaded & FONT_14)
563 sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
564 if (scp->sc->fonts_loaded & FONT_16)
565 sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
567 #endif /* SC_NO_FONT_LOADING */
570 #ifndef SC_NO_PALETTE_LOADING
571 load_palette(adp, scp->sc->palette);
574 /* move hardware cursor out of the way */
575 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
578 case KD_TEXT1: /* switch to TEXT (known) mode */
580 * If scp->mode is of graphics modes, we don't know which
581 * text/pixel mode to switch back to...
583 if (scp->status & GRAPHICS_MODE) {
584 lwkt_reltoken(&tty_token);
588 if ((error = sc_clean_up(scp))) {
590 lwkt_reltoken(&tty_token);
593 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
595 /* no restore fonts & palette */
596 if (scp == scp->sc->cur_scp)
598 sc_clear_screen(scp);
599 scp->status &= ~UNKNOWN_MODE;
600 lwkt_reltoken(&tty_token);
604 case KD_PIXEL: /* pixel (raster) display */
605 if (!(scp->status & (GRAPHICS_MODE | PIXEL_MODE))) {
606 lwkt_reltoken(&tty_token);
609 if (scp->status & GRAPHICS_MODE) {
610 lwkt_reltoken(&tty_token);
611 return sc_set_pixel_mode(scp, tp, scp->xsize, scp->ysize,
615 if ((error = sc_clean_up(scp))) {
617 lwkt_reltoken(&tty_token);
620 scp->status |= (UNKNOWN_MODE | PIXEL_MODE | MOUSE_HIDDEN);
622 if (scp == scp->sc->cur_scp) {
624 #ifndef SC_NO_PALETTE_LOADING
625 load_palette(adp, scp->sc->palette);
628 sc_clear_screen(scp);
629 scp->status &= ~UNKNOWN_MODE;
630 lwkt_reltoken(&tty_token);
632 #endif /* SC_PIXEL_MODE */
634 case KD_GRAPHICS: /* switch to GRAPHICS (unknown) mode */
636 if ((error = sc_clean_up(scp))) {
638 lwkt_reltoken(&tty_token);
641 scp->status |= UNKNOWN_MODE | MOUSE_HIDDEN;
643 lwkt_reltoken(&tty_token);
647 lwkt_reltoken(&tty_token);
653 case KDRASTER: /* set pixel (raster) display mode */
654 if (ISUNKNOWNSC(scp) || ISTEXTSC(scp)) {
655 lwkt_reltoken(&tty_token);
658 lwkt_reltoken(&tty_token);
659 return sc_set_pixel_mode(scp, tp, ((int *)data)[0], ((int *)data)[1],
661 #endif /* SC_PIXEL_MODE */
663 case KDGETMODE: /* get current mode of this (virtual) console */
665 * From the user program's point of view, KD_PIXEL is the same
668 *data = ISGRAPHSC(scp) ? KD_GRAPHICS : KD_TEXT;
669 lwkt_reltoken(&tty_token);
672 case KDSBORDER: /* set border color of this (virtual) console */
674 if (scp == scp->sc->cur_scp)
675 sc_set_border(scp, scp->border);
676 lwkt_reltoken(&tty_token);
680 lwkt_reltoken(&tty_token);
684 static LIST_HEAD(, sc_renderer) sc_rndr_list =
685 LIST_HEAD_INITIALIZER(sc_rndr_list);
688 sc_render_add(sc_renderer_t *rndr)
690 LIST_INSERT_HEAD(&sc_rndr_list, rndr, link);
695 sc_render_remove(sc_renderer_t *rndr)
698 LIST_REMOVE(rndr, link);
700 return EBUSY; /* XXX */
704 sc_render_match(scr_stat *scp, char *name, int model)
706 const sc_renderer_t **list;
707 const sc_renderer_t *p;
709 if (!LIST_EMPTY(&sc_rndr_list)) {
710 LIST_FOREACH(p, &sc_rndr_list, link) {
711 if ((strcmp(p->name, name) == 0) &&
712 (model == p->model)) {
714 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);
719 SET_FOREACH(list, scrndr_set) {
721 if ((strcmp(p->name, name) == 0) &&
722 (model == p->model)) {
724 ~(VR_CURSOR_ON | VR_CURSOR_BLINK);