1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*-
* Copyright (c) 1991-1997 Søren Schmidt
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software withough specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <machine/console.h>
#include "vgl.h"
VGLBitmap *VGLDisplay;
static int VGLMode;
static int VGLOldMode;
static byte *VGLBuf;
static byte *VGLMem;
static int VGLSwitchPending;
static int VGLOnDisplay;
static int VGLInitDone = 0;
void
VGLEnd()
{
struct vt_mode smode;
if (!VGLInitDone)
return;
if (VGLOnDisplay && !VGLSwitchPending) {
outb(0x3c4, 0x02);
outb(0x3c5, 0x0f);
bzero(VGLMem, 64*1024);
}
if (VGLOldMode >= M_VESA_BASE) {
/* ugly, but necessary */
ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0);
if (VGLOldMode == M_VESA_800x600) {
int size[3];
size[0] = 80;
size[1] = 25;
size[2] = 16;
ioctl(0, KDRASTER, size);
}
} else {
ioctl(0, _IO('S', VGLOldMode), 0);
}
ioctl(0, KDDISABIO, 0);
ioctl(0, KDSETMODE, KD_TEXT);
smode.mode = VT_AUTO;
ioctl(0, VT_SETMODE, &smode);
free(VGLBuf);
free(VGLDisplay);
VGLKeyboardEnd();
}
static void
VGLAbort()
{
VGLEnd();
exit(0);
}
static void
VGLSwitch()
{
if (!VGLOnDisplay)
VGLOnDisplay = 1;
else
VGLOnDisplay = 0;
VGLSwitchPending = 1;
signal(SIGUSR1, VGLSwitch);
}
int
VGLInit(int mode)
{
struct vt_mode smode;
struct winsize winsz;
int error;
signal(SIGUSR1, VGLSwitch);
signal(SIGINT, VGLAbort);
signal(SIGTERM, VGLAbort);
signal(SIGSEGV, VGLAbort);
signal(SIGBUS, VGLAbort);
VGLOnDisplay = 1;
VGLSwitchPending = 0;
ioctl(0, CONS_GET, &VGLOldMode);
VGLMem = (byte*)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_FILE,
open("/dev/mem", O_RDWR), 0xA0000);
if (VGLMem <= (byte*)0)
return 1;
VGLBuf = (byte*)malloc(256*1024);
if (VGLBuf == NULL)
return 1;
VGLDisplay = (VGLBitmap*) malloc(sizeof(VGLBitmap));
if (VGLDisplay == NULL) {
free(VGLBuf);
return 1;
}
switch (mode) {
case SW_BG640x480: case SW_CG640x480:
VGLDisplay->Type = VIDBUF4;
break;
case SW_VGA_CG320:
VGLDisplay->Type = VIDBUF8;
break;
case SW_VGA_MODEX:
VGLDisplay->Type = VIDBUF8X;
break;
default:
VGLEnd();
return 1;
}
if ((error = ioctl(0, KDENABIO, 0)))
return error;
ioctl(0, VT_WAITACTIVE, 0);
ioctl(0, KDSETMODE, KD_GRAPHICS);
if ((error = ioctl(0, mode, 0))) {
ioctl(0, KDSETMODE, KD_TEXT);
ioctl(0, KDDISABIO, 0);
return error;
}
VGLMode = mode;
outb(0x3c4, 0x02);
outb(0x3c5, 0x0f);
bzero(VGLMem, 64*1024);
if (ioctl(0, TIOCGWINSZ, &winsz)) {
VGLEnd();
return 1;
}
VGLDisplay->Bitmap = VGLMem;
VGLDisplay->Xsize = winsz.ws_xpixel;
VGLDisplay->Ysize = winsz.ws_ypixel;
VGLSavePalette();
smode.mode = VT_PROCESS;
smode.waitv = 0;
smode.relsig = SIGUSR1;
smode.acqsig = SIGUSR1;
smode.frsig = SIGINT;
if (ioctl(0, VT_SETMODE, &smode) == -1) {
VGLEnd();
return 1;
}
VGLTextSetFontFile((byte*)0);
VGLInitDone = 1;
return 0;
}
void
VGLCheckSwitch()
{
while (VGLSwitchPending) {
int i;
VGLSwitchPending = 0;
if (VGLOnDisplay) {
ioctl(0, KDENABIO, 0);
ioctl(0, KDSETMODE, KD_GRAPHICS);
ioctl(0, VGLMode, 0);
outb(0x3c6, 0xff);
for (i=0; i<4; i++) {
outb(0x3c4, 0x02);
outb(0x3c5, 0x01<<i);
bcopy(&VGLBuf[i*64*1024], VGLMem, 64*1024);
}
VGLRestorePalette();
ioctl(0, VT_RELDISP, VT_ACKACQ);
VGLDisplay->Bitmap = VGLMem;
switch (VGLMode) {
case SW_BG640x480: case SW_CG640x480:
VGLDisplay->Type = VIDBUF4;
break;
case SW_VGA_CG320:
VGLDisplay->Type = VIDBUF8;
break;
case SW_VGA_MODEX:
VGLDisplay->Type = VIDBUF8X;
break;
default:
VGLDisplay->Type = VIDBUF8; /* XXX */
break;
}
}
else {
for (i=0; i<4; i++) {
outb(0x3ce, 0x04);
outb(0x3cf, i);
bcopy(VGLMem, &VGLBuf[i*64*1024], 64*1024);
}
ioctl(0, VGLOldMode, 0);
ioctl(0, KDSETMODE, KD_TEXT);
ioctl(0, KDDISABIO, 0);
ioctl(0, VT_RELDISP, VT_TRUE);
VGLDisplay->Bitmap = VGLBuf;
VGLDisplay->Type = MEMBUF;
while (!VGLOnDisplay) pause();
}
}
}
|