]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libvgl/simple.c
This commit was generated by cvs2svn to compensate for changes in r83174,
[FreeBSD/FreeBSD.git] / lib / libvgl / simple.c
1 /*-
2  * Copyright (c) 1991-1997 Søren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
16  *
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.
27  *
28  * $FreeBSD$
29  */
30
31 #include <signal.h>
32 #include <sys/fbio.h>
33 #include "vgl.h"
34
35 static byte VGLSavePaletteRed[256];
36 static byte VGLSavePaletteGreen[256];
37 static byte VGLSavePaletteBlue[256];
38
39 #define ABS(a)          (((a)<0) ? -(a) : (a))
40 #define SGN(a)          (((a)<0) ? -1 : 1)
41 #define min(x, y)       (((x) < (y)) ? (x) : (y))
42 #define max(x, y)       (((x) > (y)) ? (x) : (y))
43
44 static void
45 color2mem(u_long color, byte *b, int len)
46 {
47   switch (len) {
48   case 4:
49     b[3] = (color >> 24) & 0xff;
50     /* fallthrough */
51   case 3:
52     b[2] = (color >> 16) & 0xff;
53     /* fallthrough */
54   case 2:
55     b[1] = (color >> 8) & 0xff;
56     /* fallthrough */
57   case 1:
58   default:
59     b[0] = color & 0xff;
60     break;
61   }
62
63   return;
64 }
65
66 static u_long
67 mem2color(byte *b, int len)
68 {
69   u_long color = 0;
70
71   switch (len) {
72   case 4:
73     color |= (b[3] & 0xff) << 24;
74     /* fallthrough */
75   case 3:
76     color |= (b[2] & 0xff) << 16;
77     /* fallthrough */
78   case 2:
79     color |= (b[1] & 0xff) << 8;
80     /* fallthrough */
81   case 1:
82   default:
83     color |= (b[0] & 0xff);
84     break;
85   }
86
87   return color;
88 }
89
90 void
91 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
92 {
93   int offset;
94   byte b[4];
95
96   VGLCheckSwitch();
97   if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
98     if (!VGLMouseFreeze(x, y, 1, 1, color)) {
99       switch (object->Type) {
100       case MEMBUF:
101       case VIDBUF8:
102         object->Bitmap[y*object->VXsize+x]=((byte)color);
103         break;
104       case VIDBUF8S:
105         object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=((byte)color);
106         break;
107       case VIDBUF16:
108       case VIDBUF24:
109       case VIDBUF32:
110         color2mem(color, b, object->PixelBytes);
111         bcopy(b, &object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
112                 object->PixelBytes);
113         break;
114       case VIDBUF16S:
115       case VIDBUF24S:
116       case VIDBUF32S:
117         color2mem(color, b, object->PixelBytes);
118         offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
119         bcopy(b, &object->Bitmap[offset], object->PixelBytes);
120         break;
121       case VIDBUF8X:
122         outb(0x3c4, 0x02);
123         outb(0x3c5, 0x01 << (x&0x3));
124         object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
125         break;
126       case VIDBUF4S:
127         offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
128         goto set_planar;
129       case VIDBUF4:
130         offset = y*VGLAdpInfo.va_line_width + x/8;
131 set_planar:
132         outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
133         outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f);     /* set/reset */
134         outb(0x3ce, 0x01); outb(0x3cf, 0x0f);           /* set/reset enable */
135         outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8));  /* bit mask */
136         object->Bitmap[offset] |= (byte)color;
137       }
138     }
139     VGLMouseUnFreeze();
140   }
141 }
142
143 u_long
144 VGLGetXY(VGLBitmap *object, int x, int y)
145 {
146   int offset;
147   byte b[4];
148 #if 0
149   int i;
150   u_long color;
151   byte mask;
152 #endif
153
154   VGLCheckSwitch();
155   if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
156     return 0;
157   switch (object->Type) {
158     case MEMBUF:
159     case VIDBUF8:
160       return object->Bitmap[((y*object->VXsize)+x)];
161     case VIDBUF8S:
162       return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
163     case VIDBUF16:
164     case VIDBUF24:
165     case VIDBUF32:
166       bcopy(&object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
167                 b, object->PixelBytes);
168       return (mem2color(b, object->PixelBytes));
169     case VIDBUF16S:
170     case VIDBUF24S:
171     case VIDBUF32S:
172         offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
173         bcopy(&object->Bitmap[offset], b, object->PixelBytes);
174
175       return (mem2color(b, object->PixelBytes));
176     case VIDBUF8X:
177       outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
178       return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
179     case VIDBUF4S:
180       offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
181       goto get_planar;
182     case VIDBUF4:
183       offset = y*VGLAdpInfo.va_line_width + x/8;
184 get_planar:
185 #if 1
186       return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0;    /* XXX */
187 #else
188       color = 0;
189       mask = 0x80 >> (x%8);
190       for (i = 0; i < VGLModeInfo.vi_planes; i++) {
191         outb(0x3ce, 0x04); outb(0x3cf, i);
192         color |= (object->Bitmap[offset] & mask) ? (1 << i) : 0;
193       }
194       return color;
195 #endif
196   }
197   return 0;             /* XXX black? */
198 }
199
200 void
201 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
202 {
203   int d, x, y, ax, ay, sx, sy, dx, dy;
204
205   dx = x2-x1; ax = ABS(dx)<<1; sx = SGN(dx); x = x1;
206   dy = y2-y1; ay = ABS(dy)<<1; sy = SGN(dy); y = y1;
207
208   if (ax>ay) {                                  /* x dominant */
209     d = ay-(ax>>1);
210     for (;;) {
211       VGLSetXY(object, x, y, color);
212       if (x==x2)
213         break;
214       if (d>=0) {
215         y += sy; d -= ax;
216       }
217       x += sx; d += ay;
218     }
219   }
220   else {                                        /* y dominant */
221     d = ax-(ay>>1);
222     for (;;) {
223       VGLSetXY(object, x, y, color);
224       if (y==y2) 
225         break;
226       if (d>=0) {
227         x += sx; d -= ay;
228       }
229       y += sy; d += ax;
230     }
231   }
232 }
233
234 void
235 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
236 {
237   VGLLine(object, x1, y1, x2, y1, color);
238   VGLLine(object, x2, y1, x2, y2, color);
239   VGLLine(object, x2, y2, x1, y2, color);
240   VGLLine(object, x1, y2, x1, y1, color);
241 }
242
243 void
244 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
245 {
246   int y;
247
248   for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
249 }
250
251 void
252 inline set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color) 
253 {
254   if (x!=0) { 
255     VGLSetXY(object, xc+x, yc+y, color); 
256     VGLSetXY(object, xc-x, yc+y, color); 
257     if (y!=0) { 
258       VGLSetXY(object, xc+x, yc-y, color); 
259       VGLSetXY(object, xc-x, yc-y, color); 
260     } 
261   } 
262   else { 
263     VGLSetXY(object, xc, yc+y, color); 
264     if (y!=0) 
265       VGLSetXY(object, xc, yc-y, color); 
266   } 
267 }
268
269 void
270 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
271 {
272   int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
273   int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
274
275   while (dx<dy) {
276     set4pixels(object, x, y, xc, yc, color);
277     if (d>0) {
278       y--; dy-=asq2; d-=dy;
279     }
280     x++; dx+=bsq2; d+=bsq+dx;
281   }
282   d+=(3*(asq-bsq)/2-(dx+dy))/2;
283   while (y>=0) {
284     set4pixels(object, x, y, xc, yc, color);
285     if (d<0) {
286       x++; dx+=bsq2; d+=dx;
287     }
288     y--; dy-=asq2; d+=asq-dy;
289   }
290 }
291
292 void
293 inline set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color) 
294 {
295   if (x!=0) { 
296     VGLLine(object, xc+x, yc+y, xc-x, yc+y, color); 
297     if (y!=0) 
298       VGLLine(object, xc+x, yc-y, xc-x, yc-y, color); 
299   } 
300   else { 
301     VGLLine(object, xc, yc+y, xc, yc-y, color); 
302   } 
303 }
304
305 void
306 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
307 {
308   int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
309   int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
310
311   while (dx<dy) {
312     set2lines(object, x, y, xc, yc, color);
313     if (d>0) {
314       y--; dy-=asq2; d-=dy;
315     }
316     x++; dx+=bsq2; d+=bsq+dx;
317   }
318   d+=(3*(asq-bsq)/2-(dx+dy))/2;
319   while (y>=0) {
320     set2lines(object, x, y, xc, yc, color);
321     if (d<0) {
322       x++; dx+=bsq2; d+=dx;
323     }
324     y--; dy-=asq2; d+=asq-dy;
325   }
326 }
327
328 void
329 VGLClear(VGLBitmap *object, u_long color)
330 {
331   int offset;
332   int len;
333   int i, total = 0;
334   byte b[4];
335
336   VGLCheckSwitch();
337   VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color); /* XXX */
338   switch (object->Type) {
339   case MEMBUF:
340   case VIDBUF8:
341     memset(object->Bitmap, (byte)color, object->VXsize*object->VYsize);
342     break;
343
344   case VIDBUF8S:
345     for (offset = 0; offset < object->VXsize*object->VYsize; ) {
346       VGLSetSegment(offset);
347       len = min(object->VXsize*object->VYsize - offset,
348                 VGLAdpInfo.va_window_size);
349       memset(object->Bitmap, (byte)color, len);
350       offset += len;
351     }
352     break;
353   case VIDBUF16:
354   case VIDBUF24:
355   case VIDBUF32:
356     color2mem(color, b, object->PixelBytes);
357     total = object->VXsize*object->VYsize*object->PixelBytes;
358     for (i = 0; i < total; i += object->PixelBytes)
359       bcopy(b, object->Bitmap + i, object->PixelBytes);
360     break;
361
362   case VIDBUF16S:
363   case VIDBUF24S:
364   case VIDBUF32S:
365     color2mem(color, b, object->PixelBytes);
366     total = object->VXsize*object->VYsize*object->PixelBytes;
367     for (offset = 0; offset < total; ) {
368       VGLSetSegment(offset);
369       len = min(total - offset, VGLAdpInfo.va_window_size);
370       for (i = 0; i < len; i += object->PixelBytes)
371         bcopy(b, object->Bitmap + offset + i, object->PixelBytes);
372       offset += len;
373     }
374     break;
375
376   case VIDBUF8X:
377     /* XXX works only for Xsize % 4 = 0 */
378     outb(0x3c6, 0xff);
379     outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
380     memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
381     break;
382
383   case VIDBUF4:
384   case VIDBUF4S:
385     /* XXX works only for Xsize % 8 = 0 */
386     outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
387     outb(0x3ce, 0x05); outb(0x3cf, 0x02);               /* mode 2 */
388     outb(0x3ce, 0x01); outb(0x3cf, 0x00);               /* set/reset enable */
389     outb(0x3ce, 0x08); outb(0x3cf, 0xff);               /* bit mask */
390     for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
391       VGLSetSegment(offset);
392       len = min(object->VXsize*object->VYsize - offset,
393                 VGLAdpInfo.va_window_size);
394       memset(object->Bitmap, (byte)color, len);
395       offset += len;
396     }
397     outb(0x3ce, 0x05); outb(0x3cf, 0x00);
398     break;
399   }
400   VGLMouseUnFreeze();
401 }
402
403 void
404 VGLRestorePalette()
405 {
406   int i;
407
408   outb(0x3C6, 0xFF);
409   inb(0x3DA); 
410   outb(0x3C8, 0x00);
411   for (i=0; i<256; i++) {
412     outb(0x3C9, VGLSavePaletteRed[i]);
413     inb(0x84);
414     outb(0x3C9, VGLSavePaletteGreen[i]);
415     inb(0x84);
416     outb(0x3C9, VGLSavePaletteBlue[i]);
417     inb(0x84);
418   }
419   inb(0x3DA);
420   outb(0x3C0, 0x20);
421 }
422
423 void
424 VGLSavePalette()
425 {
426   int i;
427
428   outb(0x3C6, 0xFF);
429   inb(0x3DA);
430   outb(0x3C7, 0x00);
431   for (i=0; i<256; i++) {
432     VGLSavePaletteRed[i] = inb(0x3C9);
433     inb(0x84);
434     VGLSavePaletteGreen[i] = inb(0x3C9);
435     inb(0x84);
436     VGLSavePaletteBlue[i] = inb(0x3C9);
437     inb(0x84);
438   }
439   inb(0x3DA);
440   outb(0x3C0, 0x20);
441 }
442
443 void
444 VGLSetPalette(byte *red, byte *green, byte *blue)
445 {
446   int i;
447   
448   for (i=0; i<256; i++) {
449     VGLSavePaletteRed[i] = red[i];
450     VGLSavePaletteGreen[i] = green[i];
451     VGLSavePaletteBlue[i] = blue[i];
452   }
453   VGLCheckSwitch();
454   outb(0x3C6, 0xFF);
455   inb(0x3DA); 
456   outb(0x3C8, 0x00);
457   for (i=0; i<256; i++) {
458     outb(0x3C9, VGLSavePaletteRed[i]);
459     inb(0x84);
460     outb(0x3C9, VGLSavePaletteGreen[i]);
461     inb(0x84);
462     outb(0x3C9, VGLSavePaletteBlue[i]);
463     inb(0x84);
464   }
465   inb(0x3DA);
466   outb(0x3C0, 0x20);
467 }
468
469 void
470 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
471 {
472   VGLSavePaletteRed[color] = red;
473   VGLSavePaletteGreen[color] = green;
474   VGLSavePaletteBlue[color] = blue;
475   VGLCheckSwitch();
476   outb(0x3C6, 0xFF);
477   inb(0x3DA);
478   outb(0x3C8, color); 
479   outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
480   inb(0x3DA);
481   outb(0x3C0, 0x20);
482 }
483
484 void
485 VGLSetBorder(byte color)
486 {
487   VGLCheckSwitch();
488   inb(0x3DA);
489   outb(0x3C0,0x11); outb(0x3C0, color); 
490   inb(0x3DA);
491   outb(0x3C0, 0x20);
492 }
493
494 void
495 VGLBlankDisplay(int blank)
496 {
497   byte val;
498
499   VGLCheckSwitch();
500   outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
501   outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));
502 }