X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Ffreeglut_gamemode.c;h=313f546fd205fd7590e7620b42fbb5ce54c95250;hb=63f212e2f8143b6220b755abc12cf76e77a75aed;hp=7c9b7eb477c0c04bac14f7ce3a3d8b6e0cf81031;hpb=113c06a96687cf466979972f5780c3ddd4d5dee5;p=freeglut diff --git a/src/freeglut_gamemode.c b/src/freeglut_gamemode.c index 7c9b7eb..313f546 100644 --- a/src/freeglut_gamemode.c +++ b/src/freeglut_gamemode.c @@ -25,11 +25,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "../include/GL/freeglut.h" +#include #include "freeglut_internal.h" /* @@ -48,7 +44,7 @@ * Remembers the current visual settings, so that * we can change them and restore later... */ -void fghRememberState( void ) +static void fghRememberState( void ) { #if TARGET_HOST_UNIX_X11 @@ -63,12 +59,12 @@ void fghRememberState( void ) * Remember the current ViewPort location of the screen to be able to * restore the ViewPort on LeaveGameMode(): */ - XF86VidModeGetViewPort( - fgDisplay.Display, - fgDisplay.Screen, - &fgDisplay.DisplayViewPortX, - &fgDisplay.DisplayViewPortY - ); + if( !XF86VidModeGetViewPort( + fgDisplay.Display, + fgDisplay.Screen, + &fgDisplay.DisplayViewPortX, + &fgDisplay.DisplayViewPortY ) ) + fgWarning( "XF86VidModeGetViewPort failed" ); /* * Remember the current pointer location before going fullscreen @@ -86,10 +82,8 @@ void fghRememberState( void ) ); } - /* - * Query the current display settings: - */ - fgDisplay.DisplayModeValid = + /* Query the current display settings: */ + fgDisplay.DisplayModeValid = XF86VidModeGetModeLine( fgDisplay.Display, fgDisplay.Screen, @@ -98,19 +92,20 @@ void fghRememberState( void ) ); if( !fgDisplay.DisplayModeValid ) - fgWarning( "Runtime use of XF86VidModeGetModeLine failed.\n" ); + fgWarning( "XF86VidModeGetModeLine failed" ); # else -# warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated + /* + * XXX warning fghRememberState: missing XFree86 video mode extensions, + * XXX game mode will not change screen resolution when activated + */ # endif -#elif TARGET_HOST_WIN32 +#elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE /* DEVMODE devMode; */ - - /* - * Grab the current desktop settings... - */ + + /* Grab the current desktop settings... */ /* hack to get around my stupid cross-gcc headers */ #define FREEGLUT_ENUM_CURRENT_SETTINGS -1 @@ -118,9 +113,7 @@ void fghRememberState( void ) EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode ); - /* - * Make sure we will be restoring all settings needed - */ + /* Make sure we will be restoring all settings needed */ fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY; @@ -130,14 +123,12 @@ void fghRememberState( void ) /* * Restores the previously remembered visual settings */ -void fghRestoreState( void ) +static void fghRestoreState( void ) { #if TARGET_HOST_UNIX_X11 # ifdef X_XF86VidModeGetAllModeLines - /* - * Restore the remembered pointer position: - */ + /* Restore the remembered pointer position: */ XWarpPointer( fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0, fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY @@ -153,12 +144,16 @@ void fghRestoreState( void ) XF86VidModeModeInfo** displayModes; int i, displayModesCount; - XF86VidModeGetAllModeLines( - fgDisplay.Display, - fgDisplay.Screen, - &displayModesCount, - &displayModes - ); + if( !XF86VidModeGetAllModeLines( + fgDisplay.Display, + fgDisplay.Screen, + &displayModesCount, + &displayModes ) ) + { + fgWarning( "XF86VidModeGetAllModeLines failed" ); + return; + } + /* * Check every of the modes looking for one that matches our demands. @@ -170,17 +165,22 @@ void fghRestoreState( void ) displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay && displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock ) { - XF86VidModeSwitchToMode( - fgDisplay.Display, - fgDisplay.Screen, - displayModes[ i ] - ); - XF86VidModeSetViewPort( - fgDisplay.Display, - fgDisplay.Screen, - fgDisplay.DisplayViewPortX, - fgDisplay.DisplayViewPortY - ); + if( !XF86VidModeSwitchToMode( + fgDisplay.Display, + fgDisplay.Screen, + displayModes[ i ] ) ) + { + fgWarning( "XF86VidModeSwitchToMode failed" ); + break; + } + + if( !XF86VidModeSetViewPort( + fgDisplay.Display, + fgDisplay.Screen, + fgDisplay.DisplayViewPortX, + fgDisplay.DisplayViewPortY ) ) + fgWarning( "XF86VidModeSetViewPort failed" ); + /* * For the case this would be the last X11 call the application @@ -189,20 +189,22 @@ void fghRestoreState( void ) */ XFlush( fgDisplay.Display ); - return; + break; } } + XFree( displayModes ); } # else -# warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated + /* + * XXX warning fghRestoreState: missing XFree86 video mode extensions, + * XXX game mode will not change screen resolution when activated + */ # endif -#elif TARGET_HOST_WIN32 +#elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE - /* - * Restore the previously rememebered desktop display settings - */ + /* Restore the previously rememebered desktop display settings */ ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 ); #endif @@ -211,11 +213,9 @@ void fghRestoreState( void ) /* * Checks the display mode settings against user's preferences */ -GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh ) +static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh ) { - /* - * The desired values should be stored in fgState structure... - */ + /* The desired values should be stored in fgState structure... */ return ( width == fgState.GameModeSize.X ) && ( height == fgState.GameModeSize.Y ) && ( depth == fgState.GameModeDepth ) && @@ -225,8 +225,9 @@ GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh ) /* * Changes the current display mode to match user's settings */ -GLboolean fghChangeDisplayMode( GLboolean haveToTest ) +static GLboolean fghChangeDisplayMode( GLboolean haveToTest ) { + GLboolean success = GL_FALSE; #if TARGET_HOST_UNIX_X11 /* @@ -242,53 +243,66 @@ GLboolean fghChangeDisplayMode( GLboolean haveToTest ) if( haveToTest || fgDisplay.DisplayModeValid ) { XF86VidModeModeInfo** displayModes; - int i, displayModesCount; + int i, ignoreRefreshRate, displayModesCount; + + if( !XF86VidModeGetAllModeLines( + fgDisplay.Display, + fgDisplay.Screen, + &displayModesCount, + &displayModes ) ) + { + fgWarning( "XF86VidModeGetAllModeLines failed" ); + return success; + } - XF86VidModeGetAllModeLines( - fgDisplay.Display, - fgDisplay.Screen, - &displayModesCount, - &displayModes - ); /* - * Check every of the modes looking for one that matches our demands + * Check every of the modes looking for one that matches our demands, + * ignoring the refresh rate if no exact match could be found. */ - for( i = 0; i < displayModesCount; i++ ) + for( ignoreRefreshRate = 0; + !success && ( ignoreRefreshRate <= 1 ); + ignoreRefreshRate++) { - if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, - displayModes[ i ]->vdisplay, - fgState.GameModeDepth, - fgState.GameModeRefresh ) ) + for( i = 0; + !success && ( i < displayModesCount ); + i++ ) { - if( haveToTest ) - return GL_TRUE; - /* - * OKi, this is the display mode we have been looking for... - */ - XF86VidModeSwitchToMode( - fgDisplay.Display, - fgDisplay.Screen, - displayModes[ i ] - ); - return GL_TRUE; + /* Compute the displays refresh rate, dotclock comes in kHz. */ + int refresh = ( displayModes[ i ]->dotclock * 1000 ) / + ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal ); + + success = fghCheckDisplayMode( displayModes[ i ]->hdisplay, + displayModes[ i ]->vdisplay, + fgState.GameModeDepth, + ( ignoreRefreshRate ? fgState.GameModeRefresh : refresh ) ); } } + + if( !haveToTest && success ) { + if( !XF86VidModeSwitchToMode( + fgDisplay.Display, + fgDisplay.Screen, + displayModes[ i ] ) ) + fgWarning( "XF86VidModeSwitchToMode failed" ); + } + + XFree( displayModes ); } +# else + /* - * Something must have went wrong + * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions, + * XXX game mode will not change screen resolution when activated */ - return GL_FALSE; + success = GL_TRUE; -# else -# warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated # endif -#elif TARGET_HOST_WIN32 +#elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE unsigned int displayModes = 0, mode = 0xffffffff; - GLboolean success = GL_FALSE; /* HDC desktopDC; */ DEVMODE devMode; @@ -298,9 +312,7 @@ GLboolean fghChangeDisplayMode( GLboolean haveToTest ) */ while( EnumDisplaySettings( NULL, displayModes, &devMode ) ) { - /* - * Does the enumerated display mode match the user's preferences? - */ + /* Does the enumerated display mode match the user's preferences? */ if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmDisplayFrequency ) ) @@ -316,9 +328,7 @@ GLboolean fghChangeDisplayMode( GLboolean haveToTest ) /* then try without Display Frequency */ displayModes = 0; - /* - * Enumerate the available display modes - */ + /* Enumerate the available display modes */ while( EnumDisplaySettings( NULL, displayModes, &devMode ) ) { /* then try without Display Frequency */ @@ -334,24 +344,18 @@ GLboolean fghChangeDisplayMode( GLboolean haveToTest ) } } - /* - * Did we find a matching display mode? - */ + /* Did we find a matching display mode? */ if( mode != 0xffffffff ) { int retVal = DISP_CHANGE_SUCCESSFUL; - /* - * Mark the values we want to modify in the display change call - */ + /* Mark the values we want to modify in the display change call */ devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY; retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 ); - /* - * I don't know if it's really needed, but looks nice: - */ + /* I don't know if it's really needed, but looks nice: */ success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED); @@ -364,9 +368,9 @@ GLboolean fghChangeDisplayMode( GLboolean haveToTest ) } } - return success; - #endif + + return success; } @@ -379,6 +383,8 @@ void FGAPIENTRY glutGameModeString( const char* string ) { int width = 640, height = 480, depth = 16, refresh = 72; + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" ); + /* * This one seems a bit easier than glutInitDisplayString. The bad thing * about it that I was unable to find the game mode string definition, so @@ -398,9 +404,7 @@ void FGAPIENTRY glutGameModeString( const char* string ) string ); - /* - * Hopefully it worked, and if not, we still have the default values - */ + /* Hopefully it worked, and if not, we still have the default values */ fgState.GameModeSize.X = width; fgState.GameModeSize.Y = height; fgState.GameModeDepth = depth; @@ -412,8 +416,10 @@ void FGAPIENTRY glutGameModeString( const char* string ) */ int FGAPIENTRY glutEnterGameMode( void ) { + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" ); + if( fgStructure.GameMode ) - fgAddToWindowDestroyList( fgStructure.GameMode, GL_TRUE ); + fgAddToWindowDestroyList( fgStructure.GameMode ); else fghRememberState( ); @@ -423,15 +429,14 @@ int FGAPIENTRY glutEnterGameMode( void ) return FALSE; } - fgStructure.GameMode = fgCreateWindow( + fgStructure.GameMode = fgCreateWindow( NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, GL_TRUE, GL_FALSE ); -#if TARGET_HOST_UNIX_X11 + fgStructure.GameMode->State.IsGameMode = GL_TRUE; - /* Move the window up to the topleft corner */ - XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0 ); +#if TARGET_HOST_UNIX_X11 /* * Sync needed to avoid a real race, the Xserver must have really created @@ -439,15 +444,6 @@ int FGAPIENTRY glutEnterGameMode( void ) */ XSync( fgDisplay.Display, False ); - /* Move the Pointer to the middle of the fullscreen window */ - XWarpPointer( - fgDisplay.Display, - None, - fgDisplay.RootWindow, - 0, 0, 0, 0, - fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2 - ); - /* * Grab the pointer to confine it into the window after the calls to * XWrapPointer() which ensure that the pointer really enters the window. @@ -465,7 +461,7 @@ int FGAPIENTRY glutEnterGameMode( void ) GrabModeAsync, GrabModeAsync, fgStructure.GameMode->Window.Handle, None, CurrentTime) ) usleep( 100 ); - + /* * Change input focus to the new window. This will exit the application * if the new window is not viewable yet, see the XGrabPointer loop above. @@ -477,6 +473,15 @@ int FGAPIENTRY glutEnterGameMode( void ) CurrentTime ); + /* Move the Pointer to the middle of the fullscreen window */ + XWarpPointer( + fgDisplay.Display, + None, + fgDisplay.RootWindow, + 0, 0, 0, 0, + fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2 + ); + # ifdef X_XF86VidModeSetViewPort if( fgDisplay.DisplayModeValid ) @@ -484,10 +489,9 @@ int FGAPIENTRY glutEnterGameMode( void ) int x, y; Window child; - /* - * Change to viewport to the window topleft edge: - */ - XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 ); + /* Change to viewport to the window topleft edge: */ + if( !XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 ) ) + fgWarning( "XF86VidModeSetViewPort failed" ); /* * Final window repositioning: It could be avoided using an undecorated @@ -498,22 +502,20 @@ int FGAPIENTRY glutEnterGameMode( void ) /* Get the current postion of the drawable area on screen */ XTranslateCoordinates( fgDisplay.Display, - fgStructure.Window->Window.Handle, + fgStructure.CurrentWindow->Window.Handle, fgDisplay.RootWindow, 0, 0, &x, &y, &child ); /* Move the decorataions out of the topleft corner of the display */ - XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, + XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle, -x, -y); } #endif - /* - * Grab the keyboard, too - */ + /* Grab the keyboard, too */ XGrabKeyboard( fgDisplay.Display, fgStructure.GameMode->Window.Handle, @@ -532,9 +534,14 @@ int FGAPIENTRY glutEnterGameMode( void ) */ void FGAPIENTRY glutLeaveGameMode( void ) { + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" ); + freeglut_return_if_fail( fgStructure.GameMode ); - fgAddToWindowDestroyList( fgStructure.GameMode, GL_TRUE ); + fgStructure.GameMode->State.IsGameMode = GL_FALSE; + + fgAddToWindowDestroyList( fgStructure.GameMode ); + fgStructure.GameMode = NULL; #if TARGET_HOST_UNIX_X11 @@ -551,6 +558,8 @@ void FGAPIENTRY glutLeaveGameMode( void ) */ int FGAPIENTRY glutGameModeGet( GLenum eWhat ) { + FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" ); + switch( eWhat ) { case GLUT_GAME_MODE_ACTIVE: @@ -578,6 +587,7 @@ int FGAPIENTRY glutGameModeGet( GLenum eWhat ) return !!fgStructure.GameMode; } + fgWarning( "Unknown gamemode get: %d", eWhat ); return -1; }