2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991-1997 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
42 #include <sys/consio.h>
45 /* XXX Direct Color 24bits modes unsupported */
47 #define min(x, y) (((x) < (y)) ? (x) : (y))
48 #define max(x, y) (((x) > (y)) ? (x) : (y))
50 VGLBitmap *VGLDisplay;
51 VGLBitmap VGLVDisplay;
52 video_info_t VGLModeInfo;
53 video_adapter_info_t VGLAdpInfo;
57 static int VGLOldMode;
58 static size_t VGLBufSize;
59 static byte *VGLMem = MAP_FAILED;
60 static int VGLSwitchPending;
61 static int VGLAbortPending;
62 static int VGLOnDisplay;
63 static unsigned int VGLCurWindow;
64 static int VGLInitDone = 0;
65 static video_info_t VGLOldModeInfo;
66 static vid_info_t VGLOldVInfo;
76 signal(SIGUSR1, SIG_IGN);
77 signal(SIGUSR2, SIG_IGN);
80 VGLMousePointerHide();
82 if (VGLMem != MAP_FAILED) {
83 VGLClear(VGLDisplay, 0);
84 munmap(VGLMem, VGLAdpInfo.va_window_size);
87 if (VGLOldMode >= M_VESA_BASE)
88 ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0);
90 ioctl(0, _IO('S', VGLOldMode), 0);
91 if (VGLOldModeInfo.vi_flags & V_INFO_GRAPHICS) {
92 size[0] = VGLOldVInfo.mv_csz;
93 size[1] = VGLOldVInfo.mv_rsz;
94 size[2] = VGLOldVInfo.font_size;;
95 ioctl(0, KDRASTER, size);
97 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
98 ioctl(0, KDDISABIO, 0);
99 ioctl(0, KDSETMODE, KD_TEXT);
100 smode.mode = VT_AUTO;
101 ioctl(0, VT_SETMODE, &smode);
116 signal(SIGINT, SIG_IGN);
117 signal(SIGTERM, SIG_IGN);
118 signal(SIGUSR2, SIG_IGN);
119 if (arg == SIGBUS || arg == SIGSEGV) {
120 signal(arg, SIG_DFL);
122 sigaddset(&mask, arg);
123 sigprocmask(SIG_UNBLOCK, &mask, NULL);
130 VGLSwitch(int arg __unused)
136 VGLSwitchPending = 1;
137 signal(SIGUSR1, VGLSwitch);
143 struct vt_mode smode;
149 signal(SIGUSR1, VGLSwitch);
150 signal(SIGINT, VGLAbort);
151 signal(SIGTERM, VGLAbort);
152 signal(SIGSEGV, VGLAbort);
153 signal(SIGBUS, VGLAbort);
154 signal(SIGUSR2, SIG_IGN);
157 VGLSwitchPending = 0;
160 if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype))
162 if (IOCGROUP(mode) == 'V') /* XXX: this is ugly */
163 VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE;
165 VGLModeInfo.vi_mode = mode & 0x0ff;
166 if (ioctl(0, CONS_MODEINFO, &VGLModeInfo)) /* FBIO_MODEINFO */
169 /* Save info for old mode to restore font size if old mode is graphics. */
170 VGLOldModeInfo.vi_mode = VGLOldMode;
171 if (ioctl(0, CONS_MODEINFO, &VGLOldModeInfo))
173 VGLOldVInfo.size = sizeof(VGLOldVInfo);
174 if (ioctl(0, CONS_GETINFO, &VGLOldVInfo))
177 VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap));
178 if (VGLDisplay == NULL)
181 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0)) {
189 * vi_mem_model specifies the memory model of the current video mode
192 switch (VGLModeInfo.vi_mem_model) {
193 case V_INFO_MM_PLANAR:
194 /* we can handle EGA/VGA planner modes only */
195 if (VGLModeInfo.vi_depth != 4 || VGLModeInfo.vi_planes != 4
196 || (adptype != KD_EGA && adptype != KD_VGA)) {
200 VGLDisplay->Type = VIDBUF4;
201 VGLDisplay->PixelBytes = 1;
203 case V_INFO_MM_PACKED:
204 /* we can do only 256 color packed modes */
205 if (VGLModeInfo.vi_depth != 8) {
209 VGLDisplay->Type = VIDBUF8;
210 VGLDisplay->PixelBytes = 1;
213 VGLDisplay->Type = VIDBUF8X;
214 VGLDisplay->PixelBytes = 1;
216 case V_INFO_MM_DIRECT:
217 VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size;
218 switch (VGLDisplay->PixelBytes) {
220 VGLDisplay->Type = VIDBUF16;
224 VGLDisplay->Type = VIDBUF24;
228 VGLDisplay->Type = VIDBUF32;
240 ioctl(0, VT_WAITACTIVE, 0);
241 ioctl(0, KDSETMODE, KD_GRAPHICS);
242 if (ioctl(0, mode, 0)) {
246 if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) { /* FBIO_ADPINFO */
252 * Calculate the shadow screen buffer size. In -CURRENT, va_buffer_size
253 * always holds the entire frame buffer size, wheather it's in the linear
254 * mode or windowed mode.
255 * VGLBufSize = VGLAdpInfo.va_buffer_size;
256 * In -STABLE, va_buffer_size holds the frame buffer size, only if
257 * the linear frame buffer mode is supported. Otherwise the field is zero.
258 * We shall calculate the minimal size in this case:
259 * VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*VGLModeInfo.vi_planes
261 * VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes;
262 * Use whichever is larger.
264 if (VGLAdpInfo.va_buffer_size != 0)
265 VGLBufSize = VGLAdpInfo.va_buffer_size;
267 VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height,
268 VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes;
270 * The above is for old -CURRENT. Current -CURRENT since r203535 and/or
271 * r248799 restricts va_buffer_size to the displayed size in VESA modes to
272 * avoid wasting kva for mapping unused parts of the frame buffer. But all
273 * parts were usable here. Applying the same restriction to user mappings
274 * makes our virtualization useless and breaks our panning, but large frame
275 * buffers are also difficult for us to manage (clearing and switching may
276 * be too slow, and malloc() may fail). Restrict ourselves similarly to
277 * get the same efficiency and bugs for all kernels.
279 if (VGLModeInfo.vi_mode >= M_VESA_BASE)
280 VGLBufSize = VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*
281 VGLModeInfo.vi_planes;
282 VGLBuf = malloc(VGLBufSize);
283 if (VGLBuf == NULL) {
289 fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize);
292 /* see if we are in the windowed buffer mode or in the linear buffer mode */
293 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) {
294 switch (VGLDisplay->Type) {
296 VGLDisplay->Type = VIDBUF4S;
299 VGLDisplay->Type = VIDBUF8S;
302 VGLDisplay->Type = VIDBUF16S;
305 VGLDisplay->Type = VIDBUF24S;
308 VGLDisplay->Type = VIDBUF32S;
319 VGLDisplay->Xsize = VGLModeInfo.vi_width;
320 VGLDisplay->Ysize = VGLModeInfo.vi_height;
321 depth = VGLModeInfo.vi_depth;
324 VGLDisplay->VXsize = VGLAdpInfo.va_line_width
325 *8/(depth/VGLModeInfo.vi_planes);
326 VGLDisplay->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
327 VGLDisplay->Xorigin = 0;
328 VGLDisplay->Yorigin = 0;
330 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
331 MAP_FILE | MAP_SHARED, 0, 0);
332 if (VGLMem == MAP_FAILED) {
336 VGLDisplay->Bitmap = VGLMem;
338 VGLVDisplay = *VGLDisplay;
339 VGLVDisplay.Type = MEMBUF;
340 if (VGLModeInfo.vi_depth < 8)
341 VGLVDisplay.Bitmap = malloc(2 * VGLBufSize);
343 VGLVDisplay.Bitmap = VGLBuf;
348 fprintf(stderr, "va_line_width:%d\n", VGLAdpInfo.va_line_width);
349 fprintf(stderr, "VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
350 VGLDisplay->Xsize, VGLDisplay->Ysize,
351 VGLDisplay->VXsize, VGLDisplay->VYsize);
354 smode.mode = VT_PROCESS;
356 smode.relsig = SIGUSR1;
357 smode.acqsig = SIGUSR1;
358 smode.frsig = SIGINT;
359 if (ioctl(0, VT_SETMODE, &smode)) {
363 VGLTextSetFontFile((byte*)0);
364 VGLClear(VGLDisplay, 0);
371 if (VGLAbortPending) {
375 while (VGLSwitchPending) {
376 VGLSwitchPending = 0;
378 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
379 ioctl(0, KDENABIO, 0);
380 ioctl(0, KDSETMODE, KD_GRAPHICS);
381 ioctl(0, VGLMode, 0);
383 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
384 MAP_FILE | MAP_SHARED, 0, 0);
386 /* XXX: what if mmap() has failed! */
387 VGLDisplay->Type = VIDBUF8; /* XXX */
388 switch (VGLModeInfo.vi_mem_model) {
389 case V_INFO_MM_PLANAR:
390 if (VGLModeInfo.vi_depth == 4 && VGLModeInfo.vi_planes == 4) {
391 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
392 VGLDisplay->Type = VIDBUF4S;
394 VGLDisplay->Type = VIDBUF4;
396 /* shouldn't be happening */
399 case V_INFO_MM_PACKED:
400 if (VGLModeInfo.vi_depth == 8) {
401 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
402 VGLDisplay->Type = VIDBUF8S;
404 VGLDisplay->Type = VIDBUF8;
408 VGLDisplay->Type = VIDBUF8X;
410 case V_INFO_MM_DIRECT:
411 switch (VGLModeInfo.vi_pixel_size) {
413 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
414 VGLDisplay->Type = VIDBUF16S;
416 VGLDisplay->Type = VIDBUF16;
419 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
420 VGLDisplay->Type = VIDBUF24S;
422 VGLDisplay->Type = VIDBUF24;
425 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
426 VGLDisplay->Type = VIDBUF32S;
428 VGLDisplay->Type = VIDBUF32;
431 /* shouldn't be happening */
435 /* shouldn't be happening */
439 VGLDisplay->Bitmap = VGLMem;
440 VGLDisplay->Xsize = VGLModeInfo.vi_width;
441 VGLDisplay->Ysize = VGLModeInfo.vi_height;
442 VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize);
446 VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin);
447 VGLBitmapCopy(&VGLVDisplay, 0, 0, VGLDisplay, 0, 0,
448 VGLDisplay->VXsize, VGLDisplay->VYsize);
450 ioctl(0, VT_RELDISP, VT_ACKACQ);
454 munmap(VGLDisplay->Bitmap, VGLAdpInfo.va_window_size);
455 ioctl(0, VGLOldMode, 0);
456 ioctl(0, KDSETMODE, KD_TEXT);
457 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
458 ioctl(0, KDDISABIO, 0);
459 ioctl(0, VT_RELDISP, VT_TRUE);
460 VGLDisplay->Bitmap = VGLBuf;
461 VGLDisplay->Type = MEMBUF;
462 VGLDisplay->Xsize = VGLDisplay->VXsize;
463 VGLDisplay->Ysize = VGLDisplay->VYsize;
464 while (!VGLOnDisplay) pause();
470 VGLSetSegment(unsigned int offset)
472 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) {
473 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */
474 VGLCurWindow = offset/VGLAdpInfo.va_window_size;
476 return (offset%VGLAdpInfo.va_window_size);
480 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize)
484 if (VXsize < object->Xsize || VYsize < object->Ysize)
486 if (object->Type == MEMBUF)
488 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize))
490 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */
491 depth = VGLModeInfo.vi_depth;
494 object->VXsize = VGLAdpInfo.va_line_width
495 *8/(depth/VGLModeInfo.vi_planes);
496 object->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
497 if (VYsize < object->VYsize)
498 object->VYsize = VYsize;
501 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
502 object->Xsize, object->Ysize, object->VXsize, object->VYsize);
509 VGLPanScreen(VGLBitmap *object, int x, int y)
511 video_display_start_t origin;
513 if (x < 0 || x + object->Xsize > object->VXsize
514 || y < 0 || y + object->Ysize > object->VYsize)
516 if (object->Type == MEMBUF)
520 if (ioctl(0, FBIO_SETDISPSTART, &origin))
526 fprintf(stderr, "new origin: (%d, %d)\n", x, y);