about summary refs log tree commit diff
path: root/src/engine/external/glfw/lib/win32/win32_fullscreen.c
blob: fcecee67b0b274de34ed3a0ccadd96b1a7885bf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//========================================================================
// GLFW - An OpenGL framework
// File:        win32_fullscreen.c
// Platform:    Windows
// API version: 2.6
// WWW:         http://glfw.sourceforge.net
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Camilla Berglund
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would
//    be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
//    be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
//    distribution.
//
//========================================================================

#include "internal.h"


//************************************************************************
//****                  GLFW internal functions                       ****
//************************************************************************

//========================================================================
// _glfwBPP2RGB() - Convert BPP to RGB bits (based on "best guess")
//========================================================================

static void _glfwBPP2RGB( int bpp, int *r, int *g, int *b )
{
    int delta;

    // Special case: BPP = 32
    if( bpp == 32 ) bpp = 24;

    // Convert "bits per pixel" to red, green & blue sizes
    *r = *g = *b = bpp / 3;
    delta = bpp - (*r * 3);
    if( delta >= 1 )
    {
        *g = *g + 1;
    }
    if( delta == 2 )
    {
        *r = *r + 1;
    }
}


//========================================================================
// _glfwGetClosestVideoModeBPP()
//========================================================================

int _glfwGetClosestVideoModeBPP( int *w, int *h, int *bpp, int *refresh )
{
    int     mode, bestmode, match, bestmatch, rr, bestrr, success;
    DEVMODE dm;

    // Find best match
    bestmatch = 0x7fffffff;
    bestrr    = 0x7fffffff;
    mode = bestmode = 0;
    do
    {
        dm.dmSize = sizeof( DEVMODE );
        success = EnumDisplaySettings( NULL, mode, &dm );
        if( success )
        {
            match = dm.dmBitsPerPel - *bpp;
            if( match < 0 ) match = -match;
            match = ( match << 25 ) |
                    ( (dm.dmPelsWidth - *w) *
                      (dm.dmPelsWidth - *w) +
                      (dm.dmPelsHeight - *h) *
                      (dm.dmPelsHeight - *h) );
            if( match < bestmatch )
            {
                bestmatch = match;
                bestmode  = mode;
                bestrr = (dm.dmDisplayFrequency - *refresh) *
                         (dm.dmDisplayFrequency - *refresh);
            }
            else if( match == bestmatch && *refresh > 0 )
            {
                rr = (dm.dmDisplayFrequency - *refresh) *
                     (dm.dmDisplayFrequency - *refresh);
                if( rr < bestrr )
                {
                    bestmatch = match;
                    bestmode  = mode;
                    bestrr    = rr;
                }
            }
        }
        mode ++;
    }
    while( success );

    // Get the parameters for the best matching display mode
    dm.dmSize = sizeof( DEVMODE );
    (void) EnumDisplaySettings( NULL, bestmode, &dm );

    // Fill out actual width and height
    *w = dm.dmPelsWidth;
    *h = dm.dmPelsHeight;

    // Return bits per pixel
    *bpp = dm.dmBitsPerPel;

    // Return vertical refresh rate
    *refresh = dm.dmDisplayFrequency;

    return bestmode;
}


//========================================================================
// _glfwGetClosestVideoMode()
//========================================================================

int _glfwGetClosestVideoMode( int *w, int *h, int *r, int *g, int *b,
    int *refresh )
{
    int     bpp, bestmode;

    // Colorbits = sum of red/green/blue bits
    bpp = *r + *g + *b;

    // If colorbits < 15 (e.g. 0) or >= 24, default to 32 bpp
    if( bpp < 15 || bpp >= 24 )
    {
        bpp = 32;
    }

    // Find best match
    bestmode = _glfwGetClosestVideoModeBPP( w, h, &bpp, refresh );

    // Convert "bits per pixel" to red, green & blue sizes
    _glfwBPP2RGB( bpp, r, g, b );

    return bestmode;
}


//========================================================================
// Change the current video mode
//========================================================================

