comparison src/video/svga/SDL_svgavideo.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 011b633fa0c9
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
49 #include "SDL_svgavideo.h" 49 #include "SDL_svgavideo.h"
50 #include "SDL_svgaevents_c.h" 50 #include "SDL_svgaevents_c.h"
51 #include "SDL_svgamouse_c.h" 51 #include "SDL_svgamouse_c.h"
52 52
53 /* Initialization/Query functions */ 53 /* Initialization/Query functions */
54 static int SVGA_VideoInit(_THIS, SDL_PixelFormat *vformat); 54 static int SVGA_VideoInit (_THIS, SDL_PixelFormat * vformat);
55 static SDL_Rect **SVGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); 55 static SDL_Rect **SVGA_ListModes (_THIS, SDL_PixelFormat * format,
56 static SDL_Surface *SVGA_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); 56 Uint32 flags);
57 static int SVGA_SetColors(_THIS, int firstcolor, int ncolors, 57 static SDL_Surface *SVGA_SetVideoMode (_THIS, SDL_Surface * current,
58 SDL_Color *colors); 58 int width, int height, int bpp,
59 static void SVGA_VideoQuit(_THIS); 59 Uint32 flags);
60 static int SVGA_SetColors (_THIS, int firstcolor, int ncolors,
61 SDL_Color * colors);
62 static void SVGA_VideoQuit (_THIS);
60 63
61 /* Hardware surface functions */ 64 /* Hardware surface functions */
62 static int SVGA_AllocHWSurface(_THIS, SDL_Surface *surface); 65 static int SVGA_AllocHWSurface (_THIS, SDL_Surface * surface);
63 static int SVGA_LockHWSurface(_THIS, SDL_Surface *surface); 66 static int SVGA_LockHWSurface (_THIS, SDL_Surface * surface);
64 static int SVGA_FlipHWSurface(_THIS, SDL_Surface *surface); 67 static int SVGA_FlipHWSurface (_THIS, SDL_Surface * surface);
65 static void SVGA_UnlockHWSurface(_THIS, SDL_Surface *surface); 68 static void SVGA_UnlockHWSurface (_THIS, SDL_Surface * surface);
66 static void SVGA_FreeHWSurface(_THIS, SDL_Surface *surface); 69 static void SVGA_FreeHWSurface (_THIS, SDL_Surface * surface);
67 70
68 /* SVGAlib driver bootstrap functions */ 71 /* SVGAlib driver bootstrap functions */
69 72
70 static int SVGA_Available(void) 73 static int
71 { 74 SVGA_Available (void)
72 /* Check to see if we are root and stdin is a virtual console */ 75 {
73 int console; 76 /* Check to see if we are root and stdin is a virtual console */
74 77 int console;
75 /* SVGALib 1.9.x+ doesn't require root (via /dev/svga) */ 78
76 int svgalib2 = -1; 79 /* SVGALib 1.9.x+ doesn't require root (via /dev/svga) */
77 80 int svgalib2 = -1;
78 /* See if we are connected to a virtual terminal */ 81
79 console = STDIN_FILENO; 82 /* See if we are connected to a virtual terminal */
80 #if 0 /* This is no longer needed, SVGAlib can switch consoles for us */ 83 console = STDIN_FILENO;
81 if ( console >= 0 ) { 84 #if 0 /* This is no longer needed, SVGAlib can switch consoles for us */
82 struct stat sb; 85 if (console >= 0) {
83 struct vt_mode dummy; 86 struct stat sb;
84 87 struct vt_mode dummy;
85 if ( (fstat(console, &sb) < 0) || 88
86 (ioctl(console, VT_GETMODE, &dummy) < 0) ) { 89 if ((fstat (console, &sb) < 0) ||
87 console = -1; 90 (ioctl (console, VT_GETMODE, &dummy) < 0)) {
88 } 91 console = -1;
89 } 92 }
93 }
90 #endif /* 0 */ 94 #endif /* 0 */
91 95
92 /* See if SVGAlib 2.0 is available */ 96 /* See if SVGAlib 2.0 is available */
93 svgalib2 = open("/dev/svga", O_RDONLY); 97 svgalib2 = open ("/dev/svga", O_RDONLY);
94 if (svgalib2 != -1) { 98 if (svgalib2 != -1) {
95 close(svgalib2); 99 close (svgalib2);
96 } 100 }
97 101
98 return(((svgalib2 != -1) || (geteuid() == 0)) && (console >= 0)); 102 return (((svgalib2 != -1) || (geteuid () == 0)) && (console >= 0));
99 } 103 }
100 104
101 static void SVGA_DeleteDevice(SDL_VideoDevice *device) 105 static void
102 { 106 SVGA_DeleteDevice (SDL_VideoDevice * device)
103 SDL_free(device->hidden); 107 {
104 SDL_free(device); 108 SDL_free (device->hidden);
105 } 109 SDL_free (device);
106 110 }
107 static SDL_VideoDevice *SVGA_CreateDevice(int devindex) 111
108 { 112 static SDL_VideoDevice *
109 SDL_VideoDevice *device; 113 SVGA_CreateDevice (int devindex)
110 114 {
111 /* Initialize all variables that we clean on shutdown */ 115 SDL_VideoDevice *device;
112 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice)); 116
113 if ( device ) { 117 /* Initialize all variables that we clean on shutdown */
114 SDL_memset(device, 0, (sizeof *device)); 118 device = (SDL_VideoDevice *) SDL_malloc (sizeof (SDL_VideoDevice));
115 device->hidden = (struct SDL_PrivateVideoData *) 119 if (device) {
116 SDL_malloc((sizeof *device->hidden)); 120 SDL_memset (device, 0, (sizeof *device));
117 } 121 device->hidden = (struct SDL_PrivateVideoData *)
118 if ( (device == NULL) || (device->hidden == NULL) ) { 122 SDL_malloc ((sizeof *device->hidden));
119 SDL_OutOfMemory(); 123 }
120 if ( device ) { 124 if ((device == NULL) || (device->hidden == NULL)) {
121 SDL_free(device); 125 SDL_OutOfMemory ();
122 } 126 if (device) {
123 return(0); 127 SDL_free (device);
124 } 128 }
125 SDL_memset(device->hidden, 0, (sizeof *device->hidden)); 129 return (0);
126 130 }
127 /* Set the function pointers */ 131 SDL_memset (device->hidden, 0, (sizeof *device->hidden));
128 device->VideoInit = SVGA_VideoInit; 132
129 device->ListModes = SVGA_ListModes; 133 /* Set the function pointers */
130 device->SetVideoMode = SVGA_SetVideoMode; 134 device->VideoInit = SVGA_VideoInit;
131 device->SetColors = SVGA_SetColors; 135 device->ListModes = SVGA_ListModes;
132 device->UpdateRects = NULL; 136 device->SetVideoMode = SVGA_SetVideoMode;
133 device->VideoQuit = SVGA_VideoQuit; 137 device->SetColors = SVGA_SetColors;
134 device->AllocHWSurface = SVGA_AllocHWSurface; 138 device->UpdateRects = NULL;
135 device->CheckHWBlit = NULL; 139 device->VideoQuit = SVGA_VideoQuit;
136 device->FillHWRect = NULL; 140 device->AllocHWSurface = SVGA_AllocHWSurface;
137 device->SetHWColorKey = NULL; 141 device->CheckHWBlit = NULL;
138 device->SetHWAlpha = NULL; 142 device->FillHWRect = NULL;
139 device->LockHWSurface = SVGA_LockHWSurface; 143 device->SetHWColorKey = NULL;
140 device->UnlockHWSurface = SVGA_UnlockHWSurface; 144 device->SetHWAlpha = NULL;
141 device->FlipHWSurface = SVGA_FlipHWSurface; 145 device->LockHWSurface = SVGA_LockHWSurface;
142 device->FreeHWSurface = SVGA_FreeHWSurface; 146 device->UnlockHWSurface = SVGA_UnlockHWSurface;
143 device->SetCaption = NULL; 147 device->FlipHWSurface = SVGA_FlipHWSurface;
144 device->SetIcon = NULL; 148 device->FreeHWSurface = SVGA_FreeHWSurface;
145 device->IconifyWindow = NULL; 149 device->SetCaption = NULL;
146 device->GrabInput = NULL; 150 device->SetIcon = NULL;
147 device->GetWMInfo = NULL; 151 device->IconifyWindow = NULL;
148 device->InitOSKeymap = SVGA_InitOSKeymap; 152 device->GrabInput = NULL;
149 device->PumpEvents = SVGA_PumpEvents; 153 device->GetWMInfo = NULL;
150 154 device->InitOSKeymap = SVGA_InitOSKeymap;
151 device->free = SVGA_DeleteDevice; 155 device->PumpEvents = SVGA_PumpEvents;
152 156
153 return device; 157 device->free = SVGA_DeleteDevice;
158
159 return device;
154 } 160 }
155 161
156 VideoBootStrap SVGALIB_bootstrap = { 162 VideoBootStrap SVGALIB_bootstrap = {
157 "svgalib", "SVGAlib", 163 "svgalib", "SVGAlib",
158 SVGA_Available, SVGA_CreateDevice 164 SVGA_Available, SVGA_CreateDevice
159 }; 165 };
160 166
161 static int SVGA_AddMode(_THIS, int mode, int actually_add) 167 static int
162 { 168 SVGA_AddMode (_THIS, int mode, int actually_add)
163 int i, j; 169 {
164 vga_modeinfo *modeinfo; 170 int i, j;
165 171 vga_modeinfo *modeinfo;
166 modeinfo = vga_getmodeinfo(mode); 172
167 173 modeinfo = vga_getmodeinfo (mode);
168 i = modeinfo->bytesperpixel-1; 174
169 if ( i < 0 ) { 175 i = modeinfo->bytesperpixel - 1;
170 return 0; 176 if (i < 0) {
171 } 177 return 0;
172 if ( actually_add ) { 178 }
173 SDL_Rect saved_rect[2]; 179 if (actually_add) {
174 int saved_mode[2]; 180 SDL_Rect saved_rect[2];
175 int b; 181 int saved_mode[2];
176 182 int b;
177 /* Add the mode, sorted largest to smallest */ 183
178 b = 0; 184 /* Add the mode, sorted largest to smallest */
179 j = 0; 185 b = 0;
180 while ( (SDL_modelist[i][j]->w > modeinfo->width) || 186 j = 0;
181 (SDL_modelist[i][j]->h > modeinfo->height) ) { 187 while ((SDL_modelist[i][j]->w > modeinfo->width) ||
182 ++j; 188 (SDL_modelist[i][j]->h > modeinfo->height)) {
183 } 189 ++j;
184 /* Skip modes that are already in our list */ 190 }
185 if ( (SDL_modelist[i][j]->w == modeinfo->width) && 191 /* Skip modes that are already in our list */
186 (SDL_modelist[i][j]->h == modeinfo->height) ) { 192 if ((SDL_modelist[i][j]->w == modeinfo->width) &&
187 return(0); 193 (SDL_modelist[i][j]->h == modeinfo->height)) {
188 } 194 return (0);
189 /* Insert the new mode */ 195 }
190 saved_rect[b] = *SDL_modelist[i][j]; 196 /* Insert the new mode */
191 saved_mode[b] = SDL_vgamode[i][j]; 197 saved_rect[b] = *SDL_modelist[i][j];
192 SDL_modelist[i][j]->w = modeinfo->width; 198 saved_mode[b] = SDL_vgamode[i][j];
193 SDL_modelist[i][j]->h = modeinfo->height; 199 SDL_modelist[i][j]->w = modeinfo->width;
194 SDL_vgamode[i][j] = mode; 200 SDL_modelist[i][j]->h = modeinfo->height;
195 /* Everybody scoot down! */ 201 SDL_vgamode[i][j] = mode;
196 if ( saved_rect[b].w && saved_rect[b].h ) { 202 /* Everybody scoot down! */
197 for ( ++j; SDL_modelist[i][j]->w; ++j ) { 203 if (saved_rect[b].w && saved_rect[b].h) {
198 saved_rect[!b] = *SDL_modelist[i][j]; 204 for (++j; SDL_modelist[i][j]->w; ++j) {
199 saved_mode[!b] = SDL_vgamode[i][j]; 205 saved_rect[!b] = *SDL_modelist[i][j];
200 *SDL_modelist[i][j] = saved_rect[b]; 206 saved_mode[!b] = SDL_vgamode[i][j];
201 SDL_vgamode[i][j] = saved_mode[b]; 207 *SDL_modelist[i][j] = saved_rect[b];
202 b = !b; 208 SDL_vgamode[i][j] = saved_mode[b];
203 } 209 b = !b;
204 *SDL_modelist[i][j] = saved_rect[b]; 210 }
205 SDL_vgamode[i][j] = saved_mode[b]; 211 *SDL_modelist[i][j] = saved_rect[b];
206 } 212 SDL_vgamode[i][j] = saved_mode[b];
207 } else { 213 }
208 ++SDL_nummodes[i]; 214 } else {
209 } 215 ++SDL_nummodes[i];
210 return(1); 216 }
211 } 217 return (1);
212 218 }
213 static void SVGA_UpdateVideoInfo(_THIS) 219
214 { 220 static void
215 vga_modeinfo *modeinfo; 221 SVGA_UpdateVideoInfo (_THIS)
216 222 {
217 this->info.wm_available = 0; 223 vga_modeinfo *modeinfo;
218 this->info.hw_available = (banked ? 0 : 1); 224
219 modeinfo = vga_getmodeinfo(vga_getcurrentmode()); 225 this->info.wm_available = 0;
220 this->info.video_mem = modeinfo->memory; 226 this->info.hw_available = (banked ? 0 : 1);
221 /* FIXME: Add hardware accelerated blit information */ 227 modeinfo = vga_getmodeinfo (vga_getcurrentmode ());
228 this->info.video_mem = modeinfo->memory;
229 /* FIXME: Add hardware accelerated blit information */
222 #ifdef SVGALIB_DEBUG 230 #ifdef SVGALIB_DEBUG
223 printf("Hardware accelerated blit: %savailable\n", modeinfo->haveblit ? "" : "not "); 231 printf ("Hardware accelerated blit: %savailable\n",
232 modeinfo->haveblit ? "" : "not ");
224 #endif 233 #endif
225 } 234 }
226 235
227 int SVGA_VideoInit(_THIS, SDL_PixelFormat *vformat) 236 int
228 { 237 SVGA_VideoInit (_THIS, SDL_PixelFormat * vformat)
229 int keyboard; 238 {
230 int i, j; 239 int keyboard;
231 int mode, total_modes; 240 int i, j;
232 241 int mode, total_modes;
233 /* Initialize all variables that we clean on shutdown */ 242
234 for ( i=0; i<NUM_MODELISTS; ++i ) { 243 /* Initialize all variables that we clean on shutdown */
235 SDL_nummodes[i] = 0; 244 for (i = 0; i < NUM_MODELISTS; ++i) {
236 SDL_modelist[i] = NULL; 245 SDL_nummodes[i] = 0;
237 SDL_vgamode[i] = NULL; 246 SDL_modelist[i] = NULL;
238 } 247 SDL_vgamode[i] = NULL;
239 248 }
240 /* Initialize the library */ 249
241 vga_disabledriverreport(); 250 /* Initialize the library */
242 if ( vga_init() < 0 ) { 251 vga_disabledriverreport ();
243 SDL_SetError("Unable to initialize SVGAlib"); 252 if (vga_init () < 0) {
244 return(-1); 253 SDL_SetError ("Unable to initialize SVGAlib");
245 } 254 return (-1);
246 vga_setmode(TEXT); 255 }
247 256 vga_setmode (TEXT);
248 /* Enable mouse and keyboard support */ 257
249 vga_setmousesupport(1); 258 /* Enable mouse and keyboard support */
250 keyboard = keyboard_init_return_fd(); 259 vga_setmousesupport (1);
251 if ( keyboard < 0 ) { 260 keyboard = keyboard_init_return_fd ();
252 SDL_SetError("Unable to initialize keyboard"); 261 if (keyboard < 0) {
253 return(-1); 262 SDL_SetError ("Unable to initialize keyboard");
254 } 263 return (-1);
255 if ( SVGA_initkeymaps(keyboard) < 0 ) { 264 }
256 return(-1); 265 if (SVGA_initkeymaps (keyboard) < 0) {
257 } 266 return (-1);
258 keyboard_seteventhandler(SVGA_keyboardcallback); 267 }
259 268 keyboard_seteventhandler (SVGA_keyboardcallback);
260 /* Determine the current screen size */ 269
261 this->info.current_w = 0; 270 /* Determine the current screen size */
262 this->info.current_h = 0; 271 this->info.current_w = 0;
263 272 this->info.current_h = 0;
264 /* Determine the screen depth (use default 8-bit depth) */ 273
265 vformat->BitsPerPixel = 8; 274 /* Determine the screen depth (use default 8-bit depth) */
266 275 vformat->BitsPerPixel = 8;
267 /* Enumerate the available fullscreen modes */ 276
268 total_modes = 0; 277 /* Enumerate the available fullscreen modes */
269 for ( mode=vga_lastmodenumber(); mode; --mode ) { 278 total_modes = 0;
270 if ( vga_hasmode(mode) ) { 279 for (mode = vga_lastmodenumber (); mode; --mode) {
271 if ( SVGA_AddMode(this, mode, 0) ) { 280 if (vga_hasmode (mode)) {
272 ++total_modes; 281 if (SVGA_AddMode (this, mode, 0)) {
273 } 282 ++total_modes;
274 } 283 }
275 } 284 }
276 if ( SVGA_AddMode(this, G320x200x256, 0) ) ++total_modes; 285 }
277 if ( total_modes == 0 ) { 286 if (SVGA_AddMode (this, G320x200x256, 0))
278 SDL_SetError("No linear video modes available"); 287 ++total_modes;
279 return(-1); 288 if (total_modes == 0) {
280 } 289 SDL_SetError ("No linear video modes available");
281 for ( i=0; i<NUM_MODELISTS; ++i ) { 290 return (-1);
282 SDL_vgamode[i] = (int *)SDL_malloc(SDL_nummodes[i]*sizeof(int)); 291 }
283 if ( SDL_vgamode[i] == NULL ) { 292 for (i = 0; i < NUM_MODELISTS; ++i) {
284 SDL_OutOfMemory(); 293 SDL_vgamode[i] = (int *) SDL_malloc (SDL_nummodes[i] * sizeof (int));
285 return(-1); 294 if (SDL_vgamode[i] == NULL) {
286 } 295 SDL_OutOfMemory ();
287 SDL_modelist[i] = (SDL_Rect **) 296 return (-1);
288 SDL_malloc((SDL_nummodes[i]+1)*sizeof(SDL_Rect *)); 297 }
289 if ( SDL_modelist[i] == NULL ) { 298 SDL_modelist[i] = (SDL_Rect **)
290 SDL_OutOfMemory(); 299 SDL_malloc ((SDL_nummodes[i] + 1) * sizeof (SDL_Rect *));
291 return(-1); 300 if (SDL_modelist[i] == NULL) {
292 } 301 SDL_OutOfMemory ();
293 for ( j=0; j<SDL_nummodes[i]; ++j ) { 302 return (-1);
294 SDL_modelist[i][j]=(SDL_Rect *)SDL_malloc(sizeof(SDL_Rect)); 303 }
295 if ( SDL_modelist[i][j] == NULL ) { 304 for (j = 0; j < SDL_nummodes[i]; ++j) {
296 SDL_OutOfMemory(); 305 SDL_modelist[i][j] = (SDL_Rect *) SDL_malloc (sizeof (SDL_Rect));
297 return(-1); 306 if (SDL_modelist[i][j] == NULL) {
298 } 307 SDL_OutOfMemory ();
299 SDL_memset(SDL_modelist[i][j], 0, sizeof(SDL_Rect)); 308 return (-1);
300 } 309 }
301 SDL_modelist[i][j] = NULL; 310 SDL_memset (SDL_modelist[i][j], 0, sizeof (SDL_Rect));
302 } 311 }
303 for ( mode=vga_lastmodenumber(); mode; --mode ) { 312 SDL_modelist[i][j] = NULL;
304 if ( vga_hasmode(mode) ) { 313 }
305 SVGA_AddMode(this, mode, 1); 314 for (mode = vga_lastmodenumber (); mode; --mode) {
306 } 315 if (vga_hasmode (mode)) {
307 } 316 SVGA_AddMode (this, mode, 1);
308 SVGA_AddMode(this, G320x200x256, 1); 317 }
309 318 }
310 /* Free extra (duplicated) modes */ 319 SVGA_AddMode (this, G320x200x256, 1);
311 for ( i=0; i<NUM_MODELISTS; ++i ) { 320
312 j = 0; 321 /* Free extra (duplicated) modes */
313 while ( SDL_modelist[i][j] && SDL_modelist[i][j]->w ) { 322 for (i = 0; i < NUM_MODELISTS; ++i) {
314 j++; 323 j = 0;
315 } 324 while (SDL_modelist[i][j] && SDL_modelist[i][j]->w) {
316 while ( SDL_modelist[i][j] ) { 325 j++;
317 SDL_free(SDL_modelist[i][j]); 326 }
318 SDL_modelist[i][j] = NULL; 327 while (SDL_modelist[i][j]) {
319 j++; 328 SDL_free (SDL_modelist[i][j]);
320 } 329 SDL_modelist[i][j] = NULL;
321 } 330 j++;
322 331 }
323 /* Fill in our hardware acceleration capabilities */ 332 }
324 SVGA_UpdateVideoInfo(this); 333
325 334 /* Fill in our hardware acceleration capabilities */
326 /* We're done! */ 335 SVGA_UpdateVideoInfo (this);
327 return(0); 336
328 } 337 /* We're done! */
329 338 return (0);
330 SDL_Rect **SVGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) 339 }
331 { 340
332 return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]); 341 SDL_Rect **
342 SVGA_ListModes (_THIS, SDL_PixelFormat * format, Uint32 flags)
343 {
344 return (SDL_modelist[((format->BitsPerPixel + 7) / 8) - 1]);
333 } 345 }
334 346
335 /* Various screen update functions available */ 347 /* Various screen update functions available */
336 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects); 348 static void SVGA_DirectUpdate (_THIS, int numrects, SDL_Rect * rects);
337 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects); 349 static void SVGA_BankedUpdate (_THIS, int numrects, SDL_Rect * rects);
338 350
339 SDL_Surface *SVGA_SetVideoMode(_THIS, SDL_Surface *current, 351 SDL_Surface *
340 int width, int height, int bpp, Uint32 flags) 352 SVGA_SetVideoMode (_THIS, SDL_Surface * current,
341 { 353 int width, int height, int bpp, Uint32 flags)
342 int mode; 354 {
343 int vgamode; 355 int mode;
344 vga_modeinfo *modeinfo; 356 int vgamode;
345 int screenpage_len; 357 vga_modeinfo *modeinfo;
346 358 int screenpage_len;
347 /* Free old pixels if we were in banked mode */ 359
348 if ( banked && current->pixels ) { 360 /* Free old pixels if we were in banked mode */
349 free(current->pixels); 361 if (banked && current->pixels) {
350 current->pixels = NULL; 362 free (current->pixels);
351 } 363 current->pixels = NULL;
352 364 }
353 /* Try to set the requested linear video mode */ 365
354 bpp = (bpp+7)/8-1; 366 /* Try to set the requested linear video mode */
355 for ( mode=0; SDL_modelist[bpp][mode]; ++mode ) { 367 bpp = (bpp + 7) / 8 - 1;
356 if ( (SDL_modelist[bpp][mode]->w == width) && 368 for (mode = 0; SDL_modelist[bpp][mode]; ++mode) {
357 (SDL_modelist[bpp][mode]->h == height) ) { 369 if ((SDL_modelist[bpp][mode]->w == width) &&
358 break; 370 (SDL_modelist[bpp][mode]->h == height)) {
359 } 371 break;
360 } 372 }
361 if ( SDL_modelist[bpp][mode] == NULL ) { 373 }
362 SDL_SetError("Couldn't find requested mode in list"); 374 if (SDL_modelist[bpp][mode] == NULL) {
363 return(NULL); 375 SDL_SetError ("Couldn't find requested mode in list");
364 } 376 return (NULL);
365 vgamode = SDL_vgamode[bpp][mode]; 377 }
366 vga_setmode(vgamode); 378 vgamode = SDL_vgamode[bpp][mode];
367 vga_setpage(0); 379 vga_setmode (vgamode);
368 380 vga_setpage (0);
369 if ( (vga_setlinearaddressing() < 0) && (vgamode != G320x200x256) ) { 381
370 banked = 1; 382 if ((vga_setlinearaddressing () < 0) && (vgamode != G320x200x256)) {
371 } else { 383 banked = 1;
372 banked = 0; 384 } else {
373 } 385 banked = 0;
374 386 }
375 modeinfo = vga_getmodeinfo(SDL_vgamode[bpp][mode]); 387
376 388 modeinfo = vga_getmodeinfo (SDL_vgamode[bpp][mode]);
377 /* Update hardware acceleration info */ 389
378 SVGA_UpdateVideoInfo(this); 390 /* Update hardware acceleration info */
379 391 SVGA_UpdateVideoInfo (this);
380 /* Allocate the new pixel format for the screen */ 392
381 bpp = (bpp+1)*8; 393 /* Allocate the new pixel format for the screen */
382 if ( (bpp == 16) && (modeinfo->colors == 32768) ) { 394 bpp = (bpp + 1) * 8;
383 bpp = 15; 395 if ((bpp == 16) && (modeinfo->colors == 32768)) {
384 } 396 bpp = 15;
385 if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) { 397 }
386 return(NULL); 398 if (!SDL_ReallocFormat (current, bpp, 0, 0, 0, 0)) {
387 } 399 return (NULL);
388 400 }
389 /* Set up the new mode framebuffer */ 401
390 current->flags = SDL_FULLSCREEN; 402 /* Set up the new mode framebuffer */
391 if ( !banked ) { 403 current->flags = SDL_FULLSCREEN;
392 current->flags |= SDL_HWSURFACE; 404 if (!banked) {
393 } 405 current->flags |= SDL_HWSURFACE;
394 if ( bpp == 8 ) { 406 }
395 /* FIXME: What about DirectColor? */ 407 if (bpp == 8) {
396 current->flags |= SDL_HWPALETTE; 408 /* FIXME: What about DirectColor? */
397 } 409 current->flags |= SDL_HWPALETTE;
398 current->w = width; 410 }
399 current->h = height; 411 current->w = width;
400 current->pitch = modeinfo->linewidth; 412 current->h = height;
401 if ( banked ) { 413 current->pitch = modeinfo->linewidth;
402 current->pixels = SDL_malloc(current->h * current->pitch); 414 if (banked) {
403 if ( !current->pixels ) { 415 current->pixels = SDL_malloc (current->h * current->pitch);
404 SDL_OutOfMemory(); 416 if (!current->pixels) {
405 return(NULL); 417 SDL_OutOfMemory ();
406 } 418 return (NULL);
407 } else { 419 }
408 current->pixels = vga_getgraphmem(); 420 } else {
409 } 421 current->pixels = vga_getgraphmem ();
410 422 }
411 /* set double-buffering */ 423
412 if ( (flags & SDL_DOUBLEBUF) && !banked ) 424 /* set double-buffering */
413 { 425 if ((flags & SDL_DOUBLEBUF) && !banked) {
414 /* length of one screen page in bytes */ 426 /* length of one screen page in bytes */
415 screenpage_len=current->h*modeinfo->linewidth; 427 screenpage_len = current->h * modeinfo->linewidth;
416 428
417 /* if start address should be aligned */ 429 /* if start address should be aligned */
418 if ( modeinfo->linewidth_unit ) 430 if (modeinfo->linewidth_unit) {
419 { 431 if (screenpage_len % modeinfo->linewidth_unit) {
420 if ( screenpage_len % modeinfo->linewidth_unit ) 432 screenpage_len +=
421 { 433 modeinfo->linewidth_unit -
422 screenpage_len += modeinfo->linewidth_unit - ( screenpage_len % modeinfo->linewidth_unit ); 434 (screenpage_len % modeinfo->linewidth_unit);
423 } 435 }
424 } 436 }
425 437
426 /* if we heve enough videomemory = ak je dost videopamete */ 438 /* if we heve enough videomemory = ak je dost videopamete */
427 if ( modeinfo->memory > ( screenpage_len * 2 / 1024 ) ) 439 if (modeinfo->memory > (screenpage_len * 2 / 1024)) {
428 { 440 current->flags |= SDL_DOUBLEBUF;
429 current->flags |= SDL_DOUBLEBUF; 441 flip_page = 0;
430 flip_page = 0; 442 flip_offset[0] = 0;
431 flip_offset[0] = 0; 443 flip_offset[1] = screenpage_len;
432 flip_offset[1] = screenpage_len; 444 flip_address[0] = vga_getgraphmem ();
433 flip_address[0] = vga_getgraphmem(); 445 flip_address[1] = flip_address[0] + screenpage_len;
434 flip_address[1] = flip_address[0]+screenpage_len; 446 SVGA_FlipHWSurface (this, current);
435 SVGA_FlipHWSurface(this,current); 447 }
436 } 448 }
437 } 449
438 450 /* Set the blit function */
439 /* Set the blit function */ 451 if (banked) {
440 if ( banked ) { 452 this->UpdateRects = SVGA_BankedUpdate;
441 this->UpdateRects = SVGA_BankedUpdate; 453 } else {
442 } else { 454 this->UpdateRects = SVGA_DirectUpdate;
443 this->UpdateRects = SVGA_DirectUpdate; 455 }
444 } 456
445 457 /* Set up the mouse handler again (buggy SVGAlib 1.40) */
446 /* Set up the mouse handler again (buggy SVGAlib 1.40) */ 458 mouse_seteventhandler (SVGA_mousecallback);
447 mouse_seteventhandler(SVGA_mousecallback); 459
448 460 /* We're done */
449 /* We're done */ 461 return (current);
450 return(current);
451 } 462 }
452 463
453 /* We don't actually allow hardware surfaces other than the main one */ 464 /* We don't actually allow hardware surfaces other than the main one */
454 static int SVGA_AllocHWSurface(_THIS, SDL_Surface *surface) 465 static int
455 { 466 SVGA_AllocHWSurface (_THIS, SDL_Surface * surface)
456 return(-1); 467 {
457 } 468 return (-1);
458 static void SVGA_FreeHWSurface(_THIS, SDL_Surface *surface) 469 }
459 { 470 static void
460 return; 471 SVGA_FreeHWSurface (_THIS, SDL_Surface * surface)
472 {
473 return;
461 } 474 }
462 475
463 /* We need to wait for vertical retrace on page flipped displays */ 476 /* We need to wait for vertical retrace on page flipped displays */
464 static int SVGA_LockHWSurface(_THIS, SDL_Surface *surface) 477 static int
465 { 478 SVGA_LockHWSurface (_THIS, SDL_Surface * surface)
466 /* The waiting is done in SVGA_FlipHWSurface() */ 479 {
467 return(0); 480 /* The waiting is done in SVGA_FlipHWSurface() */
468 } 481 return (0);
469 static void SVGA_UnlockHWSurface(_THIS, SDL_Surface *surface) 482 }
470 { 483 static void
471 return; 484 SVGA_UnlockHWSurface (_THIS, SDL_Surface * surface)
472 } 485 {
473 486 return;
474 static int SVGA_FlipHWSurface(_THIS, SDL_Surface *surface) 487 }
475 { 488
476 if ( !banked ) { 489 static int
477 vga_setdisplaystart(flip_offset[flip_page]); 490 SVGA_FlipHWSurface (_THIS, SDL_Surface * surface)
478 flip_page=!flip_page; 491 {
479 surface->pixels=flip_address[flip_page]; 492 if (!banked) {
480 vga_waitretrace(); 493 vga_setdisplaystart (flip_offset[flip_page]);
481 } 494 flip_page = !flip_page;
482 return(0); 495 surface->pixels = flip_address[flip_page];
483 } 496 vga_waitretrace ();
484 497 }
485 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects) 498 return (0);
486 { 499 }
487 return; 500
488 } 501 static void
489 502 SVGA_DirectUpdate (_THIS, int numrects, SDL_Rect * rects)
490 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects) 503 {
491 { 504 return;
492 int i, j; 505 }
493 SDL_Rect *rect; 506
494 int page, vp; 507 static void
495 int x, y, w, h; 508 SVGA_BankedUpdate (_THIS, int numrects, SDL_Rect * rects)
496 unsigned char *src; 509 {
497 unsigned char *dst; 510 int i, j;
498 int bpp = this->screen->format->BytesPerPixel; 511 SDL_Rect *rect;
499 int pitch = this->screen->pitch; 512 int page, vp;
500 513 int x, y, w, h;
501 dst = vga_getgraphmem(); 514 unsigned char *src;
502 for ( i=0; i < numrects; ++i ) { 515 unsigned char *dst;
503 rect = &rects[i]; 516 int bpp = this->screen->format->BytesPerPixel;
504 x = rect->x; 517 int pitch = this->screen->pitch;
505 y = rect->y; 518
506 w = rect->w * bpp; 519 dst = vga_getgraphmem ();
507 h = rect->h; 520 for (i = 0; i < numrects; ++i) {
508 521 rect = &rects[i];
509 vp = y * pitch + x * bpp; 522 x = rect->x;
510 src = (unsigned char *)this->screen->pixels + vp; 523 y = rect->y;
511 page = vp >> 16; 524 w = rect->w * bpp;
512 vp &= 0xffff; 525 h = rect->h;
513 vga_setpage(page); 526
514 for (j = 0; j < h; j++) { 527 vp = y * pitch + x * bpp;
515 if (vp + w > 0x10000) { 528 src = (unsigned char *) this->screen->pixels + vp;
516 if (vp >= 0x10000) { 529 page = vp >> 16;
517 page++; 530 vp &= 0xffff;
518 vga_setpage(page); 531 vga_setpage (page);
519 vp &= 0xffff; 532 for (j = 0; j < h; j++) {
520 } else { 533 if (vp + w > 0x10000) {
521 SDL_memcpy(dst + vp, src, 0x10000 - vp); 534 if (vp >= 0x10000) {
522 page++; 535 page++;
523 vga_setpage(page); 536 vga_setpage (page);
524 SDL_memcpy(dst, src + 0x10000 - vp, 537 vp &= 0xffff;
525 (vp + w) & 0xffff); 538 } else {
526 vp = (vp + pitch) & 0xffff; 539 SDL_memcpy (dst + vp, src, 0x10000 - vp);
527 src += pitch; 540 page++;
528 continue; 541 vga_setpage (page);
529 } 542 SDL_memcpy (dst, src + 0x10000 - vp, (vp + w) & 0xffff);
530 } 543 vp = (vp + pitch) & 0xffff;
531 SDL_memcpy(dst + vp, src, w); 544 src += pitch;
532 src += pitch; 545 continue;
533 vp += pitch; 546 }
534 } 547 }
535 } 548 SDL_memcpy (dst + vp, src, w);
536 } 549 src += pitch;
537 550 vp += pitch;
538 int SVGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) 551 }
539 { 552 }
540 int i; 553 }
541 554
542 for(i = 0; i < ncolors; i++) { 555 int
543 vga_setpalette(firstcolor + i, 556 SVGA_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color * colors)
544 colors[i].r>>2, 557 {
545 colors[i].g>>2, 558 int i;
546 colors[i].b>>2); 559
547 } 560 for (i = 0; i < ncolors; i++) {
548 return(1); 561 vga_setpalette (firstcolor + i,
562 colors[i].r >> 2, colors[i].g >> 2, colors[i].b >> 2);
563 }
564 return (1);
549 } 565 }
550 566
551 /* Note: If we are terminated, this could be called in the middle of 567 /* Note: If we are terminated, this could be called in the middle of
552 another SDL video routine -- notably UpdateRects. 568 another SDL video routine -- notably UpdateRects.
553 */ 569 */
554 void SVGA_VideoQuit(_THIS) 570 void
555 { 571 SVGA_VideoQuit (_THIS)
556 int i, j; 572 {
557 573 int i, j;
558 /* Reset the console video mode */ 574
559 if ( this->screen && (this->screen->w && this->screen->h) ) { 575 /* Reset the console video mode */
560 vga_setmode(TEXT); 576 if (this->screen && (this->screen->w && this->screen->h)) {
561 } 577 vga_setmode (TEXT);
562 keyboard_close(); 578 }
563 579 keyboard_close ();
564 /* Free video mode lists */ 580
565 for ( i=0; i<NUM_MODELISTS; ++i ) { 581 /* Free video mode lists */
566 if ( SDL_modelist[i] != NULL ) { 582 for (i = 0; i < NUM_MODELISTS; ++i) {
567 for ( j=0; SDL_modelist[i][j]; ++j ) 583 if (SDL_modelist[i] != NULL) {
568 SDL_free(SDL_modelist[i][j]); 584 for (j = 0; SDL_modelist[i][j]; ++j)
569 SDL_free(SDL_modelist[i]); 585 SDL_free (SDL_modelist[i][j]);
570 SDL_modelist[i] = NULL; 586 SDL_free (SDL_modelist[i]);
571 } 587 SDL_modelist[i] = NULL;
572 if ( SDL_vgamode[i] != NULL ) { 588 }
573 SDL_free(SDL_vgamode[i]); 589 if (SDL_vgamode[i] != NULL) {
574 SDL_vgamode[i] = NULL; 590 SDL_free (SDL_vgamode[i]);
575 } 591 SDL_vgamode[i] = NULL;
576 } 592 }
577 if ( this->screen ) { 593 }
578 if ( banked && this->screen->pixels ) { 594 if (this->screen) {
579 SDL_free(this->screen->pixels); 595 if (banked && this->screen->pixels) {
580 } 596 SDL_free (this->screen->pixels);
581 this->screen->pixels = NULL; 597 }
582 } 598 this->screen->pixels = NULL;
583 } 599 }
584 600 }
601
602 /* vi: set ts=4 sw=4 expandtab: */