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 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
36 * TODO BEFORE THE STABLE RELEASE:
38 * glutGameModeString() -- missing
39 * glutEnterGameMode() -- X11 version
40 * glutLeaveGameMode() -- is that correct?
41 * glutGameModeGet() -- is that correct?
45 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
48 * Remembers the current visual settings, so that
49 * we can change them and restore later...
51 void fghRememberState( void )
53 #if TARGET_HOST_UNIX_X11
56 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
58 # ifdef X_XF86VidModeGetModeLine
62 * Remember the current ViewPort location of the screen to be able to
63 * restore the ViewPort on LeaveGameMode():
65 XF86VidModeGetViewPort(
68 &fgDisplay.DisplayViewPortX,
69 &fgDisplay.DisplayViewPortY
73 * Remember the current pointer location before going fullscreen
74 * for restoring it later:
81 fgDisplay.Display, fgDisplay.RootWindow,
82 &junk_window, &junk_window,
83 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
84 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &mask
89 * Query the current display settings:
91 fgDisplay.DisplayModeValid =
92 XF86VidModeGetModeLine(
95 &fgDisplay.DisplayModeClock,
96 &fgDisplay.DisplayMode
99 if (!fgDisplay.DisplayModeValid)
100 fgWarning( "Runtime use of XF86VidModeGetModeLine failed.\n" );
103 # warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
106 #elif TARGET_HOST_WIN32
108 /* DEVMODE devMode; */
111 * Grab the current desktop settings...
114 /* hack to get around my stupid cross-gcc headers */
115 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
117 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode );
120 * Make sure we will be restoring all settings needed
122 fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
128 * Restores the previously remembered visual settings
130 void fghRestoreState( void )
132 #if TARGET_HOST_UNIX_X11
134 # ifdef X_XF86VidModeGetAllModeLines
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
147 if (fgDisplay.DisplayModeValid)
149 XF86VidModeModeInfo** displayModes;
150 int i, displayModesCount;
153 * Query for all the display available...
155 XF86VidModeGetAllModeLines(
163 * Check every of the modes looking for one that matches our demands
165 for( i=0; i<displayModesCount; i++ )
167 if( displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
168 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
169 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
172 * OK, this is the display mode we have been looking for...
174 XF86VidModeSwitchToMode(
181 * Now we can restore the remembered ViewPort:
183 XF86VidModeSetViewPort(
186 fgDisplay.DisplayViewPortX,
187 fgDisplay.DisplayViewPortY
191 * For the case this would be the last X11 call the application
192 * calls exit() we've to flush the X11 output queue to have the
193 * commands sent to the X server before the application exists.
195 XFlush(fgDisplay.Display);
203 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
206 #elif TARGET_HOST_WIN32
209 * Restore the previously rememebered desktop display settings
211 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
217 * Checks the display mode settings against user's preferences
219 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
222 * The desired values should be stored in fgState structure...
224 return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
225 (depth == fgState.GameModeDepth) && (refresh == fgState.GameModeRefresh) );
229 * Changes the current display mode to match user's settings
231 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
233 #if TARGET_HOST_UNIX_X11
236 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
238 # ifdef X_XF86VidModeGetAllModeLines
241 * This is also used by applcations which check modes by calling
242 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
244 if (haveToTest || fgDisplay.DisplayModeValid)
246 XF86VidModeModeInfo** displayModes;
247 int i, displayModesCount;
250 * Query for all the display available...
252 XF86VidModeGetAllModeLines(
260 * Check every of the modes looking for one that matches our demands
262 for( i=0; i<displayModesCount; i++ )
264 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
265 fgState.GameModeDepth, fgState.GameModeRefresh ) )
270 * OKi, this is the display mode we have been looking for...
272 XF86VidModeSwitchToMode(
278 * Return successfull...
286 * Something must have went wrong
291 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
294 #elif TARGET_HOST_WIN32
296 unsigned int displayModes = 0, mode = 0xffffffff;
297 GLboolean success = FALSE;
302 * Enumerate the available display modes
303 * Try to get a complete match
305 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
308 * Does the enumerated display mode match the user's preferences?
310 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
311 devMode.dmBitsPerPel, devMode.dmDisplayFrequency ) )
314 * OKi, we've found a matching display mode, remember its number and break
321 * Switch to the next display mode, if any
326 if ( mode == 0xffffffff )
328 /* then try without Display Frequency */
332 * Enumerate the available display modes
334 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
336 /* then try without Display Frequency */
338 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
339 devMode.dmBitsPerPel, fgState.GameModeRefresh))
342 * OKi, we've found a matching display mode, remember its number and break
349 * Switch to the next display mode, if any
356 * Did we find a matching display mode?
358 if( mode != 0xffffffff )
360 int retVal = DISP_CHANGE_SUCCESSFUL;
363 * Mark the values we want to modify in the display change call
365 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
368 * Change the current display mode (possibly in test mode only)
370 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
373 * I don't know if it's really needed, but looks nice:
375 success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
378 * If it was not a test, remember the current screen settings
380 if( !haveToTest && success )
382 fgState.GameModeSize.X = devMode.dmPelsWidth;
383 fgState.GameModeSize.Y = devMode.dmPelsHeight;
384 fgState.GameModeDepth = devMode.dmBitsPerPel;
385 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
390 * Otherwise we must have failed somewhere
398 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
401 * Sets the game mode display string
403 void FGAPIENTRY glutGameModeString( const char* string )
405 int width = 640, height = 480, depth = 16, refresh = 72;
408 * This one seems a bit easier than glutInitDisplayString. The bad thing
409 * about it that I was unable to find the game mode string definition, so
410 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
411 * appears in all GLUT game mode programs I have seen to date.
413 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
414 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
415 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
416 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
417 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
418 if( sscanf( string, ":%i", &depth ) != 1 )
419 if( sscanf( string, "@%i", &refresh ) != 1 )
420 fgWarning( "unable to parse game mode string `%s'", string );
423 * Hopefully it worked, and if not, we still have the default values
425 fgState.GameModeSize.X = width;
426 fgState.GameModeSize.Y = height;
427 fgState.GameModeDepth = depth;
428 fgState.GameModeRefresh = refresh;
432 * Enters the game mode
434 int FGAPIENTRY glutEnterGameMode( void )
437 * Check if a game mode window already exists...
439 if( fgStructure.GameMode != NULL )
442 * ...if so, delete it before proceeding...
444 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
449 * ...otherwise remember the current resolution, etc.
455 * We are ready to change the current screen's resolution now
457 if( fghChangeDisplayMode( FALSE ) == FALSE )
459 fgWarning( "failed to change screen settings" );
464 * Finally, have the game mode window created
466 fgStructure.GameMode = fgCreateWindow(
467 NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
470 #if TARGET_HOST_UNIX_X11
472 /* Move the window up to the topleft corner */
473 XMoveWindow(fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0);
476 * Sync needed to avoid a real race, the Xserver must have really created
477 * the window before we can grab the pointer into it:
479 XSync(fgDisplay.Display, False);
481 /* Move the Pointer to the middle of the fullscreen window */
485 fgDisplay.RootWindow,
487 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
491 * Grab the pointer to confine it into the window after the calls to
492 * XWrapPointer() which ensure that the pointer really enters the window.
494 * We also need to wait here until XGrabPointer() returns GrabSuccess,
495 * otherwise the new window is not viewable yet and if the next function
496 * (XSetInputFocus) is called with a not yet viewable window, it will exit
497 * the application which we have to aviod, so wait until it's viewable:
499 while (GrabSuccess != XGrabPointer(
500 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
501 TRUE, ButtonPressMask|ButtonReleaseMask|ButtonMotionMask
503 GrabModeAsync, GrabModeAsync,
504 fgStructure.GameMode->Window.Handle, None, CurrentTime)) {
509 * Change input focus to the new window. This will exit the application
510 * if the new window is not viewable yet, see the XGrabPointer loop above.
514 fgStructure.GameMode->Window.Handle,
519 # ifdef X_XF86VidModeSetViewPort
521 if (fgDisplay.DisplayModeValid) {
526 * Change to viewport to the window topleft edge:
528 XF86VidModeSetViewPort(fgDisplay.Display, fgDisplay.Screen, 0, 0);
531 * Final window repositioning: It could be avoided using an undecorated
532 * window using override_redirect, but this * would possily require more
533 * changes and investigation.
536 /* Get the current postion of the drawable area on screen */
537 XTranslateCoordinates(
539 fgStructure.Window->Window.Handle,
540 fgDisplay.RootWindow,
545 /* Move the decorataions out of the topleft corner of the display */
546 XMoveWindow(fgDisplay.Display, fgStructure.Window->Window.Handle, -x, -y);
552 * Grab the keyboard, too
556 fgStructure.GameMode->Window.Handle,
558 GrabModeAsync, GrabModeAsync,
571 * Leaves the game mode
573 void FGAPIENTRY glutLeaveGameMode( void )
575 freeglut_return_if_fail( fgStructure.GameMode != NULL );
578 * First of all, have the game mode window destroyed
580 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
582 #if TARGET_HOST_UNIX_X11
585 * Ungrab the mouse and keyboard
587 XUngrabPointer( fgDisplay.Display, CurrentTime );
588 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
593 * Then, have the desktop visual settings restored
599 * Returns information concerning the freeglut game mode
601 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
604 * See why are we bothered
608 case GLUT_GAME_MODE_ACTIVE:
610 * Check if the game mode is currently active
612 return( fgStructure.GameMode != NULL );
614 case GLUT_GAME_MODE_POSSIBLE:
616 * Check if the current game mode settings are valid
618 return( fghChangeDisplayMode( TRUE ) );
620 case GLUT_GAME_MODE_WIDTH:
622 * The game mode screen width
624 return( fgState.GameModeSize.X );
626 case GLUT_GAME_MODE_HEIGHT:
628 * The game mode screen height
630 return( fgState.GameModeSize.Y );
632 case GLUT_GAME_MODE_PIXEL_DEPTH:
634 * The game mode pixel depth
636 return( fgState.GameModeDepth );
638 case GLUT_GAME_MODE_REFRESH_RATE:
640 * The game mode refresh rate
642 return( fgState.GameModeRefresh );
644 case GLUT_GAME_MODE_DISPLAY_CHANGED:
646 * This is true if the game mode has been activated successfully..
648 return( fgStructure.GameMode != NULL );
654 /*** END OF FILE ***/