glfw/src/win32/win32_init.c

256 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"
// With the Borland C++ compiler, we want to disable FPU exceptions
#ifdef __BORLANDC__
#include <float.h>
#endif // __BORLANDC__
//========================================================================
2010-09-07 17:50:43 +02:00
// Load necessary libraries (DLLs)
2010-09-07 17:34:51 +02:00
//========================================================================
2010-09-10 22:03:36 +02:00
static GLboolean initLibraries(void)
2010-09-07 17:34:51 +02:00
{
// gdi32.dll (OpenGL pixel format functions & SwapBuffers)
#ifndef _GLFW_NO_DLOAD_GDI32
2010-09-10 22:03:36 +02:00
_glfwLibrary.Libs.gdi32 = LoadLibrary("gdi32.dll");
if (_glfwLibrary.Libs.gdi32 != NULL)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
_glfwLibrary.Libs.ChoosePixelFormat = (CHOOSEPIXELFORMAT_T)
GetProcAddress(_glfwLibrary.Libs.gdi32, "ChoosePixelFormat");
2010-09-07 17:34:51 +02:00
_glfwLibrary.Libs.DescribePixelFormat = (DESCRIBEPIXELFORMAT_T)
2010-09-10 22:03:36 +02:00
GetProcAddress(_glfwLibrary.Libs.gdi32, "DescribePixelFormat");
_glfwLibrary.Libs.GetPixelFormat = (GETPIXELFORMAT_T)
GetProcAddress(_glfwLibrary.Libs.gdi32, "GetPixelFormat");
_glfwLibrary.Libs.SetPixelFormat = (SETPIXELFORMAT_T)
GetProcAddress(_glfwLibrary.Libs.gdi32, "SetPixelFormat");
_glfwLibrary.Libs.SwapBuffers = (SWAPBUFFERS_T)
GetProcAddress(_glfwLibrary.Libs.gdi32, "SwapBuffers");
if (_glfwLibrary.Libs.ChoosePixelFormat &&
_glfwLibrary.Libs.DescribePixelFormat &&
_glfwLibrary.Libs.GetPixelFormat &&
_glfwLibrary.Libs.SetPixelFormat &&
_glfwLibrary.Libs.SwapBuffers)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
FreeLibrary(_glfwLibrary.Libs.gdi32);
2010-09-07 17:34:51 +02:00
_glfwLibrary.Libs.gdi32 = NULL;
return GL_FALSE;
}
}
else
return GL_FALSE;
#endif // _GLFW_NO_DLOAD_GDI32
// winmm.dll (for joystick and timer support)
#ifndef _GLFW_NO_DLOAD_WINMM
2010-09-10 22:03:36 +02:00
_glfwLibrary.Libs.winmm = LoadLibrary("winmm.dll");
if (_glfwLibrary.Libs.winmm != NULL)
2010-09-07 17:34:51 +02:00
{
_glfwLibrary.Libs.joyGetDevCapsA = (JOYGETDEVCAPSA_T)
2010-09-10 22:03:36 +02:00
GetProcAddress(_glfwLibrary.Libs.winmm, "joyGetDevCapsA");
_glfwLibrary.Libs.joyGetPos = (JOYGETPOS_T)
GetProcAddress(_glfwLibrary.Libs.winmm, "joyGetPos");
_glfwLibrary.Libs.joyGetPosEx = (JOYGETPOSEX_T)
GetProcAddress(_glfwLibrary.Libs.winmm, "joyGetPosEx");
_glfwLibrary.Libs.timeGetTime = (TIMEGETTIME_T)
GetProcAddress(_glfwLibrary.Libs.winmm, "timeGetTime");
if (_glfwLibrary.Libs.joyGetDevCapsA &&
_glfwLibrary.Libs.joyGetPos &&
_glfwLibrary.Libs.joyGetPosEx &&
_glfwLibrary.Libs.timeGetTime)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
FreeLibrary(_glfwLibrary.Libs.winmm);
2010-09-07 17:34:51 +02:00
_glfwLibrary.Libs.winmm = NULL;
return GL_FALSE;
}
}
else
return GL_FALSE;
#endif // _GLFW_NO_DLOAD_WINMM
return GL_TRUE;
}
//========================================================================
2010-09-07 17:50:43 +02:00
// Unload used libraries (DLLs)
2010-09-07 17:34:51 +02:00
//========================================================================
2010-09-10 22:03:36 +02:00
static void freeLibraries(void)
2010-09-07 17:34:51 +02:00
{
// gdi32.dll
#ifndef _GLFW_NO_DLOAD_GDI32
2010-09-10 22:03:36 +02:00
if (_glfwLibrary.Libs.gdi32 != NULL)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
FreeLibrary(_glfwLibrary.Libs.gdi32);
2010-09-07 17:34:51 +02:00
_glfwLibrary.Libs.gdi32 = NULL;
}
#endif // _GLFW_NO_DLOAD_GDI32
// winmm.dll
#ifndef _GLFW_NO_DLOAD_WINMM
2010-09-10 22:03:36 +02:00
if (_glfwLibrary.Libs.winmm != NULL)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
FreeLibrary(_glfwLibrary.Libs.winmm);
2010-09-07 17:34:51 +02:00
_glfwLibrary.Libs.winmm = NULL;
}
#endif // _GLFW_NO_DLOAD_WINMM
}
//========================================================================
2010-09-07 17:50:43 +02:00
// Terminate GLFW when exiting application
2010-09-07 17:34:51 +02:00
//========================================================================
2010-09-10 22:03:36 +02:00
static void glfw_atexit(void)
2010-09-07 17:34:51 +02:00
{
glfwTerminate();
}
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
// Initialize various GLFW state
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
{
OSVERSIONINFO osi;
// To make SetForegroundWindow() work as we want, we need to fiddle
// with the FOREGROUNDLOCKTIMEOUT system setting (we do this as early
// as possible in the hope of still being the foreground process)
2010-09-10 22:03:36 +02:00
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0,
&_glfwLibrary.Sys.foregroundLockTimeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID) 0,
SPIF_SENDCHANGE);
2010-09-07 17:34:51 +02:00
// Check which OS version we are running
2010-09-10 22:03:36 +02:00
osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osi);
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_UNKNOWN;
2010-09-10 22:03:36 +02:00
if (osi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
if (osi.dwMajorVersion == 4 && osi.dwMinorVersion < 10)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_95;
2010-09-10 22:03:36 +02:00
else if (osi.dwMajorVersion == 4 && osi.dwMinorVersion < 90)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_98;
2010-09-10 22:03:36 +02:00
else if (osi.dwMajorVersion == 4 && osi.dwMinorVersion == 90)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_ME;
2010-09-10 22:03:36 +02:00
else if (osi.dwMajorVersion >= 4)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_UNKNOWN_9x;
}
2010-09-10 22:03:36 +02:00
else if (osi.dwPlatformId == VER_PLATFORM_WIN32_NT)
2010-09-07 17:34:51 +02:00
{
2010-09-10 22:03:36 +02:00
if (osi.dwMajorVersion == 4 && osi.dwMinorVersion == 0)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_NT4;
2010-09-10 22:03:36 +02:00
else if (osi.dwMajorVersion == 5 && osi.dwMinorVersion == 0)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_2K;
2010-09-10 22:03:36 +02:00
else if (osi.dwMajorVersion == 5 && osi.dwMinorVersion == 1)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_XP;
2010-09-10 22:03:36 +02:00
else if (osi.dwMajorVersion == 5 && osi.dwMinorVersion == 2)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_NET_SERVER;
2010-09-10 22:03:36 +02:00
else if (osi.dwMajorVersion >= 5)
2010-09-07 17:34:51 +02:00
_glfwLibrary.Sys.winVer = _GLFW_WIN_UNKNOWN_NT;
}
// Do we have Unicode support?
2010-09-10 22:03:36 +02:00
if (_glfwLibrary.Sys.winVer >= _GLFW_WIN_NT4)
2010-09-07 17:34:51 +02:00
{
// Windows NT/2000/XP/.NET has Unicode support
_glfwLibrary.Sys.hasUnicode = GL_TRUE;
}
else
{
// Windows 9x/ME does not have Unicode support
_glfwLibrary.Sys.hasUnicode = GL_FALSE;
}
// Load libraries (DLLs)
2010-09-10 22:03:36 +02:00
if (!_glfwInitLibraries())
2010-09-07 17:34:51 +02:00
return GL_FALSE;
// With the Borland C++ compiler, we want to disable FPU exceptions
// (this is recommended for OpenGL applications under Windows)
#ifdef __BORLANDC__
2010-09-10 22:03:36 +02:00
_control87(MCW_EM, MCW_EM);
2010-09-07 17:34:51 +02:00
#endif
// Retrieve GLFW instance handle
2010-09-10 22:03:36 +02:00
_glfwLibrary.instance = GetModuleHandle(NULL);
2010-09-07 17:34:51 +02:00
// System keys are not disabled
_glfwWin.keyboardHook = NULL;
// Install atexit() routine
2010-09-10 22:03:36 +02:00
atexit(glfw_atexit);
2010-09-07 17:34:51 +02:00
// Start the timer
_glfwInitTimer();
return GL_TRUE;
}
//========================================================================
// Close window and shut down library
//========================================================================
2010-09-10 22:03:36 +02:00
int _glfwPlatformTerminate(void)
2010-09-07 17:34:51 +02:00
{
// Close OpenGL window
glfwCloseWindow();
// Enable system keys again (if they were disabled)
2010-09-10 22:03:36 +02:00
glfwEnable(GLFW_SYSTEM_KEYS);
2010-09-07 17:34:51 +02:00
// Unload libraries (DLLs)
_glfwFreeLibraries();
// Restore FOREGROUNDLOCKTIMEOUT system setting
2010-09-10 22:03:36 +02:00
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
(LPVOID) _glfwLibrary.Sys.foregroundLockTimeout,
SPIF_SENDCHANGE);
2010-09-07 17:34:51 +02:00
return GL_TRUE;
}