xref: /freebsd/lib/libvgl/main.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
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 #define min(x, y)	(((x) < (y)) ? (x) : (y))
46 #define max(x, y)	(((x) > (y)) ? (x) : (y))
47 
48 VGLBitmap *VGLDisplay;
49 VGLBitmap VGLVDisplay;
50 video_info_t VGLModeInfo;
51 video_adapter_info_t VGLAdpInfo;
52 byte *VGLBuf;
53 
54 static int VGLMode;
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;
66 
67 void
68 VGLEnd()
69 {
70 struct vt_mode smode;
71   int size[3];
72 
73   if (!VGLInitDone)
74     return;
75   VGLInitDone = 0;
76   signal(SIGUSR1, SIG_IGN);
77   signal(SIGUSR2, SIG_IGN);
78   VGLSwitchPending = 0;
79   VGLAbortPending = 0;
80   VGLMouseMode(VGL_MOUSEHIDE);
81 
82   if (VGLMem != MAP_FAILED) {
83     VGLClear(VGLDisplay, 0);
84     munmap(VGLMem, VGLAdpInfo.va_window_size);
85   }
86 
87   ioctl(0, FBIO_SETLINEWIDTH, &VGLOldVXsize);
88 
89   if (VGLOldMode >= M_VESA_BASE)
90     ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0);
91   else
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);
98   }
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);
104   if (VGLBuf)
105     free(VGLBuf);
106   VGLBuf = NULL;
107   free(VGLDisplay);
108   VGLDisplay = NULL;
109   VGLKeyboardEnd();
110 }
111 
112 static void
113 VGLAbort(int arg)
114 {
115   sigset_t mask;
116 
117   VGLAbortPending = 1;
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);
123     sigemptyset(&mask);
124     sigaddset(&mask, arg);
125     sigprocmask(SIG_UNBLOCK, &mask, NULL);
126     VGLEnd();
127     kill(getpid(), arg);
128   }
129 }
130 
131 static void
132 VGLSwitch(int arg __unused)
133 {
134   if (!VGLOnDisplay)
135     VGLOnDisplay = 1;
136   else
137     VGLOnDisplay = 0;
138   VGLSwitchPending = 1;
139   signal(SIGUSR1, VGLSwitch);
140 }
141 
142 int
143 VGLInit(int mode)
144 {
145   struct vt_mode smode;
146   int adptype, depth;
147 
148   if (VGLInitDone)
149     return -1;
150 
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);
157 
158   VGLOnDisplay = 1;
159   VGLSwitchPending = 0;
160   VGLAbortPending = 0;
161 
162   if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype))
163     return -1;
164   if (IOCGROUP(mode) == 'V')	/* XXX: this is ugly */
165     VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE;
166   else
167     VGLModeInfo.vi_mode = mode & 0x0ff;
168   if (ioctl(0, CONS_MODEINFO, &VGLModeInfo))	/* FBIO_MODEINFO */
169     return -1;
170 
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))
174     return -1;
175   VGLOldVInfo.size = sizeof(VGLOldVInfo);
176   if (ioctl(0, CONS_GETINFO, &VGLOldVInfo))
177     return -1;
178 
179   VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap));
180   if (VGLDisplay == NULL)
181     return -2;
182 
183   if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0)) {
184     free(VGLDisplay);
185     return -3;
186   }
187 
188   VGLInitDone = 1;
189 
190   /*
191    * vi_mem_model specifies the memory model of the current video mode
192    * in -CURRENT.
193    */
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)) {
199       VGLEnd();
200       return -4;
201     }
202     VGLDisplay->Type = VIDBUF4;
203     VGLDisplay->PixelBytes = 1;
204     break;
205   case V_INFO_MM_PACKED:
206     /* we can do only 256 color packed modes */
207     if (VGLModeInfo.vi_depth != 8) {
208       VGLEnd();
209       return -4;
210     }
211     VGLDisplay->Type = VIDBUF8;
212     VGLDisplay->PixelBytes = 1;
213     break;
214   case V_INFO_MM_VGAX:
215     VGLDisplay->Type = VIDBUF8X;
216     VGLDisplay->PixelBytes = 1;
217     break;
218   case V_INFO_MM_DIRECT:
219     VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size;
220     switch (VGLDisplay->PixelBytes) {
221     case 2:
222       VGLDisplay->Type = VIDBUF16;
223       break;
224     case 3:
225       VGLDisplay->Type = VIDBUF24;
226       break;
227     case 4:
228       VGLDisplay->Type = VIDBUF32;
229       break;
230     default:
231       VGLEnd();
232       return -4;
233     }
234     break;
235   default:
236     VGLEnd();
237     return -4;
238   }
239 
240   ioctl(0, VT_WAITACTIVE, 0);
241   ioctl(0, KDSETMODE, KD_GRAPHICS);
242   if (ioctl(0, mode, 0)) {
243     VGLEnd();
244     return -5;
245   }
246   if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) {	/* FBIO_ADPINFO */
247     VGLEnd();
248     return -6;
249   }
250 
251   /*
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
260    * or
261    *     VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes;
262    * Use whichever is larger.
263    */
264   if (VGLAdpInfo.va_buffer_size != 0)
265     VGLBufSize = VGLAdpInfo.va_buffer_size;
266   else
267     VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height,
268 		     VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes;
269   /*
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.
278    */
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) {
284     VGLEnd();
285     return -7;
286   }
287 
288 #ifdef LIBVGL_DEBUG
289   fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize);
290 #endif
291 
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) {
295     case VIDBUF4:
296       VGLDisplay->Type = VIDBUF4S;
297       break;
298     case VIDBUF8:
299       VGLDisplay->Type = VIDBUF8S;
300       break;
301     case VIDBUF16:
302       VGLDisplay->Type = VIDBUF16S;
303       break;
304     case VIDBUF24:
305       VGLDisplay->Type = VIDBUF24S;
306       break;
307     case VIDBUF32:
308       VGLDisplay->Type = VIDBUF32S;
309       break;
310     default:
311       VGLEnd();
312       return -8;
313     }
314   }
315 
316   VGLMode = mode;
317   VGLCurWindow = 0;
318 
319   VGLDisplay->Xsize = VGLModeInfo.vi_width;
320   VGLDisplay->Ysize = VGLModeInfo.vi_height;
321   depth = VGLModeInfo.vi_depth;
322   if (depth == 15)
323     depth = 16;
324   VGLOldVXsize =
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