void _glfwSetVideoModeMODE( int mode )
{
    DEVMODE dm;
    int     success;

    // Get the parameters for the best matching display mode
    dm.dmSize = sizeof( DEVMODE );
    (void) EnumDisplaySettings( NULL, mode, &dm );

    // Set which fields we want to specify
    dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;

    // Do we have a prefered refresh rate?
    if( _glfwWin.DesiredRefreshRate > 0 )
    {
        dm.dmFields = dm.dmFields | DM_DISPLAYFREQUENCY;
	dm.dmDisplayFrequency = _glfwWin.DesiredRefreshRate;
    }

    // Change display setting
    dm.dmSize = sizeof( DEVMODE );
    success = ChangeDisplaySettings( &dm, CDS_FULLSCREEN );

    // If the mode change was not possible, query the current display
    // settings (we'll use the desktop resolution for fullscreen mode)
    if( success == DISP_CHANGE_SUCCESSFUL )
    {
        _glfwWin.ModeID = mode;
    }
    else
    {
        _glfwWin.ModeID = ENUM_REGISTRY_SETTINGS;
        EnumDisplaySettings( NULL, ENUM_REGISTRY_SETTINGS, &dm );
    }

    // Set the window size to that of the display mode
    _glfwWin.Width  = dm.dmPelsWidth;
    _glfwWin.Height = dm.dmPelsHeight;
}


//========================================================================
// _glfwSetVideoMode() - Change the current video mode
//========================================================================

void _glfwSetVideoMode( int *w, int *h, int r, int g, int b, int refresh )
{
    int     bestmode;

    // Find a best match mode
    bestmode = _glfwGetClosestVideoMode( w, h, &r, &g, &b, &refresh );

    // Change mode
    _glfwSetVideoModeMODE( bestmode );
}


//************************************************************************
//****                    GLFW user functions                         ****
//************************************************************************

//========================================================================
// _glfwPlatformGetVideoModes() - Get a list of available video modes
//========================================================================

int _glfwPlatformGetVideoModes( GLFWvidmode *list, int maxcount )
{
    int count, success, mode, i, j;
    int m1, m2, bpp, r, g, b;
    DEVMODE dm;

    // Loop through all video modes and extract all the UNIQUE modes
    count = 0;
    mode  = 0;
    do
    {
        // Get video mode properties
        dm.dmSize = sizeof( DEVMODE );
        success = EnumDisplaySettings( NULL, mode, &dm );

        // Is it a valid mode? (only list depths >= 15 bpp)
        if( success && dm.dmBitsPerPel >= 15 )
        {
            // Convert to RGB, and back to bpp ("mask out" alpha bits etc)
            _glfwBPP2RGB( dm.dmBitsPerPel, &r, &g, &b );
            bpp = r + g + b;

            // Mode "code" for this mode
            m1 = (bpp << 25) | (dm.dmPelsWidth * dm.dmPelsHeight);

            // Insert mode in list (sorted), and avoid duplicates
            for( i = 0; i < count; i ++ )
            {
                // Mode "code" for already listed mode
                bpp = list[i].RedBits + list[i].GreenBits +
                      list[i].BlueBits;
                m2 = (bpp << 25) | (list[i].Width * list[i].Height);
                if( m1 <= m2 )
                {
                    break;
                }
            }

            // New entry at the end of the list?
            if( i >= count )
            {
                list[count].Width     = dm.dmPelsWidth;
                list[count].Height    = dm.dmPelsHeight;
                list[count].RedBits   = r;
                list[count].GreenBits = g;
                list[count].BlueBits  = b;
                count ++;
            }
            // Insert new entry in the list?
            else if( m1 < m2 )
            {
                for( j = count; j > i; j -- )
                {
                    list[j] = list[j-1];
                }
                list[i].Width     = dm.dmPelsWidth;
                list[i].Height    = dm.dmPelsHeight;
                list[i].RedBits   = r;
                list[i].GreenBits = g;
                list[i].BlueBits  = b;
                count ++;
            }
        }
        mode ++;
    }
    while( success && (count < maxcount) );

    return count;
}


//========================================================================
// _glfwPlatformGetDesktopMode() - Get the desktop video mode
//========================================================================

void _glfwPlatformGetDesktopMode( GLFWvidmode *mode )
{
    DEVMODE dm;

    // Get desktop display mode
    dm.dmSize = sizeof( DEVMODE );
    (void) EnumDisplaySettings( NULL, ENUM_REGISTRY_SETTINGS, &dm );

    // Return desktop mode parameters
    mode->Width  = dm.dmPelsWidth;
    mode->Height = dm.dmPelsHeight;
    _glfwBPP2RGB( dm.dmBitsPerPel, &mode->RedBits, &mode->GreenBits,
                  &mode->BlueBits );
}