xref: /freebsd/lib/libvgl/main.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
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 <stdio.h>
35 #include <sys/types.h>
36 #include <sys/signal.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 video_info_t VGLModeInfo;
52 video_adapter_info_t VGLAdpInfo;
53 byte *VGLBuf;
54 
55 static int VGLMode;
56 static int VGLOldMode;
57 static size_t VGLBufSize;
58 static byte *VGLMem = MAP_FAILED;
59 static int VGLSwitchPending;
60 static int VGLAbortPending;
61 static int VGLOnDisplay;
62 static unsigned int VGLCurWindow;
63 static int VGLInitDone = 0;
64 static vid_info_t VGLOldVInfo;
65 
66 void
67 VGLEnd()
68 {
69 struct vt_mode smode;
70 
71   if (!VGLInitDone)
72     return;
73   VGLInitDone = 0;
74   VGLSwitchPending = 0;
75   VGLAbortPending = 0;
76 
77   signal(SIGUSR1, SIG_IGN);
78 
79   if (VGLMem != MAP_FAILED) {
80     VGLClear(VGLDisplay, 0);
81     munmap(VGLMem, VGLAdpInfo.va_window_size);
82   }
83 
84   if (VGLOldMode >= M_VESA_BASE) {
85     /* ugly, but necessary */
86     ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0);
87     if (VGLOldMode == M_VESA_800x600) {
88       int size[3];
89       size[0] = VGLOldVInfo.mv_csz;
90       size[1] = VGLOldVInfo.mv_rsz;
91       size[2] = 16;
92       ioctl(0, KDRASTER, size);
93     }
94   } else {
95     ioctl(0, _IO('S', VGLOldMode), 0);
96   }
97   ioctl(0, KDDISABIO, 0);
98   ioctl(0, KDSETMODE, KD_TEXT);
99   smode.mode = VT_AUTO;
100   ioctl(0, VT_SETMODE, &smode);
101   if (VGLBuf)
102     free(VGLBuf);
103   VGLBuf = NULL;
104   free(VGLDisplay);
105   VGLDisplay = NULL;
106   VGLKeyboardEnd();
107 }
108 
109 static void
110 VGLAbort(int arg __unused)
111 {
112   VGLAbortPending = 1;
113   signal(SIGINT, SIG_IGN);
114   signal(SIGTERM, SIG_IGN);
115   signal(SIGSEGV, SIG_IGN);
116   signal(SIGBUS, SIG_IGN);
117   signal(SIGUSR2, SIG_IGN);
118 }
119 
120 static void
121 VGLSwitch(int arg __unused)
122 {
123   if (!VGLOnDisplay)
124     VGLOnDisplay = 1;
125   else
126     VGLOnDisplay = 0;
127   VGLSwitchPending = 1;
128   signal(SIGUSR1, VGLSwitch);
129 }
130 
131 int
132 VGLInit(int mode)
133 {
134   struct vt_mode smode;
135   int adptype;
136 
137   if (VGLInitDone)
138     return -1;
139 
140   signal(SIGUSR1, VGLSwitch);
141   signal(SIGINT, VGLAbort);
142   signal(SIGTERM, VGLAbort);
143   signal(SIGSEGV, VGLAbort);
144   signal(SIGBUS, VGLAbort);
145   signal(SIGUSR2, SIG_IGN);
146 
147   VGLOnDisplay = 1;
148   VGLSwitchPending = 0;
149   VGLAbortPending = 0;
150 
151   if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype))
152     return -1;
153   if (IOCGROUP(mode) == 'V')	/* XXX: this is ugly */
154     VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE;
155   else
156     VGLModeInfo.vi_mode = mode & 0x0ff;
157   if (ioctl(0, CONS_MODEINFO, &VGLModeInfo))	/* FBIO_MODEINFO */
158     return -1;
159 
160   /* If current mode is VESA_800x600 then save its geometry to restore later */
161   if ((VGLOldMode >= M_VESA_BASE) && (VGLOldMode == M_VESA_800x600)) {
162     VGLOldVInfo.size = sizeof(VGLOldVInfo);
163     if (ioctl(0, CONS_GETINFO, &VGLOldVInfo))
164       return -1;
165   }
166 
167   VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap));
168   if (VGLDisplay == NULL)
169     return -2;
170 
171   if (ioctl(0, KDENABIO, 0)) {
172     free(VGLDisplay);
173     return -3;
174   }
175 
176   VGLInitDone = 1;
177 
178   /*
179    * vi_mem_model specifies the memory model of the current video mode
180    * in -CURRENT.
181    */
182   switch (VGLModeInfo.vi_mem_model) {
183   case V_INFO_MM_PLANAR:
184     /* we can handle EGA/VGA planner modes only */
185     if (VGLModeInfo.vi_depth != 4 || VGLModeInfo.vi_planes != 4
186 	|| (adptype != KD_EGA && adptype != KD_VGA)) {
187       VGLEnd();
188       return -4;
189     }
190     VGLDisplay->Type = VIDBUF4;
191     break;
192   case V_INFO_MM_PACKED:
193     /* we can do only 256 color packed modes */
194     if (VGLModeInfo.vi_depth != 8) {
195       VGLEnd();
196       return -4;
197     }
198     VGLDisplay->Type = VIDBUF8;
199     VGLDisplay->PixelBytes = 1;
200     break;
201   case V_INFO_MM_VGAX:
202     VGLDisplay->Type = VIDBUF8X;
203     VGLDisplay->PixelBytes = 1;
204     break;
205   case V_INFO_MM_DIRECT:
206     VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size;
207     switch (VGLDisplay->PixelBytes) {
208     case 2:
209       VGLDisplay->Type = VIDBUF16;
210       break;
211 #if notyet
212     case 3:
213       VGLDisplay->Type = VIDBUF24;
214       break;
215 #endif
216     case 4:
217       VGLDisplay->Type = VIDBUF32;
218       break;
219     default:
220       VGLEnd();
221       return -4;
222     }
223     break;
224   default:
225     VGLEnd();
226     return -4;
227   }
228 
229   ioctl(0, VT_WAITACTIVE, 0);
230   ioctl(0, KDSETMODE, KD_GRAPHICS);
231   if (ioctl(0, mode, 0)) {
232     VGLEnd();
233     return -5;
234   }
235   if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) {	/* FBIO_ADPINFO */
236     VGLEnd();
237     return -6;
238   }
239 
240   /*
241    * Calculate the shadow screen buffer size.  In -CURRENT, va_buffer_size
242    * always holds the entire frame buffer size, wheather it's in the linear
243    * mode or windowed mode.
244    *     VGLBufSize = VGLAdpInfo.va_buffer_size;
245    * In -STABLE, va_buffer_size holds the frame buffer size, only if
246    * the linear frame buffer mode is supported. Otherwise the field is zero.
247    * We shall calculate the minimal size in this case:
248    *     VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*VGLModeInfo.vi_planes
249    * or
250    *     VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes;
251    * Use whichever is larger.
252    */
253   if (VGLAdpInfo.va_buffer_size != 0)
254     VGLBufSize = VGLAdpInfo.va_buffer_size;
255   else
256     VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height,
257 		     VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes;
258   VGLBuf = malloc(VGLBufSize);
259   if (VGLBuf == NULL) {
260     VGLEnd();
261     return -7;
262   }
263 
264 #ifdef LIBVGL_DEBUG
265   fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize);
266 #endif
267 
268   /* see if we are in the windowed buffer mode or in the linear buffer mode */
269   if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) {
270     switch (VGLDisplay->Type) {
271     case VIDBUF4:
272       VGLDisplay->Type = VIDBUF4S;
273       break;
274     case VIDBUF8:
275       VGLDisplay->Type = VIDBUF8S;
276       break;
277     case VIDBUF16:
278       VGLDisplay->Type = VIDBUF16S;
279       break;
280     case VIDBUF24:
281       VGLDisplay->Type = VIDBUF24S;
282       break;
283     case VIDBUF32:
284       VGLDisplay->Type = VIDBUF32S;
285       break;
286     default:
287       VGLEnd();
288       return -8;
289     }
290   }
291 
292   VGLMode = mode;
293   VGLCurWindow = 0;
294 
295   VGLDisplay->Xsize = VGLModeInfo.vi_width;
296   VGLDisplay->Ysize = VGLModeInfo.vi_height;
297   VGLDisplay->VXsize = VGLAdpInfo.va_line_width
298 			   *8/(VGLModeInfo.vi_depth/VGLModeInfo.vi_planes);
299   VGLDisplay->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
300   VGLDisplay->Xorigin = 0;
301   VGLDisplay->Yorigin = 0;
302 
303   VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
304 		       MAP_FILE, 0, 0);
305   if (VGLMem == MAP_FAILED) {
306     VGLEnd();
307     return -7;
308   }
309   VGLDisplay->Bitmap = VGLMem;
310 
311   VGLSavePalette();
312 
313 #ifdef LIBVGL_DEBUG
314   fprintf(stderr, "va_line_width:%d\n", VGLAdpInfo.va_line_width);
315   fprintf(stderr, "VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
316 	  VGLDisplay->Xsize, VGLDisplay->Ysize,
317 	  VGLDisplay->VXsize, VGLDisplay->VYsize);
318 #endif
319 
320   smode.mode = VT_PROCESS;
321   smode.waitv = 0;
322   smode.relsig = SIGUSR1;
323   smode.acqsig = SIGUSR1;
324   smode.frsig  = SIGINT;
325   if (ioctl(0, VT_SETMODE, &smode)) {
326     VGLEnd();
327     return -9;
328   }
329   VGLTextSetFontFile((byte*)0);
330   VGLClear(VGLDisplay, 0);
331   return 0;
332 }
333 
334 void
335 VGLCheckSwitch()
336 {
337   if (VGLAbortPending) {
338     VGLEnd();
339     exit(0);
340   }
341   while (VGLSwitchPending) {
342     unsigned int offset;
343     unsigned int len;
344     int i;
345 
346     VGLSwitchPending = 0;
347     if (VGLOnDisplay) {
348       ioctl(0, KDENABIO, 0);
349       ioctl(0, KDSETMODE, KD_GRAPHICS);
350       ioctl(0, VGLMode, 0);
351       VGLCurWindow = 0;
352       VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
353 			   MAP_FILE, 0, 0);
354 
355       /* XXX: what if mmap() has failed! */
356       VGLDisplay->Type = VIDBUF8;	/* XXX */
357       switch (VGLModeInfo.vi_mem_model) {
358       case V_INFO_MM_PLANAR:
359 	if (VGLModeInfo.vi_depth == 4 && VGLModeInfo.vi_planes == 4) {
360 	  if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
361 	    VGLDisplay->Type = VIDBUF4S;
362 	  else
363 	    VGLDisplay->Type = VIDBUF4;
364 	} else {
365 	  /* shouldn't be happening */
366 	}
367         break;
368       case V_INFO_MM_PACKED:
369 	if (VGLModeInfo.vi_depth == 8) {
370 	  if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
371 	    VGLDisplay->Type = VIDBUF8S;
372 	  else
373 	    VGLDisplay->Type = VIDBUF8;
374 	}
375         break;
376       case V_INFO_MM_VGAX:
377 	VGLDisplay->Type = VIDBUF8X;
378 	break;
379       case V_INFO_MM_DIRECT:
380 	switch (VGLModeInfo.vi_pixel_size) {
381 	  case 2:
382 	    if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
383 	      VGLDisplay->Type = VIDBUF16S;
384 	    else
385 	      VGLDisplay->Type = VIDBUF16;
386 	    break;
387 	  case 3:
388 	    if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
389 	      VGLDisplay->Type = VIDBUF24S;
390 	    else
391 	      VGLDisplay->Type = VIDBUF24;
392 	    break;
393 	  case 4:
394 	    if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
395 	      VGLDisplay->Type = VIDBUF32S;
396 	    else
397 	      VGLDisplay->Type = VIDBUF32;
398 	    break;
399 	  default:
400 	  /* shouldn't be happening */
401           break;
402         }
403       default:
404 	/* shouldn't be happening */
405         break;
406       }
407 
408       VGLDisplay->Bitmap = VGLMem;
409       VGLDisplay->Xsize = VGLModeInfo.vi_width;
410       VGLDisplay->Ysize = VGLModeInfo.vi_height;
411       VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize);
412       VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin);
413       switch (VGLDisplay->Type) {
414       case VIDBUF4S:
415 	outb(0x3c6, 0xff);
416 	outb(0x3ce, 0x01); outb(0x3cf, 0x00);		/* set/reset enable */
417 	outb(0x3ce, 0x08); outb(0x3cf, 0xff);		/* bit mask */
418 	for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes;
419 	     offset += len) {
420 	  VGLSetSegment(offset);
421 	  len = min(VGLBufSize/VGLModeInfo.vi_planes - offset,
422 		    VGLAdpInfo.va_window_size);
423 	  for (i = 0; i < VGLModeInfo.vi_planes; i++) {
424 	    outb(0x3c4, 0x02);
425 	    outb(0x3c5, 0x01<<i);
426 	    bcopy(&VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
427 		  VGLMem, len);
428 	  }
429 	}
430 	break;
431       case VIDBUF4:
432       case VIDBUF8X:
433 	outb(0x3c6, 0xff);
434 	outb(0x3ce, 0x01); outb(0x3cf, 0x00);		/* set/reset enable */
435 	outb(0x3ce, 0x08); outb(0x3cf, 0xff);		/* bit mask */
436 	for (i = 0; i < VGLModeInfo.vi_planes; i++) {
437 	  outb(0x3c4, 0x02);
438 	  outb(0x3c5, 0x01<<i);
439 	  bcopy(&VGLBuf[i*VGLAdpInfo.va_window_size], VGLMem,
440 		VGLAdpInfo.va_window_size);
441 	}
442 	break;
443       case VIDBUF8:
444       case VIDBUF8S:
445       case VIDBUF16:
446       case VIDBUF16S:
447       case VIDBUF24:
448       case VIDBUF24S:
449       case VIDBUF32:
450       case VIDBUF32S:
451 	for (offset = 0; offset < VGLBufSize; offset += len) {
452 	  VGLSetSegment(offset);
453 	  len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size);
454           bcopy(&VGLBuf[offset], VGLMem, len);
455 	}
456 	break;
457       }
458       VGLRestorePalette();
459       ioctl(0, VT_RELDISP, VT_ACKACQ);
460     }
461     else {
462       switch (VGLDisplay->Type) {
463       case VIDBUF4S:
464 	for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes;
465 	     offset += len) {
466 	  VGLSetSegment(offset);
467 	  len = min(VGLBufSize/VGLModeInfo.vi_planes - offset,
468 		    VGLAdpInfo.va_window_size);
469 	  for (i = 0; i < VGLModeInfo.vi_planes; i++) {
470 	    outb(0x3ce, 0x04);
471 	    outb(0x3cf, i);
472 	    bcopy(VGLMem, &VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
473 		  len);
474 	  }
475 	}
476 	break;
477       case VIDBUF4:
478       case VIDBUF8X:
479 	/*
480 	 * NOTE: the saved buffer is NOT in the MEMBUF format which
481 	 * the ordinary memory bitmap object is stored in. XXX
482 	 */
483 	for (i = 0; i < VGLModeInfo.vi_planes; i++) {
484 	  outb(0x3ce, 0x04);
485 	  outb(0x3cf, i);
486 	  bcopy(VGLMem, &VGLBuf[i*VGLAdpInfo.va_window_size],
487 		VGLAdpInfo.va_window_size);
488 	}
489 	break;
490       case VIDBUF8:
491       case VIDBUF8S:
492       case VIDBUF16:
493       case VIDBUF16S:
494       case VIDBUF24:
495       case VIDBUF24S:
496       case VIDBUF32:
497       case VIDBUF32S:
498 	for (offset = 0; offset < VGLBufSize; offset += len) {
499 	  VGLSetSegment(offset);
500 	  len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size);
501           bcopy(VGLMem, &VGLBuf[offset], len);
502 	}
503 	break;
504       }
505       VGLMem = MAP_FAILED;
506       munmap(VGLDisplay->Bitmap, VGLAdpInfo.va_window_size);
507       ioctl(0, VGLOldMode, 0);
508       ioctl(0, KDSETMODE, KD_TEXT);
509       ioctl(0, KDDISABIO, 0);
510       ioctl(0, VT_RELDISP, VT_TRUE);
511       VGLDisplay->Bitmap = VGLBuf;
512       VGLDisplay->Type = MEMBUF;
513       VGLDisplay->Xsize = VGLDisplay->VXsize;
514       VGLDisplay->Ysize = VGLDisplay->VYsize;
515       while (!VGLOnDisplay) pause();
516     }
517   }
518 }
519 
520 int
521 VGLSetSegment(unsigned int offset)
522 {
523   if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) {
524     ioctl(0, CONS_SETWINORG, offset);		/* FBIO_SETWINORG */
525     VGLCurWindow = offset/VGLAdpInfo.va_window_size;
526   }
527   return (offset%VGLAdpInfo.va_window_size);
528 }
529 
530 int
531 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize)
532 {
533   if (VXsize < object->Xsize || VYsize < object->Ysize)
534     return -1;
535   if (object->Type == MEMBUF)
536     return -1;
537   if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize))
538     return -1;
539   ioctl(0, CONS_ADPINFO, &VGLAdpInfo);	/* FBIO_ADPINFO */
540   object->VXsize = VGLAdpInfo.va_line_width
541 			   *8/(VGLModeInfo.vi_depth/VGLModeInfo.vi_planes);
542   object->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
543   if (VYsize < object->VYsize)
544     object->VYsize = VYsize;
545 
546 #ifdef LIBVGL_DEBUG
547   fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
548 	  object->Xsize, object->Ysize, object->VXsize, object->VYsize);
549 #endif
550 
551   return 0;
552 }
553 
554 int
555 VGLPanScreen(VGLBitmap *object, int x, int y)
556 {
557   video_display_start_t origin;
558 
559   if (x < 0 || x + object->Xsize > object->VXsize
560       || y < 0 || y + object->Ysize > object->VYsize)
561     return -1;
562   if (object->Type == MEMBUF)
563     return 0;
564   origin.x = x;
565   origin.y = y;
566   if (ioctl(0, FBIO_SETDISPSTART, &origin))
567     return -1;
568   object->Xorigin = x;
569   object->Yorigin = y;
570 
571 #ifdef LIBVGL_DEBUG
572   fprintf(stderr, "new origin: (%d, %d)\n", x, y);
573 #endif
574 
575   return 0;
576 }
577