glfw/src/win32_monitor.c

402 lines
12 KiB
C
Raw Normal View History

//========================================================================
2015-06-01 22:55:06 +02:00
// GLFW 3.2 Win32 - 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"
#include <stdlib.h>
#include <string.h>
2012-09-12 21:04:24 +02:00
#include <limits.h>
#include <malloc.h>
// Create monitor from an adapter and (optionally) a display
//
static _GLFWmonitor* createMonitor(DISPLAY_DEVICEW* adapter,
DISPLAY_DEVICEW* display)
{
_GLFWmonitor* monitor;
char* name;
HDC dc;
if (display)
name = _glfwCreateUTF8FromWideStringWin32(display->DeviceString);
else
name = _glfwCreateUTF8FromWideStringWin32(adapter->DeviceString);
if (!name)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Failed to convert string to UTF-8");
return NULL;
}
dc = CreateDCW(L"DISPLAY", adapter->DeviceName, NULL, NULL);
monitor = _glfwAllocMonitor(name,
GetDeviceCaps(dc, HORZSIZE),
GetDeviceCaps(dc, VERTSIZE));
DeleteDC(dc);
free(name);
if (adapter->StateFlags & DISPLAY_DEVICE_MODESPRUNED)
monitor->win32.modesPruned = GLFW_TRUE;
wcscpy(monitor->win32.adapterName, adapter->DeviceName);
WideCharToMultiByte(CP_UTF8, 0,
adapter->DeviceName, -1,
monitor->win32.publicAdapterName,
sizeof(monitor->win32.publicAdapterName),
NULL, NULL);
if (display)
{
wcscpy(monitor->win32.displayName, display->DeviceName);
WideCharToMultiByte(CP_UTF8, 0,
display->DeviceName, -1,
monitor->win32.publicDisplayName,
sizeof(monitor->win32.publicDisplayName),
NULL, NULL);
}
return monitor;
}
2012-09-12 21:04:24 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
// Change the current video mode
2013-02-04 13:22:10 +01:00
//
GLFWbool _glfwSetVideoModeWin32(_GLFWmonitor* monitor, const GLFWvidmode* desired)
2012-09-12 21:04:24 +02:00
{
GLFWvidmode current;
const GLFWvidmode* best;
DEVMODEW dm;
2012-09-12 21:04:24 +02:00
2013-04-21 21:28:07 +02:00
best = _glfwChooseVideoMode(monitor, desired);
_glfwPlatformGetVideoMode(monitor, &current);
if (_glfwCompareVideoModes(&current, best) == 0)
2015-08-23 19:30:04 +02:00
return GLFW_TRUE;
2012-09-12 21:04:24 +02:00
2013-06-09 11:45:51 +02:00
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(DEVMODEW);
2013-05-30 15:52:42 +02:00
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL |
DM_DISPLAYFREQUENCY;
dm.dmPelsWidth = best->width;
dm.dmPelsHeight = best->height;
dm.dmBitsPerPel = best->redBits + best->greenBits + best->blueBits;
dm.dmDisplayFrequency = best->refreshRate;
if (dm.dmBitsPerPel < 15 || dm.dmBitsPerPel >= 24)
dm.dmBitsPerPel = 32;
if (ChangeDisplaySettingsExW(monitor->win32.adapterName,
&dm,
NULL,
CDS_FULLSCREEN,
NULL) != DISP_CHANGE_SUCCESSFUL)
2012-09-12 21:04:24 +02:00
{
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to set video mode");
2015-08-23 19:30:04 +02:00
return GLFW_FALSE;
2012-09-12 21:04:24 +02:00
}
2015-08-23 19:30:04 +02:00
monitor->win32.modeChanged = GLFW_TRUE;
return GLFW_TRUE;
2012-09-12 21:04:24 +02:00
}
// Restore the previously saved (original) video mode
2013-02-04 13:22:10 +01:00
//
void _glfwRestoreVideoModeWin32(_GLFWmonitor* monitor)
2012-09-12 21:04:24 +02:00
{
if (monitor->win32.modeChanged)
{
ChangeDisplaySettingsExW(monitor->win32.adapterName,
NULL, NULL, CDS_FULLSCREEN, NULL);
2015-08-23 19:30:04 +02:00
monitor->win32.modeChanged = GLFW_FALSE;
}
2012-09-12 21:04:24 +02:00
}
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2012-09-12 19:35:52 +02:00
_GLFWmonitor** _glfwPlatformGetMonitors(int* count)
{
int found = 0;
DWORD adapterIndex, displayIndex, primaryIndex = 0;
DISPLAY_DEVICEW adapter, display;
GLFWbool hasDisplays = GLFW_FALSE;
2012-09-12 19:35:52 +02:00
_GLFWmonitor** monitors = NULL;
2013-06-09 13:07:23 +02:00
*count = 0;
// HACK: Check if any active adapters have connected displays
// If not, this is a headless system or a VMware guest
for (adapterIndex = 0; ; adapterIndex++)
2012-09-12 19:35:52 +02:00
{
ZeroMemory(&adapter, sizeof(DISPLAY_DEVICEW));
adapter.cb = sizeof(DISPLAY_DEVICEW);
if (!EnumDisplayDevicesW(NULL, adapterIndex, &adapter, 0))
2012-09-12 19:35:52 +02:00
break;
if (!(adapter.StateFlags & DISPLAY_DEVICE_ACTIVE))
2012-09-12 19:35:52 +02:00
continue;
ZeroMemory(&display, sizeof(DISPLAY_DEVICEW));
display.cb = sizeof(DISPLAY_DEVICEW);
if (EnumDisplayDevicesW(adapter.DeviceName, 0, &display, 0))
2012-09-12 22:51:55 +02:00
{
hasDisplays = GLFW_TRUE;
break;
}
}
2012-09-12 19:35:52 +02:00
for (adapterIndex = 0; ; adapterIndex++)
{
ZeroMemory(&adapter, sizeof(DISPLAY_DEVICEW));
adapter.cb = sizeof(DISPLAY_DEVICEW);
2012-09-13 17:46:40 +02:00
if (!EnumDisplayDevicesW(NULL, adapterIndex, &adapter, 0))
break;
2012-09-12 19:35:52 +02:00
if (!(adapter.StateFlags & DISPLAY_DEVICE_ACTIVE))
continue;
if (adapter.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
primaryIndex = found;
2012-09-12 19:35:52 +02:00
if (hasDisplays)
{
for (displayIndex = 0; ; displayIndex++)
{
ZeroMemory(&display, sizeof(DISPLAY_DEVICEW));
display.cb = sizeof(DISPLAY_DEVICEW);
2014-10-28 15:35:03 +01:00
if (!EnumDisplayDevicesW(adapter.DeviceName, displayIndex, &display, 0))
break;
found++;
monitors = realloc(monitors, sizeof(_GLFWmonitor*) * found);
monitors[found - 1] = createMonitor(&adapter, &display);
}
}
else
{
found++;
monitors = realloc(monitors, sizeof(_GLFWmonitor*) * found);
monitors[found - 1] = createMonitor(&adapter, NULL);
}
}
_GLFW_SWAP_POINTERS(monitors[0], monitors[primaryIndex]);
2012-09-12 19:35:52 +02:00
*count = found;
return monitors;
}
2013-02-20 16:00:53 +01:00
2015-08-23 19:30:04 +02:00
GLFWbool _glfwPlatformIsSameMonitor(_GLFWmonitor* first, _GLFWmonitor* second)
2013-04-22 14:43:13 +02:00
{
if (wcslen(first->win32.displayName))
return wcscmp(first->win32.displayName, second->win32.displayName) == 0;
else
return wcscmp(first->win32.adapterName, second->win32.adapterName) == 0;
2013-04-22 14:43:13 +02:00
}
2013-02-20 16:00:53 +01:00
void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos)
{
DEVMODEW settings;
ZeroMemory(&settings, sizeof(DEVMODEW));
settings.dmSize = sizeof(DEVMODEW);
2013-02-20 16:00:53 +01:00
EnumDisplaySettingsExW(monitor->win32.adapterName,
ENUM_CURRENT_SETTINGS,
&settings,
EDS_ROTATEDMODE);
2013-02-20 16:00:53 +01:00
if (xpos)
*xpos = settings.dmPosition.x;
if (ypos)
*ypos = settings.dmPosition.y;
}
GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count)
2012-09-12 21:04:24 +02:00
{
int modeIndex = 0, size = 0;
2012-09-12 21:04:24 +02:00
GLFWvidmode* result = NULL;
*count = 0;
2012-09-12 21:04:24 +02:00
for (;;)
{
int i;
GLFWvidmode mode;
DEVMODEW dm;
2012-09-12 21:04:24 +02:00
ZeroMemory(&dm, sizeof(DEVMODEW));
dm.dmSize = sizeof(DEVMODEW);
2012-09-12 21:04:24 +02:00
if (!EnumDisplaySettingsW(monitor->win32.adapterName, modeIndex, &dm))
2012-09-12 21:04:24 +02:00
break;
modeIndex++;
// Skip modes with less than 15 BPP
2012-09-12 21:04:24 +02:00
if (dm.dmBitsPerPel < 15)
continue;
2013-05-30 15:52:42 +02:00
mode.width = dm.dmPelsWidth;
2012-09-12 21:04:24 +02:00
mode.height = dm.dmPelsHeight;
2013-05-30 15:52:42 +02:00
mode.refreshRate = dm.dmDisplayFrequency;
2012-09-12 21:04:24 +02:00
_glfwSplitBPP(dm.dmBitsPerPel,
2012-09-12 23:03:15 +02:00
&mode.redBits,
&mode.greenBits,
&mode.blueBits);
2012-09-12 21:04:24 +02:00
for (i = 0; i < *count; i++)
2012-09-12 21:04:24 +02:00
{
if (_glfwCompareVideoModes(result + i, &mode) == 0)
break;
}
// Skip duplicate modes
if (i < *count)
2012-09-12 21:04:24 +02:00
continue;
if (monitor->win32.modesPruned)
{
// Skip modes not supported by the connected displays
if (ChangeDisplaySettingsExW(monitor->win32.adapterName,
&dm,
NULL,
CDS_TEST,
NULL) != DISP_CHANGE_SUCCESSFUL)
{
continue;
}
}
if (*count == size)
2012-09-12 21:04:24 +02:00
{
2015-12-06 15:08:23 +01:00
size += 128;
result = (GLFWvidmode*) realloc(result, size * sizeof(GLFWvidmode));
2012-09-12 21:04:24 +02:00
}
(*count)++;
result[*count - 1] = mode;
2012-09-12 21:04:24 +02:00
}
if (!*count)
{
// HACK: Report the current mode if no valid modes were found
result = calloc(1, sizeof(GLFWvidmode));
2015-12-10 12:16:13 +01:00
_glfwPlatformGetVideoMode(monitor, result);
*count = 1;
}
2012-09-12 21:04:24 +02:00
return result;
}
void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
{
DEVMODEW dm;
2012-09-12 21:04:24 +02:00
ZeroMemory(&dm, sizeof(DEVMODEW));
dm.dmSize = sizeof(DEVMODEW);
2012-09-12 21:04:24 +02:00
EnumDisplaySettingsW(monitor->win32.adapterName, ENUM_CURRENT_SETTINGS, &dm);
2012-09-12 21:04:24 +02:00
mode->width = dm.dmPelsWidth;
mode->height = dm.dmPelsHeight;
2013-05-30 15:52:42 +02:00
mode->refreshRate = dm.dmDisplayFrequency;
2012-09-12 21:04:24 +02:00
_glfwSplitBPP(dm.dmBitsPerPel,
&mode->redBits,
&mode->greenBits,
&mode->blueBits);
}
2014-08-18 12:31:48 +02:00
void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
{
HDC dc;
WORD values[768];
dc = CreateDCW(L"DISPLAY", monitor->win32.adapterName, NULL, NULL);
2014-08-18 12:31:48 +02:00
GetDeviceGammaRamp(dc, values);
DeleteDC(dc);
_glfwAllocGammaArrays(ramp, 256);
memcpy(ramp->red, values + 0, 256 * sizeof(unsigned short));
memcpy(ramp->green, values + 256, 256 * sizeof(unsigned short));
memcpy(ramp->blue, values + 512, 256 * sizeof(unsigned short));
}
void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
{
HDC dc;
WORD values[768];
if (ramp->size != 256)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Win32: Gamma ramp size must be 256");
return;
}
memcpy(values + 0, ramp->red, 256 * sizeof(unsigned short));
memcpy(values + 256, ramp->green, 256 * sizeof(unsigned short));
memcpy(values + 512, ramp->blue, 256 * sizeof(unsigned short));
dc = CreateDCW(L"DISPLAY", monitor->win32.adapterName, NULL, NULL);
2014-08-18 12:31:48 +02:00
SetDeviceGammaRamp(dc, values);
DeleteDC(dc);
}
//////////////////////////////////////////////////////////////////////////
////// GLFW native API //////
//////////////////////////////////////////////////////////////////////////
2014-10-28 15:35:03 +01:00
GLFWAPI const char* glfwGetWin32Adapter(GLFWmonitor* handle)
{
_GLFWmonitor* monitor = (_GLFWmonitor*) handle;
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
return monitor->win32.publicAdapterName;
}
GLFWAPI const char* glfwGetWin32Monitor(GLFWmonitor* handle)
{
_GLFWmonitor* monitor = (_GLFWmonitor*) handle;
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
2014-10-28 15:35:03 +01:00
return monitor->win32.publicDisplayName;
}