I've discovered that at least one Win XP configuration doesn't support the DirectInput cooperative level NONEXCLUSIVE | FOREGROUND for mouse devices. Fortunately, we don't really need that coop level anyway, so I have replaced it with Unacquire/Acquire.

This commit is contained in:
Elias Naur 2005-01-10 08:48:13 +00:00
parent 69bc20f3f5
commit e31b1ee12d
1 changed files with 78 additions and 99 deletions

View File

@ -49,6 +49,8 @@
#define EVENT_SIZE 5
#define BUTTON_STATES_SIZE 7
extern HINSTANCE dll_handle; // Handle to the LWJGL dll
static LPDIRECTINPUT lpdi = NULL; // DirectInput
static LPDIRECTINPUTDEVICE mDIDevice; // DI Device instance
@ -58,8 +60,9 @@ static bool mHaswheel; // Temporary wheel check
static bool mFirstTimeInitialization = true; // boolean to determine first time initialization
static bool created = false;
static jboolean win32_message_button_states[BUTTON_STATES_SIZE];
static bool mouse_grabbed;
static int mouseMask = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
/* These accumulated deltas track the cursor position from Windows messages */
static int accum_dx;
@ -158,7 +161,7 @@ static bool SetupMouse(JNIEnv *env) {
IDirectInputDevice_SetProperty(mDIDevice, DIPROP_BUFFERSIZE, &dipropdw.diph);
// set the cooperative level
if (IDirectInputDevice_SetCooperativeLevel(mDIDevice, getCurrentHWND(), mouseMask) != DI_OK) {
if (IDirectInputDevice_SetCooperativeLevel(mDIDevice, getCurrentHWND(), DISCL_EXCLUSIVE | DISCL_FOREGROUND) != DI_OK) {
throwException(env, "SetCooperativeLevel failed");
return false;
}
@ -243,6 +246,8 @@ void handleMouseMoved(int x, int y) {
void handleMouseButton(int button, int state) {
if(created) {
putMouseEvent(button, state, 0);
if (button < BUTTON_STATES_SIZE)
win32_message_button_states[button] = state != 0 : JNI_TRUE: JNI_FALSE;
}
}
@ -344,8 +349,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_setNativeCursor
{
HCURSOR *cursor_handle;
HCURSOR cursor;
if (mDIDevice == NULL)
throwException(env, "null device!");
if (handle_buffer != NULL) {
cursor_handle = (HCURSOR *)(*env)->GetDirectBufferAddress(env, handle_buffer);
cursor = *cursor_handle;
@ -374,46 +377,25 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_destroyMouse(JNIEnv *e
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_pollMouse(JNIEnv * env, jobject self, jobject coord_buffer_obj, jobject button_buffer_obj) {
IDirectInputDevice_Acquire(mDIDevice);
UpdateMouseFields(env, coord_buffer_obj, button_buffer_obj);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_grabMouse
(JNIEnv * env, jobject self, jboolean grab) {
HRESULT di_res;
IDirectInputDevice_Unacquire(mDIDevice);
if(grab) {
if (!mouse_grabbed) {
mouse_grabbed = true;
ShowCursor(false);
mouseMask = DISCL_EXCLUSIVE | DISCL_FOREGROUND;
IDirectInputDevice_Acquire(mDIDevice);
}
} else {
if (mouse_grabbed) {
mouse_grabbed = false;
ShowCursor(true);
mouseMask = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
IDirectInputDevice_Unacquire(mDIDevice);
}
}
di_res = IDirectInputDevice_SetCooperativeLevel(mDIDevice, getCurrentHWND(), mouseMask);
switch (di_res) {
case DI_OK:
break;
case DIERR_INVALIDPARAM:
throwException(env, "Could not set the CooperativeLevel (DIERR_INVALIDPARAM).");
return;
case DIERR_NOTINITIALIZED:
throwException(env, "Could not set the CooperativeLevel (DIERR_NOTINITIALIZED).");
return;
case E_HANDLE:
throwException(env, "Could not set the CooperativeLevel (E_HANDLE).");
return;
default:
printfDebugJava(env, "Failed to set cooperative level with unknown error code %x", di_res);
throwException(env, "Could not set the CooperativeLevel (Unknown error code).");
return;
}
IDirectInputDevice_Acquire(mDIDevice);
initEventQueue(&event_queue, EVENT_SIZE);
}
@ -423,13 +405,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_grabMouse
static void ShutdownMouse() {
// release device
if (mDIDevice != NULL) {
printfDebug("Releasing mouse DI device\n");
IDirectInputDevice_Unacquire(mDIDevice);
IDirectInputDevice_Release(mDIDevice);
mDIDevice = NULL;
}
// Release DirectInput
if (lpdi != NULL) {
printfDebug("Destroying directinput\n");
printfDebug("Releasing directinput\n");
IDirectInput_Release(lpdi);
lpdi = NULL;
}
@ -482,7 +465,7 @@ static void UpdateMouseFields(JNIEnv *env, jobject coord_buffer_obj, jobject but
handleMessages();
// get data from the Mouse
if (mouse_grabbed) {
hRes = IDirectInputDevice_GetDeviceState(mDIDevice, sizeof(DIMOUSESTATE), &diMouseState);
if (hRes != DI_OK) {
// Don't allow the mouse to drift when failed
@ -501,27 +484,23 @@ static void UpdateMouseFields(JNIEnv *env, jobject coord_buffer_obj, jobject but
}
}
if (mouse_grabbed) {
coords[0] = diMouseState.lX;
coords[1] = -diMouseState.lY;
coords[2] = diMouseState.lZ;
num_buttons = mButtoncount;
if (num_buttons > buttons_length)
num_buttons = buttons_length;
for (j = 0; j < num_buttons; j++)
buttons_buffer[j] = diMouseState.rgbButtons[j] != 0 ? JNI_TRUE : JNI_FALSE;
} else {
coords[0] = last_x;
coords[1] = last_y;
coords[2] = accum_dwheel;
accum_dx = accum_dy = accum_dwheel = 0;
}
for (i = 0; i < mButtoncount; i++) {
if (diMouseState.rgbButtons[i] != 0) {
diMouseState.rgbButtons[i] = JNI_TRUE;
} else {
diMouseState.rgbButtons[i] = JNI_FALSE;
}
}
num_buttons = mButtoncount;
if (num_buttons > buttons_length)
num_buttons = buttons_length;
if (num_buttons > BUTTON_STATES_SIZE)
num_buttons = BUTTON_STATES_SIZE;
for (j = 0; j < num_buttons; j++)
buttons_buffer[j] = (unsigned char)diMouseState.rgbButtons[j];
buttons_buffer[j] = win32_message_button_states[j];
}
}