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_POSIX_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_MS_WINDOWS
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( fgDisplay.DisplayName, 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_POSIX_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_MS_WINDOWS
207 /* Restore the previously remembered desktop display settings */
208 ChangeDisplaySettingsEx( fgDisplay.DisplayName,&fgDisplay.DisplayMode, 0,0,0 );
213 #if TARGET_HOST_POSIX_X11
214 #ifdef X_XF86VidModeGetAllModeLines
217 * Checks a single display mode settings against user's preferences.
219 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
221 /* The desired values should be stored in fgState structure... */
222 return ( width == fgState.GameModeSize.X ) &&
223 ( height == fgState.GameModeSize.Y ) &&
224 ( depth == fgState.GameModeDepth ) &&
225 ( refresh == fgState.GameModeRefresh );
229 * Checks all display modes settings against user's preferences.
230 * Returns the mode number found or -1 if none could be found.
232 static int fghCheckDisplayModes( GLboolean exactMatch, int displayModesCount, XF86VidModeModeInfo** displayModes )
235 for( i = 0; i < displayModesCount; i++ )
237 /* Compute the displays refresh rate, dotclock comes in kHz. */
238 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
239 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
241 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
242 displayModes[ i ]->vdisplay,
243 fgState.GameModeDepth,
244 ( exactMatch ? refresh : fgState.GameModeRefresh ) ) ) {
247 /* Update the chosen refresh rate, otherwise a
248 * glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE) would not
249 * return the right values
251 fgState.GameModeRefresh = refresh;
264 * Changes the current display mode to match user's settings
266 static GLboolean fghChangeDisplayMode( GLboolean haveToTest )
268 GLboolean success = GL_FALSE;
269 #if TARGET_HOST_POSIX_X11
272 * This highly depends on the XFree86 extensions,
273 * not approved as X Consortium standards
275 # ifdef X_XF86VidModeGetAllModeLines
278 * This is also used by applcations which check modes by calling
279 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
281 if( haveToTest || fgDisplay.DisplayModeValid )
283 XF86VidModeModeInfo** displayModes;
284 int i, displayModesCount;
286 if( !XF86VidModeGetAllModeLines(
292 fgWarning( "XF86VidModeGetAllModeLines failed" );
298 * Check every of the modes looking for one that matches our demands,
299 * ignoring the refresh rate if no exact match could be found.
301 i = fghCheckDisplayModes( GL_TRUE, displayModesCount, displayModes );
303 i = fghCheckDisplayModes( GL_FALSE, displayModesCount, displayModes );
305 success = ( i < 0 ) ? GL_FALSE : GL_TRUE;
307 if( !haveToTest && success ) {
308 if( !XF86VidModeSwitchToMode(
311 displayModes[ i ] ) )
312 fgWarning( "XF86VidModeSwitchToMode failed" );
315 XFree( displayModes );
321 * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
322 * XXX game mode will not change screen resolution when activated
328 #elif TARGET_HOST_MS_WINDOWS
331 char *fggmstr = NULL;
335 EnumDisplaySettings( fgDisplay.DisplayName, -1, &devMode );
336 devMode.dmFields = 0;
338 if (fgState.GameModeSize.X!=-1)
340 devMode.dmPelsWidth = fgState.GameModeSize.X;
341 devMode.dmFields |= DM_PELSWIDTH;
343 if (fgState.GameModeSize.Y!=-1)
345 devMode.dmPelsHeight = fgState.GameModeSize.Y;
346 devMode.dmFields |= DM_PELSHEIGHT;
348 if (fgState.GameModeDepth!=-1)
350 devMode.dmBitsPerPel = fgState.GameModeDepth;
351 devMode.dmFields |= DM_BITSPERPEL;
353 if (fgState.GameModeRefresh!=-1)
355 devMode.dmDisplayFrequency = fgState.GameModeRefresh;
356 devMode.dmFields |= DM_DISPLAYFREQUENCY;
359 switch ( ChangeDisplaySettingsEx(fgDisplay.DisplayName, &devMode, NULL, haveToTest ? CDS_TEST : CDS_FULLSCREEN , NULL) )
361 case DISP_CHANGE_SUCCESSFUL:
364 /* update vars in case if windows switched to proper mode */
365 EnumDisplaySettings( fgDisplay.DisplayName, FREEGLUT_ENUM_CURRENT_SETTINGS, &devMode );
366 fgState.GameModeSize.X = devMode.dmPelsWidth;
367 fgState.GameModeSize.Y = devMode.dmPelsHeight;
368 fgState.GameModeDepth = devMode.dmBitsPerPel;
369 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
371 case DISP_CHANGE_RESTART:
372 fggmstr = "The computer must be restarted for the graphics mode to work.";
374 case DISP_CHANGE_BADFLAGS:
375 fggmstr = "An invalid set of flags was passed in.";
377 case DISP_CHANGE_BADPARAM:
378 fggmstr = "An invalid parameter was passed in. This can include an invalid flag or combination of flags.";
380 case DISP_CHANGE_FAILED:
381 fggmstr = "The display driver failed the specified graphics mode.";
383 case DISP_CHANGE_BADMODE:
384 fggmstr = "The graphics mode is not supported.";
387 fggmstr = "Unknown error in graphics mode???"; /* dunno if it is possible,MSDN does not mention any other error */
392 fgWarning(fggmstr); /* I'd rather get info whats going on in my program than wonder about */
393 /* magic happenings behind my back, its lib for devels at last ;) */
400 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
403 * Sets the game mode display string
405 void FGAPIENTRY glutGameModeString( const char* string )
407 int width = -1, height = -1, depth = -1, refresh = -1;
409 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );
412 * This one seems a bit easier than glutInitDisplayString. The bad thing
413 * about it that I was unable to find the game mode string definition, so
414 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
415 * appears in all GLUT game mode programs I have seen to date.
417 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
419 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
420 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
421 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
422 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
423 if( sscanf( string, ":%i", &depth ) != 1 )
424 if( sscanf( string, "@%i", &refresh ) != 1 )
426 "unable to parse game mode string `%s'",
430 /* Hopefully it worked, and if not, we still have the default values */
431 if ( width > 0 ) fgState.GameModeSize.X = width;
432 if ( height > 0 ) fgState.GameModeSize.Y = height;
433 if ( depth > 0 ) fgState.GameModeDepth = depth;
434 if ( refresh > 0 ) fgState.GameModeRefresh = refresh;
440 * Enters the game mode
442 int FGAPIENTRY glutEnterGameMode( void )
444 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );
446 if( fgStructure.GameModeWindow )
447 fgAddToWindowDestroyList( fgStructure.GameModeWindow );
451 if( ! fghChangeDisplayMode( GL_FALSE ) )
453 fgWarning( "failed to change screen settings" );
457 fgStructure.GameModeWindow = fgCreateWindow(
458 NULL, "FREEGLUT", GL_TRUE, 0, 0,
459 GL_TRUE, fgState.GameModeSize.X, fgState.GameModeSize.Y,
463 fgStructure.GameModeWindow->State.Width = fgState.GameModeSize.X;
464 fgStructure.GameModeWindow->State.Height = fgState.GameModeSize.Y;
465 fgStructure.GameModeWindow->State.NeedToResize = GL_TRUE;
467 #if TARGET_HOST_POSIX_X11
470 * Sync needed to avoid a real race, the Xserver must have really created
471 * the window before we can grab the pointer into it:
473 XSync( fgDisplay.Display, False );
476 * Grab the pointer to confine it into the window after the calls to
477 * XWrapPointer() which ensure that the pointer really enters the window.
479 * We also need to wait here until XGrabPointer() returns GrabSuccess,
480 * otherwise the new window is not viewable yet and if the next function
481 * (XSetInputFocus) is called with a not yet viewable window, it will exit
482 * the application which we have to aviod, so wait until it's viewable:
484 while( GrabSuccess != XGrabPointer(
485 fgDisplay.Display, fgStructure.GameModeWindow->Window.Handle,
487 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
489 GrabModeAsync, GrabModeAsync,
490 fgStructure.GameModeWindow->Window.Handle, None, CurrentTime) )
494 * Change input focus to the new window. This will exit the application
495 * if the new window is not viewable yet, see the XGrabPointer loop above.
499 fgStructure.GameModeWindow->Window.Handle,
504 /* Move the Pointer to the middle of the fullscreen window */
508 fgDisplay.RootWindow,
510 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
513 # ifdef X_XF86VidModeSetViewPort
515 if( fgDisplay.DisplayModeValid )
520 /* Change to viewport to the window topleft edge: */
521 if( !XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 ) )
522 fgWarning( "XF86VidModeSetViewPort failed" );
525 * Final window repositioning: It could be avoided using an undecorated
526 * window using override_redirect, but this * would possily require
527 * more changes and investigation.
530 /* Get the current postion of the drawable area on screen */
531 XTranslateCoordinates(
533 fgStructure.CurrentWindow->Window.Handle,
534 fgDisplay.RootWindow,
539 /* Move the decorataions out of the topleft corner of the display */
540 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
546 /* Grab the keyboard, too */
549 fgStructure.GameModeWindow->Window.Handle,
551 GrabModeAsync, GrabModeAsync,
557 return fgStructure.GameModeWindow->ID;
561 * Leaves the game mode
563 void FGAPIENTRY glutLeaveGameMode( void )
565 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );
567 freeglut_return_if_fail( fgStructure.GameModeWindow );
569 fgAddToWindowDestroyList( fgStructure.GameModeWindow );
570 fgStructure.GameModeWindow = NULL;
572 #if TARGET_HOST_POSIX_X11
574 XUngrabPointer( fgDisplay.Display, CurrentTime );
575 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
583 * Returns information concerning the freeglut game mode
585 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
587 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );
591 case GLUT_GAME_MODE_ACTIVE:
592 return !!fgStructure.GameModeWindow;
594 case GLUT_GAME_MODE_POSSIBLE:
595 return fghChangeDisplayMode( GL_TRUE );
597 case GLUT_GAME_MODE_WIDTH:
598 return fgState.GameModeSize.X;
600 case GLUT_GAME_MODE_HEIGHT:
601 return fgState.GameModeSize.Y;
603 case GLUT_GAME_MODE_PIXEL_DEPTH:
604 return fgState.GameModeDepth;
606 case GLUT_GAME_MODE_REFRESH_RATE:
607 return fgState.GameModeRefresh;
609 case GLUT_GAME_MODE_DISPLAY_CHANGED:
611 * This is true if the game mode has been activated successfully..
613 return !!fgStructure.GameModeWindow;
616 fgWarning( "Unknown gamemode get: %d", eWhat );
620 /*** END OF FILE ***/