bulk commit, since cvs server was down while fixing :/

fix: formatting got fucked
fix: multiple poll
fix: removed AquireInput method
This commit is contained in:
Brian Matzon 2002-11-18 06:16:12 +00:00
parent 5d596cd7c3
commit e2c8914168
1 changed files with 206 additions and 231 deletions

View File

@ -1,4 +1,4 @@
/* /*
* Copyright (c) 2002 Light Weight Java Game Library Project * Copyright (c) 2002 Light Weight Java Game Library Project
* All rights reserved. * All rights reserved.
* *
@ -16,8 +16,7 @@
* * Neither the name of 'Light Weight Java Game Library' nor the names of * * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived * its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission. * from this software without specific prior written permission.
* * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
@ -38,31 +37,30 @@
* @author Brian Matzon <brian@matzon.com> * @author Brian Matzon <brian@matzon.com>
* @version $Revision$ * @version $Revision$
*/ */
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include "org_lwjgl_input_Joystick.h" #include "org_lwjgl_input_Joystick.h"
#include <windows.h> #include <windows.h>
#undef DIRECTINPUT_VERSION #undef DIRECTINPUT_VERSION
#define DIRECTINPUT_VERSION 0x0500 #define DIRECTINPUT_VERSION 0x0500
#include <dinput.h> #include <dinput.h>
#define JOYMAX 1000 // Maxmimum range to which we'll gauge the swing #define JOYMAX 1000 // Maxmimum range to which we'll gauge the swing
#define JOYMIN -1000 // Minimum range to which we'll gauge the swing #define JOYMIN -1000 // Minimum range to which we'll gauge the swing
extern HWND hwnd; // Handle to window extern HWND hwnd; // Handle to window
IDirectInput* lpDI; // DI instance IDirectInput* lpDI; // DI instance
IDirectInputDevice2* lpDIDevice; // DI Device instance IDirectInputDevice2* lpDIDevice; // DI Device instance
DIJOYSTATE2 js; // State of joystick DIJOYSTATE2 js; // State of joystick
int buttoncount = 0; // Temporary buttoncount int buttoncount = 0; // Temporary buttoncount
bool hasz; // Temporary zaxis check bool hasz; // Temporary zaxis check
bool haspov; // Temporary pov check bool haspov; // Temporary pov check
JNIEnv* environment; // JNIEnvironment copy JNIEnv* environment; // JNIEnvironment copy
bool create_success; // bool used to determine successfull creation bool create_success; // bool used to determine successfull creation
// Cached fields of Joystick.java // Cached fields of Joystick.java
jclass clsJoystick; jclass clsJoystick;
@ -94,52 +92,52 @@ void PrintError(HRESULT error);
* Initializes any field ids * Initializes any field ids
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_initIDs(JNIEnv * env, jclass clazz) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_initIDs(JNIEnv * env, jclass clazz) {
environment = env; environment = env;
} }
/** /**
* Called when the Joystick instance is to be created * Called when the Joystick instance is to be created
*/ */
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Joystick_nCreate(JNIEnv *env, jobject obj) { JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Joystick_nCreate(JNIEnv *env, jobject obj) {
/* Cache fields in Joystick */ /* Cache fields in Joystick */
CacheFields(obj); CacheFields(obj);
// Create the DirectInput object. // Create the DirectInput object.
HRESULT hr; HRESULT hr;
hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &lpDI, NULL); hr = DirectInputCreate(GetModuleHandle(NULL), DIRECTINPUT_VERSION, &lpDI, NULL);
if (FAILED(hr)) { if (FAILED(hr)) {
Shutdown(); Shutdown();
return JNI_FALSE; return JNI_FALSE;
} }
/* Find all joysticks */ /* Find all joysticks */
EnumerateJoysticks(); EnumerateJoysticks();
if (!create_success) { if (!create_success) {
Shutdown(); Shutdown();
return JNI_FALSE; return JNI_FALSE;
} }
/* Enumerate capabilities of joystick */ /* Enumerate capabilities of joystick */
EnumerateCapabilities(); EnumerateCapabilities();
if (!create_success) { if (!create_success) {
Shutdown(); Shutdown();
return JNI_FALSE; return JNI_FALSE;
} }
/* Initialize any fields on the Joystick */ /* Initialize any fields on the Joystick */
InitializeFields(); InitializeFields();
/* Set capabilities */ /* Set capabilities */
SetCapabilities(); SetCapabilities();
/* Aquire the joystick */ /* Aquire the joystick */
hr = lpDIDevice->Acquire(); hr = lpDIDevice->Acquire();
if(FAILED(hr)) { if(FAILED(hr)) {
Shutdown(); Shutdown();
return JNI_FALSE; return JNI_FALSE;
} }
return create_success; return create_success;
} }
/* /*
@ -148,7 +146,7 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Joystick_nCreate(JNIEnv *env, jo
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nDestroy(JNIEnv *env, jobject obj) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nDestroy(JNIEnv *env, jobject obj) {
Shutdown(); Shutdown();
} }
/* /*
@ -157,271 +155,248 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nDestroy(JNIEnv *env, jobje
* Signature: ()V * Signature: ()V
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nPoll(JNIEnv * env, jobject obj) { JNIEXPORT void JNICALL Java_org_lwjgl_input_Joystick_nPoll(JNIEnv * env, jobject obj) {
lpDIDevice->Poll(); HRESULT hRes;
UpdateFields();
// poll the joystick to read the current state
hRes = lpDIDevice->Poll();
if (FAILED(hRes)) {
#if _DEBUG
printf("Poll fail\n");
#endif
return;
}
UpdateFields();
} }
/** /**
* Shutdown DI * Shutdown DI
*/ */
void Shutdown() { void Shutdown() {
// release DI instance // release DI instance
if (lpDI != NULL) { if (lpDI != NULL) {
// release device // release device
if (lpDIDevice != NULL) { if (lpDIDevice != NULL) {
lpDIDevice->Unacquire(); lpDIDevice->Unacquire();
lpDIDevice->Release(); lpDIDevice->Release();
lpDIDevice = NULL; lpDIDevice = NULL;
} }
lpDI->Release(); lpDI->Release();
lpDI = NULL; lpDI = NULL;
} }
// Delete are global reference to the Joystick // Delete are global reference to the Joystick
if(objJoystick != NULL) { if(objJoystick != NULL) {
environment->DeleteGlobalRef(objJoystick); environment->DeleteGlobalRef(objJoystick);
} }
} }
/** /**
* Enumerates the capabilities of the joystick attached to the system * Enumerates the capabilities of the joystick attached to the system
*/ */
void EnumerateCapabilities() { void EnumerateCapabilities() {
HRESULT hr; HRESULT hr;
hr = lpDIDevice->EnumObjects(EnumJoystickObjectsCallback, NULL, DIDFT_ALL); hr = lpDIDevice->EnumObjects(EnumJoystickObjectsCallback, NULL, DIDFT_ALL);
if FAILED(hr) { if FAILED(hr) {
create_success = false; create_success = false;
return; return;
} }
create_success = true; create_success = true;
} }
/** /**
* Enumerates the joysticks attached to the system * Enumerates the joysticks attached to the system
*/ */
void EnumerateJoysticks() { void EnumerateJoysticks() {
HRESULT hr; HRESULT hr;
hr = lpDI->EnumDevices(DIDEVTYPE_JOYSTICK, EnumJoystickCallback, 0, DIEDFL_ATTACHEDONLY); hr = lpDI->EnumDevices(DIDEVTYPE_JOYSTICK, EnumJoystickCallback, 0, DIEDFL_ATTACHEDONLY);
if FAILED(hr) { if FAILED(hr) {
create_success = false; create_success = false;
return; return;
} }
create_success = true; create_success = true;
} }
/** /**
* Callback from EnumDevices. Called for each joystick attached to the system * Callback from EnumDevices. Called for each joystick attached to the system
*/ */
BOOL CALLBACK EnumJoystickCallback(LPCDIDEVICEINSTANCE pdinst, LPVOID pvRef) { BOOL CALLBACK EnumJoystickCallback(LPCDIDEVICEINSTANCE pdinst, LPVOID pvRef) {
/* Add the joystick */ /* Add the joystick */
CreateJoystick(pdinst); CreateJoystick(pdinst);
if(create_success) { if(create_success) {
/* Do setup of joystick */ /* Do setup of joystick */
SetupJoystick(); SetupJoystick();
} }
/* just stop after 1st joystick */ /* just stop after 1st joystick */
return DIENUM_STOP; return DIENUM_STOP;
} }
/** /**
* Callback from EnumObjects. Called for each "object" on the joystick. * Callback from EnumObjects. Called for each "object" on the joystick.
*/ */
BOOL CALLBACK EnumJoystickObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) { BOOL CALLBACK EnumJoystickObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) {
if(lpddoi->guidType == GUID_Button) { if(lpddoi->guidType == GUID_Button) {
buttoncount++; buttoncount++;
} else if(lpddoi->guidType == GUID_XAxis || lpddoi->guidType == GUID_YAxis) { } else if(lpddoi->guidType == GUID_XAxis || lpddoi->guidType == GUID_YAxis) {
//do nothing //do nothing
} else if(lpddoi->guidType == GUID_ZAxis || strcmp("Throttle", lpddoi->tszName) == 0){ } else if(lpddoi->guidType == GUID_ZAxis || strcmp("Throttle", lpddoi->tszName) == 0){
hasz = true; hasz = true;
} else if (lpddoi->guidType == GUID_POV){ } else if (lpddoi->guidType == GUID_POV){
haspov = true; haspov = true;
} else { } else {
#ifdef _DEBUG #ifdef _DEBUG
printf("Unhandled object found: %s\n", lpddoi->tszName); printf("Unhandled object found: %s\n", lpddoi->tszName);
#endif #endif
} }
return DIENUM_CONTINUE; return DIENUM_CONTINUE;
} }
/** /**
* Creates the specified device as a joystick * Creates the specified device as a joystick
*/ */
void CreateJoystick(LPCDIDEVICEINSTANCE lpddi) { void CreateJoystick(LPCDIDEVICEINSTANCE lpddi) {
HRESULT hr; HRESULT hr;
hr = lpDI->CreateDevice(lpddi->guidInstance, (LPDIRECTINPUTDEVICE*) &lpDIDevice, NULL); hr = lpDI->CreateDevice(lpddi->guidInstance, (LPDIRECTINPUTDEVICE*) &lpDIDevice, NULL);
if FAILED(hr) { if FAILED(hr) {
create_success = false; create_success = false;
return; return;
} }
create_success = true; create_success = true;
} }
/** /**
* Sets up the joystick properties * Sets up the joystick properties
*/ */
void SetupJoystick() { void SetupJoystick() {
// set joystick data format // set joystick data format
if(lpDIDevice->SetDataFormat(&c_dfDIJoystick2) != DI_OK) { if(lpDIDevice->SetDataFormat(&c_dfDIJoystick2) != DI_OK) {
create_success = false; create_success = false;
return; return;
} }
// set the cooperative level // set the cooperative level
if(lpDIDevice->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) { if(lpDIDevice->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK) {
create_success = false; create_success = false;
return; return;
} }
// set X-axis range to (-1000 ... +1000) // set X-axis range to (-1000 ... +1000)
// This lets us test against 0 to see which way the stick is pointed. // This lets us test against 0 to see which way the stick is pointed.
DIPROPRANGE diprg; DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(diprg); diprg.diph.dwSize = sizeof(diprg);
diprg.diph.dwHeaderSize = sizeof(diprg.diph); diprg.diph.dwHeaderSize = sizeof(diprg.diph);
diprg.diph.dwObj = DIJOFS_X; diprg.diph.dwObj = DIJOFS_X;
diprg.diph.dwHow = DIPH_BYOFFSET; diprg.diph.dwHow = DIPH_BYOFFSET;
diprg.lMin = JOYMIN; diprg.lMin = JOYMIN;
diprg.lMax = JOYMAX; diprg.lMax = JOYMAX;
if(lpDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) { if(lpDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
create_success = false; create_success = false;
return; return;
} }
// //
// And again for Y-axis range // And again for Y-axis range
// //
diprg.diph.dwObj = DIJOFS_Y; diprg.diph.dwObj = DIJOFS_Y;
if(lpDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) if(lpDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK)
{ {
create_success = false; create_success = false;
return; return;
} }
// //
// And again for z-axis range (throttle) // And again for z-axis range (throttle)
// //
diprg.diph.dwObj = DIJOFS_Z; diprg.diph.dwObj = DIJOFS_Z;
if(lpDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) { if(lpDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
create_success = false; create_success = false;
return; return;
} }
create_success = true; create_success = true;
}
/**
* Aquires the Joystick
*/
bool AcquireInput(void) {
HRESULT hRes;
// if we have a device
if (lpDIDevice != NULL) {
// acquire the device
hRes = lpDIDevice->Acquire();
if (SUCCEEDED(hRes)) {
// acquisition successful
return TRUE;
} else {
// acquisition failed
return FALSE;
}
} else {
// we don't have a current device
return FALSE;
}
} }
/** /**
* Sets the fields on the Joystick * Sets the fields on the Joystick
*/ */
void InitializeFields() { void InitializeFields() {
//set buttons array //set buttons array
jbooleanArray buttonsArray = environment->NewBooleanArray(buttoncount); jbooleanArray buttonsArray = environment->NewBooleanArray(buttoncount);
environment->SetObjectField(objJoystick, fidButtons, buttonsArray); environment->SetObjectField(objJoystick, fidButtons, buttonsArray);
} }
/** /**
* Updates the fields on the Joystick * Updates the fields on the Joystick
*/ */
void UpdateFields() { void UpdateFields() {
HRESULT hRes; HRESULT hRes;
// poll the joystick to read the current state // get data from the joystick
hRes = lpDIDevice->Poll(); hRes = lpDIDevice->GetDeviceState(sizeof(DIJOYSTATE2), &js);
if (FAILED(hRes)) {
if (hRes != DI_OK) {
// did the read fail because we lost input for some reason?
// if so, then attempt to reacquire.
if(hRes == DIERR_INPUTLOST) {
lpDIDevice->Acquire();
#if _DEBUG #if _DEBUG
printf("Poll fail\n"); printf("DIERR_INPUTLOST, reaquiring input\n");
#endif #endif
return;
}
// get data from the joystick
hRes = lpDIDevice->GetDeviceState(sizeof(DIJOYSTATE2), &js);
if (hRes != DI_OK) {
// did the read fail because we lost input for some reason?
// if so, then attempt to reacquire.
if(hRes == DIERR_INPUTLOST) {
AcquireInput();
#if _DEBUG
printf("DIERR_INPUTLOST, reaquiring input\n");
#endif
}
return;
} }
return;
//axis's }
environment->SetIntField(objJoystick, fidX, js.lX);
environment->SetIntField(objJoystick, fidY, js.lY);
if(hasz) {
environment->SetIntField(objJoystick, fidZ, js.lZ);
}
//buttons //axis's
jbooleanArray buttonsArray = (jbooleanArray) environment->GetObjectField(objJoystick, fidButtons); environment->SetIntField(objJoystick, fidX, js.lX);
BYTE * buttons = (BYTE *) environment->GetPrimitiveArrayCritical(buttonsArray, NULL); environment->SetIntField(objJoystick, fidY, js.lY);
memcpy(buttons, js.rgbButtons, 4); if(hasz) {
environment->ReleasePrimitiveArrayCritical(buttonsArray, buttons, 0); environment->SetIntField(objJoystick, fidZ, js.lZ);
}
//pov //buttons
if(haspov) { jbooleanArray buttonsArray = (jbooleanArray) environment->GetObjectField(objJoystick, fidButtons);
environment->SetIntField(objJoystick, fidPOV, js.rgdwPOV[0]); BYTE * buttons = (BYTE *) environment->GetPrimitiveArrayCritical(buttonsArray, NULL);
} memcpy(buttons, js.rgbButtons, 4);
environment->ReleasePrimitiveArrayCritical(buttonsArray, buttons, 0);
//pov
if(haspov) {
environment->SetIntField(objJoystick, fidPOV, js.rgdwPOV[0]);
}
} }
/** /**
* Sets the capabilities of the joystick * Sets the capabilities of the joystick
*/ */
void SetCapabilities() { void SetCapabilities() {
//set buttoncount //set buttoncount
environment->SetIntField(objJoystick, fidButtonCount, buttoncount); environment->SetIntField(objJoystick, fidButtonCount, buttoncount);
//set z axis //set z axis
environment->SetIntField(objJoystick, fidHasZAxis, hasz); environment->SetIntField(objJoystick, fidHasZAxis, hasz);
//set pov //set pov
environment->SetIntField(objJoystick, fidHasPOV, haspov); environment->SetIntField(objJoystick, fidHasPOV, haspov);
} }
/** /**
* Caches the field ids for quicker access * Caches the field ids for quicker access
*/ */
void CacheFields(jobject obj) { void CacheFields(jobject obj) {
/* make certain that we're caching from a global object, and not a local */ /* make certain that we're caching from a global object, and not a local */
objJoystick = environment->NewGlobalRef(obj); objJoystick = environment->NewGlobalRef(obj);
clsJoystick = environment->GetObjectClass(objJoystick); clsJoystick = environment->GetObjectClass(objJoystick);
/* cache fields */ /* cache fields */
fidButtonCount = environment->GetFieldID(clsJoystick, "buttonCount", "I"); fidButtonCount = environment->GetFieldID(clsJoystick, "buttonCount", "I");
fidHasZAxis = environment->GetFieldID(clsJoystick, "hasZAxis", "Z"); fidHasZAxis = environment->GetFieldID(clsJoystick, "hasZAxis", "Z");
fidHasPOV = environment->GetFieldID(clsJoystick, "hasPOV", "Z"); fidHasPOV = environment->GetFieldID(clsJoystick, "hasPOV", "Z");
fidButtons = environment->GetFieldID(clsJoystick, "buttons", "[Z"); fidButtons = environment->GetFieldID(clsJoystick, "buttons", "[Z");
fidX = environment->GetFieldID(clsJoystick, "x", "I"); fidX = environment->GetFieldID(clsJoystick, "x", "I");
fidY = environment->GetFieldID(clsJoystick, "y", "I"); fidY = environment->GetFieldID(clsJoystick, "y", "I");
fidZ = environment->GetFieldID(clsJoystick, "z", "I"); fidZ = environment->GetFieldID(clsJoystick, "z", "I");
fidPOV = environment->GetFieldID(clsJoystick, "pov", "I"); fidPOV = environment->GetFieldID(clsJoystick, "pov", "I");
} }