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.
28 * $FreeBSD: src/lib/libvgl/simple.c,v 1.4 1999/11/08 11:37:39 yokota Exp $
32 #include <machine/console.h>
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 VGLSetXY(VGLBitmap
*object
, int x
, int y
, byte color
)
50 if (x
>=0 && x
<object
->VXsize
&& y
>=0 && y
<object
->VYsize
) {
51 if (!VGLMouseFreeze(x
, y
, 1, 1, color
)) {
52 switch (object
->Type
) {
55 object
->Bitmap
[y
*object
->VXsize
+x
]=(color
);
58 object
->Bitmap
[VGLSetSegment(y
*object
->VXsize
+x
)]=(color
);
62 outb(0x3c5, 0x01 << (x
&0x3));
63 object
->Bitmap
[(unsigned)(VGLAdpInfo
.va_line_width
*y
)+(x
/4)] = (color
);
66 offset
= VGLSetSegment(y
*VGLAdpInfo
.va_line_width
+ x
/8);
69 offset
= y
*VGLAdpInfo
.va_line_width
+ x
/8;
71 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
72 outb(0x3ce, 0x00); outb(0x3cf, color
& 0x0f); /* set/reset */
73 outb(0x3ce, 0x01); outb(0x3cf, 0x0f); /* set/reset enable */
74 outb(0x3ce, 0x08); outb(0x3cf, 0x80 >> (x
%8)); /* bit mask */
75 object
->Bitmap
[offset
] |= color
;
83 VGLGetXY(VGLBitmap
*object
, int x
, int y
)
93 if (x
<0 || x
>=object
->VXsize
|| y
<0 || y
>=object
->VYsize
)
95 switch (object
->Type
) {
98 return object
->Bitmap
[((y
*object
->VXsize
)+x
)];
100 return object
->Bitmap
[VGLSetSegment(y
*object
->VXsize
+x
)];
102 outb(0x3ce, 0x04); outb(0x3cf, x
& 0x3);
103 return object
->Bitmap
[(unsigned)(VGLAdpInfo
.va_line_width
*y
)+(x
/4)];
105 offset
= VGLSetSegment(y
*VGLAdpInfo
.va_line_width
+ x
/8);
108 offset
= y
*VGLAdpInfo
.va_line_width
+ x
/8;
111 return (object
->Bitmap
[offset
]&(0x80>>(x
%8))) ? 1 : 0; /* XXX */
114 mask
= 0x80 >> (x
%8);
115 for (i
= 0; i
< VGLModeInfo
.vi_planes
; i
++) {
116 outb(0x3ce, 0x04); outb(0x3cf, i
);
117 color
|= (object
->Bitmap
[offset
] & mask
) ? (1 << i
) : 0;
126 VGLLine(VGLBitmap
*object
, int x1
, int y1
, int x2
, int y2
, byte color
)
128 int d
, x
, y
, ax
, ay
, sx
, sy
, dx
, dy
;
130 dx
= x2
-x1
; ax
= ABS(dx
)<<1; sx
= SGN(dx
); x
= x1
;
131 dy
= y2
-y1
; ay
= ABS(dy
)<<1; sy
= SGN(dy
); y
= y1
;
133 if (ax
>ay
) { /* x dominant */
136 VGLSetXY(object
, x
, y
, color
);
145 else { /* y dominant */
148 VGLSetXY(object
, x
, y
, color
);
160 VGLBox(VGLBitmap
*object
, int x1
, int y1
, int x2
, int y2
, byte color
)
162 VGLLine(object
, x1
, y1
, x2
, y1
, color
);
163 VGLLine(object
, x2
, y1
, x2
, y2
, color
);
164 VGLLine(object
, x2
, y2
, x1
, y2
, color
);
165 VGLLine(object
, x1
, y2
, x1
, y1
, color
);
169 VGLFilledBox(VGLBitmap
*object
, int x1
, int y1
, int x2
, int y2
, byte color
)
173 for (y
=y1
; y
<=y2
; y
++) VGLLine(object
, x1
, y
, x2
, y
, color
);
177 set4pixels(VGLBitmap
*object
, int x
, int y
, int xc
, int yc
, byte color
)
180 VGLSetXY(object
, xc
+x
, yc
+y
, color
);
181 VGLSetXY(object
, xc
-x
, yc
+y
, color
);
183 VGLSetXY(object
, xc
+x
, yc
-y
, color
);
184 VGLSetXY(object
, xc
-x
, yc
-y
, color
);
188 VGLSetXY(object
, xc
, yc
+y
, color
);
190 VGLSetXY(object
, xc
, yc
-y
, color
);
195 VGLEllipse(VGLBitmap
*object
, int xc
, int yc
, int a
, int b
, byte color
)
197 int x
= 0, y
= b
, asq
= a
*a
, asq2
= a
*a
*2, bsq
= b
*b
;
198 int bsq2
= b
*b
*2, d
= bsq
-asq
*b
+asq
/4, dx
= 0, dy
= asq2
*b
;
201 set4pixels(object
, x
, y
, xc
, yc
, color
);
203 y
--; dy
-=asq2
; d
-=dy
;
205 x
++; dx
+=bsq2
; d
+=bsq
+dx
;
207 d
+=(3*(asq
-bsq
)/2-(dx
+dy
))/2;
209 set4pixels(object
, x
, y
, xc
, yc
, color
);
211 x
++; dx
+=bsq2
; d
+=dx
;
213 y
--; dy
-=asq2
; d
+=asq
-dy
;
218 set2lines(VGLBitmap
*object
, int x
, int y
, int xc
, int yc
, byte color
)
221 VGLLine(object
, xc
+x
, yc
+y
, xc
-x
, yc
+y
, color
);
223 VGLLine(object
, xc
+x
, yc
-y
, xc
-x
, yc
-y
, color
);
226 VGLLine(object
, xc
, yc
+y
, xc
, yc
-y
, color
);
231 VGLFilledEllipse(VGLBitmap
*object
, int xc
, int yc
, int a
, int b
, byte color
)
233 int x
= 0, y
= b
, asq
= a
*a
, asq2
= a
*a
*2, bsq
= b
*b
;
234 int bsq2
= b
*b
*2, d
= bsq
-asq
*b
+asq
/4, dx
= 0, dy
= asq2
*b
;
237 set2lines(object
, x
, y
, xc
, yc
, color
);
239 y
--; dy
-=asq2
; d
-=dy
;
241 x
++; dx
+=bsq2
; d
+=bsq
+dx
;
243 d
+=(3*(asq
-bsq
)/2-(dx
+dy
))/2;
245 set2lines(object
, x
, y
, xc
, yc
, color
);
247 x
++; dx
+=bsq2
; d
+=dx
;
249 y
--; dy
-=asq2
; d
+=asq
-dy
;
254 VGLClear(VGLBitmap
*object
, byte color
)
260 VGLMouseFreeze(0, 0, object
->Xsize
, object
->Ysize
, color
);
261 switch (object
->Type
) {
264 memset(object
->Bitmap
, color
, object
->VXsize
*object
->VYsize
);
268 for (offset
= 0; offset
< object
->VXsize
*object
->VYsize
; ) {
269 VGLSetSegment(offset
);
270 len
= min(object
->VXsize
*object
->VYsize
- offset
,
271 VGLAdpInfo
.va_window_size
);
272 memset(object
->Bitmap
, color
, len
);
278 /* XXX works only for Xsize % 4 = 0 */
280 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
281 memset(object
->Bitmap
, color
, VGLAdpInfo
.va_line_width
*object
->VYsize
);
286 /* XXX works only for Xsize % 8 = 0 */
287 outb(0x3c4, 0x02); outb(0x3c5, 0x0f);
288 outb(0x3ce, 0x05); outb(0x3cf, 0x02); /* mode 2 */
289 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
290 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
291 for (offset
= 0; offset
< VGLAdpInfo
.va_line_width
*object
->VYsize
; ) {
292 VGLSetSegment(offset
);
293 len
= min(object
->VXsize
*object
->VYsize
- offset
,
294 VGLAdpInfo
.va_window_size
);
295 memset(object
->Bitmap
, color
, len
);
298 outb(0x3ce, 0x05); outb(0x3cf, 0x00);
305 VGLRestorePalette(void)
312 for (i
=0; i
<256; i
++) {
313 outb(0x3C9, VGLSavePaletteRed
[i
]);
315 outb(0x3C9, VGLSavePaletteGreen
[i
]);
317 outb(0x3C9, VGLSavePaletteBlue
[i
]);
332 for (i
=0; i
<256; i
++) {
333 VGLSavePaletteRed
[i
] = inb(0x3C9);
335 VGLSavePaletteGreen
[i
] = inb(0x3C9);
337 VGLSavePaletteBlue
[i
] = inb(0x3C9);
345 VGLSetPalette(byte
*red
, byte
*green
, byte
*blue
)
349 for (i
=0; i
<256; i
++) {
350 VGLSavePaletteRed
[i
] = red
[i
];
351 VGLSavePaletteGreen
[i
] = green
[i
];
352 VGLSavePaletteBlue
[i
] = blue
[i
];
358 for (i
=0; i
<256; i
++) {
359 outb(0x3C9, VGLSavePaletteRed
[i
]);
361 outb(0x3C9, VGLSavePaletteGreen
[i
]);
363 outb(0x3C9, VGLSavePaletteBlue
[i
]);
371 VGLSetPaletteIndex(byte color
, byte red
, byte green
, byte blue
)
373 VGLSavePaletteRed
[color
] = red
;
374 VGLSavePaletteGreen
[color
] = green
;
375 VGLSavePaletteBlue
[color
] = blue
;
380 outb(0x3C9, red
); outb(0x3C9, green
); outb(0x3C9, blue
);
386 VGLSetBorder(byte color
)
390 outb(0x3C0,0x11); outb(0x3C0, color
);
396 VGLBlankDisplay(int blank
)
401 outb(0x3C4, 0x01); val
= inb(0x3C5); outb(0x3C4, 0x01);
402 outb(0x3C5, ((blank
) ? (val
|= 0x20) : (val
&= 0xDF)));