Added FREEGLUT_VERSION_1_3.
[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      */
259     while( EnumDisplaySettings( NULL, displayModes, &devMode ) == TRUE )
260     {
261         /*
262          * Does the enumerated display mode match the user's preferences?
263          */
264         if( fghCheckDisplayMode( devMode.dmPelsWidth,  devMode.dmPelsHeight,
265                                  devMode.dmBitsPerPel, fgState.GameModeRefresh ) )
266         {
267             /*
268              * OKi, we've found a matching display mode, remember its number and break
269              */
270             mode = displayModes;
271             break;
272         }
273
274         /*
275          * Switch to the next display mode, if any
276          */
277         displayModes++;
278     }
279
280     /*
281      * Did we find a matching display mode?
282      */
283     if( mode != 0xffffffff )
284     {
285         int retVal = DISP_CHANGE_SUCCESSFUL;
286
287         /*
288          * Mark the values we want to modify in the display change call
289          */
290         devMode.dmFields |= DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
291
292         /*
293          * Change the current display mode (possibly in test mode only)
294          */
295         retVal = ChangeDisplaySettings( &devMode, haveToTest ? CDS_TEST : 0 );
296
297         /*
298          * I don't know if it's really needed, but looks nice:
299          */
300         success = (retVal == DISP_CHANGE_SUCCESSFUL) || (retVal == DISP_CHANGE_NOTUPDATED);
301
302         /*
303          * If it was not a test, remember the current screen settings
304          */
305         if( !haveToTest && success )
306         {
307             fgState.GameModeSize.X  = devMode.dmPelsWidth;
308             fgState.GameModeSize.Y  = devMode.dmPelsHeight;
309             fgState.GameModeDepth   = devMode.dmBitsPerPel;
310             fgState.GameModeRefresh = devMode.dmDisplayFrequency;
311         }
312     }
313
314     /*
315      * Otherwise we must have failed somewhere
316      */
317     return( success );
318
319 #endif
320 }
321
322
323 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
324
325 /*
326  * Sets the game mode display string
327  */
328 void FGAPIENTRY glutGameModeString( const char* string )
329 {
330     int width = 640, height = 480, depth = 16, refresh = 72;
331
332     /*
333      * This one seems a bit easier than glutInitDisplayString. The bad thing
334      * about it that I was unable to find the game mode string definition, so
335      * that I assumed it is: "[width]x[height]:[depth]@[refresh rate]", which
336      * appears in all GLUT game mode programs I have seen to date.
337      */
338     if( sscanf( string, "%ix%i:%i@%i", &width, &height, &depth, &refresh ) != 4 )
339         if( sscanf( string, "%ix%i:%i", &width, &height, &depth ) != 3 )
340             if( sscanf( string, "%ix%i@%i", &width, &height, &refresh ) != 3 )
341                 if( sscanf( string, "%ix%i", &width, &height ) != 2 )
342                     if( sscanf( string, ":%i@%i", &depth, &refresh ) != 2 )
343                         if( sscanf( string, ":%i", &depth ) != 1 )
344                             if( sscanf( string, "@%i", &refresh ) != 1 )
345                                 fgWarning( "unable to parse game mode string `%s'", string );
346
347     /*
348      * Hopefully it worked, and if not, we still have the default values
349      */
350     fgState.GameModeSize.X  = width;
351     fgState.GameModeSize.Y  = height;
352     fgState.GameModeDepth   = depth;
353     fgState.GameModeRefresh = refresh;
354 }
355
356 /*
357  * Enters the game mode
358  */
359 int FGAPIENTRY glutEnterGameMode( void )
360 {
361     /*
362      * Check if a game mode window already exists...
363      */
364     if( fgStructure.GameMode != NULL )
365     {
366         /*
367          * ...if so, delete it before proceeding...
368          */
369         fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
370     }
371     else
372     {
373         /*
374          * ...otherwise remember the current resolution, etc.
375          */
376         fghRememberState();
377     }
378
379     /*
380      * We are ready to change the current screen's resolution now
381      */
382     if( fghChangeDisplayMode( FALSE ) == FALSE )
383     {
384               fgWarning( "failed to change screen settings" );
385         return( FALSE );
386     }
387
388     /*
389      * Finally, have the game mode window created
390      */
391     fgStructure.GameMode = fgCreateWindow( 
392         NULL, "FREEGLUT", 0, 0, fgState.GameModeSize.X, fgState.GameModeSize.Y, TRUE 
393     );
394
395 #if TARGET_HOST_UNIX_X11
396
397     /*
398      * Move the mouse pointer over the game mode window
399      */
400     XSetInputFocus(
401         fgDisplay.Display,
402         fgStructure.GameMode->Window.Handle,
403         RevertToNone,
404         CurrentTime
405     );
406
407     /*
408      * Confine the mouse pointer to the window's client area
409      */
410     XGrabPointer(
411         fgDisplay.Display,
412         fgStructure.GameMode->Window.Handle,
413         TRUE,
414         ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask,
415         GrabModeAsync, GrabModeAsync,
416         fgStructure.GameMode->Window.Handle,
417         None,
418         CurrentTime
419     );
420
421     /*
422      * Grab the keyboard, too
423      */
424     XGrabKeyboard(
425         fgDisplay.Display,
426         fgStructure.GameMode->Window.Handle,
427         FALSE,
428         GrabModeAsync, GrabModeAsync,
429         CurrentTime
430     );
431
432 #endif
433
434     /*
435      * Return successfull
436      */
437     return( TRUE );
438 }
439
440 /*
441  * Leaves the game mode
442  */
443 void FGAPIENTRY glutLeaveGameMode( void )
444 {
445     freeglut_return_if_fail( fgStructure.GameMode != NULL );
446
447     /*
448      * First of all, have the game mode window destroyed
449      */
450     fgAddToWindowDestroyList( fgStructure.GameMode, TRUE );
451
452 #if TARGET_HOST_UNIX_X11
453
454     /*
455      * Ungrab the mouse and keyboard
456      */
457     XUngrabPointer( fgDisplay.Display, CurrentTime );
458     XUngrabKeyboard( fgDisplay.Display, CurrentTime );
459
460 #endif
461
462     /*
463      * Then, have the desktop visual settings restored
464      */
465     fghRestoreState();
466 }
467
468 /*
469  * Returns information concerning the freeglut game mode
470  */
471 int FGAPIENTRY glutGameModeGet( GLenum eWhat )
472 {
473     /*
474      * See why are we bothered
475      */
476     switch( eWhat )
477     {
478     case GLUT_GAME_MODE_ACTIVE:
479         /*
480          * Check if the game mode is currently active
481          */
482         return( fgStructure.GameMode != NULL );
483
484     case GLUT_GAME_MODE_POSSIBLE:
485         /*
486          * Check if the current game mode settings are valid
487          */
488         return( fghChangeDisplayMode( TRUE ) );
489
490     case GLUT_GAME_MODE_WIDTH:
491         /*
492          * The game mode screen width
493          */
494         return( fgState.GameModeSize.X );
495
496     case GLUT_GAME_MODE_HEIGHT:
497         /*
498          * The game mode screen height
499          */
500         return( fgState.GameModeSize.Y );
501
502     case GLUT_GAME_MODE_PIXEL_DEPTH:
503         /*
504          * The game mode pixel depth
505          */
506         return( fgState.GameModeDepth );
507
508     case GLUT_GAME_MODE_REFRESH_RATE:
509         /*
510          * The game mode refresh rate
511          */
512         return( fgState.GameModeRefresh );
513
514     case GLUT_GAME_MODE_DISPLAY_CHANGED:
515         /*
516          * This is true if the game mode has been activated successfully..
517          */
518         return( fgStructure.GameMode != NULL );
519     }
520
521     return( -1 );
522 }
523
524 /*** END OF FILE ***/
525
526
527
528