2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* SVGAlib based SDL video driver implementation.
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
33 #if defined(__LINUX__)
35 #elif defined(__FREEBSD__)
36 #include <sys/consio.h>
38 #error You must choose your operating system here
42 #include <vgakeyboard.h>
44 #include "SDL_video.h"
45 #include "SDL_mouse.h"
46 #include "../SDL_sysvideo.h"
47 #include "../SDL_pixels_c.h"
48 #include "../../events/SDL_events_c.h"
49 #include "SDL_svgavideo.h"
50 #include "SDL_svgaevents_c.h"
51 #include "SDL_svgamouse_c.h"
53 /* Initialization/Query functions */
54 static int SVGA_VideoInit(_THIS, SDL_PixelFormat *vformat);
55 static SDL_Rect **SVGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
56 static SDL_Surface *SVGA_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
57 static int SVGA_SetColors(_THIS, int firstcolor, int ncolors,
59 static void SVGA_VideoQuit(_THIS);
61 /* Hardware surface functions */
62 static int SVGA_AllocHWSurface(_THIS, SDL_Surface *surface);
63 static int SVGA_LockHWSurface(_THIS, SDL_Surface *surface);
64 static int SVGA_FlipHWSurface(_THIS, SDL_Surface *surface);
65 static void SVGA_UnlockHWSurface(_THIS, SDL_Surface *surface);
66 static void SVGA_FreeHWSurface(_THIS, SDL_Surface *surface);
68 /* SVGAlib driver bootstrap functions */
70 static int SVGA_Available(void)
72 /* Check to see if we are root and stdin is a virtual console */
75 /* SVGALib 1.9.x+ doesn't require root (via /dev/svga) */
78 /* See if we are connected to a virtual terminal */
79 console = STDIN_FILENO;
80 #if 0 /* This is no longer needed, SVGAlib can switch consoles for us */
85 if ( (fstat(console, &sb) < 0) ||
86 (ioctl(console, VT_GETMODE, &dummy) < 0) ) {
92 /* See if SVGAlib 2.0 is available */
93 svgalib2 = open("/dev/svga", O_RDONLY);
98 return(((svgalib2 != -1) || (geteuid() == 0)) && (console >= 0));
101 static void SVGA_DeleteDevice(SDL_VideoDevice *device)
103 SDL_free(device->hidden);
107 static SDL_VideoDevice *SVGA_CreateDevice(int devindex)
109 SDL_VideoDevice *device;
111 /* Initialize all variables that we clean on shutdown */
112 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
114 SDL_memset(device, 0, (sizeof *device));
115 device->hidden = (struct SDL_PrivateVideoData *)
116 SDL_malloc((sizeof *device->hidden));
118 if ( (device == NULL) || (device->hidden == NULL) ) {
125 SDL_memset(device->hidden, 0, (sizeof *device->hidden));
127 /* Set the function pointers */
128 device->VideoInit = SVGA_VideoInit;
129 device->ListModes = SVGA_ListModes;
130 device->SetVideoMode = SVGA_SetVideoMode;
131 device->SetColors = SVGA_SetColors;
132 device->UpdateRects = NULL;
133 device->VideoQuit = SVGA_VideoQuit;
134 device->AllocHWSurface = SVGA_AllocHWSurface;
135 device->CheckHWBlit = NULL;
136 device->FillHWRect = NULL;
137 device->SetHWColorKey = NULL;
138 device->SetHWAlpha = NULL;
139 device->LockHWSurface = SVGA_LockHWSurface;
140 device->UnlockHWSurface = SVGA_UnlockHWSurface;
141 device->FlipHWSurface = SVGA_FlipHWSurface;
142 device->FreeHWSurface = SVGA_FreeHWSurface;
143 device->SetCaption = NULL;
144 device->SetIcon = NULL;
145 device->IconifyWindow = NULL;
146 device->GrabInput = NULL;
147 device->GetWMInfo = NULL;
148 device->InitOSKeymap = SVGA_InitOSKeymap;
149 device->PumpEvents = SVGA_PumpEvents;
151 device->free = SVGA_DeleteDevice;
156 VideoBootStrap SVGALIB_bootstrap = {
157 "svgalib", "SVGAlib",
158 SVGA_Available, SVGA_CreateDevice
161 static int SVGA_AddMode(_THIS, int mode, int actually_add)
164 vga_modeinfo *modeinfo;
166 modeinfo = vga_getmodeinfo(mode);
168 i = modeinfo->bytesperpixel-1;
172 if ( actually_add ) {
173 SDL_Rect saved_rect[2];
177 /* Add the mode, sorted largest to smallest */
180 while ( (SDL_modelist[i][j]->w > modeinfo->width) ||
181 (SDL_modelist[i][j]->h > modeinfo->height) ) {
184 /* Skip modes that are already in our list */
185 if ( (SDL_modelist[i][j]->w == modeinfo->width) &&
186 (SDL_modelist[i][j]->h == modeinfo->height) ) {
189 /* Insert the new mode */
190 saved_rect[b] = *SDL_modelist[i][j];
191 saved_mode[b] = SDL_vgamode[i][j];
192 SDL_modelist[i][j]->w = modeinfo->width;
193 SDL_modelist[i][j]->h = modeinfo->height;
194 SDL_vgamode[i][j] = mode;
195 /* Everybody scoot down! */
196 if ( saved_rect[b].w && saved_rect[b].h ) {
197 for ( ++j; SDL_modelist[i][j]->w; ++j ) {
198 saved_rect[!b] = *SDL_modelist[i][j];
199 saved_mode[!b] = SDL_vgamode[i][j];
200 *SDL_modelist[i][j] = saved_rect[b];
201 SDL_vgamode[i][j] = saved_mode[b];
204 *SDL_modelist[i][j] = saved_rect[b];
205 SDL_vgamode[i][j] = saved_mode[b];
213 static void SVGA_UpdateVideoInfo(_THIS)
215 vga_modeinfo *modeinfo;
217 this->info.wm_available = 0;
218 this->info.hw_available = (banked ? 0 : 1);
219 modeinfo = vga_getmodeinfo(vga_getcurrentmode());
220 this->info.video_mem = modeinfo->memory;
221 /* FIXME: Add hardware accelerated blit information */
223 printf("Hardware accelerated blit: %savailable\n", modeinfo->haveblit ? "" : "not ");
227 int SVGA_VideoInit(_THIS, SDL_PixelFormat *vformat)
231 int mode, total_modes;
233 /* Initialize all variables that we clean on shutdown */
234 for ( i=0; i<NUM_MODELISTS; ++i ) {
236 SDL_modelist[i] = NULL;
237 SDL_vgamode[i] = NULL;
240 /* Initialize the library */
241 vga_disabledriverreport();
242 if ( vga_init() < 0 ) {
243 SDL_SetError("Unable to initialize SVGAlib");
248 /* Enable mouse and keyboard support */
249 vga_setmousesupport(1);
250 keyboard = keyboard_init_return_fd();
251 if ( keyboard < 0 ) {
252 SDL_SetError("Unable to initialize keyboard");
255 if ( SVGA_initkeymaps(keyboard) < 0 ) {
258 keyboard_seteventhandler(SVGA_keyboardcallback);
260 /* Determine the current screen size */
261 this->info.current_w = 0;
262 this->info.current_h = 0;
264 /* Determine the screen depth (use default 8-bit depth) */
265 vformat->BitsPerPixel = 8;
267 /* Enumerate the available fullscreen modes */
269 for ( mode=vga_lastmodenumber(); mode; --mode ) {
270 if ( vga_hasmode(mode) ) {
271 if ( SVGA_AddMode(this, mode, 0) ) {
276 if ( SVGA_AddMode(this, G320x200x256, 0) ) ++total_modes;
277 if ( total_modes == 0 ) {
278 SDL_SetError("No linear video modes available");
281 for ( i=0; i<NUM_MODELISTS; ++i ) {
282 SDL_vgamode[i] = (int *)SDL_malloc(SDL_nummodes[i]*sizeof(int));
283 if ( SDL_vgamode[i] == NULL ) {
287 SDL_modelist[i] = (SDL_Rect **)
288 SDL_malloc((SDL_nummodes[i]+1)*sizeof(SDL_Rect *));
289 if ( SDL_modelist[i] == NULL ) {
293 for ( j=0; j<SDL_nummodes[i]; ++j ) {
294 SDL_modelist[i][j]=(SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
295 if ( SDL_modelist[i][j] == NULL ) {
299 SDL_memset(SDL_modelist[i][j], 0, sizeof(SDL_Rect));
301 SDL_modelist[i][j] = NULL;
303 for ( mode=vga_lastmodenumber(); mode; --mode ) {
304 if ( vga_hasmode(mode) ) {
305 SVGA_AddMode(this, mode, 1);
308 SVGA_AddMode(this, G320x200x256, 1);
310 /* Free extra (duplicated) modes */
311 for ( i=0; i<NUM_MODELISTS; ++i ) {
313 while ( SDL_modelist[i][j] && SDL_modelist[i][j]->w ) {
316 while ( SDL_modelist[i][j] ) {
317 SDL_free(SDL_modelist[i][j]);
318 SDL_modelist[i][j] = NULL;
323 /* Fill in our hardware acceleration capabilities */
324 SVGA_UpdateVideoInfo(this);
330 SDL_Rect **SVGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
332 return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
335 /* Various screen update functions available */
336 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
337 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects);
339 SDL_Surface *SVGA_SetVideoMode(_THIS, SDL_Surface *current,
340 int width, int height, int bpp, Uint32 flags)
344 vga_modeinfo *modeinfo;
347 /* Free old pixels if we were in banked mode */
348 if ( banked && current->pixels ) {
349 free(current->pixels);
350 current->pixels = NULL;
353 /* Try to set the requested linear video mode */
355 for ( mode=0; SDL_modelist[bpp][mode]; ++mode ) {
356 if ( (SDL_modelist[bpp][mode]->w == width) &&
357 (SDL_modelist[bpp][mode]->h == height) ) {
361 if ( SDL_modelist[bpp][mode] == NULL ) {
362 SDL_SetError("Couldn't find requested mode in list");
365 vgamode = SDL_vgamode[bpp][mode];
366 vga_setmode(vgamode);
369 if ( (vga_setlinearaddressing() < 0) && (vgamode != G320x200x256) ) {
375 modeinfo = vga_getmodeinfo(SDL_vgamode[bpp][mode]);
377 /* Update hardware acceleration info */
378 SVGA_UpdateVideoInfo(this);
380 /* Allocate the new pixel format for the screen */
382 if ( (bpp == 16) && (modeinfo->colors == 32768) ) {
385 if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
389 /* Set up the new mode framebuffer */
390 current->flags = SDL_FULLSCREEN;
392 current->flags |= SDL_HWSURFACE;
395 /* FIXME: What about DirectColor? */
396 current->flags |= SDL_HWPALETTE;
400 current->pitch = modeinfo->linewidth;
402 current->pixels = SDL_malloc(current->h * current->pitch);
403 if ( !current->pixels ) {
408 current->pixels = vga_getgraphmem();
411 /* set double-buffering */
412 if ( (flags & SDL_DOUBLEBUF) && !banked )
414 /* length of one screen page in bytes */
415 screenpage_len=current->h*modeinfo->linewidth;
417 /* if start address should be aligned */
418 if ( modeinfo->linewidth_unit )
420 if ( screenpage_len % modeinfo->linewidth_unit )
422 screenpage_len += modeinfo->linewidth_unit - ( screenpage_len % modeinfo->linewidth_unit );
426 /* if we heve enough videomemory = ak je dost videopamete */
427 if ( modeinfo->memory > ( screenpage_len * 2 / 1024 ) )
429 current->flags |= SDL_DOUBLEBUF;
432 flip_offset[1] = screenpage_len;
433 flip_address[0] = vga_getgraphmem();
434 flip_address[1] = flip_address[0]+screenpage_len;
435 SVGA_FlipHWSurface(this,current);
439 /* Set the blit function */
441 this->UpdateRects = SVGA_BankedUpdate;
443 this->UpdateRects = SVGA_DirectUpdate;
446 /* Set up the mouse handler again (buggy SVGAlib 1.40) */
447 mouse_seteventhandler(SVGA_mousecallback);
453 /* We don't actually allow hardware surfaces other than the main one */
454 static int SVGA_AllocHWSurface(_THIS, SDL_Surface *surface)
458 static void SVGA_FreeHWSurface(_THIS, SDL_Surface *surface)
463 /* We need to wait for vertical retrace on page flipped displays */
464 static int SVGA_LockHWSurface(_THIS, SDL_Surface *surface)
466 /* The waiting is done in SVGA_FlipHWSurface() */
469 static void SVGA_UnlockHWSurface(_THIS, SDL_Surface *surface)
474 static int SVGA_FlipHWSurface(_THIS, SDL_Surface *surface)
477 vga_setdisplaystart(flip_offset[flip_page]);
478 flip_page=!flip_page;
479 surface->pixels=flip_address[flip_page];
485 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
490 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects)
498 int bpp = this->screen->format->BytesPerPixel;
499 int pitch = this->screen->pitch;
501 dst = vga_getgraphmem();
502 for ( i=0; i < numrects; ++i ) {
509 vp = y * pitch + x * bpp;
510 src = (unsigned char *)this->screen->pixels + vp;
514 for (j = 0; j < h; j++) {
515 if (vp + w > 0x10000) {
521 SDL_memcpy(dst + vp, src, 0x10000 - vp);
524 SDL_memcpy(dst, src + 0x10000 - vp,
526 vp = (vp + pitch) & 0xffff;
531 SDL_memcpy(dst + vp, src, w);
538 int SVGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
542 for(i = 0; i < ncolors; i++) {
543 vga_setpalette(firstcolor + i,
551 /* Note: If we are terminated, this could be called in the middle of
552 another SDL video routine -- notably UpdateRects.
554 void SVGA_VideoQuit(_THIS)
558 /* Reset the console video mode */
559 if ( this->screen && (this->screen->w && this->screen->h) ) {
564 /* Free video mode lists */
565 for ( i=0; i<NUM_MODELISTS; ++i ) {
566 if ( SDL_modelist[i] != NULL ) {
567 for ( j=0; SDL_modelist[i][j]; ++j )
568 SDL_free(SDL_modelist[i][j]);
569 SDL_free(SDL_modelist[i]);
570 SDL_modelist[i] = NULL;
572 if ( SDL_vgamode[i] != NULL ) {
573 SDL_free(SDL_vgamode[i]);
574 SDL_vgamode[i] = NULL;
577 if ( this->screen ) {
578 if ( banked && this->screen->pixels ) {
579 SDL_free(this->screen->pixels);
581 this->screen->pixels = NULL;