LINUX: Implemented setGrabbed

This commit is contained in:
Elias Naur 2004-04-12 10:05:13 +00:00
parent e7eb679f24
commit 0085c8862c
5 changed files with 93 additions and 171 deletions

View File

@ -55,7 +55,7 @@ public class Cursor {
private CursorElement[] cursors = null; private CursorElement[] cursors = null;
/** Index into list of cursors */ /** 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 * 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 // offset to next image
images_copy.position(width*height*(i+1)); images_copy.position(width*height*(i+1));
} }
// set index
index = 0;
break; break;
case Display.PLATFORM_AGL: case Display.PLATFORM_AGL:
break; break;

View File

@ -49,11 +49,6 @@
#include "extgl.h" #include "extgl.h"
#include "extgl_glx.h" #include "extgl_glx.h"
/*
* update input grabbing(keyboard, mouse)
*/
extern void updateInput(void);
/* /*
* release input (keyboard, mouse) * release input (keyboard, mouse)
*/ */
@ -66,10 +61,11 @@
extern void handleButtonPress(XButtonEvent *); extern void handleButtonPress(XButtonEvent *);
extern void handleButtonRelease(XButtonEvent *); extern void handleButtonRelease(XButtonEvent *);
extern void handleKeyEvent(XKeyEvent *); extern void handleKeyEvent(XKeyEvent *);
extern void releaseKeyboard(void); extern void updatePointerGrab(void);
extern void releasePointer(void); extern void updateKeyboardGrab(void);
extern void acquireKeyboard(void); extern void setGrab(bool);
extern void acquirePointer(void); extern bool shouldGrab(void);
extern bool isGrabbed(void);
/* /*
* get the current window width * get the current window width
@ -109,11 +105,6 @@
*/ */
extern Window getCurrentWindow(void); extern Window getCurrentWindow(void);
/*
* Return true if a native cursor is active
*/
extern bool isNativeCursor(void);
/* /*
* Return true if we are in fullscreen mode * Return true if we are in fullscreen mode
*/ */

View File

@ -61,57 +61,32 @@ static bool keyboard_grabbed;
static bool buffer_enabled; static bool buffer_enabled;
static bool translation_enabled; static bool translation_enabled;
static bool created = false; 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) { static void grabKeyboard(void) {
if (isFullscreen() || !isNativeCursor()) { if (!keyboard_grabbed) {
if (!keyboard_grabbed) { int result = XGrabKeyboard(getDisplay(), getCurrentWindow(), False, GrabModeAsync, GrabModeAsync, CurrentTime);
int result = XGrabKeyboard(getDisplay(), getCurrentWindow(), False, GrabModeAsync, GrabModeAsync, CurrentTime); if (result == GrabSuccess)
if (result == GrabSuccess) { keyboard_grabbed = true;
keyboard_grabbed = true; }
setRepeatMode(AutoRepeatModeOff);
XFlush(getDisplay());
}
}
} else
setRepeatMode(AutoRepeatModeOff);
} }
static void ungrabKeyboard(void) { static void ungrabKeyboard(void) {
if (keyboard_grabbed) { if (keyboard_grabbed) {
keyboard_grabbed = false; keyboard_grabbed = false;
XUngrabKeyboard(getDisplay(), CurrentTime); XUngrabKeyboard(getDisplay(), CurrentTime);
XFlush(getDisplay());
} }
setRepeatMode(AutoRepeatModeDefault);
} }
static void updateGrab(void) { void updateKeyboardGrab(void) {
if (!created) if (!created)
return; return;
if (should_grab) { if (shouldGrab()) {
grabKeyboard(); grabKeyboard();
} else { } else {
ungrabKeyboard(); ungrabKeyboard();
} }
} }
void acquireKeyboard(void) {
should_grab = true;
updateGrab();
}
void releaseKeyboard(void) {
should_grab = false;
updateGrab();
}
/* /*
* Class: org_lwjgl_input_Keyboard * Class: org_lwjgl_input_Keyboard
* Method: nCreate * Method: nCreate
@ -149,9 +124,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nCreate
keyboard_grabbed = false; keyboard_grabbed = false;
translation_enabled = false; translation_enabled = false;
buffer_enabled = false; buffer_enabled = false;
should_grab = true;
initEventQueue(&event_queue); initEventQueue(&event_queue);
updateGrab(); updateKeyboardGrab();
} }
/* /*

View File

@ -57,10 +57,8 @@
// scale the mouse wheel according to win32 // scale the mouse wheel according to win32
#define WHEEL_SCALE 120 #define WHEEL_SCALE 120
static bool pointer_grabbed = false; static bool pointer_grabbed;
static bool created = false; static bool created;
static bool should_grab = false;
static bool native_cursor = false;
static int last_x; static int last_x;
static int last_y; static int last_y;
@ -119,23 +117,26 @@ static bool blankCursor(void) {
return true; return true;
} }
bool isNativeCursor(void) { static void updateCursor(void) {
return native_cursor; Cursor cursor;
if (isGrabbed())
cursor = blank_cursor;
else
cursor = current_cursor;
XDefineCursor(getDisplay(), getCurrentWindow(), cursor);
} }
static void grabPointer(void) { static void grabPointer(void) {
if (isFullscreen() || !native_cursor) { if (!pointer_grabbed) {
if (!pointer_grabbed) { int result;
int result; int grab_mask = PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
int grab_mask = PointerMotionMask | ButtonPressMask | ButtonReleaseMask; result = XGrabPointer(getDisplay(), getCurrentWindow(), False, grab_mask, GrabModeAsync,
result = XGrabPointer(getDisplay(), getCurrentWindow(), False, grab_mask, GrabModeAsync, GrabModeAsync, getCurrentWindow(), None, CurrentTime);
GrabModeAsync, getCurrentWindow(), current_cursor, CurrentTime); if (result == GrabSuccess) {
if (result == GrabSuccess) { pointer_grabbed = true;
pointer_grabbed = true; // make sure we have a centered window
// make sure we have a centered window XF86VidModeSetViewPort(getDisplay(), getCurrentScreen(), 0, 0);
XF86VidModeSetViewPort(getDisplay(), getCurrentScreen(), 0, 0); XFlush(getDisplay());
XFlush(getDisplay());
}
} }
} }
} }
@ -148,24 +149,15 @@ static void ungrabPointer(void) {
} }
} }
static void updateGrab(void) { void updatePointerGrab(void) {
if (!created) if (!created)
return; return;
if (should_grab) { if (shouldGrab()) {
grabPointer(); grabPointer();
} else { } else {
ungrabPointer(); ungrabPointer();
} }
} updateCursor();
void acquirePointer(void) {
should_grab = true;
updateGrab();
}
void releasePointer(void) {
should_grab = false;
updateGrab();
} }
static void doWarpPointer(void ) { static void doWarpPointer(void ) {
@ -188,7 +180,7 @@ static void doWarpPointer(void ) {
} }
static void warpPointer(void) { static void warpPointer(void) {
if (!pointer_grabbed || native_cursor) if (!pointer_grabbed || !isGrabbed())
return; return;
// Reset pointer to middle of screen if outside a certain inner border // Reset pointer to middle of screen if outside a certain inner border
if (current_x < POINTER_WARP_BORDER || current_y < POINTER_WARP_BORDER || if (current_x < POINTER_WARP_BORDER || current_y < POINTER_WARP_BORDER ||
@ -196,11 +188,6 @@ static void warpPointer(void) {
doWarpPointer(); doWarpPointer();
} }
/*
* Class: org_lwjgl_input_Mouse
* Method: nIsNativeCursorSupported
* Signature: ()Z
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps
(JNIEnv *env, jclass clazz) { (JNIEnv *env, jclass clazz) {
int caps = 0; int caps = 0;
@ -215,38 +202,15 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetNativeCursorCaps
return caps; return caps;
} }
/* JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor (JNIEnv *env, jclass clazz, jlong cursor_handle) {
* 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)
{
if (cursor_handle != 0) { if (cursor_handle != 0) {
Cursor cursor = (Cursor)cursor_handle; Cursor cursor = (Cursor)cursor_handle;
if (!native_cursor) {
doWarpPointer();
native_cursor = true;
}
XDefineCursor(getDisplay(), getCurrentWindow(), cursor);
current_cursor = cursor; current_cursor = cursor;
updateInput(); } else
} else { current_cursor = None;
if (native_cursor) { updateCursor();
current_cursor = blank_cursor;
XUndefineCursor(getDisplay(), getCurrentWindow());
native_cursor = false;
updateInput();
}
}
} }
/*
* Class: org_lwjgl_input_Mouse
* Method: nGetMaxCursorSize
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMinCursorSize JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMinCursorSize
(JNIEnv *env, jclass clazz) (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; 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 JNIEXPORT jint JNICALL Java_org_lwjgl_input_Mouse_nGetMaxCursorSize
(JNIEnv *env, jclass clazz) (JNIEnv *env, jclass clazz)
{ {
@ -292,13 +251,11 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nCreate
throwException(env, "Could not create blank cursor"); throwException(env, "Could not create blank cursor");
return; return;
} }
current_cursor = blank_cursor; current_cursor = None;
native_cursor = false;
created = true; created = true;
should_grab = true;
pointer_grabbed = false; pointer_grabbed = false;
buffer_enabled = false; buffer_enabled = false;
updateGrab(); updatePointerGrab();
initEventQueue(&event_queue); initEventQueue(&event_queue);
loadXcursor(); loadXcursor();
doWarpPointer(); doWarpPointer();
@ -311,7 +268,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy
ungrabPointer(); ungrabPointer();
XFreeCursor(getDisplay(), blank_cursor); XFreeCursor(getDisplay(), blank_cursor);
created = false; created = false;
should_grab = false;
decDisplay(); decDisplay();
} }
@ -339,29 +295,24 @@ static void handleButton(XButtonEvent *event, unsigned char state) {
} }
void handleButtonPress(XButtonEvent *event) { void handleButtonPress(XButtonEvent *event) {
if (pointer_grabbed || native_cursor) { switch (event->button) {
switch (event->button) { case Button4:
case Button4: current_z += WHEEL_SCALE;
current_z += WHEEL_SCALE; break;
break; case Button5:
case Button5: current_z -= WHEEL_SCALE;
current_z -= WHEEL_SCALE; break;
break; default: break;
default: break;
}
handleButton(event, 1);
} }
handleButton(event, 1);
} }
void handleButtonRelease(XButtonEvent *event) { void handleButtonRelease(XButtonEvent *event) {
if (pointer_grabbed || native_cursor) { handleButton(event, 0);
handleButton(event, 0);
}
} }
void handlePointerMotion(XMotionEvent *event) { 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) { 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); return copyEvents(&event_queue, buffer_ptr + buffer_position, buffer_size, 2);
} }
/* JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse(JNIEnv * env, jclass clazz, jboolean new_grab) {
* Class: org_lwjgl_input_Mouse setGrab(new_grab == JNI_TRUE ? true : false);
* Method: nGrabMouse if (created)
* Signature: (Z)Z doWarpPointer();
*/ }
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse
(JNIEnv * env, jclass clazz, jboolean grab) {
if(native_cursor) {
return;
}
// do it?
}

