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.
28 * $FreeBSD: src/lib/libvgl/simple.c,v 1.4 1999/11/08 11:37:39 yokota Exp $
29 * $DragonFly: src/lib/libvgl/simple.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
33 #include <machine/console.h>
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 VGLSetXY(VGLBitmap *object, int x, int y, byte color)
51 if (x>=0 && x<object->VXsize && y>=0 && y<object->VYsize) {
52 if (!VGLMouseFreeze(x, y, 1, 1, color)) {
53 switch (object->Type) {
56 object->Bitmap[y*object->VXsize+x]=(color);
59 object->Bitmap[VGLSetSegment(y*object->VXsize+x)]=(color);
63 outb(0x3c5, 0x01 << (x&0x3));
64 object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)] = (color);
67 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
70 offset = y*VGLAdpInfo.va_line_width + x/8;
72 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
73 outb(0x3ce, 0x00); outb(0x3cf, color & 0x0f); /* set/reset */
74 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
75 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x%8)); /* bit mask */
76 object->Bitmap[offset] |= color;
84 VGLGetXY(VGLBitmap *object, int x, int y)
94 if (x<0 || x>=object->VXsize || y<0 || y>=object->VYsize)
96 switch (object->Type) {
99 return object->Bitmap[((y*object->VXsize)+x)];
101 return object->Bitmap[VGLSetSegment(y*object->VXsize+x)];
103 outb(0x3ce, 0x04); outb(0x3cf, x & 0x3);
104 return object->Bitmap[(unsigned)(VGLAdpInfo.va_line_width*y)+(x/4)];
106 offset = VGLSetSegment(y*VGLAdpInfo.va_line_width + x/8);
109 offset = y*VGLAdpInfo.va_line_width + x/8;
112 return (object->Bitmap[offset]&(0x80>>(x%8))) ? 1 : 0; /* XXX */
115 mask = 0x80 >> (x%8);
116 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
117 outb(0x3ce, 0x04); outb(0x3cf, i);
118 color |= (object->Bitmap[offset] & mask) ? (1 << i) : 0;
127 VGLLine(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
129 int d, x, y, ax, ay, sx, sy, dx, dy;
131 dx = x2-x1; ax = ABS(dx)<<1; sx = SGN(dx); x = x1;
132 dy = y2-y1; ay = ABS(dy)<<1; sy = SGN(dy); y = y1;
134 if (ax>ay) { /* x dominant */
137 VGLSetXY(object, x, y, color);
146 else { /* y dominant */
149 VGLSetXY(object, x, y, color);
161 VGLBox(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
163 VGLLine(object, x1, y1, x2, y1, color);
164 VGLLine(object, x2, y1, x2, y2, color);
165 VGLLine(object, x2, y2, x1, y2, color);
166 VGLLine(object, x1, y2, x1, y1, color);
170 VGLFilledBox(VGLBitmap *object, int x1, int y1, int x2, int y2, byte color)
174 for (y=y1; y<=y2; y++) VGLLine(object, x1, y, x2, y, color);
178 inline set4pixels(VGLBitmap *object, int x, int y, int xc, int yc, byte color)
181 VGLSetXY(object, xc+x, yc+y, color);
182 VGLSetXY(object, xc-x, yc+y, color);
184 VGLSetXY(object, xc+x, yc-y, color);
185 VGLSetXY(object, xc-x, yc-y, color);
189 VGLSetXY(object, xc, yc+y, color);
191 VGLSetXY(object, xc, yc-y, color);
196 VGLEllipse(VGLBitmap *object, int xc, int yc, int a, int b, byte color)
198 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
199 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
202 set4pixels(object, x, y, xc, yc, color);
204 y--; dy-=asq2; d-=dy;
206 x++; dx+=bsq2; d+=bsq+dx;
208 d+=(3*(asq-bsq)/2-(dx+dy))/2;
210 set4pixels(object, x, y, xc, yc, color);
212 x++; dx+=bsq2; d+=dx;
214 y--; dy-=asq2; d+=asq-dy;
219 inline set2lines(VGLBitmap *object, int x, int y, int xc, int yc, byte color)
222 VGLLine(object, xc+x, yc+y, xc-x, yc+y, color);
224 VGLLine(object, xc+x, yc-y, xc-x, yc-y, color);
227 VGLLine(object, xc, yc+y, xc, yc-y, color);
232 VGLFilledEllipse(VGLBitmap *object, int xc, int yc, int a, int b, byte color)
234 int x = 0, y = b, asq = a*a, asq2 = a*a*2, bsq = b*b;
235 int bsq2 = b*b*2, d = bsq-asq*b+asq/4, dx = 0, dy = asq2*b;
238 set2lines(object, x, y, xc, yc, color);
240 y--; dy-=asq2; d-=dy;
242 x++; dx+=bsq2; d+=bsq+dx;
244 d+=(3*(asq-bsq)/2-(dx+dy))/2;
246 set2lines(object, x, y, xc, yc, color);
248 x++; dx+=bsq2; d+=dx;
250 y--; dy-=asq2; d+=asq-dy;
255 VGLClear(VGLBitmap *object, byte color)
261 VGLMouseFreeze(0, 0, object->Xsize, object->Ysize, color);
262 switch (object->Type) {
265 memset(object->Bitmap, color, object->VXsize*object->VYsize);
269 for (offset = 0; offset < object->VXsize*object->VYsize; ) {
270 VGLSetSegment(offset);
271 len = min(object->VXsize*object->VYsize - offset,
272 VGLAdpInfo.va_window_size);
273 memset(object->Bitmap, color, len);
279 /* XXX works only for Xsize % 4 = 0 */
281 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
282 memset(object->Bitmap, color, VGLAdpInfo.va_line_width*object->VYsize);
287 /* XXX works only for Xsize % 8 = 0 */
288 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
289 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
290 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
291 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
292 for (offset = 0; offset < VGLAdpInfo.va_line_width*object->VYsize; ) {
293 VGLSetSegment(offset);
294 len = min(object->VXsize*object->VYsize - offset,
295 VGLAdpInfo.va_window_size);
296 memset(object->Bitmap, color, len);
299 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
313 for (i=0; i<256; i++) {
314 outb(0x3C9, VGLSavePaletteRed[i]);
316 outb(0x3C9, VGLSavePaletteGreen[i]);
318 outb(0x3C9, VGLSavePaletteBlue[i]);
333 for (i=0; i<256; i++) {
334 VGLSavePaletteRed[i] = inb(0x3C9);
336 VGLSavePaletteGreen[i] = inb(0x3C9);
338 VGLSavePaletteBlue[i] = inb(0x3C9);
346 VGLSetPalette(byte *red, byte *green, byte *blue)
350 for (i=0; i<256; i++) {
351 VGLSavePaletteRed[i] = red[i];
352 VGLSavePaletteGreen[i] = green[i];
353 VGLSavePaletteBlue[i] = blue[i];
359 for (i=0; i<256; i++) {
360 outb(0x3C9, VGLSavePaletteRed[i]);
362 outb(0x3C9, VGLSavePaletteGreen[i]);
364 outb(0x3C9, VGLSavePaletteBlue[i]);
372 VGLSetPaletteIndex(byte color, byte red, byte green, byte blue)
374 VGLSavePaletteRed[color] = red;
375 VGLSavePaletteGreen[color] = green;
376 VGLSavePaletteBlue[color] = blue;
381 outb(0x3C9, red); outb(0x3C9, green); outb(0x3C9, blue);
387 VGLSetBorder(byte color)
391 outb(0x3C0,0x11); outb(0x3C0, color);
397 VGLBlankDisplay(int blank)
402 outb(0x3C4, 0x01); val = inb(0x3C5); outb(0x3C4, 0x01);
403 outb(0x3C5, ((blank) ? (val |= 0x20) : (val &= 0xDF)));