diff --git a/src/native/win32/Window.h b/src/native/win32/Window.h index d078206b..c88c01b0 100644 --- a/src/native/win32/Window.h +++ b/src/native/win32/Window.h @@ -45,9 +45,6 @@ #define WIN32_LEAN_AND_MEAN #include #include - #undef DIRECTINPUT_VERSION - #define DIRECTINPUT_VERSION 0x0500 - #include #include "extgl.h" #ifdef _PRIVATE_WINDOW_H_ @@ -57,7 +54,6 @@ extern HWND hwnd; // Handle to the window extern HDC hdc; // Device context - extern LPDIRECTINPUT lpdi; // DirectInput extern bool isFullScreen; // Whether we're fullscreen or not extern bool isMinimized; // Whether we're minimized or not extern bool isFocused; // Whether we're focused or not diff --git a/src/native/win32/org_lwjgl_input_Keyboard.cpp b/src/native/win32/org_lwjgl_input_Keyboard.cpp index 7700cc1f..4cf48138 100644 --- a/src/native/win32/org_lwjgl_input_Keyboard.cpp +++ b/src/native/win32/org_lwjgl_input_Keyboard.cpp @@ -51,6 +51,9 @@ #define KEYBOARD_BUFFER_SIZE 50 +extern HINSTANCE dll_handle; // Handle to the LWJGL dll + +static LPDIRECTINPUT lpdi = NULL; // DirectInput static LPDIRECTINPUTDEVICE lpdiKeyboard = NULL; static bool translationEnabled; @@ -64,6 +67,13 @@ static bool useUnicode; JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nCreate (JNIEnv * env, jclass clazz) { + // Create input + HRESULT ret = DirectInputCreate(dll_handle, DIRECTINPUT_VERSION, &lpdi, NULL); + if (ret != DI_OK && ret != DIERR_BETADIRECTINPUTVERSION) { + throwException(env, "Failed to create DirectInput"); + return; + } + translationEnabled = false; // Check to see if we're already initialized if (lpdiKeyboard != NULL) { @@ -98,7 +108,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nCreate dipropdw.dwData = KEYBOARD_BUFFER_SIZE; lpdiKeyboard->SetProperty(DIPROP_BUFFERSIZE, &dipropdw.diph); - HRESULT ret = lpdiKeyboard->Acquire(); + ret = lpdiKeyboard->Acquire(); if(FAILED(ret)) { printfDebug("Failed to acquire keyboard\n"); } @@ -118,6 +128,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nDestroy lpdiKeyboard->Release(); lpdiKeyboard = NULL; } + // Release DirectInput + if (lpdi != NULL) { + printfDebug("Destroying directinput\n"); + lpdi->Release(); + lpdi = NULL; + } } /* diff --git a/src/native/win32/org_lwjgl_input_Mouse.cpp b/src/native/win32/org_lwjgl_input_Mouse.cpp index e07b86a6..f7a0cd5b 100644 --- a/src/native/win32/org_lwjgl_input_Mouse.cpp +++ b/src/native/win32/org_lwjgl_input_Mouse.cpp @@ -43,10 +43,12 @@ #include #undef DIRECTINPUT_VERSION #define DIRECTINPUT_VERSION 0x0300 +#include #include "Window.h" #include "common_tools.h" -#include +extern HINSTANCE dll_handle; // Handle to the LWJGL dll +static LPDIRECTINPUT lpdi = NULL; // DirectInput static LPDIRECTINPUTDEVICE mDIDevice; // DI Device instance static int mButtoncount = 0; // Temporary buttoncount static bool mHaswheel; // Temporary wheel check @@ -92,11 +94,12 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetButtonCount(JNIEnv *, jcla JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclass clazz) { HRESULT hr; - // assert that Direct Input has been created - if(lpdi == NULL) { - throwException(env, "Please create the window before initializing input devices"); - return; - } + // Create input + HRESULT ret = DirectInputCreate(dll_handle, DIRECTINPUT_VERSION, &lpdi, NULL); + if (ret != DI_OK && ret != DIERR_BETADIRECTINPUTVERSION) { + throwException(env, "Failed to create DirectInput"); + return; + } /* skip enumeration, since we only want system mouse */ CreateMouse(); @@ -313,18 +316,24 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse /** * Shutdown DI */ -void ShutdownMouse() { +static void ShutdownMouse() { // release device if (mDIDevice != NULL) { mDIDevice->Unacquire(); mDIDevice->Release(); mDIDevice = NULL; } + // Release DirectInput + if (lpdi != NULL) { + printfDebug("Destroying directinput\n"); + lpdi->Release(); + lpdi = NULL; + } } /** * Enumerates the capabilities of the Mouse attached to the system */ -void EnumerateMouseCapabilities() { +static void EnumerateMouseCapabilities() { HRESULT hr; hr = mDIDevice->EnumObjects(EnumMouseObjectsCallback, NULL, DIDFT_ALL); if FAILED(hr) { @@ -345,7 +354,7 @@ void EnumerateMouseCapabilities() { /** * Callback from EnumObjects. Called for each "object" on the Mouse. */ -BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) { +static BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) { printfDebug("found %s\n", lpddoi->tszName); if(lpddoi->guidType == GUID_Button) { mButtoncount++; @@ -362,7 +371,7 @@ BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID /** * Creates the specified device as a Mouse */ -void CreateMouse() { +static void CreateMouse() { HRESULT hr; hr = lpdi->CreateDevice(GUID_SysMouse, &mDIDevice, NULL); if FAILED(hr) { @@ -376,7 +385,7 @@ void CreateMouse() { /** * Sets up the Mouse properties */ -void SetupMouse() { +static void SetupMouse() { // set Mouse data format if(mDIDevice->SetDataFormat(&c_dfDIMouse) != DI_OK) { printfDebug("SetDataFormat failed\n"); diff --git a/src/native/win32/org_lwjgl_opengl_Window.cpp b/src/native/win32/org_lwjgl_opengl_Window.cpp index 63d15ea7..7d8db6e9 100755 --- a/src/native/win32/org_lwjgl_opengl_Window.cpp +++ b/src/native/win32/org_lwjgl_opengl_Window.cpp @@ -49,7 +49,6 @@ static bool oneShotInitialised = false; // Registers the LWJGL window class HWND hwnd = NULL; // Handle to the window HDC hdc = NULL; // Device context HGLRC hglrc = NULL; // OpenGL context -LPDIRECTINPUT lpdi = NULL; // DirectInput static bool isFullScreen = false; // Whether we're fullscreen or not static bool isMinimized = false; // Whether we're minimized or not static bool isFocused = false; // whether we're focused or not @@ -190,46 +189,12 @@ static int findPixelFormat(JNIEnv *env, int bpp, int alpha, int depth, int stenc return iPixelFormat; } -/* - * Create DirectInput. - * Returns true for success, or false for failure - */ -static bool createDirectInput(JNIEnv *env) -{ - // Create input - HRESULT ret = DirectInputCreate(dll_handle, DIRECTINPUT_VERSION, &lpdi, NULL); - if (ret != DI_OK && ret != DIERR_BETADIRECTINPUTVERSION ) { - switch (ret) { - case DIERR_INVALIDPARAM : - throwException(env, "Failed to create DirectInput - Invalid parameter\n"); - break; - case DIERR_OLDDIRECTINPUTVERSION : - throwException(env, "Failed to create DirectInput - Old Version\n"); - break; - case DIERR_OUTOFMEMORY : - throwException(env, "Failed to create DirectInput - Out Of Memory\n"); - break; - default: - throwException(env, "Failed to create DirectInput - Unknown failure\n"); - } - return false; - } else { - return true; - } -} /* * Close the window */ static void closeWindow() { - // Release DirectInput - if (lpdi != NULL) { - printfDebug("Destroying directinput\n"); - lpdi->Release(); - lpdi = NULL; - } - // Release device context if (hdc != NULL && hwnd != NULL) { printfDebug("Releasing DC\n"); @@ -551,13 +516,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate } } - if (!createDirectInput(env)) { - // Close the window - closeWindow(); - extgl_Close(); - return; - } - ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); SetForegroundWindow(hwnd);