2 * freeglut_gamemode.c
\r
4 * The game mode handling code.
\r
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
\r
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
\r
8 * Creation date: Thu Dec 16 1999
\r
10 * Permission is hereby granted, free of charge, to any person obtaining a
\r
11 * copy of this software and associated documentation files (the "Software"),
\r
12 * to deal in the Software without restriction, including without limitation
\r
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
14 * and/or sell copies of the Software, and to permit persons to whom the
\r
15 * Software is furnished to do so, subject to the following conditions:
\r
17 * The above copyright notice and this permission notice shall be included
\r
18 * in all copies or substantial portions of the Software.
\r
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
28 #include <GL/freeglut.h>
\r
29 #include "freeglut_internal.h"
\r
32 * TODO BEFORE THE STABLE RELEASE:
\r
34 * glutGameModeString() -- missing
\r
35 * glutEnterGameMode() -- X11 version
\r
36 * glutLeaveGameMode() -- is that correct?
\r
37 * glutGameModeGet() -- is that correct?
\r
41 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
\r
42 extern void fgPlatformRememberState( void );
\r
43 extern void fgPlatformRestoreState( void );
\r
44 extern GLboolean fgPlatformChangeDisplayMode( GLboolean haveToTest );
\r
47 #if TARGET_HOST_POSIX_X11
\r
48 static int xrandr_resize(int xsz, int ysz, int rate, int just_checking)
\r
50 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
\r
51 int event_base, error_base, ver_major, ver_minor, use_rate;
\r
52 XRRScreenConfiguration *xrr_config = 0;
\r
55 /* must check at runtime for the availability of the extension */
\r
56 if(!XRRQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
\r
60 XRRQueryVersion(fgDisplay.Display, &ver_major, &ver_minor);
\r
62 /* we only heed the rate if we CAN actually use it (Xrandr >= 1.1) and
\r
63 * the user actually cares about it (rate > 0)
\r
65 use_rate = ( rate > 0 ) && ( ( ver_major >= 1 ) ||
\r
66 ( ( ver_major == 1 ) && ( ver_minor >= 1 ) ) );
\r
68 /* this loop is only so that the whole thing will be repeated if someone
\r
69 * else changes video mode between our query of the current information and
\r
70 * the attempt to change it.
\r
73 XRRScreenSize *ssizes;
\r
76 int i, ssizes_count, rates_count, curr, res_idx = -1;
\r
77 Time timestamp, cfg_timestamp;
\r
80 XRRFreeScreenConfigInfo(xrr_config);
\r
83 if(!(xrr_config = XRRGetScreenInfo(fgDisplay.Display, fgDisplay.RootWindow))) {
\r
84 fgWarning("XRRGetScreenInfo failed");
\r
87 ssizes = XRRConfigSizes(xrr_config, &ssizes_count);
\r
88 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
\r
89 timestamp = XRRConfigTimes(xrr_config, &cfg_timestamp);
\r
91 /* if either of xsz or ysz are unspecified, use the current values */
\r
93 xsz = fgState.GameModeSize.X = ssizes[curr].width;
\r
95 ysz = fgState.GameModeSize.Y = ssizes[curr].height;
\r
98 if(xsz == ssizes[curr].width && ysz == ssizes[curr].height) {
\r
99 /* no need to switch, we're already in the requested resolution */
\r
102 for(i=0; i<ssizes_count; i++) {
\r
103 if(ssizes[i].width == xsz && ssizes[i].height == ysz) {
\r
105 break; /* found it */
\r
110 break; /* no matching resolution */
\r
112 #if ( RANDR_MAJOR >= 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
\r
114 rate = fgState.GameModeRefresh;
\r
116 /* for the selected resolution, let's find out if there is
\r
117 * a matching refresh rate available.
\r
119 rates = XRRConfigRates(xrr_config, res_idx, &rates_count);
\r
121 for(i=0; i<rates_count; i++) {
\r
122 if(rates[i] == rate) {
\r
126 if(i == rates_count) {
\r
127 break; /* no matching rate */
\r
132 if(just_checking) {
\r
137 #if ( RANDR_MAJOR >= 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
\r
139 result = XRRSetScreenConfigAndRate(fgDisplay.Display, xrr_config,
\r
140 fgDisplay.RootWindow, res_idx, rot, rate, timestamp);
\r
143 result = XRRSetScreenConfig(fgDisplay.Display, xrr_config,
\r
144 fgDisplay.RootWindow, res_idx, rot, timestamp);
\r
146 } while(result == RRSetConfigInvalidTime);
\r
149 XRRFreeScreenConfigInfo(xrr_config);
\r
156 #endif /* HAVE_X11_EXTENSIONS_XRANDR_H */
\r
159 #endif /* TARGET_HOST_POSIX_X11 */
\r
161 #if TARGET_HOST_POSIX_X11
\r
163 * Remembers the current visual settings, so that
\r
164 * we can change them and restore later...
\r
166 static void fgPlatformRememberState( void )
\r
168 int event_base, error_base;
\r
171 * Remember the current pointer location before going fullscreen
\r
172 * for restoring it later:
\r
174 Window junk_window;
\r
175 unsigned int junk_mask;
\r
177 XQueryPointer(fgDisplay.Display, fgDisplay.RootWindow,
\r
178 &junk_window, &junk_window,
\r
179 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
\r
180 &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &junk_mask);
\r
182 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
\r
183 if(XRRQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
\r
184 XRRScreenConfiguration *xrr_config;
\r
185 XRRScreenSize *ssizes;
\r
187 int ssize_count, curr;
\r
189 if((xrr_config = XRRGetScreenInfo(fgDisplay.Display, fgDisplay.RootWindow))) {
\r
190 ssizes = XRRConfigSizes(xrr_config, &ssize_count);
\r
191 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
\r
193 fgDisplay.prev_xsz = ssizes[curr].width;
\r
194 fgDisplay.prev_ysz = ssizes[curr].height;
\r
195 fgDisplay.prev_refresh = -1;
\r
197 # if ( RANDR_MAJOR >= 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
\r
198 if(fgState.GameModeRefresh != -1) {
\r
199 fgDisplay.prev_refresh = XRRConfigCurrentRate(xrr_config);
\r
203 fgDisplay.prev_size_valid = 1;
\r
205 XRRFreeScreenConfigInfo(xrr_config);
\r
211 * This highly depends on the XFree86 extensions,
\r
212 * not approved as X Consortium standards
\r
214 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
\r
215 if(!XF86VidModeQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
\r
220 * Remember the current ViewPort location of the screen to be able to
\r
221 * restore the ViewPort on LeaveGameMode():
\r
223 if( !XF86VidModeGetViewPort(
\r
226 &fgDisplay.DisplayViewPortX,
\r
227 &fgDisplay.DisplayViewPortY ) )
\r
228 fgWarning( "XF86VidModeGetViewPort failed" );
\r
231 /* Query the current display settings: */
\r
232 fgDisplay.DisplayModeValid =
\r
233 XF86VidModeGetModeLine(
\r
236 &fgDisplay.DisplayModeClock,
\r
237 &fgDisplay.DisplayMode
\r
240 if( !fgDisplay.DisplayModeValid )
\r
241 fgWarning( "XF86VidModeGetModeLine failed" );
\r
247 #if TARGET_HOST_POSIX_X11
\r
249 * Restores the previously remembered visual settings
\r
251 static void fgPlatformRestoreState( void )
\r
253 /* Restore the remembered pointer position: */
\r
255 fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
\r
256 fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
\r
260 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
\r
261 if(fgDisplay.prev_size_valid) {
\r
262 if(xrandr_resize(fgDisplay.prev_xsz, fgDisplay.prev_ysz, fgDisplay.prev_refresh, 0) != -1) {
\r
263 fgDisplay.prev_size_valid = 0;
\r
264 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
\r
265 fgDisplay.DisplayModeValid = 0;
\r
274 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
\r
276 * This highly depends on the XFree86 extensions,
\r
277 * not approved as X Consortium standards
\r
280 if( fgDisplay.DisplayModeValid )
\r
282 XF86VidModeModeInfo** displayModes;
\r
283 int i, displayModesCount;
\r
285 if( !XF86VidModeGetAllModeLines(
\r
288 &displayModesCount,
\r
291 fgWarning( "XF86VidModeGetAllModeLines failed" );
\r
297 * Check every of the modes looking for one that matches our demands.
\r
298 * If we find one, switch to it and restore the remembered viewport.
\r
300 for( i = 0; i < displayModesCount; i++ )
\r
302 if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
\r
303 displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
\r
304 displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
\r
306 if( !XF86VidModeSwitchToMode(
\r
309 displayModes[ i ] ) )
\r
311 fgWarning( "XF86VidModeSwitchToMode failed" );
\r
315 if( !XF86VidModeSetViewPort(
\r
318 fgDisplay.DisplayViewPortX,
\r
319 fgDisplay.DisplayViewPortY ) )
\r
320 fgWarning( "XF86VidModeSetViewPort failed" );
\r
324 * For the case this would be the last X11 call the application
\r
325 * calls exit() we've to flush the X11 output queue to have the
\r
326 * commands sent to the X server before the application exits.
\r
328 XFlush( fgDisplay.Display );
\r
330 fgDisplay.DisplayModeValid = 0;
\r
331 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
\r
332 fgDisplay.prev_size_valid = 0;
\r
338 XFree( displayModes );
\r
346 #if TARGET_HOST_POSIX_X11
\r
347 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
\r
350 * Checks a single display mode settings against user's preferences.
\r
352 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
\r
354 /* The desired values should be stored in fgState structure... */
\r
355 return ( width == fgState.GameModeSize.X ) &&
\r
356 ( height == fgState.GameModeSize.Y ) &&
\r
357 ( depth == fgState.GameModeDepth ) &&
\r
358 ( refresh == fgState.GameModeRefresh );
\r
362 * Checks all display modes settings against user's preferences.
\r
363 * Returns the mode number found or -1 if none could be found.
\r
365 static int fghCheckDisplayModes( GLboolean exactMatch, int displayModesCount, XF86VidModeModeInfo** displayModes )
\r
368 for( i = 0; i < displayModesCount; i++ )
\r
370 /* Compute the displays refresh rate, dotclock comes in kHz. */
\r
371 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
\r
372 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
\r
374 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
\r
375 displayModes[ i ]->vdisplay,
\r
376 fgState.GameModeDepth,
\r
377 ( exactMatch ? refresh : fgState.GameModeRefresh ) ) ) {
\r
380 /* Update the chosen refresh rate, otherwise a
\r
381 * glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE) would not
\r
382 * return the right values
\r
384 fgState.GameModeRefresh = refresh;
\r
396 #if TARGET_HOST_POSIX_X11
\r
399 * Changes the current display mode to match user's settings
\r
401 static GLboolean fgPlatformChangeDisplayMode( GLboolean haveToTest )
\r
403 GLboolean success = GL_FALSE;
\r
404 /* first try to use XRandR, then fallback to XF86VidMode */
\r
405 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
\r
406 if(xrandr_resize(fgState.GameModeSize.X, fgState.GameModeSize.Y,
\r
407 fgState.GameModeRefresh, haveToTest) != -1) {
\r
414 * This highly depends on the XFree86 extensions,
\r
415 * not approved as X Consortium standards
\r
417 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
\r
420 * This is also used by applications which check modes by calling
\r
421 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
\r
423 if( haveToTest || fgDisplay.DisplayModeValid )
\r
425 XF86VidModeModeInfo** displayModes;
\r
426 int i, displayModesCount;
\r
428 /* If we don't have a valid modeline in the display structure, which
\r
429 * can happen if this is called from glutGameModeGet instead of
\r
430 * glutEnterGameMode, then we need to query the current mode, to make
\r
431 * unspecified settings to default to their current values.
\r
433 if(!fgDisplay.DisplayModeValid) {
\r
434 if(!XF86VidModeGetModeLine(fgDisplay.Display, fgDisplay.Screen,
\r
435 &fgDisplay.DisplayModeClock, &fgDisplay.DisplayMode)) {
\r
440 if (fgState.GameModeSize.X == -1)
\r
442 fgState.GameModeSize.X = fgDisplay.DisplayMode.hdisplay;
\r
444 if (fgState.GameModeSize.Y == -1)
\r
446 fgState.GameModeSize.Y = fgDisplay.DisplayMode.vdisplay;
\r
448 if (fgState.GameModeDepth == -1)
\r
450 /* can't get color depth from this, nor can we change it, do nothing
\r
451 * TODO: get with XGetVisualInfo()? but then how to set?
\r
454 if (fgState.GameModeRefresh == -1)
\r
456 /* Compute the displays refresh rate, dotclock comes in kHz. */
\r
457 int refresh = ( fgDisplay.DisplayModeClock * 1000 ) /
\r
458 ( fgDisplay.DisplayMode.htotal * fgDisplay.DisplayMode.vtotal );
\r
460 fgState.GameModeRefresh = refresh;
\r
463 /* query all possible display modes */
\r
464 if( !XF86VidModeGetAllModeLines(
\r
467 &displayModesCount,
\r
470 fgWarning( "XF86VidModeGetAllModeLines failed" );
\r
476 * Check every of the modes looking for one that matches our demands,
\r
477 * ignoring the refresh rate if no exact match could be found.
\r
479 i = fghCheckDisplayModes( GL_TRUE, displayModesCount, displayModes );
\r
481 i = fghCheckDisplayModes( GL_FALSE, displayModesCount, displayModes );
\r
483 success = ( i < 0 ) ? GL_FALSE : GL_TRUE;
\r
485 if( !haveToTest && success ) {
\r
486 if( !XF86VidModeSwitchToMode(
\r
489 displayModes[ i ] ) )
\r
490 fgWarning( "XF86VidModeSwitchToMode failed" );
\r
493 XFree( displayModes );
\r
503 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
\r
506 * Sets the game mode display string
\r
508 void FGAPIENTRY glutGameModeString( const char* string )
\r
510 int width = -1, height = -1, depth = -1, refresh = -1;
\r
512 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );
\r
515 * This one seems a bit easier than glutInitDisplayString. The bad thing
\r
516 * about it that I was unable to find the game mode string definition, so
\r
517 * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
\r
518 * appears in all GLUT game mode programs I have seen to date.
\r
520 if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
\r
522 if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
\r
523 if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
\r
524 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
\r
525 if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
\r
526 if( sscanf( string, ":%i", &depth ) != 1 )
\r
527 if( sscanf( string, "@%i", &refresh ) != 1 )
\r
529 "unable to parse game mode string `%s'",
\r
533 /* All values not specified are now set to -1, which means those
\r
534 * aspects of the current display mode are not changed in
\r
535 * fgPlatformChangeDisplayMode() above.
\r
537 fgState.GameModeSize.X = width;
\r
538 fgState.GameModeSize.Y = height;
\r
539 fgState.GameModeDepth = depth;
\r
540 fgState.GameModeRefresh = refresh;
\r
546 * Enters the game mode
\r
548 int FGAPIENTRY glutEnterGameMode( void )
\r
550 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );
\r
552 if( fgStructure.GameModeWindow )
\r
553 fgAddToWindowDestroyList( fgStructure.GameModeWindow );
\r
555 fgPlatformRememberState( );
\r
557 if( ! fgPlatformChangeDisplayMode( GL_FALSE ) )
\r
559 fgWarning( "failed to change screen settings" );
\r
563 fgStructure.GameModeWindow = fgCreateWindow(
\r
564 NULL, "FREEGLUT", GL_TRUE, 0, 0,
\r
565 GL_TRUE, fgState.GameModeSize.X, fgState.GameModeSize.Y,
\r
569 fgStructure.GameModeWindow->State.Width = fgState.GameModeSize.X;
\r
570 fgStructure.GameModeWindow->State.Height = fgState.GameModeSize.Y;
\r
571 fgStructure.GameModeWindow->State.NeedToResize = GL_TRUE;
\r
573 #if TARGET_HOST_POSIX_X11
\r
576 * Sync needed to avoid a real race, the Xserver must have really created
\r
577 * the window before we can grab the pointer into it:
\r
579 XSync( fgDisplay.Display, False );
\r
581 * Grab the pointer to confine it into the window after the calls to
\r
582 * XWrapPointer() which ensure that the pointer really enters the window.
\r
584 * We also need to wait here until XGrabPointer() returns GrabSuccess,
\r
585 * otherwise the new window is not viewable yet and if the next function
\r
586 * (XSetInputFocus) is called with a not yet viewable window, it will exit
\r
587 * the application which we have to aviod, so wait until it's viewable:
\r
589 while( GrabSuccess != XGrabPointer(
\r
590 fgDisplay.Display, fgStructure.GameModeWindow->Window.Handle,
\r
592 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
\r
593 | PointerMotionMask,
\r
594 GrabModeAsync, GrabModeAsync,
\r
595 fgStructure.GameModeWindow->Window.Handle, None, CurrentTime) )
\r
598 * Change input focus to the new window. This will exit the application
\r
599 * if the new window is not viewable yet, see the XGrabPointer loop above.
\r
603 fgStructure.GameModeWindow->Window.Handle,
\r
608 /* Move the Pointer to the middle of the fullscreen window */
\r
612 fgDisplay.RootWindow,
\r
614 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
\r
617 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
\r
619 if( fgDisplay.DisplayModeValid )
\r
624 /* Change to viewport to the window topleft edge: */
\r
625 if( !XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 ) )
\r
626 fgWarning( "XF86VidModeSetViewPort failed" );
\r
629 * Final window repositioning: It could be avoided using an undecorated
\r
630 * window using override_redirect, but this * would possily require
\r
631 * more changes and investigation.
\r
634 /* Get the current postion of the drawable area on screen */
\r
635 XTranslateCoordinates(
\r
637 fgStructure.CurrentWindow->Window.Handle,
\r
638 fgDisplay.RootWindow,
\r
643 /* Move the decorataions out of the topleft corner of the display */
\r
644 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
\r
650 /* Grab the keyboard, too */
\r
653 fgStructure.GameModeWindow->Window.Handle,
\r
655 GrabModeAsync, GrabModeAsync,
\r
661 return fgStructure.GameModeWindow->ID;
\r
665 * Leaves the game mode
\r
667 void FGAPIENTRY glutLeaveGameMode( void )
\r
669 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );
\r
671 freeglut_return_if_fail( fgStructure.GameModeWindow );
\r
673 fgAddToWindowDestroyList( fgStructure.GameModeWindow );
\r
674 fgStructure.GameModeWindow = NULL;
\r
676 #if TARGET_HOST_POSIX_X11
\r
678 XUngrabPointer( fgDisplay.Display, CurrentTime );
\r
679 XUngrabKeyboard( fgDisplay.Display, CurrentTime );
\r
683 fgPlatformRestoreState();
\r
687 * Returns information concerning the freeglut game mode
\r
689 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
\r
691 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );
\r
695 case GLUT_GAME_MODE_ACTIVE:
\r
696 return !!fgStructure.GameModeWindow;
\r
698 case GLUT_GAME_MODE_POSSIBLE:
\r
699 return fgPlatformChangeDisplayMode( GL_TRUE );
\r
701 case GLUT_GAME_MODE_WIDTH:
\r
702 return fgState.GameModeSize.X;
\r
704 case GLUT_GAME_MODE_HEIGHT:
\r
705 return fgState.GameModeSize.Y;
\r
707 case GLUT_GAME_MODE_PIXEL_DEPTH:
\r
708 return fgState.GameModeDepth;
\r
710 case GLUT_GAME_MODE_REFRESH_RATE:
\r
711 return fgState.GameModeRefresh;
\r
713 case GLUT_GAME_MODE_DISPLAY_CHANGED:
\r
715 * This is true if the game mode has been activated successfully..
\r
717 return !!fgStructure.GameModeWindow;
\r
720 fgWarning( "Unknown gamemode get: %d", eWhat );
\r
724 /*** END OF FILE ***/
\r