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 ---------------------------------------------------- */
43 static int xrandr_resize(int xsz, int ysz, int just_checking)
47 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
48 int event_base, error_base;
50 XRRScreenConfiguration *xrr_config;
51 XRRScreenSize *ssizes;
53 int i, ssizes_count, curr;
54 Time timestamp, cfg_timestamp;
56 /* must check at runtime for the availability of the extension */
57 if(!XRRQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
61 if(!(xrr_config = XRRGetScreenInfo(fgDisplay.Display, fgDisplay.RootWindow))) {
62 fgWarning("XRRGetScreenInfo failed");
65 ssizes = XRRConfigSizes(xrr_config, &ssizes_count);
66 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
67 timestamp = XRRConfigTimes(xrr_config, &cfg_timestamp);
69 if(xsz == ssizes[curr].width && ysz == ssizes[curr].height) {
70 /* no need to switch, we're already in the requested mode */
75 for(i=0; i<ssizes_count; i++) {
76 if(ssizes[i].width == xsz && ssizes[i].height == ysz) {
88 if((st = XRRSetScreenConfig(fgDisplay.Display, xrr_config, fgDisplay.RootWindow,
89 i, rot, timestamp)) != 0) {
90 fgWarning("XRRSetScreenConfig failed");
96 XRRFreeScreenConfigInfo(xrr_config);
103 * Remembers the current visual settings, so that
104 * we can change them and restore later...
106 static void fghRememberState( void )
108 #if TARGET_HOST_POSIX_X11
109 int event_base, error_base;
111 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
112 if(XRRQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
113 XRRScreenConfiguration *xrr_config;
114 XRRScreenSize *ssizes;
116 int ssize_count, curr;
118 if((xrr_config = XRRGetScreenInfo(fgDisplay.Display, fgDisplay.RootWindow))) {
119 ssizes = XRRConfigSizes(xrr_config, &ssize_count);
120 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
122 fgDisplay.prev_xsz = ssizes[curr].width;
123 fgDisplay.prev_ysz = ssizes[curr].height;
124 fgDisplay.prev_size_valid = 1;
125 XRRFreeScreenConfigInfo(xrr_config);
132 * This highly depends on the XFree86 extensions,
133 * not approved as X Consortium standards
135 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
136 if(!XF86VidModeQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
141 * Remember the current ViewPort location of the screen to be able to
142 * restore the ViewPort on LeaveGameMode():
144 if( !XF86VidModeGetViewPort(
147 &fgDisplay.DisplayViewPortX,
148 &fgDisplay.DisplayViewPortY ) )
149 fgWarning( "XF86VidModeGetViewPort failed" );
152 * Remember the current pointer location before going fullscreen
153 * for restoring it later:
160 fgDisplay.Display, fgDisplay.RootWindow,
161 &junk_window, &junk_window,
162 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
163 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &mask
167 /* Query the current display settings: */
168 fgDisplay.DisplayModeValid =
169 XF86VidModeGetModeLine(
172 &fgDisplay.DisplayModeClock,
173 &fgDisplay.DisplayMode
176 if( !fgDisplay.DisplayModeValid )
177 fgWarning( "XF86VidModeGetModeLine failed" );
180 #elif TARGET_HOST_MS_WINDOWS
182 /* DEVMODE devMode; */
184 /* Grab the current desktop settings... */
186 /* hack to get around my stupid cross-gcc headers */
187 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
189 EnumDisplaySettings( fgDisplay.DisplayName, FREEGLUT_ENUM_CURRENT_SETTINGS,
190 &fgDisplay.DisplayMode );
192 /* Make sure we will be restoring all settings needed */
193 fgDisplay.DisplayMode.dmFields |=
194 DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
200 * Restores the previously remembered visual settings
202 static void fghRestoreState( void )
204 #if TARGET_HOST_POSIX_X11
206 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
207 if(fgDisplay.prev_size_valid) {
208 if(xrandr_resize(fgDisplay.prev_xsz, fgDisplay.prev_ysz, 0) != -1) {
209 fgDisplay.prev_size_valid = 0;
216 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
217 /* Restore the remembered pointer position: */
219 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
220 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
224 * This highly depends on the XFree86 extensions,
225 * not approved as X Consortium standards
228 if( fgDisplay.DisplayModeValid )
230 XF86VidModeModeInfo** displayModes;
231 int i, displayModesCount;
233 if( !XF86VidModeGetAllModeLines(
239 fgWarning( "XF86VidModeGetAllModeLines failed" );
245 * Check every of the modes looking for one that matches our demands.
246 * If we find one, switch to it and restore the remembered viewport.
248 for( i = 0; i < displayModesCount; i++ )
250 if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
251 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
252 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
254 if( !XF86VidModeSwitchToMode(
257 displayModes[ i ] ) )
259 fgWarning( "XF86VidModeSwitchToMode failed" );
263 if( !XF86VidModeSetViewPort(
266 fgDisplay.DisplayViewPortX,
267 fgDisplay.DisplayViewPortY ) )
268 fgWarning( "HAVE_X11_EXTENSIONS_XF86VMODE_H failed" );
272 * For the case this would be the last X11 call the application
273 * calls exit() we've to flush the X11 output queue to have the
274 * commands sent to the X server before the application exits.
276 XFlush( fgDisplay.Display );
281 XFree( displayModes );
286 #elif TARGET_HOST_MS_WINDOWS
288 /* Restore the previously remembered desktop display settings */
289 ChangeDisplaySettingsEx( fgDisplay.DisplayName,&fgDisplay.DisplayMode, 0,0,0 );
294 #if TARGET_HOST_POSIX_X11
295 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
298 * Checks a single display mode settings against user's preferences.
300 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
302 /* The desired values should be stored in fgState structure... */
303 return ( width == fgState.GameModeSize.X ) &&
304 ( height == fgState.GameModeSize.Y ) &&
305 ( depth == fgState.GameModeDepth ) &&
306 ( refresh == fgState.GameModeRefresh );
310 * Checks all display modes settings against user's preferences.
311 * Returns the mode number found or -1 if none could be found.
313 static int fghCheckDisplayModes( GLboolean exactMatch, int displayModesCount, XF86VidModeModeInfo** displayModes )
316 for( i = 0; i < displayModesCount; i++ )
318 /* Compute the displays refresh rate, dotclock comes in kHz. */
319 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
320 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
322 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
323 displayModes[ i ]->vdisplay,
324 fgState.GameModeDepth,
325 ( exactMatch ? refresh : fgState.GameModeRefresh ) ) ) {
328 /* Update the chosen refresh rate, otherwise a
329 * glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE) would not
330 * return the right values
332 fgState.GameModeRefresh = refresh;
345 * Changes the current display mode to match user's settings
347 static GLboolean fghChangeDisplayMode( GLboolean haveToTest )
349 GLboolean success = GL_FALSE;
350 #if TARGET_HOST_POSIX_X11
352 /* first try to use XRandR, then fallback to XF86VidMode */
353 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
354 if(xrandr_resize(fgState.GameModeSize.X, fgState.GameModeSize.Y, haveToTest) != -1) {
361 * This highly depends on the XFree86 extensions,
362 * not approved as X Consortium standards
364 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
367 * This is also used by applcations which check modes by calling
368 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
370 if( haveToTest || fgDisplay.DisplayModeValid )
372 XF86VidModeModeInfo** displayModes;
373 int i, displayModesCount;
375 if( !XF86VidModeGetAllModeLines(
381 fgWarning( "XF86VidModeGetAllModeLines failed" );
387 * Check every of the modes looking for one that matches our demands,
388 * ignoring the refresh rate if no exact match could be found.
390 i = fghCheckDisplayModes( GL_TRUE, displayModesCount, displayModes );
392 i = fghCheckDisplayModes( GL_FALSE, displayModesCount, displayModes );
394 success = ( i < 0 ) ? GL_FALSE : GL_TRUE;
396 if( !haveToTest && success ) {
397 if( !XF86VidModeSwitchToMode(
400 displayModes[ i ] ) )
401 fgWarning( "XF86VidModeSwitchToMode failed" );
404 XFree( displayModes );
410 * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
411 * XXX game mode will not change screen resolution when activated
417 #elif TARGET_HOST_MS_WINDOWS
420 char *fggmstr = NULL;
424 EnumDisplaySettings( fgDisplay.DisplayName, -1, &devMode );
425 devMode.dmFields = 0;
427 if (fgState.GameModeSize.X!=-1)
429 devMode.dmPelsWidth = fgState.GameModeSize.X;
430 devMode.dmFields |= DM_PELSWIDTH;
432 if (fgState.GameModeSize.Y!=-1)
434 devMode.dmPelsHeight = fgState.GameModeSize.Y;
435 devMode.dmFields |= DM_PELSHEIGHT;
437 if (fgState.GameModeDepth!=-1)
439 devMode.dmBitsPerPel = fgState.GameModeDepth;
440 devMode.dmFields |= DM_BITSPERPEL;
442 if (fgState.GameModeRefresh!=-1)
444 devMode.dmDisplayFrequency = fgState.GameModeRefresh;
445 devMode.dmFields |= DM_DISPLAYFREQUENCY;
448 switch ( ChangeDisplaySettingsEx(fgDisplay.DisplayName, &devMode, NULL, haveToTest ? CDS_TEST : CDS_FULLSCREEN , NULL) )
450 case DISP_CHANGE_SUCCESSFUL:
453 /* update vars in case if windows switched to proper mode */
454 EnumDisplaySettings( fgDisplay.DisplayName, FREEGLUT_ENUM_CURRENT_SETTINGS, &devMode );
455 fgState.GameModeSize.X = devMode.dmPelsWidth;
456 fgState.GameModeSize.Y = devMode.dmPelsHeight;
457 fgState.GameModeDepth = devMode.dmBitsPerPel;
458 fgState.GameModeRefresh = devMode.dmDisplayFrequency;
460 case DISP_CHANGE_RESTART:
461 fggmstr = "The computer must be restarted for the graphics mode to work.";
463 case DISP_CHANGE_BADFLAGS:
464 fggmstr = "An invalid set of flags was passed in.";
466 case DISP_CHANGE_BADPARAM:
467 fggmstr = "An invalid parameter was passed in. This can include an invalid flag or combination of flags.";
469 case DISP_CHANGE_FAILED:
470 fggmstr = "The display driver failed the specified graphics mode.";
472 case DISP_CHANGE_BADMODE:
473 fggmstr = "The graphics mode is not supported.";
476 fggmstr = "Unknown error in graphics mode???"; /* dunno if it is possible,MSDN does not mention any other error */
481 fgWarning(fggmstr); /* I'd rather get info whats going on in my program than wonder about */
482 /* magic happenings behind my back, its lib for devels at last ;) */
489 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
492 * Sets the game mode display string
494 void FGAPIENTRY glutGameModeString( const char* string )
496 int width = -1, height = -1, depth = -1, refresh = -1;
498 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );
501 * This one seems a bit easier than glutInitDisplayString. The bad thing
502 * about it that I was unable to find the game mode string definition, so
503 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
504 * appears in all GLUT game mode programs I have seen to date.
506 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
508 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
509 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
510 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
511 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
512 if( sscanf( string, ":%i", &depth ) != 1 )
513 if( sscanf( string, "@%i", &refresh ) != 1 )
515 "unable to parse game mode string `%s'",
519 /* Hopefully it worked, and if not, we still have the default values */
520 if ( width > 0 ) fgState.GameModeSize.X = width;
521 if ( height > 0 ) fgState.GameModeSize.Y = height;
522 if ( depth > 0 ) fgState.GameModeDepth = depth;
523 if ( refresh > 0 ) fgState.GameModeRefresh = refresh;
529 * Enters the game mode
531 int FGAPIENTRY glutEnterGameMode( void )
533 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );
535 if( fgStructure.GameModeWindow )
536 fgAddToWindowDestroyList( fgStructure.GameModeWindow );
540 if( ! fghChangeDisplayMode( GL_FALSE ) )
542 fgWarning( "failed to change screen settings" );
546 fgStructure.GameModeWindow = fgCreateWindow(
547 NULL, "FREEGLUT", GL_TRUE, 0, 0,
548 GL_TRUE, fgState.GameModeSize.X, fgState.GameModeSize.Y,
552 fgStructure.GameModeWindow->State.Width = fgState.GameModeSize.X;
553 fgStructure.GameModeWindow->State.Height = fgState.GameModeSize.Y;
554 fgStructure.GameModeWindow->State.NeedToResize = GL_TRUE;
556 #if TARGET_HOST_POSIX_X11
559 * Sync needed to avoid a real race, the Xserver must have really created
560 * the window before we can grab the pointer into it:
562 XSync( fgDisplay.Display, False );
565 * Grab the pointer to confine it into the window after the calls to
566 * XWrapPointer() which ensure that the pointer really enters the window.
568 * We also need to wait here until XGrabPointer() returns GrabSuccess,
569 * otherwise the new window is not viewable yet and if the next function
570 * (XSetInputFocus) is called with a not yet viewable window, it will exit
571 * the application which we have to aviod, so wait until it's viewable:
573 while( GrabSuccess != XGrabPointer(
574 fgDisplay.Display, fgStructure.GameModeWindow->Window.Handle,
576 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
578 GrabModeAsync, GrabModeAsync,
579 fgStructure.GameModeWindow->Window.Handle, None, CurrentTime) )
583 * Change input focus to the new window. This will exit the application
584 * if the new window is not viewable yet, see the XGrabPointer loop above.
588 fgStructure.GameModeWindow->Window.Handle,
593 /* Move the Pointer to the middle of the fullscreen window */
597 fgDisplay.RootWindow,
599 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
602 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
604 if( fgDisplay.DisplayModeValid )
609 /* Change to viewport to the window topleft edge: */
610 if( !XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 ) )
611 fgWarning( "XF86VidModeSetViewPort failed" );
614 * Final window repositioning: It could be avoided using an undecorated
615 * window using override_redirect, but this * would possily require
616 * more changes and investigation.
619 /* Get the current postion of the drawable area on screen */
620 XTranslateCoordinates(
622 fgStructure.CurrentWindow->Window.Handle,
623 fgDisplay.RootWindow,
628 /* Move the decorataions out of the topleft corner of the display */
629 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
635 /* Grab the keyboard, too */
638 fgStructure.GameModeWindow->Window.Handle,
640 GrabModeAsync, GrabModeAsync,
646 return fgStructure.GameModeWindow->ID;
650 * Leaves the game mode
652 void FGAPIENTRY glutLeaveGameMode( void )
654 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );
656 freeglut_return_if_fail( fgStructure.GameModeWindow );
658 fgAddToWindowDestroyList( fgStructure.GameModeWindow );
659 fgStructure.GameModeWindow = NULL;
661 #if TARGET_HOST_POSIX_X11
663 XUngrabPointer( fgDisplay.Display, CurrentTime );
664 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
672 * Returns information concerning the freeglut game mode
674 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
676 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );
680 case GLUT_GAME_MODE_ACTIVE:
681 return !!fgStructure.GameModeWindow;
683 case GLUT_GAME_MODE_POSSIBLE:
684 return fghChangeDisplayMode( GL_TRUE );
686 case GLUT_GAME_MODE_WIDTH:
687 return fgState.GameModeSize.X;
689 case GLUT_GAME_MODE_HEIGHT:
690 return fgState.GameModeSize.Y;
692 case GLUT_GAME_MODE_PIXEL_DEPTH:
693 return fgState.GameModeDepth;
695 case GLUT_GAME_MODE_REFRESH_RATE:
696 return fgState.GameModeRefresh;
698 case GLUT_GAME_MODE_DISPLAY_CHANGED:
700 * This is true if the game mode has been activated successfully..
702 return !!fgStructure.GameModeWindow;
705 fgWarning( "Unknown gamemode get: %d", eWhat );
709 /*** END OF FILE ***/