glfw/src/win32_monitor.c

280 lines
7.5 KiB
C
Raw Normal View History

//========================================================================
// GLFW - An OpenGL library
// Platform: X11 (Unix)
// API version: 3.0
// 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"
#include <stdlib.h>
#include <string.h>
2012-09-12 19:04:24 +00:00
#include <limits.h>
#include <malloc.h>
2012-09-12 18:46:54 +00:00
// These constants are missing on MinGW
#ifndef EDS_ROTATEDMODE
2012-09-12 18:46:54 +00:00
#define EDS_ROTATEDMODE 0x00000004
#endif
2012-01-31 22:47:01 +00:00
#ifndef DISPLAY_DEVICE_ACTIVE
2012-09-12 18:46:54 +00:00
#define DISPLAY_DEVICE_ACTIVE 0x00000001
2012-01-31 22:47:01 +00:00
#endif
2012-09-12 19:04:24 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
// Change the current video mode
2013-02-04 12:22:10 +00:00
//
int _glfwSetVideoMode(_GLFWmonitor* monitor, const GLFWvidmode* mode)
2012-09-12 19:04:24 +00:00
{
GLFWvidmode current;
const GLFWvidmode* best;
DEVMODE dm;
2012-09-12 19:04:24 +00:00
best = _glfwChooseVideoMode(monitor, mode);
2012-09-12 19:04:24 +00:00
_glfwPlatformGetVideoMode(monitor, &current);
if (_glfwCompareVideoModes(&current, best) == 0)
return GL_TRUE;
2012-09-12 19:04:24 +00:00
dm.dmSize = sizeof(DEVMODE);
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
dm.dmPelsWidth = best->width;
dm.dmPelsHeight = best->height;
dm.dmBitsPerPel = best->redBits + best->greenBits + best->blueBits;
if (dm.dmBitsPerPel < 15 || dm.dmBitsPerPel >= 24)
dm.dmBitsPerPel = 32;
if (ChangeDisplaySettingsEx(monitor->win32.name,
&dm,
NULL,
CDS_FULLSCREEN,
NULL) != DISP_CHANGE_SUCCESSFUL)
2012-09-12 19:04:24 +00:00
{
_glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to set video mode");
return GL_FALSE;
2012-09-12 19:04:24 +00:00
}
return GL_TRUE;
2012-09-12 19:04:24 +00:00
}
// Restore the previously saved (original) video mode
2013-02-04 12:22:10 +00:00
//
void _glfwRestoreVideoMode(_GLFWmonitor* monitor)
2012-09-12 19:04:24 +00:00
{
ChangeDisplaySettingsEx(monitor->win32.name,
NULL, NULL, CDS_FULLSCREEN, NULL);
2012-09-12 19:04:24 +00:00
}
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2012-09-12 17:35:52 +00:00
_GLFWmonitor** _glfwPlatformGetMonitors(int* count)
{
2012-09-12 17:35:52 +00:00
int size = 0, found = 0;
_GLFWmonitor** monitors = NULL;
DWORD adapterIndex = 0;
int primaryIndex = 0;
2012-09-12 17:35:52 +00:00
for (;;)
{
2012-09-12 18:54:12 +00:00
// Enumerate display adapters
2013-02-12 12:43:47 +00:00
DISPLAY_DEVICE adapter, display;
char* name;
2012-09-12 20:51:55 +00:00
HDC dc;
2012-09-12 17:35:52 +00:00
ZeroMemory(&adapter, sizeof(DISPLAY_DEVICE));
adapter.cb = sizeof(DISPLAY_DEVICE);
2012-09-12 17:35:52 +00:00
if (!EnumDisplayDevices(NULL, adapterIndex, &adapter, 0))
break;
2012-09-12 17:35:52 +00:00
adapterIndex++;
2012-09-12 17:35:52 +00:00
if ((adapter.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) ||
!(adapter.StateFlags & DISPLAY_DEVICE_ACTIVE))
{
continue;
}
2012-09-12 20:51:55 +00:00
if (found == size)
{
if (size)
size *= 2;
else
size = 4;
2012-09-12 17:35:52 +00:00
2012-09-12 20:51:55 +00:00
monitors = (_GLFWmonitor**) realloc(monitors, sizeof(_GLFWmonitor*) * size);
}
2012-09-12 17:35:52 +00:00
2013-02-12 12:43:47 +00:00
ZeroMemory(&display, sizeof(DISPLAY_DEVICE));
display.cb = sizeof(DISPLAY_DEVICE);
2012-09-12 17:35:52 +00:00
2013-02-12 12:43:47 +00:00
EnumDisplayDevices(adapter.DeviceName, 0, &display, 0);
dc = CreateDC(L"DISPLAY", display.DeviceString, NULL, NULL);
2012-09-12 17:35:52 +00:00
if (adapter.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
primaryIndex = found;
2012-09-13 15:46:40 +00:00
2013-02-12 12:43:47 +00:00
name = _glfwCreateUTF8FromWideString(display.DeviceString);
if (!name)
{
// TODO: wat
return NULL;
}
monitors[found] = _glfwCreateMonitor(name,
2012-09-12 20:51:55 +00:00
GetDeviceCaps(dc, HORZSIZE),
2013-02-20 15:00:53 +00:00
GetDeviceCaps(dc, VERTSIZE));
2012-09-12 17:35:52 +00:00
free(name);
2012-09-12 20:51:55 +00:00
DeleteDC(dc);
2012-09-12 17:35:52 +00:00
2012-09-12 20:51:55 +00:00
if (!monitors[found])
{
// TODO: wat
return NULL;
2012-09-12 17:35:52 +00:00
}
2012-09-12 20:51:55 +00:00
wcscpy(monitors[found]->win32.name, adapter.DeviceName);
2012-09-12 20:51:55 +00:00
found++;
2012-09-12 17:35:52 +00:00
}
2012-07-06 11:55:54 +00:00
if (primaryIndex > 0)
{
_GLFWmonitor* temp = monitors[0];
monitors[0] = monitors[primaryIndex];
monitors[primaryIndex] = temp;
}
2012-09-12 17:35:52 +00:00
*count = found;
return monitors;
}
2013-02-20 15:00:53 +00:00
void _glfwPlatformGetMonitorPos(_GLFWmonitor* monitor, int* xpos, int* ypos)
{
DEVMODE settings;
ZeroMemory(&settings, sizeof(DEVMODE));
settings.dmSize = sizeof(DEVMODE);
EnumDisplaySettingsEx(monitor->win32.name,
ENUM_CURRENT_SETTINGS,
&settings,
EDS_ROTATEDMODE);
if (xpos)
*xpos = settings.dmPosition.x;
if (ypos)
*ypos = settings.dmPosition.y;
}
2012-09-12 19:04:24 +00:00
GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found)
{
int modeIndex = 0, count = 0;
GLFWvidmode* result = NULL;
*found = 0;
for (;;)
{
int i;
GLFWvidmode mode;
DEVMODE dm;
ZeroMemory(&dm, sizeof(DEVMODE));
dm.dmSize = sizeof(DEVMODE);
if (!EnumDisplaySettings(monitor->win32.name, modeIndex, &dm))
2012-09-12 19:04:24 +00:00
break;
modeIndex++;
if (dm.dmBitsPerPel < 15)
{
// Skip modes with less than 15 BPP
continue;
}
mode.width = dm.dmPelsWidth;
mode.height = dm.dmPelsHeight;
_glfwSplitBPP(dm.dmBitsPerPel,
2012-09-12 21:03:15 +00:00
&mode.redBits,
&mode.greenBits,
&mode.blueBits);
2012-09-12 19:04:24 +00:00
for (i = 0; i < *found; i++)
{
if (_glfwCompareVideoModes(result + i, &mode) == 0)
break;
}
if (i < *found)
{
// This is a duplicate, so skip it
continue;
}
if (*found == count)
{
if (count)
count *= 2;
else
count = 128;
2013-03-11 20:30:01 +00:00
result = (GLFWvidmode*) realloc(result, count * sizeof(GLFWvidmode));
2012-09-12 19:04:24 +00:00
}
result[*found] = mode;
(*found)++;
}
return result;
}
void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
{
DEVMODE dm;
ZeroMemory(&dm, sizeof(DEVMODE));
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(monitor->win32.name, ENUM_CURRENT_SETTINGS, &dm);
2012-09-12 19:04:24 +00:00
mode->width = dm.dmPelsWidth;
mode->height = dm.dmPelsHeight;
_glfwSplitBPP(dm.dmBitsPerPel,
&mode->redBits,
&mode->greenBits,
&mode->blueBits);
}