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 if( !XF86VidModeGetViewPort(
65 &fgDisplay.DisplayViewPortX,
66 &fgDisplay.DisplayViewPortY ) )
67 fgWarning( "XF86VidModeGetViewPort failed" );
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( "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 if( !XF86VidModeGetAllModeLines(
153 fgWarning( "XF86VidModeGetAllModeLines failed" );
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 if( !XF86VidModeSwitchToMode(
171 displayModes[ i ] ) )
173 fgWarning( "XF86VidModeSwitchToMode failed" );
177 if( !XF86VidModeSetViewPort(
180 fgDisplay.DisplayViewPortX,
181 fgDisplay.DisplayViewPortY ) )
182 fgWarning( "XF86VidModeSetViewPort failed" );
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 );
195 XFree( displayModes );
200 * XXX warning fghRestoreState: missing XFree86 video mode extensions,
201 * XXX game mode will not change screen resolution when activated
205 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
207 /* Restore the previously rememebered desktop display settings */
208 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
214 * Checks the display mode settings against user's preferences
216 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
218 /* 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 static GLboolean fghChangeDisplayMode( GLboolean haveToTest )
230 GLboolean success = GL_FALSE;
231 #if TARGET_HOST_UNIX_X11
234 * This highly depends on the XFree86 extensions,
235 * not approved as X Consortium standards
237 # ifdef X_XF86VidModeGetAllModeLines
240 * This is also used by applcations which check modes by calling
241 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
243 if( haveToTest || fgDisplay.DisplayModeValid )
245 XF86VidModeModeInfo** displayModes;
246 int i, ignoreRefreshRate, displayModesCount;
248 if( !XF86VidModeGetAllModeLines(
254 fgWarning( "XF86VidModeGetAllModeLines failed" );
260 * Check every of the modes looking for one that matches our demands,
261 * ignoring the refresh rate if no exact match could be found.
263 for( ignoreRefreshRate = 0;
264 !success && ( ignoreRefreshRate <= 1 );
268 !success && ( i < displayModesCount );
271 /* Compute the displays refresh rate, dotclock comes in kHz. */
272 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
273 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
275 success = fghCheckDisplayMode( displayModes[ i ]->hdisplay,
276 displayModes[ i ]->vdisplay,
277 fgState.GameModeDepth,
278 ( ignoreRefreshRate ? fgState.GameModeRefresh : refresh ) );
282 if( !haveToTest && success ) {
283 if( !XF86VidModeSwitchToMode(
286 displayModes[ i ] ) )
287 fgWarning( "XF86VidModeSwitchToMode failed" );
290 XFree( displayModes );
296 * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
297 * XXX game mode will not change screen resolution when activated
303 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
306 char *fggmstr = NULL;
310 EnumDisplaySettings( NULL, -1, &devMode );
311 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
313 devMode.dmPelsWidth = fgState.GameModeSize.X;
314 devMode.dmPelsHeight = fgState.GameModeSize.Y;
315 devMode.dmBitsPerPel = fgState.GameModeDepth;
316 devMode.dmDisplayFrequency = fgState.GameModeRefresh;
317 devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
319 switch ( ChangeDisplaySettingsEx(NULL, &devMode, NULL, haveToTest ? CDS_TEST : CDS_FULLSCREEN , NULL) )
321 case DISP_CHANGE_SUCCESSFUL:
324 /* update vars in case if windows switched to proper mode */
325 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS, &devMode );
326 fgState.GameModeSize.X = devMode.dmPelsWidth;
327 fgState.GameModeSize.Y = devMode.dmPelsHeight;
328 fgState.GameModeDepth = devMode.dmBitsPerPel;
329 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
331 case DISP_CHANGE_RESTART:
332 fggmstr = "The computer must be restarted for the graphics mode to work.";
334 case DISP_CHANGE_BADFLAGS:
335 fggmstr = "An invalid set of flags was passed in.";
337 case DISP_CHANGE_BADPARAM:
338 fggmstr = "An invalid parameter was passed in. This can include an invalid flag or combination of flags.";
340 case DISP_CHANGE_FAILED:
341 fggmstr = "The display driver failed the specified graphics mode.";
343 case DISP_CHANGE_BADMODE:
344 fggmstr = "The graphics mode is not supported.";
347 fggmstr = "Unknown error in graphics mode???"; /* dunno if it is possible,MSDN does not mention any other error */
352 fgWarning(fggmstr); /* I'd rather get info whats going on in my program than wonder about */
353 /* magic happenings behind my back, its lib for devels at last ;) */
360 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
363 * Sets the game mode display string
365 void FGAPIENTRY glutGameModeString( const char* string )
367 int width = 640, height = 480, depth = 16, refresh = 72;
369 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );
372 * This one seems a bit easier than glutInitDisplayString. The bad thing
373 * about it that I was unable to find the game mode string definition, so
374 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
375 * appears in all GLUT game mode programs I have seen to date.
377 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
379 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
380 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
381 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
382 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
383 if( sscanf( string, ":%i", &depth ) != 1 )
384 if( sscanf( string, "@%i", &refresh ) != 1 )
386 "unable to parse game mode string `%s'",
390 /* Hopefully it worked, and if not, we still have the default values */
391 fgState.GameModeSize.X = width;
392 fgState.GameModeSize.Y = height;
393 fgState.GameModeDepth = depth;
394 fgState.GameModeRefresh = refresh;
398 * Enters the game mode
400 int FGAPIENTRY glutEnterGameMode( void )
402 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );
404 if( fgStructure.GameMode )
405 fgAddToWindowDestroyList( fgStructure.GameMode );
409 if( ! fghChangeDisplayMode( GL_FALSE ) )
411 fgWarning( "failed to change screen settings" );
415 fgStructure.GameMode = fgCreateWindow(
416 NULL, "FREEGLUT", 0, 0,
417 fgState.GameModeSize.X, fgState.GameModeSize.Y, GL_TRUE, GL_FALSE
420 fgStructure.GameMode->State.Width = fgState.GameModeSize.X;
421 fgStructure.GameMode->State.Height = fgState.GameModeSize.Y;
423 fgStructure.GameMode->State.IsGameMode = GL_TRUE;
425 #if TARGET_HOST_UNIX_X11
428 * Sync needed to avoid a real race, the Xserver must have really created
429 * the window before we can grab the pointer into it:
431 XSync( fgDisplay.Display, False );
434 * Grab the pointer to confine it into the window after the calls to
435 * XWrapPointer() which ensure that the pointer really enters the window.
437 * We also need to wait here until XGrabPointer() returns GrabSuccess,
438 * otherwise the new window is not viewable yet and if the next function
439 * (XSetInputFocus) is called with a not yet viewable window, it will exit
440 * the application which we have to aviod, so wait until it's viewable:
442 while( GrabSuccess != XGrabPointer(
443 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
445 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
447 GrabModeAsync, GrabModeAsync,
448 fgStructure.GameMode->Window.Handle, None, CurrentTime) )
452 * Change input focus to the new window. This will exit the application
453 * if the new window is not viewable yet, see the XGrabPointer loop above.
457 fgStructure.GameMode->Window.Handle,
462 /* Move the Pointer to the middle of the fullscreen window */
466 fgDisplay.RootWindow,
468 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
471 # ifdef X_XF86VidModeSetViewPort
473 if( fgDisplay.DisplayModeValid )
478 /* Change to viewport to the window topleft edge: */
479 if( !XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 ) )
480 fgWarning( "XF86VidModeSetViewPort failed" );
483 * Final window repositioning: It could be avoided using an undecorated
484 * window using override_redirect, but this * would possily require
485 * more changes and investigation.
488 /* Get the current postion of the drawable area on screen */
489 XTranslateCoordinates(
491 fgStructure.CurrentWindow->Window.Handle,
492 fgDisplay.RootWindow,
497 /* Move the decorataions out of the topleft corner of the display */
498 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
504 /* Grab the keyboard, too */
507 fgStructure.GameMode->Window.Handle,
509 GrabModeAsync, GrabModeAsync,
519 * Leaves the game mode
521 void FGAPIENTRY glutLeaveGameMode( void )
523 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );
525 freeglut_return_if_fail( fgStructure.GameMode );
527 fgStructure.GameMode->State.IsGameMode = GL_FALSE;
529 fgAddToWindowDestroyList( fgStructure.GameMode );
530 fgStructure.GameMode = NULL;
532 #if TARGET_HOST_UNIX_X11
534 XUngrabPointer( fgDisplay.Display, CurrentTime );
535 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
543 * Returns information concerning the freeglut game mode
545 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
547 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );
551 case GLUT_GAME_MODE_ACTIVE:
552 return !!fgStructure.GameMode;
554 case GLUT_GAME_MODE_POSSIBLE:
555 return fghChangeDisplayMode( GL_TRUE );
557 case GLUT_GAME_MODE_WIDTH:
558 return fgState.GameModeSize.X;
560 case GLUT_GAME_MODE_HEIGHT:
561 return fgState.GameModeSize.Y;
563 case GLUT_GAME_MODE_PIXEL_DEPTH:
564 return fgState.GameModeDepth;
566 case GLUT_GAME_MODE_REFRESH_RATE:
567 return fgState.GameModeRefresh;
569 case GLUT_GAME_MODE_DISPLAY_CHANGED:
571 * This is true if the game mode has been activated successfully..
573 return !!fgStructure.GameMode;
576 fgWarning( "Unknown gamemode get: %d", eWhat );
580 /*** END OF FILE ***/