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 #define min(x, y) (((x) < (y)) ? (x) : (y))
46 #define max(x, y) (((x) > (y)) ? (x) : (y))
48 VGLBitmap *VGLDisplay;
49 VGLBitmap VGLVDisplay;
50 video_info_t VGLModeInfo;
51 video_adapter_info_t VGLAdpInfo;
55 static int VGLOldMode;
56 static size_t VGLBufSize;
57 static byte *VGLMem = MAP_FAILED;
58 static int VGLSwitchPending;
59 static int VGLAbortPending;
60 static int VGLOnDisplay;
61 static unsigned int VGLCurWindow;
62 static int VGLInitDone = 0;
63 static video_info_t VGLOldModeInfo;
64 static vid_info_t VGLOldVInfo;
65 static int VGLOldVXsize;
76 signal(SIGUSR1, SIG_IGN);
77 signal(SIGUSR2, SIG_IGN);
80 VGLMouseMode(VGL_MOUSEHIDE);
82 if (VGLMem != MAP_FAILED) {
83 VGLClear(VGLDisplay, 0);
84 munmap(VGLMem, VGLAdpInfo.va_window_size);
87 ioctl(0, FBIO_SETLINEWIDTH, &VGLOldVXsize);
89 if (VGLOldMode >= M_VESA_BASE)
90 ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0);
92 ioctl(0, _IO('S', VGLOldMode), 0);
93 if (VGLOldModeInfo.vi_flags & V_INFO_GRAPHICS) {
94 size[0] = VGLOldVInfo.mv_csz;
95 size[1] = VGLOldVInfo.mv_rsz;
96 size[2] = VGLOldVInfo.font_size;;
97 ioctl(0, KDRASTER, size);
99 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
100 ioctl(0, KDDISABIO, 0);
101 ioctl(0, KDSETMODE, KD_TEXT);
102 smode.mode = VT_AUTO;
103 ioctl(0, VT_SETMODE, &smode);
118 signal(SIGINT, SIG_IGN);
119 signal(SIGTERM, SIG_IGN);
120 signal(SIGUSR2, SIG_IGN);
121 if (arg == SIGBUS || arg == SIGSEGV) {
122 signal(arg, SIG_DFL);
124 sigaddset(&mask, arg);
125 sigprocmask(SIG_UNBLOCK, &mask, NULL);
132 VGLSwitch(int arg __unused)
138 VGLSwitchPending = 1;
139 signal(SIGUSR1, VGLSwitch);
145 struct vt_mode smode;
151 signal(SIGUSR1, VGLSwitch);
152 signal(SIGINT, VGLAbort);
153 signal(SIGTERM, VGLAbort);
154 signal(SIGSEGV, VGLAbort);
155 signal(SIGBUS, VGLAbort);
156 signal(SIGUSR2, SIG_IGN);
159 VGLSwitchPending = 0;
162 if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype))
164 if (IOCGROUP(mode) == 'V') /* XXX: this is ugly */
165 VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE;
167 VGLModeInfo.vi_mode = mode & 0x0ff;
168 if (ioctl(0, CONS_MODEINFO, &VGLModeInfo)) /* FBIO_MODEINFO */
171 /* Save info for old mode to restore font size if old mode is graphics. */
172 VGLOldModeInfo.vi_mode = VGLOldMode;
173 if (ioctl(0, CONS_MODEINFO, &VGLOldModeInfo))
175 VGLOldVInfo.size = sizeof(VGLOldVInfo);
176 if (ioctl(0, CONS_GETINFO, &VGLOldVInfo))
179 VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap));
180 if (VGLDisplay == NULL)
183 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0)) {
191 * vi_mem_model specifies the memory model of the current video mode
194 switch (VGLModeInfo.vi_mem_model) {
195 case V_INFO_MM_PLANAR:
196 /* we can handle EGA/VGA planner modes only */
197 if (VGLModeInfo.vi_depth != 4 || VGLModeInfo.vi_planes != 4
198 || (adptype != KD_EGA && adptype != KD_VGA)) {
202 VGLDisplay->Type = VIDBUF4;
203 VGLDisplay->PixelBytes = 1;
205 case V_INFO_MM_PACKED:
206 /* we can do only 256 color packed modes */
207 if (VGLModeInfo.vi_depth != 8) {
211 VGLDisplay->Type = VIDBUF8;
212 VGLDisplay->PixelBytes = 1;
215 VGLDisplay->Type = VIDBUF8X;
216 VGLDisplay->PixelBytes = 1;
218 case V_INFO_MM_DIRECT:
219 VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size;
220 switch (VGLDisplay->PixelBytes) {
222 VGLDisplay->Type = VIDBUF16;
225 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;
325 VGLDisplay->VXsize = VGLAdpInfo.va_line_width
326 *8/(depth/VGLModeInfo.vi_planes);
327 VGLDisplay->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
328 VGLDisplay->Xorigin = 0;
329 VGLDisplay->Yorigin = 0;
331 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
332 MAP_FILE | MAP_SHARED, 0, 0);
333 if (VGLMem == MAP_FAILED) {
337 VGLDisplay->Bitmap = VGLMem;
339 VGLVDisplay = *VGLDisplay;
340 VGLVDisplay.Type = MEMBUF;
341 if (VGLModeInfo.vi_depth < 8)
342 VGLVDisplay.Bitmap = malloc(2 * VGLBufSize);
344 VGLVDisplay.Bitmap = VGLBuf;
349 fprintf(stderr, "va_line_width:%d\n", VGLAdpInfo.va_line_width);
350 fprintf(stderr, "VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
351 VGLDisplay->Xsize, VGLDisplay->Ysize,
352 VGLDisplay->VXsize, VGLDisplay->VYsize);
355 smode.mode = VT_PROCESS;
357 smode.relsig = SIGUSR1;
358 smode.acqsig = SIGUSR1;
359 smode.frsig = SIGINT;
360 if (ioctl(0, VT_SETMODE, &smode)) {
364 VGLTextSetFontFile((byte*)0);
365 VGLClear(VGLDisplay, 0);
372 if (VGLAbortPending) {
376 while (VGLSwitchPending) {
377 VGLSwitchPending = 0;
379 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
380 ioctl(0, KDENABIO, 0);
381 ioctl(0, KDSETMODE, KD_GRAPHICS);
382 ioctl(0, VGLMode, 0);
384 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
385 MAP_FILE | MAP_SHARED, 0, 0);
387 /* XXX: what if mmap() has failed! */
388 VGLDisplay->Type = VIDBUF8; /* XXX */
389 switch (VGLModeInfo.vi_mem_model) {
390 case V_INFO_MM_PLANAR:
391 if (VGLModeInfo.vi_depth == 4 && VGLModeInfo.vi_planes == 4) {
392 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
393 VGLDisplay->Type = VIDBUF4S;
395 VGLDisplay->Type = VIDBUF4;
397 /* shouldn't be happening */
400 case V_INFO_MM_PACKED:
401 if (VGLModeInfo.vi_depth == 8) {
402 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
403 VGLDisplay->Type = VIDBUF8S;
405 VGLDisplay->Type = VIDBUF8;
409 VGLDisplay->Type = VIDBUF8X;
411 case V_INFO_MM_DIRECT:
412 switch (VGLModeInfo.vi_pixel_size) {
414 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
415 VGLDisplay->Type = VIDBUF16S;
417 VGLDisplay->Type = VIDBUF16;
420 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
421 VGLDisplay->Type = VIDBUF24S;
423 VGLDisplay->Type = VIDBUF24;
426 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
427 VGLDisplay->Type = VIDBUF32S;
429 VGLDisplay->Type = VIDBUF32;
432 /* shouldn't be happening */
436 /* shouldn't be happening */
440 VGLDisplay->Bitmap = VGLMem;
441 VGLDisplay->Xsize = VGLModeInfo.vi_width;
442 VGLDisplay->Ysize = VGLModeInfo.vi_height;
443 VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize);
447 VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin);
448 VGLBitmapCopy(&VGLVDisplay, 0, 0, VGLDisplay, 0, 0,
449 VGLDisplay->VXsize, VGLDisplay->VYsize);
451 ioctl(0, VT_RELDISP, VT_ACKACQ);
455 munmap(VGLDisplay->Bitmap, VGLAdpInfo.va_window_size);
456 ioctl(0, VGLOldMode, 0);
457 ioctl(0, KDSETMODE, KD_TEXT);
458 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
459 ioctl(0, KDDISABIO, 0);
460 ioctl(0, VT_RELDISP, VT_TRUE);
461 VGLDisplay->Bitmap = VGLBuf;
462 VGLDisplay->Type = MEMBUF;
463 VGLDisplay->Xsize = VGLDisplay->VXsize;
464 VGLDisplay->Ysize = VGLDisplay->VYsize;
465 while (!VGLOnDisplay) pause();
471 VGLSetSegment(unsigned int offset)
473 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) {
474 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */
475 VGLCurWindow = offset/VGLAdpInfo.va_window_size;
477 return (offset%VGLAdpInfo.va_window_size);
481 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize)
485 if (VXsize < object->Xsize || VYsize < object->Ysize)
487 if (object->Type == MEMBUF)
489 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize))
491 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */
492 depth = VGLModeInfo.vi_depth;
495 object->VXsize = VGLAdpInfo.va_line_width
496 *8/(depth/VGLModeInfo.vi_planes);
497 object->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
498 if (VYsize < object->VYsize)
499 object->VYsize = VYsize;
502 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
503 object->Xsize, object->Ysize, object->VXsize, object->VYsize);
510 VGLPanScreen(VGLBitmap *object, int x, int y)
512 video_display_start_t origin;
514 if (x < 0 || x + object->Xsize > object->VXsize
515 || y < 0 || y + object->Ysize > object->VYsize)
517 if (object->Type == MEMBUF)
521 if (ioctl(0, FBIO_SETDISPSTART, &origin))
527 fprintf(stderr, "new origin: (%d, %d)\n", x, y);