Synchronize Keyboard and Mouse to avoid problems with AWTInputAdapter based usage

This commit is contained in:
Elias Naur 2006-11-24 10:18:36 +00:00
parent 6abfeb4df2
commit 2577827e4a
2 changed files with 43 additions and 43 deletions

View File

@ -306,7 +306,7 @@ public class Keyboard {
* *
* @throws LWJGLException if the keyboard could not be created for any reason * @throws LWJGLException if the keyboard could not be created for any reason
*/ */
public static void create() throws LWJGLException { public static synchronized void create() throws LWJGLException {
if (!Display.isCreated()) throw new IllegalStateException("Display must be created."); if (!Display.isCreated()) throw new IllegalStateException("Display must be created.");
create(Mouse.createImplementation()); create(Mouse.createImplementation());
@ -324,14 +324,14 @@ public class Keyboard {
/** /**
* @return true if the keyboard has been created * @return true if the keyboard has been created
*/ */
public static boolean isCreated() { public static synchronized boolean isCreated() {
return created; return created;
} }
/** /**
* "Destroy" the keyboard * "Destroy" the keyboard
*/ */
public static void destroy() { public static synchronized void destroy() {
if (!created) if (!created)
return; return;
created = false; created = false;
@ -357,7 +357,7 @@ public class Keyboard {
* @see org.lwjgl.input.Keyboard#getEventKeyState() * @see org.lwjgl.input.Keyboard#getEventKeyState()
* @see org.lwjgl.input.Keyboard#getEventCharacter() * @see org.lwjgl.input.Keyboard#getEventCharacter()
*/ */
public static void poll() { public static synchronized void poll() {
if (!created) if (!created)
throw new IllegalStateException("Keyboard must be created before you can poll the device"); throw new IllegalStateException("Keyboard must be created before you can poll the device");
implementation.pollKeyboard(keyDownBuffer); implementation.pollKeyboard(keyDownBuffer);
@ -375,7 +375,7 @@ public class Keyboard {
* @param key Keycode to check * @param key Keycode to check
* @return true if the key is down according to the last poll() * @return true if the key is down according to the last poll()
*/ */
public static boolean isKeyDown(int key) { public static synchronized boolean isKeyDown(int key) {
if (!created) if (!created)
throw new IllegalStateException("Keyboard must be created before you can query key state"); throw new IllegalStateException("Keyboard must be created before you can query key state");
return keyDownBuffer.get(key) != 0; return keyDownBuffer.get(key) != 0;
@ -398,7 +398,7 @@ public class Keyboard {
* @param key The key * @param key The key
* @return a String with the key's human readable name in it or null if the key is unnamed * @return a String with the key's human readable name in it or null if the key is unnamed
*/ */
public static String getKeyName(int key) { public static synchronized String getKeyName(int key) {
return keyName[key]; return keyName[key];
} }
@ -406,7 +406,7 @@ public class Keyboard {
* Get's a key's index. If the key is unrecognised then KEY_NONE is returned. * Get's a key's index. If the key is unrecognised then KEY_NONE is returned.
* @param keyName The key name * @param keyName The key name
*/ */
public static int getKeyIndex(String keyName) { public static synchronized int getKeyIndex(String keyName) {
Integer ret = (Integer) keyMap.get(keyName); Integer ret = (Integer) keyMap.get(keyName);
if (ret == null) if (ret == null)
return KEY_NONE; return KEY_NONE;
@ -418,7 +418,7 @@ public class Keyboard {
* Gets the number of keyboard events waiting after doing a buffer enabled poll(). * Gets the number of keyboard events waiting after doing a buffer enabled poll().
* @return the number of keyboard events * @return the number of keyboard events
*/ */
public static int getNumKeyboardEvents() { public static synchronized int getNumKeyboardEvents() {
if (!created) if (!created)
throw new IllegalStateException("Keyboard must be created before you can read events"); throw new IllegalStateException("Keyboard must be created before you can read events");
return readBuffer.remaining()/EVENT_SIZE; return readBuffer.remaining()/EVENT_SIZE;
@ -435,7 +435,7 @@ public class Keyboard {
* @see org.lwjgl.input.Keyboard#getEventCharacter() * @see org.lwjgl.input.Keyboard#getEventCharacter()
* @return true if a keyboard event was read, false otherwise * @return true if a keyboard event was read, false otherwise
*/ */
public static boolean next() { public static synchronized boolean next() {
if (!created) if (!created)
throw new IllegalStateException("Keyboard must be created before you can read events"); throw new IllegalStateException("Keyboard must be created before you can read events");
@ -453,14 +453,14 @@ public class Keyboard {
/** /**
* @return Number of keys on this keyboard * @return Number of keys on this keyboard
*/ */
public static int getKeyCount() { public static synchronized int getKeyCount() {
return keyCount; return keyCount;
} }
/** /**
* @return The character from the current event * @return The character from the current event
*/ */
public static char getEventCharacter() { public static synchronized char getEventCharacter() {
return (char)eventCharacter; return (char)eventCharacter;
} }
@ -471,7 +471,7 @@ public class Keyboard {
* *
* @return The key from the current event * @return The key from the current event
*/ */
public static int getEventKey() { public static synchronized int getEventKey() {
return eventKey; return eventKey;
} }
@ -481,7 +481,7 @@ public class Keyboard {
* *
* @return True if key was down, or false if released * @return True if key was down, or false if released
*/ */
public static boolean getEventKeyState() { public static synchronized boolean getEventKeyState() {
return eventState; return eventState;
} }
@ -492,7 +492,7 @@ public class Keyboard {
* origin. * origin.
* @return The time in nanoseconds of the current event * @return The time in nanoseconds of the current event
*/ */
public static long getEventNanoseconds() { public static synchronized long getEventNanoseconds() {
return eventNanos; return eventNanos;
} }
} }

View File

@ -150,7 +150,7 @@ public class Mouse {
* *
* @return the currently bound native cursor, if any. * @return the currently bound native cursor, if any.
*/ */
public static Cursor getNativeCursor() { public static synchronized Cursor getNativeCursor() {
return currentCursor; return currentCursor;
} }
@ -165,7 +165,7 @@ public class Mouse {
* @return The previous Cursor object set, or null. * @return The previous Cursor object set, or null.
* @throws LWJGLException if the cursor could not be set for any reason * @throws LWJGLException if the cursor could not be set for any reason
*/ */
public static Cursor setNativeCursor(Cursor cursor) throws LWJGLException { public static synchronized Cursor setNativeCursor(Cursor cursor) throws LWJGLException {
if ((Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) == 0) if ((Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) == 0)
throw new IllegalStateException("Mouse doesn't support native cursors"); throw new IllegalStateException("Mouse doesn't support native cursors");
Cursor oldCursor = currentCursor; Cursor oldCursor = currentCursor;
@ -190,7 +190,7 @@ public class Mouse {
* @param y The y coordinate of the new cursor position in OpenGL coordinates relative * @param y The y coordinate of the new cursor position in OpenGL coordinates relative
* to the window origin. * to the window origin.
*/ */
public static void setCursorPosition(int new_x, int new_y) { public static synchronized void setCursorPosition(int new_x, int new_y) {
if (!isCreated()) if (!isCreated())
throw new IllegalStateException("Mouse is not created"); throw new IllegalStateException("Mouse is not created");
x = event_x = new_x; x = event_x = new_x;
@ -275,7 +275,7 @@ public class Mouse {
* *
* @throws LWJGLException if the mouse could not be created for any reason * @throws LWJGLException if the mouse could not be created for any reason
*/ */
public static void create() throws LWJGLException { public static synchronized void create() throws LWJGLException {
if (!Display.isCreated()) throw new IllegalStateException("Display must be created."); if (!Display.isCreated()) throw new IllegalStateException("Display must be created.");
create(createImplementation()); create(createImplementation());
@ -284,14 +284,14 @@ public class Mouse {
/** /**
* @return true if the mouse has been created * @return true if the mouse has been created
*/ */
public static boolean isCreated() { public static synchronized boolean isCreated() {
return created; return created;
} }
/** /**
* "Destroy" the mouse. * "Destroy" the mouse.
*/ */
public static void destroy() { public static synchronized void destroy() {
if (!created) return; if (!created) return;
created = false; created = false;
buttons = null; buttons = null;
@ -321,7 +321,7 @@ public class Mouse {
* @see org.lwjgl.input.Mouse#getDY() * @see org.lwjgl.input.Mouse#getDY()
* @see org.lwjgl.input.Mouse#getDWheel() * @see org.lwjgl.input.Mouse#getDWheel()
*/ */
public static void poll() { public static synchronized void poll() {
if (!created) throw new IllegalStateException("Mouse must be created before you can poll it"); if (!created) throw new IllegalStateException("Mouse must be created before you can poll it");
implementation.pollMouse(coord_buffer, buttons); implementation.pollMouse(coord_buffer, buttons);
@ -360,7 +360,7 @@ public class Mouse {
* @param button The index of the button you wish to test (0..getButtonCount-1) * @param button The index of the button you wish to test (0..getButtonCount-1)
* @return true if the specified button is down * @return true if the specified button is down
*/ */
public static boolean isButtonDown(int button) { public static synchronized boolean isButtonDown(int button) {
if (!created) throw new IllegalStateException("Mouse must be created before you can poll the button state"); if (!created) throw new IllegalStateException("Mouse must be created before you can poll the button state");
if (button >= buttonCount || button < 0) if (button >= buttonCount || button < 0)
return false; return false;
@ -373,7 +373,7 @@ public class Mouse {
* @param button The button * @param button The button
* @return a String with the button's human readable name in it or null if the button is unnamed * @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) { public static synchronized String getButtonName(int button) {
if (button >= buttonName.length || button < 0) if (button >= buttonName.length || button < 0)
return null; return null;
else else
@ -384,7 +384,7 @@ public class Mouse {
* Get's a button's index. If the button is unrecognised then -1 is returned. * Get's a button's index. If the button is unrecognised then -1 is returned.
* @param buttonName The button name * @param buttonName The button name
*/ */
public static int getButtonIndex(String buttonName) { public static synchronized int getButtonIndex(String buttonName) {
Integer ret = (Integer) buttonMap.get(buttonName); Integer ret = (Integer) buttonMap.get(buttonName);
if (ret == null) if (ret == null)
return -1; return -1;
@ -401,7 +401,7 @@ public class Mouse {
* @see org.lwjgl.input.Mouse#getEventButtonState() * @see org.lwjgl.input.Mouse#getEventButtonState()
* @return true if a mouse event was read, false otherwise * @return true if a mouse event was read, false otherwise
*/ */
public static boolean next() { public static synchronized boolean next() {
if (!created) throw new IllegalStateException("Mouse must be created before you can read events"); if (!created) throw new IllegalStateException("Mouse must be created before you can read events");
if (readBuffer.hasRemaining()) { if (readBuffer.hasRemaining()) {
eventButton = readBuffer.get(); eventButton = readBuffer.get();
@ -431,7 +431,7 @@ public class Mouse {
/** /**
* @return Current events button. Returns -1 if no button state was changed * @return Current events button. Returns -1 if no button state was changed
*/ */
public static int getEventButton() { public static synchronized int getEventButton() {
return eventButton; return eventButton;
} }
@ -439,42 +439,42 @@ public class Mouse {
* Get the current events button state. * Get the current events button state.
* @return Current events button state. * @return Current events button state.
*/ */
public static boolean getEventButtonState() { public static synchronized boolean getEventButtonState() {
return eventState; return eventState;
} }
/** /**
* @return Current events delta x. Only valid when the mouse is grabbed. * @return Current events delta x. Only valid when the mouse is grabbed.
*/ */
public static int getEventDX() { public static synchronized int getEventDX() {
return event_dx; return event_dx;
} }
/** /**
* @return Current events delta y. Only valid when the mouse is grabbed. * @return Current events delta y. Only valid when the mouse is grabbed.
*/ */
public static int getEventDY() { public static synchronized int getEventDY() {
return event_dy; return event_dy;
} }
/** /**
* @return Current events absolute x. Only valid when the mouse is not grabbed. * @return Current events absolute x. Only valid when the mouse is not grabbed.
*/ */
public static int getEventX() { public static synchronized int getEventX() {
return event_x; return event_x;
} }
/** /**
* @return Current events absolute y. Only valid when the mouse is not grabbed. * @return Current events absolute y. Only valid when the mouse is not grabbed.
*/ */
public static int getEventY() { public static synchronized int getEventY() {
return event_y; return event_y;
} }
/** /**
* @return Current events delta z * @return Current events delta z
*/ */
public static int getEventDWheel() { public static synchronized int getEventDWheel() {
return event_dwheel; return event_dwheel;
} }
@ -486,7 +486,7 @@ public class Mouse {
* *
* @return The time in nanoseconds of the current event * @return The time in nanoseconds of the current event
*/ */
public static long getEventNanoseconds() { public static synchronized long getEventNanoseconds() {
return event_nanos; return event_nanos;
} }
@ -496,7 +496,7 @@ public class Mouse {
* *
* @return Absolute x axis position of mouse * @return Absolute x axis position of mouse
*/ */
public static int getX() { public static synchronized int getX() {
return x; return x;
} }
@ -506,14 +506,14 @@ public class Mouse {
* *
* @return Absolute y axis position of mouse * @return Absolute y axis position of mouse
*/ */
public static int getY() { public static synchronized int getY() {
return y; return y;
} }
/** /**
* @return Movement on the x axis since last time getDX() was called. Only valid when the mouse is grabbed. * @return Movement on the x axis since last time getDX() was called. Only valid when the mouse is grabbed.
*/ */
public static int getDX() { public static synchronized int getDX() {
int result = dx; int result = dx;
dx = 0; dx = 0;
return result; return result;
@ -522,7 +522,7 @@ public class Mouse {
/** /**
* @return Movement on the y axis since last time getDY() was called. Only valid when the mouse is grabbed. * @return Movement on the y axis since last time getDY() was called. Only valid when the mouse is grabbed.
*/ */
public static int getDY() { public static synchronized int getDY() {
int result = dy; int result = dy;
dy = 0; dy = 0;
return result; return result;
@ -531,7 +531,7 @@ public class Mouse {
/** /**
* @return Movement of the wheel since last time getDWheel() was called * @return Movement of the wheel since last time getDWheel() was called
*/ */
public static int getDWheel() { public static synchronized int getDWheel() {
int result = dwheel; int result = dwheel;
dwheel = 0; dwheel = 0;
return result; return result;
@ -540,21 +540,21 @@ public class Mouse {
/** /**
* @return Number of buttons on this mouse * @return Number of buttons on this mouse
*/ */
public static int getButtonCount() { public static synchronized int getButtonCount() {
return buttonCount; return buttonCount;
} }
/** /**
* @return Whether or not this mouse has wheel support * @return Whether or not this mouse has wheel support
*/ */
public static boolean hasWheel() { public static synchronized boolean hasWheel() {
return hasWheel; return hasWheel;
} }
/** /**
* @return whether or not the mouse has grabbed the cursor * @return whether or not the mouse has grabbed the cursor
*/ */
public static boolean isGrabbed() { public static synchronized boolean isGrabbed() {
return isGrabbed; return isGrabbed;
} }
@ -566,7 +566,7 @@ public class Mouse {
* *
* @param grab whether the mouse should be grabbed * @param grab whether the mouse should be grabbed
*/ */
public static void setGrabbed(boolean grab) { public static synchronized void setGrabbed(boolean grab) {
isGrabbed = grab; isGrabbed = grab;
if (isCreated()) { if (isCreated()) {
implementation.grabMouse(isGrabbed); implementation.grabMouse(isGrabbed);
@ -579,7 +579,7 @@ public class Mouse {
* This method is called automatically by the window on its update, and * This method is called automatically by the window on its update, and
* shouldn't be called otherwise * shouldn't be called otherwise
*/ */
public static void updateCursor() { public static synchronized void updateCursor() {
if (isWindows && currentCursor != null && currentCursor.hasTimedOut()) { if (isWindows && currentCursor != null && currentCursor.hasTimedOut()) {
currentCursor.nextCursor(); currentCursor.nextCursor();
try { try {