xref: /freebsd/lib/libvgl/simple.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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 #include <signal.h>
33 #include <sys/fbio.h>
34 #include <sys/kbio.h>
35 #include <sys/endian.h>
36 #include "vgl.h"
37 
38 static int VGLBlank;
39 static byte VGLBorderColor;
40 static byte VGLSavePaletteRed[256];
41 static byte VGLSavePaletteGreen[256];
42 static byte VGLSavePaletteBlue[256];
43 
44 #define ABS(a)		(((a)<0) ? -(a) : (a))
45 #define SGN(a)		(((a)<0) ? -1 : 1)
46 #define min(x, y)	(((x) < (y)) ? (x) : (y))
47 #define max(x, y)	(((x) > (y)) ? (x) : (y))
48 
49 void
VGLSetXY(VGLBitmap * object,int x,int y,u_long color)50 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
51 {
52   int offset, soffset, undermouse;
53 
54   VGLCheckSwitch();
55   if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
56     if (object == VGLDisplay) {
57       undermouse = VGLMouseFreezeXY(x, y);
58       VGLSetXY(&VGLVDisplay, x, y, color);
59     } else if (object->Type != MEMBUF)
60       return;		/* invalid */
61     else
62       undermouse = 0;
63     if (!undermouse) {
64       offset = (y * object->VXsize + x) * object->PixelBytes;
65       switch (object->Type) {
66       case VIDBUF8S:
67       case VIDBUF16S:
68       case VIDBUF32S:
69         offset = VGLSetSegment(offset);
70         /* FALLTHROUGH */
71       case MEMBUF:
72       case VIDBUF8:
73       case VIDBUF16:
74       case VIDBUF24:
75       case VIDBUF32:
76         color = htole32(color);
77         switch (object->PixelBytes) {
78         case 1:
79           memcpy(&object->Bitmap[offset], &color, 1);
80           break;
81         case 2:
82           memcpy(&object->Bitmap[offset], &color, 2);
83           break;
84         case 3:
85           memcpy(&object->Bitmap[offset], &color, 3);
86           break;
87         case 4:
88           memcpy(&object->Bitmap[offset], &color, 4);
89           break;
90         }
91         break;
92       case VIDBUF24S:
93         soffset = VGLSetSegment(offset);
94         color = htole32(color);
95         switch (VGLAdpInfo.va_window_size - soffset) {
96         case 1:
97           memcpy(&object->Bitmap[soffset], &color, 1);
98           soffset = VGLSetSegment(offset + 1);
99           memcpy(&object->Bitmap[soffset], (byte *)&color + 1, 2);
100           break;
101         case 2:
102           memcpy(&object->Bitmap[soffset], &color, 2);
103           soffset = VGLSetSegment(offset + 2);
104           memcpy(&object->Bitmap[soffset], (byte *)&color + 2, 1);
105           break;
106         default:
107           memcpy(&object->Bitmap[soffset], &color, 3);
108           break;
109         }
110         break;
111       case VIDBUF8X:
112         outb(0x3c4, 0x02);
113         outb(0x3c5, 0x01 << (x&0x3));
114 	object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
115 	break;
116       case VIDBUF4S:
117 	offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
118 	goto set_planar;
119       case VIDBUF4:
120 	offset = y*VGLAdpInfo.va_line_width + x/8;
121 set_planar:
122         outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
123         outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f);	/* set/reset */
124         outb(0x3ce, 0x01); outb(0x3cf, 0x0f);		/* set/reset enable */
125         outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8));	/* bit mask */
126 	object->Bitmap[offset] |= (byte)color;
127       }
128     }
129     if (object == VGLDisplay)
130       VGLMouseUnFreeze();
131   }
132 }
133 
134 u_long
VGLGetXY(VGLBitmap * object,int x,int y)135 VGLGetXY(VGLBitmap *object, int x, int y)
136 {
137   u_long color;
138   int offset;
139 
140   VGLCheckSwitch();
141   if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
142     return 0;
143   if (object == VGLDisplay)
144     object = &VGLVDisplay;
145   else if (object->Type != MEMBUF)
146     return 0;		/* invalid */
147   offset = (y * object->VXsize + x) * object->PixelBytes;
148   switch (object->PixelBytes) {
149   case 1:
150     memcpy(&color, &object->Bitmap[offset], 1);
151     return le32toh(color) & 0xff;
152   case 2:
153     memcpy(&color, &object->Bitmap[offset], 2);
154     return le32toh(color) & 0xffff;
155   case 3:
156     memcpy(&color, &object->Bitmap[offset], 3);
157     return le32toh(color) & 0xffffff;
158   case 4:
159     memcpy(&color, &object->Bitmap[offset], 4);
160     return le32toh(color);
161   }
162   return 0;		/* invalid */
163 }
164 
165  /*
166   * Symmetric Double Step Line Algorithm by Brian Wyvill from
167   * "Graphics Gems", Academic Press, 1990.
168   */
169 
170 #define SL_SWAP(a,b)           {a^=b; b^=a; a^=b;}
171 #define SL_ABSOLUTE(i,j,k)     ( (i-j)*(k = ( (i-j)<0 ? -1 : 1)))
172 
173 void
plot(VGLBitmap * object,int x,int y,int flag,u_long color)174 plot(VGLBitmap * object, int x, int y, int flag, u_long color)
175 {
176   /* non-zero flag indicates the pixels need swapping back. */
177   if (flag)
178     VGLSetXY(object, y, x, color);
179   else
180     VGLSetXY(object, x, y, color);
181 }
182 
183 
184 void
VGLLine(VGLBitmap * object,int x1,int y1,int x2,int y2,u_long color)185 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
186 {
187   int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
188   int sign_x, sign_y, step, reverse, i;
189 
190   dx = SL_ABSOLUTE(x2, x1, sign_x);
191   dy = SL_ABSOLUTE(y2, y1, sign_y);
192   /* decide increment sign by the slope sign */
193   if (sign_x == sign_y)
194     step = 1;
195   else
196     step = -1;
197 
198   if (dy > dx) {	/* chooses axis of greatest movement (make dx) */
199     SL_SWAP(x1, y1);
200     SL_SWAP(x2, y2);
201     SL_SWAP(dx, dy);
202     reverse = 1;
203   } else
204     reverse = 0;
205   /* note error check for dx==0 should be included here */
206   if (x1 > x2) {      /* start from the smaller coordinate */
207     x = x2;
208     y = y2;
209 /*  x1 = x1;
210     y1 = y1; */
211   } else {
212     x = x1;
213     y = y1;
214     x1 = x2;
215     y1 = y2;
216   }
217 
218 
219   /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
220   /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
221   /* In fact (dx-1)/4 as 2 pixels are already plotted */
222   xend = (dx - 1) / 4;
223   pixels_left = (dx - 1) % 4;  /* number of pixels left over at the
224            * end */
225   plot(object, x, y, reverse, color);
226   if (pixels_left < 0)
227     return;      /* plot only one pixel for zero length
228            * vectors */
229   plot(object, x1, y1, reverse, color);  /* plot first two points */
230   incr2 = 4 * dy - 2 * dx;
231   if (incr2 < 0) {    /* slope less than 1/2 */
232     c = 2 * dy;
233     incr1 = 2 * c;
234     D = incr1 - dx;
235 
236     for (i = 0; i < xend; i++) {  /* plotting loop */
237       ++x;
238       --x1;
239       if (D < 0) {
240         /* pattern 1 forwards */
241         plot(object, x, y, reverse, color);
242         plot(object, ++x, y, reverse, color);
243         /* pattern 1 backwards */
244         plot(object, x1, y1, reverse, color);
245         plot(object, --x1, y1, reverse, color);
246         D += incr1;
247       } else {
248         if (D < c) {
249           /* pattern 2 forwards */
250           plot(object, x, y, reverse, color);
251           plot(object, ++x, y += step, reverse,
252               color);
253           /* pattern 2 backwards */
254           plot(object, x1, y1, reverse, color);
255           plot(object, --x1, y1 -= step, reverse,
256               color);
257         } else {
258           /* pattern 3 forwards */
259           plot(object, x, y += step, reverse, color);
260           plot(object, ++x, y, reverse, color);
261           /* pattern 3 backwards */
262           plot(object, x1, y1 -= step, reverse,
263               color);
264           plot(object, --x1, y1, reverse, color);
265         }
266         D += incr2;
267       }
268     }      /* end for */
269 
270     /* plot last pattern */
271     if (pixels_left) {
272       if (D < 0) {
273         plot(object, ++x, y, reverse, color);  /* pattern 1 */
274         if (pixels_left > 1)
275           plot(object, ++x, y, reverse, color);
276         if (pixels_left > 2)
277           plot(object, --x1, y1, reverse, color);
278       } else {
279         if (D < c) {
280           plot(object, ++x, y, reverse, color);  /* pattern 2  */
281           if (pixels_left > 1)
282             plot(object, ++x, y += step, reverse, color);
283           if (pixels_left > 2)
284             plot(object, --x1, y1, reverse, color);
285         } else {
286           /* pattern 3 */
287           plot(object, ++x, y += step, reverse, color);
288           if (pixels_left > 1)
289             plot(object, ++x, y, reverse, color);
290           if (pixels_left > 2)
291             plot(object, --x1, y1 -= step, reverse, color);
292         }
293       }
294     }      /* end if pixels_left */
295   }
296   /* end slope < 1/2 */
297   else {        /* slope greater than 1/2 */
298     c = 2 * (dy - dx);
299     incr1 = 2 * c;
300     D = incr1 + dx;
301     for (i = 0; i < xend; i++) {
302       ++x;
303       --x1;
304       if (D > 0) {
305         /* pattern 4 forwards */
306         plot(object, x, y += step, reverse, color);
307         plot(object, ++x, y += step, reverse, color);
308         /* pattern 4 backwards */
309         plot(object, x1, y1 -= step, reverse, color);
310         plot(object, --x1, y1 -= step, reverse, color);
311         D += incr1;
312       } else {
313         if (D < c) {
314           /* pattern 2 forwards */
315           plot(object, x, y, reverse, color);
316           plot(object, ++x, y += step, reverse,
317               color);
318 
319           /* pattern 2 backwards */
320           plot(object, x1, y1, reverse, color);
321           plot(object, --x1, y1 -= step, reverse,
322               color);
323         } else {
324           /* pattern 3 forwards */
325           plot(object, x, y += step, reverse, color);
326           plot(object, ++x, y, reverse, color);
327           /* pattern 3 backwards */
328           plot(object, x1, y1 -= step, reverse, color);
329           plot(object, --x1, y1, reverse, color);
330         }
331         D += incr2;
332       }
333     }      /* end for */
334     /* plot last pattern */
335     if (pixels_left) {
336       if (D > 0) {
337         plot(object, ++x, y += step, reverse, color);  /* pattern 4 */
338         if (pixels_left > 1)
339           plot(object, ++x, y += step, reverse,
340               color);
341         if (pixels_left > 2)
342           plot(object, --x1, y1 -= step, reverse,
343               color);
344       } else {
345         if (D < c) {
346           plot(object, ++x, y, reverse, color);  /* pattern 2  */
347           if (pixels_left > 1)
348             plot(object, ++x, y += step, reverse, color);
349           if (pixels_left > 2)
350             plot(object, --x1, y1, reverse, color);
351         } else {
352           /* pattern 3 */
353           plot(object, ++x, y += step, reverse, color);
354           if (pixels_left > 1)
355             plot(object, ++x, y, reverse, color);
356           if (pixels_left > 2) {
357             if (D > c)  /* step 3 */
358               plot(object, --x1, y1 -= step, reverse, color);
359             else  /* step 2 */
360               plot(object, --x1, y1, reverse, color);
361           }
362         }
363       }
364     }
365   }
366 }
367 
368 void
VGLBox(VGLBitmap * object,int x1,int y1,int x2,int y2,u_long color)369 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
370 {
371   VGLLine(object, x1, y1, x2, y1, color);
372   VGLLine(object, x2, y1, x2, y2, color);
373   VGLLine(object, x2, y2, x1, y2, color);
374   VGLLine(object, x1, y2, x1, y1, color);
375 }
376 
377 void
VGLFilledBox(VGLBitmap * object,int x1,int y1,int x2,int y2,u_long color)378 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
379 {
380   int y;
381 
382   for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
383 }
384 
385 static inline void
set4pixels(VGLBitmap * object,int x,int y,int xc,int yc,u_long color)386 set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
387 {
388   if (x!=0) {
389     VGLSetXY(object, xc+x, yc+y, color);
390     VGLSetXY(object, xc-x, yc+y, color);
391     if (y!=0) {
392       VGLSetXY(object, xc+x, yc-y, color);
393       VGLSetXY(object, xc-x, yc-y, color);
394     }
395   }
396   else {
397     VGLSetXY(object, xc, yc+y, color);
398     if (y!=0)
399       VGLSetXY(object, xc, yc-y, color);
400   }
401 }
402 
403 void
VGLEllipse(VGLBitmap * object,int xc,int yc,int a,int b,u_long color)404 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
405 {
406   int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
407   int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
408 
409   while (dx<dy) {
410     set4pixels(object, x, y, xc, yc, color);
411     if (d>0) {
412       y--; dy-=asq2; d-=dy;
413     }
414     x++; dx+=bsq2; d+=bsq+dx;
415   }
416   d+=(3*(asq-bsq)/2-(dx+dy))/2;
417   while (y>=0) {
418     set4pixels(object, x, y, xc, yc, color);
419     if (d<0) {
420       x++; dx+=bsq2; d+=dx;
421     }
422     y--; dy-=asq2; d+=asq-dy;
423   }
424 }
425 
426 static inline void
set2lines(VGLBitmap * object,int x,int y,int xc,int yc,u_long color)427 set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
428 {
429   if (x!=0) {
430     VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
431     if (y!=0)
432       VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
433   }
434   else {
435     VGLLine(object, xc, yc+y, xc, yc-y, color);
436   }
437 }
438 
439 void
VGLFilledEllipse(VGLBitmap * object,int xc,int yc,int a,int b,u_long color)440 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
441 {
442   int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
443   int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
444 
445   while (dx<dy) {
446     set2lines(object, x, y, xc, yc, color);
447     if (d>0) {
448       y--; dy-=asq2; d-=dy;
449     }
450     x++; dx+=bsq2; d+=bsq+dx;
451   }
452   d+=(3*(asq-bsq)/2-(dx+dy))/2;
453   while (y>=0) {
454     set2lines(object, x, y, xc, yc, color);
455     if (d<0) {
456       x++; dx+=bsq2; d+=dx;
457     }
458     y--; dy-=asq2; d+=asq-dy;
459   }
460 }
461 
462 void
VGLClear(VGLBitmap * object,u_long color)463 VGLClear(VGLBitmap *object, u_long color)
464 {
465   VGLBitmap src;
466   int i, len, mousemode, offset;
467 
468   VGLCheckSwitch();
469   if (object == VGLDisplay) {
470     VGLMouseFreeze();
471     VGLClear(&VGLVDisplay, color);
472   } else if (object->Type != MEMBUF)
473     return;		/* invalid */
474   switch (object->Type) {
475   case MEMBUF:
476   case VIDBUF8:
477   case VIDBUF8S:
478   case VIDBUF16:
479   case VIDBUF16S:
480   case VIDBUF24:
481   case VIDBUF24S:
482   case VIDBUF32:
483   case VIDBUF32S:
484     src.Type = MEMBUF;
485     src.Xsize = object->Xsize;
486     src.VXsize = object->VXsize;
487     src.Ysize = 1;
488     src.VYsize = 1;
489     src.Xorigin = 0;
490     src.Yorigin = 0;
491     src.Bitmap = alloca(object->VXsize * object->PixelBytes);
492     src.PixelBytes = object->PixelBytes;
493     color = htole32(color);
494     for (i = 0; i < object->VXsize; i++)
495       bcopy(&color, src.Bitmap + i * object->PixelBytes, object->PixelBytes);
496     for (i = 0; i < object->VYsize; i++)
497       __VGLBitmapCopy(&src, 0, 0, object, 0, i, object->VXsize, -1);
498     break;
499 
500   case VIDBUF8X:
501     mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
502     /* XXX works only for Xsize % 4 = 0 */
503     outb(0x3c6, 0xff);
504     outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
505     memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
506     __VGLMouseMode(mousemode);
507     break;
508 
509   case VIDBUF4:
510   case VIDBUF4S:
511     mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
512     /* XXX works only for Xsize % 8 = 0 */
513     outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
514     outb(0x3ce, 0x05); outb(0x3cf, 0x02);		/* mode 2 */
515     outb(0x3ce, 0x01); outb(0x3cf, 0x00);		/* set/reset enable */
516     outb(0x3ce, 0x08); outb(0x3cf, 0xff);		/* bit mask */
517     for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
518       VGLSetSegment(offset);
519       len = min(object->VXsize*object->VYsize - offset,
520 		VGLAdpInfo.va_window_size);
521       memset(object->Bitmap, (byte)color, len);
522       offset += len;
523     }
524     outb(0x3ce, 0x05); outb(0x3cf, 0x00);
525     __VGLMouseMode(mousemode);
526     break;
527   }
528   if (object == VGLDisplay)
529     VGLMouseUnFreeze();
530 }
531 
532 static inline u_long
VGLrgbToNative(uint16_t r,uint16_t g,uint16_t b)533 VGLrgbToNative(uint16_t r, uint16_t g, uint16_t b)
534 {
535   int nr, ng, nb;
536 
537   nr = VGLModeInfo.vi_pixel_fsizes[2];
538   ng = VGLModeInfo.vi_pixel_fsizes[1];
539   nb = VGLModeInfo.vi_pixel_fsizes[0];
540   return (r >> (16 - nr) << (ng + nb)) | (g >> (16 - ng) << nb) |
541 	 (b >> (16 - nb) << 0);
542 }
543 
544 u_long
VGLrgb332ToNative(byte c)545 VGLrgb332ToNative(byte c)
546 {
547   uint16_t r, g, b;
548 
549   /* 3:3:2 to 16:16:16 */
550   r = ((c & 0xe0) >> 5) * 0xffff / 7;
551   g = ((c & 0x1c) >> 2) * 0xffff / 7;
552   b = ((c & 0x03) >> 0) * 0xffff / 3;
553 
554   return VGLrgbToNative(r, g, b);
555 }
556 
557 void
VGLRestorePalette()558 VGLRestorePalette()
559 {
560   int i;
561 
562   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
563     return;
564   outb(0x3C6, 0xFF);
565   inb(0x3DA);
566   outb(0x3C8, 0x00);
567   for (i=0; i<256; i++) {
568     outb(0x3C9, VGLSavePaletteRed[i]);
569     inb(0x84);
570     outb(0x3C9, VGLSavePaletteGreen[i]);
571     inb(0x84);
572     outb(0x3C9, VGLSavePaletteBlue[i]);
573     inb(0x84);
574   }
575   inb(0x3DA);
576   outb(0x3C0, 0x20);
577 }
578 
579 void
VGLSavePalette()580 VGLSavePalette()
581 {
582   int i;
583 
584   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
585     return;
586   outb(0x3C6, 0xFF);
587   inb(0x3DA);
588   outb(0x3C7, 0x00);
589   for (i=0; i<256; i++) {
590     VGLSavePaletteRed[i] = inb(0x3C9);
591     inb(0x84);
592     VGLSavePaletteGreen[i] = inb(0x3C9);
593     inb(0x84);
594     VGLSavePaletteBlue[i] = inb(0x3C9);
595     inb(0x84);
596   }
597   inb(0x3DA);
598   outb(0x3C0, 0x20);
599 }
600 
601 void
VGLSetPalette(byte * red,byte * green,byte * blue)602 VGLSetPalette(byte *red, byte *green, byte *blue)
603 {
604   int i;
605 
606   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
607     return;
608   for (i=0; i<256; i++) {
609     VGLSavePaletteRed[i] = red[i];
610     VGLSavePaletteGreen[i] = green[i];
611     VGLSavePaletteBlue[i] = blue[i];
612   }
613   VGLCheckSwitch();
614   outb(0x3C6, 0xFF);
615   inb(0x3DA);
616   outb(0x3C8, 0x00);
617   for (i=0; i<256; i++) {
618     outb(0x3C9, VGLSavePaletteRed[i]);
619     inb(0x84);
620     outb(0x3C9, VGLSavePaletteGreen[i]);
621     inb(0x84);
622     outb(0x3C9, VGLSavePaletteBlue[i]);
623     inb(0x84);
624   }
625   inb(0x3DA);
626   outb(0x3C0, 0x20);
627 }
628 
629 void
VGLSetPaletteIndex(byte color,byte red,byte green,byte blue)630 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
631 {
632   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
633     return;
634   VGLSavePaletteRed[color] = red;
635   VGLSavePaletteGreen[color] = green;
636   VGLSavePaletteBlue[color] = blue;
637   VGLCheckSwitch();
638   outb(0x3C6, 0xFF);
639   inb(0x3DA);
640   outb(0x3C8, color);
641   outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
642   inb(0x3DA);
643   outb(0x3C0, 0x20);
644 }
645 
646 void
VGLRestoreBorder(void)647 VGLRestoreBorder(void)
648 {
649   VGLSetBorder(VGLBorderColor);
650 }
651 
652 void
VGLSetBorder(byte color)653 VGLSetBorder(byte color)
654 {
655   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
656     return;
657   VGLCheckSwitch();
658   inb(0x3DA);
659   outb(0x3C0,0x11); outb(0x3C0, color);
660   inb(0x3DA);
661   outb(0x3C0, 0x20);
662   VGLBorderColor = color;
663   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
664     ioctl(0, KDDISABIO, 0);
665 }
666 
667 void
VGLRestoreBlank(void)668 VGLRestoreBlank(void)
669 {
670   VGLBlankDisplay(VGLBlank);
671 }
672 
673 void
VGLBlankDisplay(int blank)674 VGLBlankDisplay(int blank)
675 {
676   byte val;
677 
678   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0))
679     return;
680   VGLCheckSwitch();
681   outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
682   outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
683   VGLBlank = blank;
684   if (VGLModeInfo.vi_mem_model == V_INFO_MM_DIRECT)
685     ioctl(0, KDDISABIO, 0);
686 }
687