48d9c9e7e9510b3d0486a5286e80a9933c78f19a
[freeglut] / src / mswin / fg_gamemode_mswin.c
1 /*
2  * freeglut_gamemode_mswin.c
3  *
4  * The Windows-specific mouse cursor related stuff.
5  *
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7  * Written by John F. Fay, <fayjf@sourceforge.net>
8  * Creation date: Thu Jan 19, 2012
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 "../fg_internal.h"
30
31 /*
32  * Changes to requested devmode, if it doesn't match current mode
33  */
34 GLboolean fghChangeDisplayMode(GLboolean haveToTest, DEVMODE *devModeRequested)
35 {
36     GLboolean success = GL_FALSE;
37     DEVMODE  devModeCurrent;
38     char *fggmstr = NULL;
39     char displayMode[300];
40
41     /* Get current display mode */
42     EnumDisplaySettings( fgDisplay.pDisplay.DisplayName, ENUM_CURRENT_SETTINGS, &devModeCurrent );
43     /* Now see if requested matches current mode, then we're done
44      * There's only four fields we touch:
45      * - dmPelsWidth
46      * - dmPelsHeight
47      * - dmBitsPerPel
48      * - dmDisplayFrequency
49      */
50     if (devModeCurrent.dmPelsWidth       ==devModeRequested->dmPelsWidth &&
51         devModeCurrent.dmPelsHeight      ==devModeRequested->dmPelsHeight && 
52         devModeCurrent.dmBitsPerPel      ==devModeRequested->dmBitsPerPel && 
53         devModeCurrent.dmDisplayFrequency==devModeRequested->dmDisplayFrequency)
54     {
55         if (!haveToTest)
56         {
57             /* update vars in case if actual switch was requested */
58             EnumDisplaySettings( fgDisplay.pDisplay.DisplayName, ENUM_CURRENT_SETTINGS, &devModeCurrent );
59             fgState.GameModeSize.X  = devModeCurrent.dmPelsWidth;        
60             fgState.GameModeSize.Y  = devModeCurrent.dmPelsHeight;
61             fgState.GameModeDepth   = devModeCurrent.dmBitsPerPel;
62             fgState.GameModeRefresh = devModeCurrent.dmDisplayFrequency;
63         }
64
65         /* We're done */
66         return GL_TRUE;
67     }
68
69
70     /* Ok, we do have a mode switch to perform/test */
71     switch ( ChangeDisplaySettingsEx(fgDisplay.pDisplay.DisplayName, devModeRequested, NULL, haveToTest ? CDS_TEST : CDS_FULLSCREEN , NULL) )
72     {
73     case DISP_CHANGE_SUCCESSFUL:
74         success = GL_TRUE;
75
76         if (!haveToTest)
77         {
78             /* update vars in case if windows switched to proper mode */
79             EnumDisplaySettings( fgDisplay.pDisplay.DisplayName, ENUM_CURRENT_SETTINGS, &devModeCurrent );
80             fgState.GameModeSize.X  = devModeCurrent.dmPelsWidth;        
81             fgState.GameModeSize.Y  = devModeCurrent.dmPelsHeight;
82             fgState.GameModeDepth   = devModeCurrent.dmBitsPerPel;
83             fgState.GameModeRefresh = devModeCurrent.dmDisplayFrequency;
84         }
85         break;
86     case DISP_CHANGE_RESTART:
87         fggmstr = "The computer must be restarted for the graphics mode to work.";
88         break;
89     case DISP_CHANGE_BADFLAGS:
90         fggmstr = "An invalid set of flags was passed in.";
91         break;
92     case DISP_CHANGE_BADPARAM:
93         fggmstr = "An invalid parameter was passed in. This can include an invalid flag or combination of flags.";
94         break;
95     case DISP_CHANGE_FAILED:
96         fggmstr = "The display driver failed the specified graphics mode.";
97         break;
98     case DISP_CHANGE_BADMODE:
99         fggmstr = "The graphics mode is not supported.";
100         break;
101     default:
102         fggmstr = "Unknown error in graphics mode???"; /* dunno if it is possible, MSDN does not mention any other error */
103         break;
104     }
105
106     if ( !success )
107     {
108         /* I'd rather get info whats going on in my program than wonder about */
109         /* what magic happens behind my back, its lib for devels after all ;) */
110
111         /* append display mode to error to make things more informative */
112         sprintf(displayMode,"%s Problem with requested mode: %lux%lu:%lu@%lu", fggmstr, devModeRequested->dmPelsWidth, devModeRequested->dmPelsHeight, devModeRequested->dmBitsPerPel, devModeRequested->dmDisplayFrequency);
113         fgWarning(displayMode);
114     }
115     
116     return success;
117 }
118
119 /*
120  * Remembers the current visual settings, so that
121  * we can change them and restore later...
122  */
123 void fgPlatformRememberState( void )
124 {
125     /* Grab the current desktop settings... */
126     EnumDisplaySettings( fgDisplay.pDisplay.DisplayName, ENUM_CURRENT_SETTINGS,
127                          &fgDisplay.pDisplay.DisplayMode );
128
129     /* Make sure we will be restoring all settings needed */
130     fgDisplay.pDisplay.DisplayMode.dmFields |=
131         DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
132
133 }
134
135 /*
136  * Restores the previously remembered visual settings
137  */
138 void fgPlatformRestoreState( void )
139 {
140     /* Restore the previously remembered desktop display settings */
141     fghChangeDisplayMode(GL_FALSE,&fgDisplay.pDisplay.DisplayMode);
142 }
143
144
145
146
147 /*
148  * Changes the current display mode to match user's settings
149  */
150 GLboolean fgPlatformChangeDisplayMode( GLboolean haveToTest )
151 {
152     DEVMODE  devMode;
153
154     /* Get current display mode */
155     EnumDisplaySettings( fgDisplay.pDisplay.DisplayName, ENUM_CURRENT_SETTINGS, &devMode ); 
156     devMode.dmFields = 0;
157
158     if (fgState.GameModeSize.X!=-1)
159     {
160         devMode.dmPelsWidth  = fgState.GameModeSize.X;
161         devMode.dmFields |= DM_PELSWIDTH;
162     }
163     if (fgState.GameModeSize.Y!=-1)
164     {
165         devMode.dmPelsHeight  = fgState.GameModeSize.Y;
166         devMode.dmFields |= DM_PELSHEIGHT;
167     }
168     if (fgState.GameModeDepth!=-1)
169     {
170         devMode.dmBitsPerPel  = fgState.GameModeDepth;
171         devMode.dmFields |= DM_BITSPERPEL;
172     }
173     if (fgState.GameModeRefresh!=-1)
174     {
175         devMode.dmDisplayFrequency  = fgState.GameModeRefresh;
176         devMode.dmFields |= DM_DISPLAYFREQUENCY;
177     }
178
179     return fghChangeDisplayMode(haveToTest, &devMode);
180 }
181
182 void fgPlatformEnterGameMode( void ) 
183 {
184 }
185
186 void fgPlatformLeaveGameMode( void ) 
187 {
188 }