src/video/svga/SDL_svgavideo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 14 Oct 2001 19:49:02 +0000
changeset 205 13161d3d349d
parent 67 3647c809813d
child 226 bb72c418a1f9
permissions -rw-r--r--
Added double-buffering support for SVGAlib (thanks Kutak!)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@devolution.com
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* SVGAlib based SDL video driver implementation.
    29 */
    30 
    31 #include <stdlib.h>
    32 #include <stdio.h>
    33 #include <unistd.h>
    34 #include <sys/stat.h>
    35 #include <sys/types.h>
    36 #include <sys/ioctl.h>
    37 #include <fcntl.h>
    38 
    39 #if defined(linux)
    40 #include <linux/vt.h>
    41 #elif defined(__FreeBSD__)
    42 #include <sys/consio.h>
    43 #else
    44 #error You must choose your operating system here
    45 #endif
    46 #include <vga.h>
    47 #include <vgamouse.h>
    48 #include <vgakeyboard.h>
    49 
    50 #include "SDL.h"
    51 #include "SDL_error.h"
    52 #include "SDL_video.h"
    53 #include "SDL_mouse.h"
    54 #include "SDL_sysvideo.h"
    55 #include "SDL_pixels_c.h"
    56 #include "SDL_events_c.h"
    57 #include "SDL_svgavideo.h"
    58 #include "SDL_svgaevents_c.h"
    59 #include "SDL_svgamouse_c.h"
    60 
    61 
    62 /* Initialization/Query functions */
    63 static int SVGA_VideoInit(_THIS, SDL_PixelFormat *vformat);
    64 static SDL_Rect **SVGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    65 static SDL_Surface *SVGA_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    66 static int SVGA_SetColors(_THIS, int firstcolor, int ncolors,
    67 			  SDL_Color *colors);
    68 static void SVGA_VideoQuit(_THIS);
    69 
    70 /* Hardware surface functions */
    71 static int SVGA_AllocHWSurface(_THIS, SDL_Surface *surface);
    72 static int SVGA_LockHWSurface(_THIS, SDL_Surface *surface);
    73 static int SVGA_FlipHWSurface(_THIS, SDL_Surface *surface);
    74 static void SVGA_UnlockHWSurface(_THIS, SDL_Surface *surface);
    75 static void SVGA_FreeHWSurface(_THIS, SDL_Surface *surface);
    76 
    77 /* SVGAlib driver bootstrap functions */
    78 
    79 static int SVGA_Available(void)
    80 {
    81 	/* Check to see if we are root and stdin is a virtual console */
    82 	int console;
    83 	
    84 	/* SVGALib 1.9.x+ doesn't require root (via /dev/svga) */
    85 	int svgalib2 = -1;
    86 
    87 	/* See if we are connected to a virtual terminal */
    88 	console = STDIN_FILENO;
    89 	if ( console >= 0 ) {
    90 		struct stat sb;
    91 		struct vt_mode dummy;
    92 
    93 		if ( (fstat(console, &sb) < 0) ||
    94 		     (ioctl(console, VT_GETMODE, &dummy) < 0) ) {
    95 			console = -1;
    96 		}
    97 	}
    98 
    99 	/* See if SVGAlib 2.0 is available */
   100 	svgalib2 = open("/dev/svga", O_RDONLY);
   101 	if (svgalib2 != -1) {
   102 		close(svgalib2);
   103 	}
   104 
   105 	return(((svgalib2 != -1) || (geteuid() == 0)) && (console >= 0));
   106 }
   107 
   108 static void SVGA_DeleteDevice(SDL_VideoDevice *device)
   109 {
   110 	free(device->hidden);
   111 	free(device);
   112 }
   113 
   114 static SDL_VideoDevice *SVGA_CreateDevice(int devindex)
   115 {
   116 	SDL_VideoDevice *device;
   117 
   118 	/* Initialize all variables that we clean on shutdown */
   119 	device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
   120 	if ( device ) {
   121 		memset(device, 0, (sizeof *device));
   122 		device->hidden = (struct SDL_PrivateVideoData *)
   123 				malloc((sizeof *device->hidden));
   124 	}
   125 	if ( (device == NULL) || (device->hidden == NULL) ) {
   126 		SDL_OutOfMemory();
   127 		if ( device ) {
   128 			free(device);
   129 		}
   130 		return(0);
   131 	}
   132 	memset(device->hidden, 0, (sizeof *device->hidden));
   133 
   134 	/* Set the function pointers */
   135 	device->VideoInit = SVGA_VideoInit;
   136 	device->ListModes = SVGA_ListModes;
   137 	device->SetVideoMode = SVGA_SetVideoMode;
   138 	device->SetColors = SVGA_SetColors;
   139 	device->UpdateRects = NULL;
   140 	device->VideoQuit = SVGA_VideoQuit;
   141 	device->AllocHWSurface = SVGA_AllocHWSurface;
   142 	device->CheckHWBlit = NULL;
   143 	device->FillHWRect = NULL;
   144 	device->SetHWColorKey = NULL;
   145 	device->SetHWAlpha = NULL;
   146 	device->LockHWSurface = SVGA_LockHWSurface;
   147 	device->UnlockHWSurface = SVGA_UnlockHWSurface;
   148 	device->FlipHWSurface = SVGA_FlipHWSurface;
   149 	device->FreeHWSurface = SVGA_FreeHWSurface;
   150 	device->SetCaption = NULL;
   151 	device->SetIcon = NULL;
   152 	device->IconifyWindow = NULL;
   153 	device->GrabInput = NULL;
   154 	device->GetWMInfo = NULL;
   155 	device->InitOSKeymap = SVGA_InitOSKeymap;
   156 	device->PumpEvents = SVGA_PumpEvents;
   157 
   158 	device->free = SVGA_DeleteDevice;
   159 
   160 	return device;
   161 }
   162 
   163 VideoBootStrap SVGALIB_bootstrap = {
   164 	"svgalib", "SVGAlib",
   165 	SVGA_Available, SVGA_CreateDevice
   166 };
   167 
   168 static int SVGA_AddMode(_THIS, int mode, int actually_add, int force)
   169 {
   170 	vga_modeinfo *modeinfo;
   171 
   172 	modeinfo = vga_getmodeinfo(mode);
   173 	if ( force || ( modeinfo->flags & CAPABLE_LINEAR ) ) {
   174 		int i, j;
   175 
   176 		i = modeinfo->bytesperpixel-1;
   177 		if ( actually_add ) {
   178 			SDL_Rect saved_rect[2];
   179 			int      saved_mode[2];
   180 			int b;
   181 
   182 			/* Add the mode, sorted largest to smallest */
   183 			b = 0;
   184 			j = 0;
   185 			while ( (SDL_modelist[i][j]->w > modeinfo->width) ||
   186 			        (SDL_modelist[i][j]->h > modeinfo->height) ) {
   187 				++j;
   188 			}
   189 			/* Skip modes that are already in our list */
   190 			if ( (SDL_modelist[i][j]->w == modeinfo->width) &&
   191 			     (SDL_modelist[i][j]->h == modeinfo->height) ) {
   192 				return(0);
   193 			}
   194 			/* Insert the new mode */
   195 			saved_rect[b] = *SDL_modelist[i][j];
   196 			saved_mode[b] = SDL_vgamode[i][j];
   197 			SDL_modelist[i][j]->w = modeinfo->width;
   198 			SDL_modelist[i][j]->h = modeinfo->height;
   199 			SDL_vgamode[i][j] = mode;
   200 			/* Everybody scoot down! */
   201 			if ( saved_rect[b].w && saved_rect[b].h ) {
   202 			    for ( ++j; SDL_modelist[i][j]->w; ++j ) {
   203 				saved_rect[!b] = *SDL_modelist[i][j];
   204 				saved_mode[!b] = SDL_vgamode[i][j];
   205 				*SDL_modelist[i][j] = saved_rect[b];
   206 				SDL_vgamode[i][j] = saved_mode[b];
   207 				b = !b;
   208 			    }
   209 			    *SDL_modelist[i][j] = saved_rect[b];
   210 			    SDL_vgamode[i][j] = saved_mode[b];
   211 			}
   212 		} else {
   213 			++SDL_nummodes[i];
   214 		}
   215 	}
   216 	return( force || ( modeinfo->flags & CAPABLE_LINEAR ) );
   217 }
   218 
   219 static void SVGA_UpdateVideoInfo(_THIS)
   220 {
   221 	vga_modeinfo *modeinfo;
   222 
   223 	this->info.wm_available = 0;
   224 	this->info.hw_available = 1;
   225 	modeinfo = vga_getmodeinfo(vga_getcurrentmode());
   226 	this->info.video_mem = modeinfo->memory;
   227 	/* FIXME: Add hardware accelerated blit information */
   228 #if 0
   229 printf("Hardware accelerated blit: %savailable\n", modeinfo->haveblit ? "" : "not ");
   230 #endif
   231 }
   232 
   233 int SVGA_VideoInit(_THIS, SDL_PixelFormat *vformat)
   234 {
   235 	int keyboard;
   236 	int i, j;
   237 	int mode, total_modes;
   238 
   239 	/* Initialize all variables that we clean on shutdown */
   240 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   241 		SDL_nummodes[i] = 0;
   242 		SDL_modelist[i] = NULL;
   243 		SDL_vgamode[i] = NULL;
   244 	}
   245 
   246 	/* Initialize the library */
   247 	vga_disabledriverreport();
   248 	if ( vga_init() < 0 ) {
   249 		SDL_SetError("Unable to initialize SVGAlib");
   250 		return(-1);
   251 	}
   252 	vga_setmode(TEXT);
   253 
   254 	/* Enable mouse and keyboard support */
   255 	vga_setmousesupport(1);
   256 	keyboard = keyboard_init_return_fd();
   257 	if ( keyboard < 0 ) {
   258 		SDL_SetError("Unable to initialize keyboard");
   259 		return(-1);
   260 	}
   261 	if ( SVGA_initkeymaps(keyboard) < 0 ) {
   262 		return(-1);
   263 	}
   264 	keyboard_seteventhandler(SVGA_keyboardcallback);
   265 
   266 	/* Determine the screen depth (use default 8-bit depth) */
   267 	vformat->BitsPerPixel = 8;
   268 
   269 	/* Enumerate the available fullscreen modes */
   270 	total_modes = 0;
   271 	for ( mode=vga_lastmodenumber(); mode; --mode ) {
   272 		if ( vga_hasmode(mode) ) {
   273 			if ( SVGA_AddMode(this, mode, 0, 0) ) {
   274 				++total_modes;
   275 			}
   276 		}
   277 	}
   278 	if ( SVGA_AddMode(this, G320x200x256, 0, 1) ) ++total_modes;
   279 	if ( total_modes == 0 ) {
   280 		SDL_SetError("No linear video modes available");
   281 		return(-1);
   282 	}
   283 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   284 		SDL_vgamode[i] = (int *)malloc(SDL_nummodes[i]*sizeof(int));
   285 		if ( SDL_vgamode[i] == NULL ) {
   286 			SDL_OutOfMemory();
   287 			return(-1);
   288 		}
   289 		SDL_modelist[i] = (SDL_Rect **)
   290 				malloc((SDL_nummodes[i]+1)*sizeof(SDL_Rect *));
   291 		if ( SDL_modelist[i] == NULL ) {
   292 			SDL_OutOfMemory();
   293 			return(-1);
   294 		}
   295 		for ( j=0; j<SDL_nummodes[i]; ++j ) {
   296 			SDL_modelist[i][j]=(SDL_Rect *)malloc(sizeof(SDL_Rect));
   297 			if ( SDL_modelist[i][j] == NULL ) {
   298 				SDL_OutOfMemory();
   299 				return(-1);
   300 			}
   301 			memset(SDL_modelist[i][j], 0, sizeof(SDL_Rect));
   302 		}
   303 		SDL_modelist[i][j] = NULL;
   304 	}
   305 	for ( mode=vga_lastmodenumber(); mode; --mode ) {
   306 		if ( vga_hasmode(mode) ) {
   307 			SVGA_AddMode(this, mode, 1, 0);
   308 		}
   309 	}
   310 	SVGA_AddMode(this, G320x200x256, 1, 1);
   311 
   312 	/* Free extra (duplicated) modes */
   313 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   314 		j = 0;
   315 		while ( SDL_modelist[i][j] && SDL_modelist[i][j]->w ) {
   316 			j++;
   317 		}
   318 		while ( SDL_modelist[i][j] ) {
   319 			free(SDL_modelist[i][j]);
   320 			SDL_modelist[i][j] = NULL;
   321 			j++;
   322 		}
   323 	}
   324 
   325 	/* Fill in our hardware acceleration capabilities */
   326 	SVGA_UpdateVideoInfo(this);
   327 
   328 	/* We're done! */
   329 	return(0);
   330 }
   331 
   332 SDL_Rect **SVGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   333 {
   334 	return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
   335 }
   336 
   337 /* Various screen update functions available */
   338 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
   339 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects);
   340 
   341 SDL_Surface *SVGA_SetVideoMode(_THIS, SDL_Surface *current,
   342 				int width, int height, int bpp, Uint32 flags)
   343 {
   344 	int mode;
   345 	int vgamode;
   346 	vga_modeinfo *modeinfo;
   347 	int screenpage_len;
   348 
   349 	/* Try to set the requested linear video mode */
   350 	bpp = (bpp+7)/8-1;
   351 	for ( mode=0; SDL_modelist[bpp][mode]; ++mode ) {
   352 		if ( (SDL_modelist[bpp][mode]->w == width) &&
   353 		     (SDL_modelist[bpp][mode]->h == height) ) {
   354 			break;
   355 		}
   356 	}
   357 	if ( SDL_modelist[bpp][mode] == NULL ) {
   358 		SDL_SetError("Couldn't find requested mode in list");
   359 		return(NULL);
   360 	}
   361 	vga_setmode(SDL_vgamode[bpp][mode]);
   362 	vga_setpage(0);
   363 
   364 	vgamode=SDL_vgamode[bpp][mode];
   365 	if ((vga_setlinearaddressing()<0) && (vgamode!=G320x200x256)) {
   366 		SDL_SetError("Unable to set linear addressing");
   367 		return(NULL);
   368 	}
   369 	modeinfo = vga_getmodeinfo(SDL_vgamode[bpp][mode]);
   370 
   371 	/* Update hardware acceleration info */
   372 	SVGA_UpdateVideoInfo(this);
   373 
   374 	/* Allocate the new pixel format for the screen */
   375 	bpp = (bpp+1)*8;
   376 	if ( (bpp == 16) && (modeinfo->colors == 32768) ) {
   377 		bpp = 15;
   378 	}
   379 	if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
   380 		return(NULL);
   381 	}
   382 
   383 	/* Set up the new mode framebuffer */
   384 	current->flags = (SDL_FULLSCREEN|SDL_HWSURFACE);
   385 	if ( bpp == 8 ) {
   386 		/* FIXME: What about DirectColor? */
   387 		current->flags |= SDL_HWPALETTE;
   388 	}
   389 	current->w = width;
   390 	current->h = height;
   391 	current->pitch = modeinfo->linewidth;
   392 	current->pixels = vga_getgraphmem();
   393 
   394 	/* set double-buffering */
   395 	if ( flags & SDL_DOUBLEBUF )
   396 	{
   397 	    /* length of one screen page in bytes */
   398 	    screenpage_len=current->h*modeinfo->linewidth;
   399 
   400 	    /* if start address should be aligned */
   401 	    if ( modeinfo->linewidth_unit )
   402 	    {
   403 		if ( screenpage_len % modeinfo->linewidth_unit )    
   404 		{
   405 		    screenpage_len += modeinfo->linewidth_unit - ( screenpage_len % modeinfo->linewidth_unit );
   406 		}
   407 	    }
   408 
   409 	    /* if we heve enough videomemory =  ak je dost videopamete  */
   410 	    if ( modeinfo->memory > ( screenpage_len * 2 / 1024 ) )
   411 	    {
   412 		current->flags |= SDL_DOUBLEBUF;
   413 		flip_page = 0;
   414 		flip_offset[0] = 0;
   415 		flip_offset[1] = screenpage_len;
   416 		flip_address[0] = vga_getgraphmem();
   417 		flip_address[1] = flip_address[0]+screenpage_len;
   418 		SVGA_FlipHWSurface(this,current);
   419 	    }
   420 	} 
   421 
   422 	/* Set the blit function */
   423 	this->UpdateRects = SVGA_DirectUpdate;
   424 
   425 	/* Set up the mouse handler again (buggy SVGAlib 1.40) */
   426 	mouse_seteventhandler(SVGA_mousecallback);
   427 
   428 	/* We're done */
   429 	return(current);
   430 }
   431 
   432 /* We don't actually allow hardware surfaces other than the main one */
   433 static int SVGA_AllocHWSurface(_THIS, SDL_Surface *surface)
   434 {
   435 	return(-1);
   436 }
   437 static void SVGA_FreeHWSurface(_THIS, SDL_Surface *surface)
   438 {
   439 	return;
   440 }
   441 
   442 /* We need to wait for vertical retrace on page flipped displays */
   443 static int SVGA_LockHWSurface(_THIS, SDL_Surface *surface)
   444 {
   445 	/* The waiting is done in SVGA_FlipHWSurface() */
   446 	return(0);
   447 }
   448 static void SVGA_UnlockHWSurface(_THIS, SDL_Surface *surface)
   449 {
   450 	return;
   451 }
   452 
   453 static int SVGA_FlipHWSurface(_THIS, SDL_Surface *surface)
   454 {
   455 	vga_setdisplaystart(flip_offset[flip_page]);
   456 	flip_page=!flip_page;
   457 	surface->pixels=flip_address[flip_page];
   458 	vga_waitretrace();
   459 	return(0);
   460 }
   461 
   462 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
   463 {
   464 	return;
   465 }
   466 
   467 /* FIXME: Can this be used under SVGAlib? */
   468 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects)
   469 {
   470 	return;
   471 }
   472 
   473 int SVGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   474 {
   475         int i;
   476 
   477 	for(i = 0; i < ncolors; i++) {
   478 	        vga_setpalette(firstcolor + i,
   479 			       colors[i].r>>2,
   480 			       colors[i].g>>2,
   481 			       colors[i].b>>2);
   482 	}
   483 	return(1);
   484 }
   485 
   486 /* Note:  If we are terminated, this could be called in the middle of
   487    another SDL video routine -- notably UpdateRects.
   488 */
   489 void SVGA_VideoQuit(_THIS)
   490 {
   491 	int i, j;
   492 
   493 	/* Reset the console video mode */
   494 	if ( this->screen && (this->screen->w && this->screen->h) ) {
   495 		vga_setmode(TEXT);
   496 	}
   497 	keyboard_close();
   498 
   499 	/* Free video mode lists */
   500 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   501 		if ( SDL_modelist[i] != NULL ) {
   502 			for ( j=0; SDL_modelist[i][j]; ++j )
   503 				free(SDL_modelist[i][j]);
   504 			free(SDL_modelist[i]);
   505 			SDL_modelist[i] = NULL;
   506 		}
   507 		if ( SDL_vgamode[i] != NULL ) {
   508 			free(SDL_vgamode[i]);
   509 			SDL_vgamode[i] = NULL;
   510 		}
   511 	}
   512 	if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) {
   513 		/* Direct screen access, no memory buffer */
   514 		this->screen->pixels = NULL;
   515 	}
   516 }
   517