e9e92764b83cb2ea3bbee0338158c7bb6a2e117f
[freeglut] / freeglut-1.3 / 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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define  G_LOG_DOMAIN  "freeglut-gamemode"
33
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  *  glutGameModeString()    -- missing
41  *  glutEnterGameMode()     -- X11 version
42  *  glutLeaveGameMode()     -- is that correct?
43  *  glutGameModeGet()       -- is that correct?
44  */
45
46
47 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
48
49 /*
50  * Remembers the current visual settings, so that
51  * we can change them and restore later...
52  */
53 void fghRememberState( void )
54 {
55 #if TARGET_HOST_UNIX_X11
56
57     /*
58      * This highly depends on the XFree86 extensions, not approved as X Consortium standards
59      */
60 #   ifdef X_XF86VidModeGetModeLine
61
62     /*
63      * Query the current display settings:
64      */
65     XF86VidModeGetModeLine(
66         fgDisplay.Display,
67         fgDisplay.Screen,
68         &fgDisplay.DisplayModeClock,
69         &fgDisplay.DisplayMode
70     );
71
72 #   else
73 #       warning fghRememberState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
74 #   endif
75
76 #elif TARGET_HOST_WIN32
77
78     DEVMODE devMode;
79
80     /*
81      * Grab the current desktop settings...
82      */
83
84 /* hack to get around my stupid cross-gcc headers */
85 #define FREEGLUT_ENUM_CURRENT_SETTINGS -1
86
87     EnumDisplaySettings( NULL, FREEGLUT_ENUM_CURRENT_SETTINGS, &fgDisplay.DisplayMode );
88
89     /*
90      * Make sure we will be restoring all settings needed
91      */
92     fgDisplay.DisplayMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
93
94 #endif
95 }
96
97 /*
98  * Restores the previously remembered visual settings
99  */
100 void fghRestoreState( void )
101 {
102 #if TARGET_HOST_UNIX_X11
103
104     /*
105      * This highly depends on the XFree86 extensions, not approved as X Consortium standards
106      */
107 #   ifdef X_XF86VidModeGetAllModeLines
108
109     XF86VidModeModeInfo** displayModes;
110     int i, displayModesCount;
111
112     /*
113      * Query for all the display available...
114      */
115     XF86VidModeGetAllModeLines(
116         fgDisplay.Display,
117         fgDisplay.Screen,
118         &displayModesCount,
119         &displayModes
120     );
121
122     /*
123      * Check every of the modes looking for one that matches our demands
124      */
125     for( i=0; i<displayModesCount; i++ )
126     {
127         if( displayModes[ i ]->hdisplay == fgDisplay.DisplayMode.hdisplay &&
128             displayModes[ i ]->vdisplay == fgDisplay.DisplayMode.vdisplay &&
129             displayModes[ i ]->dotclock == fgDisplay.DisplayModeClock )
130         {
131             /*
132              * OKi, this is the display mode we have been looking for...
133              */
134             XF86VidModeSwitchToMode(
135                 fgDisplay.Display,
136                 fgDisplay.Screen,
137                 displayModes[ i ]
138             );
139
140             /*
141              * In case this will be the last X11 call we do before exit,
142              * we've to flush the X11 output queue to be sure the command
143              * is really brought onto it's way to the X server.
144              * The application should not do this because it
145              * would not be platform independent then.
146              */
147             XFlush(fgDisplay.Display);
148
149             return;
150         }
151     }
152
153 #   else
154 #       warning fghRestoreState: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
155 #   endif
156
157 #elif TARGET_HOST_WIN32
158
159     /*
160      * Restore the previously rememebered desktop display settings
161      */
162     ChangeDisplaySettings( &fgDisplay.DisplayMode, 0 );
163
164 #endif
165 }
166
167 /*
168  * Checks the display mode settings against user's preferences
169  */
170 GLboolean fghCheckDisplayMode( int width, int height, int depth, int refresh )
171 {
172     /*
173      * The desired values should be stored in fgState structure...
174      */
175     return( (width == fgState.GameModeSize.X) && (height == fgState.GameModeSize.Y) &&
176             (depth == fgState.GameModeDepth)  && (refresh == fgState.GameModeRefresh) );
177 }
178
179 /*
180  * Changes the current display mode to match user's settings
181  */
182 GLboolean fghChangeDisplayMode( GLboolean haveToTest )
183 {
184 #if TARGET_HOST_UNIX_X11
185
186     /*
187      * This highly depends on the XFree86 extensions, not approved as X Consortium standards
188      */
189 #   ifdef X_XF86VidModeGetAllModeLines
190
191     XF86VidModeModeInfo** displayModes;
192     int i, displayModesCount;
193
194     /*
195      * Query for all the display available...
196      */
197     XF86VidModeGetAllModeLines(
198         fgDisplay.Display,
199         fgDisplay.Screen,
200         &displayModesCount,
201         &displayModes
202     );
203
204     /*
205      * Check every of the modes looking for one that matches our demands
206      */
207     for( i=0; i<displayModesCount; i++ )
208     {
209         if( fghCheckDisplayMode( displayModes[ i ]->hdisplay, displayModes[ i ]->vdisplay,
210                                  fgState.GameModeDepth, fgState.GameModeRefresh ) )
211         {
212             if( haveToTest )
213                 return( TRUE );
214             /*
215              * OKi, this is the display mode we have been looking for...
216              */
217             XF86VidModeSwitchToMode(
218                 fgDisplay.Display,
219                 fgDisplay.Screen,
220                 displayModes[ i ]
221             );
222
223             /*
224              * Set the viewport's origin to (0,0) (the game mode window's top-left corner)
225              */
226             XF86VidModeSetViewPort(
227                 fgDisplay.Display,
228                 fgDisplay.Screen,
229                 0,
230                 0
231             );
232
233             /*
234              * Return successfull...
235              */
236             return( TRUE );
237         }
238     }
239
240     /*
241      * Something must have went wrong
242      */
243     return( FALSE );
244
245 #   else
246 #       warning fghChangeDisplayMode: missing XFree86 video mode extensions, game mode will not change screen resolution when activated
247 #   endif
248
249 #elif TARGET_HOST_WIN32
250
251     unsigned int    displayModes = 0, mode = 0xffffffff;
252     GLboolean success = FALSE;
253     HDC      desktopDC;
254     DEVMODE  devMode;
255
256     /*
257      * Enumerate the available display modes
258      * Try to get a complete match
259      */
260     while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
261     {
262         /*
263          * Does the enumerated display mode match the user's preferences?
264          */
265         if( fghCheckDisplayMode( devMode.dmPelsWidth,  devMode.dmPelsHeight,
266                                  devMode.dmBitsPerPel, devMode.dmDisplayFrequency ) )
267         {
268             /*
269              * OKi, we've found a matching display mode, remember its number and break
270              */
271             mode = displayModes;
272             break;
273         }
274
275         /*
276          * Switch to the next display mode, if any
277          */
278         displayModes++;
279     }
280
281     if( mode == 0xffffffff )
282     {
283       /* then try without Display Frequency */
284       displayModes=0;
285
286       /*
287        * Enumerate the available display modes
288        */
289       while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
290         {
291           /* then try without Display Frequency */
292         
293           if( fghCheckDisplayMode( devMode.dmPelsWidth,  devMode.dmPelsHeight,
294                                    devMode.dmBitsPerPel, fgState.GameModeRefresh))
295             {
296               /*
297                * OKi, we've found a matching display mode, remember its number and break
298                */
299               mode = displayModes;
300               break;
301             }
302         
303           /*
304            * Switch to the next display mode, if any
305            */
306           displayModes++;
307         }
308     }
309
310     /*
311      * Did we find a matching display mode?
312      */
313     if( mode != 0xffffffff )
314     {
315         int retVal = DISP_CHANGE_SUCCESSFUL;
316
317         /*
318          * Mark the values we want to modify in the display change call
319          */
320         devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
321
322         /*
323          * Change the current display mode (possibly in test mode only)
324          */
325         retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
326
327         /*
328          * I don't know if it's really needed, but looks nice:
329          */
330         success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
331
332         /*
333          * If it was not a test, remember the current screen settings
334          */
335         if( !haveToTest && success )
336         {
337             fgState.GameModeSize.X  = devMode.dmPelsWidth;
338             fgState.GameModeSize.Y  = devMode.dmPelsHeight;
339             fgState.GameModeDepth   = devMode.dmBitsPerPel;
340             fgState.GameModeRefresh = devMode.dmDisplayFrequency;
341         }
342     }
343
344     /*
345      * Otherwise we must have failed somewhere
346      */
347     return( success );
348
349 #endif
350 }
351
352
353 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
354
355 /*
356  * Sets the game mode display string
357  */
358 void FGAPIENTRY glutGameModeString( const char* string )
359 {
360     int width = 640, height = 480, depth = 16, refresh = 72;
361
362     /*
363      * This one seems a bit easier than glutInitDisplayString. The bad thing
364      * about it that I was unable to find the game mode string definition, so
365      * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
366      * appears in all GLUT game mode programs I have seen to date.
367      */
368     if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
369         if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
370             if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
371                 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
372                     if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
373                         if( sscanf( string, ":%i", &depth ) != 1 )
374                             if( sscanf( string, "@%i", &refresh ) != 1 )
375                                 fgWarning( "unable to parse game mode string `%s'", string );
376
377     /*
378      * Hopefully it worked, and if not, we still have the default values
379      */
380     fgState.GameModeSize.X  = width;
381     fgState.GameModeSize.Y  = height;
382     fgState.GameModeDepth   = depth;
383     fgState.GameModeRefresh = refresh;
384 }
385
386 /*
387  * Enters the game mode
388  */
389 int FGAPIENTRY glutEnterGameMode( void )
390 {
391     /*
392      * Check if a game mode window already exists...
393      */
394     if( fgStructure.GameMode != NULL )
395     {
396         /*
397          * ...if so, delete it before proceeding...
398          */
399         fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
400     }
401     else
402     {
403         /*
404          * ...otherwise remember the current resolution, etc.
405          */
406         fghRememberState();
407     }
408
409     /*
410      * We are ready to change the current screen's resolution now
411      */
412     if( fghChangeDisplayMode( FALSE ) == FALSE )
413     {
414               fgWarning( "failed to change screen settings" );
415         return( FALSE );
416     }
417
418     /*
419      * Finally, have the game mode window created
420      */
421     fgStructure.GameMode = fgCreateWindow( 
422         NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE 
423     );
424
425 #if TARGET_HOST_UNIX_X11
426
427     /*
428      * Move the mouse pointer over the game mode window
429      */
430     XSetInputFocus(
431         fgDisplay.Display,
432         fgStructure.GameMode->Window.Handle,
433         RevertToNone,
434         CurrentTime
435     );
436
437     /*
438      * Confine the mouse pointer to the window's client area
439      */
440     XGrabPointer(
441         fgDisplay.Display,
442         fgStructure.GameMode->Window.Handle,
443         TRUE,
444         ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask,
445         GrabModeAsync, GrabModeAsync,
446         fgStructure.GameMode->Window.Handle,
447         None,
448         CurrentTime
449     );
450
451     /*
452      * Grab the keyboard, too
453      */
454     XGrabKeyboard(
455         fgDisplay.Display,
456         fgStructure.GameMode->Window.Handle,
457         FALSE,
458         GrabModeAsync, GrabModeAsync,
459         CurrentTime
460     );
461
462 #endif
463
464     /*
465      * Return successfull
466      */
467     return( TRUE );
468 }
469
470 /*
471  * Leaves the game mode
472  */
473 void FGAPIENTRY glutLeaveGameMode( void )
474 {
475     freeglut_return_if_fail( fgStructure.GameMode != NULL );
476
477     /*
478      * First of all, have the game mode window destroyed
479      */
480     fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
481
482 #if TARGET_HOST_UNIX_X11
483
484     /*
485      * Ungrab the mouse and keyboard
486      */
487     XUngrabPointer( fgDisplay.Display, CurrentTime );
488     XUngrabKeyboard( fgDisplay.Display, CurrentTime );
489
490 #endif
491
492     /*
493      * Then, have the desktop visual settings restored
494      */
495     fghRestoreState();
496 }
497
498 /*
499  * Returns information concerning the freeglut game mode
500  */
501 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
502 {
503     /*
504      * See why are we bothered
505      */
506     switch( eWhat )
507     {
508     case GLUT_GAME_MODE_ACTIVE:
509         /*
510          * Check if the game mode is currently active
511          */
512         return( fgStructure.GameMode != NULL );
513
514     case GLUT_GAME_MODE_POSSIBLE:
515         /*
516          * Check if the current game mode settings are valid
517          */
518         return( fghChangeDisplayMode( TRUE ) );
519
520     case GLUT_GAME_MODE_WIDTH:
521         /*
522          * The game mode screen width
523          */
524         return( fgState.GameModeSize.X );
525
526     case GLUT_GAME_MODE_HEIGHT:
527         /*
528          * The game mode screen height
529          */
530         return( fgState.GameModeSize.Y );
531
532     case GLUT_GAME_MODE_PIXEL_DEPTH:
533         /*
534          * The game mode pixel depth
535          */
536         return( fgState.GameModeDepth );
537
538     case GLUT_GAME_MODE_REFRESH_RATE:
539         /*
540          * The game mode refresh rate
541          */
542         return( fgState.GameModeRefresh );
543
544     case GLUT_GAME_MODE_DISPLAY_CHANGED:
545         /*
546          * This is true if the game mode has been activated successfully..
547          */
548         return( fgStructure.GameMode != NULL );
549     }
550
551     return( -1 );
552 }
553
554 /*** END OF FILE ***/
555
556
557
558