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...
83 EnumDisplaySettings( NULL, ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode );
86 * Make sure we will be restoring all settings needed
88 fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
94 * Restores the previously remembered visual settings
96 void fghRestoreState( void )
98 #if TARGET_HOST_UNIX_X11
101 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
103 # ifdef X_XF86VidModeGetAllModeLines
105 XF86VidModeModeInfo** displayModes;
106 int i, displayModesCount;
109 * Query for all the display available...
111 XF86VidModeGetAllModeLines(
119 * Check every of the modes looking for one that matches our demands
121 for( i=0; i<displayModesCount; i++ )
123 if( displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
124 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
125 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
128 * OKi, this is the display mode we have been looking for...
130 XF86VidModeSwitchToMode(
141 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
144 #elif TARGET_HOST_WIN32
147 * Restore the previously rememebered desktop display settings
149 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
155 * Checks the display mode settings against user's preferences
157 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
160 * The desired values should be stored in fgState structure...
162 return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
163 (depth == fgState.GameModeDepth) && (refresh == fgState.GameModeRefresh) );
167 * Changes the current display mode to match user's settings
169 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
171 #if TARGET_HOST_UNIX_X11
174 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
176 # ifdef X_XF86VidModeGetAllModeLines
178 XF86VidModeModeInfo** displayModes;
179 int i, displayModesCount;
182 * Query for all the display available...
184 XF86VidModeGetAllModeLines(
192 * Check every of the modes looking for one that matches our demands
194 for( i=0; i<displayModesCount; i++ )
196 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
197 fgState.GameModeDepth, fgState.GameModeRefresh ) )
200 * OKi, this is the display mode we have been looking for...
202 XF86VidModeSwitchToMode(
209 * Set the viewport's origin to (0,0) (the game mode window's top-left corner)
211 XF86VidModeSetViewPort(
219 * Return successfull...
226 * Something must have went wrong
231 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
234 #elif TARGET_HOST_WIN32
236 guint displayModes = 0, mode = 0xffffffff;
237 gboolean success = FALSE;
242 * Enumerate the available display modes
244 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
247 * Does the enumerated display mode match the user's preferences?
249 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
250 devMode.dmBitsPerPel, fgState.GameModeRefresh ) )
253 * OKi, we've found a matching display mode, remember it's number and break
260 * Switch to the next display mode, if any
266 * Did we find a matching display mode?
268 if( mode != 0xffffffff )
270 int retVal = DISP_CHANGE_SUCCESSFUL;
273 * Mark the values we want to modify in the display change call
275 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
278 * Change the current display mode (possibly in test mode only)
280 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
283 * I don't know if it's really needed, but looks nice:
285 success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
288 * If it was not a test, remember the current screen settings
290 if( !haveToTest && success )
292 fgState.GameModeSize.X = devMode.dmPelsWidth;
293 fgState.GameModeSize.Y = devMode.dmPelsHeight;
294 fgState.GameModeDepth = devMode.dmBitsPerPel;
295 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
300 * Otherwise we must have failed somewhere
308 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
311 * Sets the game mode display string
313 void FGAPIENTRY glutGameModeString( const char* string )
315 int width = 640, height = 480, depth = 16, refresh = 72;
318 * This one seems a bit easier than glutInitDisplayString. The bad thing
319 * about it that I was unable to find the game mode string definition, so
320 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
321 * appears in all GLUT game mode programs I have seen to date.
323 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
324 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
325 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
326 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
327 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
328 if( sscanf( string, ":%i", &depth ) != 1 )
329 if( sscanf( string, "@%i", &refresh ) != 1 )
330 fgWarning( "unable to parse game mode string `%s'", string );
333 * Hopefully it worked, and if not, we still have the default values
335 fgState.GameModeSize.X = width;
336 fgState.GameModeSize.Y = height;
337 fgState.GameModeDepth = depth;
338 fgState.GameModeRefresh = refresh;
342 * Enters the game mode
344 int FGAPIENTRY glutEnterGameMode( void )
347 * Check if a game mode window already exists...
349 if( fgStructure.GameMode != NULL )
352 * ...if so, delete it before proceeding...
354 fgDestroyWindow( fgStructure.GameMode, TRUE );
359 * ...otherwise remember the current resolution, etc.
365 * We are ready to change the current screen's resolution now
367 if( fghChangeDisplayMode( FALSE ) == FALSE )
369 fgWarning( "failed to change screen settings" );
374 * Finally, have the game mode window created
376 fgStructure.GameMode = fgCreateWindow(
377 NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
380 #if TARGET_HOST_UNIX_X11
383 * Move the mouse pointer over the game mode window
387 fgStructure.GameMode->Window.Handle,
393 * Confine the mouse pointer to the window's client area
397 fgStructure.GameMode->Window.Handle,
399 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask,
400 GrabModeAsync, GrabModeAsync,
401 fgStructure.GameMode->Window.Handle,
407 * Grab the keyboard, too
411 fgStructure.GameMode->Window.Handle,
413 GrabModeAsync, GrabModeAsync,
426 * Leaves the game mode
428 void FGAPIENTRY glutLeaveGameMode( void )
430 freeglut_return_if_fail( fgStructure.GameMode != NULL );
433 * First of all, have the game mode window created
435 fgDestroyWindow( fgStructure.GameMode, TRUE );
437 #if TARGET_HOST_UNIX_X11
440 * Ungrab the mouse and keyboard
442 XUngrabPointer( fgDisplay.Display, CurrentTime );
443 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
448 * Then, have the desktop visual settings restored
454 * Returns information concerning the freeglut game mode
456 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
459 * See why are we bothered
463 case GLUT_GAME_MODE_ACTIVE:
465 * Check if the game mode is currently active
467 return( fgStructure.GameMode != NULL );
469 case GLUT_GAME_MODE_POSSIBLE:
471 * Check if the current game mode settings are valid
473 return( fghChangeDisplayMode( TRUE ) );
475 case GLUT_GAME_MODE_WIDTH:
477 * The game mode screen width
479 return( fgState.GameModeSize.X );
481 case GLUT_GAME_MODE_HEIGHT:
483 * The game mode screen height
485 return( fgState.GameModeSize.Y );
487 case GLUT_GAME_MODE_PIXEL_DEPTH:
489 * The game mode pixel depth
491 return( fgState.GameModeDepth );
493 case GLUT_GAME_MODE_REFRESH_RATE:
495 * The game mode refresh rate
497 return( fgState.GameModeRefresh );
499 case GLUT_GAME_MODE_DISPLAY_CHANGED:
501 * This is true if the game mode has been activated successfully..
503 return( fgStructure.GameMode != NULL );
509 /*** END OF FILE ***/