1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991-1997 Søren Schmidt 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 18 * 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. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <signal.h> 35 #include <stdio.h> 36 #include <sys/types.h> 37 #include <sys/file.h> 38 #include <sys/ioctl.h> 39 #include <sys/mman.h> 40 #include <sys/fbio.h> 41 #include <sys/kbio.h> 42 #include <sys/consio.h> 43 #include "vgl.h" 44 45 /* XXX Direct Color 24bits modes unsupported */ 46 47 #define min(x, y) (((x) < (y)) ? (x) : (y)) 48 #define max(x, y) (((x) > (y)) ? (x) : (y)) 49 50 VGLBitmap *VGLDisplay; 51 VGLBitmap VGLVDisplay; 52 video_info_t VGLModeInfo; 53 video_adapter_info_t VGLAdpInfo; 54 byte *VGLBuf; 55 56 static int VGLMode; 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; 67 68 void 69 VGLEnd() 70 { 71 struct vt_mode smode; 72 int size[3]; 73 74 if (!VGLInitDone) 75 return; 76 VGLInitDone = 0; 77 signal(SIGUSR1, SIG_IGN); 78 signal(SIGUSR2, SIG_IGN); 79 VGLSwitchPending = 0; 80 VGLAbortPending = 0; 81 VGLMousePointerHide(); 82 83 if (VGLMem != MAP_FAILED) { 84 VGLClear(VGLDisplay, 0); 85 munmap(VGLMem, VGLAdpInfo.va_window_size); 86 } 87 88 if (VGLOldMode >= M_VESA_BASE) 89 ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0); 90 else 91 ioctl(0, _IO('S', VGLOldMode), 0); 92 if (VGLOldModeInfo.vi_flags & V_INFO_GRAPHICS) { 93 size[0] = VGLOldVInfo.mv_csz; 94 size[1] = VGLOldVInfo.mv_rsz; 95 size[2] = VGLOldVInfo.font_size;; 96 ioctl(0, KDRASTER, size); 97 } 98 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT) 99 ioctl(0, KDDISABIO, 0); 100 ioctl(0, KDSETMODE, KD_TEXT); 101 smode.mode = VT_AUTO; 102 ioctl(0, VT_SETMODE, &smode); 103 if (VGLBuf) 104 free(VGLBuf); 105 VGLBuf = NULL; 106 free(VGLDisplay); 107 VGLDisplay = NULL; 108 VGLKeyboardEnd(); 109 } 110 111 static void 112 VGLAbort(int arg) 113 { 114 sigset_t mask; 115 116 VGLAbortPending = 1; 117 signal(SIGINT, SIG_IGN); 118 signal(SIGTERM, SIG_IGN); 119 signal(SIGUSR2, SIG_IGN); 120 if (arg == SIGBUS || arg == SIGSEGV) { 121 signal(arg, SIG_DFL); 122 sigemptyset(&mask); 123 sigaddset(&mask, arg); 124 sigprocmask(SIG_UNBLOCK, &mask, NULL); 125 VGLEnd(); 126 kill(getpid(), arg); 127 } 128 } 129 130 static void 131 VGLSwitch(int arg __unused) 132 { 133 if (!VGLOnDisplay) 134 VGLOnDisplay = 1; 135 else 136 VGLOnDisplay = 0; 137 VGLSwitchPending = 1; 138 signal(SIGUSR1, VGLSwitch); 139 } 140 141 int 142 VGLInit(int mode) 143 { 144 struct vt_mode smode; 145 int adptype, depth; 146 147 if (VGLInitDone) 148 return -1; 149 150 signal(SIGUSR1, VGLSwitch); 151 signal(SIGINT, VGLAbort); 152 signal(SIGTERM, VGLAbort); 153 signal(SIGSEGV, VGLAbort); 154 signal(SIGBUS, VGLAbort); 155 signal(SIGUSR2, SIG_IGN); 156 157 VGLOnDisplay = 1; 158 VGLSwitchPending = 0; 159 VGLAbortPending = 0; 160 161 if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype)) 162 return -1; 163 if (IOCGROUP(mode) == 'V') /* XXX: this is ugly */ 164 VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE; 165 else 166 VGLModeInfo.vi_mode = mode & 0x0ff; 167 if (ioctl(0, CONS_MODEINFO, &VGLModeInfo)) /* FBIO_MODEINFO */ 168 return -1; 169 170 /* Save info for old mode to restore font size if old mode is graphics. */ 171 VGLOldModeInfo.vi_mode = VGLOldMode; 172 if (ioctl(0, CONS_MODEINFO, &VGLOldModeInfo)) 173 return -1; 174 VGLOldVInfo.size = sizeof(VGLOldVInfo); 175 if (ioctl(0, CONS_GETINFO, &VGLOldVInfo)) 176 return -1; 177 178 VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap)); 179 if (VGLDisplay == NULL) 180 return -2; 181 182 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0)) { 183 free(VGLDisplay); 184 return -3; 185 } 186 187 VGLInitDone = 1; 188 189 /* 190 * vi_mem_model specifies the memory model of the current video mode 191 * in -CURRENT. 192 */ 193 switch (VGLModeInfo.vi_mem_model) { 194 case V_INFO_MM_PLANAR: 195 /* we can handle EGA/VGA planner modes only */ 196 if (VGLModeInfo.vi_depth != 4 || VGLModeInfo.vi_planes != 4 197 || (adptype != KD_EGA && adptype != KD_VGA)) { 198 VGLEnd(); 199 return -4; 200 } 201 VGLDisplay->Type = VIDBUF4; 202 VGLDisplay->PixelBytes = 1; 203 break; 204 case V_INFO_MM_PACKED: 205 /* we can do only 256 color packed modes */ 206 if (VGLModeInfo.vi_depth != 8) { 207 VGLEnd(); 208 return -4; 209 } 210 VGLDisplay->Type = VIDBUF8; 211 VGLDisplay->PixelBytes = 1; 212 break; 213 case V_INFO_MM_VGAX: 214 VGLDisplay->Type = VIDBUF8X; 215 VGLDisplay->PixelBytes = 1; 216 break; 217 case V_INFO_MM_DIRECT: 218 VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size; 219 switch (VGLDisplay->PixelBytes) { 220 case 2: 221 VGLDisplay->Type = VIDBUF16; 222 break; 223 #if notyet 224 case 3: 225 VGLDisplay->Type = VIDBUF24; 226 break; 227 #endif 228 case 4: 229 VGLDisplay->Type = VIDBUF32; 230 break; 231 default: 232 VGLEnd(); 233 return -4; 234 } 235 break; 236 default: 237 VGLEnd(); 238 return -4; 239 } 240 241 ioctl(0, VT_WAITACTIVE, 0); 242 ioctl(0, KDSETMODE, KD_GRAPHICS); 243 if (ioctl(0, mode, 0)) { 244 VGLEnd(); 245 return -5; 246 } 247 if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) { /* FBIO_ADPINFO */ 248 VGLEnd(); 249 return -6; 250 } 251 252 /* 253 * Calculate the shadow screen buffer size. In -CURRENT, va_buffer_size 254 * always holds the entire frame buffer size, wheather it's in the linear 255 * mode or windowed mode. 256 * VGLBufSize = VGLAdpInfo.va_buffer_size; 257 * In -STABLE, va_buffer_size holds the frame buffer size, only if 258 * the linear frame buffer mode is supported. Otherwise the field is zero. 259 * We shall calculate the minimal size in this case: 260 * VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*VGLModeInfo.vi_planes 261 * or 262 * VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes; 263 * Use whichever is larger. 264 */ 265 if (VGLAdpInfo.va_buffer_size != 0) 266 VGLBufSize = VGLAdpInfo.va_buffer_size; 267 else 268 VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height, 269 VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes; 270 /* 271 * The above is for old -CURRENT. Current -CURRENT since r203535 and/or 272 * r248799 restricts va_buffer_size to the displayed size in VESA modes to 273 * avoid wasting kva for mapping unused parts of the frame buffer. But all 274 * parts were usable here. Applying the same restriction to user mappings 275 * makes our virtualization useless and breaks our panning, but large frame 276 * buffers are also difficult for us to manage (clearing and switching may 277 * be too slow, and malloc() may fail). Restrict ourselves similarly to 278 * get the same efficiency and bugs for all kernels. 279 */ 280 if (VGLModeInfo.vi_mode >= M_VESA_BASE) 281 VGLBufSize = VGLAdpInfo.va_line_width*VGLModeInfo.vi_height* 282 VGLModeInfo.vi_planes; 283 VGLBuf = malloc(VGLBufSize); 284 if (VGLBuf == NULL) { 285 VGLEnd(); 286 return -7; 287 } 288 289 #ifdef LIBVGL_DEBUG 290 fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize); 291 #endif 292 293 /* see if we are in the windowed buffer mode or in the linear buffer mode */ 294 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) { 295 switch (VGLDisplay->Type) { 296 case VIDBUF4: 297 VGLDisplay->Type = VIDBUF4S; 298 break; 299 case VIDBUF8: 300 VGLDisplay->Type = VIDBUF8S; 301 break; 302 case VIDBUF16: 303 VGLDisplay->Type = VIDBUF16S; 304 break; 305 case VIDBUF24: 306 VGLDisplay->Type = VIDBUF24S; 307 break; 308 case VIDBUF32: 309 VGLDisplay->Type = VIDBUF32S; 310 break; 311 default: 312 VGLEnd(); 313 return -8; 314 } 315 } 316 317 VGLMode = mode; 318 VGLCurWindow = 0; 319 320 VGLDisplay->Xsize = VGLModeInfo.vi_width; 321 VGLDisplay->Ysize = VGLModeInfo.vi_height; 322 depth = VGLModeInfo.vi_depth; 323 if (depth == 15) 324 depth = 16; 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; 330 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) { 334 VGLEnd(); 335 return -7; 336 } 337 VGLDisplay->Bitmap = VGLMem; 338 339 VGLVDisplay = *VGLDisplay; 340 VGLVDisplay.Type = MEMBUF; 341 if (VGLModeInfo.vi_depth < 8) 342 VGLVDisplay.Bitmap = malloc(2 * VGLBufSize); 343 else 344 VGLVDisplay.Bitmap = VGLBuf; 345 346 VGLSavePalette(); 347 348 #ifdef LIBVGL_DEBUG 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); 353 #endif 354 355 smode.mode = VT_PROCESS; 356 smode.waitv = 0; 357 smode.relsig = SIGUSR1; 358 smode.acqsig = SIGUSR1; 359 smode.frsig = SIGINT; 360 if (ioctl(0, VT_SETMODE, &smode)) { 361 VGLEnd(); 362 return -9; 363 } 364 VGLTextSetFontFile((byte*)0); 365 VGLClear(VGLDisplay, 0); 366 return 0; 367 } 368 369 void 370 VGLCheckSwitch() 371 { 372 if (VGLAbortPending) { 373 VGLEnd(); 374 exit(0); 375 } 376 while (VGLSwitchPending) { 377 VGLSwitchPending = 0; 378 if (VGLOnDisplay) { 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); 383 VGLCurWindow = 0; 384 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE, 385 MAP_FILE | MAP_SHARED, 0, 0); 386 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; 394 else 395 VGLDisplay->Type = VIDBUF4; 396 } else { 397 /* shouldn't be happening */ 398 } 399 break; 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; 404 else 405 VGLDisplay->Type = VIDBUF8; 406 } 407 break; 408 case V_INFO_MM_VGAX: 409 VGLDisplay->Type = VIDBUF8X; 410 break; 411 case V_INFO_MM_DIRECT: 412 switch (VGLModeInfo.vi_pixel_size) { 413 case 2: 414 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 415 VGLDisplay->Type = VIDBUF16S; 416 else 417 VGLDisplay->Type = VIDBUF16; 418 break; 419 case 3: 420 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 421 VGLDisplay->Type = VIDBUF24S; 422 else 423 VGLDisplay->Type = VIDBUF24; 424 break; 425 case 4: 426 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 427 VGLDisplay->Type = VIDBUF32S; 428 else 429 VGLDisplay->Type = VIDBUF32; 430 break; 431 default: 432 /* shouldn't be happening */ 433 break; 434 } 435 default: 436 /* shouldn't be happening */ 437 break; 438 } 439 440 VGLDisplay->Bitmap = VGLMem; 441 VGLDisplay->Xsize = VGLModeInfo.vi_width; 442 VGLDisplay->Ysize = VGLModeInfo.vi_height; 443 VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize); 444 VGLRestoreBlank(); 445 VGLRestoreBorder(); 446 VGLMouseRestore(); 447 VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin); 448 VGLBitmapCopy(&VGLVDisplay, 0, 0, VGLDisplay, 0, 0, 449 VGLDisplay->VXsize, VGLDisplay->VYsize); 450 VGLRestorePalette(); 451 ioctl(0, VT_RELDISP, VT_ACKACQ); 452 } 453 else { 454 VGLMem = MAP_FAILED; 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(); 466 } 467 } 468 } 469 470 int 471 VGLSetSegment(unsigned int offset) 472 { 473 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) { 474 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */ 475 VGLCurWindow = offset/VGLAdpInfo.va_window_size; 476 } 477 return (offset%VGLAdpInfo.va_window_size); 478 } 479 480 int 481 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize) 482 { 483 int depth; 484 485 if (VXsize < object->Xsize || VYsize < object->Ysize) 486 return -1; 487 if (object->Type == MEMBUF) 488 return -1; 489 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize)) 490 return -1; 491 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */ 492 depth = VGLModeInfo.vi_depth; 493 if (depth == 15) 494 depth = 16; 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; 500 501 #ifdef LIBVGL_DEBUG 502 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n", 503 object->Xsize, object->Ysize, object->VXsize, object->VYsize); 504 #endif 505 506 return 0; 507 } 508 509 int 510 VGLPanScreen(VGLBitmap *object, int x, int y) 511 { 512 video_display_start_t origin; 513 514 if (x < 0 || x + object->Xsize > object->VXsize 515 || y < 0 || y + object->Ysize > object->VYsize) 516 return -1; 517 if (object->Type == MEMBUF) 518 return 0; 519 origin.x = x; 520 origin.y = y; 521 if (ioctl(0, FBIO_SETDISPSTART, &origin)) 522 return -1; 523 object->Xorigin = x; 524 object->Yorigin = y; 525 526 #ifdef LIBVGL_DEBUG 527 fprintf(stderr, "new origin: (%d, %d)\n", x, y); 528 #endif 529 530 return 0; 531 } 532