src/video/windib/SDL_dibvideo.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 07 Mar 2006 05:21:32 +0000
changeset 1480 0a2bd6507477
parent 1472 4aac8563c296
child 1523 21b1fbb53f4a
permissions -rw-r--r--
More Win64 updates
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     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.
     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     Lesser General Public License for more details.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #define WIN32_LEAN_AND_MEAN
    25 #include <windows.h>
    26 
    27 /* Not yet in the mingw32 cross-compile headers */
    28 #ifndef CDS_FULLSCREEN
    29 #define CDS_FULLSCREEN	4
    30 #endif
    31 
    32 #include "SDL_syswm.h"
    33 #include "../SDL_sysvideo.h"
    34 #include "../SDL_pixels_c.h"
    35 #include "../../events/SDL_sysevents.h"
    36 #include "../../events/SDL_events_c.h"
    37 #include "SDL_dibvideo.h"
    38 #include "../wincommon/SDL_syswm_c.h"
    39 #include "../wincommon/SDL_sysmouse_c.h"
    40 #include "SDL_dibevents_c.h"
    41 #include "../wincommon/SDL_wingl_c.h"
    42 
    43 #ifdef _WIN32_WCE
    44 #define NO_GETDIBITS
    45 #define NO_GAMMA_SUPPORT
    46   #if _WIN32_WCE < 420
    47     #define NO_CHANGEDISPLAYSETTINGS
    48   #else
    49     #define ChangeDisplaySettings(lpDevMode, dwFlags) ChangeDisplaySettingsEx(NULL, (lpDevMode), 0, (dwFlags), 0)
    50   #endif
    51 #endif
    52 #ifndef WS_MAXIMIZE
    53 #define WS_MAXIMIZE	0
    54 #endif
    55 #ifndef WS_THICKFRAME
    56 #define WS_THICKFRAME	0
    57 #endif
    58 #ifndef SWP_NOCOPYBITS
    59 #define SWP_NOCOPYBITS	0
    60 #endif
    61 #ifndef PC_NOCOLLAPSE
    62 #define PC_NOCOLLAPSE	0
    63 #endif
    64 
    65 #ifdef _WIN32_WCE
    66 // defined and used in SDL_sysevents.c
    67 extern HINSTANCE aygshell;
    68 #endif
    69 
    70 /* Initialization/Query functions */
    71 static int DIB_VideoInit(_THIS, SDL_PixelFormat *vformat);
    72 static SDL_Rect **DIB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    73 SDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    74 static int DIB_SetColors(_THIS, int firstcolor, int ncolors,
    75 			 SDL_Color *colors);
    76 static void DIB_CheckGamma(_THIS);
    77 void DIB_SwapGamma(_THIS);
    78 void DIB_QuitGamma(_THIS);
    79 int DIB_SetGammaRamp(_THIS, Uint16 *ramp);
    80 int DIB_GetGammaRamp(_THIS, Uint16 *ramp);
    81 static void DIB_VideoQuit(_THIS);
    82 
    83 /* Hardware surface functions */
    84 static int DIB_AllocHWSurface(_THIS, SDL_Surface *surface);
    85 static int DIB_LockHWSurface(_THIS, SDL_Surface *surface);
    86 static void DIB_UnlockHWSurface(_THIS, SDL_Surface *surface);
    87 static void DIB_FreeHWSurface(_THIS, SDL_Surface *surface);
    88 
    89 /* Windows message handling functions */
    90 static void DIB_RealizePalette(_THIS);
    91 static void DIB_PaletteChanged(_THIS, HWND window);
    92 static void DIB_WinPAINT(_THIS, HDC hdc);
    93 
    94 /* helper fn */
    95 static int DIB_SussScreenDepth();
    96 
    97 /* DIB driver bootstrap functions */
    98 
    99 static int DIB_Available(void)
   100 {
   101 	return(1);
   102 }
   103 
   104 static void DIB_DeleteDevice(SDL_VideoDevice *device)
   105 {
   106 	if ( device ) {
   107 		if ( device->hidden ) {
   108 			SDL_free(device->hidden);
   109 		}
   110 		if ( device->gl_data ) {
   111 			SDL_free(device->gl_data);
   112 		}
   113 		SDL_free(device);
   114 	}
   115 }
   116 
   117 static SDL_VideoDevice *DIB_CreateDevice(int devindex)
   118 {
   119 	SDL_VideoDevice *device;
   120 
   121 	/* Initialize all variables that we clean on shutdown */
   122 	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
   123 	if ( device ) {
   124 		SDL_memset(device, 0, (sizeof *device));
   125 		device->hidden = (struct SDL_PrivateVideoData *)
   126 				SDL_malloc((sizeof *device->hidden));
   127 		device->gl_data = (struct SDL_PrivateGLData *)
   128 				SDL_malloc((sizeof *device->gl_data));
   129 	}
   130 	if ( (device == NULL) || (device->hidden == NULL) ||
   131 		                 (device->gl_data == NULL) ) {
   132 		SDL_OutOfMemory();
   133 		DIB_DeleteDevice(device);
   134 		return(NULL);
   135 	}
   136 	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
   137 	SDL_memset(device->gl_data, 0, (sizeof *device->gl_data));
   138 
   139 	/* Set the function pointers */
   140 	device->VideoInit = DIB_VideoInit;
   141 	device->ListModes = DIB_ListModes;
   142 	device->SetVideoMode = DIB_SetVideoMode;
   143 	device->UpdateMouse = WIN_UpdateMouse;
   144 	device->SetColors = DIB_SetColors;
   145 	device->UpdateRects = NULL;
   146 	device->VideoQuit = DIB_VideoQuit;
   147 	device->AllocHWSurface = DIB_AllocHWSurface;
   148 	device->CheckHWBlit = NULL;
   149 	device->FillHWRect = NULL;
   150 	device->SetHWColorKey = NULL;
   151 	device->SetHWAlpha = NULL;
   152 	device->LockHWSurface = DIB_LockHWSurface;
   153 	device->UnlockHWSurface = DIB_UnlockHWSurface;
   154 	device->FlipHWSurface = NULL;
   155 	device->FreeHWSurface = DIB_FreeHWSurface;
   156 	device->SetGammaRamp = DIB_SetGammaRamp;
   157 	device->GetGammaRamp = DIB_GetGammaRamp;
   158 #if SDL_VIDEO_OPENGL
   159 	device->GL_LoadLibrary = WIN_GL_LoadLibrary;
   160 	device->GL_GetProcAddress = WIN_GL_GetProcAddress;
   161 	device->GL_GetAttribute = WIN_GL_GetAttribute;
   162 	device->GL_MakeCurrent = WIN_GL_MakeCurrent;
   163 	device->GL_SwapBuffers = WIN_GL_SwapBuffers;
   164 #endif
   165 	device->SetCaption = WIN_SetWMCaption;
   166 	device->SetIcon = WIN_SetWMIcon;
   167 	device->IconifyWindow = WIN_IconifyWindow;
   168 	device->GrabInput = WIN_GrabInput;
   169 	device->GetWMInfo = WIN_GetWMInfo;
   170 	device->FreeWMCursor = WIN_FreeWMCursor;
   171 	device->CreateWMCursor = WIN_CreateWMCursor;
   172 	device->ShowWMCursor = WIN_ShowWMCursor;
   173 	device->WarpWMCursor = WIN_WarpWMCursor;
   174 	device->CheckMouseMode = WIN_CheckMouseMode;
   175 	device->InitOSKeymap = DIB_InitOSKeymap;
   176 	device->PumpEvents = DIB_PumpEvents;
   177 
   178 	/* Set up the windows message handling functions */
   179 	WIN_RealizePalette = DIB_RealizePalette;
   180 	WIN_PaletteChanged = DIB_PaletteChanged;
   181 	WIN_WinPAINT = DIB_WinPAINT;
   182 	HandleMessage = DIB_HandleMessage;
   183 
   184 	device->free = DIB_DeleteDevice;
   185 
   186 	/* We're finally ready */
   187 	return device;
   188 }
   189 
   190 VideoBootStrap WINDIB_bootstrap = {
   191 	"windib", "Win95/98/NT/2000/CE GDI",
   192 	DIB_Available, DIB_CreateDevice
   193 };
   194 
   195 static int cmpmodes(const void *va, const void *vb)
   196 {
   197     SDL_Rect *a = *(SDL_Rect **)va;
   198     SDL_Rect *b = *(SDL_Rect **)vb;
   199     if ( a->w == b->w )
   200         return b->h - a->h;
   201     else
   202         return b->w - a->w;
   203 }
   204 
   205 static int DIB_AddMode(_THIS, int bpp, int w, int h)
   206 {
   207 	SDL_Rect *mode;
   208 	int i, index;
   209 	int next_mode;
   210 
   211 	/* Check to see if we already have this mode */
   212 	if ( bpp < 8 ) {  /* Not supported */
   213 		return(0);
   214 	}
   215 	index = ((bpp+7)/8)-1;
   216 	for ( i=0; i<SDL_nummodes[index]; ++i ) {
   217 		mode = SDL_modelist[index][i];
   218 		if ( (mode->w == w) && (mode->h == h) ) {
   219 			return(0);
   220 		}
   221 	}
   222 
   223 	/* Set up the new video mode rectangle */
   224 	mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
   225 	if ( mode == NULL ) {
   226 		SDL_OutOfMemory();
   227 		return(-1);
   228 	}
   229 	mode->x = 0;
   230 	mode->y = 0;
   231 	mode->w = w;
   232 	mode->h = h;
   233 
   234 	/* Allocate the new list of modes, and fill in the new mode */
   235 	next_mode = SDL_nummodes[index];
   236 	SDL_modelist[index] = (SDL_Rect **)
   237 	       SDL_realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
   238 	if ( SDL_modelist[index] == NULL ) {
   239 		SDL_OutOfMemory();
   240 		SDL_nummodes[index] = 0;
   241 		SDL_free(mode);
   242 		return(-1);
   243 	}
   244 	SDL_modelist[index][next_mode] = mode;
   245 	SDL_modelist[index][next_mode+1] = NULL;
   246 	SDL_nummodes[index]++;
   247 
   248 	return(0);
   249 }
   250 
   251 static HPALETTE DIB_CreatePalette(int bpp)
   252 {
   253 /*	RJR: March 28, 2000
   254 	moved palette creation here from "DIB_VideoInit" */
   255 
   256 	HPALETTE handle = NULL;
   257 	
   258 	if ( bpp <= 8 )
   259 	{
   260 		LOGPALETTE *palette;
   261 		HDC hdc;
   262 		int ncolors;
   263 		int i;
   264 
   265 		ncolors = 1;
   266 		for ( i=0; i<bpp; ++i ) {
   267 			ncolors *= 2;
   268 		}
   269 		palette = (LOGPALETTE *)SDL_malloc(sizeof(*palette)+
   270 					ncolors*sizeof(PALETTEENTRY));
   271 		palette->palVersion = 0x300;
   272 		palette->palNumEntries = ncolors;
   273 		hdc = GetDC(SDL_Window);
   274 		GetSystemPaletteEntries(hdc, 0, ncolors, palette->palPalEntry);
   275 		ReleaseDC(SDL_Window, hdc);
   276 		handle = CreatePalette(palette);
   277 		SDL_free(palette);
   278 	}
   279 	
   280 	return handle;
   281 }
   282 
   283 int DIB_VideoInit(_THIS, SDL_PixelFormat *vformat)
   284 {
   285 #ifndef NO_CHANGEDISPLAYSETTINGS
   286 	int i;
   287 	DEVMODE settings;
   288 #endif
   289 
   290 	/* Create the window */
   291 	if ( DIB_CreateWindow(this) < 0 ) {
   292 		return(-1);
   293 	}
   294 #if !SDL_AUDIO_DISABLED
   295 	DX5_SoundFocus(SDL_Window);
   296 #endif
   297 
   298 	/* Determine the screen depth */
   299 	vformat->BitsPerPixel = DIB_SussScreenDepth();
   300 	switch (vformat->BitsPerPixel) {
   301 		case 15:
   302 			vformat->Rmask = 0x00007c00;
   303 			vformat->Gmask = 0x000003e0;
   304 			vformat->Bmask = 0x0000001f;
   305 			vformat->BitsPerPixel = 16;
   306 			break;
   307 		case 16:
   308 			vformat->Rmask = 0x0000f800;
   309 			vformat->Gmask = 0x000007e0;
   310 			vformat->Bmask = 0x0000001f;
   311 			break;
   312 		case 24:
   313 		case 32:
   314 			/* GDI defined as 8-8-8 */
   315 			vformat->Rmask = 0x00ff0000;
   316 			vformat->Gmask = 0x0000ff00;
   317 			vformat->Bmask = 0x000000ff;
   318 			break;
   319 		default:
   320 			break;
   321 	}
   322 
   323 	/* See if gamma is supported on this screen */
   324 	DIB_CheckGamma(this);
   325 
   326 #ifndef NO_CHANGEDISPLAYSETTINGS
   327 
   328 	settings.dmSize = sizeof(DEVMODE);
   329 	settings.dmDriverExtra = 0;
   330 #ifdef _WIN32_WCE
   331 	settings.dmFields = DM_DISPLAYQUERYORIENTATION;
   332 	this->hidden->supportRotation = ChangeDisplaySettingsEx(NULL, &settings, NULL, CDS_TEST, NULL) == DISP_CHANGE_SUCCESSFUL;
   333 #endif
   334 	/* Query for the desktop resolution */
   335 	EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &SDL_desktop_mode);
   336 
   337 	/* Query for the list of available video modes */
   338 	for ( i=0; EnumDisplaySettings(NULL, i, &settings); ++i ) {
   339 		DIB_AddMode(this, settings.dmBitsPerPel,
   340 			settings.dmPelsWidth, settings.dmPelsHeight);
   341 #ifdef _WIN32_WCE		
   342 		if( this->hidden->supportRotation )
   343 			DIB_AddMode(this, settings.dmBitsPerPel,
   344 				settings.dmPelsHeight, settings.dmPelsWidth);
   345 #endif
   346 	}
   347 	/* Sort the mode lists */
   348 	if( i > 1 )
   349 	for ( i=0; i<NUM_MODELISTS; ++i ) {
   350 		if ( SDL_nummodes[i] > 0 ) {
   351 			SDL_qsort(SDL_modelist[i], SDL_nummodes[i], sizeof *SDL_modelist[i], cmpmodes);
   352 		}
   353 	}
   354 #else
   355 	// WinCE and fullscreen mode:
   356 	// We use only vformat->BitsPerPixel that allow SDL to
   357 	// emulate other bpp (8, 32) and use triple buffer, 
   358 	// because SDL surface conversion is much faster than the WinCE one.
   359 	// Although it should be tested on devices with graphics accelerator.
   360 
   361     DIB_AddMode(this, vformat->BitsPerPixel,
   362 			GetDeviceCaps(GetDC(NULL), HORZRES), 
   363 			GetDeviceCaps(GetDC(NULL), VERTRES));
   364 
   365 #endif /* !NO_CHANGEDISPLAYSETTINGS */
   366 
   367 	/* Grab an identity palette if we are in a palettized mode */
   368 	if ( vformat->BitsPerPixel <= 8 ) {
   369 	/*	RJR: March 28, 2000
   370 		moved palette creation to "DIB_CreatePalette" */
   371 		screen_pal = DIB_CreatePalette(vformat->BitsPerPixel);
   372 	}
   373 
   374 	/* Fill in some window manager capabilities */
   375 	this->info.wm_available = 1;
   376 
   377 #ifdef _WIN32_WCE
   378 	this->hidden->origRotation = -1;
   379 #endif
   380 
   381 	/* We're done! */
   382 	return(0);
   383 }
   384 
   385 /* We support any format at any dimension */
   386 SDL_Rect **DIB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   387 {
   388 	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   389 		return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
   390 	} else {
   391 		return((SDL_Rect **)-1);
   392 	}
   393 }
   394 
   395 
   396 /*
   397   Helper fn to work out which screen depth windows is currently using.
   398   15 bit mode is considered 555 format, 16 bit is 565.
   399   returns 0 for unknown mode.
   400   (Derived from code in sept 1999 Windows Developer Journal
   401   http://www.wdj.com/code/archive.html)
   402 */
   403 static int DIB_SussScreenDepth()
   404 {
   405 #ifdef NO_GETDIBITS
   406 	int depth;
   407 	HDC hdc;
   408 
   409 	hdc = GetDC(SDL_Window);
   410 	depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
   411 	ReleaseDC(SDL_Window, hdc);
   412 	return(depth);
   413 #else
   414     int dib_size;
   415     LPBITMAPINFOHEADER dib_hdr;
   416     HDC hdc;
   417     HBITMAP hbm;
   418 
   419     /* Allocate enough space for a DIB header plus palette (for
   420      * 8-bit modes) or bitfields (for 16- and 32-bit modes)
   421      */
   422     dib_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
   423     dib_hdr = (LPBITMAPINFOHEADER) SDL_malloc(dib_size);
   424     SDL_memset(dib_hdr, 0, dib_size);
   425     dib_hdr->biSize = sizeof(BITMAPINFOHEADER);
   426     
   427     /* Get a device-dependent bitmap that's compatible with the
   428        screen.
   429      */
   430     hdc = GetDC(NULL);
   431     hbm = CreateCompatibleBitmap( hdc, 1, 1 );
   432 
   433     /* Convert the DDB to a DIB.  We need to call GetDIBits twice:
   434      * the first call just fills in the BITMAPINFOHEADER; the 
   435      * second fills in the bitfields or palette.
   436      */
   437     GetDIBits(hdc, hbm, 0, 1, NULL, (LPBITMAPINFO) dib_hdr, DIB_RGB_COLORS);
   438     GetDIBits(hdc, hbm, 0, 1, NULL, (LPBITMAPINFO) dib_hdr, DIB_RGB_COLORS);
   439     DeleteObject(hbm);
   440     ReleaseDC(NULL, hdc);
   441 
   442     switch( dib_hdr->biBitCount )
   443     {
   444     case 8:     return 8;
   445     case 24:    return 24;
   446     case 32:    return 32;
   447     case 16:
   448         if( dib_hdr->biCompression == BI_BITFIELDS ) {
   449             /* check the red mask */
   450             switch( ((DWORD*)((char*)dib_hdr + dib_hdr->biSize))[0] ) {
   451                 case 0xf800: return 16;    /* 565 */
   452                 case 0x7c00: return 15;    /* 555 */
   453             }
   454         }
   455     }
   456     return 0;    /* poo. */
   457 #endif /* NO_GETDIBITS */
   458 }
   459 
   460 
   461 /* Various screen update functions available */
   462 static void DIB_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
   463 
   464 SDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current,
   465 				int width, int height, int bpp, Uint32 flags)
   466 {
   467 	SDL_Surface *video;
   468 	Uint32 prev_flags;
   469 	DWORD style;
   470 	const DWORD directstyle =
   471 			(WS_POPUP);
   472 	const DWORD windowstyle = 
   473 			(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX);
   474 	const DWORD resizestyle =
   475 			(WS_THICKFRAME|WS_MAXIMIZEBOX);
   476 	int binfo_size;
   477 	BITMAPINFO *binfo;
   478 	HDC hdc;
   479 	RECT bounds;
   480 	int x, y;
   481 	Uint32 Rmask, Gmask, Bmask;
   482 
   483 	/* Clean up any GL context that may be hanging around */
   484 	if ( current->flags & SDL_OPENGL ) {
   485 		WIN_GL_ShutDown(this);
   486 	}
   487 	SDL_resizing = 1;
   488 
   489 	/* Recalculate the bitmasks if necessary */
   490 	if ( bpp == current->format->BitsPerPixel ) {
   491 		video = current;
   492 	} else {
   493 		switch (bpp) {
   494 			case 15:
   495 			case 16:
   496 				if ( DIB_SussScreenDepth() == 15 ) {
   497 					/* 5-5-5 */
   498 					Rmask = 0x00007c00;
   499 					Gmask = 0x000003e0;
   500 					Bmask = 0x0000001f;
   501 				} else {
   502 					/* 5-6-5 */
   503 					Rmask = 0x0000f800;
   504 					Gmask = 0x000007e0;
   505 					Bmask = 0x0000001f;
   506 				}
   507 				break;
   508 			case 24:
   509 			case 32:
   510 				/* GDI defined as 8-8-8 */
   511 				Rmask = 0x00ff0000;
   512 				Gmask = 0x0000ff00;
   513 				Bmask = 0x000000ff;
   514 				break;
   515 			default:
   516 				Rmask = 0x00000000;
   517 				Gmask = 0x00000000;
   518 				Bmask = 0x00000000;
   519 				break;
   520 		}
   521 		video = SDL_CreateRGBSurface(SDL_SWSURFACE,
   522 					0, 0, bpp, Rmask, Gmask, Bmask, 0);
   523 		if ( video == NULL ) {
   524 			SDL_OutOfMemory();
   525 			return(NULL);
   526 		}
   527 	}
   528 
   529 	/* Fill in part of the video surface */
   530 	prev_flags = video->flags;
   531 	video->flags = 0;	/* Clear flags */
   532 	video->w = width;
   533 	video->h = height;
   534 	video->pitch = SDL_CalculatePitch(video);
   535 
   536 	/* Small fix for WinCE/Win32 - when activating window
   537 	   SDL_VideoSurface is equal to zero, so activating code
   538 	   is not called properly for fullscreen windows because
   539 	   macros WINDIB_FULLSCREEN uses SDL_VideoSurface
   540 	*/
   541 	SDL_VideoSurface = video;
   542 
   543 #if defined(_WIN32_WCE)
   544 	if ( flags & SDL_FULLSCREEN )
   545 		video->flags |= SDL_FULLSCREEN;
   546 #endif
   547 
   548 #ifndef NO_CHANGEDISPLAYSETTINGS
   549 	/* Set fullscreen mode if appropriate */
   550 	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   551 		DEVMODE settings;
   552 		BOOL changed;
   553 
   554 		SDL_memset(&settings, 0, sizeof(DEVMODE));
   555 		settings.dmSize = sizeof(DEVMODE);
   556 
   557 #ifdef _WIN32_WCE
   558 		// try to rotate screen to fit requested resolution
   559 		if( this->hidden->supportRotation )
   560 		{
   561 			DWORD rotation;
   562 
   563 			// ask current mode
   564 			settings.dmFields = DM_DISPLAYORIENTATION;
   565 			ChangeDisplaySettingsEx(NULL, &settings, NULL, CDS_TEST, NULL);
   566 			rotation = settings.dmDisplayOrientation;
   567 
   568 			if( (width > GetDeviceCaps(GetDC(NULL), HORZRES))
   569 				&& (height < GetDeviceCaps(GetDC(NULL), VERTRES)))
   570 			{
   571 				switch( rotation )
   572 				{
   573 				case DMDO_0:
   574 					settings.dmDisplayOrientation = DMDO_90;
   575 					break;
   576 				case DMDO_270:
   577 					settings.dmDisplayOrientation = DMDO_180;
   578 					break;
   579 				}
   580 				if( settings.dmDisplayOrientation != rotation )
   581 				{
   582 					// go to landscape
   583 					this->hidden->origRotation = rotation;
   584 					ChangeDisplaySettingsEx(NULL,&settings,NULL,CDS_RESET,NULL);
   585 				}
   586 			}
   587 			if( (width < GetDeviceCaps(GetDC(NULL), HORZRES))
   588 				&& (height > GetDeviceCaps(GetDC(NULL), VERTRES)))
   589 			{
   590 				switch( rotation )
   591 				{
   592 				case DMDO_90:
   593 					settings.dmDisplayOrientation = DMDO_0;
   594 					break;
   595 				case DMDO_180:
   596 					settings.dmDisplayOrientation = DMDO_270;
   597 					break;
   598 				}
   599 				if( settings.dmDisplayOrientation != rotation )
   600 				{
   601 					// go to portrait
   602 					this->hidden->origRotation = rotation;
   603 					ChangeDisplaySettingsEx(NULL,&settings,NULL,CDS_RESET,NULL);
   604 				}
   605 			}
   606 
   607 		}
   608 #endif
   609 
   610 #ifndef _WIN32_WCE
   611 		settings.dmBitsPerPel = video->format->BitsPerPixel;
   612 		settings.dmPelsWidth = width;
   613 		settings.dmPelsHeight = height;
   614 		settings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
   615 		if ( width <= (int)SDL_desktop_mode.dmPelsWidth &&
   616 		     height <= (int)SDL_desktop_mode.dmPelsHeight ) {
   617 			settings.dmDisplayFrequency = SDL_desktop_mode.dmDisplayFrequency;
   618 			settings.dmFields |= DM_DISPLAYFREQUENCY;
   619 		}
   620 		changed = (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL);
   621 		if ( ! changed && (settings.dmFields & DM_DISPLAYFREQUENCY) ) {
   622 			settings.dmFields &= ~DM_DISPLAYFREQUENCY;
   623 			changed = (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL);
   624 		}
   625 #else
   626 		changed = 1;
   627 #endif
   628 		if ( changed ) {
   629 			video->flags |= SDL_FULLSCREEN;
   630 			SDL_fullscreen_mode = settings;
   631 		}
   632 
   633 	}
   634 #endif /* !NO_CHANGEDISPLAYSETTINGS */
   635 
   636 	/* Reset the palette and create a new one if necessary */
   637 	if ( screen_pal != NULL ) {
   638 	/*	RJR: March 28, 2000
   639 		delete identity palette if switching from a palettized mode */
   640 		DeleteObject(screen_pal);
   641 		screen_pal = NULL;
   642 	}
   643 	if ( bpp <= 8 )
   644 	{
   645 	/*	RJR: March 28, 2000
   646 		create identity palette switching to a palettized mode */
   647 		screen_pal = DIB_CreatePalette(bpp);
   648 	}
   649 
   650 	style = GetWindowLong(SDL_Window, GWL_STYLE);
   651 	style &= ~(resizestyle|WS_MAXIMIZE);
   652 	if ( (video->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   653 		style &= ~windowstyle;
   654 		style |= directstyle;
   655 	} else {
   656 #ifndef NO_CHANGEDISPLAYSETTINGS
   657 		if ( (prev_flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   658 			ChangeDisplaySettings(NULL, 0);
   659 		}
   660 #endif
   661 		if ( flags & SDL_NOFRAME ) {
   662 			style &= ~windowstyle;
   663 			style |= directstyle;
   664 			video->flags |= SDL_NOFRAME;
   665 		} else {
   666 			style &= ~directstyle;
   667 			style |= windowstyle;
   668 			if ( flags & SDL_RESIZABLE ) {
   669 				style |= resizestyle;
   670 				video->flags |= SDL_RESIZABLE;
   671 			}
   672 		}
   673 #if WS_MAXIMIZE
   674 		if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE;
   675 #endif
   676 	}
   677 
   678 	/* DJM: Don't piss of anyone who has setup his own window */
   679 	if ( !SDL_windowid )
   680 		SetWindowLong(SDL_Window, GWL_STYLE, style);
   681 
   682 	/* Delete the old bitmap if necessary */
   683 	if ( screen_bmp != NULL ) {
   684 		DeleteObject(screen_bmp);
   685 	}
   686 	if ( ! (flags & SDL_OPENGL) ) {
   687 		BOOL is16bitmode = (video->format->BytesPerPixel == 2);
   688 
   689 		/* Suss out the bitmap info header */
   690 		binfo_size = sizeof(*binfo);
   691 		if( is16bitmode ) {
   692 			/* 16bit modes, palette area used for rgb bitmasks */
   693 			binfo_size += 3*sizeof(DWORD);
   694 		} else if ( video->format->palette ) {
   695 			binfo_size += video->format->palette->ncolors *
   696 							sizeof(RGBQUAD);
   697 		}
   698 		binfo = (BITMAPINFO *)SDL_malloc(binfo_size);
   699 		if ( ! binfo ) {
   700 			if ( video != current ) {
   701 				SDL_FreeSurface(video);
   702 			}
   703 			SDL_OutOfMemory();
   704 			return(NULL);
   705 		}
   706 
   707 		binfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
   708 		binfo->bmiHeader.biWidth = video->w;
   709 		binfo->bmiHeader.biHeight = -video->h;	/* -ve for topdown bitmap */
   710 		binfo->bmiHeader.biPlanes = 1;
   711 		binfo->bmiHeader.biSizeImage = video->h * video->pitch;
   712 		binfo->bmiHeader.biXPelsPerMeter = 0;
   713 		binfo->bmiHeader.biYPelsPerMeter = 0;
   714 		binfo->bmiHeader.biClrUsed = 0;
   715 		binfo->bmiHeader.biClrImportant = 0;
   716 		binfo->bmiHeader.biBitCount = video->format->BitsPerPixel;
   717 
   718 		if ( is16bitmode ) {
   719 			/* BI_BITFIELDS tells CreateDIBSection about the rgb masks in the palette */
   720 			binfo->bmiHeader.biCompression = BI_BITFIELDS;
   721 			((Uint32*)binfo->bmiColors)[0] = video->format->Rmask;
   722 			((Uint32*)binfo->bmiColors)[1] = video->format->Gmask;
   723 			((Uint32*)binfo->bmiColors)[2] = video->format->Bmask;
   724 		} else {
   725 			binfo->bmiHeader.biCompression = BI_RGB;	/* BI_BITFIELDS for 565 vs 555 */
   726 			if ( video->format->palette ) {
   727 				SDL_memset(binfo->bmiColors, 0,
   728 					video->format->palette->ncolors*sizeof(RGBQUAD));
   729 			}
   730 		}
   731 
   732 		/* Create the offscreen bitmap buffer */
   733 		hdc = GetDC(SDL_Window);
   734 		screen_bmp = CreateDIBSection(hdc, binfo, DIB_RGB_COLORS,
   735 					(void **)(&video->pixels), NULL, 0);
   736 		ReleaseDC(SDL_Window, hdc);
   737 		SDL_free(binfo);
   738 		if ( screen_bmp == NULL ) {
   739 			if ( video != current ) {
   740 				SDL_FreeSurface(video);
   741 			}
   742 			SDL_SetError("Couldn't create DIB section");
   743 			return(NULL);
   744 		}
   745 		this->UpdateRects = DIB_NormalUpdate;
   746 
   747 		/* Set video surface flags */
   748 		if ( bpp <= 8 ) {
   749 			/* BitBlt() maps colors for us */
   750 			video->flags |= SDL_HWPALETTE;
   751 		}
   752 	}
   753 #ifndef _WIN32_WCE
   754 	/* Resize the window */
   755 	if ( !SDL_windowid && !IsZoomed(SDL_Window) ) {
   756 #else
   757 	if ( !SDL_windowid ) {
   758 #endif
   759 		HWND top;
   760 		UINT swp_flags;
   761 		const char *window = NULL;
   762 		const char *center = NULL;
   763 
   764 		if ( !SDL_windowX && !SDL_windowY ) {
   765 			window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
   766 			center = SDL_getenv("SDL_VIDEO_CENTERED");
   767 			if ( window ) {
   768 				if ( SDL_sscanf(window, "%d,%d", &x, &y) == 2 ) {
   769 					SDL_windowX = x;
   770 					SDL_windowY = y;
   771 				}
   772 				if ( SDL_strcmp(window, "center") == 0 ) {
   773 					center = window;
   774 				}
   775 			}
   776 		}
   777 		swp_flags = (SWP_NOCOPYBITS | SWP_SHOWWINDOW);
   778 
   779 		bounds.left = SDL_windowX;
   780 		bounds.top = SDL_windowY;
   781 		bounds.right = SDL_windowX+video->w;
   782 		bounds.bottom = SDL_windowY+video->h;
   783 		AdjustWindowRectEx(&bounds, GetWindowLong(SDL_Window, GWL_STYLE), FALSE, 0);
   784 		width = bounds.right-bounds.left;
   785 		height = bounds.bottom-bounds.top;
   786 		if ( (flags & SDL_FULLSCREEN) ) {
   787 			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   788 			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   789 		} else if ( center ) {
   790 			x = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
   791 			y = (GetSystemMetrics(SM_CYSCREEN)-height)/2;
   792 		} else if ( SDL_windowX || SDL_windowY || window ) {
   793 			x = bounds.left;
   794 			y = bounds.top;
   795 		} else {
   796 			x = y = -1;
   797 			swp_flags |= SWP_NOMOVE;
   798 		}
   799 		if ( flags & SDL_FULLSCREEN ) {
   800 			top = HWND_TOPMOST;
   801 		} else {
   802 			top = HWND_NOTOPMOST;
   803 		}
   804 		SetWindowPos(SDL_Window, top, x, y, width, height, swp_flags);
   805 		if ( !(flags & SDL_FULLSCREEN) ) {
   806 			SDL_windowX = SDL_bounds.left;
   807 			SDL_windowY = SDL_bounds.top;
   808 		}
   809 		SetForegroundWindow(SDL_Window);
   810 	}
   811 	SDL_resizing = 0;
   812 
   813 	/* Set up for OpenGL */
   814 	if ( flags & SDL_OPENGL ) {
   815 		if ( WIN_GL_SetupWindow(this) < 0 ) {
   816 			return(NULL);
   817 		}
   818 		video->flags |= SDL_OPENGL;
   819 	}
   820 
   821 	/* We're live! */
   822 	return(video);
   823 }
   824 
   825 /* We don't actually allow hardware surfaces in the DIB driver */
   826 static int DIB_AllocHWSurface(_THIS, SDL_Surface *surface)
   827 {
   828 	return(-1);
   829 }
   830 static void DIB_FreeHWSurface(_THIS, SDL_Surface *surface)
   831 {
   832 	return;
   833 }
   834 static int DIB_LockHWSurface(_THIS, SDL_Surface *surface)
   835 {
   836 	return(0);
   837 }
   838 static void DIB_UnlockHWSurface(_THIS, SDL_Surface *surface)
   839 {
   840 	return;
   841 }
   842 
   843 static void DIB_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
   844 {
   845 	HDC hdc, mdc;
   846 	int i;
   847 
   848 	hdc = GetDC(SDL_Window);
   849 	if ( screen_pal ) {
   850 		SelectPalette(hdc, screen_pal, FALSE);
   851 	}
   852 	mdc = CreateCompatibleDC(hdc);
   853 	SelectObject(mdc, screen_bmp);
   854 	for ( i=0; i<numrects; ++i ) {
   855 		BitBlt(hdc, rects[i].x, rects[i].y, rects[i].w, rects[i].h,
   856 					mdc, rects[i].x, rects[i].y, SRCCOPY);
   857 	}
   858 	DeleteDC(mdc);
   859 	ReleaseDC(SDL_Window, hdc);
   860 }
   861 
   862 
   863 int DIB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   864 {
   865 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
   866 	HDC hdc, mdc;
   867 	RGBQUAD *pal;
   868 #else
   869 	HDC hdc;
   870 #endif
   871 	int i;
   872 
   873 	/* Update the display palette */
   874 	hdc = GetDC(SDL_Window);
   875 	if ( screen_pal ) {
   876 		PALETTEENTRY *entries;
   877 
   878 		entries = SDL_stack_alloc(PALETTEENTRY, ncolors);
   879 		for ( i=0; i<ncolors; ++i ) {
   880 			entries[i].peRed   = colors[i].r;
   881 			entries[i].peGreen = colors[i].g;
   882 			entries[i].peBlue  = colors[i].b;
   883 			entries[i].peFlags = PC_NOCOLLAPSE;
   884 		}
   885 		SetPaletteEntries(screen_pal, firstcolor, ncolors, entries);
   886 		SelectPalette(hdc, screen_pal, FALSE);
   887 		RealizePalette(hdc);
   888 		SDL_stack_free(entries);
   889 	}
   890 
   891 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
   892 	/* Copy palette colors into DIB palette */
   893 	pal = SDL_stack_alloc(RGBQUAD, ncolors);
   894 	for ( i=0; i<ncolors; ++i ) {
   895 		pal[i].rgbRed = colors[i].r;
   896 		pal[i].rgbGreen = colors[i].g;
   897 		pal[i].rgbBlue = colors[i].b;
   898 		pal[i].rgbReserved = 0;
   899 	}
   900 
   901 	/* Set the DIB palette and update the display */
   902 	mdc = CreateCompatibleDC(hdc);
   903 	SelectObject(mdc, screen_bmp);
   904 	SetDIBColorTable(mdc, firstcolor, ncolors, pal);
   905 	BitBlt(hdc, 0, 0, this->screen->w, this->screen->h,
   906 	       mdc, 0, 0, SRCCOPY);
   907 	DeleteDC(mdc);
   908 	SDL_stack_free(pal);
   909 #endif
   910 	ReleaseDC(SDL_Window, hdc);
   911 	return(1);
   912 }
   913 
   914 
   915 static void DIB_CheckGamma(_THIS)
   916 {
   917 #ifndef NO_GAMMA_SUPPORT
   918 	HDC hdc;
   919 	WORD ramp[3*256];
   920 
   921 	/* If we fail to get gamma, disable gamma control */
   922 	hdc = GetDC(SDL_Window);
   923 	if ( ! GetDeviceGammaRamp(hdc, ramp) ) {
   924 		this->GetGammaRamp = NULL;
   925 		this->SetGammaRamp = NULL;
   926 	}
   927 	ReleaseDC(SDL_Window, hdc);
   928 #endif /* !NO_GAMMA_SUPPORT */
   929 }
   930 void DIB_SwapGamma(_THIS)
   931 {
   932 #ifndef NO_GAMMA_SUPPORT
   933 	HDC hdc;
   934 
   935 	if ( gamma_saved ) {
   936 		hdc = GetDC(SDL_Window);
   937 		if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
   938 			/* About to leave active state, restore gamma */
   939 			SetDeviceGammaRamp(hdc, gamma_saved);
   940 		} else {
   941 			/* About to enter active state, set game gamma */
   942 			GetDeviceGammaRamp(hdc, gamma_saved);
   943 			SetDeviceGammaRamp(hdc, this->gamma);
   944 		}
   945 		ReleaseDC(SDL_Window, hdc);
   946 	}
   947 #endif /* !NO_GAMMA_SUPPORT */
   948 }
   949 void DIB_QuitGamma(_THIS)
   950 {
   951 #ifndef NO_GAMMA_SUPPORT
   952 	if ( gamma_saved ) {
   953 		/* Restore the original gamma if necessary */
   954 		if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
   955 			HDC hdc;
   956 
   957 			hdc = GetDC(SDL_Window);
   958 			SetDeviceGammaRamp(hdc, gamma_saved);
   959 			ReleaseDC(SDL_Window, hdc);
   960 		}
   961 
   962 		/* Free the saved gamma memory */
   963 		SDL_free(gamma_saved);
   964 		gamma_saved = 0;
   965 	}
   966 #endif /* !NO_GAMMA_SUPPORT */
   967 }
   968 
   969 int DIB_SetGammaRamp(_THIS, Uint16 *ramp)
   970 {
   971 #ifdef NO_GAMMA_SUPPORT
   972 	SDL_SetError("SDL compiled without gamma ramp support");
   973 	return -1;
   974 #else
   975 	HDC hdc;
   976 	BOOL succeeded;
   977 
   978 	/* Set the ramp for the display */
   979 	if ( ! gamma_saved ) {
   980 		gamma_saved = (WORD *)SDL_malloc(3*256*sizeof(*gamma_saved));
   981 		if ( ! gamma_saved ) {
   982 			SDL_OutOfMemory();
   983 			return -1;
   984 		}
   985 		hdc = GetDC(SDL_Window);
   986 		GetDeviceGammaRamp(hdc, gamma_saved);
   987 		ReleaseDC(SDL_Window, hdc);
   988 	}
   989 	if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
   990 		hdc = GetDC(SDL_Window);
   991 		succeeded = SetDeviceGammaRamp(hdc, ramp);
   992 		ReleaseDC(SDL_Window, hdc);
   993 	} else {
   994 		succeeded = TRUE;
   995 	}
   996 	return succeeded ? 0 : -1;
   997 #endif /* !NO_GAMMA_SUPPORT */
   998 }
   999 
  1000 int DIB_GetGammaRamp(_THIS, Uint16 *ramp)
  1001 {
  1002 #ifdef NO_GAMMA_SUPPORT
  1003 	SDL_SetError("SDL compiled without gamma ramp support");
  1004 	return -1;
  1005 #else
  1006 	HDC hdc;
  1007 	BOOL succeeded;
  1008 
  1009 	/* Get the ramp from the display */
  1010 	hdc = GetDC(SDL_Window);
  1011 	succeeded = GetDeviceGammaRamp(hdc, ramp);
  1012 	ReleaseDC(SDL_Window, hdc);
  1013 	return succeeded ? 0 : -1;
  1014 #endif /* !NO_GAMMA_SUPPORT */
  1015 }
  1016 
  1017 static void FlushMessageQueue()
  1018 {
  1019 	MSG  msg;
  1020 	while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) {
  1021 		if ( msg.message == WM_QUIT ) break;
  1022 		TranslateMessage( &msg );
  1023 		DispatchMessage( &msg );
  1024 	}
  1025 }
  1026 
  1027 void DIB_VideoQuit(_THIS)
  1028 {
  1029 	/* Destroy the window and everything associated with it */
  1030 	if ( SDL_Window ) {
  1031 		/* Delete the screen bitmap (also frees screen->pixels) */
  1032 		if ( this->screen ) {
  1033 #ifndef NO_CHANGEDISPLAYSETTINGS
  1034 			if ( this->screen->flags & SDL_FULLSCREEN ) {
  1035 				ChangeDisplaySettings(NULL, 0);
  1036 				ShowWindow(SDL_Window, SW_HIDE);
  1037 			}
  1038 #endif
  1039 			if ( this->screen->flags & SDL_OPENGL ) {
  1040 				WIN_GL_ShutDown(this);
  1041 			}
  1042 			this->screen->pixels = NULL;
  1043 		}
  1044 		if ( screen_bmp ) {
  1045 			DeleteObject(screen_bmp);
  1046 			screen_bmp = NULL;
  1047 		}
  1048 		if ( screen_icn ) {
  1049 			DestroyIcon(screen_icn);
  1050 			screen_icn = NULL;
  1051 		}
  1052 		DIB_QuitGamma(this);
  1053 		DIB_DestroyWindow(this);
  1054 		FlushMessageQueue();
  1055 
  1056 		SDL_Window = NULL;
  1057 
  1058 #if defined(_WIN32_WCE)
  1059 
  1060 // Unload wince aygshell library to prevent leak
  1061 		if( aygshell ) 
  1062 		{
  1063 			FreeLibrary(aygshell);
  1064 			aygshell = NULL;
  1065 		}
  1066 #endif
  1067 
  1068 	}
  1069 }
  1070 
  1071 /* Exported for the windows message loop only */
  1072 static void DIB_FocusPalette(_THIS, int foreground)
  1073 {
  1074 	if ( screen_pal != NULL ) {
  1075 		HDC hdc;
  1076 
  1077 		hdc = GetDC(SDL_Window);
  1078 		SelectPalette(hdc, screen_pal, FALSE);
  1079 		if ( RealizePalette(hdc) )
  1080 			InvalidateRect(SDL_Window, NULL, FALSE);
  1081 		ReleaseDC(SDL_Window, hdc);
  1082 	}
  1083 }
  1084 static void DIB_RealizePalette(_THIS)
  1085 {
  1086 	DIB_FocusPalette(this, 1);
  1087 }
  1088 static void DIB_PaletteChanged(_THIS, HWND window)
  1089 {
  1090 	if ( window != SDL_Window ) {
  1091 		DIB_FocusPalette(this, 0);
  1092 	}
  1093 }
  1094 
  1095 /* Exported for the windows message loop only */
  1096 static void DIB_WinPAINT(_THIS, HDC hdc)
  1097 {
  1098 	HDC mdc;
  1099 
  1100 	if ( screen_pal ) {
  1101 		SelectPalette(hdc, screen_pal, FALSE);
  1102 	}
  1103 	mdc = CreateCompatibleDC(hdc);
  1104 	SelectObject(mdc, screen_bmp);
  1105 	BitBlt(hdc, 0, 0, SDL_VideoSurface->w, SDL_VideoSurface->h,
  1106 							mdc, 0, 0, SRCCOPY);
  1107 	DeleteDC(mdc);
  1108 }
  1109 
  1110 /* Stub in case DirectX isn't available */
  1111 #if !SDL_AUDIO_DRIVER_DSOUND
  1112 void DX5_SoundFocus(HWND hwnd)
  1113 {
  1114 	return;
  1115 }
  1116 #endif