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 fgDisplay.DisplayModeValid =
66 XF86VidModeGetModeLine(
69 &fgDisplay.DisplayModeClock,
70 &fgDisplay.DisplayMode
73 if (!fgDisplay.DisplayModeValid)
74 fgWarning( "Runtime use of XF86VidModeGetModeLine failed.\n" );
77 # warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
80 #elif TARGET_HOST_WIN32
82 /* DEVMODE devMode; */
85 * Grab the current desktop settings...
88 /* hack to get around my stupid cross-gcc headers */
89 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
91 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode );
94 * Make sure we will be restoring all settings needed
96 fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
102 * Restores the previously remembered visual settings
104 void fghRestoreState( void )
106 #if TARGET_HOST_UNIX_X11
109 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
111 # ifdef X_XF86VidModeGetAllModeLines
113 if (fgDisplay.DisplayModeValid)
115 XF86VidModeModeInfo** displayModes;
116 int i, displayModesCount;
119 * Query for all the display available...
121 XF86VidModeGetAllModeLines(
129 * Check every of the modes looking for one that matches our demands
131 for( i=0; i<displayModesCount; i++ )
133 if( displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
134 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
135 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
138 * OKi, this is the display mode we have been looking for...
140 XF86VidModeSwitchToMode(
147 * In case this will be the last X11 call we do before exit,
148 * we've to flush the X11 output queue to be sure the command
149 * is really brought onto it's way to the X server.
150 * The application should not do this because it
151 * would not be platform independent then.
153 XFlush(fgDisplay.Display);
161 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
164 #elif TARGET_HOST_WIN32
167 * Restore the previously rememebered desktop display settings
169 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
175 * Checks the display mode settings against user's preferences
177 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
180 * The desired values should be stored in fgState structure...
182 return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
183 (depth == fgState.GameModeDepth) && (refresh == fgState.GameModeRefresh) );
187 * Changes the current display mode to match user's settings
189 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
191 #if TARGET_HOST_UNIX_X11
194 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
196 # ifdef X_XF86VidModeGetAllModeLines
198 if (fgDisplay.DisplayModeValid)
200 XF86VidModeModeInfo** displayModes;
201 int i, displayModesCount;
204 * Query for all the display available...
206 XF86VidModeGetAllModeLines(
214 * Check every of the modes looking for one that matches our demands
216 for( i=0; i<displayModesCount; i++ )
218 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
219 fgState.GameModeDepth, fgState.GameModeRefresh ) )
224 * OKi, this is the display mode we have been looking for...
226 XF86VidModeSwitchToMode(
233 * Set the viewport's origin to (0,0) (the game mode window's top-left corner)
235 XF86VidModeSetViewPort(
243 * Return successfull...
251 * Something must have went wrong
256 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
259 #elif TARGET_HOST_WIN32
261 unsigned int displayModes = 0, mode = 0xffffffff;
262 GLboolean success = FALSE;
267 * Enumerate the available display modes
268 * Try to get a complete match
270 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
273 * Does the enumerated display mode match the user's preferences?
275 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
276 devMode.dmBitsPerPel, devMode.dmDisplayFrequency ) )
279 * OKi, we've found a matching display mode, remember its number and break
286 * Switch to the next display mode, if any
291 if ( mode == 0xffffffff )
293 /* then try without Display Frequency */
297 * Enumerate the available display modes
299 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
301 /* then try without Display Frequency */
303 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
304 devMode.dmBitsPerPel, fgState.GameModeRefresh))
307 * OKi, we've found a matching display mode, remember its number and break
314 * Switch to the next display mode, if any
321 * Did we find a matching display mode?
323 if( mode != 0xffffffff )
325 int retVal = DISP_CHANGE_SUCCESSFUL;
328 * Mark the values we want to modify in the display change call
330 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
333 * Change the current display mode (possibly in test mode only)
335 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
338 * I don't know if it's really needed, but looks nice:
340 success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
343 * If it was not a test, remember the current screen settings
345 if( !haveToTest && success )
347 fgState.GameModeSize.X = devMode.dmPelsWidth;
348 fgState.GameModeSize.Y = devMode.dmPelsHeight;
349 fgState.GameModeDepth = devMode.dmBitsPerPel;
350 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
355 * Otherwise we must have failed somewhere
363 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
366 * Sets the game mode display string
368 void FGAPIENTRY glutGameModeString( const char* string )
370 int width = 640, height = 480, depth = 16, refresh = 72;
373 * This one seems a bit easier than glutInitDisplayString. The bad thing
374 * about it that I was unable to find the game mode string definition, so
375 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
376 * appears in all GLUT game mode programs I have seen to date.
378 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
379 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
380 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
381 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
382 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
383 if( sscanf( string, ":%i", &depth ) != 1 )
384 if( sscanf( string, "@%i", &refresh ) != 1 )
385 fgWarning( "unable to parse game mode string `%s'", string );
388 * Hopefully it worked, and if not, we still have the default values
390 fgState.GameModeSize.X = width;
391 fgState.GameModeSize.Y = height;
392 fgState.GameModeDepth = depth;
393 fgState.GameModeRefresh = refresh;
397 * Enters the game mode
399 int FGAPIENTRY glutEnterGameMode( void )
402 * Check if a game mode window already exists...
404 if( fgStructure.GameMode != NULL )
407 * ...if so, delete it before proceeding...
409 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
414 * ...otherwise remember the current resolution, etc.
420 * We are ready to change the current screen's resolution now
422 if( fghChangeDisplayMode( FALSE ) == FALSE )
424 fgWarning( "failed to change screen settings" );
429 * Finally, have the game mode window created
431 fgStructure.GameMode = fgCreateWindow(
432 NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
435 #if TARGET_HOST_UNIX_X11
438 * Move the mouse pointer over the game mode window
442 fgStructure.GameMode->Window.Handle,
448 * Confine the mouse pointer to the window's client area
452 fgStructure.GameMode->Window.Handle,
454 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask,
455 GrabModeAsync, GrabModeAsync,
456 fgStructure.GameMode->Window.Handle,
462 * Grab the keyboard, too
466 fgStructure.GameMode->Window.Handle,
468 GrabModeAsync, GrabModeAsync,
481 * Leaves the game mode
483 void FGAPIENTRY glutLeaveGameMode( void )
485 freeglut_return_if_fail( fgStructure.GameMode != NULL );
488 * First of all, have the game mode window destroyed
490 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
492 #if TARGET_HOST_UNIX_X11
495 * Ungrab the mouse and keyboard
497 XUngrabPointer( fgDisplay.Display, CurrentTime );
498 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
503 * Then, have the desktop visual settings restored
509 * Returns information concerning the freeglut game mode
511 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
514 * See why are we bothered
518 case GLUT_GAME_MODE_ACTIVE:
520 * Check if the game mode is currently active
522 return( fgStructure.GameMode != NULL );
524 case GLUT_GAME_MODE_POSSIBLE:
526 * Check if the current game mode settings are valid
528 return( fghChangeDisplayMode( TRUE ) );
530 case GLUT_GAME_MODE_WIDTH:
532 * The game mode screen width
534 return( fgState.GameModeSize.X );
536 case GLUT_GAME_MODE_HEIGHT:
538 * The game mode screen height
540 return( fgState.GameModeSize.Y );
542 case GLUT_GAME_MODE_PIXEL_DEPTH:
544 * The game mode pixel depth
546 return( fgState.GameModeDepth );
548 case GLUT_GAME_MODE_REFRESH_RATE:
550 * The game mode refresh rate
552 return( fgState.GameModeRefresh );
554 case GLUT_GAME_MODE_DISPLAY_CHANGED:
556 * This is true if the game mode has been activated successfully..
558 return( fgStructure.GameMode != NULL );
564 /*** END OF FILE ***/