00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "internal.h"
00029
00030 #include <stdlib.h>
00031 #include <malloc.h>
00032
00033
00034 #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG)
00035
00036
00037
00038
00039
00040 __declspec(dllexport) DWORD NvOptimusEnablement = 1;
00041
00042
00043
00044
00045
00046 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
00047
00048 #endif // _GLFW_USE_HYBRID_HPG
00049
00050 #if defined(_GLFW_BUILD_DLL)
00051
00052
00053
00054 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
00055 {
00056 return TRUE;
00057 }
00058
00059 #endif // _GLFW_BUILD_DLL
00060
00061
00062
00063 static GLboolean initLibraries(void)
00064 {
00065 _glfw.win32.winmm.instance = LoadLibraryW(L"winmm.dll");
00066 if (!_glfw.win32.winmm.instance)
00067 {
00068 _glfwInputError(GLFW_PLATFORM_ERROR,
00069 "Win32: Failed to load winmm.dll");
00070 return GL_FALSE;
00071 }
00072
00073 _glfw.win32.winmm.joyGetDevCaps = (JOYGETDEVCAPS_T)
00074 GetProcAddress(_glfw.win32.winmm.instance, "joyGetDevCapsW");
00075 _glfw.win32.winmm.joyGetPos = (JOYGETPOS_T)
00076 GetProcAddress(_glfw.win32.winmm.instance, "joyGetPos");
00077 _glfw.win32.winmm.joyGetPosEx = (JOYGETPOSEX_T)
00078 GetProcAddress(_glfw.win32.winmm.instance, "joyGetPosEx");
00079 _glfw.win32.winmm.timeGetTime = (TIMEGETTIME_T)
00080 GetProcAddress(_glfw.win32.winmm.instance, "timeGetTime");
00081
00082 if (!_glfw.win32.winmm.joyGetDevCaps ||
00083 !_glfw.win32.winmm.joyGetPos ||
00084 !_glfw.win32.winmm.joyGetPosEx ||
00085 !_glfw.win32.winmm.timeGetTime)
00086 {
00087 _glfwInputError(GLFW_PLATFORM_ERROR,
00088 "Win32: Failed to load winmm functions");
00089 return GL_FALSE;
00090 }
00091
00092 _glfw.win32.user32.instance = LoadLibraryW(L"user32.dll");
00093 if (_glfw.win32.user32.instance)
00094 {
00095 _glfw.win32.user32.SetProcessDPIAware = (SETPROCESSDPIAWARE_T)
00096 GetProcAddress(_glfw.win32.user32.instance, "SetProcessDPIAware");
00097 _glfw.win32.user32.ChangeWindowMessageFilterEx = (CHANGEWINDOWMESSAGEFILTEREX_T)
00098 GetProcAddress(_glfw.win32.user32.instance, "ChangeWindowMessageFilterEx");
00099 }
00100
00101 _glfw.win32.dwmapi.instance = LoadLibraryW(L"dwmapi.dll");
00102 if (_glfw.win32.dwmapi.instance)
00103 {
00104 _glfw.win32.dwmapi.DwmIsCompositionEnabled = (DWMISCOMPOSITIONENABLED_T)
00105 GetProcAddress(_glfw.win32.dwmapi.instance, "DwmIsCompositionEnabled");
00106 _glfw.win32.dwmapi.DwmFlush = (DWMFLUSH_T)
00107 GetProcAddress(_glfw.win32.dwmapi.instance, "DwmFlush");
00108 }
00109
00110 return GL_TRUE;
00111 }
00112
00113
00114
00115 static void terminateLibraries(void)
00116 {
00117 if (_glfw.win32.winmm.instance)
00118 FreeLibrary(_glfw.win32.winmm.instance);
00119
00120 if (_glfw.win32.user32.instance)
00121 FreeLibrary(_glfw.win32.user32.instance);
00122
00123 if (_glfw.win32.dwmapi.instance)
00124 FreeLibrary(_glfw.win32.dwmapi.instance);
00125 }
00126
00127
00128
00129 static void createKeyTables(void)
00130 {
00131 memset(_glfw.win32.publicKeys, -1, sizeof(_glfw.win32.publicKeys));
00132
00133 _glfw.win32.publicKeys[0x00B] = GLFW_KEY_0;
00134 _glfw.win32.publicKeys[0x002] = GLFW_KEY_1;
00135 _glfw.win32.publicKeys[0x003] = GLFW_KEY_2;
00136 _glfw.win32.publicKeys[0x004] = GLFW_KEY_3;
00137 _glfw.win32.publicKeys[0x005] = GLFW_KEY_4;
00138 _glfw.win32.publicKeys[0x006] = GLFW_KEY_5;
00139 _glfw.win32.publicKeys[0x007] = GLFW_KEY_6;
00140 _glfw.win32.publicKeys[0x008] = GLFW_KEY_7;
00141 _glfw.win32.publicKeys[0x009] = GLFW_KEY_8;
00142 _glfw.win32.publicKeys[0x00A] = GLFW_KEY_9;
00143 _glfw.win32.publicKeys[0x01E] = GLFW_KEY_A;
00144 _glfw.win32.publicKeys[0x030] = GLFW_KEY_B;
00145 _glfw.win32.publicKeys[0x02E] = GLFW_KEY_C;
00146 _glfw.win32.publicKeys[0x020] = GLFW_KEY_D;
00147 _glfw.win32.publicKeys[0x012] = GLFW_KEY_E;
00148 _glfw.win32.publicKeys[0x021] = GLFW_KEY_F;
00149 _glfw.win32.publicKeys[0x022] = GLFW_KEY_G;
00150 _glfw.win32.publicKeys[0x023] = GLFW_KEY_H;
00151 _glfw.win32.publicKeys[0x017] = GLFW_KEY_I;
00152 _glfw.win32.publicKeys[0x024] = GLFW_KEY_J;
00153 _glfw.win32.publicKeys[0x025] = GLFW_KEY_K;
00154 _glfw.win32.publicKeys[0x026] = GLFW_KEY_L;
00155 _glfw.win32.publicKeys[0x032] = GLFW_KEY_M;
00156 _glfw.win32.publicKeys[0x031] = GLFW_KEY_N;
00157 _glfw.win32.publicKeys[0x018] = GLFW_KEY_O;
00158 _glfw.win32.publicKeys[0x019] = GLFW_KEY_P;
00159 _glfw.win32.publicKeys[0x010] = GLFW_KEY_Q;
00160 _glfw.win32.publicKeys[0x013] = GLFW_KEY_R;
00161 _glfw.win32.publicKeys[0x01F] = GLFW_KEY_S;
00162 _glfw.win32.publicKeys[0x014] = GLFW_KEY_T;
00163 _glfw.win32.publicKeys[0x016] = GLFW_KEY_U;
00164 _glfw.win32.publicKeys[0x02F] = GLFW_KEY_V;
00165 _glfw.win32.publicKeys[0x011] = GLFW_KEY_W;
00166 _glfw.win32.publicKeys[0x02D] = GLFW_KEY_X;
00167 _glfw.win32.publicKeys[0x015] = GLFW_KEY_Y;
00168 _glfw.win32.publicKeys[0x02C] = GLFW_KEY_Z;
00169
00170 _glfw.win32.publicKeys[0x028] = GLFW_KEY_APOSTROPHE;
00171 _glfw.win32.publicKeys[0x02B] = GLFW_KEY_BACKSLASH;
00172 _glfw.win32.publicKeys[0x033] = GLFW_KEY_COMMA;
00173 _glfw.win32.publicKeys[0x00D] = GLFW_KEY_EQUAL;
00174 _glfw.win32.publicKeys[0x029] = GLFW_KEY_GRAVE_ACCENT;
00175 _glfw.win32.publicKeys[0x01A] = GLFW_KEY_LEFT_BRACKET;
00176 _glfw.win32.publicKeys[0x00C] = GLFW_KEY_MINUS;
00177 _glfw.win32.publicKeys[0x034] = GLFW_KEY_PERIOD;
00178 _glfw.win32.publicKeys[0x01B] = GLFW_KEY_RIGHT_BRACKET;
00179 _glfw.win32.publicKeys[0x027] = GLFW_KEY_SEMICOLON;
00180 _glfw.win32.publicKeys[0x035] = GLFW_KEY_SLASH;
00181 _glfw.win32.publicKeys[0x056] = GLFW_KEY_WORLD_2;
00182
00183 _glfw.win32.publicKeys[0x00E] = GLFW_KEY_BACKSPACE;
00184 _glfw.win32.publicKeys[0x153] = GLFW_KEY_DELETE;
00185 _glfw.win32.publicKeys[0x14F] = GLFW_KEY_END;
00186 _glfw.win32.publicKeys[0x01C] = GLFW_KEY_ENTER;
00187 _glfw.win32.publicKeys[0x001] = GLFW_KEY_ESCAPE;
00188 _glfw.win32.publicKeys[0x147] = GLFW_KEY_HOME;
00189 _glfw.win32.publicKeys[0x152] = GLFW_KEY_INSERT;
00190 _glfw.win32.publicKeys[0x15D] = GLFW_KEY_MENU;
00191 _glfw.win32.publicKeys[0x151] = GLFW_KEY_PAGE_DOWN;
00192 _glfw.win32.publicKeys[0x149] = GLFW_KEY_PAGE_UP;
00193 _glfw.win32.publicKeys[0x045] = GLFW_KEY_PAUSE;
00194 _glfw.win32.publicKeys[0x039] = GLFW_KEY_SPACE;
00195 _glfw.win32.publicKeys[0x00F] = GLFW_KEY_TAB;
00196 _glfw.win32.publicKeys[0x03A] = GLFW_KEY_CAPS_LOCK;
00197 _glfw.win32.publicKeys[0x145] = GLFW_KEY_NUM_LOCK;
00198 _glfw.win32.publicKeys[0x046] = GLFW_KEY_SCROLL_LOCK;
00199 _glfw.win32.publicKeys[0x03B] = GLFW_KEY_F1;
00200 _glfw.win32.publicKeys[0x03C] = GLFW_KEY_F2;
00201 _glfw.win32.publicKeys[0x03D] = GLFW_KEY_F3;
00202 _glfw.win32.publicKeys[0x03E] = GLFW_KEY_F4;
00203 _glfw.win32.publicKeys[0x03F] = GLFW_KEY_F5;
00204 _glfw.win32.publicKeys[0x040] = GLFW_KEY_F6;
00205 _glfw.win32.publicKeys[0x041] = GLFW_KEY_F7;
00206 _glfw.win32.publicKeys[0x042] = GLFW_KEY_F8;
00207 _glfw.win32.publicKeys[0x043] = GLFW_KEY_F9;
00208 _glfw.win32.publicKeys[0x044] = GLFW_KEY_F10;
00209 _glfw.win32.publicKeys[0x057] = GLFW_KEY_F11;
00210 _glfw.win32.publicKeys[0x058] = GLFW_KEY_F12;
00211 _glfw.win32.publicKeys[0x064] = GLFW_KEY_F13;
00212 _glfw.win32.publicKeys[0x065] = GLFW_KEY_F14;
00213 _glfw.win32.publicKeys[0x066] = GLFW_KEY_F15;
00214 _glfw.win32.publicKeys[0x067] = GLFW_KEY_F16;
00215 _glfw.win32.publicKeys[0x068] = GLFW_KEY_F17;
00216 _glfw.win32.publicKeys[0x069] = GLFW_KEY_F18;
00217 _glfw.win32.publicKeys[0x06A] = GLFW_KEY_F19;
00218 _glfw.win32.publicKeys[0x06B] = GLFW_KEY_F20;
00219 _glfw.win32.publicKeys[0x06C] = GLFW_KEY_F21;
00220 _glfw.win32.publicKeys[0x06D] = GLFW_KEY_F22;
00221 _glfw.win32.publicKeys[0x06E] = GLFW_KEY_F23;
00222 _glfw.win32.publicKeys[0x076] = GLFW_KEY_F24;
00223 _glfw.win32.publicKeys[0x038] = GLFW_KEY_LEFT_ALT;
00224 _glfw.win32.publicKeys[0x01D] = GLFW_KEY_LEFT_CONTROL;
00225 _glfw.win32.publicKeys[0x02A] = GLFW_KEY_LEFT_SHIFT;
00226 _glfw.win32.publicKeys[0x15B] = GLFW_KEY_LEFT_SUPER;
00227 _glfw.win32.publicKeys[0x137] = GLFW_KEY_PRINT_SCREEN;
00228 _glfw.win32.publicKeys[0x138] = GLFW_KEY_RIGHT_ALT;
00229 _glfw.win32.publicKeys[0x11D] = GLFW_KEY_RIGHT_CONTROL;
00230 _glfw.win32.publicKeys[0x036] = GLFW_KEY_RIGHT_SHIFT;
00231 _glfw.win32.publicKeys[0x15C] = GLFW_KEY_RIGHT_SUPER;
00232 _glfw.win32.publicKeys[0x150] = GLFW_KEY_DOWN;
00233 _glfw.win32.publicKeys[0x14B] = GLFW_KEY_LEFT;
00234 _glfw.win32.publicKeys[0x14D] = GLFW_KEY_RIGHT;
00235 _glfw.win32.publicKeys[0x148] = GLFW_KEY_UP;
00236
00237 _glfw.win32.publicKeys[0x052] = GLFW_KEY_KP_0;
00238 _glfw.win32.publicKeys[0x04F] = GLFW_KEY_KP_1;
00239 _glfw.win32.publicKeys[0x050] = GLFW_KEY_KP_2;
00240 _glfw.win32.publicKeys[0x051] = GLFW_KEY_KP_3;
00241 _glfw.win32.publicKeys[0x04B] = GLFW_KEY_KP_4;
00242 _glfw.win32.publicKeys[0x04C] = GLFW_KEY_KP_5;
00243 _glfw.win32.publicKeys[0x04D] = GLFW_KEY_KP_6;
00244 _glfw.win32.publicKeys[0x047] = GLFW_KEY_KP_7;
00245 _glfw.win32.publicKeys[0x048] = GLFW_KEY_KP_8;
00246 _glfw.win32.publicKeys[0x049] = GLFW_KEY_KP_9;
00247 _glfw.win32.publicKeys[0x04E] = GLFW_KEY_KP_ADD;
00248 _glfw.win32.publicKeys[0x053] = GLFW_KEY_KP_DECIMAL;
00249 _glfw.win32.publicKeys[0x135] = GLFW_KEY_KP_DIVIDE;
00250 _glfw.win32.publicKeys[0x11C] = GLFW_KEY_KP_ENTER;
00251 _glfw.win32.publicKeys[0x037] = GLFW_KEY_KP_MULTIPLY;
00252 _glfw.win32.publicKeys[0x04A] = GLFW_KEY_KP_SUBTRACT;
00253 }
00254
00255
00259
00260
00261
00262 BOOL _glfwIsCompositionEnabled(void)
00263 {
00264 BOOL enabled;
00265
00266 if (!_glfw_DwmIsCompositionEnabled)
00267 return FALSE;
00268
00269 if (_glfw_DwmIsCompositionEnabled(&enabled) != S_OK)
00270 return FALSE;
00271
00272 return enabled;
00273 }
00274
00275
00276
00277 WCHAR* _glfwCreateWideStringFromUTF8(const char* source)
00278 {
00279 WCHAR* target;
00280 int length;
00281
00282 length = MultiByteToWideChar(CP_UTF8, 0, source, -1, NULL, 0);
00283 if (!length)
00284 return NULL;
00285
00286 target = calloc(length, sizeof(WCHAR));
00287
00288 if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, length))
00289 {
00290 free(target);
00291 return NULL;
00292 }
00293
00294 return target;
00295 }
00296
00297
00298
00299 char* _glfwCreateUTF8FromWideString(const WCHAR* source)
00300 {
00301 char* target;
00302 int length;
00303
00304 length = WideCharToMultiByte(CP_UTF8, 0, source, -1, NULL, 0, NULL, NULL);
00305 if (!length)
00306 return NULL;
00307
00308 target = calloc(length, sizeof(char));
00309
00310 if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, length, NULL, NULL))
00311 {
00312 free(target);
00313 return NULL;
00314 }
00315
00316 return target;
00317 }
00318
00319
00323
00324 int _glfwPlatformInit(void)
00325 {
00326
00327
00328
00329 SystemParametersInfoW(SPI_GETFOREGROUNDLOCKTIMEOUT, 0,
00330 &_glfw.win32.foregroundLockTimeout, 0);
00331 SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, UIntToPtr(0),
00332 SPIF_SENDCHANGE);
00333
00334 if (!initLibraries())
00335 return GL_FALSE;
00336
00337 createKeyTables();
00338
00339 if (_glfw_SetProcessDPIAware)
00340 _glfw_SetProcessDPIAware();
00341
00342 if (!_glfwRegisterWindowClass())
00343 return GL_FALSE;
00344
00345 if (!_glfwInitContextAPI())
00346 return GL_FALSE;
00347
00348 _glfwInitTimer();
00349 _glfwInitJoysticks();
00350
00351 return GL_TRUE;
00352 }
00353
00354 void _glfwPlatformTerminate(void)
00355 {
00356 _glfwUnregisterWindowClass();
00357
00358
00359 SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
00360 UIntToPtr(_glfw.win32.foregroundLockTimeout),
00361 SPIF_SENDCHANGE);
00362
00363 free(_glfw.win32.clipboardString);
00364
00365 _glfwTerminateJoysticks();
00366 _glfwTerminateContextAPI();
00367 terminateLibraries();
00368 }
00369
00370 const char* _glfwPlatformGetVersionString(void)
00371 {
00372 return _GLFW_VERSION_NUMBER " Win32"
00373 #if defined(_GLFW_WGL)
00374 " WGL"
00375 #elif defined(_GLFW_EGL)
00376 " EGL"
00377 #endif
00378 #if defined(__MINGW32__)
00379 " MinGW"
00380 #elif defined(_MSC_VER)
00381 " VisualC"
00382 #endif
00383 #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG)
00384 " hybrid-GPU"
00385 #endif
00386 #if defined(_GLFW_BUILD_DLL)
00387 " DLL"
00388 #endif
00389 ;
00390 }
00391