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
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" );
105 * XXX warning fghRememberState: missing XFree86 video mode extensions,
106 * XXX game mode will not change screen resolution when activated
110 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
112 /* DEVMODE devMode; */
115 * Grab the current desktop settings...
118 /* hack to get around my stupid cross-gcc headers */
119 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
121 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS,
122 &fgDisplay.DisplayMode );
125 * Make sure we will be restoring all settings needed
127 fgDisplay.DisplayMode.dmFields |=
128 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
134 * Restores the previously remembered visual settings
136 void fghRestoreState( void )
138 #if TARGET_HOST_UNIX_X11
140 # ifdef X_XF86VidModeGetAllModeLines
142 * Restore the remembered pointer position:
145 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
146 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
150 * This highly depends on the XFree86 extensions,
151 * not approved as X Consortium standards
154 if( fgDisplay.DisplayModeValid )
156 XF86VidModeModeInfo** displayModes;
157 int i, displayModesCount;
159 XF86VidModeGetAllModeLines(
167 * Check every of the modes looking for one that matches our demands.
168 * If we find one, switch to it and restore the remembered viewport.
170 for( i = 0; i < displayModesCount; i++ )
172 if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
173 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
174 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
176 XF86VidModeSwitchToMode(
181 XF86VidModeSetViewPort(
184 fgDisplay.DisplayViewPortX,
185 fgDisplay.DisplayViewPortY
189 * For the case this would be the last X11 call the application
190 * calls exit() we've to flush the X11 output queue to have the
191 * commands sent to the X server before the application exits.
193 XFlush( fgDisplay.Display );
202 * XXX warning fghRestoreState: missing XFree86 video mode extensions,
203 * XXX game mode will not change screen resolution when activated
207 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
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 ) &&
226 ( height == fgState.GameModeSize.Y ) &&
227 ( depth == fgState.GameModeDepth ) &&
228 (refresh == fgState.GameModeRefresh );
232 * Changes the current display mode to match user's settings
234 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
236 #if TARGET_HOST_UNIX_X11
239 * This highly depends on the XFree86 extensions,
240 * not approved as X Consortium standards
242 # ifdef X_XF86VidModeGetAllModeLines
245 * This is also used by applcations which check modes by calling
246 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
248 if( haveToTest || fgDisplay.DisplayModeValid )
250 XF86VidModeModeInfo** displayModes;
251 int i, displayModesCount;
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,
266 displayModes[ i ]->vdisplay,
267 fgState.GameModeDepth,
268 fgState.GameModeRefresh ) )
273 * OKi, this is the display mode we have been looking for...
275 XF86VidModeSwitchToMode(
286 * Something must have went wrong
292 * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
293 * XXX game mode will not change screen resolution when activated
297 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
299 unsigned int displayModes = 0, mode = 0xffffffff;
300 GLboolean success = GL_FALSE;
305 * Enumerate the available display modes
306 * Try to get a complete match
308 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
311 * Does the enumerated display mode match the user's preferences?
313 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
314 devMode.dmBitsPerPel,
315 devMode.dmDisplayFrequency ) )
323 if( mode == 0xffffffff )
325 /* then try without Display Frequency */
329 * Enumerate the available display modes
331 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
333 /* then try without Display Frequency */
334 if( fghCheckDisplayMode( devMode.dmPelsWidth,
335 devMode.dmPelsHeight,
336 devMode.dmBitsPerPel,
337 fgState.GameModeRefresh ) )
347 * Did we find a matching display mode?
349 if( mode != 0xffffffff )
351 int retVal = DISP_CHANGE_SUCCESSFUL;
354 * Mark the values we want to modify in the display change call
357 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
359 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
362 * I don't know if it's really needed, but looks nice:
364 success = (retVal == DISP_CHANGE_SUCCESSFUL) ||
365 (retVal == DISP_CHANGE_NOTUPDATED);
367 if( !haveToTest && success )
369 fgState.GameModeSize.X = devMode.dmPelsWidth;
370 fgState.GameModeSize.Y = devMode.dmPelsHeight;
371 fgState.GameModeDepth = devMode.dmBitsPerPel;
372 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
382 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
385 * Sets the game mode display string
387 void FGAPIENTRY glutGameModeString( const char* string )
389 int width = 640, height = 480, depth = 16, refresh = 72;
392 * This one seems a bit easier than glutInitDisplayString. The bad thing
393 * about it that I was unable to find the game mode string definition, so
394 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
395 * appears in all GLUT game mode programs I have seen to date.
397 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
399 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
400 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
401 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
402 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
403 if( sscanf( string, ":%i", &depth ) != 1 )
404 if( sscanf( string, "@%i", &refresh ) != 1 )
406 "unable to parse game mode string `%s'",
411 * Hopefully it worked, and if not, we still have the default values
413 fgState.GameModeSize.X = width;
414 fgState.GameModeSize.Y = height;
415 fgState.GameModeDepth = depth;
416 fgState.GameModeRefresh = refresh;
420 * Enters the game mode
422 int FGAPIENTRY glutEnterGameMode( void )
424 if( fgStructure.GameMode )
425 fgAddToWindowDestroyList( fgStructure.GameMode );
429 if( ! fghChangeDisplayMode( GL_FALSE ) )
431 fgWarning( "failed to change screen settings" );
435 fgStructure.GameMode = fgCreateWindow(
436 NULL, "FREEGLUT", 0, 0,
437 fgState.GameModeSize.X, fgState.GameModeSize.Y, GL_TRUE, GL_FALSE
440 fgStructure.GameMode->State.IsGameMode = GL_TRUE;
442 #if TARGET_HOST_UNIX_X11
444 /* Move the window up to the topleft corner */
445 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0 );
448 * Sync needed to avoid a real race, the Xserver must have really created
449 * the window before we can grab the pointer into it:
451 XSync( fgDisplay.Display, False );
453 /* Move the Pointer to the middle of the fullscreen window */
457 fgDisplay.RootWindow,
459 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
463 * Grab the pointer to confine it into the window after the calls to
464 * XWrapPointer() which ensure that the pointer really enters the window.
466 * We also need to wait here until XGrabPointer() returns GrabSuccess,
467 * otherwise the new window is not viewable yet and if the next function
468 * (XSetInputFocus) is called with a not yet viewable window, it will exit
469 * the application which we have to aviod, so wait until it's viewable:
471 while( GrabSuccess != XGrabPointer(
472 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
474 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
476 GrabModeAsync, GrabModeAsync,
477 fgStructure.GameMode->Window.Handle, None, CurrentTime) )
481 * Change input focus to the new window. This will exit the application
482 * if the new window is not viewable yet, see the XGrabPointer loop above.
486 fgStructure.GameMode->Window.Handle,
491 # ifdef X_XF86VidModeSetViewPort
493 if( fgDisplay.DisplayModeValid )
499 * Change to viewport to the window topleft edge:
501 XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 );
504 * Final window repositioning: It could be avoided using an undecorated
505 * window using override_redirect, but this * would possily require
506 * more changes and investigation.
509 /* Get the current postion of the drawable area on screen */
510 XTranslateCoordinates(
512 fgStructure.Window->Window.Handle,
513 fgDisplay.RootWindow,
518 /* Move the decorataions out of the topleft corner of the display */
519 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
526 * Grab the keyboard, too
530 fgStructure.GameMode->Window.Handle,
532 GrabModeAsync, GrabModeAsync,
542 * Leaves the game mode
544 void FGAPIENTRY glutLeaveGameMode( void )
546 freeglut_return_if_fail( fgStructure.GameMode );
548 fgStructure.GameMode->State.IsGameMode = GL_FALSE;
550 fgAddToWindowDestroyList( fgStructure.GameMode );
552 fgStructure.GameMode = NULL ;
554 #if TARGET_HOST_UNIX_X11
556 XUngrabPointer( fgDisplay.Display, CurrentTime );
557 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
565 * Returns information concerning the freeglut game mode
567 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
571 case GLUT_GAME_MODE_ACTIVE:
572 return !!fgStructure.GameMode;
574 case GLUT_GAME_MODE_POSSIBLE:
575 return fghChangeDisplayMode( GL_TRUE );
577 case GLUT_GAME_MODE_WIDTH:
578 return fgState.GameModeSize.X;
580 case GLUT_GAME_MODE_HEIGHT:
581 return fgState.GameModeSize.Y;
583 case GLUT_GAME_MODE_PIXEL_DEPTH:
584 return fgState.GameModeDepth;
586 case GLUT_GAME_MODE_REFRESH_RATE:
587 return fgState.GameModeRefresh;
589 case GLUT_GAME_MODE_DISPLAY_CHANGED:
591 * This is true if the game mode has been activated successfully..
593 return !!fgStructure.GameMode;
599 /*** END OF FILE ***/