Windows: Cleanup of native input code

This commit is contained in:
Elias Naur 2006-06-23 16:45:21 +00:00
parent 9e811f332a
commit e99b357a1b
2 changed files with 48 additions and 81 deletions

View File

@ -69,34 +69,28 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_createKeyboard
return; return;
} }
// Check to see if we're already initialized
if (lpdiKeyboard != NULL) {
throwException(env, "Keyboard already created.");
return;
}
// Create a keyboard device // Create a keyboard device
if (IDirectInput_CreateDevice(lpdi, &GUID_SysKeyboard, &lpdiKeyboard, NULL) != DI_OK) { if (IDirectInput_CreateDevice(lpdi, &GUID_SysKeyboard, &lpdiKeyboard, NULL) != DI_OK) {
throwException(env, "Failed to create keyboard."); throwException(env, "Failed to create keyboard.");
return; return;
} }
if (IDirectInputDevice_SetCooperativeLevel(lpdiKeyboard, getCurrentHWND(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK) { if (IDirectInputDevice_SetCooperativeLevel(lpdiKeyboard, getCurrentHWND(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK) {
throwException(env, "Failed to set keyboard cooperation mode."); throwException(env, "Failed to set keyboard cooperation mode.");
return; return;
} }
// Tell 'em wot format to be in (the default "you are a mouse and keyboard" format) // Tell 'em wot format to be in (the default "you are a mouse and keyboard" format)
IDirectInputDevice_SetDataFormat(lpdiKeyboard, &c_dfDIKeyboard); IDirectInputDevice_SetDataFormat(lpdiKeyboard, &c_dfDIKeyboard);
dipropdw.diph.dwSize = sizeof(DIPROPDWORD); dipropdw.diph.dwSize = sizeof(DIPROPDWORD);
dipropdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipropdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipropdw.diph.dwObj = 0; dipropdw.diph.dwObj = 0;
dipropdw.diph.dwHow = DIPH_DEVICE; dipropdw.diph.dwHow = DIPH_DEVICE;
dipropdw.dwData = KEYBOARD_BUFFER_SIZE; dipropdw.dwData = KEYBOARD_BUFFER_SIZE;
IDirectInputDevice_SetProperty(lpdiKeyboard, DIPROP_BUFFERSIZE, &dipropdw.diph); IDirectInputDevice_SetProperty(lpdiKeyboard, DIPROP_BUFFERSIZE, &dipropdw.diph);
ret = IDirectInputDevice_Acquire(lpdiKeyboard); ret = IDirectInputDevice_Acquire(lpdiKeyboard);
if(FAILED(ret)) { if(FAILED(ret)) {
printfDebugJava(env, "Failed to acquire keyboard"); printfDebugJava(env, "Failed to acquire keyboard");
} }
@ -120,14 +114,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_destroyKeyboard
{ {
// Release keyboard // Release keyboard
if (lpdiKeyboard != NULL) { if (lpdiKeyboard != NULL) {
IDirectInputDevice_Unacquire(lpdiKeyboard); IDirectInputDevice_Unacquire(lpdiKeyboard);
IDirectInputDevice_Release(lpdiKeyboard); IDirectInputDevice_Release(lpdiKeyboard);
lpdiKeyboard = NULL; lpdiKeyboard = NULL;
} }
// Release DirectInput // Release DirectInput
if (lpdi != NULL) { if (lpdi != NULL) {
printfDebugJava(env, "Destroying directinput"); printfDebugJava(env, "Destroying directinput");
IDirectInput_Release(lpdi); IDirectInput_Release(lpdi);
lpdi = NULL; lpdi = NULL;
} }
} }
@ -140,50 +134,32 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_destroyKeyboard
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_nPollKeyboard JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_nPollKeyboard
(JNIEnv * env, jobject self, jobject buffer) (JNIEnv * env, jobject self, jobject buffer)
{ {
HRESULT ret; void *keyboardBuffer;
void *keyboardBuffer; jlong buffer_size;
jlong buffer_size; HRESULT ret = IDirectInputDevice_Acquire(lpdiKeyboard);
do { if (ret != DI_OK && ret != S_FALSE) {
ret = IDirectInputDevice_Acquire(lpdiKeyboard); // printfDebugJava(env, "Failed to acquire keyboard (%x)\n", ret);
if (ret == DIERR_INPUTLOST) { return;
printf("Input lost\n"); }
return;
} else if (ret == DIERR_NOTACQUIRED) {
printf("not acquired\n");
return;
} else if (ret == DIERR_INVALIDPARAM) {
printf("invalid parameter\n");
return;
} else if (ret == DIERR_NOTBUFFERED) {
printf("not buffered\n");
return;
} else if (ret == DIERR_NOTINITIALIZED) {
printf("not inited\n");
return;
} else if (ret != DI_OK && ret != S_FALSE) {
//printf("unknown keyboard error\n");
return;
}
} while (ret != DI_OK && ret != S_FALSE);
keyboardBuffer = (void *)(*env)->GetDirectBufferAddress(env, buffer); keyboardBuffer = (void *)(*env)->GetDirectBufferAddress(env, buffer);
buffer_size = (*env)->GetDirectBufferCapacity(env, buffer); buffer_size = (*env)->GetDirectBufferCapacity(env, buffer);
IDirectInputDevice_GetDeviceState(lpdiKeyboard, (DWORD)buffer_size, keyboardBuffer); IDirectInputDevice_GetDeviceState(lpdiKeyboard, (DWORD)buffer_size, keyboardBuffer);
} }
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Win32Display_nReadKeyboard JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Win32Display_nReadKeyboard
(JNIEnv * env, jobject self, jobject buffer_obj, jint buffer_position) (JNIEnv * env, jobject self, jobject buffer_obj, jint buffer_position)
{ {
UINT scan_code; UINT scan_code;
UINT virt_key; UINT virt_key;
bool key_down; bool key_down;
jint * buf; jint * buf;
jint ch_int; jint ch_int;
int buffer_size; int buffer_size;
int index = 0; int index = 0;
int event_size = 3; int event_size = 3;
DWORD current_di_event = 0; DWORD current_di_event = 0;
DIDEVICEOBJECTDATA rgdod[KEYBOARD_BUFFER_SIZE]; DIDEVICEOBJECTDATA rgdod[KEYBOARD_BUFFER_SIZE];
wchar_t transBufUnicode[KEYBOARD_BUFFER_SIZE]; wchar_t transBufUnicode[KEYBOARD_BUFFER_SIZE];
WORD transBufAscii[KEYBOARD_BUFFER_SIZE]; WORD transBufAscii[KEYBOARD_BUFFER_SIZE];
@ -194,42 +170,42 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Win32Display_nReadKeyboard
int num_chars; int num_chars;
int num_events = 0; int num_events = 0;
ret = IDirectInputDevice_Acquire(lpdiKeyboard); ret = IDirectInputDevice_Acquire(lpdiKeyboard);
if (ret != DI_OK && ret != S_FALSE) if (ret != DI_OK && ret != S_FALSE)
return 0; return 0;
ret = IDirectInputDevice_GetDeviceData(lpdiKeyboard, ret = IDirectInputDevice_GetDeviceData(lpdiKeyboard,
sizeof(DIDEVICEOBJECTDATA), sizeof(DIDEVICEOBJECTDATA),
rgdod, rgdod,
&num_di_events, &num_di_events,
0); 0);
if (ret == DI_OK) { if (ret == DI_OK) {
buf = buffer_position + (jint *)(*env)->GetDirectBufferAddress(env, buffer_obj); buf = buffer_position + (jint *)(*env)->GetDirectBufferAddress(env, buffer_obj);
buffer_size = ((int)(*env)->GetDirectBufferCapacity(env, buffer_obj))/sizeof(jint) - buffer_position; buffer_size = ((int)(*env)->GetDirectBufferCapacity(env, buffer_obj))/sizeof(jint) - buffer_position;
while (index + event_size <= buffer_size && current_di_event < num_di_events) { while (index + event_size <= buffer_size && current_di_event < num_di_events) {
num_events++; num_events++;
buf[index++] = (unsigned char) rgdod[current_di_event].dwOfs; buf[index++] = (unsigned char) rgdod[current_di_event].dwOfs;
buf[index++] = (unsigned char) rgdod[current_di_event].dwData; buf[index++] = (unsigned char) rgdod[current_di_event].dwData;
key_down = (rgdod[current_di_event].dwData & 0x80) != 0; key_down = (rgdod[current_di_event].dwData & 0x80) != 0;
if (key_down) { if (key_down) {
scan_code = rgdod[current_di_event].dwOfs; scan_code = rgdod[current_di_event].dwOfs;
virt_key = MapVirtualKey(scan_code, 1); virt_key = MapVirtualKey(scan_code, 1);
if (virt_key != 0 && GetKeyboardState(state)) { if (virt_key != 0 && GetKeyboardState(state)) {
// Mark key down in the scan code // Mark key down in the scan code
scan_code = scan_code & 0x7fff; scan_code = scan_code & 0x7fff;
if (useUnicode) { if (useUnicode) {
num_chars = ToUnicode(virt_key, num_chars = ToUnicode(virt_key,
scan_code, scan_code,
state, state,
transBufUnicode, transBufUnicode,
KEYBOARD_BUFFER_SIZE, 0); KEYBOARD_BUFFER_SIZE, 0);
} else { } else {
num_chars = ToAscii(virt_key, num_chars = ToAscii(virt_key,
scan_code, scan_code,
state, state,
transBufAscii, transBufAscii,
0); 0);
} }
if (num_chars > 0) { if (num_chars > 0) {
int current_char = 0; int current_char = 0;

View File

@ -127,7 +127,7 @@ static bool EnumerateMouseCapabilities(JNIEnv *env) {
*/ */
static bool CreateMouse(JNIEnv *env) { static bool CreateMouse(JNIEnv *env) {
HRESULT hr; HRESULT hr;
hr = IDirectInput_CreateDevice(lpdi, &GUID_SysMouse, &mDIDevice, NULL); hr = IDirectInput_CreateDevice(lpdi, &GUID_SysMouse, &mDIDevice, NULL);
if FAILED(hr) { if FAILED(hr) {
throwException(env, "CreateDevice failed"); throwException(env, "CreateDevice failed");
return false; return false;
@ -447,15 +447,6 @@ static BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi,
return DIENUM_CONTINUE; return DIENUM_CONTINUE;
} }
static int cap(int val, int min, int max) {
if (val < min)
return min;
else if (val > max)
return max;
else
return val;
}
/** /**
* Updates the fields on the Mouse * Updates the fields on the Mouse
*/ */