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 #define G_LOG_DOMAIN "freeglut-gamemode"
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * glutGameModeString() -- missing
41 * glutEnterGameMode() -- X11 version
42 * glutLeaveGameMode() -- is that correct?
43 * glutGameModeGet() -- is that correct?
47 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
50 * Remembers the current visual settings, so that
51 * we can change them and restore later...
53 void fghRememberState( void )
55 #if TARGET_HOST_UNIX_X11
58 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
60 # ifdef X_XF86VidModeGetModeLine
64 * Remember the current ViewPort location of the screen to be able to
65 * restore the ViewPort on LeaveGameMode():
67 XF86VidModeGetViewPort(
70 &fgDisplay.DisplayViewPortX,
71 &fgDisplay.DisplayViewPortY
75 * Remember the current pointer location before going fullscreen
76 * for restoring it later:
83 fgDisplay.Display, fgDisplay.RootWindow,
84 &junk_window, &junk_window,
85 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
86 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &mask
91 * Query the current display settings:
93 fgDisplay.DisplayModeValid =
94 XF86VidModeGetModeLine(
97 &fgDisplay.DisplayModeClock,
98 &fgDisplay.DisplayMode
101 if (!fgDisplay.DisplayModeValid)
102 fgWarning( "Runtime use of XF86VidModeGetModeLine failed.\n" );
105 # warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
108 #elif TARGET_HOST_WIN32
110 /* DEVMODE devMode; */
113 * Grab the current desktop settings...
116 /* hack to get around my stupid cross-gcc headers */
117 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
119 EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode );
122 * Make sure we will be restoring all settings needed
124 fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
130 * Restores the previously remembered visual settings
132 void fghRestoreState( void )
134 #if TARGET_HOST_UNIX_X11
136 # ifdef X_XF86VidModeGetAllModeLines
138 * Restore the remembered pointer position:
141 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
142 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
146 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
149 if (fgDisplay.DisplayModeValid)
151 XF86VidModeModeInfo** displayModes;
152 int i, displayModesCount;
155 * Query for all the display available...
157 XF86VidModeGetAllModeLines(
165 * Check every of the modes looking for one that matches our demands
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 )
174 * OK, this is the display mode we have been looking for...
176 XF86VidModeSwitchToMode(
183 * Now we can restore the remembered ViewPort:
185 XF86VidModeSetViewPort(
188 fgDisplay.DisplayViewPortX,
189 fgDisplay.DisplayViewPortY
193 * For the case this would be the last X11 call the application
194 * calls exit() we've to flush the X11 output queue to have the
195 * commands sent to the X server before the application exists.
197 XFlush(fgDisplay.Display);
205 # warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
208 #elif TARGET_HOST_WIN32
211 * Restore the previously rememebered desktop display settings
213 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
219 * Checks the display mode settings against user's preferences
221 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
224 * The desired values should be stored in fgState structure...
226 return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
227 (depth == fgState.GameModeDepth) && (refresh == fgState.GameModeRefresh) );
231 * Changes the current display mode to match user's settings
233 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
235 #if TARGET_HOST_UNIX_X11
238 * This highly depends on the XFree86 extensions, not approved as X Consortium standards
240 # ifdef X_XF86VidModeGetAllModeLines
243 * This is also used by applcations which check modes by calling
244 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
246 if (haveToTest || fgDisplay.DisplayModeValid)
248 XF86VidModeModeInfo** displayModes;
249 int i, displayModesCount;
252 * Query for all the display available...
254 XF86VidModeGetAllModeLines(
262 * Check every of the modes looking for one that matches our demands
264 for( i=0; i<displayModesCount; i++ )
266 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
267 fgState.GameModeDepth, fgState.GameModeRefresh ) )
272 * OKi, this is the display mode we have been looking for...
274 XF86VidModeSwitchToMode(
280 * Return successfull...
288 * Something must have went wrong
293 # warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
296 #elif TARGET_HOST_WIN32
298 unsigned int displayModes = 0, mode = 0xffffffff;
299 GLboolean success = FALSE;
304 * Enumerate the available display modes
305 * Try to get a complete match
307 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
310 * Does the enumerated display mode match the user's preferences?
312 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
313 devMode.dmBitsPerPel, devMode.dmDisplayFrequency ) )
316 * OKi, we've found a matching display mode, remember its number and break
323 * Switch to the next display mode, if any
328 if ( mode == 0xffffffff )
330 /* then try without Display Frequency */
334 * Enumerate the available display modes
336 while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
338 /* then try without Display Frequency */
340 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
341 devMode.dmBitsPerPel, fgState.GameModeRefresh))
344 * OKi, we've found a matching display mode, remember its number and break
351 * Switch to the next display mode, if any
358 * Did we find a matching display mode?
360 if( mode != 0xffffffff )
362 int retVal = DISP_CHANGE_SUCCESSFUL;
365 * Mark the values we want to modify in the display change call
367 devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
370 * Change the current display mode (possibly in test mode only)
372 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
375 * I don't know if it's really needed, but looks nice:
377 success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
380 * If it was not a test, remember the current screen settings
382 if( !haveToTest && success )
384 fgState.GameModeSize.X = devMode.dmPelsWidth;
385 fgState.GameModeSize.Y = devMode.dmPelsHeight;
386 fgState.GameModeDepth = devMode.dmBitsPerPel;
387 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
392 * Otherwise we must have failed somewhere
400 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
403 * Sets the game mode display string
405 void FGAPIENTRY glutGameModeString( const char* string )
407 int width = 640, height = 480, depth = 16, refresh = 72;
410 * This one seems a bit easier than glutInitDisplayString. The bad thing
411 * about it that I was unable to find the game mode string definition, so
412 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
413 * appears in all GLUT game mode programs I have seen to date.
415 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
416 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
417 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
418 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
419 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
420 if( sscanf( string, ":%i", &depth ) != 1 )
421 if( sscanf( string, "@%i", &refresh ) != 1 )
422 fgWarning( "unable to parse game mode string `%s'", string );
425 * Hopefully it worked, and if not, we still have the default values
427 fgState.GameModeSize.X = width;
428 fgState.GameModeSize.Y = height;
429 fgState.GameModeDepth = depth;
430 fgState.GameModeRefresh = refresh;
434 * Enters the game mode
436 int FGAPIENTRY glutEnterGameMode( void )
439 * Check if a game mode window already exists...
441 if( fgStructure.GameMode != NULL )
444 * ...if so, delete it before proceeding...
446 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
451 * ...otherwise remember the current resolution, etc.
457 * We are ready to change the current screen's resolution now
459 if( fghChangeDisplayMode( FALSE ) == FALSE )
461 fgWarning( "failed to change screen settings" );
466 * Finally, have the game mode window created
468 fgStructure.GameMode = fgCreateWindow(
469 NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE
472 #if TARGET_HOST_UNIX_X11
474 /* Move the window up to the topleft corner */
475 XMoveWindow(fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0);
478 * Sync needed to avoid a real race, the Xserver must have really created
479 * the window before we can grab the pointer into it:
481 XSync(fgDisplay.Display, False);
483 /* Move the Pointer to the middle of the fullscreen window */
487 fgDisplay.RootWindow,
489 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
493 * Grab the pointer to confine it into the window after the calls to
494 * XWrapPointer() which ensure that the pointer really enters the window.
496 * We also need to wait here until XGrabPointer() returns GrabSuccess,
497 * otherwise the new window is not viewable yet and if the next function
498 * (XSetInputFocus) is called with a not yet viewable window, it will exit
499 * the application which we have to aviod, so wait until it's viewable:
501 while (GrabSuccess != XGrabPointer(
502 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
503 TRUE, ButtonPressMask|ButtonReleaseMask|ButtonMotionMask
505 GrabModeAsync, GrabModeAsync,
506 fgStructure.GameMode->Window.Handle, None, CurrentTime)) {
511 * Change input focus to the new window. This will exit the application
512 * if the new window is not viewable yet, see the XGrabPointer loop above.
516 fgStructure.GameMode->Window.Handle,
521 # ifdef X_XF86VidModeSetViewPort
523 if (fgDisplay.DisplayModeValid) {
528 * Change to viewport to the window topleft edge:
530 XF86VidModeSetViewPort(fgDisplay.Display, fgDisplay.Screen, 0, 0);
533 * Final window repositioning: It could be avoided using an undecorated
534 * window using override_redirect, but this * would possily require more
535 * changes and investigation.
538 /* Get the current postion of the drawable area on screen */
539 XTranslateCoordinates(
541 fgStructure.Window->Window.Handle,
542 fgDisplay.RootWindow,
547 /* Move the decorataions out of the topleft corner of the display */
548 XMoveWindow(fgDisplay.Display, fgStructure.Window->Window.Handle, -x, -y);
554 * Grab the keyboard, too
558 fgStructure.GameMode->Window.Handle,
560 GrabModeAsync, GrabModeAsync,
573 * Leaves the game mode
575 void FGAPIENTRY glutLeaveGameMode( void )
577 freeglut_return_if_fail( fgStructure.GameMode != NULL );
580 * First of all, have the game mode window destroyed
582 fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
584 #if TARGET_HOST_UNIX_X11
587 * Ungrab the mouse and keyboard
589 XUngrabPointer( fgDisplay.Display, CurrentTime );
590 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
595 * Then, have the desktop visual settings restored
601 * Returns information concerning the freeglut game mode
603 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
606 * See why are we bothered
610 case GLUT_GAME_MODE_ACTIVE:
612 * Check if the game mode is currently active
614 return( fgStructure.GameMode != NULL );
616 case GLUT_GAME_MODE_POSSIBLE:
618 * Check if the current game mode settings are valid
620 return( fghChangeDisplayMode( TRUE ) );
622 case GLUT_GAME_MODE_WIDTH:
624 * The game mode screen width
626 return( fgState.GameModeSize.X );
628 case GLUT_GAME_MODE_HEIGHT:
630 * The game mode screen height
632 return( fgState.GameModeSize.Y );
634 case GLUT_GAME_MODE_PIXEL_DEPTH:
636 * The game mode pixel depth
638 return( fgState.GameModeDepth );
640 case GLUT_GAME_MODE_REFRESH_RATE:
642 * The game mode refresh rate
644 return( fgState.GameModeRefresh );
646 case GLUT_GAME_MODE_DISPLAY_CHANGED:
648 * This is true if the game mode has been activated successfully..
650 return( fgStructure.GameMode != NULL );
656 /*** END OF FILE ***/