From 55ac8dd74c427433afa6a47dea5d671f38c14480 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 14 Feb 2003 11:23:01 +0000 Subject: [PATCH] Don't fail create if grab doesn't succeed on the first try --- src/native/linux/org_lwjgl_Display.cpp | 18 ++++---- src/native/linux/org_lwjgl_input_Keyboard.cpp | 42 +++++++++++-------- src/native/linux/org_lwjgl_input_Mouse.cpp | 34 +++++++++------ src/native/linux/org_lwjgl_opengl_BaseGL.cpp | 4 +- 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/native/linux/org_lwjgl_Display.cpp b/src/native/linux/org_lwjgl_Display.cpp index a252100d..b83c28a4 100644 --- a/src/native/linux/org_lwjgl_Display.cpp +++ b/src/native/linux/org_lwjgl_Display.cpp @@ -84,7 +84,7 @@ struct pixelformat { int stencil; }; -int fillFormat(struct pixelformat *formats, int index, int bpp, int depth, int alpha, int stencil) { +static int fillFormat(struct pixelformat *formats, int index, int bpp, int depth, int alpha, int stencil) { for (int i = 0; i < index; i++) if (formats[i].bpp == bpp && formats[i].depth == depth && @@ -98,7 +98,7 @@ int fillFormat(struct pixelformat *formats, int index, int bpp, int depth, int a return 1; } -struct pixelformat *getGLXAvailablePixelFormats(Display *disp, int screen, int *length) { +static struct pixelformat *getGLXAvailablePixelFormats(Display *disp, int screen, int *length) { if (extgl_Extensions.glx.GLX13 == 1) { int num_formats; int attriblist[] = {GLX_DOUBLEBUFFER, True, @@ -148,7 +148,7 @@ struct pixelformat *getGLXAvailablePixelFormats(Display *disp, int screen, int * return NULL; } -XVisualInfo *chooseVisual(Display *disp, int screen, int bpp, int depth, int alpha, int stencil) { +static XVisualInfo *chooseVisual(Display *disp, int screen, int bpp, int depth, int alpha, int stencil) { int bpe; switch (bpp) { case 32: @@ -174,7 +174,7 @@ XVisualInfo *chooseVisual(Display *disp, int screen, int bpp, int depth, int alp return glXChooseVisual(disp, screen, attriblist); } -struct pixelformat *getAvailablePixelFormats(Display *disp, int screen, int *length) { +static struct pixelformat *getAvailablePixelFormats(Display *disp, int screen, int *length) { struct pixelformat *formats = getGLXAvailablePixelFormats(disp, screen, length); if (formats != NULL) return formats; @@ -195,7 +195,7 @@ struct pixelformat *getAvailablePixelFormats(Display *disp, int screen, int *len return formats; } -void waitMapped(Display *disp, Window win) { +static void waitMapped(Display *disp, Window win) { XEvent event; do { @@ -250,7 +250,7 @@ void handleMessages(void) { } } -bool loadGL(Display *disp, int screen) { +static bool loadGL(Display *disp, int screen) { if (gl_loaded == true) return true; if (extgl_Open(disp, screen) != 0) { @@ -263,12 +263,12 @@ bool loadGL(Display *disp, int screen) { return true; } -void closeGL(void) { +static void closeGL(void) { gl_loaded = false; extgl_Close(); } -int getDisplayModes(Display *disp, int screen, int *num_modes, XF86VidModeModeInfo ***avail_modes) { +static int getDisplayModes(Display *disp, int screen, int *num_modes, XF86VidModeModeInfo ***avail_modes) { int event_base, error_base, xvid_ver, xvid_rev; if (!XF86VidModeQueryExtension(disp, &event_base, &error_base)) { @@ -285,7 +285,7 @@ int getDisplayModes(Display *disp, int screen, int *num_modes, XF86VidModeModeIn return 1; } -bool isMinimized() { +static bool isMinimized() { handleMessages(); return current_minimized; } diff --git a/src/native/linux/org_lwjgl_input_Keyboard.cpp b/src/native/linux/org_lwjgl_input_Keyboard.cpp index ce9e06c9..1356faa0 100644 --- a/src/native/linux/org_lwjgl_input_Keyboard.cpp +++ b/src/native/linux/org_lwjgl_input_Keyboard.cpp @@ -63,6 +63,7 @@ static bool keyboard_grabbed; static bool buffer_enabled; static bool translation_enabled; static bool created = false; +static bool should_grab = false; extern Display *disp; extern Window win; @@ -88,14 +89,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_initIDs fid_readBuffer = env->GetStaticFieldID(clazz, "readBuffer", "Ljava/nio/ByteBuffer;"); } -int grabKeyboard(void) { +static int grabKeyboard(void) { int result = XGrabKeyboard(disp, win, False, GrabModeAsync, GrabModeAsync, CurrentTime); if (result == GrabSuccess) keyboard_grabbed = true; return result; } -void ungrabKeyboard(void) { +static void ungrabKeyboard(void) { keyboard_grabbed = false; XUngrabKeyboard(disp, CurrentTime); } @@ -103,13 +104,23 @@ void ungrabKeyboard(void) { void acquireKeyboard(void) { if (!created) return; - grabKeyboard(); + should_grab = true; } void releaseKeyboard(void) { if (!created) return; - ungrabKeyboard(); + should_grab = false; +} + +static void updateGrab(void) { + if (should_grab) { + if (!keyboard_grabbed) + grabKeyboard(); + } else { + if (keyboard_grabbed) + ungrabKeyboard(); + } } /* @@ -123,13 +134,8 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Keyboard_nCreate keyboard_grabbed = false; translation_enabled = false; buffer_enabled = false; - - if (grabKeyboard() != GrabSuccess) { -#ifdef _DEBUG - printf("Could not grab keyboard\n"); -#endif - return JNI_FALSE; - } + should_grab = true; + updateGrab(); for (int i = 0; i < KEYBOARD_SIZE; i++) key_map[i] = i; key_map[0x6b] = 0xdb; // Left doze key @@ -169,7 +175,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nDestroy created = false; } -XKeyEvent *nextEventElement(void) { +static XKeyEvent *nextEventElement(void) { if (list_start == list_end) return NULL; XKeyEvent *result = &(saved_key_events[list_start]); @@ -177,7 +183,7 @@ XKeyEvent *nextEventElement(void) { return result; } -void putEventElement(XKeyEvent *event) { +static void putEventElement(XKeyEvent *event) { int next_index = (list_end + 1)%KEY_EVENT_BACKLOG; if (next_index == list_start) return; @@ -185,13 +191,13 @@ void putEventElement(XKeyEvent *event) { list_end = next_index; } -unsigned char getKeycode(XKeyEvent *event) { +static unsigned char getKeycode(XKeyEvent *event) { unsigned char keycode = (unsigned char)((event->keycode - 8) & 0xff); keycode = key_map[keycode]; return keycode; } -int translateEvent(int *position, XKeyEvent *event) { +static int translateEvent(int *position, XKeyEvent *event) { static char temp_translation_buffer[KEYBOARD_BUFFER_SIZE]; static XComposeStatus status; int num_chars, i; @@ -222,7 +228,7 @@ int translateEvent(int *position, XKeyEvent *event) { return num_chars; } -unsigned char eventState(XKeyEvent *event) { +static unsigned char eventState(XKeyEvent *event) { if (event->type == KeyPress) { return 1; } else if (event->type == KeyRelease) { @@ -262,7 +268,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nPoll unsigned char state; handleMessages(); - + updateGrab(); memcpy((unsigned char*)buf, key_buf, KEYBOARD_SIZE*sizeof(unsigned char)); } @@ -281,7 +287,7 @@ JNIEXPORT int JNICALL Java_org_lwjgl_input_Keyboard_nRead int num_events = 0; handleMessages(); - + updateGrab(); while (buf_count < KEYBOARD_BUFFER_SIZE * 2 && (key_event = nextEventElement()) != NULL) { num_events++; unsigned char keycode = getKeycode(key_event); diff --git a/src/native/linux/org_lwjgl_input_Mouse.cpp b/src/native/linux/org_lwjgl_input_Mouse.cpp index f9a6d29b..1693d177 100644 --- a/src/native/linux/org_lwjgl_input_Mouse.cpp +++ b/src/native/linux/org_lwjgl_input_Mouse.cpp @@ -61,6 +61,7 @@ extern void handleMessages(void); static bool pointer_grabbed; static bool created = false; +static bool should_grab = false; static jfieldID fid_has_wheel = NULL; static jfieldID fid_button_count = NULL; @@ -101,7 +102,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_initIDs fid_dwheel = env->GetStaticFieldID(clazz, "dwheel", "I"); } -int blankCursor(void) { +static int blankCursor(void) { unsigned int best_width, best_height; if (XQueryBestCursor(disp, win, 1, 1, &best_width, &best_height) == 0) { #ifdef _DEBUG @@ -121,7 +122,7 @@ int blankCursor(void) { return 1; } -int grabPointer(void) { +static int grabPointer(void) { int result; int mask = FocusChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask; result = XGrabPointer(disp, win, False, mask, GrabModeAsync, GrabModeAsync, win, blank_cursor, CurrentTime); @@ -132,7 +133,7 @@ int grabPointer(void) { return result; } -void ungrabPointer(void) { +static void ungrabPointer(void) { pointer_grabbed = false; XUngrabPointer(disp, CurrentTime); } @@ -140,13 +141,24 @@ void ungrabPointer(void) { void acquirePointer(void) { if (!created) return; - grabPointer(); + should_grab = true; } void releasePointer(void) { if (!created) return; - ungrabPointer(); + should_grab = false; +} + + +static void updateGrab(void) { + if (should_grab) { + if (!pointer_grabbed) + grabPointer(); + } else { + if (pointer_grabbed) + ungrabPointer(); + } } /* @@ -169,16 +181,12 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nCreate if (!blankCursor()) { #ifdef _DEBUG printf("Could create blank cursor\n"); -#endif - return JNI_FALSE; - } - if (grabPointer() != GrabSuccess) { -#ifdef _DEBUG - printf("Could not grab pointer\n"); #endif return JNI_FALSE; } created = true; + should_grab = true; + updateGrab(); return JNI_TRUE; } @@ -194,6 +202,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy ungrabPointer(); XFreeCursor(disp, blank_cursor); created = false; + should_grab = false; } void handleButtonPress(XButtonEvent *event) { @@ -240,7 +249,7 @@ void handlePointerMotion(XMotionEvent *event) { current_y = event->y; } -void warpPointer(void) { +static void warpPointer(void) { int i; if (!pointer_grabbed) return; @@ -279,6 +288,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll (JNIEnv * env, jclass clazz) { handleMessages(); + updateGrab(); int moved_x = current_x - last_x; int moved_y = current_y - last_y; int moved_z = current_z - last_z; diff --git a/src/native/linux/org_lwjgl_opengl_BaseGL.cpp b/src/native/linux/org_lwjgl_opengl_BaseGL.cpp index 1fc00303..48d23930 100644 --- a/src/native/linux/org_lwjgl_opengl_BaseGL.cpp +++ b/src/native/linux/org_lwjgl_opengl_BaseGL.cpp @@ -49,11 +49,11 @@ extern Display * disp; extern void handleMessages(); -void makeCurrent(void) { +static void makeCurrent(void) { glXMakeCurrent(disp, win, context); } -void releaseContext(void) { +static void releaseContext(void) { glXMakeCurrent(disp, None, NULL); }