From 38ebcf5fb304f7dae6ed5f40a38613bbb7759920 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 22 Sep 2004 15:06:27 +0000 Subject: [PATCH] Updated win32 to report Mouse deltas --- src/native/linux/org_lwjgl_input_Mouse.c | 13 +- src/native/win32/Window.h | 13 ++ src/native/win32/org_lwjgl_Sys.cpp | 3 +- .../win32/org_lwjgl_input_Controller.cpp | 6 +- src/native/win32/org_lwjgl_input_Cursor.cpp | 3 +- src/native/win32/org_lwjgl_input_Keyboard.cpp | 24 ++- src/native/win32/org_lwjgl_input_Mouse.cpp | 154 ++++++++++++------ src/native/win32/org_lwjgl_opengl_Display.cpp | 48 +++++- 8 files changed, 185 insertions(+), 79 deletions(-) diff --git a/src/native/linux/org_lwjgl_input_Mouse.c b/src/native/linux/org_lwjgl_input_Mouse.c index f9b83f80..748a576f 100644 --- a/src/native/linux/org_lwjgl_input_Mouse.c +++ b/src/native/linux/org_lwjgl_input_Mouse.c @@ -65,7 +65,6 @@ static int dy; static int dz; static int last_x; static int last_y; -static int last_z; static jbyte buttons[NUM_BUTTONS]; static event_queue_t event_queue; static bool buffer_enabled; @@ -84,14 +83,12 @@ static void putEvent(jint button, jint state, jint dx, jint dy, jint dz) { } static void setCursorPos(int x, int y) { - int current_x = x; - int current_y = y; - jint event_dx = current_x - last_x; - jint event_dy = current_y - last_y; + jint event_dx = x - last_x; + jint event_dy = y - last_y; dx += event_dx; dy += event_dy; - last_x = current_x; - last_y = current_y; + last_x = x; + last_y = y; putEvent(-1, 0, event_dx, -event_dy, 0); } @@ -245,7 +242,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate if (disp == NULL) return; int i; - last_z = last_y = last_x = dx = dy = dz = 0; + last_y = last_x = dx = dy = dz = 0; for (i = 0; i < NUM_BUTTONS; i++) buttons[i] = 0; if (!blankCursor()) { diff --git a/src/native/win32/Window.h b/src/native/win32/Window.h index ca0c934c..c1e9fd6f 100644 --- a/src/native/win32/Window.h +++ b/src/native/win32/Window.h @@ -43,6 +43,11 @@ #define _LWJGL_WINDOW_H_INCLUDED_ #define WIN32_LEAN_AND_MEAN + #define _WIN32_WINDOWS 0x0410 + #define WINVER 0x0410 + #define _WIN32_WINNT 0x0400 + + #include #include #include "extgl.h" @@ -66,6 +71,14 @@ WINDOW_H_API void closeWindow(HWND hwnd, HDC hdc); + WINDOW_H_API void handleMouseMoved(int x, int y); + + WINDOW_H_API void handleMouseScrolled(int dwheel); + + WINDOW_H_API void handleMouseButton(int button, int state); + + WINDOW_H_API void handleMessages(void); + /* * Find a suitable pixel format */ diff --git a/src/native/win32/org_lwjgl_Sys.cpp b/src/native/win32/org_lwjgl_Sys.cpp index 973305a6..9bfb5357 100644 --- a/src/native/win32/org_lwjgl_Sys.cpp +++ b/src/native/win32/org_lwjgl_Sys.cpp @@ -39,10 +39,9 @@ * @version $Revision$ */ -#include +#include "Window.h" #include "org_lwjgl_Sys.h" #include "common_tools.h" -#include "Window.h" unsigned __int64 hires_timer_freq = 0; // Hires timer frequency unsigned __int64 hires_timer = 0; // Hires timer current time diff --git a/src/native/win32/org_lwjgl_input_Controller.cpp b/src/native/win32/org_lwjgl_input_Controller.cpp index 5ace79df..42b34917 100644 --- a/src/native/win32/org_lwjgl_input_Controller.cpp +++ b/src/native/win32/org_lwjgl_input_Controller.cpp @@ -40,13 +40,11 @@ */ -#define WIN32_LEAN_AND_MEAN -#include "org_lwjgl_input_Controller.h" -#include #undef DIRECTINPUT_VERSION #define DIRECTINPUT_VERSION 0x0500 -#include #include "Window.h" +#include "org_lwjgl_input_Controller.h" +#include #include "common_tools.h" #define CONTROLLER_AXISMAX 1000 // Maxmimum range to which we'll gauge the swing diff --git a/src/native/win32/org_lwjgl_input_Cursor.cpp b/src/native/win32/org_lwjgl_input_Cursor.cpp index 8cd638f2..007418e2 100755 --- a/src/native/win32/org_lwjgl_input_Cursor.cpp +++ b/src/native/win32/org_lwjgl_input_Cursor.cpp @@ -39,9 +39,8 @@ * @version $Revision$ */ -#include -#include "org_lwjgl_input_Cursor.h" #include "Window.h" +#include "org_lwjgl_input_Cursor.h" #include "common_tools.h" JNIEXPORT void JNICALL Java_org_lwjgl_input_Cursor_nCreateCursor diff --git a/src/native/win32/org_lwjgl_input_Keyboard.cpp b/src/native/win32/org_lwjgl_input_Keyboard.cpp index ee26e65e..ce7d86c5 100644 --- a/src/native/win32/org_lwjgl_input_Keyboard.cpp +++ b/src/native/win32/org_lwjgl_input_Keyboard.cpp @@ -39,13 +39,11 @@ * @version $Revision$ */ -#define WIN32_LEAN_AND_MEAN -#include #undef DIRECTINPUT_VERSION #define DIRECTINPUT_VERSION 0x0300 +#include "Window.h" #include #include "org_lwjgl_input_Keyboard.h" -#include "Window.h" #include "common_tools.h" @@ -199,10 +197,10 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead 0); if (ret == DI_OK) { - unsigned char * buf = buffer_position + (unsigned char *)env->GetDirectBufferAddress(buffer_obj); - int buffer_size = (int)env->GetDirectBufferCapacity(buffer_obj) - buffer_position; + jint * buf = buffer_position + (jint *)env->GetDirectBufferAddress(buffer_obj); + int buffer_size = ((int)env->GetDirectBufferCapacity(buffer_obj))/sizeof(jint) - buffer_position; int index = 0; - int event_size = translationEnabled ? 4 : 2; + int event_size = 3; DWORD current_di_event = 0; while (index + event_size <= buffer_size && current_di_event < num_di_events) { num_events++; @@ -232,25 +230,25 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead buf[index++] = 0; buf[index++] = 0; } + jint ch_int; if (useUnicode) { wchar_t ch = transBufUnicode[current_char]; - buf[index++] = (unsigned char) (ch & 0xff); - buf[index++] = (unsigned char) ((ch & 0xff00) >> 8); + ch_int = ((int)ch) & 0xFFFF; } else { - buf[index++] = (unsigned char)transBufAscii[current_char]; - buf[index++] = 0; + unsigned char ch = (unsigned char)transBufAscii[current_char]; + ch_int = ((int)ch) & 0xFF; } + buf[index++] = ch_int; current_char++; } while (index + event_size <= buffer_size && current_char < num_chars); } else { buf[index++] = 0; - buf[index++] = 0; } } else { buf[index++] = 0; - buf[index++] = 0; } - } + } else + buf[index++] = 0; current_di_event++; } } else if (ret == DI_BUFFEROVERFLOW) { diff --git a/src/native/win32/org_lwjgl_input_Mouse.cpp b/src/native/win32/org_lwjgl_input_Mouse.cpp index b6f6f96d..3c560c3b 100644 --- a/src/native/win32/org_lwjgl_input_Mouse.cpp +++ b/src/native/win32/org_lwjgl_input_Mouse.cpp @@ -39,14 +39,12 @@ * @version $Revision$ */ -#define WIN32_LEAN_AND_MEAN -#include "org_lwjgl_input_Mouse.h" -#include #undef DIRECTINPUT_VERSION #define DIRECTINPUT_VERSION 0x0300 -#include #include "Window.h" +#include #include "common_tools.h" +#include "org_lwjgl_input_Mouse.h" extern HINSTANCE dll_handle; // Handle to the LWJGL dll static LPDIRECTINPUT lpdi = NULL; // DirectInput @@ -57,10 +55,19 @@ static bool mHaswheel; // Temporary wheel check static bool mCreate_success; // bool used to determine successfull creation static bool mFirstTimeInitialization = true; // boolean to determine first time initialization -static POINT cursorPos; static bool mouse_grabbed; static int mouseMask = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND; +/* These deltas track the cursor position from Windows messages */ +static int dx; +static int dy; +static int dwheel; +static int last_x; +static int last_y; + +static event_queue_t event_queue; +static bool buffer_enabled; + // Function prototypes (defined in the cpp file, since header file is generic across platforms void EnumerateMouseCapabilities(); BOOL CALLBACK EnumMouseObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef); @@ -70,12 +77,22 @@ void SetupMouse(); void InitializeMouseFields(); void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj); +static void putEvent(jint button, jint state, jint dx, jint dy, jint dz) { + if (buffer_enabled) { + putEventElement(&event_queue, button); + putEventElement(&event_queue, state); + putEventElement(&event_queue, dx); + putEventElement(&event_queue, dy); + putEventElement(&event_queue, dz); + } +} + static void resetCursorPos(void) { /* Reset cursor position to middle of the window */ RECT clientRect; GetClientRect(getCurrentHWND(), &clientRect); - cursorPos.x = (clientRect.left + clientRect.right)/2; - cursorPos.y = clientRect.bottom - 1 - (clientRect.bottom - clientRect.top)/2; + last_x = (clientRect.left + clientRect.right)/2; + last_y = clientRect.bottom - 1 - (clientRect.bottom - clientRect.top)/2; } JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nHasWheel(JNIEnv *, jclass) { @@ -92,6 +109,11 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetButtonCount(JNIEnv *, jcla JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclass clazz) { HRESULT hr; + initEventQueue(&event_queue); + + last_x = last_y = dx = dy = dwheel = 0; + buffer_enabled = false; + // Create input HRESULT ret = DirectInputCreate(dll_handle, DIRECTINPUT_VERSION, &lpdi, NULL); if (ret != DI_OK && ret != DIERR_BETADIRECTINPUTVERSION) { @@ -128,51 +150,76 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate(JNIEnv *env, jclass cl } JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nEnableBuffer(JNIEnv * env, jclass clazz) { + buffer_enabled = true; } -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; - } +void handleMouseScrolled(int event_dwheel) { + dwheel += event_dwheel; + putEvent(-1, 0, 0, 0, event_dwheel); } -static int bufferButtons(int num_di_events, DIDEVICEOBJECTDATA *di_buffer, unsigned char *buffer, int buffer_size) { +void handleMouseMoved(int x, int y) { + int event_dx = x - last_x; + int event_dy = y - last_y; + dx += event_dx; + dy += event_dy; + putEvent(-1, 0, event_dx, -event_dy, 0); + last_x = x; + last_y = y; +} + +void handleMouseButton(int button, int state) { + putEvent(button, state, 0, 0, 0); +} + +static void copyDXEvents(int num_di_events, DIDEVICEOBJECTDATA *di_buffer) { int buffer_index = 0; + int dx = 0, dy = 0, dwheel = 0; for (int i = 0; i < num_di_events; i++) { - 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; - if (buffer_index == buffer_size) + int button_state = (di_buffer[i].dwData & 0x80) != 0 ? 1 : 0; + switch (di_buffer[i].dwOfs) { + case DIMOFS_BUTTON0: + putEvent(0, button_state, dx, dy, dwheel); + dx = dy = dwheel = 0; + break; + case DIMOFS_BUTTON1: + putEvent(1, button_state, dx, dy, dwheel); + dx = dy = dwheel = 0; + break; + case DIMOFS_BUTTON2: + putEvent(2, button_state, dx, dy, dwheel); + dx = dy = dwheel = 0; + break; + case DIMOFS_BUTTON3: + putEvent(3, button_state, dx, dy, dwheel); + dx = dy = dwheel = 0; + break; + case DIMOFS_X: + dx += di_buffer[i].dwData; + break; + case DIMOFS_Y: + dy += di_buffer[i].dwData; + break; + case DIMOFS_Z: + dwheel += di_buffer[i].dwData; break; - buffer[buffer_index++] = button; - buffer[buffer_index++] = state; } } - return buffer_index/2; + if (dx != 0 || dy != 0 || dwheel != 0) + putEvent(-1, 0, dx, -dy, dwheel); } -JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead - (JNIEnv * env, jclass clazz, jobject buffer_obj, jint buffer_position) +static void readDXBuffer() { - static DIDEVICEOBJECTDATA rgdod[EVENT_BUFFER_SIZE]; + DIDEVICEOBJECTDATA rgdod[EVENT_BUFFER_SIZE]; DWORD num_di_events = EVENT_BUFFER_SIZE; HRESULT ret; ret = mDIDevice->Acquire(); if (ret != DI_OK && ret != S_FALSE) - return 0; + return; ret = mDIDevice->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), @@ -181,9 +228,7 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead 0); if (ret == DI_OK) { - 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); + copyDXEvents(num_di_events, rgdod); } else if (ret == DI_BUFFEROVERFLOW) { printfDebug("Buffer overflowed\n"); } else if (ret == DIERR_INPUTLOST) { @@ -199,7 +244,19 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead } else { printfDebug("unknown keyboard error\n"); } - return 0; +} + +JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead + (JNIEnv * env, jclass clazz, jobject buffer_obj, jint buffer_position) +{ + jint* buffer_ptr = (jint *)env->GetDirectBufferAddress(buffer_obj) + buffer_position; + int buffer_size = (env->GetDirectBufferCapacity(buffer_obj))/sizeof(jint) - buffer_position; + if (mouse_grabbed) { + readDXBuffer(); + } else { + handleMessages(); + } + return copyEvents(&event_queue, buffer_ptr, buffer_size, 5); } JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps @@ -378,7 +435,8 @@ static int cap(int val, int min, int max) { return val; } -static void getGDICursorDelta(int* return_dx, int* return_dy) { +/*static void getGDICursorDelta(int* return_dx, int* return_dy) { + int dx; int dy; @@ -397,14 +455,15 @@ static void getGDICursorDelta(int* return_dx, int* return_dy) { *return_dx = dx; *return_dy = dy; } - +*/ /** * Updates the fields on the Mouse */ static void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer_obj, jobject button_buffer_obj) { HRESULT hRes; DIMOUSESTATE diMouseState; // State of Mouse - int dx, dy; + + handleMessages(); int *coords = (int *)env->GetDirectBufferAddress(coord_buffer_obj); int coords_length = (int)env->GetDirectBufferCapacity(coord_buffer_obj); @@ -435,17 +494,16 @@ static void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer } if (mouse_grabbed) { - dx = diMouseState.lX; - dy = diMouseState.lY; + coords[0] = diMouseState.lX; + coords[1] = -diMouseState.lY; + coords[2] = diMouseState.lZ; } else { - getGDICursorDelta(&dx, &dy); + coords[0] = dx; + coords[1] = -dy; + coords[2] = dwheel; + dx = dy = dwheel = 0; } - dy = -dy; - coords[0] = dx; - coords[1] = dy; - coords[2] = diMouseState.lZ; - for (int i = 0; i < mButtoncount; i++) { if (diMouseState.rgbButtons[i] != 0) { diMouseState.rgbButtons[i] = JNI_TRUE; diff --git a/src/native/win32/org_lwjgl_opengl_Display.cpp b/src/native/win32/org_lwjgl_opengl_Display.cpp index 87418700..e9cf7fbf 100644 --- a/src/native/win32/org_lwjgl_opengl_Display.cpp +++ b/src/native/win32/org_lwjgl_opengl_Display.cpp @@ -41,6 +41,7 @@ #define _PRIVATE_WINDOW_H_ #include "Window.h" +#include #include "extgl_wgl.h" #include "common_tools.h" #include "extgl_wgl.h" @@ -322,6 +323,49 @@ LRESULT CALLBACK lwjglWindowProc(HWND hWnd, } } break; + case WM_MOUSEMOVE: + { + int xPos = GET_X_LPARAM(lParam); + int yPos = GET_Y_LPARAM(lParam); + handleMouseMoved(xPos, yPos); + return 0; + } + case WM_MOUSEWHEEL: + { + int dwheel = GET_WHEEL_DELTA_WPARAM(wParam); + handleMouseScrolled(dwheel); + return 0; + } + case WM_LBUTTONDOWN: + { + handleMouseButton(0, 1); + return 0; + } + case WM_LBUTTONUP: + { + handleMouseButton(0, 0); + return 0; + } + case WM_RBUTTONDOWN: + { + handleMouseButton(1, 1); + return 0; + } + case WM_RBUTTONUP: + { + handleMouseButton(1, 0); + return 0; + } + case WM_MBUTTONDOWN: + { + handleMouseButton(2, 1); + return 0; + } + case WM_MBUTTONUP: + { + handleMouseButton(2, 0); + return 0; + } case WM_ACTIVATE: { switch(LOWORD(wParam)) { @@ -398,7 +442,7 @@ static bool registerWindow() /* * Handle native Win32 messages */ -static void handleMessages(JNIEnv * env, jclass clazz) +void handleMessages(void) { /* * Now's our chance to deal with Windows messages that are @@ -496,7 +540,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_nSetTitle JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_nUpdate (JNIEnv * env, jclass clazz) { - handleMessages(env, clazz); + handleMessages(); }