2 * Copyright (c) 1991-1997 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 without specific prior written permission
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
36 static byte VGLSavePaletteRed[256];
37 static byte VGLSavePaletteGreen[256];
38 static byte VGLSavePaletteBlue[256];
40 #define ABS(a) (((a)<0) ? -(a) : (a))
41 #define SGN(a) (((a)<0) ? -1 : 1)
42 #define min(x, y) (((x) < (y)) ? (x) : (y))
43 #define max(x, y) (((x) > (y)) ? (x) : (y))
46 color2mem(u_long color, byte *b, int len)
50 b[3] = (color >> 24) & 0xff;
53 b[2] = (color >> 16) & 0xff;
56 b[1] = (color >> 8) & 0xff;
68 mem2color(byte *b, int len)
74 color |= (b[3] & 0xff) << 24;
77 color |= (b[2] & 0xff) << 16;
80 color |= (b[1] & 0xff) << 8;
84 color |= (b[0] & 0xff);
92 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
98 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
99 if (!VGLMouseFreeze(x, y, 1, 1, color)) {
100 switch (object->Type) {
103 object->Bitmap[y*object->VXsize+x]=((byte)color);
106 object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=((byte)color);
111 color2mem(color, b, object->PixelBytes);
112 bcopy(b, &object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
118 color2mem(color, b, object->PixelBytes);
119 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
120 bcopy(b, &object->Bitmap[offset], object->PixelBytes);
124 outb(0x3c5, 0x01 << (x&0x3));
125 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
128 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
131 offset = y*VGLAdpInfo.va_line_width + x/8;
133 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
134 outb(0x3ce, 0x00); outb(0x3cf, (byte)color & 0x0f); /* set/reset */
135 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
136 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */
137 object->Bitmap[offset] |= (byte)color;
145 VGLGetXY(VGLBitmap *object, int x, int y)
156 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
158 switch (object->Type) {
161 return object->Bitmap[((y*object->VXsize)+x)];
163 return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
167 bcopy(&object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
168 b, object->PixelBytes);
169 return (mem2color(b, object->PixelBytes));
173 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
174 bcopy(&object->Bitmap[offset], b, object->PixelBytes);
176 return (mem2color(b, object->PixelBytes));
178 outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
179 return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
181 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
184 offset = y*VGLAdpInfo.va_line_width + x/8;
187 return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0; /* XXX */
190 mask = 0x80 >> (x%8);
191 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
192 outb(0x3ce, 0x04); outb(0x3cf, i);
193 color |= (object->Bitmap[offset] & mask) ? (1 << i) : 0;
198 return 0; /* XXX black? */
202 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
204 int d, x, y, ax, ay, sx, sy, dx, dy;
206 dx = x2-x1; ax = ABS(dx)<<1; sx = SGN(dx); x = x1;
207 dy = y2-y1; ay = ABS(dy)<<1; sy = SGN(dy); y = y1;
209 if (ax>ay) { /* x dominant */
212 VGLSetXY(object, x, y, color);
221 else { /* y dominant */
224 VGLSetXY(object, x, y, color);
236 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
238 VGLLine(object, x1, y1, x2, y1, color);
239 VGLLine(object, x2, y1, x2, y2, color);
240 VGLLine(object, x2, y2, x1, y2, color);
241 VGLLine(object, x1, y2, x1, y1, color);
245 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
249 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
253 inline set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
256 VGLSetXY(object, xc+x, yc+y, color);
257 VGLSetXY(object, xc-x, yc+y, color);
259 VGLSetXY(object, xc+x, yc-y, color);
260 VGLSetXY(object, xc-x, yc-y, color);
264 VGLSetXY(object, xc, yc+y, color);
266 VGLSetXY(object, xc, yc-y, color);
271 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
273 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
274 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
277 set4pixels(object, x, y, xc, yc, color);
279 y--; dy-=asq2; d-=dy;
281 x++; dx+=bsq2; d+=bsq+dx;
283 d+=(3*(asq-bsq)/2-(dx+dy))/2;
285 set4pixels(object, x, y, xc, yc, color);
287 x++; dx+=bsq2; d+=dx;
289 y--; dy-=asq2; d+=asq-dy;
294 inline set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
297 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
299 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
302 VGLLine(object, xc, yc+y, xc, yc-y, color);
307 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
309 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
310 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
313 set2lines(object, x, y, xc, yc, color);
315 y--; dy-=asq2; d-=dy;
317 x++; dx+=bsq2; d+=bsq+dx;
319 d+=(3*(asq-bsq)/2-(dx+dy))/2;
321 set2lines(object, x, y, xc, yc, color);
323 x++; dx+=bsq2; d+=dx;
325 y--; dy-=asq2; d+=asq-dy;
330 VGLClear(VGLBitmap *object, u_long color)
338 VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color); /* XXX */
339 switch (object->Type) {
342 memset(object->Bitmap, (byte)color, object->VXsize*object->VYsize);
346 for (offset = 0; offset < object->VXsize*object->VYsize; ) {
347 VGLSetSegment(offset);
348 len = min(object->VXsize*object->VYsize - offset,
349 VGLAdpInfo.va_window_size);
350 memset(object->Bitmap, (byte)color, len);
357 color2mem(color, b, object->PixelBytes);
358 total = object->VXsize*object->VYsize*object->PixelBytes;
359 for (i = 0; i < total; i += object->PixelBytes)
360 bcopy(b, object->Bitmap + i, object->PixelBytes);
366 color2mem(color, b, object->PixelBytes);
367 total = object->VXsize*object->VYsize*object->PixelBytes;
368 for (offset = 0; offset < total; ) {
369 VGLSetSegment(offset);
370 len = min(total - offset, VGLAdpInfo.va_window_size);
371 for (i = 0; i < len; i += object->PixelBytes)
372 bcopy(b, object->Bitmap + offset + i, object->PixelBytes);
378 /* XXX works only for Xsize % 4 = 0 */
380 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
381 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
386 /* XXX works only for Xsize % 8 = 0 */
387 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
388 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
389 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
390 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
391 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
392 VGLSetSegment(offset);
393 len = min(object->VXsize*object->VYsize - offset,
394 VGLAdpInfo.va_window_size);
395 memset(object->Bitmap, (byte)color, len);
398 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
412 for (i=0; i<256; i++) {
413 outb(0x3C9, VGLSavePaletteRed[i]);
415 outb(0x3C9, VGLSavePaletteGreen[i]);
417 outb(0x3C9, VGLSavePaletteBlue[i]);
432 for (i=0; i<256; i++) {
433 VGLSavePaletteRed[i] = inb(0x3C9);
435 VGLSavePaletteGreen[i] = inb(0x3C9);
437 VGLSavePaletteBlue[i] = inb(0x3C9);
445 VGLSetPalette(byte *red, byte *green, byte *blue)
449 for (i=0; i<256; i++) {
450 VGLSavePaletteRed[i] = red[i];
451 VGLSavePaletteGreen[i] = green[i];
452 VGLSavePaletteBlue[i] = blue[i];
458 for (i=0; i<256; i++) {
459 outb(0x3C9, VGLSavePaletteRed[i]);
461 outb(0x3C9, VGLSavePaletteGreen[i]);
463 outb(0x3C9, VGLSavePaletteBlue[i]);
471 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
473 VGLSavePaletteRed[color] = red;
474 VGLSavePaletteGreen[color] = green;
475 VGLSavePaletteBlue[color] = blue;
480 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
486 VGLSetBorder(byte color)
490 outb(0x3C0,0x11); outb(0x3C0, color);
496 VGLBlankDisplay(int blank)
501 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
502 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));