src/video/svga/SDL_svgavideo.c
author Sam Lantinga <slouken@lokigames.com>
Sat, 16 Jun 2001 01:32:09 +0000
changeset 67 3647c809813d
parent 1 cf2af46e9e2a
child 205 13161d3d349d
permissions -rw-r--r--
Support for SVGALib 2.0, thanks to Benjamin Joel Stover
     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 = NULL;
   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->maxpixels/1024);
   227 	if ( modeinfo->bytesperpixel > 0 ) {
   228 		this->info.video_mem *= modeinfo->bytesperpixel;
   229 	}
   230 	/* FIXME: Add hardware accelerated blit information */
   231 #if 0
   232 printf("Hardware accelerated blit: %savailable\n", modeinfo->haveblit ? "" : "not ");
   233 #endif
   234 }
   235 
   236 int SVGA_VideoInit(_THIS, SDL_PixelFormat *vformat)
   237 {
   238 	int keyboard;
   239 	int i, j;
   240 	int mode, total_modes;
   241 
   242 	/* Initialize all variables that we clean on shutdown */
   243 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   244 		SDL_nummodes[i] = 0;
   245 		SDL_modelist[i] = NULL;
   246 		SDL_vgamode[i] = NULL;
   247 	}
   248 
   249 	/* Initialize the library */
   250 	vga_disabledriverreport();
   251 	if ( vga_init() < 0 ) {
   252 		SDL_SetError("Unable to initialize SVGAlib");
   253 		return(-1);
   254 	}
   255 	vga_setmode(TEXT);
   256 
   257 	/* Enable mouse and keyboard support */
   258 	vga_setmousesupport(1);
   259 	keyboard = keyboard_init_return_fd();
   260 	if ( keyboard < 0 ) {
   261 		SDL_SetError("Unable to initialize keyboard");
   262 		return(-1);
   263 	}
   264 	if ( SVGA_initkeymaps(keyboard) < 0 ) {
   265 		return(-1);
   266 	}
   267 	keyboard_seteventhandler(SVGA_keyboardcallback);
   268 
   269 	/* Determine the screen depth (use default 8-bit depth) */
   270 	vformat->BitsPerPixel = 8;
   271 
   272 	/* Enumerate the available fullscreen modes */
   273 	total_modes = 0;
   274 	for ( mode=vga_lastmodenumber(); mode; --mode ) {
   275 		if ( vga_hasmode(mode) ) {
   276 			if ( SVGA_AddMode(this, mode, 0, 0) ) {
   277 				++total_modes;
   278 			}
   279 		}
   280 	}
   281 	if ( SVGA_AddMode(this, G320x200x256, 0, 1) ) ++total_modes;
   282 	if ( total_modes == 0 ) {
   283 		SDL_SetError("No linear video modes available");
   284 		return(-1);
   285 	}
   286 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   287 		SDL_vgamode[i] = (int *)malloc(SDL_nummodes[i]*sizeof(int));
   288 		if ( SDL_vgamode[i] == NULL ) {
   289 			SDL_OutOfMemory();
   290 			return(-1);
   291 		}
   292 		SDL_modelist[i] = (SDL_Rect **)
   293 				malloc((SDL_nummodes[i]+1)*sizeof(SDL_Rect *));
   294 		if ( SDL_modelist[i] == NULL ) {
   295 			SDL_OutOfMemory();
   296 			return(-1);
   297 		}
   298 		for ( j=0; j<SDL_nummodes[i]; ++j ) {
   299 			SDL_modelist[i][j]=(SDL_Rect *)malloc(sizeof(SDL_Rect));
   300 			if ( SDL_modelist[i][j] == NULL ) {
   301 				SDL_OutOfMemory();
   302 				return(-1);
   303 			}
   304 			memset(SDL_modelist[i][j], 0, sizeof(SDL_Rect));
   305 		}
   306 		SDL_modelist[i][j] = NULL;
   307 	}
   308 	for ( mode=vga_lastmodenumber(); mode; --mode ) {
   309 		if ( vga_hasmode(mode) ) {
   310 			SVGA_AddMode(this, mode, 1, 0);
   311 		}
   312 	}
   313 	SVGA_AddMode(this, G320x200x256, 1, 1);
   314 
   315 	/* Free extra (duplicated) modes */
   316 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   317 		j = 0;
   318 		while ( SDL_modelist[i][j] && SDL_modelist[i][j]->w ) {
   319 			j++;
   320 		}
   321 		while ( SDL_modelist[i][j] ) {
   322 			free(SDL_modelist[i][j]);
   323 			SDL_modelist[i][j] = NULL;
   324 			j++;
   325 		}
   326 	}
   327 
   328 	/* Fill in our hardware acceleration capabilities */
   329 	SVGA_UpdateVideoInfo(this);
   330 
   331 	/* We're done! */
   332 	return(0);
   333 }
   334 
   335 SDL_Rect **SVGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   336 {
   337 	return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
   338 }
   339 
   340 /* Various screen update functions available */
   341 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
   342 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects);
   343 
   344 SDL_Surface *SVGA_SetVideoMode(_THIS, SDL_Surface *current,
   345 				int width, int height, int bpp, Uint32 flags)
   346 {
   347 	int mode;
   348 	int vgamode;
   349 	vga_modeinfo *modeinfo;
   350 
   351 	/* Try to set the requested linear video mode */
   352 	bpp = (bpp+7)/8-1;
   353 	for ( mode=0; SDL_modelist[bpp][mode]; ++mode ) {
   354 		if ( (SDL_modelist[bpp][mode]->w == width) &&
   355 		     (SDL_modelist[bpp][mode]->h == height) ) {
   356 			break;
   357 		}
   358 	}
   359 	if ( SDL_modelist[bpp][mode] == NULL ) {
   360 		SDL_SetError("Couldn't find requested mode in list");
   361 		return(NULL);
   362 	}
   363 	vga_setmode(SDL_vgamode[bpp][mode]);
   364 	vga_setpage(0);
   365 
   366 	vgamode=SDL_vgamode[bpp][mode];
   367 	if ((vga_setlinearaddressing()<0) && (vgamode!=G320x200x256)) {
   368 		SDL_SetError("Unable to set linear addressing");
   369 		return(NULL);
   370 	}
   371 	modeinfo = vga_getmodeinfo(SDL_vgamode[bpp][mode]);
   372 
   373 	/* Update hardware acceleration info */
   374 	SVGA_UpdateVideoInfo(this);
   375 
   376 	/* Allocate the new pixel format for the screen */
   377 	bpp = (bpp+1)*8;
   378 	if ( (bpp == 16) && (modeinfo->colors == 32768) ) {
   379 		bpp = 15;
   380 	}
   381 	if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
   382 		return(NULL);
   383 	}
   384 
   385 	/* Set up the new mode framebuffer */
   386 	current->flags = (SDL_FULLSCREEN|SDL_HWSURFACE);
   387 	if ( bpp == 8 ) {
   388 		/* FIXME: What about DirectColor? */
   389 		current->flags |= SDL_HWPALETTE;
   390 	}
   391 	current->w = width;
   392 	current->h = height;
   393 	current->pitch = modeinfo->linewidth;
   394 	current->pixels = vga_getgraphmem();
   395 
   396 	/* Set the blit function */
   397 	this->UpdateRects = SVGA_DirectUpdate;
   398 
   399 	/* Set up the mouse handler again (buggy SVGAlib 1.40) */
   400 	mouse_seteventhandler(SVGA_mousecallback);
   401 
   402 	/* We're done */
   403 	return(current);
   404 }
   405 
   406 /* We don't actually allow hardware surfaces other than the main one */
   407 static int SVGA_AllocHWSurface(_THIS, SDL_Surface *surface)
   408 {
   409 	return(-1);
   410 }
   411 static void SVGA_FreeHWSurface(_THIS, SDL_Surface *surface)
   412 {
   413 	return;
   414 }
   415 
   416 /* We need to wait for vertical retrace on page flipped displays */
   417 static int SVGA_LockHWSurface(_THIS, SDL_Surface *surface)
   418 {
   419 	if ( (surface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
   420 		vga_waitretrace();
   421 	}
   422 	return(0);
   423 }
   424 static void SVGA_UnlockHWSurface(_THIS, SDL_Surface *surface)
   425 {
   426 	return;
   427 }
   428 
   429 /* FIXME: How is this done with SVGAlib? */
   430 static int SVGA_FlipHWSurface(_THIS, SDL_Surface *surface)
   431 {
   432 	return(0);
   433 }
   434 
   435 static void SVGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
   436 {
   437 	return;
   438 }
   439 
   440 /* FIXME: Can this be used under SVGAlib? */
   441 static void SVGA_BankedUpdate(_THIS, int numrects, SDL_Rect *rects)
   442 {
   443 	return;
   444 }
   445 
   446 int SVGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   447 {
   448         int i;
   449 
   450 	for(i = 0; i < ncolors; i++) {
   451 	        vga_setpalette(firstcolor + i,
   452 			       colors[i].r>>2,
   453 			       colors[i].g>>2,
   454 			       colors[i].b>>2);
   455 	}
   456 	return(1);
   457 }
   458 
   459 /* Note:  If we are terminated, this could be called in the middle of
   460    another SDL video routine -- notably UpdateRects.
   461 */
   462 void SVGA_VideoQuit(_THIS)
   463 {
   464 	int i, j;
   465 
   466 	/* Reset the console video mode */
   467 	if ( this->screen && (this->screen->w && this->screen->h) ) {
   468 		vga_setmode(TEXT);
   469 	}
   470 	keyboard_close();
   471 
   472 	/* Free video mode lists */
   473 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   474 		if ( SDL_modelist[i] != NULL ) {
   475 			for ( j=0; SDL_modelist[i][j]; ++j )
   476 				free(SDL_modelist[i][j]);
   477 			free(SDL_modelist[i]);
   478 			SDL_modelist[i] = NULL;
   479 		}
   480 		if ( SDL_vgamode[i] != NULL ) {
   481 			free(SDL_vgamode[i]);
   482 			SDL_vgamode[i] = NULL;
   483 		}
   484 	}
   485 	if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) {
   486 		/* Direct screen access, no memory buffer */
   487 		this->screen->pixels = NULL;
   488 	}
   489 }