19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* libvga based SDL video driver implementation.
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <err.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <osreldate.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/stat.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/fbio.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/consio.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/kbio.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <vgl.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_vglvideo.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_vglevents_c.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_vglmouse_c.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_VideoInit(_THIS, SDL_PixelFormat *vformat);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **VGL_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *VGL_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_SetColors(_THIS, int firstcolor, int ncolors,
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  SDL_Color *colors);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_VideoQuit(_THIS);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_AllocHWSurface(_THIS, SDL_Surface *surface);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_LockHWSurface(_THIS, SDL_Surface *surface);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_FlipHWSurface(_THIS, SDL_Surface *surface);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_UnlockHWSurface(_THIS, SDL_Surface *surface);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_FreeHWSurface(_THIS, SDL_Surface *surface);
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Misc function */
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic VGLMode ** VGLListModes(int depth, int mem_model);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* VGL driver bootstrap functions */
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_Available(void)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Check to see if we are root and stdin is a
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * virtual console. Also try to ensure that
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * modes other than 320x200 are available
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int console, hires_available, i;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLMode **modes;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	console = STDIN_FILENO;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( console >= 0 ) {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		struct stat sb;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		struct vt_mode dummy;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (fstat(console, &sb) < 0) ||
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (ioctl(console, VT_GETMODE, &dummy) < 0) ) {
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			console = -1;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (geteuid() != 0 && console == -1)
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	modes = VGLListModes(8, V_INFO_MM_DIRECT | V_INFO_MM_PACKED);
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hires_available = 0;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; modes[i] != NULL; i++) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((modes[i]->ModeInfo.Xsize > 320) &&
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (modes[i]->ModeInfo.Ysize > 200) &&
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    ((modes[i]->ModeInfo.Type == VIDBUF8) ||
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (modes[i]->ModeInfo.Type == VIDBUF16) ||
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (modes[i]->ModeInfo.Type == VIDBUF32))) {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hires_available = 1;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return hires_available;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_DeleteDevice(SDL_VideoDevice *device)
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *VGL_CreateDevice(int devindex)
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *device;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( device ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(device, 0, (sizeof *device));
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device->hidden = (struct SDL_PrivateVideoData *)
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				  SDL_malloc((sizeof *device->hidden));
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (device == NULL) || (device->hidden == NULL) ) {
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( device ) {
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(device);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoInit = VGL_VideoInit;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ListModes = VGL_ListModes;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetVideoMode = VGL_SetVideoMode;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetColors = VGL_SetColors;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UpdateRects = NULL;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->VideoQuit = VGL_VideoQuit;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->AllocHWSurface = VGL_AllocHWSurface;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CheckHWBlit = NULL;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FillHWRect = NULL;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWColorKey = NULL;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetHWAlpha = NULL;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->LockHWSurface = VGL_LockHWSurface;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->UnlockHWSurface = VGL_UnlockHWSurface;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FlipHWSurface = VGL_FlipHWSurface;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeHWSurface = VGL_FreeHWSurface;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetIcon = NULL;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->SetCaption = NULL;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->GetWMInfo = NULL;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->FreeWMCursor = VGL_FreeWMCursor;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->CreateWMCursor = VGL_CreateWMCursor;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->ShowWMCursor = VGL_ShowWMCursor;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->WarpWMCursor = VGL_WarpWMCursor;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->InitOSKeymap = VGL_InitOSKeymap;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->PumpEvents = VGL_PumpEvents;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->free = VGL_DeleteDevice;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return device;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap VGL_bootstrap = {
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"vgl", "FreeBSD libVGL",
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGL_Available, VGL_CreateDevice
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_AddMode(_THIS, VGLMode *inmode)
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect *mode;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, index;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int next_mode;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if we already have this mode */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (inmode->Depth < 8) {  /* Not supported */
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	index = ((inmode->Depth + 7) / 8) - 1;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i=0; i<SDL_nummodes[index]; ++i) {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mode = SDL_modelist[index][i];
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((mode->w == inmode->ModeInfo.Xsize) &&
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (mode->h == inmode->ModeInfo.Ysize))
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 0;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the new video mode rectangle */
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (mode == NULL) {
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->x = 0;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->y = 0;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->w = inmode->ModeInfo.Xsize;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode->h = inmode->ModeInfo.Ysize;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the new list of modes, and fill in the new mode */
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	next_mode = SDL_nummodes[index];
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index] = (SDL_Rect **)
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_modelist[index] == NULL) {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_nummodes[index] = 0;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(mode);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index][next_mode] = mode;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_modelist[index][next_mode+1] = NULL;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_nummodes[index]++;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_UpdateVideoInfo(_THIS)
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.wm_available = 0;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.hw_available = 1;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.video_mem = 0;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode == NULL) {
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode->ModeInfo.PixelBytes > 0) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->info.video_mem = VGLCurMode->ModeInfo.PixelBytes *
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				       VGLCurMode->ModeInfo.Xsize *
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				       VGLCurMode->ModeInfo.Ysize;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint VGL_VideoInit(_THIS, SDL_PixelFormat *vformat)
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int total_modes;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLMode **modes;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<NUM_MODELISTS; ++i ) {
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_nummodes[i] = 0;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_modelist[i] = NULL;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Enable mouse and keyboard support */
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_getenv("SDL_NO_RAWKBD") == NULL) {
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (VGLKeyboardInit(VGL_CODEKEYS) != 0) {
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unable to initialize keyboard");
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		warnx("Requiest to put keyboard into a raw mode ignored");
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGL_initkeymaps(STDIN_FILENO) != 0) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to initialize keymap");
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGL_initmouse(STDIN_FILENO) != 0) {
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to initialize mouse");
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the current screen size */
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode != NULL) {
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->info.current_w = VGLCurMode->ModeInfo.Xsize;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->info.current_h = VGLCurMode->ModeInfo.Ysize;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the screen depth */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode != NULL)
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->BitsPerPixel = VGLCurMode->Depth;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vformat->BitsPerPixel = 16;	/* Good default */
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Query for the list of available video modes */
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	total_modes = 0;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	modes = VGLListModes(-1, V_INFO_MM_DIRECT | V_INFO_MM_PACKED);
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; modes[i] != NULL; i++) {
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((modes[i]->ModeInfo.Type == VIDBUF8) ||
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (modes[i]->ModeInfo.Type == VIDBUF16) ||
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (modes[i]->ModeInfo.Type == VIDBUF32)) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			VGL_AddMode(this, modes[i]);
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			total_modes++;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (total_modes == 0) {
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("No linear video modes available");
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in our hardware acceleration capabilities */
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGL_UpdateVideoInfo(this);
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the hardware surface lock mutex */
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hw_lock = SDL_CreateMutex();
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (hw_lock == NULL) {
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to create lock mutex");
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		VGL_VideoQuit(this);
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **VGL_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_modelist[((format->BitsPerPixel+7)/8)-1];
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Various screen update functions available */
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *VGL_SetVideoMode(_THIS, SDL_Surface *current,
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      int width, int height, int bpp, Uint32 flags)
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int mode_found;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLMode **modes;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	modes = VGLListModes(bpp, V_INFO_MM_DIRECT | V_INFO_MM_PACKED);
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mode_found = 0;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; modes[i] != NULL; i++) {
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((modes[i]->ModeInfo.Xsize == width) &&
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (modes[i]->ModeInfo.Ysize == height) &&
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    ((modes[i]->ModeInfo.Type == VIDBUF8) ||
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (modes[i]->ModeInfo.Type == VIDBUF16) ||
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     (modes[i]->ModeInfo.Type == VIDBUF32))) {
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mode_found = 1;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (mode_found == 0) {
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("No matching video mode found");
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Shutdown previous videomode (if any) */
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode != NULL)
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		VGLEnd();
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try to set the requested linear video mode */
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLInit(modes[i]->ModeId) != 0) {
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to switch to requested mode");
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLCurMode = SDL_realloc(VGLCurMode, sizeof(VGLMode));
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLCurMode->ModeInfo = *VGLDisplay;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLCurMode->Depth = modes[i]->Depth;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLCurMode->ModeId = modes[i]->ModeId;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLCurMode->Rmask = modes[i]->Rmask;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLCurMode->Gmask = modes[i]->Gmask;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLCurMode->Bmask = modes[i]->Bmask;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Workaround a bug in libvgl */
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode->ModeInfo.PixelBytes == 0)
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(VGLCurMode->ModeInfo.PixelBytes = 1);
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->w = VGLCurMode->ModeInfo.Xsize;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->h = VGLCurMode->ModeInfo.Ysize;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->pixels = VGLCurMode->ModeInfo.Bitmap;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->pitch = VGLCurMode->ModeInfo.Xsize *
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 VGLCurMode->ModeInfo.PixelBytes;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->flags = (SDL_FULLSCREEN | SDL_HWSURFACE);
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if we are in a pseudo-color mode */
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode->ModeInfo.Type == VIDBUF8)
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->flags |= SDL_HWPALETTE;
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if we can do doublebuffering */
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (flags & SDL_DOUBLEBUF) {
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (VGLCurMode->ModeInfo.Xsize * 2 <=
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    VGLCurMode->ModeInfo.VYsize) {
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			current->flags |= SDL_DOUBLEBUF;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flip_page = 0;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flip_address[0] = (byte *)current->pixels;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flip_address[1] = (byte *)current->pixels +
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					  current->h * current->pitch;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			VGL_FlipHWSurface(this, current);
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (! SDL_ReallocFormat(current, modes[i]->Depth, VGLCurMode->Rmask,
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				VGLCurMode->Gmask, VGLCurMode->Bmask, 0)) {
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update hardware acceleration info */
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGL_UpdateVideoInfo(this);
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the blit function */
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->UpdateRects = VGL_DirectUpdate;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done */
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return current;
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We don't actually allow hardware surfaces other than the main one */
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_AllocHWSurface(_THIS, SDL_Surface *surface)
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_FreeHWSurface(_THIS, SDL_Surface *surface)
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We need to wait for vertical retrace on page flipped displays */
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_LockHWSurface(_THIS, SDL_Surface *surface)
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (surface == SDL_VideoSurface) {
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexP(hw_lock);
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_UnlockHWSurface(_THIS, SDL_Surface *surface)
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (surface == SDL_VideoSurface) {
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_mutexV(hw_lock);
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int VGL_FlipHWSurface(_THIS, SDL_Surface *surface)
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLPanScreen(VGLDisplay, 0, flip_page * surface->h) < 0) {
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("VGLPanSreen() failed");
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return -1;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	flip_page = !flip_page;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->pixels = flip_address[flip_page];
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void VGL_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint VGL_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int i;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 0; i < ncolors; i++) {
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        VGLSetPaletteIndex(firstcolor + i,
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			       colors[i].r>>2,
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			       colors[i].g>>2,
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			       colors[i].b>>2);
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 1;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Note:  If we are terminated, this could be called in the middle of
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   another SDL video routine -- notably UpdateRects.
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid VGL_VideoQuit(_THIS)
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Return the keyboard to the normal state */
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VGLKeyboardEnd();
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Reset the console video mode if we actually initialised one */
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (VGLCurMode != NULL) {
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		VGLEnd();
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(VGLCurMode);
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		VGLCurMode = NULL;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clear the lock mutex */
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (hw_lock != NULL) {
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_DestroyMutex(hw_lock);
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hw_lock = NULL;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Free video mode lists */
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; i < NUM_MODELISTS; i++) {
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_modelist[i] != NULL) {
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (j = 0; SDL_modelist[i][j] != NULL; ++j) {
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free(SDL_modelist[i][j]);
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(SDL_modelist[i]);
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_modelist[i] = NULL;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) {
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Direct screen access, not a memory buffer */
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->screen->pixels = NULL;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define VGL_RED_INDEX	0
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define VGL_GREEN_INDEX	1
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define VGL_BLUE_INDEX	2
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic VGLMode **
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVGLListModes(int depth, int mem_model)
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  static VGLMode **modes = NULL;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  VGLBitmap *vminfop;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  VGLMode **modesp, *modescp;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  video_info_t minfo;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int adptype, i, modenum;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (modes == NULL) {
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modes = SDL_malloc(sizeof(VGLMode *) * M_VESA_MODE_MAX);
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bzero(modes, sizeof(VGLMode *) * M_VESA_MODE_MAX);
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  modesp = modes;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (modenum = 0; modenum < M_VESA_MODE_MAX; modenum++) {
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    minfo.vi_mode = modenum;
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ioctl(0, CONS_MODEINFO, &minfo) || ioctl(0, CONS_CURRENT, &adptype))
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      continue;
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (minfo.vi_mode != modenum)
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      continue;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((minfo.vi_flags & V_INFO_GRAPHICS) == 0)
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      continue;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((mem_model != -1) && ((minfo.vi_mem_model & mem_model) == 0))
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      continue;
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((depth > 1) && (minfo.vi_depth != depth))
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      continue;
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* reallocf can fail */
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((*modesp = reallocf(*modesp, sizeof(VGLMode))) == NULL)
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return NULL;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modescp = *modesp;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    vminfop = &(modescp->ModeInfo);
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bzero(vminfop, sizeof(VGLBitmap));
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    vminfop->Type = NOBUF;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    vminfop->PixelBytes = 1;	/* Good default value */
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (minfo.vi_mem_model) {
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case V_INFO_MM_PLANAR:
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* we can handle EGA/VGA planar modes only */
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (!(minfo.vi_depth != 4 || minfo.vi_planes != 4
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    || (adptype != KD_EGA && adptype != KD_VGA)))
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vminfop->Type = VIDBUF4;
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case V_INFO_MM_PACKED:
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* we can do only 256 color packed modes */
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (minfo.vi_depth == 8)
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vminfop->Type = VIDBUF8;
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case V_INFO_MM_VGAX:
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      vminfop->Type = VIDBUF8X;
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__FREEBSD__) && (defined(__DragonFly__) || __FreeBSD_version >= 500000)
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case V_INFO_MM_DIRECT:
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      vminfop->PixelBytes = minfo.vi_pixel_size;
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      switch (vminfop->PixelBytes) {
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case 2:
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vminfop->Type = VIDBUF16;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if notyet
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case 3:
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vminfop->Type = VIDBUF24;
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case 4:
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vminfop->Type = VIDBUF32;
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      default:
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    default:
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (vminfop->Type == NOBUF)
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      continue;
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (vminfop->Type) {
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case VIDBUF16:
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case VIDBUF32:
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      modescp->Rmask = ((1 << minfo.vi_pixel_fsizes[VGL_RED_INDEX]) - 1) <<
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       minfo.vi_pixel_fields[VGL_RED_INDEX];
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      modescp->Gmask = ((1 << minfo.vi_pixel_fsizes[VGL_GREEN_INDEX]) - 1) <<
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       minfo.vi_pixel_fields[VGL_GREEN_INDEX];
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      modescp->Bmask = ((1 << minfo.vi_pixel_fsizes[VGL_BLUE_INDEX]) - 1) <<
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       minfo.vi_pixel_fields[VGL_BLUE_INDEX];
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    default:
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    vminfop->Xsize = minfo.vi_width;
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    vminfop->Ysize = minfo.vi_height;
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modescp->Depth = minfo.vi_depth;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* XXX */
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (minfo.vi_mode >= M_VESA_BASE)
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      modescp->ModeId = _IO('V', minfo.vi_mode - M_VESA_BASE);
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      modescp->ModeId = _IO('S', minfo.vi_mode);
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Sort list */
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i = 0; modes + i < modesp ; i++) {
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (modes[i]->ModeInfo.Xsize * modes[i]->ModeInfo.Ysize >
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  vminfop->Xsize * modes[i]->ModeInfo.Ysize)
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	continue;
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((modes[i]->ModeInfo.Xsize * modes[i]->ModeInfo.Ysize ==
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   vminfop->Xsize * vminfop->Ysize) &&
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  (modes[i]->Depth >= modescp->Depth))
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	continue;
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      *modesp = modes[i];
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      modes[i] = modescp;
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      modescp = *modesp;
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      vminfop = &(modescp->ModeInfo);
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modesp++;
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (*modesp != NULL) {
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(*modesp);
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *modesp = NULL;
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return modes;
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
625