2 * Copyright (c) 1991-1997 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software withough specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/signal.h>
35 #include <sys/ioctl.h>
37 #include <machine/console.h>
40 #define min(x, y) (((x) < (y)) ? (x) : (y))
41 #define max(x, y) (((x) > (y)) ? (x) : (y))
43 VGLBitmap *VGLDisplay;
44 video_info_t VGLModeInfo;
45 video_adapter_info_t VGLAdpInfo;
49 static int VGLOldMode;
50 static size_t VGLBufSize;
51 static byte *VGLMem = MAP_FAILED;
52 static int VGLSwitchPending;
53 static int VGLOnDisplay;
54 static unsigned int VGLCurWindow;
55 static int VGLInitDone = 0;
66 signal(SIGUSR1, SIG_IGN);
68 if (VGLMem != MAP_FAILED) {
69 VGLClear(VGLDisplay, 0);
70 munmap(VGLMem, VGLAdpInfo.va_window_size);
73 if (VGLOldMode >= M_VESA_BASE) {
74 /* ugly, but necessary */
75 ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0);
76 if (VGLOldMode == M_VESA_800x600) {
81 ioctl(0, KDRASTER, size);
84 ioctl(0, _IO('S', VGLOldMode), 0);
86 ioctl(0, KDDISABIO, 0);
87 ioctl(0, KDSETMODE, KD_TEXT);
89 ioctl(0, VT_SETMODE, &smode);
112 VGLSwitchPending = 1;
113 signal(SIGUSR1, VGLSwitch);
119 struct vt_mode smode;
125 signal(SIGUSR1, VGLSwitch);
126 signal(SIGINT, VGLAbort);
127 signal(SIGTERM, VGLAbort);
128 signal(SIGSEGV, VGLAbort);
129 signal(SIGBUS, VGLAbort);
132 VGLSwitchPending = 0;
134 if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype))
136 if (IOCGROUP(mode) == 'V') /* XXX: this is ugly */
137 VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE;
139 VGLModeInfo.vi_mode = mode & 0x0ff;
140 if (ioctl(0, CONS_MODEINFO, &VGLModeInfo)) /* FBIO_MODEINFO */
143 VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap));
144 if (VGLDisplay == NULL)
147 if (ioctl(0, KDENABIO, 0)) {
155 * vi_mem_model specifies the memory model of the current video mode
158 switch (VGLModeInfo.vi_mem_model) {
159 case V_INFO_MM_PLANAR:
160 /* we can handle EGA/VGA planner modes only */
161 if (VGLModeInfo.vi_depth != 4 || VGLModeInfo.vi_planes != 4
162 || (adptype != KD_EGA && adptype != KD_VGA)) {
166 VGLDisplay->Type = VIDBUF4;
168 case V_INFO_MM_PACKED:
169 /* we can do only 256 color packed modes */
170 if (VGLModeInfo.vi_depth != 8) {
174 VGLDisplay->Type = VIDBUF8;
177 VGLDisplay->Type = VIDBUF8X;
184 ioctl(0, VT_WAITACTIVE, 0);
185 ioctl(0, KDSETMODE, KD_GRAPHICS);
186 if (ioctl(0, mode, 0)) {
190 if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) { /* FBIO_ADPINFO */
196 * Calculate the shadow screen buffer size. In -CURRENT, va_buffer_size
197 * always holds the entire frame buffer size, wheather it's in the linear
198 * mode or windowed mode.
199 * VGLBufSize = VGLAdpInfo.va_buffer_size;
200 * In -STABLE, va_buffer_size holds the frame buffer size, only if
201 * the linear frame buffer mode is supported. Otherwise the field is zero.
202 * We shall calculate the minimal size in this case:
203 * VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*VGLModeInfo.vi_planes
205 * VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes;
206 * Use whichever is larger.
208 if (VGLAdpInfo.va_buffer_size != 0)
209 VGLBufSize = VGLAdpInfo.va_buffer_size;
211 VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height,
212 VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes;
213 VGLBuf = malloc(VGLBufSize);
214 if (VGLBuf == NULL) {
220 fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize);
223 /* see if we are in the windowed buffer mode or in the linear buffer mode */
224 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) {
225 if (VGLDisplay->Type == VIDBUF4)
226 VGLDisplay->Type = VIDBUF4S;
227 else if (VGLDisplay->Type == VIDBUF8)
228 VGLDisplay->Type = VIDBUF8S;
234 VGLDisplay->Xsize = VGLModeInfo.vi_width;
235 VGLDisplay->Ysize = VGLModeInfo.vi_height;
236 VGLDisplay->VXsize = VGLAdpInfo.va_line_width
237 *8/(VGLModeInfo.vi_depth/VGLModeInfo.vi_planes);
238 VGLDisplay->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
239 VGLDisplay->Xorigin = 0;
240 VGLDisplay->Yorigin = 0;
242 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
244 if (VGLMem == MAP_FAILED) {
248 VGLDisplay->Bitmap = VGLMem;
253 fprintf(stderr, "va_line_width:%d\n", VGLAdpInfo.va_line_width);
254 fprintf(stderr, "VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
255 VGLDisplay->Xsize, VGLDisplay->Ysize,
256 VGLDisplay->VXsize, VGLDisplay->VYsize);
259 smode.mode = VT_PROCESS;
261 smode.relsig = SIGUSR1;
262 smode.acqsig = SIGUSR1;
263 smode.frsig = SIGINT;
264 if (ioctl(0, VT_SETMODE, &smode)) {
268 VGLTextSetFontFile((byte*)0);
269 VGLClear(VGLDisplay, 0);
276 while (VGLSwitchPending) {
281 VGLSwitchPending = 0;
283 ioctl(0, KDENABIO, 0);
284 ioctl(0, KDSETMODE, KD_GRAPHICS);
285 ioctl(0, VGLMode, 0);
287 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
290 /* XXX: what if mmap() has failed! */
291 VGLDisplay->Type = VIDBUF8; /* XXX */
292 switch (VGLModeInfo.vi_mem_model) {
293 case V_INFO_MM_PLANAR:
294 if (VGLModeInfo.vi_depth == 4 && VGLModeInfo.vi_planes == 4) {
295 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
296 VGLDisplay->Type = VIDBUF4S;
298 VGLDisplay->Type = VIDBUF4;
300 /* shouldn't be happening */
303 case V_INFO_MM_PACKED:
304 if (VGLModeInfo.vi_depth == 8) {
305 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
306 VGLDisplay->Type = VIDBUF8S;
308 VGLDisplay->Type = VIDBUF8;
310 /* shouldn't be happening */
314 VGLDisplay->Type = VIDBUF8X;
317 /* shouldn't be happening */
321 VGLDisplay->Bitmap = VGLMem;
322 VGLDisplay->Xsize = VGLModeInfo.vi_width;
323 VGLDisplay->Ysize = VGLModeInfo.vi_height;
324 VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize);
325 VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin);
326 switch (VGLDisplay->Type) {
329 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
330 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
331 for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes;
333 VGLSetSegment(offset);
334 len = min(VGLBufSize/VGLModeInfo.vi_planes - offset,
335 VGLAdpInfo.va_window_size);
336 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
338 outb(0x3c5, 0x01<<i);
339 bcopy(&VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
347 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
348 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
349 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
351 outb(0x3c5, 0x01<<i);
352 bcopy(&VGLBuf[i*VGLAdpInfo.va_window_size], VGLMem,
353 VGLAdpInfo.va_window_size);
358 for (offset = 0; offset < VGLBufSize; offset += len) {
359 VGLSetSegment(offset);
360 len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size);
361 bcopy(&VGLBuf[offset], VGLMem, len);
366 ioctl(0, VT_RELDISP, VT_ACKACQ);
369 switch (VGLDisplay->Type) {
371 for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes;
373 VGLSetSegment(offset);
374 len = min(VGLBufSize/VGLModeInfo.vi_planes - offset,
375 VGLAdpInfo.va_window_size);
376 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
379 bcopy(VGLMem, &VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
387 * NOTE: the saved buffer is NOT in the MEMBUF format which
388 * the ordinary memory bitmap object is stored in. XXX
390 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
393 bcopy(VGLMem, &VGLBuf[i*VGLAdpInfo.va_window_size],
394 VGLAdpInfo.va_window_size);
399 for (offset = 0; offset < VGLBufSize; offset += len) {
400 VGLSetSegment(offset);
401 len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size);
402 bcopy(VGLMem, &VGLBuf[offset], len);
407 munmap(VGLDisplay->Bitmap, VGLAdpInfo.va_window_size);
408 ioctl(0, VGLOldMode, 0);
409 ioctl(0, KDSETMODE, KD_TEXT);
410 ioctl(0, KDDISABIO, 0);
411 ioctl(0, VT_RELDISP, VT_TRUE);
412 VGLDisplay->Bitmap = VGLBuf;
413 VGLDisplay->Type = MEMBUF;
414 VGLDisplay->Xsize = VGLDisplay->VXsize;
415 VGLDisplay->Ysize = VGLDisplay->VYsize;
416 while (!VGLOnDisplay) pause();
422 VGLSetSegment(unsigned int offset)
424 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) {
425 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */
426 VGLCurWindow = offset/VGLAdpInfo.va_window_size;
428 return (offset%VGLAdpInfo.va_window_size);
432 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize)
434 if (VXsize < object->Xsize || VYsize < object->Ysize)
436 if (object->Type == MEMBUF)
438 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize))
440 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */
441 object->VXsize = VGLAdpInfo.va_line_width
442 *8/(VGLModeInfo.vi_depth/VGLModeInfo.vi_planes);
443 object->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
444 if (VYsize < object->VYsize)
445 object->VYsize = VYsize;
448 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
449 object->Xsize, object->Ysize, object->VXsize, object->VYsize);
456 VGLPanScreen(VGLBitmap *object, int x, int y)
458 video_display_start_t origin;
460 if (x < 0 || x + object->Xsize > object->VXsize
461 || y < 0 || y + object->Ysize > object->VYsize)
463 if (object->Type == MEMBUF)
467 if (ioctl(0, FBIO_SETDISPSTART, &origin))
473 fprintf(stderr, "new origin: (%d, %d)\n", x, y);