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,
57 * not approved as X Consortium standards
59 # ifdef X_XF86VidModeGetModeLine
63 * Remember the current ViewPort location of the screen to be able to
64 * restore the ViewPort on LeaveGameMode():
66 XF86VidModeGetViewPort(
69 &fgDisplay.DisplayViewPortX,
70 &fgDisplay.DisplayViewPortY
74 * Remember the current pointer location before going fullscreen
75 * for restoring it later:
82 fgDisplay.Display, fgDisplay.RootWindow,
83 &junk_window, &junk_window,
84 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
85 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &mask
90 * Query the current display settings:
92 fgDisplay.DisplayModeValid =
93 XF86VidModeGetModeLine(
96 &fgDisplay.DisplayModeClock,
97 &fgDisplay.DisplayMode
100 if (!fgDisplay.DisplayModeValid)
101 fgWarning( "Runtime use of XF86VidModeGetModeLine failed.\n" );
104 # warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
107 #elif TARGET_HOST_WIN32
109 /* DEVMODE devMode; */
112 * Grab the current desktop settings...
115 /* hack to get around my stupid cross-gcc headers */
116 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
118 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS,
119 &fgDisplay.DisplayMode );
122 * Make sure we will be restoring all settings needed
124 fgDisplay.DisplayMode.dmFields |=
125 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
131 * Restores the previously remembered visual settings
133 void fghRestoreState( void )
135 #if TARGET_HOST_UNIX_X11
137 # ifdef X_XF86VidModeGetAllModeLines
139 * Restore the remembered pointer position:
142 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
143 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
147 * This highly depends on the XFree86 extensions,
148 * not approved as X Consortium standards
151 if (fgDisplay.DisplayModeValid)
153 XF86VidModeModeInfo** displayModes;
154 int i, displayModesCount;
156 XF86VidModeGetAllModeLines(
164 * Check every of the modes looking for one that matches our demands.
165 * If we find one, switch to it and restore the remembered viewport.
167 for( i = 0; i < displayModesCount; i++ )
169 if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
170 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
171 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
173 XF86VidModeSwitchToMode(
178 XF86VidModeSetViewPort(
181 fgDisplay.DisplayViewPortX,
182 fgDisplay.DisplayViewPortY
186 * For the case this would be the last X11 call the application
187 * calls exit() we've to flush the X11 output queue to have the
188 * commands sent to the X server before the application exits.
190 XFlush( fgDisplay.Display );
198 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
201 #elif TARGET_HOST_WIN32
204 * Restore the previously rememebered desktop display settings
206 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
212 * Checks the display mode settings against user's preferences
214 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
217 * The desired values should be stored in fgState structure...
219 return( (width == fgState.GameModeSize.X) &&
220 (height == fgState.GameModeSize.Y) &&
221 (depth == fgState.GameModeDepth) &&
222 (refresh == fgState.GameModeRefresh) );
226 * Changes the current display mode to match user's settings
228 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
230 #if TARGET_HOST_UNIX_X11
233 * This highly depends on the XFree86 extensions,
234 * not approved as X Consortium standards
236 # ifdef X_XF86VidModeGetAllModeLines
239 * This is also used by applcations which check modes by calling
240 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
242 if (haveToTest || fgDisplay.DisplayModeValid)
244 XF86VidModeModeInfo** displayModes;
245 int i, displayModesCount;
247 XF86VidModeGetAllModeLines(
255 * Check every of the modes looking for one that matches our demands
257 for( i = 0; i < displayModesCount; i++ )
259 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
260 displayModes[ i ]->vdisplay,
261 fgState.GameModeDepth,
262 fgState.GameModeRefresh ) )
267 * OKi, this is the display mode we have been looking for...
269 XF86VidModeSwitchToMode(
280 * Something must have went wrong
285 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
288 #elif TARGET_HOST_WIN32
290 unsigned int displayModes = 0, mode = 0xffffffff;
291 GLboolean success = FALSE;
296 * Enumerate the available display modes
297 * Try to get a complete match
299 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
302 * Does the enumerated display mode match the user's preferences?
304 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
305 devMode.dmBitsPerPel,
306 devMode.dmDisplayFrequency ) )
314 if ( mode == 0xffffffff )
316 /* then try without Display Frequency */
320 * Enumerate the available display modes
322 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
324 /* then try without Display Frequency */
325 if( fghCheckDisplayMode( devMode.dmPelsWidth,
326 devMode.dmPelsHeight,
327 devMode.dmBitsPerPel,
328 fgState.GameModeRefresh ) )
338 * Did we find a matching display mode?
340 if( mode != 0xffffffff )
342 int retVal = DISP_CHANGE_SUCCESSFUL;
345 * Mark the values we want to modify in the display change call
348 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
350 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
353 * I don't know if it's really needed, but looks nice:
355 success = (retVal == DISP_CHANGE_SUCCESSFUL) ||
356 (retVal == DISP_CHANGE_NOTUPDATED);
358 if( !haveToTest && success )
360 fgState.GameModeSize.X = devMode.dmPelsWidth;
361 fgState.GameModeSize.Y = devMode.dmPelsHeight;
362 fgState.GameModeDepth = devMode.dmBitsPerPel;
363 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
373 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
376 * Sets the game mode display string
378 void FGAPIENTRY glutGameModeString( const char* string )
380 int width = 640, height = 480, depth = 16, refresh = 72;
383 * This one seems a bit easier than glutInitDisplayString. The bad thing
384 * about it that I was unable to find the game mode string definition, so
385 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
386 * appears in all GLUT game mode programs I have seen to date.
388 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
390 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
391 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
392 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
393 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
394 if( sscanf( string, ":%i", &depth ) != 1 )
395 if( sscanf( string, "@%i", &refresh ) != 1 )
397 "unable to parse game mode string `%s'",
402 * Hopefully it worked, and if not, we still have the default values
404 fgState.GameModeSize.X = width;
405 fgState.GameModeSize.Y = height;
406 fgState.GameModeDepth = depth;
407 fgState.GameModeRefresh = refresh;
411 * Enters the game mode
413 int FGAPIENTRY glutEnterGameMode( void )
415 if( fgStructure.GameMode )
416 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
420 if( fghChangeDisplayMode( FALSE ) == FALSE )
422 fgWarning( "failed to change screen settings" );
426 fgStructure.GameMode = fgCreateWindow(
427 NULL, "FREEGLUT", 0, 0,
428 fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
431 #if TARGET_HOST_UNIX_X11
433 /* Move the window up to the topleft corner */
434 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0 );
437 * Sync needed to avoid a real race, the Xserver must have really created
438 * the window before we can grab the pointer into it:
440 XSync( fgDisplay.Display, False );
442 /* Move the Pointer to the middle of the fullscreen window */
446 fgDisplay.RootWindow,
448 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
452 * Grab the pointer to confine it into the window after the calls to
453 * XWrapPointer() which ensure that the pointer really enters the window.
455 * We also need to wait here until XGrabPointer() returns GrabSuccess,
456 * otherwise the new window is not viewable yet and if the next function
457 * (XSetInputFocus) is called with a not yet viewable window, it will exit
458 * the application which we have to aviod, so wait until it's viewable:
460 while( GrabSuccess != XGrabPointer(
461 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
463 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
465 GrabModeAsync, GrabModeAsync,
466 fgStructure.GameMode->Window.Handle, None, CurrentTime) )
470 * Change input focus to the new window. This will exit the application
471 * if the new window is not viewable yet, see the XGrabPointer loop above.
475 fgStructure.GameMode->Window.Handle,
480 # ifdef X_XF86VidModeSetViewPort
482 if (fgDisplay.DisplayModeValid)
488 * Change to viewport to the window topleft edge:
490 XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 );
493 * Final window repositioning: It could be avoided using an undecorated
494 * window using override_redirect, but this * would possily require
495 * more changes and investigation.
498 /* Get the current postion of the drawable area on screen */
499 XTranslateCoordinates(
501 fgStructure.Window->Window.Handle,
502 fgDisplay.RootWindow,
507 /* Move the decorataions out of the topleft corner of the display */
508 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
515 * Grab the keyboard, too
519 fgStructure.GameMode->Window.Handle,
521 GrabModeAsync, GrabModeAsync,
531 * Leaves the game mode
533 void FGAPIENTRY glutLeaveGameMode( void )
535 freeglut_return_if_fail( fgStructure.GameMode );
537 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
539 #if TARGET_HOST_UNIX_X11
541 XUngrabPointer( fgDisplay.Display, CurrentTime );
542 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
550 * Returns information concerning the freeglut game mode
552 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
556 case GLUT_GAME_MODE_ACTIVE:
557 return !!fgStructure.GameMode;
559 case GLUT_GAME_MODE_POSSIBLE:
560 return fghChangeDisplayMode( TRUE );
562 case GLUT_GAME_MODE_WIDTH:
563 return fgState.GameModeSize.X;
565 case GLUT_GAME_MODE_HEIGHT:
566 return fgState.GameModeSize.Y;
568 case GLUT_GAME_MODE_PIXEL_DEPTH:
569 return fgState.GameModeDepth;
571 case GLUT_GAME_MODE_REFRESH_RATE:
572 return fgState.GameModeRefresh;
574 case GLUT_GAME_MODE_DISPLAY_CHANGED:
576 * This is true if the game mode has been activated successfully..
578 return !!fgStructure.GameMode;
584 /*** END OF FILE ***/