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 withough 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.
35 static byte VGLSavePaletteRed[256];
36 static byte VGLSavePaletteGreen[256];
37 static byte VGLSavePaletteBlue[256];
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))
45 color2mem(u_long color, byte *b, int len)
49 b[3] = (color >> 24) & 0xff;
52 b[2] = (color >> 16) & 0xff;
55 b[1] = (color >> 8) & 0xff;
67 mem2color(byte *b, int len)
73 color |= (b[3] & 0xff) << 24;
76 color |= (b[2] & 0xff) << 16;
79 color |= (b[1] & 0xff) << 8;
83 color |= (b[0] & 0xff);
91 VGLSetXY(VGLBitmap *object, int x, int y, u_long color)
97 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
98 if (!VGLMouseFreeze(x, y, 1, 1, color)) {
99 switch (object->Type) {
102 object->Bitmap[y*object->VXsize+x]=((byte)color);
105 object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=((byte)color);
110 color2mem(color, b, object->PixelBytes);
111 bcopy(b, &object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
117 color2mem(color, b, object->PixelBytes);
118 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
119 bcopy(b, &object->Bitmap[offset], object->PixelBytes);
123 outb(0x3c5, 0x01 << (x&0x3));
124 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = ((byte)color);
127 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
130 offset = y*VGLAdpInfo.va_line_width + x/8;
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;
144 VGLGetXY(VGLBitmap *object, int x, int y)
155 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
157 switch (object->Type) {
160 return object->Bitmap[((y*object->VXsize)+x)];
162 return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
166 bcopy(&object->Bitmap[(y*object->VXsize+x) * object->PixelBytes],
167 b, object->PixelBytes);
168 return (mem2color(b, object->PixelBytes));
172 offset = VGLSetSegment((y*object->VXsize+x) * object->PixelBytes);
173 bcopy(&object->Bitmap[offset], b, object->PixelBytes);
175 return (mem2color(b, object->PixelBytes));
177 outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
178 return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
180 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
183 offset = y*VGLAdpInfo.va_line_width + x/8;
186 return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0; /* XXX */
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;
197 return 0; /* XXX black? */
201 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
203 int d, x, y, ax, ay, sx, sy, dx, dy;
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;
208 if (ax>ay) { /* x dominant */
211 VGLSetXY(object, x, y, color);
220 else { /* y dominant */
223 VGLSetXY(object, x, y, color);
235 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
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);
244 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, u_long color)
248 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
252 inline set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
255 VGLSetXY(object, xc+x, yc+y, color);
256 VGLSetXY(object, xc-x, yc+y, color);
258 VGLSetXY(object, xc+x, yc-y, color);
259 VGLSetXY(object, xc-x, yc-y, color);
263 VGLSetXY(object, xc, yc+y, color);
265 VGLSetXY(object, xc, yc-y, color);
270 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
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;
276 set4pixels(object, x, y, xc, yc, color);
278 y--; dy-=asq2; d-=dy;
280 x++; dx+=bsq2; d+=bsq+dx;
282 d+=(3*(asq-bsq)/2-(dx+dy))/2;
284 set4pixels(object, x, y, xc, yc, color);
286 x++; dx+=bsq2; d+=dx;
288 y--; dy-=asq2; d+=asq-dy;
293 inline set2lines(VGLBitmap *object, int x, int y, int xc, int yc, u_long color)
296 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
298 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
301 VGLLine(object, xc, yc+y, xc, yc-y, color);
306 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, u_long color)
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;
312 set2lines(object, x, y, xc, yc, color);
314 y--; dy-=asq2; d-=dy;
316 x++; dx+=bsq2; d+=bsq+dx;
318 d+=(3*(asq-bsq)/2-(dx+dy))/2;
320 set2lines(object, x, y, xc, yc, color);
322 x++; dx+=bsq2; d+=dx;
324 y--; dy-=asq2; d+=asq-dy;
329 VGLClear(VGLBitmap *object, u_long color)
337 VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color); /* XXX */
338 switch (object->Type) {
341 memset(object->Bitmap, (byte)color, object->VXsize*object->VYsize);
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);
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);
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);
377 /* XXX works only for Xsize % 4 = 0 */
379 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
380 memset(object->Bitmap, (byte)color, VGLAdpInfo.va_line_width*object->VYsize);
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);
397 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
411 for (i=0; i<256; i++) {
412 outb(0x3C9, VGLSavePaletteRed[i]);
414 outb(0x3C9, VGLSavePaletteGreen[i]);
416 outb(0x3C9, VGLSavePaletteBlue[i]);
431 for (i=0; i<256; i++) {
432 VGLSavePaletteRed[i] = inb(0x3C9);
434 VGLSavePaletteGreen[i] = inb(0x3C9);
436 VGLSavePaletteBlue[i] = inb(0x3C9);
444 VGLSetPalette(byte *red, byte *green, byte *blue)
448 for (i=0; i<256; i++) {
449 VGLSavePaletteRed[i] = red[i];
450 VGLSavePaletteGreen[i] = green[i];
451 VGLSavePaletteBlue[i] = blue[i];
457 for (i=0; i<256; i++) {
458 outb(0x3C9, VGLSavePaletteRed[i]);
460 outb(0x3C9, VGLSavePaletteGreen[i]);
462 outb(0x3C9, VGLSavePaletteBlue[i]);
470 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
472 VGLSavePaletteRed[color] = red;
473 VGLSavePaletteGreen[color] = green;
474 VGLSavePaletteBlue[color] = blue;
479 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
485 VGLSetBorder(byte color)
489 outb(0x3C0,0x11); outb(0x3C0, color);
495 VGLBlankDisplay(int blank)
500 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
501 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));