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 * Copied for Platform code by Evan Felix <karcaw at gmail.com>
9 * Creation date: Thur Feb 2 2012
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <GL/freeglut.h>
30 #include "../fg_internal.h"
32 /* we'll try to use XR&R if it's available at compile-time, and at runtime, and the user
33 * hasn't explicitly disabled it by setting the FREEGLUT_NO_XRANDR env-var.
35 static int use_xrandr(void)
37 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
38 int event_base, error_base;
39 if(!XRRQueryExtension(fgDisplay.pDisplay.Display, &event_base, &error_base)) {
42 if(getenv("FREEGLUT_NO_XRANDR")) {
47 return 0; /* no compile-time support */
51 /* we'll try to use XF86VidMode if it's available at compile-time, and at runtime, and the
52 * user hasn't explicitly disabled it by setting the FREEGLUT_NO_XF86VM env-var.
54 static int use_xf86vm(void)
56 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
57 int event_base, error_base;
58 if(!XF86VidModeQueryExtension(fgDisplay.pDisplay.Display, &event_base, &error_base)) {
61 if(getenv("FREEGLUT_NO_XF86VM")) {
66 return 0; /* no compile-time support */
71 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
72 static int xrandr_resize(int xsz, int ysz, int rate, int just_checking)
74 int ver_major, ver_minor, use_rate;
75 XRRScreenConfiguration *xrr_config = 0;
78 /* NOTE: we have already determined that XR&R is availble and enabled before calling this */
80 XRRQueryVersion(fgDisplay.pDisplay.Display, &ver_major, &ver_minor);
82 /* we only heed the rate if we CAN actually use it (Xrandr >= 1.1) and
83 * the user actually cares about it (rate > 0)
85 use_rate = ( rate > 0 ) && ( ( ver_major > 1 ) ||
86 ( ( ver_major == 1 ) && ( ver_minor >= 1 ) ) );
88 /* this loop is only so that the whole thing will be repeated if someone
89 * else changes video mode between our query of the current information and
90 * the attempt to change it.
93 XRRScreenSize *ssizes;
96 int i, ssizes_count, rates_count, curr, res_idx = -1;
97 Time timestamp, cfg_timestamp;
100 XRRFreeScreenConfigInfo(xrr_config);
103 if(!(xrr_config = XRRGetScreenInfo(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow))) {
104 fgWarning("XRRGetScreenInfo failed");
107 ssizes = XRRConfigSizes(xrr_config, &ssizes_count);
108 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
109 timestamp = XRRConfigTimes(xrr_config, &cfg_timestamp);
111 /* if either of xsz or ysz are unspecified, use the current values */
113 xsz = fgState.GameModeSize.X = ssizes[curr].width;
115 ysz = fgState.GameModeSize.Y = ssizes[curr].height;
118 if(xsz == ssizes[curr].width && ysz == ssizes[curr].height) {
119 /* no need to switch, we're already in the requested resolution */
122 for(i=0; i<ssizes_count; i++) {
123 if(ssizes[i].width == xsz && ssizes[i].height == ysz) {
125 break; /* found it */
130 break; /* no matching resolution */
132 #if ( RANDR_MAJOR > 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
134 rate = fgState.GameModeRefresh;
136 /* for the selected resolution, let's find out if there is
137 * a matching refresh rate available.
139 rates = XRRConfigRates(xrr_config, res_idx, &rates_count);
141 for(i=0; i<rates_count; i++) {
142 if(rates[i] == rate) {
146 if(i == rates_count) {
147 break; /* no matching rate */
157 #if ( RANDR_MAJOR > 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
159 result = XRRSetScreenConfigAndRate(fgDisplay.pDisplay.Display, xrr_config,
160 fgDisplay.pDisplay.RootWindow, res_idx, rot, rate, timestamp);
163 result = XRRSetScreenConfig(fgDisplay.pDisplay.Display, xrr_config,
164 fgDisplay.pDisplay.RootWindow, res_idx, rot, timestamp);
166 } while(result == RRSetConfigInvalidTime);
169 XRRFreeScreenConfigInfo(xrr_config);
178 #endif /* HAVE_X11_EXTENSIONS_XRANDR_H */
181 * Remembers the current visual settings, so that
182 * we can change them and restore later...
184 void fgPlatformRememberState( void )
187 * Remember the current pointer location before going fullscreen
188 * for restoring it later:
191 unsigned int junk_mask;
193 XQueryPointer(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow,
194 &junk_window, &junk_window,
195 &fgDisplay.pDisplay.DisplayPointerX, &fgDisplay.pDisplay.DisplayPointerY,
196 &fgDisplay.pDisplay.DisplayPointerX, &fgDisplay.pDisplay.DisplayPointerY, &junk_mask);
198 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
200 XRRScreenConfiguration *xrr_config;
201 XRRScreenSize *ssizes;
203 int ssize_count, curr;
205 if((xrr_config = XRRGetScreenInfo(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow))) {
206 ssizes = XRRConfigSizes(xrr_config, &ssize_count);
207 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
209 fgDisplay.pDisplay.prev_xsz = ssizes[curr].width;
210 fgDisplay.pDisplay.prev_ysz = ssizes[curr].height;
211 fgDisplay.pDisplay.prev_refresh = -1;
213 # if ( RANDR_MAJOR > 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
214 if(fgState.GameModeRefresh != -1) {
215 fgDisplay.pDisplay.prev_refresh = XRRConfigCurrentRate(xrr_config);
219 fgDisplay.pDisplay.prev_size_valid = 1;
221 XRRFreeScreenConfigInfo(xrr_config);
224 # endif /* HAVE_X11_EXTENSIONS_XRANDR_H */
227 * This highly depends on the XFree86 extensions,
228 * not approved as X Consortium standards
230 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
233 * Remember the current ViewPort location of the screen to be able to
234 * restore the ViewPort on LeaveGameMode():
236 if( !XF86VidModeGetViewPort(
237 fgDisplay.pDisplay.Display,
238 fgDisplay.pDisplay.Screen,
239 &fgDisplay.pDisplay.DisplayViewPortX,
240 &fgDisplay.pDisplay.DisplayViewPortY ) )
241 fgWarning( "XF86VidModeGetViewPort failed" );
244 /* Query the current display settings: */
245 fgDisplay.pDisplay.DisplayModeValid =
246 XF86VidModeGetModeLine(
247 fgDisplay.pDisplay.Display,
248 fgDisplay.pDisplay.Screen,
249 &fgDisplay.pDisplay.DisplayModeClock,
250 &fgDisplay.pDisplay.DisplayMode
253 if( !fgDisplay.pDisplay.DisplayModeValid )
254 fgWarning( "XF86VidModeGetModeLine failed" );
261 * Restores the previously remembered visual settings
263 void fgPlatformRestoreState( void )
265 /* Restore the remembered pointer position: */
267 fgDisplay.pDisplay.Display, None, fgDisplay.pDisplay.RootWindow, 0, 0, 0, 0,
268 fgDisplay.pDisplay.DisplayPointerX, fgDisplay.pDisplay.DisplayPointerY
271 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
273 if(fgDisplay.pDisplay.prev_size_valid) {
274 if(xrandr_resize(fgDisplay.pDisplay.prev_xsz, fgDisplay.pDisplay.prev_ysz, fgDisplay.pDisplay.prev_refresh, 0) != -1) {
275 fgDisplay.pDisplay.prev_size_valid = 0;
276 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
277 fgDisplay.pDisplay.DisplayModeValid = 0;
281 return; /* don't fall back to XF86VidMode if we have XR&R */
283 #endif /* HAVE_X11_EXTENSIONS_XRANDR_H */
287 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
289 * This highly depends on the XFree86 extensions,
290 * not approved as X Consortium standards
294 if( fgDisplay.pDisplay.DisplayModeValid )
296 XF86VidModeModeInfo** displayModes;
297 int i, displayModesCount;
299 if( !XF86VidModeGetAllModeLines(
300 fgDisplay.pDisplay.Display,
301 fgDisplay.pDisplay.Screen,
305 fgWarning( "XF86VidModeGetAllModeLines failed" );
311 * Check every of the modes looking for one that matches our demands.
312 * If we find one, switch to it and restore the remembered viewport.
314 for( i = 0; i < displayModesCount; i++ )
316 if(displayModes[ i ]->hdisplay == fgDisplay.pDisplay.DisplayMode.hdisplay &&
317 displayModes[ i ]->vdisplay == fgDisplay.pDisplay.DisplayMode.vdisplay &&
318 displayModes[ i ]->dotclock == fgDisplay.pDisplay.DisplayModeClock )
320 if( !XF86VidModeSwitchToMode(
321 fgDisplay.pDisplay.Display,
322 fgDisplay.pDisplay.Screen,
323 displayModes[ i ] ) )
325 fgWarning( "XF86VidModeSwitchToMode failed" );
329 if( !XF86VidModeSetViewPort(
330 fgDisplay.pDisplay.Display,
331 fgDisplay.pDisplay.Screen,
332 fgDisplay.pDisplay.DisplayViewPortX,
333 fgDisplay.pDisplay.DisplayViewPortY ) )
334 fgWarning( "XF86VidModeSetViewPort failed" );
338 * For the case this would be the last X11 call the application
339 * calls exit() we've to flush the X11 output queue to have the
340 * commands sent to the X server before the application exits.
342 XFlush( fgDisplay.pDisplay.Display );
344 fgDisplay.pDisplay.DisplayModeValid = 0;
345 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
346 fgDisplay.pDisplay.prev_size_valid = 0;
351 XFree( displayModes );
354 #endif /* HAVE_X11_EXTENSIONS_XF86VMODE_H */
357 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
360 * Checks a single display mode settings against user's preferences.
362 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
364 /* The desired values should be stored in fgState structure... */
365 return ( width == fgState.GameModeSize.X ) &&
366 ( height == fgState.GameModeSize.Y ) &&
367 ( depth == fgState.GameModeDepth ) &&
368 ( refresh == fgState.GameModeRefresh );
372 * Checks all display modes settings against user's preferences.
373 * Returns the mode number found or -1 if none could be found.
375 static int fghCheckDisplayModes( GLboolean exactMatch, int displayModesCount, XF86VidModeModeInfo** displayModes )
378 for( i = 0; i < displayModesCount; i++ )
380 /* Compute the displays refresh rate, dotclock comes in kHz. */
381 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
382 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
384 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
385 displayModes[ i ]->vdisplay,
386 fgState.GameModeDepth,
387 ( exactMatch ? refresh : fgState.GameModeRefresh ) ) ) {
390 /* Update the chosen refresh rate, otherwise a
391 * glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE) would not
392 * return the right values
394 fgState.GameModeRefresh = refresh;
406 * Changes the current display mode to match user's settings
408 GLboolean fgPlatformChangeDisplayMode( GLboolean haveToTest )
410 GLboolean success = GL_FALSE;
411 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
413 if(xrandr_resize(fgState.GameModeSize.X, fgState.GameModeSize.Y,
414 fgState.GameModeRefresh, haveToTest) != -1) {
417 return GL_FALSE; /* don't fall back to XF86VidMode */
419 #endif /* HAVE_X11_EXTENSIONS_XRANDR_H */
423 * This highly depends on the XFree86 extensions,
424 * not approved as X Consortium standards
426 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
429 * This is also used by applications which check modes by calling
430 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
432 if( haveToTest || fgDisplay.pDisplay.DisplayModeValid )
434 XF86VidModeModeInfo** displayModes;
435 int i, displayModesCount;
437 /* If we don't have a valid modeline in the display structure, which
438 * can happen if this is called from glutGameModeGet instead of
439 * glutEnterGameMode, then we need to query the current mode, to make
440 * unspecified settings to default to their current values.
442 if(!fgDisplay.pDisplay.DisplayModeValid) {
443 if(!XF86VidModeGetModeLine(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.Screen,
444 &fgDisplay.pDisplay.DisplayModeClock, &fgDisplay.pDisplay.DisplayMode)) {
449 if (fgState.GameModeSize.X == -1)
451 fgState.GameModeSize.X = fgDisplay.pDisplay.DisplayMode.hdisplay;
453 if (fgState.GameModeSize.Y == -1)
455 fgState.GameModeSize.Y = fgDisplay.pDisplay.DisplayMode.vdisplay;
457 if (fgState.GameModeDepth == -1)
459 /* can't get color depth from this, nor can we change it, do nothing
460 * TODO: get with XGetVisualInfo()? but then how to set?
463 if (fgState.GameModeRefresh == -1)
465 /* Compute the displays refresh rate, dotclock comes in kHz. */
466 int refresh = ( fgDisplay.pDisplay.DisplayModeClock * 1000 ) /
467 ( fgDisplay.pDisplay.DisplayMode.htotal * fgDisplay.pDisplay.DisplayMode.vtotal );
469 fgState.GameModeRefresh = refresh;
472 /* query all possible display modes */
473 if( !XF86VidModeGetAllModeLines(
474 fgDisplay.pDisplay.Display,
475 fgDisplay.pDisplay.Screen,
479 fgWarning( "XF86VidModeGetAllModeLines failed" );
485 * Check every of the modes looking for one that matches our demands,
486 * ignoring the refresh rate if no exact match could be found.
488 i = fghCheckDisplayModes( GL_TRUE, displayModesCount, displayModes );
490 i = fghCheckDisplayModes( GL_FALSE, displayModesCount, displayModes );
492 success = ( i < 0 ) ? GL_FALSE : GL_TRUE;
494 if( !haveToTest && success ) {
495 if( !XF86VidModeSwitchToMode(
496 fgDisplay.pDisplay.Display,
497 fgDisplay.pDisplay.Screen,
498 displayModes[ i ] ) )
499 fgWarning( "XF86VidModeSwitchToMode failed" );
502 XFree( displayModes );
506 #endif /* HAVE_X11_EXTENSIONS_XF86VMODE_H */
512 void fgPlatformEnterGameMode( void )
516 * Sync needed to avoid a real race, the Xserver must have really created
517 * the window before we can grab the pointer into it:
519 XSync( fgDisplay.pDisplay.Display, False );
521 * Grab the pointer to confine it into the window after the calls to
522 * XWrapPointer() which ensure that the pointer really enters the window.
524 * We also need to wait here until XGrabPointer() returns GrabSuccess,
525 * otherwise the new window is not viewable yet and if the next function
526 * (XSetInputFocus) is called with a not yet viewable window, it will exit
527 * the application which we have to aviod, so wait until it's viewable:
529 while( GrabSuccess != XGrabPointer(
530 fgDisplay.pDisplay.Display, fgStructure.GameModeWindow->Window.Handle,
532 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
534 GrabModeAsync, GrabModeAsync,
535 fgStructure.GameModeWindow->Window.Handle, None, CurrentTime) )
538 * Change input focus to the new window. This will exit the application
539 * if the new window is not viewable yet, see the XGrabPointer loop above.
542 fgDisplay.pDisplay.Display,
543 fgStructure.GameModeWindow->Window.Handle,
548 /* Move the Pointer to the middle of the fullscreen window */
550 fgDisplay.pDisplay.Display,
552 fgDisplay.pDisplay.RootWindow,
554 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
557 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
560 if( fgDisplay.pDisplay.DisplayModeValid )
565 /* Change to viewport to the window topleft edge: */
566 if( !XF86VidModeSetViewPort( fgDisplay.pDisplay.Display, fgDisplay.pDisplay.Screen, 0, 0 ) )
567 fgWarning( "XF86VidModeSetViewPort failed" );
570 * Final window repositioning: It could be avoided using an undecorated
571 * window using override_redirect, but this * would possily require
572 * more changes and investigation.
575 /* Get the current postion of the drawable area on screen */
576 XTranslateCoordinates(
577 fgDisplay.pDisplay.Display,
578 fgStructure.CurrentWindow->Window.Handle,
579 fgDisplay.pDisplay.RootWindow,
584 /* Move the decorataions out of the topleft corner of the display */
585 XMoveWindow( fgDisplay.pDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
590 #endif /* HAVE_X11_EXTENSIONS_XF86VMODE_H */
592 /* Grab the keyboard, too */
594 fgDisplay.pDisplay.Display,
595 fgStructure.GameModeWindow->Window.Handle,
597 GrabModeAsync, GrabModeAsync,
603 void fgPlatformLeaveGameMode( void )
605 XUngrabPointer( fgDisplay.pDisplay.Display, CurrentTime );
606 XUngrabKeyboard( fgDisplay.pDisplay.Display, CurrentTime );