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