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 "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 FREEGLUT_ENUM_CURRENT_SETTINGS -1
87 EnumDisplaySettings( NULL, FREEGLUT_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(
141 * In case this will be the last X11 call we do before exit,
142 * we've to flush the X11 output queue to be sure the command
143 * is really brought onto it's way to the X server.
144 * The application should not do this because it
145 * would not be platform independent then.
147 XFlush(fgDisplay.Display);
154 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
157 #elif TARGET_HOST_WIN32
160 * Restore the previously rememebered desktop display settings
162 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
168 * Checks the display mode settings against user's preferences
170 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
173 * The desired values should be stored in fgState structure...
175 return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
176 (depth == fgState.GameModeDepth) && (refresh == fgState.GameModeRefresh) );
180 * Changes the current display mode to match user's settings
182 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
184 #if TARGET_HOST_UNIX_X11
187 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
189 # ifdef X_XF86VidModeGetAllModeLines
191 XF86VidModeModeInfo** displayModes;
192 int i, displayModesCount;
195 * Query for all the display available...
197 XF86VidModeGetAllModeLines(
205 * Check every of the modes looking for one that matches our demands
207 for( i=0; i<displayModesCount; i++ )
209 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
210 fgState.GameModeDepth, fgState.GameModeRefresh ) )
215 * OKi, this is the display mode we have been looking for...
217 XF86VidModeSwitchToMode(
224 * Set the viewport's origin to (0,0) (the game mode window's top-left corner)
226 XF86VidModeSetViewPort(
234 * Return successfull...
241 * Something must have went wrong
246 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
249 #elif TARGET_HOST_WIN32
251 unsigned int displayModes = 0, mode = 0xffffffff;
252 GLboolean success = FALSE;
257 * Enumerate the available display modes
259 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
262 * Does the enumerated display mode match the user's preferences?
264 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
265 devMode.dmBitsPerPel, fgState.GameModeRefresh ) )
268 * OKi, we've found a matching display mode, remember its number and break
275 * Switch to the next display mode, if any
281 * Did we find a matching display mode?
283 if( mode != 0xffffffff )
285 int retVal = DISP_CHANGE_SUCCESSFUL;
288 * Mark the values we want to modify in the display change call
290 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
293 * Change the current display mode (possibly in test mode only)
295 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
298 * I don't know if it's really needed, but looks nice:
300 success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
303 * If it was not a test, remember the current screen settings
305 if( !haveToTest && success )
307 fgState.GameModeSize.X = devMode.dmPelsWidth;
308 fgState.GameModeSize.Y = devMode.dmPelsHeight;
309 fgState.GameModeDepth = devMode.dmBitsPerPel;
310 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
315 * Otherwise we must have failed somewhere
323 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
326 * Sets the game mode display string
328 void FGAPIENTRY glutGameModeString( const char* string )
330 int width = 640, height = 480, depth = 16, refresh = 72;
333 * This one seems a bit easier than glutInitDisplayString. The bad thing
334 * about it that I was unable to find the game mode string definition, so
335 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
336 * appears in all GLUT game mode programs I have seen to date.
338 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
339 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
340 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
341 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
342 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
343 if( sscanf( string, ":%i", &depth ) != 1 )
344 if( sscanf( string, "@%i", &refresh ) != 1 )
345 fgWarning( "unable to parse game mode string `%s'", string );
348 * Hopefully it worked, and if not, we still have the default values
350 fgState.GameModeSize.X = width;
351 fgState.GameModeSize.Y = height;
352 fgState.GameModeDepth = depth;
353 fgState.GameModeRefresh = refresh;
357 * Enters the game mode
359 int FGAPIENTRY glutEnterGameMode( void )
362 * Check if a game mode window already exists...
364 if( fgStructure.GameMode != NULL )
367 * ...if so, delete it before proceeding...
369 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
374 * ...otherwise remember the current resolution, etc.
380 * We are ready to change the current screen's resolution now
382 if( fghChangeDisplayMode( FALSE ) == FALSE )
384 fgWarning( "failed to change screen settings" );
389 * Finally, have the game mode window created
391 fgStructure.GameMode = fgCreateWindow(
392 NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
395 #if TARGET_HOST_UNIX_X11
398 * Move the mouse pointer over the game mode window
402 fgStructure.GameMode->Window.Handle,
408 * Confine the mouse pointer to the window's client area
412 fgStructure.GameMode->Window.Handle,
414 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask,
415 GrabModeAsync, GrabModeAsync,
416 fgStructure.GameMode->Window.Handle,
422 * Grab the keyboard, too
426 fgStructure.GameMode->Window.Handle,
428 GrabModeAsync, GrabModeAsync,
441 * Leaves the game mode
443 void FGAPIENTRY glutLeaveGameMode( void )
445 freeglut_return_if_fail( fgStructure.GameMode != NULL );
448 * First of all, have the game mode window destroyed
450 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
452 #if TARGET_HOST_UNIX_X11
455 * Ungrab the mouse and keyboard
457 XUngrabPointer( fgDisplay.Display, CurrentTime );
458 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
463 * Then, have the desktop visual settings restored
469 * Returns information concerning the freeglut game mode
471 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
474 * See why are we bothered
478 case GLUT_GAME_MODE_ACTIVE:
480 * Check if the game mode is currently active
482 return( fgStructure.GameMode != NULL );
484 case GLUT_GAME_MODE_POSSIBLE:
486 * Check if the current game mode settings are valid
488 return( fghChangeDisplayMode( TRUE ) );
490 case GLUT_GAME_MODE_WIDTH:
492 * The game mode screen width
494 return( fgState.GameModeSize.X );
496 case GLUT_GAME_MODE_HEIGHT:
498 * The game mode screen height
500 return( fgState.GameModeSize.Y );
502 case GLUT_GAME_MODE_PIXEL_DEPTH:
504 * The game mode pixel depth
506 return( fgState.GameModeDepth );
508 case GLUT_GAME_MODE_REFRESH_RATE:
510 * The game mode refresh rate
512 return( fgState.GameModeRefresh );
514 case GLUT_GAME_MODE_DISPLAY_CHANGED:
516 * This is true if the game mode has been activated successfully..
518 return( fgStructure.GameMode != NULL );
524 /*** END OF FILE ***/