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 <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
89 /* Query the current display settings: */
90 fgDisplay.DisplayModeValid =
91 XF86VidModeGetModeLine(
94 &fgDisplay.DisplayModeClock,
95 &fgDisplay.DisplayMode
98 if( !fgDisplay.DisplayModeValid )
99 fgWarning( "Runtime use of XF86VidModeGetModeLine failed.\n" );
103 * XXX warning fghRememberState: missing XFree86 video mode extensions,
104 * XXX game mode will not change screen resolution when activated
108 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
110 /* DEVMODE devMode; */
112 /* 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,
118 &fgDisplay.DisplayMode );
120 /* Make sure we will be restoring all settings needed */
121 fgDisplay.DisplayMode.dmFields |=
122 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
135 /* Restore the remembered pointer position: */
137 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
138 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
142 * This highly depends on the XFree86 extensions,
143 * not approved as X Consortium standards
146 if( fgDisplay.DisplayModeValid )
148 XF86VidModeModeInfo** displayModes;
149 int i, displayModesCount;
151 XF86VidModeGetAllModeLines(
159 * Check every of the modes looking for one that matches our demands.
160 * If we find one, switch to it and restore the remembered viewport.
162 for( i = 0; i < displayModesCount; i++ )
164 if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
165 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
166 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
168 XF86VidModeSwitchToMode(
173 XF86VidModeSetViewPort(
176 fgDisplay.DisplayViewPortX,
177 fgDisplay.DisplayViewPortY
181 * For the case this would be the last X11 call the application
182 * calls exit() we've to flush the X11 output queue to have the
183 * commands sent to the X server before the application exits.
185 XFlush( fgDisplay.Display );
194 * XXX warning fghRestoreState: missing XFree86 video mode extensions,
195 * XXX game mode will not change screen resolution when activated
199 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
201 /* Restore the previously rememebered desktop display settings */
202 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
208 * Checks the display mode settings against user's preferences
210 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
212 /* The desired values should be stored in fgState structure... */
213 return ( width == fgState.GameModeSize.X ) &&
214 ( height == fgState.GameModeSize.Y ) &&
215 ( depth == fgState.GameModeDepth ) &&
216 (refresh == fgState.GameModeRefresh );
220 * Changes the current display mode to match user's settings
222 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
224 #if TARGET_HOST_UNIX_X11
227 * This highly depends on the XFree86 extensions,
228 * not approved as X Consortium standards
230 # ifdef X_XF86VidModeGetAllModeLines
233 * This is also used by applcations which check modes by calling
234 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
236 if( haveToTest || fgDisplay.DisplayModeValid )
238 XF86VidModeModeInfo** displayModes;
239 int i, displayModesCount;
241 XF86VidModeGetAllModeLines(
248 /* Check every of the modes looking for one that matches our demands */
249 for( i = 0; i < displayModesCount; i++ )
251 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
252 displayModes[ i ]->vdisplay,
253 fgState.GameModeDepth,
254 fgState.GameModeRefresh ) )
258 /* OKi, this is the display mode we have been looking for... */
259 XF86VidModeSwitchToMode(
269 /* Something must have gone wrong */
274 * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
275 * XXX game mode will not change screen resolution when activated
279 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
281 unsigned int displayModes = 0, mode = 0xffffffff;
282 GLboolean success = GL_FALSE;
287 * Enumerate the available display modes
288 * Try to get a complete match
290 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
292 /* Does the enumerated display mode match the user's preferences? */
293 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
294 devMode.dmBitsPerPel,
295 devMode.dmDisplayFrequency ) )
303 if( mode == 0xffffffff )
305 /* then try without Display Frequency */
308 /* Enumerate the available display modes */
309 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
311 /* then try without Display Frequency */
312 if( fghCheckDisplayMode( devMode.dmPelsWidth,
313 devMode.dmPelsHeight,
314 devMode.dmBitsPerPel,
315 fgState.GameModeRefresh ) )
324 /* Did we find a matching display mode? */
325 if( mode != 0xffffffff )
327 int retVal = DISP_CHANGE_SUCCESSFUL;
329 /* Mark the values we want to modify in the display change call */
331 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
333 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
335 /* I don't know if it's really needed, but looks nice: */
336 success = (retVal == DISP_CHANGE_SUCCESSFUL) ||
337 (retVal == DISP_CHANGE_NOTUPDATED);
339 if( !haveToTest && success )
341 fgState.GameModeSize.X = devMode.dmPelsWidth;
342 fgState.GameModeSize.Y = devMode.dmPelsHeight;
343 fgState.GameModeDepth = devMode.dmBitsPerPel;
344 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
354 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
357 * Sets the game mode display string
359 void FGAPIENTRY glutGameModeString( const char* string )
361 int width = 640, height = 480, depth = 16, refresh = 72;
364 * This one seems a bit easier than glutInitDisplayString. The bad thing
365 * about it that I was unable to find the game mode string definition, so
366 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
367 * appears in all GLUT game mode programs I have seen to date.
369 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
371 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
372 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
373 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
374 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
375 if( sscanf( string, ":%i", &depth ) != 1 )
376 if( sscanf( string, "@%i", &refresh ) != 1 )
378 "unable to parse game mode string `%s'",
382 /* Hopefully it worked, and if not, we still have the default values */
383 fgState.GameModeSize.X = width;
384 fgState.GameModeSize.Y = height;
385 fgState.GameModeDepth = depth;
386 fgState.GameModeRefresh = refresh;
390 * Enters the game mode
392 int FGAPIENTRY glutEnterGameMode( void )
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_return_if_fail( fgStructure.GameMode );
514 fgStructure.GameMode->State.IsGameMode = GL_FALSE;
516 fgAddToWindowDestroyList( fgStructure.GameMode );
518 fgStructure.GameMode = NULL;
520 #if TARGET_HOST_UNIX_X11
522 XUngrabPointer( fgDisplay.Display, CurrentTime );
523 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
531 * Returns information concerning the freeglut game mode
533 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
537 case GLUT_GAME_MODE_ACTIVE:
538 return !!fgStructure.GameMode;
540 case GLUT_GAME_MODE_POSSIBLE:
541 return fghChangeDisplayMode( GL_TRUE );
543 case GLUT_GAME_MODE_WIDTH:
544 return fgState.GameModeSize.X;
546 case GLUT_GAME_MODE_HEIGHT:
547 return fgState.GameModeSize.Y;
549 case GLUT_GAME_MODE_PIXEL_DEPTH:
550 return fgState.GameModeDepth;
552 case GLUT_GAME_MODE_REFRESH_RATE:
553 return fgState.GameModeRefresh;
555 case GLUT_GAME_MODE_DISPLAY_CHANGED:
557 * This is true if the game mode has been activated successfully..
559 return !!fgStructure.GameMode;
565 /*** END OF FILE ***/