Adding a warning to an unsuccessful game mode entry per e-mail from Diedrick Niehorst...
[freeglut] / src / freeglut_gamemode.c
1 /*
2  * freeglut_gamemode.c
3  *
4  * The game mode handling code.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 /*
32  * TODO BEFORE THE STABLE RELEASE:
33  *
34  *  glutGameModeString()    -- missing
35  *  glutEnterGameMode()     -- X11 version
36  *  glutLeaveGameMode()     -- is that correct?
37  *  glutGameModeGet()       -- is that correct?
38  */
39
40
41 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
42
43 static int xrandr_resize(int xsz, int ysz, int just_checking)
44 {
45     int res = -1;
46
47 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
48     int event_base, error_base;
49     Status st;
50     XRRScreenConfiguration *xrr_config;
51     XRRScreenSize *ssizes;
52     Rotation rot;
53     int i, ssizes_count, curr;
54     Time timestamp, cfg_timestamp;
55
56     /* must check at runtime for the availability of the extension */
57     if(!XRRQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
58         return -1;
59     }
60
61     if(!(xrr_config = XRRGetScreenInfo(fgDisplay.Display, fgDisplay.RootWindow))) {
62         fgWarning("XRRGetScreenInfo failed");
63         return -1;
64     }
65     ssizes = XRRConfigSizes(xrr_config, &ssizes_count);
66     curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
67     timestamp = XRRConfigTimes(xrr_config, &cfg_timestamp);
68
69     if(xsz == ssizes[curr].width && ysz == ssizes[curr].height) {
70         /* no need to switch, we're already in the requested mode */
71         res = 0;
72         goto done;
73     }
74
75     for(i=0; i<ssizes_count; i++) {
76         if(ssizes[i].width == xsz && ssizes[i].height == ysz) {
77             break;  /* found it */
78         }
79     }
80     if(i == ssizes_count)
81         goto done;
82
83     if(just_checking) {
84         res = 0;
85         goto done;
86     }
87
88     if((st = XRRSetScreenConfig(fgDisplay.Display, xrr_config, fgDisplay.RootWindow,
89                     i, rot, timestamp)) != 0) {
90         fgWarning("XRRSetScreenConfig failed");
91         goto done;
92     }
93     res = 0;
94
95 done:
96     XRRFreeScreenConfigInfo(xrr_config);
97 #endif
98     return res;
99 }
100
101
102 /*
103  * Remembers the current visual settings, so that
104  * we can change them and restore later...
105  */
106 static void fghRememberState( void )
107 {
108 #if TARGET_HOST_POSIX_X11
109     int event_base, error_base;
110
111 #   ifdef HAVE_X11_EXTENSIONS_XRANDR_H
112     if(XRRQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
113         XRRScreenConfiguration *xrr_config;
114         XRRScreenSize *ssizes;
115         Rotation rot;
116         int ssize_count, curr;
117
118         if((xrr_config = XRRGetScreenInfo(fgDisplay.Display, fgDisplay.RootWindow))) {
119             ssizes = XRRConfigSizes(xrr_config, &ssize_count);
120             curr = XRRConfigCurrentConfiguration(xrr_config, &rot);
121
122             fgDisplay.prev_xsz = ssizes[curr].width;
123             fgDisplay.prev_ysz = ssizes[curr].height;
124             fgDisplay.prev_size_valid = 1;
125             XRRFreeScreenConfigInfo(xrr_config);
126             return;
127         }
128     }
129 #   endif
130
131     /*
132      * This highly depends on the XFree86 extensions,
133      * not approved as X Consortium standards
134      */
135 #   ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
136     if(!XF86VidModeQueryExtension(fgDisplay.Display, &event_base, &error_base)) {
137         return;
138     }
139
140     /*
141      * Remember the current ViewPort location of the screen to be able to
142      * restore the ViewPort on LeaveGameMode():
143      */
144     if( !XF86VidModeGetViewPort(
145              fgDisplay.Display,
146              fgDisplay.Screen,
147              &fgDisplay.DisplayViewPortX,
148              &fgDisplay.DisplayViewPortY ) )
149         fgWarning( "XF86VidModeGetViewPort failed" );
150
151     /*
152      * Remember the current pointer location before going fullscreen
153      * for restoring it later:
154      */
155     {
156         Window junk_window;
157         unsigned int mask;
158
159         XQueryPointer(
160             fgDisplay.Display, fgDisplay.RootWindow,
161             &junk_window, &junk_window,
162             &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY,
163             &fgDisplay.DisplayPointerX, &fgDisplay.DisplayPointerY, &mask
164         );
165     }
166
167     /* Query the current display settings: */
168     fgDisplay.DisplayModeValid =
169       XF86VidModeGetModeLine(
170         fgDisplay.Display,
171         fgDisplay.Screen,
172         &fgDisplay.DisplayModeClock,
173         &fgDisplay.DisplayMode
174     );
175
176     if( !fgDisplay.DisplayModeValid )
177             fgWarning( "XF86VidModeGetModeLine failed" );
178 #   endif
179
180 #elif TARGET_HOST_MS_WINDOWS
181
182 /*    DEVMODE devMode; */
183
184     /* Grab the current desktop settings... */
185
186 /* hack to get around my stupid cross-gcc headers */
187 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
188
189     EnumDisplaySettings( fgDisplay.DisplayName, FREEGLUT_ENUM_CURRENT_SETTINGS,
190                          &fgDisplay.DisplayMode );
191
192     /* Make sure we will be restoring all settings needed */
193     fgDisplay.DisplayMode.dmFields |=
194         DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
195
196 #endif
197 }
198
199 /*
200  * Restores the previously remembered visual settings
201  */
202 static void fghRestoreState( void )
203 {
204 #if TARGET_HOST_POSIX_X11
205
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;
210             return;
211         }
212     }
213 #   endif
214
215
216 #   ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
217     /* Restore the remembered pointer position: */
218     XWarpPointer(
219         fgDisplay.Display, None, fgDisplay.RootWindow, 0, 0, 0, 0,
220         fgDisplay.DisplayPointerX, fgDisplay.DisplayPointerY
221     );
222
223     /*
224      * This highly depends on the XFree86 extensions,
225      * not approved as X Consortium standards
226      */
227
228     if( fgDisplay.DisplayModeValid )
229     {
230         XF86VidModeModeInfo** displayModes;
231         int i, displayModesCount;
232
233         if( !XF86VidModeGetAllModeLines(
234                  fgDisplay.Display,
235                  fgDisplay.Screen,
236                  &displayModesCount,
237                  &displayModes ) )
238         {
239             fgWarning( "XF86VidModeGetAllModeLines failed" );
240             return;
241         }
242
243
244         /*
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.
247          */
248         for( i = 0; i < displayModesCount; i++ )
249         {
250             if(displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
251                displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
252                displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
253             {
254                 if( !XF86VidModeSwitchToMode(
255                          fgDisplay.Display,
256                          fgDisplay.Screen,
257                          displayModes[ i ] ) )
258                 {
259                     fgWarning( "XF86VidModeSwitchToMode failed" );
260                     break;
261                 }
262
263                 if( !XF86VidModeSetViewPort(
264                          fgDisplay.Display,
265                          fgDisplay.Screen,
266                          fgDisplay.DisplayViewPortX,
267                          fgDisplay.DisplayViewPortY ) )
268                     fgWarning( "HAVE_X11_EXTENSIONS_XF86VMODE_H failed" );
269
270
271                 /*
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.
275                  */
276                 XFlush( fgDisplay.Display );
277
278                 break;
279             }
280         }
281         XFree( displayModes );
282     }
283
284 #   endif
285
286 #elif TARGET_HOST_MS_WINDOWS
287
288     /* Restore the previously remembered desktop display settings */
289     ChangeDisplaySettingsEx( fgDisplay.DisplayName,&fgDisplay.DisplayMode, 0,0,0 );
290
291 #endif
292 }
293
294 #if TARGET_HOST_POSIX_X11
295 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
296
297 /*
298  * Checks a single display mode settings against user's preferences.
299  */
300 static GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
301 {
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 );
307 }
308
309 /*
310  * Checks all display modes settings against user's preferences.
311  * Returns the mode number found or -1 if none could be found.
312  */
313 static int fghCheckDisplayModes( GLboolean exactMatch, int displayModesCount, XF86VidModeModeInfo** displayModes )
314 {
315     int i;
316     for( i = 0; i < displayModesCount; i++ )
317     {
318         /* Compute the displays refresh rate, dotclock comes in kHz. */
319         int refresh = ( displayModes[ i ]->dotclock * 1000 ) /
320                       ( displayModes[ i ]->htotal * displayModes[ i ]->vtotal );
321
322         if( fghCheckDisplayMode( displayModes[ i ]->hdisplay,
323                                  displayModes[ i ]->vdisplay,
324                                  fgState.GameModeDepth,
325                                  ( exactMatch ? refresh : fgState.GameModeRefresh ) ) ) {
326             if (!exactMatch)
327             {
328                 /* Update the chosen refresh rate, otherwise a
329                  * glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE) would not
330                  * return the right values
331                  */
332                 fgState.GameModeRefresh = refresh;
333             }
334
335             return i;
336         }
337     }
338     return -1;
339 }
340
341 #endif
342 #endif
343
344 /*
345  * Changes the current display mode to match user's settings
346  */
347 static GLboolean fghChangeDisplayMode( GLboolean haveToTest )
348 {
349     GLboolean success = GL_FALSE;
350 #if TARGET_HOST_POSIX_X11
351
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) {
355         return GL_TRUE;
356     }
357 #   endif
358
359
360     /*
361      * This highly depends on the XFree86 extensions,
362      * not approved as X Consortium standards
363      */
364 #   ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
365
366     /*
367      * This is also used by applications which check modes by calling
368      * glutGameModeGet(GLUT_GAME_MODE_POSSIBLE), so allow the check:
369      */
370     if( haveToTest || fgDisplay.DisplayModeValid )
371     {
372         XF86VidModeModeInfo** displayModes;
373         int i, displayModesCount;
374
375         /* current display mode was queried in fghRememberState
376          * set defaulted values to the current display mode's
377          */
378         if (fgState.GameModeSize.X == -1)
379         {
380             fgState.GameModeSize.X = fgDisplay.DisplayMode.hdisplay;
381         }
382         if (fgState.GameModeSize.Y == -1)
383         {
384             fgState.GameModeSize.Y = fgDisplay.DisplayMode.vdisplay;
385         }
386         if (fgState.GameModeDepth == -1)
387         {
388             /* can't get color depth from this, nor can we change it, do nothing
389              * TODO: get with XGetVisualInfo()? but then how to set?
390              */
391         }
392         if (fgState.GameModeRefresh != -1)
393         {
394             /* Compute the displays refresh rate, dotclock comes in kHz. */
395             int refresh = ( fgDisplay.DisplayModeClock * 1000 ) /
396                 ( fgDisplay.DisplayMode.htotal * fgDisplay.DisplayMode.vtotal );
397
398             fgState.GameModeRefresh = refresh;
399         }
400
401         /* query all possible display modes */
402         if( !XF86VidModeGetAllModeLines(
403                  fgDisplay.Display,
404                  fgDisplay.Screen,
405                  &displayModesCount,
406                  &displayModes ) )
407         {
408             fgWarning( "XF86VidModeGetAllModeLines failed" );
409             return success;
410         }
411
412
413         /*
414          * Check every of the modes looking for one that matches our demands,
415          * ignoring the refresh rate if no exact match could be found.
416          */
417         i = fghCheckDisplayModes( GL_TRUE, displayModesCount, displayModes );
418         if( i < 0 ) {
419             i = fghCheckDisplayModes( GL_FALSE, displayModesCount, displayModes );
420         }
421         success = ( i < 0 ) ? GL_FALSE : GL_TRUE;
422
423         if( !haveToTest && success ) {
424             if( !XF86VidModeSwitchToMode(
425                      fgDisplay.Display,
426                      fgDisplay.Screen,
427                      displayModes[ i ] ) )
428                 fgWarning( "XF86VidModeSwitchToMode failed" );
429         }
430
431         XFree( displayModes );
432     }
433
434 #   else
435
436     /*
437      * XXX warning fghChangeDisplayMode: missing XFree86 video mode extensions,
438      * XXX game mode will not change screen resolution when activated
439      */
440     success = GL_TRUE;
441
442 #   endif
443
444 #elif TARGET_HOST_MS_WINDOWS
445
446     DEVMODE  devMode;
447     char *fggmstr = NULL;
448     char displayMode[300];
449
450     success = GL_FALSE;
451
452     EnumDisplaySettings( fgDisplay.DisplayName, -1, &devMode ); 
453     devMode.dmFields = 0;
454
455     if (fgState.GameModeSize.X!=-1)
456     {
457         devMode.dmPelsWidth  = fgState.GameModeSize.X;
458         devMode.dmFields |= DM_PELSWIDTH;
459     }
460     if (fgState.GameModeSize.Y!=-1)
461     {
462         devMode.dmPelsHeight  = fgState.GameModeSize.Y;
463         devMode.dmFields |= DM_PELSHEIGHT;
464     }
465     if (fgState.GameModeDepth!=-1)
466     {
467         devMode.dmBitsPerPel  = fgState.GameModeDepth;
468         devMode.dmFields |= DM_BITSPERPEL;
469     }
470     if (fgState.GameModeRefresh!=-1)
471     {
472         devMode.dmDisplayFrequency  = fgState.GameModeRefresh;
473         devMode.dmFields |= DM_DISPLAYFREQUENCY;
474     }
475
476     switch ( ChangeDisplaySettingsEx(fgDisplay.DisplayName, &devMode, NULL, haveToTest ? CDS_TEST : CDS_FULLSCREEN , NULL) )
477     {
478     case DISP_CHANGE_SUCCESSFUL:
479         success = GL_TRUE;
480
481         if (!haveToTest)
482         {
483             /* update vars in case if windows switched to proper mode */
484             EnumDisplaySettings( fgDisplay.DisplayName, FREEGLUT_ENUM_CURRENT_SETTINGS, &devMode );
485             fgState.GameModeSize.X  = devMode.dmPelsWidth;        
486             fgState.GameModeSize.Y  = devMode.dmPelsHeight;
487             fgState.GameModeDepth   = devMode.dmBitsPerPel;
488             fgState.GameModeRefresh = devMode.dmDisplayFrequency;
489         }
490                 break;
491     case DISP_CHANGE_RESTART:
492         fggmstr = "The computer must be restarted for the graphics mode to work.";
493         break;
494     case DISP_CHANGE_BADFLAGS:
495         fggmstr = "An invalid set of flags was passed in.";
496         break;
497     case DISP_CHANGE_BADPARAM:
498         fggmstr = "An invalid parameter was passed in. This can include an invalid flag or combination of flags.";
499         break;
500     case DISP_CHANGE_FAILED:
501         fggmstr = "The display driver failed the specified graphics mode.";
502         break;
503     case DISP_CHANGE_BADMODE:
504         fggmstr = "The graphics mode is not supported.";
505         break;
506     default:
507         fggmstr = "Unknown error in graphics mode???"; /* dunno if it is possible,MSDN does not mention any other error */
508         break;
509     }
510
511     if ( !success )
512     {
513         /* I'd rather get info whats going on in my program than wonder about */
514         /* magic happenings behind my back, its lib for devels at last ;) */
515         
516         /* append display mode to error to make things more informative */
517         sprintf(displayMode,"%s Problem with requested mode: %ix%i:%i@%i", fggmstr, devMode.dmPelsWidth, devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmDisplayFrequency);
518         fgWarning(displayMode);
519     }
520 #endif
521
522     return success;
523 }
524
525
526 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
527
528 /*
529  * Sets the game mode display string
530  */
531 void FGAPIENTRY glutGameModeString( const char* string )
532 {
533     int width = -1, height = -1, depth = -1, refresh = -1;
534
535     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeString" );
536
537     /*
538      * This one seems a bit easier than glutInitDisplayString. The bad thing
539      * about it that I was unable to find the game mode string definition, so
540      * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
541      * appears in all GLUT game mode programs I have seen to date.
542      */
543     if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) !=
544         4 )
545         if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
546             if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
547                 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
548                     if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
549                         if( sscanf( string, ":%i", &depth ) != 1 )
550                             if( sscanf( string, "@%i", &refresh ) != 1 )
551                                 fgWarning(
552                                     "unable to parse game mode string `%s'",
553                                     string
554                                 );
555
556     /* All values not specified are now set to -1, which means those
557      * aspects of the current display mode are not changed in
558      * fghChangeDisplayMode() above.
559      */
560     fgState.GameModeSize.X  = width;
561     fgState.GameModeSize.Y  = height;
562     fgState.GameModeDepth   = depth;
563     fgState.GameModeRefresh = refresh;
564 }
565
566
567
568 /*
569  * Enters the game mode
570  */
571 int FGAPIENTRY glutEnterGameMode( void )
572 {
573     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutEnterGameMode" );
574
575     if( fgStructure.GameModeWindow )
576         fgAddToWindowDestroyList( fgStructure.GameModeWindow );
577     else
578         fghRememberState( );
579
580     if( ! fghChangeDisplayMode( GL_FALSE ) )
581     {
582         fgWarning( "failed to change screen settings" );
583         return 0;
584     }
585
586     fgStructure.GameModeWindow = fgCreateWindow(
587         NULL, "FREEGLUT", GL_TRUE, 0, 0,
588         GL_TRUE, fgState.GameModeSize.X, fgState.GameModeSize.Y,
589         GL_TRUE, GL_FALSE
590     );
591
592     fgStructure.GameModeWindow->State.Width  = fgState.GameModeSize.X;
593     fgStructure.GameModeWindow->State.Height = fgState.GameModeSize.Y;
594     fgStructure.GameModeWindow->State.NeedToResize = GL_TRUE;
595
596 #if TARGET_HOST_POSIX_X11
597
598     /*
599      * Sync needed to avoid a real race, the Xserver must have really created
600      * the window before we can grab the pointer into it:
601      */
602     XSync( fgDisplay.Display, False );
603
604     /*
605      * Grab the pointer to confine it into the window after the calls to
606      * XWrapPointer() which ensure that the pointer really enters the window.
607      *
608      * We also need to wait here until XGrabPointer() returns GrabSuccess,
609      * otherwise the new window is not viewable yet and if the next function
610      * (XSetInputFocus) is called with a not yet viewable window, it will exit
611      * the application which we have to aviod, so wait until it's viewable:
612      */
613     while( GrabSuccess != XGrabPointer(
614                fgDisplay.Display, fgStructure.GameModeWindow->Window.Handle,
615                TRUE,
616                ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
617                | PointerMotionMask,
618                GrabModeAsync, GrabModeAsync,
619                fgStructure.GameModeWindow->Window.Handle, None, CurrentTime) )
620         usleep( 100 );
621
622     /*
623      * Change input focus to the new window. This will exit the application
624      * if the new window is not viewable yet, see the XGrabPointer loop above.
625      */
626     XSetInputFocus(
627         fgDisplay.Display,
628         fgStructure.GameModeWindow->Window.Handle,
629         RevertToNone,
630         CurrentTime
631     );
632
633     /* Move the Pointer to the middle of the fullscreen window */
634     XWarpPointer(
635         fgDisplay.Display,
636         None,
637         fgDisplay.RootWindow,
638         0, 0, 0, 0,
639         fgState.GameModeSize.X/2, fgState.GameModeSize.Y/2
640     );
641
642 #   ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
643
644     if( fgDisplay.DisplayModeValid )
645     {
646         int x, y;
647         Window child;
648
649         /* Change to viewport to the window topleft edge: */
650         if( !XF86VidModeSetViewPort( fgDisplay.Display, fgDisplay.Screen, 0, 0 ) )
651             fgWarning( "XF86VidModeSetViewPort failed" );
652
653         /*
654          * Final window repositioning: It could be avoided using an undecorated
655          * window using override_redirect, but this * would possily require
656          * more changes and investigation.
657          */
658
659         /* Get the current postion of the drawable area on screen */
660         XTranslateCoordinates(
661             fgDisplay.Display,
662             fgStructure.CurrentWindow->Window.Handle,
663             fgDisplay.RootWindow,
664             0, 0, &x, &y,
665             &child
666         );
667
668         /* Move the decorataions out of the topleft corner of the display */
669         XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
670                      -x, -y);
671     }
672
673 #endif
674
675     /* Grab the keyboard, too */
676     XGrabKeyboard(
677         fgDisplay.Display,
678         fgStructure.GameModeWindow->Window.Handle,
679         FALSE,
680         GrabModeAsync, GrabModeAsync,
681         CurrentTime
682     );
683
684 #endif
685
686     return fgStructure.GameModeWindow->ID;
687 }
688
689 /*
690  * Leaves the game mode
691  */
692 void FGAPIENTRY glutLeaveGameMode( void )
693 {
694     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveGameMode" );
695
696     freeglut_return_if_fail( fgStructure.GameModeWindow );
697
698     fgAddToWindowDestroyList( fgStructure.GameModeWindow );
699     fgStructure.GameModeWindow = NULL;
700
701 #if TARGET_HOST_POSIX_X11
702
703     XUngrabPointer( fgDisplay.Display, CurrentTime );
704     XUngrabKeyboard( fgDisplay.Display, CurrentTime );
705
706 #endif
707
708     fghRestoreState();
709 }
710
711 /*
712  * Returns information concerning the freeglut game mode
713  */
714 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
715 {
716     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGameModeGet" );
717
718     switch( eWhat )
719     {
720     case GLUT_GAME_MODE_ACTIVE:
721         return !!fgStructure.GameModeWindow;
722
723     case GLUT_GAME_MODE_POSSIBLE:
724         return fghChangeDisplayMode( GL_TRUE );
725
726     case GLUT_GAME_MODE_WIDTH:
727         return fgState.GameModeSize.X;
728
729     case GLUT_GAME_MODE_HEIGHT:
730         return fgState.GameModeSize.Y;
731
732     case GLUT_GAME_MODE_PIXEL_DEPTH:
733         return fgState.GameModeDepth;
734
735     case GLUT_GAME_MODE_REFRESH_RATE:
736         return fgState.GameModeRefresh;
737
738     case GLUT_GAME_MODE_DISPLAY_CHANGED:
739         /*
740          * This is true if the game mode has been activated successfully..
741          */
742         return !!fgStructure.GameModeWindow;
743     }
744
745     fgWarning( "Unknown gamemode get: %d", eWhat );
746     return -1;
747 }
748
749 /*** END OF FILE ***/