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
64 * Remember the current ViewPort location of the screen to be able to
65 * restore the ViewPort on LeaveGameMode():
67 XF86VidModeGetViewPort(
70 &fgDisplay.DisplayViewPortX,
71 &fgDisplay.DisplayViewPortY
75 * Remember the current pointer location before going fullscreen
76 * for restoring it later:
83 fgDisplay.Display, fgDisplay.RootWindow,
84 &junk_window, &junk_window,
85 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
86 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &mask
91 * Query the current display settings:
93 fgDisplay.DisplayModeValid =
94 XF86VidModeGetModeLine(
97 &fgDisplay.DisplayModeClock,
98 &fgDisplay.DisplayMode
101 if (!fgDisplay.DisplayModeValid)
102 fgWarning( "Runtime use of XF86VidModeGetModeLine failed.\n" );
105 # warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
108 #elif TARGET_HOST_WIN32
110 /* DEVMODE devMode; */
113 * Grab the current desktop settings...
116 /* hack to get around my stupid cross-gcc headers */
117 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
119 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode );
122 * Make sure we will be restoring all settings needed
124 fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
130 * Restores the previously remembered visual settings
132 void fghRestoreState( void )
134 #if TARGET_HOST_UNIX_X11
136 * Restore the remembered pointer position:
139 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
140 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
144 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
146 # ifdef X_XF86VidModeGetAllModeLines
148 if (fgDisplay.DisplayModeValid)
150 XF86VidModeModeInfo** displayModes;
151 int i, displayModesCount;
154 * Query for all the display available...
156 XF86VidModeGetAllModeLines(
164 * Check every of the modes looking for one that matches our demands
166 for( i=0; i<displayModesCount; i++ )
168 if( displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
169 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
170 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
173 * OK, this is the display mode we have been looking for...
175 XF86VidModeSwitchToMode(
182 * Now we can restore the remembered ViewPort:
184 XF86VidModeSetViewPort(
187 fgDisplay.DisplayViewPortX,
188 fgDisplay.DisplayViewPortY
192 * For the case this would be the last X11 call the application
193 * calls exit() we've to flush the X11 output queue to have the
194 * commands sent to the X server before the application exists.
196 XFlush(fgDisplay.Display);
204 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
207 #elif TARGET_HOST_WIN32
210 * Restore the previously rememebered desktop display settings
212 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
218 * Checks the display mode settings against user's preferences
220 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
223 * The desired values should be stored in fgState structure...
225 return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
226 (depth == fgState.GameModeDepth) && (refresh == fgState.GameModeRefresh) );
230 * Changes the current display mode to match user's settings
232 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
234 #if TARGET_HOST_UNIX_X11
237 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
239 # ifdef X_XF86VidModeGetAllModeLines
242 * This is also used by applcations which check modes by calling
243 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
245 if (haveToTest || fgDisplay.DisplayModeValid)
247 XF86VidModeModeInfo** displayModes;
248 int i, displayModesCount;
251 * Query for all the display available...
253 XF86VidModeGetAllModeLines(
261 * Check every of the modes looking for one that matches our demands
263 for( i=0; i<displayModesCount; i++ )
265 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
266 fgState.GameModeDepth, fgState.GameModeRefresh ) )
271 * OKi, this is the display mode we have been looking for...
273 XF86VidModeSwitchToMode(
279 * Return successfull...
287 * Something must have went wrong
292 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
295 #elif TARGET_HOST_WIN32
297 unsigned int displayModes = 0, mode = 0xffffffff;
298 GLboolean success = FALSE;
303 * Enumerate the available display modes
304 * Try to get a complete match
306 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
309 * Does the enumerated display mode match the user's preferences?
311 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
312 devMode.dmBitsPerPel, devMode.dmDisplayFrequency ) )
315 * OKi, we've found a matching display mode, remember its number and break
322 * Switch to the next display mode, if any
327 if ( mode == 0xffffffff )
329 /* then try without Display Frequency */
333 * Enumerate the available display modes
335 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
337 /* then try without Display Frequency */
339 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
340 devMode.dmBitsPerPel, fgState.GameModeRefresh))
343 * OKi, we've found a matching display mode, remember its number and break
350 * Switch to the next display mode, if any
357 * Did we find a matching display mode?
359 if( mode != 0xffffffff )
361 int retVal = DISP_CHANGE_SUCCESSFUL;
364 * Mark the values we want to modify in the display change call
366 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
369 * Change the current display mode (possibly in test mode only)
371 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
374 * I don't know if it's really needed, but looks nice:
376 success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
379 * If it was not a test, remember the current screen settings
381 if( !haveToTest && success )
383 fgState.GameModeSize.X = devMode.dmPelsWidth;
384 fgState.GameModeSize.Y = devMode.dmPelsHeight;
385 fgState.GameModeDepth = devMode.dmBitsPerPel;
386 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
391 * Otherwise we must have failed somewhere
399 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
402 * Sets the game mode display string
404 void FGAPIENTRY glutGameModeString( const char* string )
406 int width = 640, height = 480, depth = 16, refresh = 72;
409 * This one seems a bit easier than glutInitDisplayString. The bad thing
410 * about it that I was unable to find the game mode string definition, so
411 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
412 * appears in all GLUT game mode programs I have seen to date.
414 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
415 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
416 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
417 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
418 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
419 if( sscanf( string, ":%i", &depth ) != 1 )
420 if( sscanf( string, "@%i", &refresh ) != 1 )
421 fgWarning( "unable to parse game mode string `%s'", string );
424 * Hopefully it worked, and if not, we still have the default values
426 fgState.GameModeSize.X = width;
427 fgState.GameModeSize.Y = height;
428 fgState.GameModeDepth = depth;
429 fgState.GameModeRefresh = refresh;
433 * Enters the game mode
435 int FGAPIENTRY glutEnterGameMode( void )
438 * Check if a game mode window already exists...
440 if( fgStructure.GameMode != NULL )
443 * ...if so, delete it before proceeding...
445 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
450 * ...otherwise remember the current resolution, etc.
456 * We are ready to change the current screen's resolution now
458 if( fghChangeDisplayMode( FALSE ) == FALSE )
460 fgWarning( "failed to change screen settings" );
465 * Finally, have the game mode window created
467 fgStructure.GameMode = fgCreateWindow(
468 NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
471 #if TARGET_HOST_UNIX_X11
473 /* Move the window up to the topleft corner */
474 XMoveWindow(fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0);
477 * Sync needed to avoid a real race, the Xserver must have really created
478 * the window before we can grab the pointer into it:
480 XSync(fgDisplay.Display, False);
482 /* Move the Pointer to the middle of the fullscreen window */
486 fgDisplay.RootWindow,
488 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
492 * Grab the pointer to confine it into the window after the calls to
493 * XWrapPointer() which ensure that the pointer really enters the window.
495 * We also need to wait here until XGrabPointer() returns GrabSuccess,
496 * otherwise the new window is not viewable yet and if the next function
497 * (XSetInputFocus) is called with a not yet viewable window, it will exit
498 * the application which we have to aviod, so wait until it's viewable:
500 while (GrabSuccess =! XGrabPointer(
501 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
502 TRUE, ButtonPressMask|ButtonReleaseMask|ButtonMotionMask
504 GrabModeAsync, GrabModeAsync,
505 fgStructure.GameMode->Window.Handle, None, CurrentTime)) {
510 * Change input focus to the new window. This will exit the application
511 * if the new window is not viewable yet, see the XGrabPointer loop above.
515 fgStructure.GameMode->Window.Handle,
520 # ifdef X_XF86VidModeSetViewPort
522 if (fgDisplay.DisplayModeValid) {
527 * Change to viewport to the window topleft edge:
529 XF86VidModeSetViewPort(fgDisplay.Display, fgDisplay.Screen, 0, 0);
532 * Final window repositioning: It could be avoided using an undecorated
533 * window using override_redirect, but this * would possily require more
534 * changes and investigation.
537 /* Get the current postion of the drawable area on screen */
538 XTranslateCoordinates(
540 fgStructure.Window->Window.Handle,
541 fgDisplay.RootWindow,
546 /* Move the decorataions out of the topleft corner of the display */
547 XMoveWindow(fgDisplay.Display, fgStructure.Window->Window.Handle, -x, -y);
553 * Grab the keyboard, too
557 fgStructure.GameMode->Window.Handle,
559 GrabModeAsync, GrabModeAsync,
572 * Leaves the game mode
574 void FGAPIENTRY glutLeaveGameMode( void )
576 freeglut_return_if_fail( fgStructure.GameMode != NULL );
579 * First of all, have the game mode window destroyed
581 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
583 #if TARGET_HOST_UNIX_X11
586 * Ungrab the mouse and keyboard
588 XUngrabPointer( fgDisplay.Display, CurrentTime );
589 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
594 * Then, have the desktop visual settings restored
600 * Returns information concerning the freeglut game mode
602 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
605 * See why are we bothered
609 case GLUT_GAME_MODE_ACTIVE:
611 * Check if the game mode is currently active
613 return( fgStructure.GameMode != NULL );
615 case GLUT_GAME_MODE_POSSIBLE:
617 * Check if the current game mode settings are valid
619 return( fghChangeDisplayMode( TRUE ) );
621 case GLUT_GAME_MODE_WIDTH:
623 * The game mode screen width
625 return( fgState.GameModeSize.X );
627 case GLUT_GAME_MODE_HEIGHT:
629 * The game mode screen height
631 return( fgState.GameModeSize.Y );
633 case GLUT_GAME_MODE_PIXEL_DEPTH:
635 * The game mode pixel depth
637 return( fgState.GameModeDepth );
639 case GLUT_GAME_MODE_REFRESH_RATE:
641 * The game mode refresh rate
643 return( fgState.GameModeRefresh );
645 case GLUT_GAME_MODE_DISPLAY_CHANGED:
647 * This is true if the game mode has been activated successfully..
649 return( fgStructure.GameMode != NULL );
655 /*** END OF FILE ***/