/
rw_svgalib.c
311 lines (243 loc) · 6.54 KB
/
rw_svgalib.c
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
** RW_SVGALBI.C
**
** This file contains ALL Linux specific stuff having to do with the
** software refresh. When a port is being made the following functions
** must be implemented by the port:
**
** SWimp_EndFrame
** SWimp_Init
** SWimp_InitGraphics
** SWimp_SetPalette
** SWimp_Shutdown
** SWimp_SwitchFullscreen
*/
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/vt.h>
#include <stdarg.h>
#include <stdio.h>
#include <signal.h>
#include <sys/mman.h>
#include <asm/io.h>
#include "vga.h"
#include "vgakeyboard.h"
#include "vgamouse.h"
#include "../ref_soft/r_local.h"
#include "../client/keys.h"
#include "../linux/rw_linux.h"
/*****************************************************************************/
int VGA_width, VGA_height, VGA_rowbytes, VGA_bufferrowbytes, VGA_planar;
byte *VGA_pagebase;
char *framebuffer_ptr;
void VGA_UpdatePlanarScreen (void *srcbuffer);
int num_modes;
vga_modeinfo *modes;
int current_mode;
// Console variables that we need to access from this module
/*****************************************************************************/
void VID_InitModes(void)
{
int i;
// get complete information on all modes
num_modes = vga_lastmodenumber()+1;
modes = malloc(num_modes * sizeof(vga_modeinfo));
for (i=0 ; i<num_modes ; i++)
{
if (vga_hasmode(i))
memcpy(&modes[i], vga_getmodeinfo(i), sizeof (vga_modeinfo));
else
modes[i].width = 0; // means not available
}
// filter for modes i don't support
for (i=0 ; i<num_modes ; i++)
{
if (modes[i].bytesperpixel != 1 && modes[i].colors != 256)
modes[i].width = 0;
}
for (i = 0; i < num_modes; i++)
if (modes[i].width)
ri.Con_Printf(PRINT_ALL, "mode %d: %d %d\n", modes[i].width, modes[i].height);
}
/*
** SWimp_Init
**
** This routine is responsible for initializing the implementation
** specific stuff in a software rendering subsystem.
*/
int SWimp_Init( void *hInstance, void *wndProc )
{
vga_init();
VID_InitModes();
return true;
}
int get_mode(int width, int height)
{
int i;
int ok, match;
for (i=0 ; i<num_modes ; i++)
if (modes[i].width &&
modes[i].width == width && modes[i].height == height)
break;
if (i==num_modes)
return -1; // not found
return i;
}
/*
** SWimp_InitGraphics
**
** This initializes the software refresh's implementation specific
** graphics subsystem. In the case of Windows it creates DIB or
** DDRAW surfaces.
**
** The necessary width and height parameters are grabbed from
** vid.width and vid.height.
*/
static qboolean SWimp_InitGraphics( qboolean fullscreen )
{
int bsize, zsize, tsize;
SWimp_Shutdown();
current_mode = get_mode(vid.width, vid.height);
if (current_mode < 0) {
ri.Con_Printf (PRINT_ALL, "Mode %d %d not found\n", vid.width, vid.height);
return false; // mode not found
}
// let the sound and input subsystems know about the new window
ri.Vid_NewWindow (vid.width, vid.height);
ri.Con_Printf (PRINT_ALL, "Setting VGAMode: %d\n", current_mode );
// Cvar_SetValue ("vid_mode", (float)modenum);
VGA_width = modes[current_mode].width;
VGA_height = modes[current_mode].height;
VGA_planar = modes[current_mode].bytesperpixel == 0;
VGA_rowbytes = modes[current_mode].linewidth;
vid.rowbytes = modes[current_mode].linewidth;
if (VGA_planar) {
VGA_bufferrowbytes = modes[current_mode].linewidth * 4;
vid.rowbytes = modes[current_mode].linewidth*4;
}
// get goin'
vga_setmode(current_mode);
VGA_pagebase = framebuffer_ptr = (char *) vga_getgraphmem();
// if (vga_setlinearaddressing()>0)
// framebuffer_ptr = (char *) vga_getgraphmem();
if (!framebuffer_ptr)
Sys_Error("This mode isn't hapnin'\n");
vga_setpage(0);
vid.buffer = malloc(vid.rowbytes * vid.height);
if (!vid.buffer)
Sys_Error("Unabled to alloc vid.buffer!\n");
return true;
}
/*
** SWimp_EndFrame
**
** This does an implementation specific copy from the backbuffer to the
** front buffer. In the Win32 case it uses BitBlt or BltFast depending
** on whether we're using DIB sections/GDI or DDRAW.
*/
void SWimp_EndFrame (void)
{
if (!vga_oktowrite())
return; // can't update screen if it's not active
// if (vid_waitforrefresh.value)
// vga_waitretrace();
if (VGA_planar)
VGA_UpdatePlanarScreen (vid.buffer);
else {
int total = vid.rowbytes * vid.height;
int offset;
for (offset=0;offset<total;offset+=0x10000) {
vga_setpage(offset/0x10000);
memcpy(framebuffer_ptr,
vid.buffer + offset,
((total-offset>0x10000)?0x10000:(total-offset)));
}
}
}
/*
** SWimp_SetMode
*/
rserr_t SWimp_SetMode( int *pwidth, int *pheight, int mode, qboolean fullscreen )
{
rserr_t retval = rserr_ok;
ri.Con_Printf (PRINT_ALL, "setting mode %d:", mode );
if ( !ri.Vid_GetModeInfo( pwidth, pheight, mode ) )
{
ri.Con_Printf( PRINT_ALL, " invalid mode\n" );
return rserr_invalid_mode;
}
ri.Con_Printf( PRINT_ALL, " %d %d\n", *pwidth, *pheight);
if ( !SWimp_InitGraphics( false ) ) {
// failed to set a valid mode in windowed mode
return rserr_invalid_mode;
}
R_GammaCorrectAndSetPalette( ( const unsigned char * ) d_8to24table );
return retval;
}
/*
** SWimp_SetPalette
**
** System specific palette setting routine. A NULL palette means
** to use the existing palette. The palette is expected to be in
** a padded 4-byte xRGB format.
*/
void SWimp_SetPalette( const unsigned char *palette )
{
static int tmppal[256*3];
const unsigned char *pal;
int *tp;
int i;
if ( !palette )
palette = ( const unsigned char * ) sw_state.currentpalette;
if (vga_getcolors() == 256)
{
tp = tmppal;
pal = palette;
for (i=0 ; i < 256 ; i++, pal += 4, tp += 3) {
tp[0] = pal[0] >> 2;
tp[1] = pal[1] >> 2;
tp[2] = pal[2] >> 2;
}
if (vga_oktowrite())
vga_setpalvec(0, 256, tmppal);
}
}
/*
** SWimp_Shutdown
**
** System specific graphics subsystem shutdown routine. Destroys
** DIBs or DDRAW surfaces as appropriate.
*/
void SWimp_Shutdown( void )
{
if (vid.buffer) {
free(vid.buffer);
vid.buffer = NULL;
}
vga_setmode(TEXT);
}
/*
** SWimp_AppActivate
*/
void SWimp_AppActivate( qboolean active )
{
}
//===============================================================================
/*
================
Sys_MakeCodeWriteable
================
*/
void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
{
int r;
unsigned long addr;
int psize = getpagesize();
addr = (startaddr & ~(psize-1)) - psize;
// fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
// addr, startaddr+length, length);
r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
if (r < 0)
Sys_Error("Protection change failed\n");
}