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
|
2003-12-15 06:49:17 -05:00
|
|
|
* 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
|
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-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
|
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.
|
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>
|
2003-12-15 06:49:17 -05:00
|
|
|
#undef DIRECTINPUT_VERSION
|
2002-08-11 07:49:32 -04:00
|
|
|
#define DIRECTINPUT_VERSION 0x0300
|
2003-03-27 20:28:04 -05:00
|
|
|
#include "Window.h"
|
2003-10-11 12:29:40 -04:00
|
|
|
#include "common_tools.h"
|
2002-08-11 07:49:32 -04:00
|
|
|
#include <dinput.h>
|
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
static LPDIRECTINPUTDEVICE mDIDevice; // DI Device instance
|
|
|
|
static int mButtoncount = 0; // Temporary buttoncount
|
|
|
|
static bool mHaswheel; // Temporary wheel check
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2003-12-15 06:49:17 -05:00
|
|
|
static bool mCreate_success; // bool used to determine successfull creation
|
2003-05-16 14:39:46 -04:00
|
|
|
static bool mFirstTimeInitialization = true; // boolean to determine first time initialization
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
static POINT cursorPos;
|
|
|
|
static RECT windowRect;
|
|
|
|
static bool usingNativeCursor;
|
2004-04-11 20:33:01 -04:00
|
|
|
static int mouseMask = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
|
2003-02-08 14:30:23 -05:00
|
|
|
|
|
|
|
// 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();
|
2004-04-06 10:23:19 -04:00
|
|
|
void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj);
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
static void getScreenClientRect(RECT* clientRect, RECT* windowRect)
|
|
|
|
{
|
|
|
|
GetClientRect(hwnd, clientRect);
|
|
|
|
// transform clientRect to screen coordinates
|
|
|
|
clientRect->top = -clientSize.top + windowRect->top;
|
|
|
|
clientRect->left = -clientSize.left + windowRect->left;
|
|
|
|
clientRect->bottom += clientRect->top;
|
|
|
|
clientRect->right += clientRect->left;
|
|
|
|
}
|
|
|
|
|
2003-10-07 09:10:17 -04:00
|
|
|
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasWheel(JNIEnv *, jclass) {
|
2003-10-07 10:28:33 -04:00
|
|
|
return mHaswheel;
|
2003-10-07 09:10:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetButtonCount(JNIEnv *, jclass) {
|
2003-10-07 10:28:33 -04:00
|
|
|
return mButtoncount;
|
2003-10-07 09:10:17 -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-09-30 07:14:06 -04:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclass clazz) {
|
2003-05-16 14:39:46 -04:00
|
|
|
HRESULT hr;
|
2003-03-18 11:38:31 -05:00
|
|
|
|
2004-01-12 11:28:51 -05:00
|
|
|
// assert that Direct Input has been created
|
|
|
|
if(lpdi == NULL) {
|
2004-01-12 12:24:47 -05:00
|
|
|
throwException(env, "Please create the window before initializing input devices");
|
2004-01-12 11:28:51 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
/* skip enumeration, since we only want system mouse */
|
|
|
|
CreateMouse();
|
2003-02-09 14:18:31 -05:00
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
//check for first time initialization - need to detect capabilities
|
|
|
|
if (mFirstTimeInitialization) {
|
|
|
|
mFirstTimeInitialization = false;
|
|
|
|
/* Enumerate capabilities of Mouse */
|
|
|
|
EnumerateMouseCapabilities();
|
|
|
|
if (!mCreate_success) {
|
2003-09-30 07:14:06 -04:00
|
|
|
throwException(env, "Failed to enumerate.");
|
2003-05-16 14:39:46 -04:00
|
|
|
ShutdownMouse();
|
2003-09-30 07:14:06 -04:00
|
|
|
return;
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
|
|
|
/* Do setup of Mouse */
|
|
|
|
SetupMouse();
|
|
|
|
} else {
|
|
|
|
if(mCreate_success) {
|
|
|
|
/* Do setup of Mouse */
|
2003-12-15 06:49:17 -05:00
|
|
|
SetupMouse();
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Aquire the Mouse */
|
|
|
|
hr = mDIDevice->Acquire();
|
|
|
|
if(FAILED(hr)) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("Failed to acquire mouse\n");
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
2003-02-09 14:18:31 -05:00
|
|
|
|
2004-03-26 16:49:10 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nEnableBuffer(JNIEnv * env, jclass clazz) {
|
2003-10-11 12:29:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char mapButton(DWORD button_id) {
|
|
|
|
switch (button_id) {
|
|
|
|
case DIMOFS_BUTTON0: return 0;
|
|
|
|
case DIMOFS_BUTTON1: return 1;
|
|
|
|
case DIMOFS_BUTTON2: return 2;
|
|
|
|
case DIMOFS_BUTTON3: return 3;
|
|
|
|
/* case DIMOFS_BUTTON4: return 4;
|
|
|
|
case DIMOFS_BUTTON5: return 5;
|
|
|
|
case DIMOFS_BUTTON6: return 6;
|
|
|
|
case DIMOFS_BUTTON7: return 7;*/
|
|
|
|
default: return mButtoncount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-26 16:49:10 -05:00
|
|
|
static int bufferButtons(int num_di_events, DIDEVICEOBJECTDATA *di_buffer, unsigned char *buffer, int buffer_size) {
|
2003-10-11 12:29:40 -04:00
|
|
|
int buffer_index = 0;
|
2004-03-26 16:49:10 -05:00
|
|
|
for (int i = 0; i < num_di_events; i++) {
|
2003-10-11 12:29:40 -04:00
|
|
|
unsigned char button = mapButton(di_buffer[i].dwOfs);
|
|
|
|
if (button >= 0 && button < mButtoncount) {
|
|
|
|
unsigned char state = (unsigned char)di_buffer[i].dwData & 0x80;
|
|
|
|
if (state != 0)
|
|
|
|
state = 1;
|
2004-03-26 16:49:10 -05:00
|
|
|
if (buffer_index == buffer_size)
|
2003-10-11 12:29:40 -04:00
|
|
|
break;
|
2004-04-04 03:37:22 -04:00
|
|
|
buffer[buffer_index++] = button;
|
|
|
|
buffer[buffer_index++] = state;
|
2003-10-11 12:29:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer_index/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead
|
2004-03-26 16:49:10 -05:00
|
|
|
(JNIEnv * env, jclass clazz, jobject buffer_obj, jint buffer_position)
|
2003-10-11 12:29:40 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
static DIDEVICEOBJECTDATA rgdod[EVENT_BUFFER_SIZE];
|
2004-03-26 16:49:10 -05:00
|
|
|
DWORD num_di_events = EVENT_BUFFER_SIZE;
|
2003-10-11 12:29:40 -04:00
|
|
|
|
|
|
|
HRESULT ret;
|
|
|
|
|
|
|
|
ret = mDIDevice->Acquire();
|
|
|
|
if (ret != DI_OK && ret != S_FALSE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = mDIDevice->GetDeviceData(
|
|
|
|
sizeof(DIDEVICEOBJECTDATA),
|
|
|
|
rgdod,
|
2004-03-26 16:49:10 -05:00
|
|
|
&num_di_events,
|
2003-10-11 12:29:40 -04:00
|
|
|
0);
|
|
|
|
|
|
|
|
if (ret == DI_OK) {
|
2004-03-26 16:49:10 -05:00
|
|
|
unsigned char *buffer_ptr = buffer_position + (unsigned char*)env->GetDirectBufferAddress(buffer_obj);
|
|
|
|
int buffer_size = (int)env->GetDirectBufferCapacity(buffer_obj) - buffer_position;
|
|
|
|
return bufferButtons(num_di_events, rgdod, buffer_ptr, buffer_size);
|
2003-10-11 12:29:40 -04:00
|
|
|
} else if (ret == DI_BUFFEROVERFLOW) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("Buffer overflowed\n");
|
2003-10-11 12:29:40 -04:00
|
|
|
} else if (ret == DIERR_INPUTLOST) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("Input lost\n");
|
2003-10-11 12:29:40 -04:00
|
|
|
} else if (ret == DIERR_NOTACQUIRED) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("not acquired\n");
|
2003-10-11 12:29:40 -04:00
|
|
|
} else if (ret == DIERR_INVALIDPARAM) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("invalid parameter\n");
|
2003-10-11 12:29:40 -04:00
|
|
|
} else if (ret == DIERR_NOTBUFFERED) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("not buffered\n");
|
2003-10-11 12:29:40 -04:00
|
|
|
} else if (ret == DIERR_NOTINITIALIZED) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("not inited\n");
|
2003-10-11 12:29:40 -04:00
|
|
|
} else {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("unknown keyboard error\n");
|
2003-10-11 12:29:40 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
/*
|
2003-12-15 06:49:17 -05:00
|
|
|
* Class: org_lwjgl_input_Mouse
|
|
|
|
* Method: nIsNativeCursorSupported
|
2003-05-20 08:20:13 -04:00
|
|
|
* Signature: ()I
|
2003-05-16 14:39:46 -04:00
|
|
|
*/
|
2003-05-20 08:20:13 -04:00
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps
|
2003-12-15 06:49:17 -05:00
|
|
|
(JNIEnv *env, jclass clazz)
|
2003-05-16 14:39:46 -04:00
|
|
|
{
|
2003-08-29 04:00:44 -04:00
|
|
|
return org_lwjgl_input_Mouse_CURSOR_ONE_BIT_TRANSPARENCY;
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
/*
|
2003-12-15 06:49:17 -05:00
|
|
|
* Class: org_lwjgl_input_Mouse
|
|
|
|
* Method: nSetNativeCursor
|
2003-05-16 14:39:46 -04:00
|
|
|
* Signature: (I)V
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor
|
2003-12-15 06:49:17 -05:00
|
|
|
(JNIEnv *env, jclass clazz, jlong cursor_handle)
|
2003-05-16 14:39:46 -04:00
|
|
|
{
|
2003-11-23 07:52:08 -05:00
|
|
|
if (mDIDevice == NULL)
|
|
|
|
throwException(env, "null device!");
|
2003-06-24 08:24:55 -04:00
|
|
|
if (cursor_handle != 0) {
|
2003-05-16 14:39:46 -04:00
|
|
|
HCURSOR cursor = (HCURSOR)cursor_handle;
|
|
|
|
SetClassLong(hwnd, GCL_HCURSOR, (LONG)cursor);
|
|
|
|
SetCursor(cursor);
|
|
|
|
if (!usingNativeCursor) {
|
2003-11-23 07:52:08 -05:00
|
|
|
mDIDevice->Unacquire();
|
|
|
|
if(mDIDevice->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK) {
|
|
|
|
throwException(env, "Could not set the CooperativeLevel.");
|
|
|
|
return;
|
|
|
|
}
|
2003-10-11 07:03:06 -04:00
|
|
|
/* Reset cursor position to middle of the window */
|
2003-05-16 14:39:46 -04:00
|
|
|
RECT clientRect;
|
|
|
|
GetWindowRect(hwnd, &windowRect);
|
|
|
|
getScreenClientRect(&clientRect, &windowRect);
|
2003-10-11 07:03:06 -04:00
|
|
|
cursorPos.x = (clientRect.left + clientRect.right)/2;
|
2003-10-11 08:56:26 -04:00
|
|
|
cursorPos.y = clientRect.bottom - 1 - (clientRect.bottom - clientRect.top)/2;
|
2003-08-11 12:35:45 -04:00
|
|
|
SetCursorPos(cursorPos.x, cursorPos.y);
|
2003-05-16 14:39:46 -04:00
|
|
|
usingNativeCursor = true;
|
|
|
|
}
|
|
|
|
} else {
|
2003-05-20 08:20:13 -04:00
|
|
|
if (usingNativeCursor) {
|
|
|
|
SetClassLong(hwnd, GCL_HCURSOR, (LONG)NULL);
|
2004-04-12 06:37:50 -04:00
|
|
|
SetCursor(LoadCursor(NULL, IDC_ARROW));
|
2003-05-20 08:20:13 -04:00
|
|
|
mDIDevice->Unacquire();
|
2004-04-11 20:33:01 -04:00
|
|
|
if(mDIDevice->SetCooperativeLevel(hwnd, mouseMask) != DI_OK) {
|
2003-05-20 08:20:13 -04:00
|
|
|
throwException(env, "Could not set the CooperativeLevel.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
usingNativeCursor = false;
|
2003-11-23 07:52:08 -05:00
|
|
|
mDIDevice->Acquire();
|
2004-04-12 06:37:50 -04:00
|
|
|
ShowCursor(TRUE);
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-12-15 06:49:17 -05:00
|
|
|
* Class: org_lwjgl_input_Mouse
|
|
|
|
* Method: nGetMaxCursorSize
|
2003-05-16 14:39:46 -04:00
|
|
|
* Signature: ()I
|
|
|
|
*/
|
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMaxCursorSize
|
2003-12-15 06:49:17 -05:00
|
|
|
(JNIEnv *env, jclass clazz)
|
2003-05-16 14:39:46 -04:00
|
|
|
{
|
|
|
|
return GetSystemMetrics(SM_CXCURSOR);
|
|
|
|
}
|
2003-02-09 14:18:31 -05:00
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
/*
|
2003-12-15 06:49:17 -05:00
|
|
|
* Class: org_lwjgl_input_Mouse
|
|
|
|
* Method: nGetMaxCursorSize
|
2003-05-16 14:39:46 -04:00
|
|
|
* Signature: ()I
|
|
|
|
*/
|
|
|
|
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMinCursorSize
|
2003-12-15 06:49:17 -05:00
|
|
|
(JNIEnv *env, jclass clazz)
|
2003-05-16 14:39:46 -04:00
|
|
|
{
|
|
|
|
return GetSystemMetrics(SM_CXCURSOR);
|
2002-08-11 07:49:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-12-15 06:49:17 -05:00
|
|
|
* Class: org_lwjgl_input_Mouse
|
|
|
|
* Method: nDestroy
|
2002-08-11 07:49:32 -04:00
|
|
|
* Signature: ()V
|
|
|
|
*/
|
2003-02-08 14:30:23 -05:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy(JNIEnv *env, jclass clazz) {
|
2003-05-20 08:20:13 -04:00
|
|
|
ShutdownMouse();
|
2002-08-11 07:49:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-04-11 20:33:01 -04:00
|
|
|
* Class: org_lwjgl_input_Mouse
|
2003-12-15 06:49:17 -05:00
|
|
|
* Method: nPoll
|
2002-08-11 07:49:32 -04:00
|
|
|
* Signature: ()V
|
|
|
|
*/
|
2004-04-06 10:23:19 -04:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz, jobject coord_buffer_obj, jobject button_buffer_obj) {
|
2003-12-15 06:49:17 -05:00
|
|
|
mDIDevice->Acquire();
|
2004-04-06 10:23:19 -04:00
|
|
|
UpdateMouseFields(env, clazz, coord_buffer_obj, button_buffer_obj);
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
|
2004-04-11 20:33:01 -04:00
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_input_Mouse
|
|
|
|
* Method: nGrabMouse
|
|
|
|
* Signature: (Z)Z
|
|
|
|
*/
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse
|
|
|
|
(JNIEnv * env, jclass clazz, jboolean grab) {
|
|
|
|
|
|
|
|
if(grab) {
|
|
|
|
mouseMask = DISCL_EXCLUSIVE | DISCL_FOREGROUND;
|
|
|
|
} else {
|
|
|
|
mouseMask = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
|
|
|
|
}
|
|
|
|
mDIDevice->Unacquire();
|
|
|
|
if(mDIDevice->SetCooperativeLevel(hwnd, mouseMask) != DI_OK) {
|
|
|
|
throwException(env, "Could not set the CooperativeLevel.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDIDevice->Acquire();
|
|
|
|
}
|
|
|
|
|
2003-02-08 14:30:23 -05:00
|
|
|
/**
|
|
|
|
* Shutdown DI
|
|
|
|
*/
|
|
|
|
void ShutdownMouse() {
|
2003-12-15 06:49:17 -05:00
|
|
|
// release device
|
|
|
|
if (mDIDevice != NULL) {
|
|
|
|
mDIDevice->Unacquire();
|
|
|
|
mDIDevice->Release();
|
|
|
|
mDIDevice = NULL;
|
|
|
|
}
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Enumerates the capabilities of the Mouse attached to the system
|
|
|
|
*/
|
|
|
|
void EnumerateMouseCapabilities() {
|
2003-05-16 14:39:46 -04:00
|
|
|
HRESULT hr;
|
|
|
|
hr = mDIDevice->EnumObjects(EnumMouseObjectsCallback, NULL, DIDFT_ALL);
|
|
|
|
if FAILED(hr) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("EnumObjects failed\n");
|
2003-05-16 14:39:46 -04:00
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
2003-12-15 06:49:17 -05:00
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
//check for > 4 buttons - need to clamp since we're using dx 5
|
|
|
|
if(mButtoncount > 4) {
|
|
|
|
mButtoncount = 4;
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("WARNING: Clamping to 4 mouse buttons\n");
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
2003-12-15 06:49:17 -05:00
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
mCreate_success = true;
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback from EnumObjects. Called for each "object" on the Mouse.
|
|
|
|
*/
|
|
|
|
BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("found %s\n", lpddoi->tszName);
|
2003-12-15 06:49:17 -05:00
|
|
|
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;
|
|
|
|
} else {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("Unhandled object found: %s\n", lpddoi->tszName);
|
2003-12-15 06:49:17 -05:00
|
|
|
}
|
|
|
|
return DIENUM_CONTINUE;
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the specified device as a Mouse
|
|
|
|
*/
|
|
|
|
void CreateMouse() {
|
2003-12-15 06:49:17 -05:00
|
|
|
HRESULT hr;
|
|
|
|
hr = lpdi->CreateDevice(GUID_SysMouse, &mDIDevice, NULL);
|
|
|
|
if FAILED(hr) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("CreateDevice failed\n");
|
2003-12-15 06:49:17 -05:00
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCreate_success = true;
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the Mouse properties
|
|
|
|
*/
|
|
|
|
void SetupMouse() {
|
2003-12-15 06:49:17 -05:00
|
|
|
// set Mouse data format
|
|
|
|
if(mDIDevice->SetDataFormat(&c_dfDIMouse) != DI_OK) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("SetDataFormat failed\n");
|
2003-12-15 06:49:17 -05:00
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
2002-08-11 07:49:32 -04:00
|
|
|
|
2003-10-11 12:29:40 -04:00
|
|
|
DIPROPDWORD dipropdw;
|
|
|
|
dipropdw.diph.dwSize = sizeof(DIPROPDWORD);
|
|
|
|
dipropdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
|
|
|
dipropdw.diph.dwObj = 0;
|
|
|
|
dipropdw.diph.dwHow = DIPH_DEVICE;
|
|
|
|
dipropdw.dwData = EVENT_BUFFER_SIZE;
|
|
|
|
mDIDevice->SetProperty(DIPROP_BUFFERSIZE, &dipropdw.diph);
|
|
|
|
|
|
|
|
// set the cooperative level
|
2004-04-11 20:33:01 -04:00
|
|
|
if(mDIDevice->SetCooperativeLevel(hwnd, mouseMask) != DI_OK) {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("SetCooperativeLevel failed\n");
|
2003-12-15 06:49:17 -05:00
|
|
|
mCreate_success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCreate_success = true;
|
2003-02-08 14:30:23 -05:00
|
|
|
}
|
|
|
|
|
2003-08-11 12:35:45 -04:00
|
|
|
static int cap(int val, int min, int max) {
|
|
|
|
if (val < min)
|
|
|
|
return min;
|
|
|
|
else if (val > max)
|
|
|
|
return max;
|
|
|
|
else
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
static void getGDICursorDelta(int* return_dx, int* return_dy) {
|
|
|
|
int dx = 0;
|
|
|
|
int dy = 0;
|
|
|
|
|
|
|
|
POINT newCursorPos;
|
|
|
|
GetCursorPos(&newCursorPos);
|
|
|
|
RECT clientRect;
|
|
|
|
RECT newWindowRect;
|
|
|
|
GetWindowRect(hwnd, &newWindowRect);
|
|
|
|
cursorPos.x += newWindowRect.left - windowRect.left;
|
|
|
|
cursorPos.y += newWindowRect.top - windowRect.top;
|
|
|
|
windowRect = newWindowRect;
|
|
|
|
getScreenClientRect(&clientRect, &windowRect);
|
|
|
|
// Clip the position to the client rect
|
2003-08-11 12:35:45 -04:00
|
|
|
newCursorPos.x = cap(newCursorPos.x, clientRect.left, clientRect.right - 1);
|
|
|
|
newCursorPos.y = cap(newCursorPos.y, clientRect.top, clientRect.bottom - 1);
|
|
|
|
dx = newCursorPos.x - cursorPos.x;
|
|
|
|
dy = newCursorPos.y - cursorPos.y;
|
|
|
|
cursorPos.x += dx;
|
|
|
|
cursorPos.y += dy;
|
2003-05-16 14:39:46 -04:00
|
|
|
*return_dx = dx;
|
|
|
|
*return_dy = dy;
|
|
|
|
}
|
|
|
|
|
2003-02-08 14:30:23 -05:00
|
|
|
/**
|
|
|
|
* Updates the fields on the Mouse
|
|
|
|
*/
|
2004-04-06 10:23:19 -04:00
|
|
|
static void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj) {
|
2003-12-15 06:49:17 -05:00
|
|
|
HRESULT hRes;
|
|
|
|
DIMOUSESTATE diMouseState; // State of Mouse
|
2003-05-16 14:39:46 -04:00
|
|
|
int dx, dy;
|
|
|
|
|
2004-04-06 10:23:19 -04:00
|
|
|
int *coords = (int *)env->GetDirectBufferAddress(coord_buffer_obj);
|
2004-04-06 10:40:36 -04:00
|
|
|
int coords_length = (int)env->GetDirectBufferCapacity(coord_buffer_obj);
|
2004-04-06 10:23:19 -04:00
|
|
|
unsigned char *buttons_buffer = (unsigned char *)env->GetDirectBufferAddress(button_buffer_obj);
|
2004-04-06 10:40:36 -04:00
|
|
|
int buttons_length = (int)env->GetDirectBufferCapacity(button_buffer_obj);
|
2004-04-06 10:23:19 -04:00
|
|
|
if (coords_length < 3) {
|
|
|
|
printfDebug("ERROR: Not enough space in coords array: %d < 3\n", coords_length);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
// get data from the Mouse
|
|
|
|
hRes = mDIDevice->GetDeviceState(sizeof(DIMOUSESTATE), &diMouseState);
|
|
|
|
if (hRes != DI_OK) {
|
|
|
|
// Don't allow the mouse to drift when failed
|
|
|
|
diMouseState.lX = 0;
|
|
|
|
diMouseState.lY = 0;
|
|
|
|
diMouseState.lZ = 0;
|
|
|
|
// 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();
|
|
|
|
} else {
|
2003-12-20 17:03:25 -05:00
|
|
|
printfDebug("Error getting mouse state: %d\n", hRes);
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usingNativeCursor) {
|
|
|
|
getGDICursorDelta(&dx, &dy);
|
|
|
|
} else {
|
|
|
|
dx = diMouseState.lX;
|
|
|
|
dy = diMouseState.lY;
|
|
|
|
}
|
2003-08-11 12:35:45 -04:00
|
|
|
dy = -dy;
|
2003-02-08 14:30:23 -05:00
|
|
|
|
2004-04-06 10:23:19 -04:00
|
|
|
coords[0] = dx;
|
2004-04-06 10:40:36 -04:00
|
|
|
coords[1] = dy;
|
|
|
|
coords[2] = diMouseState.lZ;
|
2003-05-16 14:39:46 -04:00
|
|
|
for (int i = 0; i < mButtoncount; i++) {
|
|
|
|
if (diMouseState.rgbButtons[i] != 0) {
|
|
|
|
diMouseState.rgbButtons[i] = JNI_TRUE;
|
|
|
|
} else {
|
|
|
|
diMouseState.rgbButtons[i] = JNI_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2004-04-06 10:23:19 -04:00
|
|
|
int num_buttons = mButtoncount;
|
|
|
|
if (num_buttons > buttons_length)
|
|
|
|
num_buttons = buttons_length;
|
2004-04-06 10:40:36 -04:00
|
|
|
for (int j = 0; j < num_buttons; j++)
|
|
|
|
buttons_buffer[j] = (unsigned char)diMouseState.rgbButtons[j];
|
2003-03-23 15:08:49 -05:00
|
|
|
}
|