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.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
32 * TODO BEFORE THE STABLE RELEASE:
34 * glutGameModeString() -- missing
35 * glutEnterGameMode() -- X11 version
36 * glutLeaveGameMode() -- is that correct?
37 * glutGameModeGet() -- is that correct?
41 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
44 * Remembers the current visual settings, so that
45 * we can change them and restore later...
47 static void fghRememberState( void )
49 #if TARGET_HOST_UNIX_X11
52 * This highly depends on the XFree86 extensions,
53 * not approved as X Consortium standards
55 # ifdef X_XF86VidModeGetModeLine
59 * Remember the current ViewPort location of the screen to be able to
60 * restore the ViewPort on LeaveGameMode():
62 XF86VidModeGetViewPort(
65 &fgDisplay.DisplayViewPortX,
66 &fgDisplay.DisplayViewPortY
70 * Remember the current pointer location before going fullscreen
71 * for restoring it later:
78 fgDisplay.Display, fgDisplay.RootWindow,
79 &junk_window, &junk_window,
80 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
81 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &mask
85 /* Query the current display settings: */
86 fgDisplay.DisplayModeValid =
87 XF86VidModeGetModeLine(
90 &fgDisplay.DisplayModeClock,
91 &fgDisplay.DisplayMode
94 if( !fgDisplay.DisplayModeValid )
95 fgWarning( "Runtime use of XF86VidModeGetModeLine failed." );
99 * XXX warning fghRememberState: missing XFree86 video mode extensions,
100 * XXX game mode will not change screen resolution when activated
104 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
106 /* DEVMODE devMode; */
108 /* Grab the current desktop settings... */
110 /* hack to get around my stupid cross-gcc headers */
111 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
113 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS,
114 &fgDisplay.DisplayMode );
116 /* Make sure we will be restoring all settings needed */
117 fgDisplay.DisplayMode.dmFields |=
118 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
124 * Restores the previously remembered visual settings
126 static void fghRestoreState( void )
128 #if TARGET_HOST_UNIX_X11
130 # ifdef X_XF86VidModeGetAllModeLines
131 /* Restore the remembered pointer position: */
133 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
134 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
138 * This highly depends on the XFree86 extensions,
139 * not approved as X Consortium standards
142 if( fgDisplay.DisplayModeValid )
144 XF86VidModeModeInfo** displayModes;
145 int i, displayModesCount;
147 XF86VidModeGetAllModeLines(
155 * Check every of the modes looking for one that matches our demands.
156 * If we find one, switch to it and restore the remembered viewport.
158 for( i = 0; i < displayModesCount; i++ )
160 if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
161 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
162 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
164 XF86VidModeSwitchToMode(
169 XF86VidModeSetViewPort(
172 fgDisplay.DisplayViewPortX,
173 fgDisplay.DisplayViewPortY
177 * For the case this would be the last X11 call the application
178 * calls exit() we've to flush the X11 output queue to have the
179 * commands sent to the X server before the application exits.
181 XFlush( fgDisplay.Display );
190 * XXX warning fghRestoreState: missing XFree86 video mode extensions,
191 * XXX game mode will not change screen resolution when activated
195 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
197 /* Restore the previously rememebered desktop display settings */
198 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
204 * Checks the display mode settings against user's preferences
206 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
208 /* The desired values should be stored in fgState structure... */
209 return ( width == fgState.GameModeSize.X ) &&
210 ( height == fgState.GameModeSize.Y ) &&
211 ( depth == fgState.GameModeDepth ) &&
212 (refresh == fgState.GameModeRefresh );
216 * Changes the current display mode to match user's settings
218 static GLboolean fghChangeDisplayMode( GLboolean haveToTest )
220 #if TARGET_HOST_UNIX_X11
223 * This highly depends on the XFree86 extensions,
224 * not approved as X Consortium standards
226 # ifdef X_XF86VidModeGetAllModeLines
229 * This is also used by applcations which check modes by calling
230 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
232 if( haveToTest || fgDisplay.DisplayModeValid )
234 XF86VidModeModeInfo** displayModes;
235 int i, displayModesCount;
237 XF86VidModeGetAllModeLines(
244 /* Check every of the modes looking for one that matches our demands */
245 for( i = 0; i < displayModesCount; i++ )
247 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
248 displayModes[ i ]->vdisplay,
249 fgState.GameModeDepth,
250 fgState.GameModeRefresh ) )
254 /* OKi, this is the display mode we have been looking for... */
255 XF86VidModeSwitchToMode(
265 /* Something must have gone wrong */
270 * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
271 * XXX game mode will not change screen resolution when activated
275 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
277 unsigned int displayModes = 0, mode = 0xffffffff;
278 GLboolean success = GL_FALSE;
283 * Enumerate the available display modes
284 * Try to get a complete match
286 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
288 /* Does the enumerated display mode match the user's preferences? */
289 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
290 devMode.dmBitsPerPel,
291 devMode.dmDisplayFrequency ) )
299 if( mode == 0xffffffff )
301 /* then try without Display Frequency */
304 /* Enumerate the available display modes */
305 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
307 /* then try without Display Frequency */
308 if( fghCheckDisplayMode( devMode.dmPelsWidth,
309 devMode.dmPelsHeight,
310 devMode.dmBitsPerPel,
311 fgState.GameModeRefresh ) )
320 /* Did we find a matching display mode? */
321 if( mode != 0xffffffff )
323 int retVal = DISP_CHANGE_SUCCESSFUL;
325 /* Mark the values we want to modify in the display change call */
327 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
329 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
331 /* I don't know if it's really needed, but looks nice: */
332 success = (retVal == DISP_CHANGE_SUCCESSFUL) ||
333 (retVal == DISP_CHANGE_NOTUPDATED);
335 if( !haveToTest && success )
337 fgState.GameModeSize.X = devMode.dmPelsWidth;
338 fgState.GameModeSize.Y = devMode.dmPelsHeight;
339 fgState.GameModeDepth = devMode.dmBitsPerPel;
340 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
350 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
353 * Sets the game mode display string
355 void FGAPIENTRY glutGameModeString( const char* string )
357 int width = 640, height = 480, depth = 16, refresh = 72;
359 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );
362 * This one seems a bit easier than glutInitDisplayString. The bad thing
363 * about it that I was unable to find the game mode string definition, so
364 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
365 * appears in all GLUT game mode programs I have seen to date.
367 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
369 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
370 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
371 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
372 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
373 if( sscanf( string, ":%i", &depth ) != 1 )
374 if( sscanf( string, "@%i", &refresh ) != 1 )
376 "unable to parse game mode string `%s'",
380 /* Hopefully it worked, and if not, we still have the default values */
381 fgState.GameModeSize.X = width;
382 fgState.GameModeSize.Y = height;
383 fgState.GameModeDepth = depth;
384 fgState.GameModeRefresh = refresh;
388 * Enters the game mode
390 int FGAPIENTRY glutEnterGameMode( void )
392 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );
394 if( fgStructure.GameMode )
395 fgAddToWindowDestroyList( fgStructure.GameMode );
399 if( ! fghChangeDisplayMode( GL_FALSE ) )
401 fgWarning( "failed to change screen settings" );
405 fgStructure.GameMode = fgCreateWindow(
406 NULL, "FREEGLUT", 0, 0,
407 fgState.GameModeSize.X, fgState.GameModeSize.Y, GL_TRUE, GL_FALSE
410 fgStructure.GameMode->State.IsGameMode = GL_TRUE;
412 #if TARGET_HOST_UNIX_X11
414 /* Move the window up to the topleft corner */
415 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0 );
418 * Sync needed to avoid a real race, the Xserver must have really created
419 * the window before we can grab the pointer into it:
421 XSync( fgDisplay.Display, False );
423 /* Move the Pointer to the middle of the fullscreen window */
427 fgDisplay.RootWindow,
429 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
433 * Grab the pointer to confine it into the window after the calls to
434 * XWrapPointer() which ensure that the pointer really enters the window.
436 * We also need to wait here until XGrabPointer() returns GrabSuccess,
437 * otherwise the new window is not viewable yet and if the next function
438 * (XSetInputFocus) is called with a not yet viewable window, it will exit
439 * the application which we have to aviod, so wait until it's viewable:
441 while( GrabSuccess != XGrabPointer(
442 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
444 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
446 GrabModeAsync, GrabModeAsync,
447 fgStructure.GameMode->Window.Handle, None, CurrentTime) )
451 * Change input focus to the new window. This will exit the application
452 * if the new window is not viewable yet, see the XGrabPointer loop above.
456 fgStructure.GameMode->Window.Handle,
461 # ifdef X_XF86VidModeSetViewPort
463 if( fgDisplay.DisplayModeValid )
468 /* Change to viewport to the window topleft edge: */
469 XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 );
472 * Final window repositioning: It could be avoided using an undecorated
473 * window using override_redirect, but this * would possily require
474 * more changes and investigation.
477 /* Get the current postion of the drawable area on screen */
478 XTranslateCoordinates(
480 fgStructure.Window->Window.Handle,
481 fgDisplay.RootWindow,
486 /* Move the decorataions out of the topleft corner of the display */
487 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
493 /* Grab the keyboard, too */
496 fgStructure.GameMode->Window.Handle,
498 GrabModeAsync, GrabModeAsync,
508 * Leaves the game mode
510 void FGAPIENTRY glutLeaveGameMode( void )
512 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );
514 freeglut_return_if_fail( fgStructure.GameMode );
516 fgStructure.GameMode->State.IsGameMode = GL_FALSE;
518 fgAddToWindowDestroyList( fgStructure.GameMode );
519 fgStructure.GameMode = NULL;
521 #if TARGET_HOST_UNIX_X11
523 XUngrabPointer( fgDisplay.Display, CurrentTime );
524 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
532 * Returns information concerning the freeglut game mode
534 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
536 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );
540 case GLUT_GAME_MODE_ACTIVE:
541 return !!fgStructure.GameMode;
543 case GLUT_GAME_MODE_POSSIBLE:
544 return fghChangeDisplayMode( GL_TRUE );
546 case GLUT_GAME_MODE_WIDTH:
547 return fgState.GameModeSize.X;
549 case GLUT_GAME_MODE_HEIGHT:
550 return fgState.GameModeSize.Y;
552 case GLUT_GAME_MODE_PIXEL_DEPTH:
553 return fgState.GameModeDepth;
555 case GLUT_GAME_MODE_REFRESH_RATE:
556 return fgState.GameModeRefresh;
558 case GLUT_GAME_MODE_DISPLAY_CHANGED:
560 * This is true if the game mode has been activated successfully..
562 return !!fgStructure.GameMode;
565 fgWarning( "Unknown gamemode get: %d", eWhat );
569 /*** END OF FILE ***/