Linux: Made cursor handles longs instead of ByteBuffers

This commit is contained in:
Elias Naur 2006-10-26 12:14:37 +00:00
parent 1802fa2d9d
commit 541ac859b1
4 changed files with 48 additions and 59 deletions

View File

@ -49,10 +49,11 @@ import org.lwjgl.input.Keyboard;
final class LinuxDisplay implements DisplayImplementation {
/* X11 constants */
private final static int GrabSuccess = 0;
public final static int GrabSuccess = 0;
private final static int AutoRepeatModeOff = 0;
private final static int AutoRepeatModeOn = 1;
private final static int AutoRepeatModeDefault = 2;
public final static int None = 0;
/** Window mode enum */
private static final int FULLSCREEN_LEGACY = 1;
@ -109,8 +110,8 @@ final class LinuxDisplay implements DisplayImplementation {
private boolean minimized;
private boolean dirty;
private boolean close_requested;
private ByteBuffer current_cursor;
private ByteBuffer blank_cursor;
private long current_cursor;
private long blank_cursor;
private LinuxKeyboard keyboard;
private LinuxMouse mouse;
@ -296,7 +297,7 @@ final class LinuxDisplay implements DisplayImplementation {
static int getDefaultScreen() {
return nGetDefaultScreen(getDisplay());
}
private static native int nGetDefaultScreen(long display);
static native int nGetDefaultScreen(long display);
static long getWindow() {
return current_window;
@ -308,7 +309,7 @@ final class LinuxDisplay implements DisplayImplementation {
keyboard_grabbed = false;
}
}
private static native int nUngrabKeyboard(long display);
static native int nUngrabKeyboard(long display);
private void grabKeyboard() {
if (!keyboard_grabbed) {
@ -317,11 +318,11 @@ final class LinuxDisplay implements DisplayImplementation {
keyboard_grabbed = true;
}
}
private static native int nGrabKeyboard(long display, long window);
static native int nGrabKeyboard(long display, long window);
private void grabPointer() {
if (!pointer_grabbed) {
int result = nGrabPointer(getDisplay(), getWindow());
int result = nGrabPointer(getDisplay(), getWindow(), None);
if (result == GrabSuccess) {
pointer_grabbed = true;
// make sure we have a centered window
@ -331,7 +332,7 @@ final class LinuxDisplay implements DisplayImplementation {
}
}
}
private static native int nGrabPointer(long display, long window);
static native int nGrabPointer(long display, long window, long cursor);
private static native void nSetViewPort(long display, long window, int screen);
private void ungrabPointer() {
@ -340,7 +341,7 @@ final class LinuxDisplay implements DisplayImplementation {
nUngrabPointer(getDisplay());
}
}
private static native int nUngrabPointer(long display);
static native int nUngrabPointer(long display);
private boolean isFullscreen() {
return current_window_mode == FULLSCREEN_LEGACY || current_window_mode == FULLSCREEN_NETWM;
@ -360,7 +361,7 @@ final class LinuxDisplay implements DisplayImplementation {
}
private void updateCursor() {
ByteBuffer cursor;
long cursor;
if (shouldGrab()) {
cursor = blank_cursor;
} else {
@ -368,7 +369,7 @@ final class LinuxDisplay implements DisplayImplementation {
}
nDefineCursor(getDisplay(), getWindow(), cursor);
}
private static native void nDefineCursor(long display, long window, ByteBuffer cursor_handle);
private static native void nDefineCursor(long display, long window, long cursor_handle);
private boolean isLegacyFullscreen() {
return current_window_mode == FULLSCREEN_LEGACY;
@ -391,7 +392,7 @@ final class LinuxDisplay implements DisplayImplementation {
current_window_mode = getWindowMode(fullscreen);
current_window = nCreateWindow(getDisplay(), getDefaultScreen(), handle, mode, current_window_mode, x, y);
blank_cursor = createBlankCursor();
current_cursor = null;
current_cursor = None;
focused = true;
input_released = false;
pointer_grabbed = false;
@ -429,7 +430,7 @@ final class LinuxDisplay implements DisplayImplementation {
LWJGLUtil.log("Failed to reset cursor: " + e.getMessage());
}
nDestroyCursor(getDisplay(), blank_cursor);
blank_cursor = null;
blank_cursor = None;
ungrabKeyboard();
nDestroyWindow(getDisplay(), getWindow());
nSetRepeatMode(getDisplay(), AutoRepeatModeDefault);
@ -438,7 +439,7 @@ final class LinuxDisplay implements DisplayImplementation {
unlockAWT();
}
}
private static native void nDestroyWindow(long display, long window);
static native void nDestroyWindow(long display, long window);
public void switchDisplayMode(DisplayMode mode) throws LWJGLException {
lockAWT();
@ -706,7 +707,7 @@ final class LinuxDisplay implements DisplayImplementation {
public void createMouse() throws LWJGLException {
lockAWT();
try {
mouse = new LinuxMouse(getDisplay(), getWindow());
mouse = new LinuxMouse(getDisplay(), getWindow(), getWindow());
} finally {
unlockAWT();
}
@ -825,7 +826,7 @@ final class LinuxDisplay implements DisplayImplementation {
private static native int nGetNativeCursorCapabilities(long display) throws LWJGLException;
public void setNativeCursor(Object handle) throws LWJGLException {
current_cursor = (ByteBuffer)handle;
current_cursor = getCursorHandle(handle);
lockAWT();
try {
updateCursor();
@ -914,19 +915,20 @@ final class LinuxDisplay implements DisplayImplementation {
return Keyboard.STATE_UNKNOWN;
}
*/
private static native ByteBuffer nCreateCursor(long display, int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException;
private static native long nCreateCursor(long display, int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, int images_offset, IntBuffer delays, int delays_offset) throws LWJGLException;
private static ByteBuffer createBlankCursor() {
private static long createBlankCursor() {
return nCreateBlankCursor(getDisplay(), getWindow());
}
private static native ByteBuffer nCreateBlankCursor(long display, long window);
static native long nCreateBlankCursor(long display, long window);
public Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException {
lockAWT();
try {
incDisplay();
try {
return nCreateCursor(getDisplay(), width, height, xHotspot, yHotspot, numImages, images, images.position(), delays, delays != null ? delays.position() : -1);
long cursor = nCreateCursor(getDisplay(), width, height, xHotspot, yHotspot, numImages, images, images.position(), delays, delays != null ? delays.position() : -1);
return new Long(cursor);
} catch (LWJGLException e) {
decDisplay();
throw e;
@ -936,16 +938,20 @@ final class LinuxDisplay implements DisplayImplementation {
}
}
private static long getCursorHandle(Object cursor_handle) {
return cursor_handle != null ? ((Long)cursor_handle).longValue() : None;
}
public void destroyCursor(Object cursorHandle) {
lockAWT();
try {
nDestroyCursor(getDisplay(), cursorHandle);
nDestroyCursor(getDisplay(), getCursorHandle(cursorHandle));
decDisplay();
} finally {
unlockAWT();
}
}
private static native void nDestroyCursor(long display, Object cursorHandle);
static native void nDestroyCursor(long display, long cursorHandle);
public int getPbufferCapabilities() {
lockAWT();

View File

@ -64,6 +64,7 @@ final class LinuxMouse {
private final long display;
private final long window;
private final long input_window;
private final long warp_atom;
private final IntBuffer query_pointer_buffer = BufferUtils.createIntBuffer(4);
private final ByteBuffer event_buffer = ByteBuffer.allocate(Mouse.EVENT_SIZE);
@ -77,15 +78,11 @@ final class LinuxMouse {
private EventQueue event_queue;
private long last_event_nanos;
public LinuxMouse(long display, long window) throws LWJGLException {
public LinuxMouse(long display, long window, long input_window) throws LWJGLException {
this.display = display;
this.window = window;
LinuxDisplay.lockAWT();
try {
this.warp_atom = LinuxDisplay.nInternAtom(display, "_LWJGL", false);
} finally {
LinuxDisplay.unlockAWT();
}
this.input_window = input_window;
this.warp_atom = LinuxDisplay.nInternAtom(display, "_LWJGL", false);
reset();
}
@ -138,7 +135,7 @@ final class LinuxMouse {
}
private void doWarpPointer(int center_x, int center_y) {
nSendWarpEvent(display, window, warp_atom, center_x, center_y);
nSendWarpEvent(display, input_window, warp_atom, center_x, center_y);
nWarpCursor(display, window, center_x, center_y);
}
private static native void nSendWarpEvent(long display, long window, long warp_atom, int center_x, int center_y);

View File

@ -81,15 +81,10 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetMaxCursorSize
return width_return > height_return ? height_return : width_return;
}
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateCursor
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateCursor
(JNIEnv *env, jclass clazz, jlong display, jint width, jint height, jint x_hotspot, jint y_hotspot, jint num_images, jobject image_buffer, jint images_offset, jobject delay_buffer, jint delays_offset)
{
Display *disp = (Display *)(intptr_t)display;
jobject handle_buffer = newJavaManagedByteBuffer(env, sizeof(Cursor));
if (handle_buffer == NULL) {
throwException(env, "Could not allocate handle buffer");
return NULL;
}
const int *delays = NULL;
if (delay_buffer != NULL)
delays = (const int *)(*env)->GetDirectBufferAddress(env, delay_buffer) + delays_offset;
@ -98,7 +93,7 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateCursor
XcursorImages *cursor_images = XcursorImagesCreate(num_images);
if (cursor_images == NULL) {
throwException(env, "Could not allocate cursor.");
return NULL;
return None;
}
cursor_images->nimage = num_images;
int i;
@ -111,16 +106,15 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateCursor
cursor_image->delay = delays[i];
cursor_images->images[i] = cursor_image;
}
Cursor *cursor = (Cursor *)(*env)->GetDirectBufferAddress(env, handle_buffer);
*cursor = XcursorImagesLoadCursor(disp, cursor_images);
Cursor cursor = XcursorImagesLoadCursor(disp, cursor_images);
XcursorImagesDestroy(cursor_images);
return handle_buffer;
return cursor;
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nDestroyCursor
(JNIEnv *env, jclass clazz, jlong display, jobject cursor_handle_buffer)
(JNIEnv *env, jclass clazz, jlong display, jlong cursor_ptr)
{
Display *disp = (Display *)(intptr_t)display;
Cursor *cursor = (Cursor *)(*env)->GetDirectBufferAddress(env, cursor_handle_buffer);
XFreeCursor(disp, *cursor);
Cursor cursor = (Cursor)cursor_ptr;
XFreeCursor(disp, cursor);
}

View File

@ -423,11 +423,12 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGrabKeyboard(JNIEnv *
return XGrabKeyboard(disp, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
}
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGrabPointer(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr) {
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGrabPointer(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr, jlong cursor_ptr) {
Display *disp = (Display *)(intptr_t)display_ptr;
Window win = (Window)window_ptr;
Cursor cursor = (Cursor)cursor_ptr;
int grab_mask = PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
return XGrabPointer(disp, win, False, grab_mask, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
return XGrabPointer(disp, win, False, grab_mask, GrabModeAsync, GrabModeAsync, win, cursor, CurrentTime);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSetViewPort(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr, jint screen) {
@ -444,25 +445,16 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nUngrabPointer(JNIEnv
return XUngrabPointer(disp, CurrentTime);
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nDefineCursor(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr, jobject cursor_handle) {
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nDefineCursor(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr, jlong cursor_ptr) {
Display *disp = (Display *)(intptr_t)display_ptr;
Window win = (Window)window_ptr;
Cursor cursor;
if (cursor_handle != NULL)
cursor = *((Cursor *)(*env)->GetDirectBufferAddress(env, cursor_handle));
else
cursor = None;
Cursor cursor = (Cursor)cursor_ptr;
XDefineCursor(disp, win, cursor);
}
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateBlankCursor(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr) {
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateBlankCursor(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr) {
Display *disp = (Display *)(intptr_t)display_ptr;
Window win = (Window)window_ptr;
jobject handle_buffer = newJavaManagedByteBuffer(env, sizeof(Cursor));
if (handle_buffer == NULL) {
return NULL;
}
Cursor *cursor = (Cursor *)(*env)->GetDirectBufferAddress(env, handle_buffer);
unsigned int best_width, best_height;
if (XQueryBestCursor(disp, win, 1, 1, &best_width, &best_height) == 0) {
throwException(env, "Could not query best cursor size");
@ -475,9 +467,9 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nCreateBlankCursor(
XFillRectangle(disp, mask, gc, 0, 0, best_width, best_height);
XFreeGC(disp, gc);
XColor dummy_color;
*cursor = XCreatePixmapCursor(disp, mask, mask, &dummy_color, &dummy_color, 0, 0);
Cursor cursor = XCreatePixmapCursor(disp, mask, mask, &dummy_color, &dummy_color, 0, 0);
XFreePixmap(disp, mask);
return handle_buffer;
return cursor;
}
JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGetInputFocus(JNIEnv *env, jclass unused, jlong display_ptr) {