From 0085c8862ce575aa4937995b3d66ced16df128ab Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 12 Apr 2004 10:05:13 +0000 Subject: [PATCH] LINUX: Implemented setGrabbed --- src/java/org/lwjgl/input/Cursor.java | 4 +- src/native/linux/Window.h | 19 +-- src/native/linux/org_lwjgl_input_Keyboard.cpp | 42 +---- src/native/linux/org_lwjgl_input_Mouse.cpp | 147 ++++++------------ src/native/linux/org_lwjgl_opengl_Window.cpp | 52 ++++--- 5 files changed, 93 insertions(+), 171 deletions(-) diff --git a/src/java/org/lwjgl/input/Cursor.java b/src/java/org/lwjgl/input/Cursor.java index 767f4c27..787f0ad3 100644 --- a/src/java/org/lwjgl/input/Cursor.java +++ b/src/java/org/lwjgl/input/Cursor.java @@ -55,7 +55,7 @@ public class Cursor { private CursorElement[] cursors = null; /** Index into list of cursors */ - private int index = -1; + private int index = 0; /** * Constructs a new Cursor, with the given parameters. Mouse must have been created before you can create @@ -127,8 +127,6 @@ public class Cursor { // offset to next image images_copy.position(width*height*(i+1)); } - // set index - index = 0; break; case Display.PLATFORM_AGL: break; diff --git a/src/native/linux/Window.h b/src/native/linux/Window.h index d387dfb6..66755574 100644 --- a/src/native/linux/Window.h +++ b/src/native/linux/Window.h @@ -49,11 +49,6 @@ #include "extgl.h" #include "extgl_glx.h" - /* - * update input grabbing(keyboard, mouse) - */ - extern void updateInput(void); - /* * release input (keyboard, mouse) */ @@ -66,10 +61,11 @@ extern void handleButtonPress(XButtonEvent *); extern void handleButtonRelease(XButtonEvent *); extern void handleKeyEvent(XKeyEvent *); - extern void releaseKeyboard(void); - extern void releasePointer(void); - extern void acquireKeyboard(void); - extern void acquirePointer(void); + extern void updatePointerGrab(void); + extern void updateKeyboardGrab(void); + extern void setGrab(bool); + extern bool shouldGrab(void); + extern bool isGrabbed(void); /* * get the current window width @@ -109,11 +105,6 @@ */ extern Window getCurrentWindow(void); - /* - * Return true if a native cursor is active - */ - extern bool isNativeCursor(void); - /* * Return true if we are in fullscreen mode */ diff --git a/src/native/linux/org_lwjgl_input_Keyboard.cpp b/src/native/linux/org_lwjgl_input_Keyboard.cpp index 4f8a88e0..0143df46 100644 --- a/src/native/linux/org_lwjgl_input_Keyboard.cpp +++ b/src/native/linux/org_lwjgl_input_Keyboard.cpp @@ -61,57 +61,32 @@ static bool keyboard_grabbed; static bool buffer_enabled; static bool translation_enabled; static bool created = false; -static bool should_grab = false; - -static void setRepeatMode(int mode) { - XKeyboardControl repeat_mode; - repeat_mode.auto_repeat_mode = mode; - XChangeKeyboardControl(getDisplay(), KBAutoRepeatMode, &repeat_mode); -} static void grabKeyboard(void) { - if (isFullscreen() || !isNativeCursor()) { - if (!keyboard_grabbed) { - int result = XGrabKeyboard(getDisplay(), getCurrentWindow(), False, GrabModeAsync, GrabModeAsync, CurrentTime); - if (result == GrabSuccess) { - keyboard_grabbed = true; - setRepeatMode(AutoRepeatModeOff); - XFlush(getDisplay()); - } - } - } else - setRepeatMode(AutoRepeatModeOff); + if (!keyboard_grabbed) { + int result = XGrabKeyboard(getDisplay(), getCurrentWindow(), False, GrabModeAsync, GrabModeAsync, CurrentTime); + if (result == GrabSuccess) + keyboard_grabbed = true; + } } static void ungrabKeyboard(void) { if (keyboard_grabbed) { keyboard_grabbed = false; XUngrabKeyboard(getDisplay(), CurrentTime); - XFlush(getDisplay()); } - setRepeatMode(AutoRepeatModeDefault); } -static void updateGrab(void) { +void updateKeyboardGrab(void) { if (!created) return; - if (should_grab) { + if (shouldGrab()) { grabKeyboard(); } else { ungrabKeyboard(); } } -void acquireKeyboard(void) { - should_grab = true; - updateGrab(); -} - -void releaseKeyboard(void) { - should_grab = false; - updateGrab(); -} - /* * Class: org_lwjgl_input_Keyboard * Method: nCreate @@ -149,9 +124,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nCreate keyboard_grabbed = false; translation_enabled = false; buffer_enabled = false; - should_grab = true; initEventQueue(&event_queue); - updateGrab(); + updateKeyboardGrab(); } /* diff --git a/src/native/linux/org_lwjgl_input_Mouse.cpp b/src/native/linux/org_lwjgl_input_Mouse.cpp index d8547338..213e879f 100644 --- a/src/native/linux/org_lwjgl_input_Mouse.cpp +++ b/src/native/linux/org_lwjgl_input_Mouse.cpp @@ -57,10 +57,8 @@ // scale the mouse wheel according to win32 #define WHEEL_SCALE 120 -static bool pointer_grabbed = false; -static bool created = false; -static bool should_grab = false; -static bool native_cursor = false; +static bool pointer_grabbed; +static bool created; static int last_x; static int last_y; @@ -119,23 +117,26 @@ static bool blankCursor(void) { return true; } -bool isNativeCursor(void) { - return native_cursor; +static void updateCursor(void) { + Cursor cursor; + if (isGrabbed()) + cursor = blank_cursor; + else + cursor = current_cursor; + XDefineCursor(getDisplay(), getCurrentWindow(), cursor); } static void grabPointer(void) { - if (isFullscreen() || !native_cursor) { - if (!pointer_grabbed) { - int result; - int grab_mask = PointerMotionMask | ButtonPressMask | ButtonReleaseMask; - result = XGrabPointer(getDisplay(), getCurrentWindow(), False, grab_mask, GrabModeAsync, - GrabModeAsync, getCurrentWindow(), current_cursor, CurrentTime); - if (result == GrabSuccess) { - pointer_grabbed = true; - // make sure we have a centered window - XF86VidModeSetViewPort(getDisplay(), getCurrentScreen(), 0, 0); - XFlush(getDisplay()); - } + if (!pointer_grabbed) { + int result; + int grab_mask = PointerMotionMask | ButtonPressMask | ButtonReleaseMask; + result = XGrabPointer(getDisplay(), getCurrentWindow(), False, grab_mask, GrabModeAsync, + GrabModeAsync, getCurrentWindow(), None, CurrentTime); + if (result == GrabSuccess) { + pointer_grabbed = true; + // make sure we have a centered window + XF86VidModeSetViewPort(getDisplay(), getCurrentScreen(), 0, 0); + XFlush(getDisplay()); } } } @@ -148,24 +149,15 @@ static void ungrabPointer(void) { } } -static void updateGrab(void) { +void updatePointerGrab(void) { if (!created) return; - if (should_grab) { + if (shouldGrab()) { grabPointer(); } else { ungrabPointer(); } -} - -void acquirePointer(void) { - should_grab = true; - updateGrab(); -} - -void releasePointer(void) { - should_grab = false; - updateGrab(); + updateCursor(); } static void doWarpPointer(void ) { @@ -188,7 +180,7 @@ static void doWarpPointer(void ) { } static void warpPointer(void) { - if (!pointer_grabbed || native_cursor) + if (!pointer_grabbed || !isGrabbed()) return; // Reset pointer to middle of screen if outside a certain inner border if (current_x < POINTER_WARP_BORDER || current_y < POINTER_WARP_BORDER || @@ -196,11 +188,6 @@ static void warpPointer(void) { doWarpPointer(); } -/* - * Class: org_lwjgl_input_Mouse - * Method: nIsNativeCursorSupported - * Signature: ()Z - */ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps (JNIEnv *env, jclass clazz) { int caps = 0; @@ -215,38 +202,15 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps return caps; } -/* - * Class: org_lwjgl_input_Mouse - * Method: nSetNativeCursor - * Signature: (I)V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor - (JNIEnv *env, jclass clazz, jlong cursor_handle) -{ +JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor (JNIEnv *env, jclass clazz, jlong cursor_handle) { if (cursor_handle != 0) { Cursor cursor = (Cursor)cursor_handle; - if (!native_cursor) { - doWarpPointer(); - native_cursor = true; - } - XDefineCursor(getDisplay(), getCurrentWindow(), cursor); current_cursor = cursor; - updateInput(); - } else { - if (native_cursor) { - current_cursor = blank_cursor; - XUndefineCursor(getDisplay(), getCurrentWindow()); - native_cursor = false; - updateInput(); - } - } + } else + current_cursor = None; + updateCursor(); } -/* - * Class: org_lwjgl_input_Mouse - * Method: nGetMaxCursorSize - * Signature: ()I - */ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMinCursorSize (JNIEnv *env, jclass clazz) { @@ -256,11 +220,6 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMinCursorSize return width_return > height_return ? width_return : height_return; } -/* - * Class: org_lwjgl_input_Mouse - * Method: nGetMaxCursorSize - * Signature: ()I - */ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMaxCursorSize (JNIEnv *env, jclass clazz) { @@ -292,13 +251,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate throwException(env, "Could not create blank cursor"); return; } - current_cursor = blank_cursor; - native_cursor = false; + current_cursor = None; created = true; - should_grab = true; pointer_grabbed = false; buffer_enabled = false; - updateGrab(); + updatePointerGrab(); initEventQueue(&event_queue); loadXcursor(); doWarpPointer(); @@ -311,7 +268,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy ungrabPointer(); XFreeCursor(getDisplay(), blank_cursor); created = false; - should_grab = false; decDisplay(); } @@ -339,29 +295,24 @@ static void handleButton(XButtonEvent *event, unsigned char state) { } void handleButtonPress(XButtonEvent *event) { - if (pointer_grabbed || native_cursor) { - switch (event->button) { - case Button4: - current_z += WHEEL_SCALE; - break; - case Button5: - current_z -= WHEEL_SCALE; - break; - default: break; - } - handleButton(event, 1); + switch (event->button) { + case Button4: + current_z += WHEEL_SCALE; + break; + case Button5: + current_z -= WHEEL_SCALE; + break; + default: break; } + handleButton(event, 1); } void handleButtonRelease(XButtonEvent *event) { - if (pointer_grabbed || native_cursor) { - handleButton(event, 0); - } + handleButton(event, 0); } void handlePointerMotion(XMotionEvent *event) { - if (pointer_grabbed || native_cursor) - setCursorPos(event->x, event->y); + setCursorPos(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) { @@ -400,16 +351,8 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nRead(JNIEnv *env, jclass claz return copyEvents(&event_queue, buffer_ptr + buffer_position, buffer_size, 2); } -/* - * 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(native_cursor) { - return; - } - - // do it? -} \ No newline at end of file +JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse(JNIEnv * env, jclass clazz, jboolean new_grab) { + setGrab(new_grab == JNI_TRUE ? true : false); + if (created) + doWarpPointer(); +} diff --git a/src/native/linux/org_lwjgl_opengl_Window.cpp b/src/native/linux/org_lwjgl_opengl_Window.cpp index 4ceeb8b2..4ff2329c 100644 --- a/src/native/linux/org_lwjgl_opengl_Window.cpp +++ b/src/native/linux/org_lwjgl_opengl_Window.cpp @@ -72,6 +72,7 @@ static bool vsync_enabled; static bool minimized; static bool focused; static bool closerequested; +static bool grab; static Display *display_connection = NULL; static int display_connection_usage = 0; @@ -106,38 +107,51 @@ static void waitMapped(Window win) { } while ((event.type != MapNotify) || (event.xmap.event != win)); } -static void acquireInput(void) { - if (input_released) { - acquireKeyboard(); - acquirePointer(); - input_released = false; - } +static void updateInputGrab(void) { + updatePointerGrab(); + updateKeyboardGrab(); } -static void doReleaseInput(void) { - releaseKeyboard(); - releasePointer(); - input_released = true; -} - -void updateInput(void) { - if (!input_released) { - doReleaseInput(); - acquireInput(); - } +static void setRepeatMode(int mode) { + XKeyboardControl repeat_mode; + repeat_mode.auto_repeat_mode = mode; + XChangeKeyboardControl(getDisplay(), KBAutoRepeatMode, &repeat_mode); } bool releaseInput(void) { if (current_fullscreen || input_released) return false; - doReleaseInput(); + input_released = true; + setRepeatMode(AutoRepeatModeDefault); + updateInputGrab(); return true; } +static void acquireInput(void) { + if (current_fullscreen || !input_released) + return; + input_released = false; + setRepeatMode(AutoRepeatModeOff); + updateInputGrab(); +} + bool isFullscreen(void) { return current_fullscreen; } +bool isGrabbed(void) { + return grab; +} + +bool shouldGrab(void) { + return current_fullscreen || (!input_released && grab); +} + +void setGrab(bool new_grab) { + grab = new_grab; + updateInputGrab(); +} + static void handleMessages() { XEvent event; Window win; @@ -208,6 +222,7 @@ static void createWindow(JNIEnv* env, int screen, XVisualInfo *vis_info, jstring minimized = false; closerequested = false; vsync_enabled = false; + grab = false; Window root_win; Window win; XSetWindowAttributes attribs; @@ -403,6 +418,7 @@ static void destroy(void) { glXDestroyContext(getDisplay(), context); context = NULL; destroyWindow(); + setRepeatMode(AutoRepeatModeDefault); decDisplay(); extgl_Close(); }