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 XF86VidModeGetViewPort(
65 &fgDisplay.DisplayViewPortX,
66 &fgDisplay.DisplayViewPortY
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( "Runtime use of 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 XF86VidModeGetAllModeLines(
155 * Check every of the modes looking for one that matches our demands.
156 * If we find one, switch to it and restore the remembered viewport.
158 for( i = 0; i < displayModesCount; i++ )
160 if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
161 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
162 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
164 XF86VidModeSwitchToMode(
169 XF86VidModeSetViewPort(
172 fgDisplay.DisplayViewPortX,
173 fgDisplay.DisplayViewPortY
177 * For the case this would be the last X11 call the application
178 * calls exit() we've to flush the X11 output queue to have the
179 * commands sent to the X server before the application exits.
181 XFlush( fgDisplay.Display );
186 XFree( displayModes );
191 * XXX warning fghRestoreState: missing XFree86 video mode extensions,
192 * XXX game mode will not change screen resolution when activated
196 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
198 /* Restore the previously rememebered desktop display settings */
199 ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
205 * Checks the display mode settings against user's preferences
207 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
209 /* The desired values should be stored in fgState structure... */
210 return ( width == fgState.GameModeSize.X ) &&
211 ( height == fgState.GameModeSize.Y ) &&
212 ( depth == fgState.GameModeDepth ) &&
213 (refresh == fgState.GameModeRefresh );
217 * Changes the current display mode to match user's settings
219 static GLboolean fghChangeDisplayMode( GLboolean haveToTest )
221 GLboolean success = GL_FALSE;
222 #if TARGET_HOST_UNIX_X11
225 * This highly depends on the XFree86 extensions,
226 * not approved as X Consortium standards
228 # ifdef X_XF86VidModeGetAllModeLines
231 * This is also used by applcations which check modes by calling
232 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
234 if( haveToTest || fgDisplay.DisplayModeValid )
236 XF86VidModeModeInfo** displayModes;
237 int i, ignoreRefreshRate, displayModesCount;
239 XF86VidModeGetAllModeLines(
247 * Check every of the modes looking for one that matches our demands,
248 * ignoring the refresh rate if no exact match could be found.
250 for( ignoreRefreshRate = 0;
251 !success && ( ignoreRefreshRate <= 1 );
255 !success && ( i < displayModesCount );
258 /* Compute the displays refresh rate, dotclock comes in kHz. */
259 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
260 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
262 success = fghCheckDisplayMode( displayModes[ i ]->hdisplay,
263 displayModes[ i ]->vdisplay,
264 fgState.GameModeDepth,
265 ( ignoreRefreshRate ? fgState.GameModeRefresh : refresh ) );
269 if( !haveToTest && success ) {
270 XF86VidModeSwitchToMode( fgDisplay.Display, fgDisplay.Screen, displayModes[ i ] );
273 XFree( displayModes );
278 * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
279 * XXX game mode will not change screen resolution when activated
283 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
285 unsigned int displayModes = 0, mode = 0xffffffff;
290 * Enumerate the available display modes
291 * Try to get a complete match
293 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
295 /* Does the enumerated display mode match the user's preferences? */
296 if( fghCheckDisplayMode( devMode.dmPelsWidth, devMode.dmPelsHeight,
297 devMode.dmBitsPerPel,
298 devMode.dmDisplayFrequency ) )
306 if( mode == 0xffffffff )
308 /* then try without Display Frequency */
311 /* Enumerate the available display modes */
312 while( EnumDisplaySettings( NULL, displayModes, &devMode ) )
314 /* then try without Display Frequency */
315 if( fghCheckDisplayMode( devMode.dmPelsWidth,
316 devMode.dmPelsHeight,
317 devMode.dmBitsPerPel,
318 fgState.GameModeRefresh ) )
327 /* Did we find a matching display mode? */
328 if( mode != 0xffffffff )
330 int retVal = DISP_CHANGE_SUCCESSFUL;
332 /* Mark the values we want to modify in the display change call */
334 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
336 retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
338 /* I don't know if it's really needed, but looks nice: */
339 success = (retVal == DISP_CHANGE_SUCCESSFUL) ||
340 (retVal == DISP_CHANGE_NOTUPDATED);
342 if( !haveToTest && success )
344 fgState.GameModeSize.X = devMode.dmPelsWidth;
345 fgState.GameModeSize.Y = devMode.dmPelsHeight;
346 fgState.GameModeDepth = devMode.dmBitsPerPel;
347 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
357 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
360 * Sets the game mode display string
362 void FGAPIENTRY glutGameModeString( const char* string )
364 int width = 640, height = 480, depth = 16, refresh = 72;
366 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );
369 * This one seems a bit easier than glutInitDisplayString. The bad thing
370 * about it that I was unable to find the game mode string definition, so
371 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
372 * appears in all GLUT game mode programs I have seen to date.
374 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
376 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
377 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
378 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
379 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
380 if( sscanf( string, ":%i", &depth ) != 1 )
381 if( sscanf( string, "@%i", &refresh ) != 1 )
383 "unable to parse game mode string `%s'",
387 /* Hopefully it worked, and if not, we still have the default values */
388 fgState.GameModeSize.X = width;
389 fgState.GameModeSize.Y = height;
390 fgState.GameModeDepth = depth;
391 fgState.GameModeRefresh = refresh;
395 * Enters the game mode
397 int FGAPIENTRY glutEnterGameMode( void )
399 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );
401 if( fgStructure.GameMode )
402 fgAddToWindowDestroyList( fgStructure.GameMode );
406 if( ! fghChangeDisplayMode( GL_FALSE ) )
408 fgWarning( "failed to change screen settings" );
412 fgStructure.GameMode = fgCreateWindow(
413 NULL, "FREEGLUT", 0, 0,
414 fgState.GameModeSize.X, fgState.GameModeSize.Y, GL_TRUE, GL_FALSE
417 fgStructure.GameMode->State.IsGameMode = GL_TRUE;
419 #if TARGET_HOST_UNIX_X11
421 /* Move the window up to the topleft corner */
422 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, 0, 0 );
425 * Sync needed to avoid a real race, the Xserver must have really created
426 * the window before we can grab the pointer into it:
428 XSync( fgDisplay.Display, False );
430 /* Move the Pointer to the middle of the fullscreen window */
434 fgDisplay.RootWindow,
436 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
440 * Grab the pointer to confine it into the window after the calls to
441 * XWrapPointer() which ensure that the pointer really enters the window.
443 * We also need to wait here until XGrabPointer() returns GrabSuccess,
444 * otherwise the new window is not viewable yet and if the next function
445 * (XSetInputFocus) is called with a not yet viewable window, it will exit
446 * the application which we have to aviod, so wait until it's viewable:
448 while( GrabSuccess != XGrabPointer(
449 fgDisplay.Display, fgStructure.GameMode->Window.Handle,
451 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
453 GrabModeAsync, GrabModeAsync,
454 fgStructure.GameMode->Window.Handle, None, CurrentTime) )
458 * Change input focus to the new window. This will exit the application
459 * if the new window is not viewable yet, see the XGrabPointer loop above.
463 fgStructure.GameMode->Window.Handle,
468 # ifdef X_XF86VidModeSetViewPort
470 if( fgDisplay.DisplayModeValid )
475 /* Change to viewport to the window topleft edge: */
476 XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 );
479 * Final window repositioning: It could be avoided using an undecorated
480 * window using override_redirect, but this * would possily require
481 * more changes and investigation.
484 /* Get the current postion of the drawable area on screen */
485 XTranslateCoordinates(
487 fgStructure.Window->Window.Handle,
488 fgDisplay.RootWindow,
493 /* Move the decorataions out of the topleft corner of the display */
494 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
500 /* Grab the keyboard, too */
503 fgStructure.GameMode->Window.Handle,
505 GrabModeAsync, GrabModeAsync,
515 * Leaves the game mode
517 void FGAPIENTRY glutLeaveGameMode( void )
519 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );
521 freeglut_return_if_fail( fgStructure.GameMode );
523 fgStructure.GameMode->State.IsGameMode = GL_FALSE;
525 fgAddToWindowDestroyList( fgStructure.GameMode );
526 fgStructure.GameMode = NULL;
528 #if TARGET_HOST_UNIX_X11
530 XUngrabPointer( fgDisplay.Display, CurrentTime );
531 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
539 * Returns information concerning the freeglut game mode
541 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
543 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );
547 case GLUT_GAME_MODE_ACTIVE:
548 return !!fgStructure.GameMode;
550 case GLUT_GAME_MODE_POSSIBLE:
551 return fghChangeDisplayMode( GL_TRUE );
553 case GLUT_GAME_MODE_WIDTH:
554 return fgState.GameModeSize.X;
556 case GLUT_GAME_MODE_HEIGHT:
557 return fgState.GameModeSize.Y;
559 case GLUT_GAME_MODE_PIXEL_DEPTH:
560 return fgState.GameModeDepth;
562 case GLUT_GAME_MODE_REFRESH_RATE:
563 return fgState.GameModeRefresh;
565 case GLUT_GAME_MODE_DISPLAY_CHANGED:
567 * This is true if the game mode has been activated successfully..
569 return !!fgStructure.GameMode;
572 fgWarning( "Unknown gamemode get: %d", eWhat );
576 /*** END OF FILE ***/