2002-11-18 01:16:12 -05:00
|
|
|
/*
|
2003-01-03 13:47:42 -05:00
|
|
|
* Copyright (c) 2002 Lightweight Java Game Library Project
|
2002-10-19 10:27:26 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
2003-12-15 06:49:17 -05:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2002-10-19 10:27:26 -04:00
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
2003-12-15 06:49:17 -05:00
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2002-10-19 10:27:26 -04:00
|
|
|
*
|
|
|
|
* * Neither the name of 'Light Weight Java Game Library' nor the names of
|
2003-12-15 06:49:17 -05:00
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2002-11-18 01:16:12 -05:00
|
|
|
* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
2002-10-19 10:27:26 -04:00
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $Id$
|
|
|
|
*
|
2003-01-03 13:47:42 -05:00
|
|
|
* Win32 controller handling.
|
2002-10-19 10:27:26 -04:00
|
|
|
*
|
2002-11-17 13:13:46 -05:00
|
|
|
* @author Brian Matzon <brian@matzon.com>
|
2002-10-19 10:27:26 -04:00
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
|
2003-09-30 07:52:32 -04:00
|
|
|
|
2002-11-18 01:16:12 -05:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2003-01-03 13:47:42 -05:00
|
|
|
#include "org_lwjgl_input_Controller.h"
|
2002-11-17 13:13:46 -05:00
|
|
|
#include <windows.h>
|
2003-12-15 06:49:17 -05:00
|
|
|
#undef DIRECTINPUT_VERSION
|
2002-11-17 13:13:46 -05:00
|
|
|
#define DIRECTINPUT_VERSION 0x0500
|
2002-10-19 10:27:26 -04:00
|
|
|
#include <dinput.h>
|
2003-10-11 12:29:40 -04:00
|
|
|
#include "Window.h"
|
|
|
|
#include "common_tools.h"
|
2002-10-19 10:27:26 -04:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
#define CONTROLLER_AXISMAX 1000 // Maxmimum range to which we'll gauge the swing
|
|
|
|
#define CONTROLLER_AXISMIN -1000 // Minimum range to which we'll gauge the swing
|
2002-11-17 13:13:46 -05:00
|
|
|
|
2003-02-08 14:31:19 -05:00
|
|
|
extern HINSTANCE dll_handle;
|
2003-05-08 17:30:30 -04:00
|
|
|
extern HWND hwnd;
|
2002-11-17 13:13:46 -05:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
static IDirectInput* cDI; // DI instance
|
|
|
|
static IDirectInputDevice2* cDIDevice; // DI Device instance
|
|
|
|
static DIJOYSTATE2 cJS; // State of Controller
|
|
|
|
|
|
|
|
static int cButtoncount = 0; // Temporary buttoncount
|
|
|
|
static bool cHasx; // Temporary xaxis check
|
|
|
|
static bool cHasrx; // Temporary rotational xaxis check
|
|
|
|
static bool cHasy; // Temporary yaxis check
|
|
|
|
static bool cHasry; // Temporary rotational yaxis check
|
|
|
|
static bool cHasz; // Temporary zaxis check
|
|
|
|
static bool cHasrz; // Temporary rotational zaxis check
|
|
|
|
static bool cHaspov; // Temporary pov check
|
|
|
|
static bool cHasslider; // Temporary slider check
|
|
|
|
|
|
|
|
static bool cCreate_success; // bool used to determine successfull creation
|
2003-05-16 14:39:46 -04:00
|
|
|
static bool cFirstTimeInitialization = true; // boolean to determine first time initialization
|
2002-11-17 13:13:46 -05:00
|
|
|
|
2003-01-03 13:47:42 -05:00
|
|
|
// Cached fields of Controller.java
|
2003-05-16 14:39:46 -04:00
|
|
|
static jfieldID fidCButtonCount;
|
|
|
|
static jfieldID fidCHasXAxis;
|
|
|
|
static jfieldID fidCHasRXAxis;
|
|
|
|
static jfieldID fidCHasYAxis;
|
|
|
|
static jfieldID fidCHasRYAxis;
|
|
|
|
static jfieldID fidCHasZAxis;
|
|
|
|
static jfieldID fidCHasRZAxis;
|
|
|
|
static jfieldID fidCHasPOV;
|
|
|
|
static jfieldID fidCHasSlider;
|
|
|
|
static jfieldID fidCButtons;
|
|
|
|
static jfieldID fidCX;
|
|
|
|
static jfieldID fidCRX;
|
|
|
|
static jfieldID fidCY;
|
|
|
|
static jfieldID fidCRY;
|
|
|
|
static jfieldID fidCZ;
|
|
|
|
static jfieldID fidCRZ;
|
|
|
|
static jfieldID fidCPOV;
|
|
|
|
static jfieldID fidCSlider;
|
2002-11-17 13:13:46 -05:00
|
|
|
|
|
|
|
// Function prototypes (defined in the cpp file, since header file is generic across platforms
|
2003-02-08 14:31:19 -05:00
|
|
|
void EnumerateControllerCapabilities();
|
2003-01-03 13:47:42 -05:00
|
|
|
void EnumerateControllers();
|
|
|
|
BOOL CALLBACK EnumControllerCallback(LPCDIDEVICEINSTANCE pdinst, LPVOID pvRef);
|
|
|
|
BOOL CALLBACK EnumControllerObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef);
|
2003-02-08 14:31:19 -05:00
|
|
|
void ShutdownController();
|
2003-01-03 13:47:42 -05:00
|
|
|
void CreateController(LPCDIDEVICEINSTANCE lpddi);
|
|
|
|
void SetupController();
|
2003-10-11 12:29:40 -04:00
|
|
|
void InitializeControllerFields(JNIEnv *env, jclass clsController);
|
|
|
|
void CacheControllerFields(JNIEnv *env, jclass clsController);
|
|
|
|
void UpdateControllerFields(JNIEnv *env, jclass clsController);
|
|
|
|
void SetControllerCapabilities(JNIEnv *env, jclass clsController);
|
2002-11-17 13:13:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes any field ids
|
2002-10-19 10:27:26 -04:00
|
|
|
*/
|
2003-01-03 13:47:42 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Controller_initIDs(JNIEnv * env, jclass clazz) {
|
2003-12-15 06:49:17 -05:00
|
|
|
/* Cache fields in Controller */
|
|
|
|
CacheControllerFields(env, clazz);
|
2002-11-17 13:13:46 -05:00
|
|
|
}
|
2002-10-19 10:27:26 -04:00
|
|
|
|
2002-11-17 13:13:46 -05:00
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Called when the Controller instance is to be created
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-09-30 07:14:06 -04:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Controller_nCreate(JNIEnv *env, jclass clazz) {
|
2003-12-15 06:49:17 -05:00
|
|
|
// Create the DirectInput object.
|
|
|
|
HRESULT hr;
|
|
|
|
hr = DirectInputCreate(dll_handle, DIRECTINPUT_VERSION, &cDI, NULL);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
printfDebug("DirectInputCreate failed\n");
|
|
|
|
ShutdownController();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find all Controllers */
|
|
|
|
EnumerateControllers();
|
|
|
|
if (!cCreate_success) {
|
|
|
|
throwException(env, "Failed to enumerate.");
|
|
|
|
ShutdownController();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that we got at least 1 controller */
|
|
|
|
if (cDIDevice == NULL) {
|
|
|
|
throwException(env, "No devices found.");
|
|
|
|
ShutdownController();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check for first time initialization - need to detect capabilities
|
|
|
|
if (cFirstTimeInitialization) {
|
|
|
|
cFirstTimeInitialization = false;
|
|
|
|
|
|
|
|
/* Enumerate capabilities of Controller */
|
|
|
|
EnumerateControllerCapabilities();
|
|
|
|
if (!cCreate_success) {
|
|
|
|
throwException(env, "Falied to enumerate capabilities.");
|
|
|
|
ShutdownController();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do setup of Controller */
|
|
|
|
SetupController();
|
|
|
|
|
|
|
|
/* Initialize any fields on the Controller */
|
|
|
|
InitializeControllerFields(env, clazz);
|
|
|
|
|
|
|
|
/* Set capabilities */
|
|
|
|
SetControllerCapabilities(env, clazz);
|
|
|
|
} else {
|
|
|
|
if(cCreate_success) {
|
|
|
|
/* Do setup of Controller */
|
|
|
|
SetupController();
|
|
|
|
|
|
|
|
/* Initialize any fields on the Controller */
|
|
|
|
InitializeControllerFields(env, clazz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Aquire the Controller */
|
|
|
|
hr = cDIDevice->Acquire();
|
|
|
|
if(FAILED(hr)) {
|
|
|
|
throwException(env, "Acquire failed");
|
|
|
|
ShutdownController();
|
|
|
|
return;
|
|
|
|
}
|
2002-10-19 10:27:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-12-15 06:49:17 -05:00
|
|
|
* Class: org_lwjgl_input_Controller
|
|
|
|
* Method: nDestroy
|
2002-10-19 10:27:26 -04:00
|
|
|
* Signature: ()V
|
|
|
|
*/
|
2003-01-03 13:47:42 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Controller_nDestroy(JNIEnv *env, jclass clazz) {
|
2003-12-15 06:49:17 -05:00
|
|
|
ShutdownController();
|
2002-10-19 10:27:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-12-15 06:49:17 -05:00
|
|
|
* Class: org_lwjgl_input_Controller
|
|
|
|
* Method: nPoll
|
2002-10-19 10:27:26 -04:00
|
|
|
* Signature: ()V
|
|
|
|
*/
|
2003-01-03 13:47:42 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Controller_nPoll(JNIEnv * env, jclass clazz) {
|
2003-12-15 06:49:17 -05:00
|
|
|
HRESULT hRes;
|
|
|
|
|
|
|
|
// poll the Controller to read the current state
|
|
|
|
hRes = cDIDevice->Poll();
|
|
|
|
if (FAILED(hRes)) {
|
|
|
|
printfDebug("Poll fail\n");
|
|
|
|
|
|
|
|
//check if we need to reaquire
|
|
|
|
if(hRes == DIERR_INPUTLOST || hRes == DIERR_NOTACQUIRED) {
|
|
|
|
cDIDevice->Acquire();
|
|
|
|
printfDebug("DIERR_INPUTLOST, reaquiring input : cCreate_success=%d\n", cCreate_success);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateControllerFields(env, clazz);
|
2002-10-19 10:27:26 -04:00
|
|
|
}
|
|
|
|
|
2002-11-17 13:13:46 -05:00
|
|
|
/**
|
|
|
|
* Shutdown DI
|
2002-10-19 10:27:26 -04:00
|
|
|
*/
|
2003-09-30 07:14:06 -04:00
|
|
|
static void ShutdownController() {
|
2003-12-15 06:49:17 -05:00
|
|
|
// release device
|
|
|
|
if (cDIDevice != NULL) {
|
|
|
|
cDIDevice->Unacquire();
|
|
|
|
cDIDevice->Release();
|
|
|
|
cDIDevice = NULL;
|
|
|
|
}
|
2002-10-19 10:27:26 -04:00
|
|
|
}
|
|
|
|
|
2002-11-17 13:13:46 -05:00
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Enumerates the capabilities of the Controller attached to the system
|
2002-10-19 10:27:26 -04:00
|
|
|
*/
|
2003-09-30 07:14:06 -04:00
|
|
|
static void EnumerateControllerCapabilities() {
|
2003-12-15 06:49:17 -05:00
|
|
|
HRESULT hr;
|
|
|
|
hr = cDIDevice->EnumObjects(EnumControllerObjectsCallback, NULL, DIDFT_ALL);
|
|
|
|
if FAILED(hr) {
|
|
|
|
printfDebug("EnumObjects failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cCreate_success = true;
|
2002-10-19 10:27:26 -04:00
|
|
|
}
|
|
|
|
|
2002-11-17 13:13:46 -05:00
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Enumerates the Controllers attached to the system
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-09-30 07:14:06 -04:00
|
|
|
static void EnumerateControllers() {
|
2003-12-15 06:49:17 -05:00
|
|
|
HRESULT hr;
|
|
|
|
hr = cDI->EnumDevices(DIDEVTYPE_JOYSTICK, EnumControllerCallback, 0, DIEDFL_ATTACHEDONLY);
|
|
|
|
if FAILED(hr) {
|
|
|
|
printfDebug("EnumDevices failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cCreate_success = true;
|
2002-10-19 10:27:26 -04:00
|
|
|
}
|
|
|
|
|
2002-11-17 13:13:46 -05:00
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Callback from EnumDevices. Called for each Controller attached to the system
|
2002-10-19 10:27:26 -04:00
|
|
|
*/
|
2003-01-03 13:47:42 -05:00
|
|
|
BOOL CALLBACK EnumControllerCallback(LPCDIDEVICEINSTANCE pdinst, LPVOID pvRef) {
|
2003-12-15 06:49:17 -05:00
|
|
|
/* Add the Controller */
|
|
|
|
CreateController(pdinst);
|
2002-11-18 01:16:12 -05:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
/* just stop after 1st Controller */
|
|
|
|
return DIENUM_STOP;
|
2002-10-19 10:27:26 -04:00
|
|
|
}
|
2002-11-17 13:13:46 -05:00
|
|
|
|
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Callback from EnumObjects. Called for each "object" on the Controller.
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-01-03 13:47:42 -05:00
|
|
|
BOOL CALLBACK EnumControllerObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) {
|
2003-12-15 06:49:17 -05:00
|
|
|
printfDebug("found %s\n", lpddoi->tszName);
|
|
|
|
if(lpddoi->guidType == GUID_Button) {
|
|
|
|
cButtoncount++;
|
|
|
|
} else if(lpddoi->guidType == GUID_XAxis) {
|
|
|
|
cHasx = true;
|
|
|
|
} else if(lpddoi->guidType == GUID_YAxis) {
|
|
|
|
cHasy = true;
|
|
|
|
} else if(lpddoi->guidType == GUID_ZAxis){
|
|
|
|
cHasz = true;
|
|
|
|
} else if (lpddoi->guidType == GUID_POV){
|
|
|
|
cHaspov = true;
|
|
|
|
} else if (lpddoi->guidType == GUID_Slider){
|
|
|
|
cHasslider = true;
|
|
|
|
} else if (lpddoi->guidType == GUID_RxAxis) {
|
|
|
|
cHasrx = true;
|
|
|
|
} else if (lpddoi->guidType == GUID_RyAxis) {
|
|
|
|
cHasry = true;
|
|
|
|
} else if (lpddoi->guidType == GUID_RzAxis) {
|
|
|
|
cHasrz = true;
|
|
|
|
} else {
|
|
|
|
printfDebug("Unhandled object found: %s\n", lpddoi->tszName);
|
|
|
|
}
|
|
|
|
return DIENUM_CONTINUE;
|
2002-11-17 13:13:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Creates the specified device as a Controller
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-09-30 07:14:06 -04:00
|
|
|
static void CreateController(LPCDIDEVICEINSTANCE lpddi) {
|
2003-12-15 06:49:17 -05:00
|
|
|
HRESULT hr;
|
|
|
|
hr = cDI->CreateDevice(lpddi->guidInstance, (LPDIRECTINPUTDEVICE*) &cDIDevice, NULL);
|
|
|
|
if FAILED(hr) {
|
|
|
|
printfDebug("CreateDevice failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cCreate_success = true;
|
2002-11-17 13:13:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Sets up the Controller properties
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-09-30 07:14:06 -04:00
|
|
|
static void SetupController() {
|
2003-12-15 06:49:17 -05:00
|
|
|
// set Controller data format
|
|
|
|
if(cDIDevice->SetDataFormat(&c_dfDIJoystick2) != DI_OK) {
|
|
|
|
printfDebug("SetDataFormat failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the cooperative level
|
|
|
|
if(cDIDevice->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND) != DI_OK) {
|
|
|
|
printfDebug("SetCooperativeLevel failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set range to (-1000 ... +1000)
|
|
|
|
// This lets us test against 0 to see which way the stick is pointed.
|
|
|
|
DIPROPRANGE diprg;
|
|
|
|
diprg.diph.dwSize = sizeof(diprg);
|
|
|
|
diprg.diph.dwHeaderSize = sizeof(diprg.diph);
|
|
|
|
diprg.diph.dwHow = DIPH_BYOFFSET;
|
|
|
|
diprg.lMin = CONTROLLER_AXISMIN;
|
|
|
|
diprg.lMax = CONTROLLER_AXISMAX;
|
|
|
|
|
|
|
|
// set X-axis
|
|
|
|
if(cHasx) {
|
|
|
|
diprg.diph.dwObj = DIJOFS_X;
|
|
|
|
if(cDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
|
|
|
|
printfDebug("SetProperty(DIJOFS_X) failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set RX-axis
|
|
|
|
if(cHasrx) {
|
|
|
|
diprg.diph.dwObj = DIJOFS_RX;
|
|
|
|
if(cDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
|
|
|
|
printfDebug("SetProperty(DIJOFS_RX) failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set Y-axis
|
|
|
|
if(cHasy) {
|
|
|
|
diprg.diph.dwObj = DIJOFS_Y;
|
|
|
|
if(cDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
|
|
|
|
printfDebug("SetProperty(DIJOFS_Y) failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set RY-axis
|
|
|
|
if(cHasry) {
|
|
|
|
diprg.diph.dwObj = DIJOFS_RY;
|
|
|
|
if(cDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
|
|
|
|
printfDebug("SetProperty(DIJOFS_RY) failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set Z-axis
|
|
|
|
if(cHasz) {
|
|
|
|
diprg.diph.dwObj = DIJOFS_Z;
|
|
|
|
if(cDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
|
|
|
|
printfDebug("SetProperty(DIJOFS_Z) failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set RZ-axis
|
|
|
|
if(cHasrz) {
|
|
|
|
diprg.diph.dwObj = DIJOFS_RZ;
|
|
|
|
if(cDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
|
|
|
|
printfDebug("SetProperty(DIJOFS_RZ) failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Lastly slider
|
|
|
|
// using z axis since we're running dx 5
|
|
|
|
//
|
|
|
|
if(cHasslider) {
|
|
|
|
diprg.diph.dwObj = DIJOFS_Z;
|
|
|
|
if(cDIDevice->SetProperty(DIPROP_RANGE, &diprg.diph) != DI_OK) {
|
|
|
|
printfDebug("SetProperty(DIJOFS_Z(SLIDER)) failed\n");
|
|
|
|
cCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cCreate_success = true;
|
2002-11-17 13:13:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Sets the fields on the Controller
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-10-11 12:29:40 -04:00
|
|
|
static void InitializeControllerFields(JNIEnv *env, jclass clsController) {
|
2003-12-15 06:49:17 -05:00
|
|
|
//create buttons array
|
|
|
|
jbooleanArray cButtonsArray = env->NewBooleanArray(cButtoncount);
|
|
|
|
|
|
|
|
//set buttons array
|
|
|
|
env->SetStaticObjectField(clsController, fidCButtons, cButtonsArray);
|
2002-11-17 13:13:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Updates the fields on the Controller
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-10-11 12:29:40 -04:00
|
|
|
static void UpdateControllerFields(JNIEnv *env, jclass clsController) {
|
2003-12-15 06:49:17 -05:00
|
|
|
HRESULT hRes;
|
|
|
|
|
|
|
|
// get data from the Controller
|
|
|
|
hRes = cDIDevice->GetDeviceState(sizeof(DIJOYSTATE2), &cJS);
|
|
|
|
|
|
|
|
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 || hRes == DIERR_NOTACQUIRED) {
|
|
|
|
cDIDevice->Acquire();
|
|
|
|
printfDebug("DIERR_INPUTLOST, reaquiring input : cCreate_success=%d\n", cCreate_success);
|
|
|
|
}
|
|
|
|
printfDebug("Error getting controller state: %d\n", hRes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//axis's
|
|
|
|
if(cHasx) {
|
|
|
|
env->SetStaticIntField(clsController, fidCX, cJS.lX);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cHasy) {
|
|
|
|
env->SetStaticIntField(clsController, fidCY, cJS.lY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cHasz) {
|
|
|
|
env->SetStaticIntField(clsController, fidCZ, cJS.lZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
//rotational axis
|
|
|
|
if(cHasrx) {
|
|
|
|
env->SetStaticIntField(clsController, fidCRX, cJS.lRx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cHasry) {
|
|
|
|
env->SetStaticIntField(clsController, fidCRY, cJS.lRy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cHasrz) {
|
|
|
|
env->SetStaticIntField(clsController, fidCRZ, cJS.lRz);
|
|
|
|
}
|
|
|
|
|
|
|
|
//buttons
|
|
|
|
for (int i = 0; i < cButtoncount; i++) {
|
|
|
|
if (cJS.rgbButtons[i] != 0) {
|
|
|
|
cJS.rgbButtons[i] = 1;
|
|
|
|
} else {
|
|
|
|
cJS.rgbButtons[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jbyteArray buttonsArray = (jbyteArray) env->GetStaticObjectField(clsController, fidCButtons);
|
|
|
|
env->SetByteArrayRegion(buttonsArray, 0, cButtoncount, (jbyte *)cJS.rgbButtons);
|
|
|
|
|
|
|
|
//pov
|
|
|
|
if(cHaspov) {
|
|
|
|
env->SetStaticIntField(clsController, fidCPOV, cJS.rgdwPOV[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//slider
|
|
|
|
if(cHasslider) {
|
|
|
|
env->SetStaticIntField(clsController, fidCSlider, cJS.lZ);
|
|
|
|
}
|
2002-11-17 13:13:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-03 13:47:42 -05:00
|
|
|
* Sets the capabilities of the Controller
|
2002-11-17 13:13:46 -05:00
|
|
|
*/
|
2003-10-11 12:29:40 -04:00
|
|
|
static void SetControllerCapabilities(JNIEnv *env, jclass clsController) {
|
2003-12-15 06:49:17 -05:00
|
|
|
//set buttoncount
|
|
|
|
env->SetStaticIntField(clsController, fidCButtonCount, cButtoncount);
|
2002-11-17 13:13:46 -05:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
//set axis
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasXAxis, cHasx);
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasYAxis, cHasy);
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasZAxis, cHasz);
|
2002-11-17 13:13:46 -05:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
//set rotational axis
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasRXAxis, cHasrx);
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasRYAxis, cHasry);
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasRZAxis, cHasrz);
|
2003-01-23 16:38:33 -05:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
//set pov
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasPOV, cHaspov);
|
2003-01-23 16:11:16 -05:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
//set slider
|
|
|
|
env->SetStaticBooleanField(clsController, fidCHasSlider, cHasslider);
|
2002-11-17 13:13:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caches the field ids for quicker access
|
|
|
|
*/
|
2003-10-11 12:29:40 -04:00
|
|
|
static void CacheControllerFields(JNIEnv *env, jclass clsController) {
|
2003-12-15 06:49:17 -05:00
|
|
|
fidCButtonCount = env->GetStaticFieldID(clsController, "buttonCount", "I");
|
|
|
|
fidCHasXAxis = env->GetStaticFieldID(clsController, "hasXAxis", "Z");
|
|
|
|
fidCHasRXAxis = env->GetStaticFieldID(clsController, "hasRXAxis", "Z");
|
|
|
|
fidCHasYAxis = env->GetStaticFieldID(clsController, "hasYAxis", "Z");
|
|
|
|
fidCHasRYAxis = env->GetStaticFieldID(clsController, "hasRYAxis", "Z");
|
|
|
|
fidCHasZAxis = env->GetStaticFieldID(clsController, "hasZAxis", "Z");
|
|
|
|
fidCHasRZAxis = env->GetStaticFieldID(clsController, "hasRZAxis", "Z");
|
|
|
|
fidCHasPOV = env->GetStaticFieldID(clsController, "hasPOV", "Z");
|
|
|
|
fidCHasSlider = env->GetStaticFieldID(clsController, "hasSlider", "Z");
|
|
|
|
fidCButtons = env->GetStaticFieldID(clsController, "buttons", "[Z");
|
|
|
|
fidCX = env->GetStaticFieldID(clsController, "x", "I");
|
|
|
|
fidCRX = env->GetStaticFieldID(clsController, "rx", "I");
|
|
|
|
fidCY = env->GetStaticFieldID(clsController, "y", "I");
|
|
|
|
fidCRY = env->GetStaticFieldID(clsController, "ry", "I");
|
|
|
|
fidCZ = env->GetStaticFieldID(clsController, "z", "I");
|
|
|
|
fidCRZ = env->GetStaticFieldID(clsController, "rz", "I");
|
|
|
|
fidCPOV = env->GetStaticFieldID(clsController, "pov", "I");
|
|
|
|
fidCSlider = env->GetStaticFieldID(clsController, "slider", "I");
|
2003-09-30 07:14:06 -04:00
|
|
|
}
|