View File

@ -72,6 +72,7 @@ static bool vsync_enabled;
static bool minimized; static bool minimized;
static bool focused; static bool focused;
static bool closerequested; static bool closerequested;
static bool grab;
static Display *display_connection = NULL; static Display *display_connection = NULL;
static int display_connection_usage = 0; static int display_connection_usage = 0;
@ -106,38 +107,51 @@ static void waitMapped(Window win) {
} while ((event.type != MapNotify) || (event.xmap.event != win)); } while ((event.type != MapNotify) || (event.xmap.event != win));
} }
static void acquireInput(void) { static void updateInputGrab(void) {
if (input_released) { updatePointerGrab();
acquireKeyboard(); updateKeyboardGrab();
acquirePointer();
input_released = false;
}
} }
static void doReleaseInput(void) { static void setRepeatMode(int mode) {
releaseKeyboard(); XKeyboardControl repeat_mode;
releasePointer(); repeat_mode.auto_repeat_mode = mode;
input_released = true; XChangeKeyboardControl(getDisplay(), KBAutoRepeatMode, &repeat_mode);
}
void updateInput(void) {
if (!input_released) {
doReleaseInput();
acquireInput();
}
} }
bool releaseInput(void) { bool releaseInput(void) {
if (current_fullscreen || input_released) if (current_fullscreen || input_released)
return false; return false;
doReleaseInput(); input_released = true;
setRepeatMode(AutoRepeatModeDefault);
updateInputGrab();
return true; return true;
} }
static void acquireInput(void) {
if (current_fullscreen || !input_released)
return;
input_released = false;
setRepeatMode(AutoRepeatModeOff);
updateInputGrab();
}
bool isFullscreen(void) { bool isFullscreen(void) {
return current_fullscreen; 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() { static void handleMessages() {
XEvent event; XEvent event;
Window win; Window win;
@ -208,6 +222,7 @@ static void createWindow(JNIEnv* env, int screen, XVisualInfo *vis_info, jstring
minimized = false; minimized = false;
closerequested = false; closerequested = false;
vsync_enabled = false; vsync_enabled = false;
grab = false;
Window root_win; Window root_win;
Window win; Window win;
XSetWindowAttributes attribs; XSetWindowAttributes attribs;
@ -403,6 +418,7 @@ static void destroy(void) {
glXDestroyContext(getDisplay(), context); glXDestroyContext(getDisplay(), context);
context = NULL; context = NULL;
destroyWindow(); destroyWindow();
setRepeatMode(AutoRepeatModeDefault);
decDisplay(); decDisplay();
extgl_Close(); extgl_Close();
} }