4 * The game mode handling code.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Thu Dec 16 1999
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #define G_LOG_DOMAIN "freeglut-gamemode"
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * glutGameModeString() -- missing
41 * glutEnterGameMode() -- X11 version
42 * glutLeaveGameMode() -- is that correct?
43 * glutGameModeGet() -- is that correct?
47 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
50 * Remembers the current visual settings, so that
51 * we can change them and restore later...
53 void fghRememberState( void )
55 #if TARGET_HOST_UNIX_X11
58 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
60 # ifdef X_XF86VidModeGetModeLine
63 * Query the current display settings:
65 XF86VidModeGetModeLine(
68 &fgDisplay.DisplayModeClock,
69 &fgDisplay.DisplayMode
73 # warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
76 #elif TARGET_HOST_WIN32
81 * Grab the current desktop settings...
84 /* hack to get around my stupid cross-gcc headers */
85 #define ENUM_CURRENT_SETTINGS -1
87 EnumDisplaySettings( NULL, ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode );
90 * Make sure we will be restoring all settings needed
92 fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
98 * Restores the previously remembered visual settings
100 void fghRestoreState( void )
102 #if TARGET_HOST_UNIX_X11
105 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
107 # ifdef X_XF86VidModeGetAllModeLines
109 XF86VidModeModeInfo** displayModes;
110 int i, displayModesCount;
113 * Query for all the display available...
115 XF86VidModeGetAllModeLines(
123 * Check every of the modes looking for one that matches our demands
125 for( i=0; i<displayModesCount; i++ )
127 if( displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
128 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
129 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
132 * OKi, this is the display mode we have been looking for...
134 XF86VidModeSwitchToMode(
145 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
148 #elif TARGET_HOST_WIN32
151 * Restore the previously rememebered desktop display settings
153 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
159 * Checks the display mode settings against user's preferences
161 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
164 * The desired values should be stored in fgState structure...
166 return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
167 (depth == fgState.GameModeDepth) && (refresh == fgState.GameModeRefresh) );
171 * Changes the current display mode to match user's settings
173 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
175 #if TARGET_HOST_UNIX_X11
178 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
180 # ifdef X_XF86VidModeGetAllModeLines
182 XF86VidModeModeInfo** displayModes;
183 int i, displayModesCount;
186 * Query for all the display available...
188 XF86VidModeGetAllModeLines(
196 * Check every of the modes looking for one that matches our demands
198 for( i=0; i<displayModesCount; i++ )
200 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
201 fgState.GameModeDepth, fgState.GameModeRefresh ) )
204 * OKi, this is the display mode we have been looking for...
206 XF86VidModeSwitchToMode(
213 * Set the viewport's origin to (0,0) (the game mode window's top-left corner)
215 XF86VidModeSetViewPort(
223 * Return successfull...
230 * Something must have went wrong
235 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
238 #elif TARGET_HOST_WIN32
240 unsigned int displayModes = 0, mode = 0xffffffff;
241 GLboolean success = FALSE;
246 * Enumerate the available display modes
248 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
251 * Does the enumerated display mode match the user's preferences?
253 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
254 devMode.dmBitsPerPel, fgState.GameModeRefresh ) )
257 * OKi, we've found a matching display mode, remember it's number and break
264 * Switch to the next display mode, if any
270 * Did we find a matching display mode?
272 if( mode != 0xffffffff )
274 int retVal = DISP_CHANGE_SUCCESSFUL;
277 * Mark the values we want to modify in the display change call
279 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
282 * Change the current display mode (possibly in test mode only)
284 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
287 * I don't know if it's really needed, but looks nice:
289 success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
292 * If it was not a test, remember the current screen settings
294 if( !haveToTest && success )
296 fgState.GameModeSize.X = devMode.dmPelsWidth;
297 fgState.GameModeSize.Y = devMode.dmPelsHeight;
298 fgState.GameModeDepth = devMode.dmBitsPerPel;
299 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
304 * Otherwise we must have failed somewhere
312 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
315 * Sets the game mode display string
317 void FGAPIENTRY glutGameModeString( const char* string )
319 int width = 640, height = 480, depth = 16, refresh = 72;
322 * This one seems a bit easier than glutInitDisplayString. The bad thing
323 * about it that I was unable to find the game mode string definition, so
324 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
325 * appears in all GLUT game mode programs I have seen to date.
327 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
328 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
329 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
330 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
331 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
332 if( sscanf( string, ":%i", &depth ) != 1 )
333 if( sscanf( string, "@%i", &refresh ) != 1 )
334 fgWarning( "unable to parse game mode string `%s'", string );
337 * Hopefully it worked, and if not, we still have the default values
339 fgState.GameModeSize.X = width;
340 fgState.GameModeSize.Y = height;
341 fgState.GameModeDepth = depth;
342 fgState.GameModeRefresh = refresh;
346 * Enters the game mode
348 int FGAPIENTRY glutEnterGameMode( void )
351 * Check if a game mode window already exists...
353 if( fgStructure.GameMode != NULL )
356 * ...if so, delete it before proceeding...
358 fgDestroyWindow( fgStructure.GameMode, TRUE );
363 * ...otherwise remember the current resolution, etc.
369 * We are ready to change the current screen's resolution now
371 if( fghChangeDisplayMode( FALSE ) == FALSE )
373 fgWarning( "failed to change screen settings" );
378 * Finally, have the game mode window created
380 fgStructure.GameMode = fgCreateWindow(
381 NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
384 #if TARGET_HOST_UNIX_X11
387 * Move the mouse pointer over the game mode window
391 fgStructure.GameMode->Window.Handle,
397 * Confine the mouse pointer to the window's client area
401 fgStructure.GameMode->Window.Handle,
403 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask,
404 GrabModeAsync, GrabModeAsync,
405 fgStructure.GameMode->Window.Handle,
411 * Grab the keyboard, too
415 fgStructure.GameMode->Window.Handle,
417 GrabModeAsync, GrabModeAsync,
430 * Leaves the game mode
432 void FGAPIENTRY glutLeaveGameMode( void )
434 freeglut_return_if_fail( fgStructure.GameMode != NULL );
437 * First of all, have the game mode window created
439 fgDestroyWindow( fgStructure.GameMode, TRUE );
441 #if TARGET_HOST_UNIX_X11
444 * Ungrab the mouse and keyboard
446 XUngrabPointer( fgDisplay.Display, CurrentTime );
447 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
452 * Then, have the desktop visual settings restored
458 * Returns information concerning the freeglut game mode
460 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
463 * See why are we bothered
467 case GLUT_GAME_MODE_ACTIVE:
469 * Check if the game mode is currently active
471 return( fgStructure.GameMode != NULL );
473 case GLUT_GAME_MODE_POSSIBLE:
475 * Check if the current game mode settings are valid
477 return( fghChangeDisplayMode( TRUE ) );
479 case GLUT_GAME_MODE_WIDTH:
481 * The game mode screen width
483 return( fgState.GameModeSize.X );
485 case GLUT_GAME_MODE_HEIGHT:
487 * The game mode screen height
489 return( fgState.GameModeSize.Y );
491 case GLUT_GAME_MODE_PIXEL_DEPTH:
493 * The game mode pixel depth
495 return( fgState.GameModeDepth );
497 case GLUT_GAME_MODE_REFRESH_RATE:
499 * The game mode refresh rate
501 return( fgState.GameModeRefresh );
503 case GLUT_GAME_MODE_DISPLAY_CHANGED:
505 * This is true if the game mode has been activated successfully..
507 return( fgStructure.GameMode != NULL );
513 /*** END OF FILE ***/