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>
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
40 #include <sys/consio.h>
43 #define min(x, y) (((x) < (y)) ? (x) : (y))
44 #define max(x, y) (((x) > (y)) ? (x) : (y))
46 VGLBitmap *VGLDisplay;
47 VGLBitmap VGLVDisplay;
48 video_info_t VGLModeInfo;
49 video_adapter_info_t VGLAdpInfo;
53 static int VGLOldMode;
54 static size_t VGLBufSize;
55 static byte *VGLMem = MAP_FAILED;
56 static int VGLSwitchPending;
57 static int VGLAbortPending;
58 static int VGLOnDisplay;
59 static unsigned int VGLCurWindow;
60 static int VGLInitDone = 0;
61 static video_info_t VGLOldModeInfo;
62 static vid_info_t VGLOldVInfo;
63 static int VGLOldVXsize;
74 signal(SIGUSR1, SIG_IGN);
75 signal(SIGUSR2, SIG_IGN);
78 VGLMouseMode(VGL_MOUSEHIDE);
80 if (VGLMem != MAP_FAILED) {
81 VGLClear(VGLDisplay, 0);
82 munmap(VGLMem, VGLAdpInfo.va_window_size);
85 ioctl(0, FBIO_SETLINEWIDTH, &VGLOldVXsize);
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;
223 VGLDisplay->Type = VIDBUF24;
226 VGLDisplay->Type = VIDBUF32;
238 ioctl(0, VT_WAITACTIVE, 0);
239 ioctl(0, KDSETMODE, KD_GRAPHICS);
240 if (ioctl(0, mode, 0)) {
244 if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) { /* FBIO_ADPINFO */
250 * Calculate the shadow screen buffer size. In -CURRENT, va_buffer_size
251 * always holds the entire frame buffer size, wheather it's in the linear
252 * mode or windowed mode.
253 * VGLBufSize = VGLAdpInfo.va_buffer_size;
254 * In -STABLE, va_buffer_size holds the frame buffer size, only if
255 * the linear frame buffer mode is supported. Otherwise the field is zero.
256 * We shall calculate the minimal size in this case:
257 * VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*VGLModeInfo.vi_planes
259 * VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes;
260 * Use whichever is larger.
262 if (VGLAdpInfo.va_buffer_size != 0)
263 VGLBufSize = VGLAdpInfo.va_buffer_size;
265 VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height,
266 VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes;
268 * The above is for old -CURRENT. Current -CURRENT since r203535 and/or
269 * r248799 restricts va_buffer_size to the displayed size in VESA modes to
270 * avoid wasting kva for mapping unused parts of the frame buffer. But all
271 * parts were usable here. Applying the same restriction to user mappings
272 * makes our virtualization useless and breaks our panning, but large frame
273 * buffers are also difficult for us to manage (clearing and switching may
274 * be too slow, and malloc() may fail). Restrict ourselves similarly to
275 * get the same efficiency and bugs for all kernels.
277 if (VGLModeInfo.vi_mode >= M_VESA_BASE)
278 VGLBufSize = VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*
279 VGLModeInfo.vi_planes;
280 VGLBuf = malloc(VGLBufSize);
281 if (VGLBuf == NULL) {
287 fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize);
290 /* see if we are in the windowed buffer mode or in the linear buffer mode */
291 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) {
292 switch (VGLDisplay->Type) {
294 VGLDisplay->Type = VIDBUF4S;
297 VGLDisplay->Type = VIDBUF8S;
300 VGLDisplay->Type = VIDBUF16S;
303 VGLDisplay->Type = VIDBUF24S;
306 VGLDisplay->Type = VIDBUF32S;
317 VGLDisplay->Xsize = VGLModeInfo.vi_width;
318 VGLDisplay->Ysize = VGLModeInfo.vi_height;
319 depth = VGLModeInfo.vi_depth;
323 VGLDisplay->VXsize = VGLAdpInfo.va_line_width
324 *8/(depth/VGLModeInfo.vi_planes);
325 VGLDisplay->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
326 VGLDisplay->Xorigin = 0;
327 VGLDisplay->Yorigin = 0;
329 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
330 MAP_FILE | MAP_SHARED, 0, 0);
331 if (VGLMem == MAP_FAILED) {
335 VGLDisplay->Bitmap = VGLMem;
337 VGLVDisplay = *VGLDisplay;
338 VGLVDisplay.Type = MEMBUF;
339 if (VGLModeInfo.vi_depth < 8)
340 VGLVDisplay.Bitmap = malloc(2 * VGLBufSize);
342 VGLVDisplay.Bitmap = VGLBuf;
347 fprintf(stderr, "va_line_width:%d\n", VGLAdpInfo.va_line_width);
348 fprintf(stderr, "VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
349 VGLDisplay->Xsize, VGLDisplay->Ysize,
350 VGLDisplay->VXsize, VGLDisplay->VYsize);
353 smode.mode = VT_PROCESS;
355 smode.relsig = SIGUSR1;
356 smode.acqsig = SIGUSR1;
357 smode.frsig = SIGINT;
358 if (ioctl(0, VT_SETMODE, &smode)) {
362 VGLTextSetFontFile((byte*)0);
363 VGLClear(VGLDisplay, 0);
370 if (VGLAbortPending) {
374 while (VGLSwitchPending) {
375 VGLSwitchPending = 0;
377 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
378 ioctl(0, KDENABIO, 0);
379 ioctl(0, KDSETMODE, KD_GRAPHICS);
380 ioctl(0, VGLMode, 0);
382 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
383 MAP_FILE | MAP_SHARED, 0, 0);
385 /* XXX: what if mmap() has failed! */
386 VGLDisplay->Type = VIDBUF8; /* XXX */
387 switch (VGLModeInfo.vi_mem_model) {
388 case V_INFO_MM_PLANAR:
389 if (VGLModeInfo.vi_depth == 4 && VGLModeInfo.vi_planes == 4) {
390 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
391 VGLDisplay->Type = VIDBUF4S;
393 VGLDisplay->Type = VIDBUF4;
395 /* shouldn't be happening */
398 case V_INFO_MM_PACKED:
399 if (VGLModeInfo.vi_depth == 8) {
400 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
401 VGLDisplay->Type = VIDBUF8S;
403 VGLDisplay->Type = VIDBUF8;
407 VGLDisplay->Type = VIDBUF8X;
409 case V_INFO_MM_DIRECT:
410 switch (VGLModeInfo.vi_pixel_size) {
412 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
413 VGLDisplay->Type = VIDBUF16S;
415 VGLDisplay->Type = VIDBUF16;
418 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
419 VGLDisplay->Type = VIDBUF24S;
421 VGLDisplay->Type = VIDBUF24;
424 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
425 VGLDisplay->Type = VIDBUF32S;
427 VGLDisplay->Type = VIDBUF32;
430 /* shouldn't be happening */
434 /* shouldn't be happening */
438 VGLDisplay->Bitmap = VGLMem;
439 VGLDisplay->Xsize = VGLModeInfo.vi_width;
440 VGLDisplay->Ysize = VGLModeInfo.vi_height;
441 VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize);
445 VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin);
446 VGLBitmapCopy(&VGLVDisplay, 0, 0, VGLDisplay, 0, 0,
447 VGLDisplay->VXsize, VGLDisplay->VYsize);
449 ioctl(0, VT_RELDISP, VT_ACKACQ);
453 munmap(VGLDisplay->Bitmap, VGLAdpInfo.va_window_size);
454 ioctl(0, VGLOldMode, 0);
455 ioctl(0, KDSETMODE, KD_TEXT);
456 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
457 ioctl(0, KDDISABIO, 0);
458 ioctl(0, VT_RELDISP, VT_TRUE);
459 VGLDisplay->Bitmap = VGLBuf;
460 VGLDisplay->Type = MEMBUF;
461 VGLDisplay->Xsize = VGLDisplay->VXsize;
462 VGLDisplay->Ysize = VGLDisplay->VYsize;
463 while (!VGLOnDisplay) pause();
469 VGLSetSegment(unsigned int offset)
471 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) {
472 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */
473 VGLCurWindow = offset/VGLAdpInfo.va_window_size;
475 return (offset%VGLAdpInfo.va_window_size);
479 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize)
483 if (VXsize < object->Xsize || VYsize < object->Ysize)
485 if (object->Type == MEMBUF)
487 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize))
489 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */
490 depth = VGLModeInfo.vi_depth;
493 object->VXsize = VGLAdpInfo.va_line_width
494 *8/(depth/VGLModeInfo.vi_planes);
495 object->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
496 if (VYsize < object->VYsize)
497 object->VYsize = VYsize;
500 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
501 object->Xsize, object->Ysize, object->VXsize, object->VYsize);
508 VGLPanScreen(VGLBitmap *object, int x, int y)
510 video_display_start_t origin;
512 if (x < 0 || x + object->Xsize > object->VXsize
513 || y < 0 || y + object->Ysize > object->VYsize)
515 if (object->Type == MEMBUF)
519 if (ioctl(0, FBIO_SETDISPSTART, &origin))
525 fprintf(stderr, "new origin: (%d, %d)\n", x, y);