2003-02-08 14:30:23 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 Lightweight Java Game Library Project
|
2002-08-15 11:46:18 -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
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2002-08-15 11:46:18 -04:00
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2002-08-15 11:46:18 -04:00
|
|
|
* * Neither the name of 'Light Weight Java Game Library' nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2003-02-08 14:30:23 -05:00
|
|
|
* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
2002-08-15 11:46:18 -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$
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2002-08-15 11:46:18 -04:00
|
|
|
* Win32 mouse handling.
|
2002-08-11 07:49:32 -04:00
|
|
|
*
|
2003-02-08 14:30:23 -05:00
|
|
|
* @author Brian Matzon <brian@matzon.com>
|
2002-08-15 11:46:18 -04:00
|
|
|
* @version $Revision$
|
2002-08-11 07:49:32 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include "org_lwjgl_input_Mouse.h"
|
2003-02-08 14:30:23 -05:00
|
|
|
#include <windows.h>
|
2002-08-11 07:49:32 -04:00
|
|
|
#undef DIRECTINPUT_VERSION
|
|
|
|
#define DIRECTINPUT_VERSION 0x0300
|
|
|
|
#include <dinput.h>
|
|
|
|
|
2003-02-08 14:30:23 -05:00
|
|
|
extern HINSTANCE dll_handle;
|
2002-08-11 07:49:32 -04:00
|
|
|
|
2003-02-09 14:18:31 -05:00
|
|
|
extern HWND hwnd; // Handle to window
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2003-02-09 14:18:31 -05:00
|
|
|
extern LPDIRECTINPUT lpdi; // DI instance
|
|
|
|
LPDIRECTINPUTDEVICE mDIDevice; // DI Device instance
|
|
|
|
DIMOUSESTATE diMouseState; // State of Mouse
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2003-02-09 14:18:31 -05:00
|
|
|
int mButtoncount = 0; // Temporary buttoncount
|
|
|
|
bool mHaswheel; // Temporary wheel check
|
|
|
|
JNIEnv* mEnvironment; // JNIEnvironment copy
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2003-02-09 14:18:31 -05:00
|
|
|
bool mCreate_success; // bool used to determine successfull creation
|
2003-02-09 14:51:53 -05:00
|
|
|
bool mFirstTimeInitialization = true; // boolean to determine first time initialization
|
2003-02-08 14:30:23 -05:00
|
|
|
|
|
|
|
// Cached fields of Mouse.java
|
|
|
|
jclass clsMouse;
|
|
|
|
jfieldID fidMButtonCount;
|
|
|
|
jfieldID fidMButtons;
|
|
|
|
jfieldID fidMDX;
|
|
|
|
jfieldID fidMDY;
|
|
|
|
jfieldID fidMDWheel;
|
|
|
|
jfieldID fidMHasWheel;
|
|
|
|
|
|
|
|
// Function prototypes (defined in the cpp file, since header file is generic across platforms
|
|
|
|
void EnumerateMouseCapabilities();
|
|
|
|
BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef);
|
|
|
|
void ShutdownMouse();
|
|
|
|
void CreateMouse();
|
|
|
|
void SetupMouse();
|
|
|
|
void InitializeMouseFields();
|
|
|
|
void CacheMouseFields();
|
|
|
|
void UpdateMouseFields();
|
|
|
|
void SetMouseCapabilities();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes any field ids
|
2002-08-11 07:49:32 -04:00
|
|
|
*/
|
2003-02-08 14:30:23 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_initIDs(JNIEnv * env, jclass clazz) {
|
|
|
|
mEnvironment = env;
|
|
|
|
clsMouse = clazz;
|
|
|
|
|
|
|
|
/* Cache fields in Mouse */
|
|
|
|
CacheMouseFields();
|
2002-08-11 07:49:32 -04:00
|
|
|
}
|
|
|
|
|
2003-02-08 14:30:23 -05:00
|
|
|
/**
|
|
|
|
* Called when the Mouse instance is to be created
|
2002-08-11 07:49:32 -04:00
|
|
|
*/
|
2003-02-08 14:30:23 -05:00
|
|
|
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclass clazz) {
|
|
|
|
HRESULT hr;
|
|
|
|
|
2003-03-18 11:38:31 -05:00
|
|
|
mEnvironment = env;
|
|
|
|
clsMouse = clazz;
|
|
|
|
|
|
|
|
CacheMouseFields();
|
|
|
|
|
2003-02-08 14:30:23 -05:00
|
|
|
/* skip enumeration, since we only want system mouse */
|
|
|
|
CreateMouse();
|
|
|
|
|
2003-02-09 14:18:31 -05:00
|
|
|
//check for first time initialization - need to detect capabilities
|
2003-02-09 14:51:53 -05:00
|
|
|
if (mFirstTimeInitialization) {
|
|
|
|
mFirstTimeInitialization = false;
|
2003-02-09 14:18:31 -05:00
|
|
|
|
|
|
|
/* Enumerate capabilities of Mouse */
|
|
|
|
EnumerateMouseCapabilities();
|
|
|
|
if (!mCreate_success) {
|
|
|
|
#if _DEBUG
|
|
|
|
printf("EnumerateMouseCapabilities failed\n");
|
|
|
|
#endif
|
|
|
|
ShutdownMouse();
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-03-23 15:00:28 -05:00
|
|
|
/* Do setup of Mouse */
|
|
|
|
SetupMouse();
|
2003-02-09 14:18:31 -05:00
|
|
|
|
|
|
|
/* Set capabilities */
|
|
|
|
SetMouseCapabilities();
|
|
|
|
} else {
|
|
|
|
if(mCreate_success) {
|
|
|
|
/* Do setup of Mouse */
|
2003-03-23 15:00:28 -05:00
|
|
|
SetupMouse();
|
2003-02-09 14:18:31 -05:00
|
|
|
}
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Aquire the Mouse */
|
|
|
|
hr = mDIDevice->Acquire();
|
|
|
|
if(FAILED(hr)) {
|
|
|
|
#if _DEBUG
|
2003-03-01 16:39:31 -05:00
|
|
|
printf("Failed to acquire mouse\n");
|
2002-08-11 07:49:32 -04:00
|
|
|
#endif
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
2003-02-09 14:18:31 -05:00
|
|
|
|
2003-03-06 17:55:13 -05:00
|
|
|
return mCreate_success ? JNI_TRUE : JNI_FALSE;
|
2002-08-11 07:49:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Mouse
|
|
|
|
* Method: nDestroy
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
2003-02-08 14:30:23 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy(JNIEnv *env, jclass clazz) {
|
2003-03-18 11:38:31 -05:00
|
|
|
mEnvironment = env;
|
|
|
|
clsMouse = clazz;
|
2003-02-08 14:30:23 -05:00
|
|
|
ShutdownMouse();
|
2002-08-11 07:49:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-02-08 14:30:23 -05:00
|
|
|
* Class: org_lwjgl_input_Controller
|
2002-08-11 07:49:32 -04:00
|
|
|
* Method: nPoll
|
|
|
|
* Signature: ()V
|
|
|
|
*/
|
2003-02-08 14:30:23 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz) {
|
2003-03-01 16:39:31 -05:00
|
|
|
mDIDevice->Acquire();
|
2003-03-18 11:38:31 -05:00
|
|
|
mEnvironment = env;
|
|
|
|
clsMouse = clazz;
|
2003-02-08 14:30:23 -05:00
|
|
|
UpdateMouseFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutdown DI
|
|
|
|
*/
|
|
|
|
void ShutdownMouse() {
|
|
|
|
// release device
|
|
|
|
if (mDIDevice != NULL) {
|
|
|
|
mDIDevice->Unacquire();
|
|
|
|
mDIDevice->Release();
|
2003-03-23 15:00:28 -05:00
|
|
|
mDIDevice = NULL;
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Enumerates the capabilities of the Mouse attached to the system
|
|
|
|
*/
|
|
|
|
void EnumerateMouseCapabilities() {
|
|
|
|
HRESULT hr;
|
|
|
|
hr = mDIDevice->EnumObjects(EnumMouseObjectsCallback, NULL, DIDFT_ALL);
|
|
|
|
if FAILED(hr) {
|
|
|
|
#if _DEBUG
|
|
|
|
printf("EnumObjects failed\n");
|
2002-08-11 07:49:32 -04:00
|
|
|
#endif
|
2003-02-08 14:30:23 -05:00
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
2003-03-23 15:00:28 -05:00
|
|
|
|
|
|
|
//check for > 4 buttons - need to clamp since we're using dx 5
|
|
|
|
if(mButtoncount > 4) {
|
2003-03-23 15:08:49 -05:00
|
|
|
mButtoncount = 4;
|
2003-03-23 15:00:28 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("WARNING: Clamping to 4 mouse buttons");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-02-08 14:30:23 -05:00
|
|
|
mCreate_success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback from EnumObjects. Called for each "object" on the Mouse.
|
|
|
|
*/
|
|
|
|
BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) {
|
|
|
|
#if _DEBUG
|
|
|
|
printf("found %s\n", lpddoi->tszName);
|
|
|
|
#endif
|
|
|
|
if(lpddoi->guidType == GUID_Button) {
|
|
|
|
mButtoncount++;
|
|
|
|
} else if(lpddoi->guidType == GUID_XAxis) {
|
|
|
|
} else if(lpddoi->guidType == GUID_YAxis) {
|
|
|
|
} else if(lpddoi->guidType == GUID_ZAxis) {
|
|
|
|
mHaswheel = true;
|
|
|
|
#if _DEBUG
|
|
|
|
} else {
|
|
|
|
printf("Unhandled object found: %s\n", lpddoi->tszName);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return DIENUM_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the specified device as a Mouse
|
|
|
|
*/
|
|
|
|
void CreateMouse() {
|
|
|
|
HRESULT hr;
|
|
|
|
hr = lpdi->CreateDevice(GUID_SysMouse, &mDIDevice, NULL);
|
|
|
|
if FAILED(hr) {
|
|
|
|
#if _DEBUG
|
|
|
|
printf("CreateDevice failed\n");
|
|
|
|
#endif
|
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCreate_success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the Mouse properties
|
|
|
|
*/
|
|
|
|
void SetupMouse() {
|
|
|
|
// set Mouse data format
|
|
|
|
if(mDIDevice->SetDataFormat(&c_dfDIMouse) != DI_OK) {
|
|
|
|
#if _DEBUG
|
|
|
|
printf("SetDataFormat failed\n");
|
|
|
|
#endif
|
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
2002-08-11 07:49:32 -04:00
|
|
|
|
2003-02-08 14:30:23 -05:00
|
|
|
// set the cooperative level
|
|
|
|
if(mDIDevice->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND) != DI_OK) {
|
|
|
|
#if _DEBUG
|
|
|
|
printf("SetCooperativeLevel failed\n");
|
|
|
|
#endif
|
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCreate_success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the fields on the Mouse
|
|
|
|
*/
|
|
|
|
void UpdateMouseFields() {
|
|
|
|
HRESULT hRes;
|
|
|
|
|
|
|
|
// get data from the Mouse
|
2003-02-12 07:11:05 -05:00
|
|
|
hRes = mDIDevice->GetDeviceState(sizeof(DIMOUSESTATE), &diMouseState);
|
2003-02-08 14:30:23 -05:00
|
|
|
if (hRes != DI_OK) {
|
2003-02-12 07:11:05 -05:00
|
|
|
// Don't allow the mouse to drift when failed
|
|
|
|
diMouseState.lX = 0;
|
|
|
|
diMouseState.lY = 0;
|
|
|
|
diMouseState.lZ = 0;
|
2003-02-08 14:30:23 -05:00
|
|
|
// did the read fail because we lost input for some reason?
|
|
|
|
// if so, then attempt to reacquire.
|
|
|
|
if(hRes == DIERR_INPUTLOST || hRes == DIERR_NOTACQUIRED) {
|
|
|
|
mDIDevice->Acquire();
|
|
|
|
#if _DEBUG
|
|
|
|
printf("DIERR_INPUTLOST, reaquiring input : mCreate_success=%d\n", mCreate_success);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#if _DEBUG
|
|
|
|
printf("Error getting mouse state: %d\n", hRes);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mEnvironment->SetStaticIntField(clsMouse, fidMDX, (jint) diMouseState.lX);
|
|
|
|
mEnvironment->SetStaticIntField(clsMouse, fidMDY, (jint) diMouseState.lY);
|
|
|
|
mEnvironment->SetStaticIntField(clsMouse, fidMDWheel, (jint) diMouseState.lZ);
|
|
|
|
|
|
|
|
for (int i = 0; i < mButtoncount; i++) {
|
|
|
|
if (diMouseState.rgbButtons[i] != 0) {
|
|
|
|
diMouseState.rgbButtons[i] = JNI_TRUE;
|
|
|
|
} else {
|
|
|
|
diMouseState.rgbButtons[i] = JNI_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2003-03-23 15:00:28 -05:00
|
|
|
jbooleanArray mButtonsArray = (jbooleanArray) mEnvironment->GetStaticObjectField(clsMouse, fidMButtons);
|
|
|
|
mEnvironment->SetBooleanArrayRegion(mButtonsArray, 0, mButtoncount, diMouseState.rgbButtons);
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the capabilities of the Mouse
|
|
|
|
*/
|
|
|
|
void SetMouseCapabilities() {
|
|
|
|
//set buttoncount
|
|
|
|
mEnvironment->SetStaticIntField(clsMouse, fidMButtonCount, mButtoncount);
|
|
|
|
|
|
|
|
//set wheel
|
|
|
|
mEnvironment->SetStaticBooleanField(clsMouse, fidMHasWheel, mHaswheel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caches the field ids for quicker access
|
|
|
|
*/
|
|
|
|
void CacheMouseFields() {
|
|
|
|
fidMButtonCount = mEnvironment->GetStaticFieldID(clsMouse, "buttonCount", "I");
|
|
|
|
fidMHasWheel = mEnvironment->GetStaticFieldID(clsMouse, "hasWheel", "Z");
|
|
|
|
fidMButtons = mEnvironment->GetStaticFieldID(clsMouse, "buttons", "[Z");
|
|
|
|
fidMDX = mEnvironment->GetStaticFieldID(clsMouse, "dx", "I");
|
|
|
|
fidMDY = mEnvironment->GetStaticFieldID(clsMouse, "dy", "I");
|
|
|
|
fidMDWheel = mEnvironment->GetStaticFieldID(clsMouse, "dwheel", "I");
|
2003-03-23 15:08:49 -05:00
|
|
|
}
|