glfw/src/win32/win32_fullscreen.c

271 lines
8.7 KiB
C
Raw Normal View History

2010-09-07 17:34:51 +02:00
//========================================================================
// GLFW - An OpenGL framework
// Platform: Win32/WGL
2010-09-07 17:41:26 +02:00
// API version: 3.0
2010-09-07 17:34:51 +02: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 23:50:04 +02:00
#include <stdlib.h>
#include <limits.h>
2010-09-07 17:34:51 +02:00
2010-09-10 22:03:36 +02:00
//========================================================================
2010-09-13 23:50:04 +02:00
// Return closest video mode by dimensions, refresh rate and bits per pixel
2010-09-10 22:03:36 +02:00
//========================================================================
2010-09-13 23:50:04 +02:00
static GLboolean getClosestVideoMode(int* width, int* height,
int* bpp, int* refreshRate,
GLboolean exactBPP)
2010-09-10 22:03:36 +02:00
{
2010-09-13 23:50:04 +02:00
int mode, bestWidth, bestHeight, bestBPP, bestRate;
unsigned int sizeDiff, rateDiff, leastSizeDiff, leastRateDiff;
GLboolean foundMode = GL_FALSE;
DEVMODE dm;
2010-09-10 22:03:36 +02:00
2010-09-13 23:50:04 +02:00
leastSizeDiff = leastRateDiff = UINT_MAX;
2010-09-10 22:03:36 +02:00
2010-09-13 23:50:04 +02:00
for (mode = 0; ; mode++)
{
dm.dmSize = sizeof(DEVMODE);
if (!EnumDisplaySettings(NULL, mode, &dm))
break;
2010-09-10 22:03:36 +02:00
2010-09-13 23:50:04 +02:00
if (exactBPP && dm.dmBitsPerPel != *bpp)
continue;
2010-09-10 22:03:36 +02:00
2010-09-13 23:50:04 +02:00
sizeDiff = (abs(dm.dmBitsPerPel - *bpp) << 25) |
((dm.dmPelsWidth - *width) *
(dm.dmPelsWidth - *width) +
(dm.dmPelsHeight - *height) *
(dm.dmPelsHeight - *height));
2010-09-10 22:03:36 +02:00
2010-09-13 23:50:04 +02: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 22:03:36 +02:00
2010-09-13 23:50:04 +02:00
// We match first BPP, then screen area and last refresh rate
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
if ((sizeDiff < leastSizeDiff) ||
(sizeDiff == leastSizeDiff && (rateDiff < leastRateDiff)))
{
bestWidth = dm.dmPelsWidth;
bestHeight = dm.dmPelsHeight;
bestBPP = dm.dmBitsPerPel;
bestRate = dm.dmDisplayFrequency;
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
leastSizeDiff = sizeDiff;
leastRateDiff = rateDiff;
2010-09-10 22:03:36 +02:00
2010-09-13 23:50:04 +02:00
foundMode = GL_TRUE;
2010-09-07 17:34:51 +02:00
}
}
2010-09-13 23:50:04 +02:00
if (!foundMode)
return GL_FALSE;
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
*width = bestWidth;
*height = bestHeight;
*bpp = bestBPP;
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02: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 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
return GL_TRUE;
2010-09-07 17:34:51 +02:00
}
2010-09-13 23:50:04 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 17:34:51 +02:00
//========================================================================
// Change the current video mode
//========================================================================
2010-09-13 23:50:04 +02:00
void _glfwSetVideoMode(int* width, int* height,
int* bpp, int* refreshRate,
GLboolean exactBPP)
2010-09-07 17:34:51 +02:00
{
DEVMODE dm;
2010-09-13 23:50:04 +02:00
int closestWidth, closestHeight, closestBPP, closestRate;
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
closestWidth = *width;
closestHeight = *height;
closestBPP = *bpp;
closestRate = *refreshRate;
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
if (getClosestVideoMode(&closestWidth, &closestHeight,
&closestBPP, &closestRate, GL_FALSE))
2010-09-07 17:34:51 +02:00
{
2010-09-13 23:50:04 +02: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 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
if (*refreshRate > 0)
{
dm.dmFields |= DM_DISPLAYFREQUENCY;
dm.dmDisplayFrequency = closestRate;
}
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
{
*width = closestWidth;
*height = closestHeight;
*bpp = closestBPP;
*refreshRate = closestRate;
}
}
2010-09-07 17:34:51 +02:00
else
{
2010-09-13 23:50:04 +02:00
dm.dmSize = sizeof(DEVMODE);
2010-09-10 22:03:36 +02:00
EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &dm);
2010-09-07 17:34:51 +02:00
2010-09-13 23:50:04 +02:00
*width = dm.dmPelsWidth;
*height = dm.dmPelsHeight;
*bpp = dm.dmBitsPerPel;
*refreshRate = dm.dmDisplayFrequency;
}
2010-09-07 17:34:51 +02:00
}
//========================================================================
2010-09-13 23:50:04 +02:00
// Restore the previously saved (original) video mode
2010-09-07 17:34:51 +02:00
//========================================================================
2010-09-13 23:50:04 +02:00
void _glfwRestoreVideoMode(void)
2010-09-07 17:34:51 +02:00
{
2010-09-13 23:50:04 +02:00
ChangeDisplaySettings(NULL, CDS_FULLSCREEN);
2010-09-07 17:34:51 +02:00
}
2010-09-10 22:03:36 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 17:34:51 +02:00
//========================================================================
2010-09-07 17:50:43 +02:00
// Get a list of available video modes
2010-09-07 17:34:51 +02:00
//========================================================================
2010-09-10 22:03:36 +02:00
int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
2010-09-07 17:34:51 +02:00
{
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;
2010-09-10 22:03:36 +02:00
2010-09-07 17:34:51 +02:00
do
{
// Get video mode properties
2010-09-10 22:03:36 +02:00
dm.dmSize = sizeof(DEVMODE);
success = EnumDisplaySettings(NULL, mode, &dm);
2010-09-07 17:34:51 +02:00
// Is it a valid mode? (only list depths >= 15 bpp)
2010-09-10 22:03:36 +02:00
if (success && dm.dmBitsPerPel >= 15)
2010-09-07 17:34:51 +02:00
{
// Convert to RGB, and back to bpp ("mask out" alpha bits etc)
_glfwSplitBPP(dm.dmBitsPerPel, &r, &g, &b);
2010-09-07 17:34:51 +02:00
bpp = r + g + b;
// Mode "code" for this mode
m1 = (bpp << 25) | (dm.dmPelsWidth * dm.dmPelsHeight);
// Insert mode in list (sorted), and avoid duplicates
2010-09-10 22:03:36 +02:00
for (i = 0; i < count; i++)
2010-09-07 17:34:51 +02:00
{
// Mode "code" for already listed mode
2010-09-10 22:03:36 +02:00
bpp = list[i].redBits + list[i].greenBits + list[i].blueBits;
2010-09-08 16:50:50 +02:00
m2 = (bpp << 25) | (list[i].width * list[i].height);
2010-09-10 22:03:36 +02:00
if (m1 <= m2)
2010-09-07 17:34:51 +02:00
break;
}
// New entry at the end of the list?
2010-09-10 22:03:36 +02:00
if (i >= count)
2010-09-07 17:34:51 +02:00
{
2010-09-08 16:50:50 +02:00
list[count].width = dm.dmPelsWidth;
list[count].height = dm.dmPelsHeight;
list[count].redBits = r;
list[count].greenBits = g;
list[count].blueBits = b;
2010-09-07 17:34:51 +02:00
count ++;
}
// Insert new entry in the list?
2010-09-10 22:03:36 +02:00
else if (m1 < m2)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
for (j = count; j > i; j--)
list[j] = list[j - 1];
2010-09-08 16:50:50 +02:00
list[i].width = dm.dmPelsWidth;
list[i].height = dm.dmPelsHeight;
list[i].redBits = r;
list[i].greenBits = g;
list[i].blueBits = b;
2010-09-10 22:03:36 +02:00
count++;
2010-09-07 17:34:51 +02:00
}
}
2010-09-10 22:03:36 +02:00
mode++;
2010-09-07 17:34:51 +02:00
}
2010-09-10 22:03:36 +02:00
while (success && (count < maxcount));
2010-09-07 17:34:51 +02:00
return count;
}
//========================================================================
// Get the desktop video mode
//========================================================================
2010-09-10 22:03:36 +02:00
void _glfwPlatformGetDesktopMode(GLFWvidmode* mode)
2010-09-07 17:34:51 +02:00
{
DEVMODE dm;
// Get desktop display mode
2010-09-10 22:03:36 +02:00
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(NULL, ENUM_REGISTRY_SETTINGS, &dm);
2010-09-07 17:34:51 +02:00
// Return desktop mode parameters
2010-09-08 16:50:50 +02:00
mode->width = dm.dmPelsWidth;
mode->height = dm.dmPelsHeight;
_glfwSplitBPP(dm.dmBitsPerPel, &mode->redBits, &mode->greenBits, &mode->blueBits);
2010-09-07 17:34:51 +02:00
}