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 = GL_FALSE;
296 * Enumerate the available display modes
297 * Try to get a complete match
299 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
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 ) )
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 );
420 if( ! fghChangeDisplayMode( GL_FALSE ) )
422 fgWarning( "failed to change screen settings" );
426 fgStructure.GameMode = fgCreateWindow(
427 NULL, "FREEGLUT", 0, 0,
428 fgState.GameModeSize.X, fgState.GameModeSize.Y, GL_TRUE, GL_FALSE
431 fgStructure.GameMode->State.IsGameMode = GL_TRUE;
433 #if TARGET_HOST_UNIX_X11
435 /* Move the window up to the topleft corner */
436 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0 );
439 * Sync needed to avoid a real race, the Xserver must have really created
440 * the window before we can grab the pointer into it:
442 XSync( fgDisplay.Display, False );
444 /* Move the Pointer to the middle of the fullscreen window */
448 fgDisplay.RootWindow,
450 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
454 * Grab the pointer to confine it into the window after the calls to
455 * XWrapPointer() which ensure that the pointer really enters the window.
457 * We also need to wait here until XGrabPointer() returns GrabSuccess,
458 * otherwise the new window is not viewable yet and if the next function
459 * (XSetInputFocus) is called with a not yet viewable window, it will exit
460 * the application which we have to aviod, so wait until it's viewable:
462 while( GrabSuccess != XGrabPointer(
463 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
465 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
467 GrabModeAsync, GrabModeAsync,
468 fgStructure.GameMode->Window.Handle, None, CurrentTime) )
472 * Change input focus to the new window. This will exit the application
473 * if the new window is not viewable yet, see the XGrabPointer loop above.
477 fgStructure.GameMode->Window.Handle,
482 # ifdef X_XF86VidModeSetViewPort
484 if( fgDisplay.DisplayModeValid )
490 * Change to viewport to the window topleft edge:
492 XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 );
495 * Final window repositioning: It could be avoided using an undecorated
496 * window using override_redirect, but this * would possily require
497 * more changes and investigation.
500 /* Get the current postion of the drawable area on screen */
501 XTranslateCoordinates(
503 fgStructure.Window->Window.Handle,
504 fgDisplay.RootWindow,
509 /* Move the decorataions out of the topleft corner of the display */
510 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
517 * Grab the keyboard, too
521 fgStructure.GameMode->Window.Handle,
523 GrabModeAsync, GrabModeAsync,
533 * Leaves the game mode
535 void FGAPIENTRY glutLeaveGameMode( void )
537 freeglut_return_if_fail( fgStructure.GameMode );
539 fgStructure.GameMode->State.IsGameMode = GL_FALSE;
541 fgAddToWindowDestroyList( fgStructure.GameMode );
543 #if TARGET_HOST_UNIX_X11
545 XUngrabPointer( fgDisplay.Display, CurrentTime );
546 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
554 * Returns information concerning the freeglut game mode
556 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
560 case GLUT_GAME_MODE_ACTIVE:
561 return !!fgStructure.GameMode;
563 case GLUT_GAME_MODE_POSSIBLE:
564 return fghChangeDisplayMode( GL_TRUE );
566 case GLUT_GAME_MODE_WIDTH:
567 return fgState.GameModeSize.X;
569 case GLUT_GAME_MODE_HEIGHT:
570 return fgState.GameModeSize.Y;
572 case GLUT_GAME_MODE_PIXEL_DEPTH:
573 return fgState.GameModeDepth;
575 case GLUT_GAME_MODE_REFRESH_RATE:
576 return fgState.GameModeRefresh;
578 case GLUT_GAME_MODE_DISPLAY_CHANGED:
580 * This is true if the game mode has been activated successfully..
582 return !!fgStructure.GameMode;
588 /*** END OF FILE ***/