glfw/src/win32_fullscreen.c

282 lines
8.2 KiB
C
Raw Normal View History

2010-09-07 15:34:51 +00:00
//========================================================================
// GLFW - An OpenGL library
2012-08-28 13:03:57 +00:00
// Platform: Win32
2010-09-07 15:41:26 +00:00
// API version: 3.0
2010-09-07 15:34:51 +00:00
// WWW: http://www.glfw.org/
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// 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"
2010-09-13 21:50:04 +00:00
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
2010-09-13 21:50:04 +00:00
2010-09-07 15:34:51 +00:00
2010-09-10 20:03:36 +00:00
//========================================================================
2010-09-13 21:50:04 +00:00
// Return closest video mode by dimensions, refresh rate and bits per pixel
2010-09-10 20:03:36 +00:00
//========================================================================
2010-09-13 21:50:04 +00:00
static GLboolean getClosestVideoMode(int* width, int* height,
int* bpp, int* refreshRate,
GLboolean exactBPP)
2010-09-10 20:03:36 +00:00
{
2012-10-30 16:37:34 +00:00
int mode, bestWidth = 0, bestHeight = 0, bestBPP = 0, bestRate = 0;
2010-09-13 21:50:04 +00:00
unsigned int sizeDiff, rateDiff, leastSizeDiff, leastRateDiff;
GLboolean foundMode = GL_FALSE;
DEVMODE dm;
2010-09-10 20:03:36 +00:00
2010-09-13 21:50:04 +00:00
leastSizeDiff = leastRateDiff = UINT_MAX;
2010-09-10 20:03:36 +00:00
2010-09-13 21:50:04 +00:00
for (mode = 0; ; mode++)
{
dm.dmSize = sizeof(DEVMODE);
if (!EnumDisplaySettings(NULL, mode, &dm))
break;
2010-09-10 20:03:36 +00:00
2010-09-13 21:50:04 +00:00
if (exactBPP && dm.dmBitsPerPel != *bpp)
continue;
2010-09-10 20:03:36 +00:00
2010-09-13 21:50:04 +00:00
sizeDiff = (abs(dm.dmBitsPerPel - *bpp) << 25) |
((dm.dmPelsWidth - *width) *
(dm.dmPelsWidth - *width) +
(dm.dmPelsHeight - *height) *
(dm.dmPelsHeight - *height));
2010-09-10 20:03:36 +00:00
2010-09-13 21:50:04 +00:00
if (*refreshRate > 0)
{
rateDiff = (dm.dmDisplayFrequency - *refreshRate) *
(dm.dmDisplayFrequency - *refreshRate);
}
else
{
// If no refresh rate was specified, then they're all the same
rateDiff = 0;
}
2010-09-10 20:03:36 +00:00
2010-09-13 21:50:04 +00:00
// We match first BPP, then screen area and last refresh rate
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
if ((sizeDiff < leastSizeDiff) ||
(sizeDiff == leastSizeDiff && (rateDiff < leastRateDiff)))
{
bestWidth = dm.dmPelsWidth;
bestHeight = dm.dmPelsHeight;
bestBPP = dm.dmBitsPerPel;
bestRate = dm.dmDisplayFrequency;
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
leastSizeDiff = sizeDiff;
leastRateDiff = rateDiff;
2010-09-10 20:03:36 +00:00
2010-09-13 21:50:04 +00:00
foundMode = GL_TRUE;
2010-09-07 15:34:51 +00:00
}
}
2010-09-13 21:50:04 +00:00
if (!foundMode)
return GL_FALSE;
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
*width = bestWidth;
*height = bestHeight;
*bpp = bestBPP;
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
// Only save the found refresh rate if the client requested a specific
// rate; otherwise keep it at zero to let Windows select the best rate
if (*refreshRate > 0)
*refreshRate = bestRate;
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
return GL_TRUE;
2010-09-07 15:34:51 +00:00
}
2010-09-13 21:50:04 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 15:34:51 +00:00
//========================================================================
// Change the current video mode
//========================================================================
2010-09-13 21:50:04 +00:00
void _glfwSetVideoMode(int* width, int* height,
int* bpp, int* refreshRate,
GLboolean exactBPP)
2010-09-07 15:34:51 +00:00
{
DEVMODE dm;
2010-09-13 21:50:04 +00:00
int closestWidth, closestHeight, closestBPP, closestRate;
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
closestWidth = *width;
closestHeight = *height;
closestBPP = *bpp;
closestRate = *refreshRate;
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
if (getClosestVideoMode(&closestWidth, &closestHeight,
2012-10-30 16:20:16 +00:00
&closestBPP, &closestRate, exactBPP))
2010-09-07 15:34:51 +00:00
{
2010-09-13 21:50:04 +00:00
dm.dmSize = sizeof(DEVMODE);
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
dm.dmPelsWidth = closestWidth;
dm.dmPelsHeight = closestHeight;
dm.dmBitsPerPel = closestBPP;
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
if (*refreshRate > 0)
{
dm.dmFields |= DM_DISPLAYFREQUENCY;
dm.dmDisplayFrequency = closestRate;
}
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
{
*width = closestWidth;
*height = closestHeight;
*bpp = closestBPP;
*refreshRate = closestRate;
}
}
2010-09-07 15:34:51 +00:00
else
{
2010-09-13 21:50:04 +00:00
dm.dmSize = sizeof(DEVMODE);
2010-09-10 20:03:36 +00:00
EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &dm);
2010-09-07 15:34:51 +00:00
2010-09-13 21:50:04 +00:00
*width = dm.dmPelsWidth;
*height = dm.dmPelsHeight;
*bpp = dm.dmBitsPerPel;
*refreshRate = dm.dmDisplayFrequency;
}
2010-09-07 15:34:51 +00:00
}
//========================================================================
2010-09-13 21:50:04 +00:00
// Restore the previously saved (original) video mode
2010-09-07 15:34:51 +00:00
//========================================================================
2010-09-13 21:50:04 +00:00
void _glfwRestoreVideoMode(void)
2010-09-07 15:34:51 +00:00
{
2010-09-13 21:50:04 +00:00
ChangeDisplaySettings(NULL, CDS_FULLSCREEN);
2010-09-07 15:34:51 +00:00
}
2010-09-10 20:03:36 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 15:34:51 +00:00
//========================================================================
2010-09-07 15:50:43 +00:00
// Get a list of available video modes
2010-09-07 15:34:51 +00:00
//========================================================================
2012-08-02 16:03:43 +00:00
GLFWvidmode* _glfwPlatformGetVideoModes(int* found)
2010-09-07 15:34:51 +00:00
{
2012-08-02 16:03:43 +00:00
int dmIndex = 0, count = 0;
GLFWvidmode* result = NULL;
2010-09-07 15:34:51 +00:00
2012-08-02 16:03:43 +00:00
*found = 0;
2010-09-10 20:03:36 +00:00
2012-08-02 16:03:43 +00:00
for (;;)
2010-09-07 15:34:51 +00:00
{
2012-08-02 16:03:43 +00:00
int i;
GLFWvidmode mode;
DEVMODE dm;
ZeroMemory(&dm, sizeof(DEVMODE));
2010-09-10 20:03:36 +00:00
dm.dmSize = sizeof(DEVMODE);
2010-09-07 15:34:51 +00:00
2012-08-02 16:03:43 +00:00
if (!EnumDisplaySettings(NULL, dmIndex, &dm))
break;
dmIndex++;
if (dm.dmBitsPerPel < 15)
2010-09-07 15:34:51 +00:00
{
2012-08-02 16:03:43 +00:00
// Skip modes with less than 15 BPP
continue;
}
2010-09-07 15:34:51 +00:00
2012-08-02 16:03:43 +00:00
mode.width = dm.dmPelsWidth;
mode.height = dm.dmPelsHeight;
_glfwSplitBPP(dm.dmBitsPerPel,
&mode.redBits,
&mode.greenBits,
&mode.blueBits);
2010-09-07 15:34:51 +00:00
2012-08-02 16:03:43 +00:00
for (i = 0; i < *found; i++)
{
if (_glfwCompareVideoModes(result + i, &mode) == 0)
break;
}
2010-09-07 15:34:51 +00:00
2012-08-02 16:03:43 +00:00
if (i < *found)
{
// This is a duplicate, so skip it
continue;
}
if (*found == count)
{
void* larger;
if (count)
count *= 2;
else
count = 128;
larger = realloc(result, count * sizeof(GLFWvidmode));
if (!larger)
2010-09-07 15:34:51 +00:00
{
2012-08-02 16:03:43 +00:00
free(result);
_glfwSetError(GLFW_OUT_OF_MEMORY, NULL);
return NULL;
2010-09-07 15:34:51 +00:00
}
2012-08-02 16:03:43 +00:00
result = (GLFWvidmode*) larger;
2010-09-07 15:34:51 +00:00
}
2012-08-02 16:03:43 +00:00
result[*found] = mode;
(*found)++;
2010-09-07 15:34:51 +00:00
}
2012-08-02 16:03:43 +00:00
return result;
2010-09-07 15:34:51 +00:00
}
//========================================================================
// Get the desktop video mode
//========================================================================
2010-09-10 20:03:36 +00:00
void _glfwPlatformGetDesktopMode(GLFWvidmode* mode)
2010-09-07 15:34:51 +00:00
{
DEVMODE dm;
// Get desktop display mode
2010-09-10 20:03:36 +00:00
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &dm);
2010-09-07 15:34:51 +00:00
// Return desktop mode parameters
2010-09-08 14:50:50 +00:00
mode->width = dm.dmPelsWidth;
mode->height = dm.dmPelsHeight;
2011-07-27 15:48:56 +00:00
_glfwSplitBPP(dm.dmBitsPerPel,
&mode->redBits,
&mode->greenBits,
&mode->blueBits);
2010-09-07 15:34:51 +00:00
}