2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
2014-01-22 01:32:00 +01:00
|
|
|
// GLFW 3.1 Win32 - www.glfw.org
|
2010-09-07 17:34:51 +02:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// 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-10-13 04:04:43 +02:00
|
|
|
#include <stdlib.h>
|
2012-08-14 13:51:39 +02:00
|
|
|
#include <malloc.h>
|
2010-10-13 04:04:43 +02:00
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
#ifdef __BORLANDC__
|
2010-09-13 23:50:04 +02:00
|
|
|
// With the Borland C++ compiler, we want to disable FPU exceptions
|
2010-09-07 17:34:51 +02:00
|
|
|
#include <float.h>
|
|
|
|
#endif // __BORLANDC__
|
|
|
|
|
|
|
|
|
2013-08-07 18:07:33 +02:00
|
|
|
#if defined(_GLFW_USE_OPTIMUS_HPG)
|
|
|
|
|
|
|
|
// Applications exporting this symbol with this value will be automatically
|
|
|
|
// directed to the high-performance GPU on nVidia Optimus systems
|
|
|
|
//
|
|
|
|
GLFWAPI DWORD NvOptimusEnablement = 0x00000001;
|
|
|
|
|
|
|
|
#endif // _GLFW_USE_OPTIMUS_HPG
|
|
|
|
|
2012-09-12 21:23:04 +02:00
|
|
|
#if defined(_GLFW_BUILD_DLL)
|
2013-02-04 13:22:10 +01:00
|
|
|
|
|
|
|
// GLFW DLL entry point
|
|
|
|
//
|
2012-09-12 21:23:04 +02:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-02-04 13:22:10 +01:00
|
|
|
#endif // _GLFW_BUILD_DLL
|
2012-09-12 21:23:04 +02:00
|
|
|
|
2010-09-07 17:50:43 +02:00
|
|
|
// Load necessary libraries (DLLs)
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2010-09-10 22:03:36 +02:00
|
|
|
static GLboolean initLibraries(void)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2014-03-06 18:30:14 +01:00
|
|
|
_glfw.win32.winmm.instance = LoadLibraryW(L"winmm.dll");
|
2013-01-02 01:40:42 +01:00
|
|
|
if (!_glfw.win32.winmm.instance)
|
2014-01-29 15:14:49 +01:00
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Win32: Failed to load winmm.dll");
|
2010-09-14 04:27:02 +02:00
|
|
|
return GL_FALSE;
|
2014-01-29 15:14:49 +01:00
|
|
|
}
|
2010-09-14 04:27:02 +02:00
|
|
|
|
2013-01-02 01:40:42 +01:00
|
|
|
_glfw.win32.winmm.joyGetDevCaps = (JOYGETDEVCAPS_T)
|
|
|
|
GetProcAddress(_glfw.win32.winmm.instance, "joyGetDevCapsW");
|
|
|
|
_glfw.win32.winmm.joyGetPos = (JOYGETPOS_T)
|
|
|
|
GetProcAddress(_glfw.win32.winmm.instance, "joyGetPos");
|
|
|
|
_glfw.win32.winmm.joyGetPosEx = (JOYGETPOSEX_T)
|
|
|
|
GetProcAddress(_glfw.win32.winmm.instance, "joyGetPosEx");
|
|
|
|
_glfw.win32.winmm.timeGetTime = (TIMEGETTIME_T)
|
|
|
|
GetProcAddress(_glfw.win32.winmm.instance, "timeGetTime");
|
|
|
|
|
|
|
|
if (!_glfw.win32.winmm.joyGetDevCaps ||
|
|
|
|
!_glfw.win32.winmm.joyGetPos ||
|
|
|
|
!_glfw.win32.winmm.joyGetPosEx ||
|
|
|
|
!_glfw.win32.winmm.timeGetTime)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2014-01-29 15:14:49 +01:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Win32: Failed to load winmm functions");
|
2010-09-07 17:34:51 +02:00
|
|
|
return GL_FALSE;
|
2010-09-14 04:27:02 +02:00
|
|
|
}
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2014-03-06 18:30:14 +01:00
|
|
|
_glfw.win32.user32.instance = LoadLibraryW(L"user32.dll");
|
2013-03-10 19:23:42 +01:00
|
|
|
if (_glfw.win32.user32.instance)
|
|
|
|
{
|
|
|
|
_glfw.win32.user32.SetProcessDPIAware = (SETPROCESSDPIAWARE_T)
|
|
|
|
GetProcAddress(_glfw.win32.user32.instance, "SetProcessDPIAware");
|
2014-02-10 15:03:23 +01:00
|
|
|
_glfw.win32.user32.ChangeWindowMessageFilterEx = (CHANGEWINDOWMESSAGEFILTEREX_T)
|
|
|
|
GetProcAddress(_glfw.win32.user32.instance, "ChangeWindowMessageFilterEx");
|
2013-03-10 19:23:42 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 18:30:14 +01:00
|
|
|
_glfw.win32.dwmapi.instance = LoadLibraryW(L"dwmapi.dll");
|
2013-03-11 20:54:32 +01:00
|
|
|
if (_glfw.win32.dwmapi.instance)
|
|
|
|
{
|
|
|
|
_glfw.win32.dwmapi.DwmIsCompositionEnabled = (DWMISCOMPOSITIONENABLED_T)
|
|
|
|
GetProcAddress(_glfw.win32.dwmapi.instance, "DwmIsCompositionEnabled");
|
|
|
|
}
|
2013-12-22 16:38:56 +01:00
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-09-07 17:50:43 +02:00
|
|
|
// Unload used libraries (DLLs)
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2013-06-24 14:38:00 +02:00
|
|
|
static void terminateLibraries(void)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2014-08-15 14:50:41 +02:00
|
|
|
if (_glfw.win32.winmm.instance)
|
2013-01-02 01:40:42 +01:00
|
|
|
FreeLibrary(_glfw.win32.winmm.instance);
|
2013-06-26 14:04:07 +02:00
|
|
|
|
|
|
|
if (_glfw.win32.user32.instance)
|
|
|
|
FreeLibrary(_glfw.win32.user32.instance);
|
|
|
|
|
|
|
|
if (_glfw.win32.dwmapi.instance)
|
|
|
|
FreeLibrary(_glfw.win32.dwmapi.instance);
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-05 02:07:50 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW internal API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-03-11 20:54:32 +01:00
|
|
|
// Returns whether desktop compositing is enabled
|
|
|
|
//
|
|
|
|
BOOL _glfwIsCompositionEnabled(void)
|
|
|
|
{
|
|
|
|
BOOL enabled;
|
|
|
|
|
|
|
|
if (!_glfw_DwmIsCompositionEnabled)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (_glfw_DwmIsCompositionEnabled(&enabled) != S_OK)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2012-02-05 02:07:50 +01:00
|
|
|
// Returns a wide string version of the specified UTF-8 string
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2012-02-05 02:07:50 +01:00
|
|
|
WCHAR* _glfwCreateWideStringFromUTF8(const char* source)
|
|
|
|
{
|
|
|
|
WCHAR* target;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
length = MultiByteToWideChar(CP_UTF8, 0, source, -1, NULL, 0);
|
|
|
|
if (!length)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-11 21:02:57 +02:00
|
|
|
target = calloc(length, sizeof(WCHAR));
|
2012-02-05 02:07:50 +01:00
|
|
|
|
2014-10-11 21:02:57 +02:00
|
|
|
if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, length))
|
2012-02-05 02:07:50 +01:00
|
|
|
{
|
2012-02-07 15:40:47 +01:00
|
|
|
free(target);
|
2012-02-05 02:07:50 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a UTF-8 string version of the specified wide string
|
2013-02-04 13:22:10 +01:00
|
|
|
//
|
2012-02-05 02:07:50 +01:00
|
|
|
char* _glfwCreateUTF8FromWideString(const WCHAR* source)
|
|
|
|
{
|
|
|
|
char* target;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
length = WideCharToMultiByte(CP_UTF8, 0, source, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (!length)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-11 21:02:57 +02:00
|
|
|
target = calloc(length, sizeof(char));
|
2012-02-05 02:07:50 +01:00
|
|
|
|
2014-10-11 21:02:57 +02:00
|
|
|
if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, length, NULL, NULL))
|
2012-02-05 02:07:50 +01:00
|
|
|
{
|
2012-02-07 15:40:47 +01:00
|
|
|
free(target);
|
2012-02-05 02:07:50 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-10 22:03:36 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW platform API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-10 22:03:36 +02:00
|
|
|
int _glfwPlatformInit(void)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2010-09-13 23:50:04 +02:00
|
|
|
// To make SetForegroundWindow work as we want, we need to fiddle
|
2010-09-07 17:34:51 +02:00
|
|
|
// with the FOREGROUNDLOCKTIMEOUT system setting (we do this as early
|
|
|
|
// as possible in the hope of still being the foreground process)
|
2014-03-06 18:30:14 +01:00
|
|
|
SystemParametersInfoW(SPI_GETFOREGROUNDLOCKTIMEOUT, 0,
|
|
|
|
&_glfw.win32.foregroundLockTimeout, 0);
|
|
|
|
SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, UIntToPtr(0),
|
|
|
|
SPIF_SENDCHANGE);
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-14 01:05:03 +02:00
|
|
|
if (!initLibraries())
|
2010-09-07 17:34:51 +02:00
|
|
|
return GL_FALSE;
|
|
|
|
|
2013-03-10 19:23:42 +01:00
|
|
|
if (_glfw_SetProcessDPIAware)
|
|
|
|
_glfw_SetProcessDPIAware();
|
|
|
|
|
2010-09-13 23:50:04 +02:00
|
|
|
#ifdef __BORLANDC__
|
2010-09-07 17:34:51 +02:00
|
|
|
// With the Borland C++ compiler, we want to disable FPU exceptions
|
|
|
|
// (this is recommended for OpenGL applications under Windows)
|
2010-09-10 22:03:36 +02:00
|
|
|
_control87(MCW_EM, MCW_EM);
|
2010-09-07 17:34:51 +02:00
|
|
|
#endif
|
|
|
|
|
2014-08-31 14:17:31 +02:00
|
|
|
if (!_glfwRegisterWindowClass())
|
|
|
|
return GL_FALSE;
|
|
|
|
|
2013-02-04 08:26:17 +01:00
|
|
|
if (!_glfwInitContextAPI())
|
2013-01-16 20:10:17 +01:00
|
|
|
return GL_FALSE;
|
2013-12-22 16:38:56 +01:00
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
_glfwInitTimer();
|
2013-01-02 01:52:28 +01:00
|
|
|
_glfwInitJoysticks();
|
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-12-30 01:15:48 +01:00
|
|
|
void _glfwPlatformTerminate(void)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2014-08-31 14:17:31 +02:00
|
|
|
_glfwUnregisterWindowClass();
|
2010-09-14 02:17:18 +02:00
|
|
|
|
2013-06-24 14:38:00 +02:00
|
|
|
// Restore previous foreground lock timeout system setting
|
2014-03-06 18:30:14 +01:00
|
|
|
SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
|
|
|
|
UIntToPtr(_glfw.win32.foregroundLockTimeout),
|
|
|
|
SPIF_SENDCHANGE);
|
2013-06-24 14:38:00 +02:00
|
|
|
|
|
|
|
free(_glfw.win32.clipboardString);
|
|
|
|
|
|
|
|
_glfwTerminateJoysticks();
|
|
|
|
_glfwTerminateContextAPI();
|
|
|
|
terminateLibraries();
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
|
2010-09-14 01:05:03 +02:00
|
|
|
const char* _glfwPlatformGetVersionString(void)
|
|
|
|
{
|
2013-11-20 13:58:57 +01:00
|
|
|
const char* version = _GLFW_VERSION_NUMBER " Win32"
|
2012-11-27 15:02:26 +01:00
|
|
|
#if defined(_GLFW_WGL)
|
|
|
|
" WGL"
|
|
|
|
#elif defined(_GLFW_EGL)
|
|
|
|
" EGL"
|
|
|
|
#endif
|
2010-09-14 01:05:03 +02:00
|
|
|
#if defined(__MINGW32__)
|
|
|
|
" MinGW"
|
|
|
|
#elif defined(_MSC_VER)
|
2013-12-24 00:48:28 +01:00
|
|
|
" VisualC"
|
2010-09-14 01:05:03 +02:00
|
|
|
#elif defined(__BORLANDC__)
|
2013-02-14 13:13:07 +01:00
|
|
|
" BorlandC"
|
|
|
|
#endif
|
2012-03-25 17:40:30 +02:00
|
|
|
#if defined(_GLFW_BUILD_DLL)
|
2010-09-14 01:05:03 +02:00
|
|
|
" DLL"
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|