2 * freeglut_gamemode_x11.c
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 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
33 static int xrandr_resize(int xsz, int ysz, int rate, int just_checking)
35 int event_base, error_base, ver_major, ver_minor, use_rate;
36 XRRScreenConfiguration *xrr_config = 0;
39 /* must check at runtime for the availability of the extension */
40 if(!XRRQueryExtension(fgDisplay.pDisplay.Display, &event_base, &error_base)) {
44 XRRQueryVersion(fgDisplay.pDisplay.Display, &ver_major, &ver_minor);
46 /* we only heed the rate if we CAN actually use it (Xrandr >= 1.1) and
47 * the user actually cares about it (rate > 0)
49 use_rate = ( rate > 0 ) && ( ( ver_major > 1 ) ||
50 ( ( ver_major == 1 ) && ( ver_minor >= 1 ) ) );
52 /* this loop is only so that the whole thing will be repeated if someone
53 * else changes video mode between our query of the current information and
54 * the attempt to change it.
57 XRRScreenSize *ssizes;
60 int i, ssizes_count, rates_count, curr, res_idx = -1;
61 Time timestamp, cfg_timestamp;
64 XRRFreeScreenConfigInfo(xrr_config);
67 if(!(xrr_config = XRRGetScreenInfo(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow))) {
68 fgWarning("XRRGetScreenInfo failed");
71 ssizes = XRRConfigSizes(xrr_config, &ssizes_count);
72 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
73 timestamp = XRRConfigTimes(xrr_config, &cfg_timestamp);
75 /* if either of xsz or ysz are unspecified, use the current values */
77 xsz = fgState.GameModeSize.X = ssizes[curr].width;
79 ysz = fgState.GameModeSize.Y = ssizes[curr].height;
82 if(xsz == ssizes[curr].width && ysz == ssizes[curr].height) {
83 /* no need to switch, we're already in the requested resolution */
86 for(i=0; i<ssizes_count; i++) {
87 if(ssizes[i].width == xsz && ssizes[i].height == ysz) {
94 break; /* no matching resolution */
96 #if ( RANDR_MAJOR > 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
98 rate = fgState.GameModeRefresh;
100 /* for the selected resolution, let's find out if there is
101 * a matching refresh rate available.
103 rates = XRRConfigRates(xrr_config, res_idx, &rates_count);
105 for(i=0; i<rates_count; i++) {
106 if(rates[i] == rate) {
110 if(i == rates_count) {
111 break; /* no matching rate */
121 #if ( RANDR_MAJOR > 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
123 result = XRRSetScreenConfigAndRate(fgDisplay.pDisplay.Display, xrr_config,
124 fgDisplay.pDisplay.RootWindow, res_idx, rot, rate, timestamp);
127 result = XRRSetScreenConfig(fgDisplay.pDisplay.Display, xrr_config,
128 fgDisplay.pDisplay.RootWindow, res_idx, rot, timestamp);
130 } while(result == RRSetConfigInvalidTime);
133 XRRFreeScreenConfigInfo(xrr_config);
142 #endif /* HAVE_X11_EXTENSIONS_XRANDR_H */
145 * Remembers the current visual settings, so that
146 * we can change them and restore later...
148 void fgPlatformRememberState( void )
150 # if defined(HAVE_X11_EXTENSIONS_XRANDR_H) | defined(HAVE_X11_EXTENSIONS_XF86VMODE_H)
151 int event_base, error_base;
155 * Remember the current pointer location before going fullscreen
156 * for restoring it later:
159 unsigned int junk_mask;
161 XQueryPointer(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow,
162 &junk_window, &junk_window,
163 &fgDisplay.pDisplay.DisplayPointerX, &fgDisplay.pDisplay.DisplayPointerY,
164 &fgDisplay.pDisplay.DisplayPointerX, &fgDisplay.pDisplay.DisplayPointerY, &junk_mask);
166 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
167 if(XRRQueryExtension(fgDisplay.pDisplay.Display, &event_base, &error_base)) {
168 XRRScreenConfiguration *xrr_config;
169 XRRScreenSize *ssizes;
171 int ssize_count, curr;
173 if((xrr_config = XRRGetScreenInfo(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow))) {
174 ssizes = XRRConfigSizes(xrr_config, &ssize_count);
175 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
177 fgDisplay.pDisplay.prev_xsz = ssizes[curr].width;
178 fgDisplay.pDisplay.prev_ysz = ssizes[curr].height;
179 fgDisplay.pDisplay.prev_refresh = -1;
181 # if ( RANDR_MAJOR > 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
182 if(fgState.GameModeRefresh != -1) {
183 fgDisplay.pDisplay.prev_refresh = XRRConfigCurrentRate(xrr_config);
187 fgDisplay.pDisplay.prev_size_valid = 1;
189 XRRFreeScreenConfigInfo(xrr_config);
195 * This highly depends on the XFree86 extensions,
196 * not approved as X Consortium standards
198 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
199 if(!XF86VidModeQueryExtension(fgDisplay.pDisplay.Display, &event_base, &error_base)) {
204 * Remember the current ViewPort location of the screen to be able to
205 * restore the ViewPort on LeaveGameMode():
207 if( !XF86VidModeGetViewPort(
208 fgDisplay.pDisplay.Display,
209 fgDisplay.pDisplay.Screen,
210 &fgDisplay.pDisplay.DisplayViewPortX,
211 &fgDisplay.pDisplay.DisplayViewPortY ) )
212 fgWarning( "XF86VidModeGetViewPort failed" );
215 /* Query the current display settings: */
216 fgDisplay.pDisplay.DisplayModeValid =
217 XF86VidModeGetModeLine(
218 fgDisplay.pDisplay.Display,
219 fgDisplay.pDisplay.Screen,
220 &fgDisplay.pDisplay.DisplayModeClock,
221 &fgDisplay.pDisplay.DisplayMode
224 if( !fgDisplay.pDisplay.DisplayModeValid )
225 fgWarning( "XF86VidModeGetModeLine failed" );
231 * Restores the previously remembered visual settings
233 void fgPlatformRestoreState( void )
235 /* Restore the remembered pointer position: */
237 fgDisplay.pDisplay.Display, None, fgDisplay.pDisplay.RootWindow, 0, 0, 0, 0,
238 fgDisplay.pDisplay.DisplayPointerX, fgDisplay.pDisplay.DisplayPointerY
242 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
243 if(fgDisplay.pDisplay.prev_size_valid) {
244 if(xrandr_resize(fgDisplay.pDisplay.prev_xsz, fgDisplay.pDisplay.prev_ysz, fgDisplay.pDisplay.prev_refresh, 0) != -1) {
245 fgDisplay.pDisplay.prev_size_valid = 0;
246 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
247 fgDisplay.pDisplay.DisplayModeValid = 0;
256 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
258 * This highly depends on the XFree86 extensions,
259 * not approved as X Consortium standards
262 if( fgDisplay.pDisplay.DisplayModeValid )
264 XF86VidModeModeInfo** displayModes;
265 int i, displayModesCount;
267 if( !XF86VidModeGetAllModeLines(
268 fgDisplay.pDisplay.Display,
269 fgDisplay.pDisplay.Screen,
273 fgWarning( "XF86VidModeGetAllModeLines failed" );
279 * Check every of the modes looking for one that matches our demands.
280 * If we find one, switch to it and restore the remembered viewport.
282 for( i = 0; i < displayModesCount; i++ )
284 if(displayModes[ i ]->hdisplay == fgDisplay.pDisplay.DisplayMode.hdisplay &&
285 displayModes[ i ]->vdisplay == fgDisplay.pDisplay.DisplayMode.vdisplay &&
286 displayModes[ i ]->dotclock == fgDisplay.pDisplay.DisplayModeClock )
288 if( !XF86VidModeSwitchToMode(
289 fgDisplay.pDisplay.Display,
290 fgDisplay.pDisplay.Screen,
291 displayModes[ i ] ) )
293 fgWarning( "XF86VidModeSwitchToMode failed" );
297 if( !XF86VidModeSetViewPort(
298 fgDisplay.pDisplay.Display,
299 fgDisplay.pDisplay.Screen,
300 fgDisplay.pDisplay.DisplayViewPortX,
301 fgDisplay.pDisplay.DisplayViewPortY ) )
302 fgWarning( "XF86VidModeSetViewPort failed" );
306 * For the case this would be the last X11 call the application
307 * calls exit() we've to flush the X11 output queue to have the
308 * commands sent to the X server before the application exits.
310 XFlush( fgDisplay.pDisplay.Display );
312 fgDisplay.pDisplay.DisplayModeValid = 0;
313 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
314 fgDisplay.pDisplay.prev_size_valid = 0;
320 XFree( displayModes );
327 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
330 * Checks a single display mode settings against user's preferences.
332 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
334 /* The desired values should be stored in fgState structure... */
335 return ( width == fgState.GameModeSize.X ) &&
336 ( height == fgState.GameModeSize.Y ) &&
337 ( depth == fgState.GameModeDepth ) &&
338 ( refresh == fgState.GameModeRefresh );
342 * Checks all display modes settings against user's preferences.
343 * Returns the mode number found or -1 if none could be found.
345 static int fghCheckDisplayModes( GLboolean exactMatch, int displayModesCount, XF86VidModeModeInfo** displayModes )
348 for( i = 0; i < displayModesCount; i++ )
350 /* Compute the displays refresh rate, dotclock comes in kHz. */
351 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
352 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
354 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
355 displayModes[ i ]->vdisplay,
356 fgState.GameModeDepth,
357 ( exactMatch ? refresh : fgState.GameModeRefresh ) ) ) {
360 /* Update the chosen refresh rate, otherwise a
361 * glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE) would not
362 * return the right values
364 fgState.GameModeRefresh = refresh;
376 * Changes the current display mode to match user's settings
378 GLboolean fgPlatformChangeDisplayMode( GLboolean haveToTest )
380 GLboolean success = GL_FALSE;
381 /* first try to use XRandR, then fallback to XF86VidMode */
382 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
383 if(xrandr_resize(fgState.GameModeSize.X, fgState.GameModeSize.Y,
384 fgState.GameModeRefresh, haveToTest) != -1) {
391 * This highly depends on the XFree86 extensions,
392 * not approved as X Consortium standards
394 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
397 * This is also used by applications which check modes by calling
398 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
400 if( haveToTest || fgDisplay.pDisplay.DisplayModeValid )
402 XF86VidModeModeInfo** displayModes;
403 int i, displayModesCount;
405 /* If we don't have a valid modeline in the display structure, which
406 * can happen if this is called from glutGameModeGet instead of
407 * glutEnterGameMode, then we need to query the current mode, to make
408 * unspecified settings to default to their current values.
410 if(!fgDisplay.pDisplay.DisplayModeValid) {
411 if(!XF86VidModeGetModeLine(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.Screen,
412 &fgDisplay.pDisplay.DisplayModeClock, &fgDisplay.pDisplay.DisplayMode)) {
417 if (fgState.GameModeSize.X == -1)
419 fgState.GameModeSize.X = fgDisplay.pDisplay.DisplayMode.hdisplay;
421 if (fgState.GameModeSize.Y == -1)
423 fgState.GameModeSize.Y = fgDisplay.pDisplay.DisplayMode.vdisplay;
425 if (fgState.GameModeDepth == -1)
427 /* can't get color depth from this, nor can we change it, do nothing
428 * TODO: get with XGetVisualInfo()? but then how to set?
431 if (fgState.GameModeRefresh == -1)
433 /* Compute the displays refresh rate, dotclock comes in kHz. */
434 int refresh = ( fgDisplay.pDisplay.DisplayModeClock * 1000 ) /
435 ( fgDisplay.pDisplay.DisplayMode.htotal * fgDisplay.pDisplay.DisplayMode.vtotal );
437 fgState.GameModeRefresh = refresh;
440 /* query all possible display modes */
441 if( !XF86VidModeGetAllModeLines(
442 fgDisplay.pDisplay.Display,
443 fgDisplay.pDisplay.Screen,
447 fgWarning( "XF86VidModeGetAllModeLines failed" );
453 * Check every of the modes looking for one that matches our demands,
454 * ignoring the refresh rate if no exact match could be found.
456 i = fghCheckDisplayModes( GL_TRUE, displayModesCount, displayModes );
458 i = fghCheckDisplayModes( GL_FALSE, displayModesCount, displayModes );
460 success = ( i < 0 ) ? GL_FALSE : GL_TRUE;
462 if( !haveToTest && success ) {
463 if( !XF86VidModeSwitchToMode(
464 fgDisplay.pDisplay.Display,
465 fgDisplay.pDisplay.Screen,
466 displayModes[ i ] ) )
467 fgWarning( "XF86VidModeSwitchToMode failed" );
470 XFree( displayModes );
479 void fgPlatformEnterGameMode( void )
483 * Sync needed to avoid a real race, the Xserver must have really created
484 * the window before we can grab the pointer into it:
486 XSync( fgDisplay.pDisplay.Display, False );
488 * Grab the pointer to confine it into the window after the calls to
489 * XWrapPointer() which ensure that the pointer really enters the window.
491 * We also need to wait here until XGrabPointer() returns GrabSuccess,
492 * otherwise the new window is not viewable yet and if the next function
493 * (XSetInputFocus) is called with a not yet viewable window, it will exit
494 * the application which we have to aviod, so wait until it's viewable:
496 while( GrabSuccess != XGrabPointer(
497 fgDisplay.pDisplay.Display, fgStructure.GameModeWindow->Window.Handle,
499 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
501 GrabModeAsync, GrabModeAsync,
502 fgStructure.GameModeWindow->Window.Handle, None, CurrentTime) )
505 * Change input focus to the new window. This will exit the application
506 * if the new window is not viewable yet, see the XGrabPointer loop above.
509 fgDisplay.pDisplay.Display,
510 fgStructure.GameModeWindow->Window.Handle,
515 /* Move the Pointer to the middle of the fullscreen window */
517 fgDisplay.pDisplay.Display,
519 fgDisplay.pDisplay.RootWindow,
521 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
524 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
526 if( fgDisplay.pDisplay.DisplayModeValid )
531 /* Change to viewport to the window topleft edge: */
532 if( !XF86VidModeSetViewPort( fgDisplay.pDisplay.Display, fgDisplay.pDisplay.Screen, 0, 0 ) )
533 fgWarning( "XF86VidModeSetViewPort failed" );
536 * Final window repositioning: It could be avoided using an undecorated
537 * window using override_redirect, but this * would possily require
538 * more changes and investigation.
541 /* Get the current postion of the drawable area on screen */
542 XTranslateCoordinates(
543 fgDisplay.pDisplay.Display,
544 fgStructure.CurrentWindow->Window.Handle,
545 fgDisplay.pDisplay.RootWindow,
550 /* Move the decorataions out of the topleft corner of the display */
551 XMoveWindow( fgDisplay.pDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
557 /* Grab the keyboard, too */
559 fgDisplay.pDisplay.Display,
560 fgStructure.GameModeWindow->Window.Handle,
562 GrabModeAsync, GrabModeAsync,
568 void fgPlatformLeaveGameMode( void )
570 XUngrabPointer( fgDisplay.pDisplay.Display, CurrentTime );
571 XUngrabKeyboard( fgDisplay.pDisplay.Display, CurrentTime );