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 static int xrandr_resize(int xsz, int ysz, int rate, int just_checking)
34 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
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);
140 #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 int event_base, error_base;
153 * Remember the current pointer location before going fullscreen
154 * for restoring it later:
157 unsigned int junk_mask;
159 XQueryPointer(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow,
160 &junk_window, &junk_window,
161 &fgDisplay.pDisplay.DisplayPointerX, &fgDisplay.pDisplay.DisplayPointerY,
162 &fgDisplay.pDisplay.DisplayPointerX, &fgDisplay.pDisplay.DisplayPointerY, &junk_mask);
164 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
165 if(XRRQueryExtension(fgDisplay.pDisplay.Display, &event_base, &error_base)) {
166 XRRScreenConfiguration *xrr_config;
167 XRRScreenSize *ssizes;
169 int ssize_count, curr;
171 if((xrr_config = XRRGetScreenInfo(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.RootWindow))) {
172 ssizes = XRRConfigSizes(xrr_config, &ssize_count);
173 curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
175 fgDisplay.pDisplay.prev_xsz = ssizes[curr].width;
176 fgDisplay.pDisplay.prev_ysz = ssizes[curr].height;
177 fgDisplay.pDisplay.prev_refresh = -1;
179 # if ( RANDR_MAJOR >= 1 ) || ( ( RANDR_MAJOR == 1 ) && ( RANDR_MINOR >= 1 ) )
180 if(fgState.GameModeRefresh != -1) {
181 fgDisplay.pDisplay.prev_refresh = XRRConfigCurrentRate(xrr_config);
185 fgDisplay.pDisplay.prev_size_valid = 1;
187 XRRFreeScreenConfigInfo(xrr_config);
193 * This highly depends on the XFree86 extensions,
194 * not approved as X Consortium standards
196 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
197 if(!XF86VidModeQueryExtension(fgDisplay.pDisplay.Display, &event_base, &error_base)) {
202 * Remember the current ViewPort location of the screen to be able to
203 * restore the ViewPort on LeaveGameMode():
205 if( !XF86VidModeGetViewPort(
206 fgDisplay.pDisplay.Display,
207 fgDisplay.pDisplay.Screen,
208 &fgDisplay.pDisplay.DisplayViewPortX,
209 &fgDisplay.pDisplay.DisplayViewPortY ) )
210 fgWarning( "XF86VidModeGetViewPort failed" );
213 /* Query the current display settings: */
214 fgDisplay.pDisplay.DisplayModeValid =
215 XF86VidModeGetModeLine(
216 fgDisplay.pDisplay.Display,
217 fgDisplay.pDisplay.Screen,
218 &fgDisplay.pDisplay.DisplayModeClock,
219 &fgDisplay.pDisplay.DisplayMode
222 if( !fgDisplay.pDisplay.DisplayModeValid )
223 fgWarning( "XF86VidModeGetModeLine failed" );
229 * Restores the previously remembered visual settings
231 void fgPlatformRestoreState( void )
233 /* Restore the remembered pointer position: */
235 fgDisplay.pDisplay.Display, None, fgDisplay.pDisplay.RootWindow, 0, 0, 0, 0,
236 fgDisplay.pDisplay.DisplayPointerX, fgDisplay.pDisplay.DisplayPointerY
240 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
241 if(fgDisplay.pDisplay.prev_size_valid) {
242 if(xrandr_resize(fgDisplay.pDisplay.prev_xsz, fgDisplay.pDisplay.prev_ysz, fgDisplay.pDisplay.prev_refresh, 0) != -1) {
243 fgDisplay.pDisplay.prev_size_valid = 0;
244 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
245 fgDisplay.pDisplay.DisplayModeValid = 0;
254 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
256 * This highly depends on the XFree86 extensions,
257 * not approved as X Consortium standards
260 if( fgDisplay.pDisplay.DisplayModeValid )
262 XF86VidModeModeInfo** displayModes;
263 int i, displayModesCount;
265 if( !XF86VidModeGetAllModeLines(
266 fgDisplay.pDisplay.Display,
267 fgDisplay.pDisplay.Screen,
271 fgWarning( "XF86VidModeGetAllModeLines failed" );
277 * Check every of the modes looking for one that matches our demands.
278 * If we find one, switch to it and restore the remembered viewport.
280 for( i = 0; i < displayModesCount; i++ )
282 if(displayModes[ i ]->hdisplay == fgDisplay.pDisplay.DisplayMode.hdisplay &&
283 displayModes[ i ]->vdisplay == fgDisplay.pDisplay.DisplayMode.vdisplay &&
284 displayModes[ i ]->dotclock == fgDisplay.pDisplay.DisplayModeClock )
286 if( !XF86VidModeSwitchToMode(
287 fgDisplay.pDisplay.Display,
288 fgDisplay.pDisplay.Screen,
289 displayModes[ i ] ) )
291 fgWarning( "XF86VidModeSwitchToMode failed" );
295 if( !XF86VidModeSetViewPort(
296 fgDisplay.pDisplay.Display,
297 fgDisplay.pDisplay.Screen,
298 fgDisplay.pDisplay.DisplayViewPortX,
299 fgDisplay.pDisplay.DisplayViewPortY ) )
300 fgWarning( "XF86VidModeSetViewPort failed" );
304 * For the case this would be the last X11 call the application
305 * calls exit() we've to flush the X11 output queue to have the
306 * commands sent to the X server before the application exits.
308 XFlush( fgDisplay.pDisplay.Display );
310 fgDisplay.pDisplay.DisplayModeValid = 0;
311 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
312 fgDisplay.pDisplay.prev_size_valid = 0;
318 XFree( displayModes );
325 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
328 * Checks a single display mode settings against user's preferences.
330 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
332 /* The desired values should be stored in fgState structure... */
333 return ( width == fgState.GameModeSize.X ) &&
334 ( height == fgState.GameModeSize.Y ) &&
335 ( depth == fgState.GameModeDepth ) &&
336 ( refresh == fgState.GameModeRefresh );
340 * Checks all display modes settings against user's preferences.
341 * Returns the mode number found or -1 if none could be found.
343 static int fghCheckDisplayModes( GLboolean exactMatch, int displayModesCount, XF86VidModeModeInfo** displayModes )
346 for( i = 0; i < displayModesCount; i++ )
348 /* Compute the displays refresh rate, dotclock comes in kHz. */
349 int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
350 ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
352 if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
353 displayModes[ i ]->vdisplay,
354 fgState.GameModeDepth,
355 ( exactMatch ? refresh : fgState.GameModeRefresh ) ) ) {
358 /* Update the chosen refresh rate, otherwise a
359 * glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE) would not
360 * return the right values
362 fgState.GameModeRefresh = refresh;
374 * Changes the current display mode to match user's settings
376 GLboolean fgPlatformChangeDisplayMode( GLboolean haveToTest )
378 GLboolean success = GL_FALSE;
379 /* first try to use XRandR, then fallback to XF86VidMode */
380 # ifdef HAVE_X11_EXTENSIONS_XRANDR_H
381 if(xrandr_resize(fgState.GameModeSize.X, fgState.GameModeSize.Y,
382 fgState.GameModeRefresh, haveToTest) != -1) {
389 * This highly depends on the XFree86 extensions,
390 * not approved as X Consortium standards
392 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
395 * This is also used by applications which check modes by calling
396 * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
398 if( haveToTest || fgDisplay.pDisplay.DisplayModeValid )
400 XF86VidModeModeInfo** displayModes;
401 int i, displayModesCount;
403 /* If we don't have a valid modeline in the display structure, which
404 * can happen if this is called from glutGameModeGet instead of
405 * glutEnterGameMode, then we need to query the current mode, to make
406 * unspecified settings to default to their current values.
408 if(!fgDisplay.pDisplay.DisplayModeValid) {
409 if(!XF86VidModeGetModeLine(fgDisplay.pDisplay.Display, fgDisplay.pDisplay.Screen,
410 &fgDisplay.pDisplay.DisplayModeClock, &fgDisplay.pDisplay.DisplayMode)) {
415 if (fgState.GameModeSize.X == -1)
417 fgState.GameModeSize.X = fgDisplay.pDisplay.DisplayMode.hdisplay;
419 if (fgState.GameModeSize.Y == -1)
421 fgState.GameModeSize.Y = fgDisplay.pDisplay.DisplayMode.vdisplay;
423 if (fgState.GameModeDepth == -1)
425 /* can't get color depth from this, nor can we change it, do nothing
426 * TODO: get with XGetVisualInfo()? but then how to set?
429 if (fgState.GameModeRefresh == -1)
431 /* Compute the displays refresh rate, dotclock comes in kHz. */
432 int refresh = ( fgDisplay.pDisplay.DisplayModeClock * 1000 ) /
433 ( fgDisplay.pDisplay.DisplayMode.htotal * fgDisplay.pDisplay.DisplayMode.vtotal );
435 fgState.GameModeRefresh = refresh;
438 /* query all possible display modes */
439 if( !XF86VidModeGetAllModeLines(
440 fgDisplay.pDisplay.Display,
441 fgDisplay.pDisplay.Screen,
445 fgWarning( "XF86VidModeGetAllModeLines failed" );
451 * Check every of the modes looking for one that matches our demands,
452 * ignoring the refresh rate if no exact match could be found.
454 i = fghCheckDisplayModes( GL_TRUE, displayModesCount, displayModes );
456 i = fghCheckDisplayModes( GL_FALSE, displayModesCount, displayModes );
458 success = ( i < 0 ) ? GL_FALSE : GL_TRUE;
460 if( !haveToTest && success ) {
461 if( !XF86VidModeSwitchToMode(
462 fgDisplay.pDisplay.Display,
463 fgDisplay.pDisplay.Screen,
464 displayModes[ i ] ) )
465 fgWarning( "XF86VidModeSwitchToMode failed" );
468 XFree( displayModes );
477 void fgPlatformEnterGameMode( void )
481 * Sync needed to avoid a real race, the Xserver must have really created
482 * the window before we can grab the pointer into it:
484 XSync( fgDisplay.pDisplay.Display, False );
486 * Grab the pointer to confine it into the window after the calls to
487 * XWrapPointer() which ensure that the pointer really enters the window.
489 * We also need to wait here until XGrabPointer() returns GrabSuccess,
490 * otherwise the new window is not viewable yet and if the next function
491 * (XSetInputFocus) is called with a not yet viewable window, it will exit
492 * the application which we have to aviod, so wait until it's viewable:
494 while( GrabSuccess != XGrabPointer(
495 fgDisplay.pDisplay.Display, fgStructure.GameModeWindow->Window.Handle,
497 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
499 GrabModeAsync, GrabModeAsync,
500 fgStructure.GameModeWindow->Window.Handle, None, CurrentTime) )
503 * Change input focus to the new window. This will exit the application
504 * if the new window is not viewable yet, see the XGrabPointer loop above.
507 fgDisplay.pDisplay.Display,
508 fgStructure.GameModeWindow->Window.Handle,
513 /* Move the Pointer to the middle of the fullscreen window */
515 fgDisplay.pDisplay.Display,
517 fgDisplay.pDisplay.RootWindow,
519 fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
522 # ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
524 if( fgDisplay.pDisplay.DisplayModeValid )
529 /* Change to viewport to the window topleft edge: */
530 if( !XF86VidModeSetViewPort( fgDisplay.pDisplay.Display, fgDisplay.pDisplay.Screen, 0, 0 ) )
531 fgWarning( "XF86VidModeSetViewPort failed" );
534 * Final window repositioning: It could be avoided using an undecorated
535 * window using override_redirect, but this * would possily require
536 * more changes and investigation.
539 /* Get the current postion of the drawable area on screen */
540 XTranslateCoordinates(
541 fgDisplay.pDisplay.Display,
542 fgStructure.CurrentWindow->Window.Handle,
543 fgDisplay.pDisplay.RootWindow,
548 /* Move the decorataions out of the topleft corner of the display */
549 XMoveWindow( fgDisplay.pDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
555 /* Grab the keyboard, too */
557 fgDisplay.pDisplay.Display,
558 fgStructure.GameModeWindow->Window.Handle,
560 GrabModeAsync, GrabModeAsync,
566 void fgPlatformLeaveGameMode( void )
568 XUngrabPointer( fgDisplay.pDisplay.Display, CurrentTime );
569 XUngrabKeyboard( fgDisplay.pDisplay.Display, CurrentTime );