From 2966d7f68202d6ffadb0418a21139c36a71b7086 Mon Sep 17 00:00:00 2001 From: Duy Tran Khanh Date: Sat, 23 Jan 2021 16:20:39 +0700 Subject: [PATCH] LWJGLX input pipe update (from @artdeell changes) --- .../java/org/lwjgl/glfw/CallbackBridge.java | 18 +- .../src/main/java/org/lwjgl/glfw/GLFW.java | 4 +- .../main/java/org/lwjgl/input/EventQueue.java | 109 ++- .../src/main/java/org/lwjgl/input/Mouse.java | 791 +++++++++++++----- .../main/java/org/lwjgl/opengl/Display.java | 33 +- .../lwjgl/opengl/GLFWInputImplementation.java | 187 +++++ Natives/input_bridge_v3.c | 2 +- 7 files changed, 880 insertions(+), 264 deletions(-) create mode 100644 JavaApp/src/main/java/org/lwjgl/opengl/GLFWInputImplementation.java diff --git a/JavaApp/src/main/java/org/lwjgl/glfw/CallbackBridge.java b/JavaApp/src/main/java/org/lwjgl/glfw/CallbackBridge.java index baa4b654..aecb87cb 100644 --- a/JavaApp/src/main/java/org/lwjgl/glfw/CallbackBridge.java +++ b/JavaApp/src/main/java/org/lwjgl/glfw/CallbackBridge.java @@ -111,10 +111,22 @@ public class CallbackBridge { } // volatile private static boolean isGrabbing = false; - + public static class PusherRunnable implements Runnable { + int button; int x; int y; + public PusherRunnable(int button, int x, int y) { + this.button = button; + this.x = x; + this.y = y; + } + @Override + public void run() { + putMouseEventWithCoords(button, 1, x, y); + try { Thread.sleep(40); } catch (InterruptedException e) {} + putMouseEventWithCoords(button, 0, x, y); + } + } public static void putMouseEventWithCoords(int button, int x, int y /* , int dz, long nanos */) { - putMouseEventWithCoords(button, 1, x, y); - putMouseEventWithCoords(button, 0, x, y); + new Thread(new PusherRunnable(button,x,y)).run(); } public static void putMouseEventWithCoords(int button, int state, int x, int y /* , int dz, long nanos */) { diff --git a/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java b/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java index e3142919..e566b093 100644 --- a/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java +++ b/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java @@ -495,6 +495,7 @@ public class GLFW private static ArrayMap mGLFWWindowMap; public static boolean mGLFWIsGrabbing, mGLFWIsInputReady, mGLFWIsUseStackQueue = false; + public static final byte[] keyDownBuffer = new byte[316]; static { // Minecraft triggers a glfwPollEvents() on splash screen, so update window size there. @@ -1075,6 +1076,7 @@ public class GLFW break; case CallbackBridge.EVENT_TYPE_KEY: if (mGLFWKeyCallback != null) { + keyDownBuffer[dataArr[1]-32]=(byte)(int)dataArr[3]; mGLFWKeyCallback.invoke(ptr, dataArr[1], dataArr[2], dataArr[3], dataArr[4]); } break; @@ -1159,7 +1161,7 @@ public class GLFW } public static int glfwGetKey(@NativeType("GLFWwindow *") long window, int key) { - return 0; + return keyDownBuffer[key-32]; } public static int glfwGetMouseButton(@NativeType("GLFWwindow *") long window, int button) { diff --git a/JavaApp/src/main/java/org/lwjgl/input/EventQueue.java b/JavaApp/src/main/java/org/lwjgl/input/EventQueue.java index c498facf..39c7ba23 100644 --- a/JavaApp/src/main/java/org/lwjgl/input/EventQueue.java +++ b/JavaApp/src/main/java/org/lwjgl/input/EventQueue.java @@ -1,61 +1,84 @@ + +/* + * Copyright (c) 2002-2008 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ package org.lwjgl.input; /** - * Internal utility class to keep track of event positions in an array. When the - * array is full the position will wrap to the beginning. + * A java implementation of a LWJGL compatible event queue. + * @author elias_naur */ + +import java.nio.ByteBuffer; + class EventQueue { + private static final int QUEUE_SIZE = 200; - private int maxEvents = 32; - private int currentEventPos = -1; - private int nextEventPos = 0; + private final int event_size; - EventQueue(int maxEvents) { - this.maxEvents = maxEvents; + private final ByteBuffer queue; + + protected EventQueue(int event_size) { + this.event_size = event_size; + this.queue = ByteBuffer.allocate(QUEUE_SIZE*event_size); + } + + protected synchronized void clearEvents() { + queue.clear(); } /** - * add event to the queue + * Copy available events into the specified buffer. */ - void add() { - nextEventPos++; // increment next event position - if (nextEventPos == maxEvents) - nextEventPos = 0; // wrap next event position - - if (nextEventPos == currentEventPos) { - currentEventPos++; // skip oldest event is queue full - if (currentEventPos == maxEvents) - currentEventPos = 0; // wrap current event position - } + public synchronized void copyEvents(ByteBuffer dest) { + queue.flip(); + int old_limit = queue.limit(); + if (dest.remaining() < queue.remaining()) + queue.limit(dest.remaining() + queue.position()); + dest.put(queue); + queue.limit(old_limit); + queue.compact(); } /** - * Increment the event queue - * - * @return - true if there is an event available + * Put an event into the queue. + * @return true if the event fitted into the queue, false otherwise */ - boolean next() { - if (currentEventPos == nextEventPos - 1) + public synchronized boolean putEvent(ByteBuffer event) { + if (event.remaining() != event_size) + throw new IllegalArgumentException("Internal error: event size " + event_size + " does not equal the given event size " + event.remaining()); + if (queue.remaining() >= event.remaining()) { + queue.put(event); + return true; + } else return false; - if (nextEventPos == 0 && currentEventPos == maxEvents - 1) - return false; - - currentEventPos++; // increment current event position - if (currentEventPos == maxEvents) - currentEventPos = 0; // wrap current event position - - return true; - } - - int getMaxEvents() { - return maxEvents; - } - - int getCurrentPos() { - return currentEventPos; - } - - int getNextPos() { - return nextEventPos; } } diff --git a/JavaApp/src/main/java/org/lwjgl/input/Mouse.java b/JavaApp/src/main/java/org/lwjgl/input/Mouse.java index 452fdaf9..b933a3f6 100644 --- a/JavaApp/src/main/java/org/lwjgl/input/Mouse.java +++ b/JavaApp/src/main/java/org/lwjgl/input/Mouse.java @@ -1,241 +1,628 @@ -package org.lwjgl.input; +/* + * Copyright (c) 2002-2008 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + package org.lwjgl.input; -import org.lwjgl.glfw.GLFW; -import org.lwjgl.system.MemoryUtil; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.HashMap; +import java.util.Map; + +import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; +import org.lwjgl.LWJGLUtil; import org.lwjgl.Sys; import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.InputImplementation; + +/** + *
+ * A raw Mouse interface. This can be used to poll the current state of the + * mouse buttons, and determine the mouse movement delta since the last poll. + * + * n buttons supported, n being a native limit. A scrolly wheel is also + * supported, if one such is available. Movement is reported as delta from + * last position or as an absolute position. If the window has been created + * the absolute position will be clamped to 0 - width | height. + * + * @author cix_foo + * @author elias_naur + * @author Brian Matzon + * @version $Revision$ + * $Id$ + */ public class Mouse { + /** Internal use - event size in bytes */ + public static final int EVENT_SIZE = 1 + 1 + 4 + 4 + 4 + 8; - private static boolean grabbed = false; + /** Has the mouse been created? */ + private static boolean created; - private static int lastX = 0; - private static int lastY = 0; + /** The mouse buttons status from the last poll */ + private static ByteBuffer buttons; - private static int latestX = 0; - private static int latestY = 0; + /** Mouse absolute X position in pixels */ + private static int x; - private static int x = 0; - private static int y = 0; + /** Mouse absolute Y position in pixels */ + private static int y; - private static int lastDWheel = 0; + /** Mouse absolute X position in pixels without any clipping */ + private static int absolute_x; - private static EventQueue queue = new EventQueue(32); + /** Mouse absolute Y position in pixels without any clipping */ + private static int absolute_y; - private static int[] buttonEvents = new int[queue.getMaxEvents()]; - private static int[] wheelEvents = new int[queue.getMaxEvents()]; - private static boolean[] buttonEventStates = new boolean[queue.getMaxEvents()]; - private static int[] xEvents = new int[queue.getMaxEvents()]; - private static int[] yEvents = new int[queue.getMaxEvents()]; - private static int[] lastxEvents = new int[queue.getMaxEvents()]; - private static int[] lastyEvents = new int[queue.getMaxEvents()]; - private static long[] nanoTimeEvents = new long[queue.getMaxEvents()]; + /** Buffer to hold the deltas dx, dy and dwheel */ + private static IntBuffer coord_buffer; - private static boolean clipPostionToDisplay = true; + /** Delta X */ + private static int dx; - private static boolean isMouseInsideWindow = true; + /** Delta Y */ + private static int dy; - private static Cursor currentCursor = null; + /** Delta Z */ + private static int dwheel; - public static void addMoveEvent(double mouseX, double mouseY) { - latestX = (int) mouseX; - latestY = Display.getHeight() - (int) mouseY; + /** Number of buttons supported by the mouse */ + private static int buttonCount = -1; - lastxEvents[queue.getNextPos()] = xEvents[queue.getNextPos()]; - lastyEvents[queue.getNextPos()] = yEvents[queue.getNextPos()]; + /** Does this mouse support a scroll wheel */ + private static boolean hasWheel; - xEvents[queue.getNextPos()] = latestX; - yEvents[queue.getNextPos()] = latestY; + /** The current native cursor, if any */ + private static Cursor currentCursor; - buttonEvents[queue.getNextPos()] = -1; - buttonEventStates[queue.getNextPos()] = false; + /** Button names. These are set upon create(), to names like BUTTON0, BUTTON1, etc. */ + private static String[] buttonName; - wheelEvents[queue.getNextPos()] = 0; + /** hashmap of button names, for fast lookup */ + private static final Map buttonMap = new HashMap(16); - nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime(); + /** Lazy initialization */ + private static boolean initialized; - queue.add(); - } + /** The mouse button events from the last read */ + private static ByteBuffer readBuffer; - public static void addButtonEvent(int button, boolean pressed) { - lastxEvents[queue.getNextPos()] = xEvents[queue.getNextPos()]; - lastyEvents[queue.getNextPos()] = yEvents[queue.getNextPos()]; + /** The current mouse event button being examined */ + private static int eventButton; - xEvents[queue.getNextPos()] = latestX; - yEvents[queue.getNextPos()] = latestY; + /** The current state of the button being examined in the event queue */ + private static boolean eventState; - buttonEvents[queue.getNextPos()] = button; - buttonEventStates[queue.getNextPos()] = pressed; + /** The current delta of the mouse in the event queue */ + private static int event_dx; + private static int event_dy; + private static int event_dwheel; + /** The current absolute position of the mouse in the event queue */ + private static int event_x; + private static int event_y; + private static long event_nanos; + /** The position of the mouse it was grabbed at */ + private static int grab_x; + private static int grab_y; + /** The last absolute mouse event position (before clipping) for delta computation */ + private static int last_event_raw_x; + private static int last_event_raw_y; - wheelEvents[queue.getNextPos()] = 0; + /** Buffer size in events */ + private static final int BUFFER_SIZE = 50; - nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime(); + private static boolean isGrabbed; - queue.add(); - } + private static InputImplementation implementation; - public static void addWheelEvent(int wheel) { - lastxEvents[queue.getNextPos()] = xEvents[queue.getNextPos()]; - lastyEvents[queue.getNextPos()] = yEvents[queue.getNextPos()]; + /** Whether we need cursor animation emulation */ + private static final boolean emulateCursorAnimation = LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_WINDOWS || + LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_MACOSX; - xEvents[queue.getNextPos()] = latestX; - yEvents[queue.getNextPos()] = latestY; + private static boolean clipMouseCoordinatesToWindow = !getPrivilegedBoolean("org.lwjgl.input.Mouse.allowNegativeMouseCoords"); - buttonEvents[queue.getNextPos()] = -1; - buttonEventStates[queue.getNextPos()] = false; + /** + * Mouse cannot be constructed. + */ + private Mouse() { + } - wheelEvents[queue.getNextPos()] = wheel; - lastDWheel = wheel; - - nanoTimeEvents[queue.getNextPos()] = Sys.getNanoTime(); - - queue.add(); - } - - public static void setMouseInsideWindow(boolean mouseInsideWindow) { - isMouseInsideWindow = mouseInsideWindow; - } - - public static void poll() { - lastX = x; - lastY = y; - - if (!grabbed && clipPostionToDisplay) { - if (latestX < 0) - latestX = 0; - if (latestY < 0) - latestY = 0; - if (latestX > Display.getWidth() - 1) - latestX = Display.getWidth() - 1; - if (latestY > Display.getHeight() - 1) - latestY = Display.getHeight() - 1; - } - - x = latestX; - y = latestY; - } - - public static void create() throws LWJGLException { - - } - - public static boolean isCreated() { - return Display.isCreated(); - } - - public static void setGrabbed(boolean grab) { - GLFW.glfwSetInputMode(Display.getWindow(), GLFW.GLFW_CURSOR, - grab ? GLFW.GLFW_CURSOR_DISABLED : GLFW.GLFW_CURSOR_NORMAL); - grabbed = grab; - } - - public static boolean isGrabbed() { - return grabbed; - } - - public static boolean isButtonDown(int button) { - return GLFW.glfwGetMouseButton(Display.getWindow(), button) == GLFW.GLFW_PRESS; - } - - public static boolean next() { - return queue.next(); - } - - public static int getEventX() { - return xEvents[queue.getCurrentPos()]; - } - - public static int getEventY() { - return yEvents[queue.getCurrentPos()]; - } - - public static int getEventDX() { - return xEvents[queue.getCurrentPos()] - lastxEvents[queue.getCurrentPos()]; - } - - public static int getEventDY() { - return yEvents[queue.getCurrentPos()] - lastyEvents[queue.getCurrentPos()]; - } - - public static long getEventNanoseconds() { - return nanoTimeEvents[queue.getCurrentPos()]; - } - - public static int getEventButton() { - return buttonEvents[queue.getCurrentPos()]; - } - - public static boolean getEventButtonState() { - return buttonEventStates[queue.getCurrentPos()]; - } - - public static int getEventDWheel() { - return wheelEvents[queue.getCurrentPos()]; - } - - public static int getX() { - return x; - } - - public static int getY() { - return y; - } - - public static int getDX() { - return x - lastX; - } - - public static int getDY() { - return y - lastY; - } - - public static int getDWheel() { - int dwheel = lastDWheel; - lastDWheel = 0; - return dwheel; - } - - public static int getButtonCount() { - return 8; // max mouse buttons supported by GLFW - } - - public static boolean isInsideWindow() { - return isMouseInsideWindow; - } - - public static void setClipMouseCoordinatesToWindow(boolean clip) { - clipPostionToDisplay = clip; - } - - public static void setCursorPosition(int new_x, int new_y) { - GLFW.glfwSetCursorPos(Display.getWindow(), new_x, new_y); - } - - public static Cursor setNativeCursor(Cursor cursor) throws LWJGLException { - if (cursor == null) { - GLFW.glfwSetCursor(Display.getWindow(), MemoryUtil.NULL); - return null; - } - - GLFW.glfwSetCursor(Display.getWindow(), cursor.getHandle()); - currentCursor = cursor; - return cursor; - } - - public static Cursor getCurrentCursor() { - return currentCursor; - } - + /** + * Gets the currently bound native cursor, if any. + * + * @return the currently bound native cursor, if any. + */ public static Cursor getNativeCursor() { + return currentCursor; + + } + + /** + * Binds a native cursor. If the cursor argument is null, any + * currently bound native cursor is disabled, and the cursor reverts + * to the default operating system supplied cursor. + * + * NOTE: The native cursor is not constrained to the window, but + * relative events will not be generated if the cursor is outside. + * + * @param cursor the native cursor object to bind. May be null. + * @return The previous Cursor object set, or null. + * @throws LWJGLException if the cursor could not be set for any reason + */ + public static Cursor setNativeCursor(Cursor cursor) throws LWJGLException { + //dummy + currentCursor = cursor; return currentCursor; } - - public static boolean hasWheel() { - return true; + + public static boolean isClipMouseCoordinatesToWindow() { + return clipMouseCoordinatesToWindow; } - public static void destroy() { + public static void setClipMouseCoordinatesToWindow(boolean clip) { + clipMouseCoordinatesToWindow = clip; + } - } -} + /** + * Set the position of the cursor. If the cursor is not grabbed, + * the native cursor is moved to the new position. + * + * @param new_x The x coordinate of the new cursor position in OpenGL coordinates relative + * to the window origin. + * @param new_y The y coordinate of the new cursor position in OpenGL coordinates relative + * to the window origin. + */ + public static void setCursorPosition(int new_x, int new_y) { + //dummy + } + + /** + * Static initialization + */ + private static void initialize() { + Sys.initialize(); + + // Assign names to all the buttons + buttonName = new String[16]; + for (int i = 0; i < 16; i++) { + buttonName[i] = "BUTTON" + i; + buttonMap.put(buttonName[i], i); + } + + initialized = true; + } + + private static void resetMouse() { + dx = dy = dwheel = 0; + readBuffer.position(readBuffer.limit()); + } + + static InputImplementation getImplementation() { + return implementation; + } + + /** + * "Create" the mouse with the given custom implementation. This is used + * reflectively by AWTInputAdapter. + * + * @throws LWJGLException if the mouse could not be created for any reason + */ + private static void create(InputImplementation impl) throws LWJGLException { + if (created) + return; + if (!initialized) + initialize(); + implementation = impl; + implementation.createMouse(); + hasWheel = implementation.hasWheel(); + created = true; + + // set mouse buttons + buttonCount = implementation.getButtonCount(); + buttons = BufferUtils.createByteBuffer(buttonCount); + coord_buffer = BufferUtils.createIntBuffer(3); + if (currentCursor != null && implementation.getNativeCursorCapabilities() != 0) + setNativeCursor(currentCursor); + readBuffer = ByteBuffer.allocate(EVENT_SIZE * BUFFER_SIZE); + readBuffer.limit(0); + setGrabbed(isGrabbed); + } + + /** + * "Create" the mouse. The display must first have been created. + * Initially, the mouse is not grabbed and the delta values are reported + * with respect to the center of the display. + * + * @throws LWJGLException if the mouse could not be created for any reason + */ + public static void create() throws LWJGLException { + if (!Display.isCreated()) throw new IllegalStateException("Display must be created."); + + create((InputImplementation) GLFWInputImplementation.singleton); + } + + /** + * @return true if the mouse has been created + */ + public static boolean isCreated() { + return created; + } + + /** + * "Destroy" the mouse. + */ + public static void destroy() { + if (!created) return; + created = false; + buttons = null; + coord_buffer = null; + + implementation.destroyMouse(); + } + + /** + * Polls the mouse for its current state. Access the polled values using the + * get methods. + * By using this method, it is possible to "miss" mouse click events if you don't + * poll fast enough. + * + * To use buffered values, you have to call next for each event you + * want to read. You can query which button caused the event by using + * getEventButton. To get the state of that button, for that event, use + * getEventButtonState. + * + * NOTE: This method does not query the operating system for new events. To do that, + * Display.processMessages() (or Display.update()) must be called first. + * + * @see org.lwjgl.input.Mouse#next() + * @see org.lwjgl.input.Mouse#getEventButton() + * @see org.lwjgl.input.Mouse#getEventButtonState() + * @see org.lwjgl.input.Mouse#isButtonDown(int button) + * @see org.lwjgl.input.Mouse#getX() + * @see org.lwjgl.input.Mouse#getY() + * @see org.lwjgl.input.Mouse#getDX() + * @see org.lwjgl.input.Mouse#getDY() + * @see org.lwjgl.input.Mouse#getDWheel() + */ + public static void poll() { + if (!created) throw new IllegalStateException("Mouse must be created before you can poll it"); + implementation.pollMouse(coord_buffer, buttons); + + /* If we're grabbed, poll returns mouse deltas, if not it returns absolute coordinates */ + int poll_coord1 = coord_buffer.get(0); + int poll_coord2 = coord_buffer.get(1); + /* The wheel is always relative */ + int poll_dwheel = coord_buffer.get(2); + + if (isGrabbed()) { + dx += poll_coord1; + dy += poll_coord2; + x += poll_coord1; + y += poll_coord2; + absolute_x += poll_coord1; + absolute_y += poll_coord2; + } else { + dx = poll_coord1 - absolute_x; + dy = poll_coord2 - absolute_y; + absolute_x = x = poll_coord1; + absolute_y = y = poll_coord2; + } + + if(clipMouseCoordinatesToWindow) { + x = Math.min(Display.getWidth() - 1, Math.max(0, x)); + y = Math.min(Display.getHeight() - 1, Math.max(0, y)); + } + + dwheel += poll_dwheel; + read(); + } + + private static void read() { + readBuffer.compact(); + implementation.readMouse(readBuffer); + readBuffer.flip(); + } + + /** + * See if a particular mouse button is down. + * + * @param button The index of the button you wish to test (0..getButtonCount-1) + * @return true if the specified button is down + */ + public static boolean isButtonDown(int button) { + if (!created) throw new IllegalStateException("Mouse must be created before you can poll the button state"); + if (button >= buttonCount || button < 0) + return false; + else + return buttons.get(button) == 1; + } + + /** + * Gets a button's name + * @param button The button + * @return a String with the button's human readable name in it or null if the button is unnamed + */ + public static String getButtonName(int button) { + if (button >= buttonName.length || button < 0) + return null; + else + return buttonName[button]; + } + + /** + * Get's a button's index. If the button is unrecognised then -1 is returned. + * @param buttonName The button name + */ + public static int getButtonIndex(String buttonName) { + Integer ret = buttonMap.get(buttonName); + if (ret == null) + return -1; + else + return ret; + } + + /** + * Gets the next mouse event. You can query which button caused the event by using + * getEventButton() (if any). To get the state of that key, for that event, use + * getEventButtonState. To get the current mouse delta values use getEventDX() + * and getEventDY(). + * @see org.lwjgl.input.Mouse#getEventButton() + * @see org.lwjgl.input.Mouse#getEventButtonState() + * @return true if a mouse event was read, false otherwise + */ + public static boolean next() { + if (!created) throw new IllegalStateException("Mouse must be created before you can read events"); + if (readBuffer.hasRemaining()) { + + eventButton = readBuffer.get(); + eventState = readBuffer.get() != 0; + if (isGrabbed()) { + event_dx = readBuffer.getInt(); + event_dy = readBuffer.getInt(); + event_x += event_dx; + event_y += event_dy; + last_event_raw_x = event_x; + last_event_raw_y = event_y; + } else { + int new_event_x = readBuffer.getInt(); + int new_event_y = readBuffer.getInt(); + event_dx = new_event_x - last_event_raw_x; + event_dy = new_event_y - last_event_raw_y; + event_x = new_event_x; + event_y = new_event_y; + last_event_raw_x = new_event_x; + last_event_raw_y = new_event_y; + } + if(clipMouseCoordinatesToWindow) { + event_x = Math.min(Display.getWidth() - 1, Math.max(0, event_x)); + event_y = Math.min(Display.getHeight() - 1, Math.max(0, event_y)); + } + event_dwheel = readBuffer.getInt(); + event_nanos = readBuffer.getLong(); + return true; + } else + return false; + } + + /** + * @return Current events button. Returns -1 if no button state was changed + */ + public static int getEventButton() { + return eventButton; + } + + /** + * Get the current events button state. + * @return Current events button state. + */ + public static boolean getEventButtonState() { + return eventState; + } + + /** + * @return Current events delta x. + */ + public static int getEventDX() { + return event_dx; + } + + /** + * @return Current events delta y. + */ + public static int getEventDY() { + return event_dy; + } + + /** + * @return Current events absolute x. + */ + public static int getEventX() { + return event_x; + } + + /** + * @return Current events absolute y. + */ + public static int getEventY() { + return event_y; + } + + /** + * @return Current events delta z + */ + public static int getEventDWheel() { + return event_dwheel; + } + + /** + * Gets the time in nanoseconds of the current event. + * Only useful for relative comparisons with other + * Mouse events, as the absolute time has no defined + * origin. + * + * @return The time in nanoseconds of the current event + */ + public static long getEventNanoseconds() { + return event_nanos; + } + + /** + * Retrieves the absolute position. It will be clamped to + * 0...width-1. + * + * @return Absolute x axis position of mouse + */ + public static int getX() { + return x; + } + + /** + * Retrieves the absolute position. It will be clamped to + * 0...height-1. + * + * @return Absolute y axis position of mouse + */ + public static int getY() { + return y; + } + + /** + * @return Movement on the x axis since last time getDX() was called. + */ + public static int getDX() { + int result = dx; + dx = 0; + return result; + } + + /** + * @return Movement on the y axis since last time getDY() was called. + */ + public static int getDY() { + int result = dy; + dy = 0; + return result; + } + + /** + * @return Movement of the wheel since last time getDWheel() was called + */ + public static int getDWheel() { + int result = dwheel; + dwheel = 0; + return result; + } + + /** + * @return Number of buttons on this mouse + */ + public static int getButtonCount() { + return buttonCount; + } + + /** + * @return Whether or not this mouse has wheel support + */ + public static boolean hasWheel() { + return hasWheel; + } + + /** + * @return whether or not the mouse has grabbed the cursor + */ + public static boolean isGrabbed() { + return isGrabbed; + } + + /** + * Sets whether or not the mouse has grabbed the cursor + * (and thus hidden). If grab is false, the getX() and getY() + * will return delta movement in pixels clamped to the display + * dimensions, from the center of the display. + * + * @param grab whether the mouse should be grabbed + */ + public static void setGrabbed(boolean grab) { + boolean grabbed = isGrabbed; + isGrabbed = grab; + if (isCreated()) { + //if (grab && !grabbed) { + // store location mouse was grabbed + // grab_x = x; + // grab_y = y; + } + //else if (!grab && grabbed) { + // // move mouse back to location it was grabbed before ungrabbing + // if ((Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) != 0) + // implementation.setCursorPosition(grab_x, grab_y); + //} + + implementation.grabMouse(grab); + // Get latest values from native side + poll(); + event_x = x; + event_y = y; + last_event_raw_x = x; + last_event_raw_y = y; + resetMouse(); + } + + /** + * Updates the cursor, so that animation can be changed if needed. + * This method is called automatically by the window on its update, and + * shouldn't be called otherwise + */ + public static void updateCursor() { + //dummy + } + + /** Gets a boolean property as a privileged action. */ + static boolean getPrivilegedBoolean(final String property_name) { + Boolean value = AccessController.doPrivileged(new PrivilegedAction() { + public Boolean run() { + return Boolean.getBoolean(property_name); + } + }); + return value; + } + + /** + * Retrieves whether or not the mouse cursor is within the bounds of the window. + * If the mouse cursor was moved outside the display during a drag, then the result of calling + * this method will be true until the button is released. + * @return true if mouse is inside display, false otherwise. + */ + public static boolean isInsideWindow() { + return implementation.isInsideWindow(); + } +} \ No newline at end of file diff --git a/JavaApp/src/main/java/org/lwjgl/opengl/Display.java b/JavaApp/src/main/java/org/lwjgl/opengl/Display.java index 06177f46..f6020d6f 100644 --- a/JavaApp/src/main/java/org/lwjgl/opengl/Display.java +++ b/JavaApp/src/main/java/org/lwjgl/opengl/Display.java @@ -13,6 +13,8 @@ import java.nio.IntBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.glfw.*; import org.lwjgl.LWJGLUtil; +import org.lwjgl.input.GLFWInputImplementation; +import org.lwjgl.input.KeyCodes; import org.lwjgl.opengl.GL11; import org.lwjgl.system.MemoryUtil; import org.lwjgl.LWJGLException; @@ -302,35 +304,37 @@ public class Display { // false); // } - Keyboard.addKeyEvent(key, action); + GLFWInputImplementation.singleton.putKeyboardEvent(KeyCodes.toLwjglKey(key),(byte)action,0,Sys.getNanoTime(),false); } }; Window.charCallback = new GLFWCharCallback() { @Override public void invoke(long window, int codepoint) { - Keyboard.addCharEvent(latestEventKey, (char) codepoint); + GLFWInputImplementation.singleton.putKeyboardEvent(KeyCodes.toLwjglKey(32),(byte)1,(int)codepoint,Sys.getNanoTime(),false); + GLFWInputImplementation.singleton.putKeyboardEvent(KeyCodes.toLwjglKey(32),(byte)0,(int)codepoint,Sys.getNanoTime(),false); } }; Window.cursorEnterCallback = new GLFWCursorEnterCallback() { @Override public void invoke(long window, boolean entered) { - Mouse.setMouseInsideWindow(entered == true); + + } }; Window.cursorPosCallback = new GLFWCursorPosCallback() { @Override public void invoke(long window, double xpos, double ypos) { - Mouse.addMoveEvent(xpos, ypos); + GLFWInputImplementation.singleton.putMouseEventWithCoords((byte)-1, (byte)0,(int)xpos,(int)((ypos - Display.getHeight())*-1),0,Sys.getNanoTime()); } }; Window.mouseButtonCallback = new GLFWMouseButtonCallback() { @Override public void invoke(long window, int button, int action, int mods) { - Mouse.addButtonEvent(button, action == GLFW.GLFW_PRESS ? true : false); + GLFWInputImplementation.singleton.putMouseEventWithCoords((byte)button,(byte)action,-1,-1,0,Sys.getNanoTime()); } }; @@ -383,7 +387,8 @@ public class Display { Window.scrollCallback = new GLFWScrollCallback() { @Override public void invoke(long window, double xoffset, double yoffset) { - Mouse.addWheelEvent((int) (yoffset * 120)); + // Mouse.addWheelEvent((int) (yoffset * 120)); + GLFWInputImplementation.singleton.putMouseEventWithCoords((byte)-1,(byte)0,-1,-1,(int)(yoffset*120),Sys.getNanoTime()); } }; @@ -732,7 +737,7 @@ public class Display { } public static boolean isCreated() { - return displayCreated; + return true; } public static boolean isActive() { @@ -1056,12 +1061,12 @@ public class Display { if (Window.handle != MemoryUtil.NULL) glfwDestroyWindow(Window.handle); Window.handle = newWindow; - try { - Mouse.setNativeCursor(Mouse.getCurrentCursor()); - } catch (LWJGLException e) { - System.err.println("Failed to set new window cursor!"); - e.printStackTrace(); - } + //try { + //Mouse.setNativeCursor(Mouse.getCurrentCursor()); + //} catch (LWJGLException e) { + // System.err.println("Failed to set new window cursor!"); + // e.printStackTrace(); + //} GLFW.glfwSetWindowTitle(newWindow, windowTitle); Window.setCallbacks(); @@ -1121,4 +1126,4 @@ public class Display { } } -} +} \ No newline at end of file diff --git a/JavaApp/src/main/java/org/lwjgl/opengl/GLFWInputImplementation.java b/JavaApp/src/main/java/org/lwjgl/opengl/GLFWInputImplementation.java new file mode 100644 index 00000000..ed775b99 --- /dev/null +++ b/JavaApp/src/main/java/org/lwjgl/opengl/GLFWInputImplementation.java @@ -0,0 +1,187 @@ +package org.lwjgl.input; + +import org.lwjgl.LWJGLException; +import org.lwjgl.glfw.GLFW; +import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.InputImplementation; + +import java.nio.ByteBuffer; +import java.nio.IntBuffer; + +public class GLFWInputImplementation implements InputImplementation { + public static final GLFWInputImplementation singleton = new GLFWInputImplementation(); + private final ByteBuffer event_buffer = ByteBuffer.allocate(Mouse.EVENT_SIZE); + private EventQueue event_queue = new EventQueue(Mouse.EVENT_SIZE); + private final EventQueue keyboardEventQueue = new EventQueue(Keyboard.EVENT_SIZE); + private final ByteBuffer keyboardEvent = ByteBuffer.allocate(Keyboard.EVENT_SIZE); + public final byte[] key_down_buffer = new byte[Keyboard.KEYBOARD_SIZE]; + public final byte[] mouse_buffer = new byte[3]; + public int mouseX = 0; + public int mouseY = 0; + public int mouseLastEventX = 0; + public int mouseLastEventY = 0; + public int mouseLastX = 0; + public int mouseLastY = 0; + public int mouseComparatorX; + public int mouseComparatorY; + public boolean grab; + private long last_event_nanos = System.nanoTime(); + @Override + public boolean hasWheel() { + return true; + } + + @Override + public int getButtonCount() { + return 3; + } + + @Override + public void createMouse() throws LWJGLException { + } + + @Override + public void destroyMouse() { + + } + + @Override + public void pollMouse(IntBuffer coord_buffer, ByteBuffer buttons) { + coord_buffer.put(0, grab? mouseX - mouseLastX: mouseX); + coord_buffer.put(1, grab? mouseY - mouseLastY: mouseY); + //System.out.println("Poll Call: Buffer length="+buttons.capacity()+"; Pos="+buttons.position()); + buttons.rewind(); + buttons.put(mouse_buffer); + mouseLastX = mouseX; + mouseLastY = mouseY; + } + + @Override + public void readMouse(ByteBuffer buffer) { + event_queue.copyEvents(buffer); + } + + @Override + public void grabMouse(boolean new_grab) { + System.out.println("Grab: " + new_grab); + grab = new_grab; + GLFW.glfwSetInputMode(Display.getWindow(), GLFW.GLFW_CURSOR, + grab ? GLFW.GLFW_CURSOR_DISABLED : GLFW.GLFW_CURSOR_NORMAL); + } + + @Override + public int getNativeCursorCapabilities() { + return 0; + } + + @Override + public void setCursorPosition(int x, int y) { + + } + + @Override + public void setNativeCursor(Object handle) throws LWJGLException { + + } + + @Override + public int getMinCursorSize() { + return 0; + } + + @Override + public int getMaxCursorSize() { + return 0; + } + + @Override + public void createKeyboard() throws LWJGLException { + + } + + @Override + public void destroyKeyboard() { + + } + + @Override + public void pollKeyboard(ByteBuffer keyDownBuffer) { + int old_position = keyDownBuffer.position(); + keyDownBuffer.put(key_down_buffer); + keyDownBuffer.position(old_position); + } + + public void readKeyboard(ByteBuffer buffer) { + keyboardEventQueue.copyEvents(buffer); + } + + @Override + public Object createCursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException { + return null; + } + + @Override + public void destroyCursor(Object cursor_handle) { + + } + + @Override + public int getWidth() { + return Display.getWidth(); + } + + @Override + public int getHeight() { + return Display.getHeight(); + } + + @Override + public boolean isInsideWindow() { + return true; + } + + public void putMouseEventWithCoords(byte button, byte state, int coord1, int coord2, int dz, long nanos) { + int acoord1=0; + int acoord2=0; + if(coord1 == -1 && coord2 == -1) { + acoord1 = mouseX; + acoord2 = mouseY; + }else{ + acoord1 = coord1; + acoord2= coord2; + } + event_buffer.clear(); + event_buffer.put(button).put(state); + //always put deltas when grabbed + if (grab) { + event_buffer.putInt(acoord1-mouseX).putInt(acoord2-mouseY); + }else{ + event_buffer.putInt(acoord1).putInt(acoord2); + } + if(button != -1) { + mouse_buffer[button]=state; + } + event_buffer.putInt(dz).putLong(nanos); + event_buffer.flip(); + event_queue.putEvent(event_buffer); + last_event_nanos = nanos; + mouseX = acoord1; + mouseY = acoord2; + } + + public void setMouseButtonInGrabMode(byte button, byte state) { + long nanos = System.nanoTime(); + event_buffer.clear(); + event_buffer.put(button).put(state).putInt(0).putInt(0).putInt(0).putLong(nanos); + event_buffer.flip(); + event_queue.putEvent(event_buffer); + last_event_nanos = nanos; + } + public void putKeyboardEvent(int keycode, byte state, int ch, long nanos, boolean repeat) { + key_down_buffer[keycode] = state; + keyboardEvent.clear(); + keyboardEvent.putInt(keycode).put(state).putInt(ch).putLong(nanos).put(repeat ? (byte)1 : (byte)0); + keyboardEvent.flip(); + keyboardEventQueue.putEvent(keyboardEvent); + } +} \ No newline at end of file diff --git a/Natives/input_bridge_v3.c b/Natives/input_bridge_v3.c index 7c91cc8d..64d4088b 100644 --- a/Natives/input_bridge_v3.c +++ b/Natives/input_bridge_v3.c @@ -322,7 +322,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSendCursorPos(JN } if (!isUseStackQueueCall) { - GLFW_invoke_CursorPos(showingWindow, (double) (isGrabbing ? grabCursorX : x), (double) (isGrabbing ? grabCursorY : y)); + GLFW_invoke_CursorPos(showingWindow, (double) (x), (double) (y)); } else { sendData(EVENT_TYPE_CURSOR_POS, (isGrabbing ? grabCursorX : x), (isGrabbing ? grabCursorY : y), 0, 0); }