From 58d78eec2cdb4cdb6fdbd970a160520707b77e2e Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 30 Sep 2004 12:31:59 +0000 Subject: [PATCH] Linux: improved cursor position tracking --- src/native/linux/org_lwjgl_input_Mouse.c | 26 +++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/native/linux/org_lwjgl_input_Mouse.c b/src/native/linux/org_lwjgl_input_Mouse.c index b18cbb38..65eb285d 100644 --- a/src/native/linux/org_lwjgl_input_Mouse.c +++ b/src/native/linux/org_lwjgl_input_Mouse.c @@ -326,17 +326,13 @@ static int min(int v1, int v2) { return v1 < v2 ? v1 : v2; } -void handlePointerMotion(XMotionEvent *event) { - int x = event->x; - int y = event->y; - setCursorPos(x, y); +static void doHandlePointerMotion(int root_x, int root_y, int win_x, int win_y) { + setCursorPos(win_x, win_y); if (!pointer_grabbed || !shouldGrab()) return; - int x_root = event->x_root; - int y_root = event->y_root; // find the window position in root coordinates - int win_left = x_root - x; - int win_top = y_root - y; + int win_left = root_x - win_x; + int win_top = root_y - win_y; int win_right = win_left + getWindowWidth(); int win_bottom = win_top + getWindowHeight(); // cap the window position to the screen dimensions @@ -345,8 +341,8 @@ void handlePointerMotion(XMotionEvent *event) { int border_right = min(getScreenModeWidth(), win_right); int border_bottom = min(getScreenModeHeight(), win_bottom); // determine whether the cursor is outside the bounds - bool outside_limits = x_root < border_left + POINTER_WARP_BORDER || y_root < border_top + POINTER_WARP_BORDER || - x_root > border_right - POINTER_WARP_BORDER || y_root > border_bottom - POINTER_WARP_BORDER; + bool outside_limits = root_x < border_left + POINTER_WARP_BORDER || root_y < border_top + POINTER_WARP_BORDER || + root_x > border_right - POINTER_WARP_BORDER || root_y > border_bottom - POINTER_WARP_BORDER; if (outside_limits) { // Find the center of the limits in window coordinates int center_x = (border_right - border_left)/2; @@ -355,6 +351,10 @@ void handlePointerMotion(XMotionEvent *event) { } } +void handlePointerMotion(XMotionEvent *event) { + doHandlePointerMotion(event->x_root, event->y_root, event->x, event->y); +} + JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll(JNIEnv * env, jclass clazz, jobject coord_buffer_obj, jobject button_buffer_obj) { int *coords = (int *)(*env)->GetDirectBufferAddress(env, coord_buffer_obj); int coords_length = (*env)->GetDirectBufferCapacity(env, coord_buffer_obj); @@ -389,7 +389,13 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead(JNIEnv *env, jclass claz } JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse(JNIEnv * env, jclass clazz, jboolean new_grab) { + Window root_return, child_return; + int root_x, root_y, win_x, win_y; + unsigned int mask_return; + setGrab(new_grab == JNI_TRUE ? true : false); resetCursorToCenter(); + XQueryPointer(getDisplay(), getCurrentWindow(), &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return); + doHandlePointerMotion(root_x, root_y, win_x, win_y); accum_dx = accum_dy = 0; }