Made Display.getImplementation package private, and made the input.* packages access it through reflection.

This commit is contained in:
Elias Naur 2006-10-08 09:05:16 +00:00
parent 55f67e68cb
commit ec9b449b78
4 changed files with 54 additions and 23 deletions

View File

@ -38,7 +38,7 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil; import org.lwjgl.LWJGLUtil;
import org.lwjgl.Sys; import org.lwjgl.Sys;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayImplementation;
/** /**
* *
@ -115,7 +115,7 @@ public class Cursor {
* @return the maximum size of a native cursor * @return the maximum size of a native cursor
*/ */
public static int getMinCursorSize() { public static int getMinCursorSize() {
return Display.getImplementation().getMinCursorSize(); return Mouse.getImplementation().getMinCursorSize();
} }
/** /**
@ -126,7 +126,7 @@ public class Cursor {
* @return the maximum size of a native cursor * @return the maximum size of a native cursor
*/ */
public static int getMaxCursorSize() { public static int getMaxCursorSize() {
return Display.getImplementation().getMaxCursorSize(); return Mouse.getImplementation().getMaxCursorSize();
} }
/** /**
@ -138,7 +138,7 @@ public class Cursor {
* @return A bit mask with native cursor capabilities. * @return A bit mask with native cursor capabilities.
*/ */
public static int getCapabilities() { public static int getCapabilities() {
return Display.getImplementation().getNativeCursorCapabilities(); return Mouse.getImplementation().getNativeCursorCapabilities();
} }
/** /**
@ -164,7 +164,7 @@ public class Cursor {
// create our cursor elements // create our cursor elements
cursors = new CursorElement[numImages]; cursors = new CursorElement[numImages];
for(int i=0; i<numImages; i++) { for(int i=0; i<numImages; i++) {
Object handle = Display.getImplementation().createCursor(width, height, xHotspot, yHotspot, 1, images_copy, null); Object handle = Mouse.getImplementation().createCursor(width, height, xHotspot, yHotspot, 1, images_copy, null);
long delay = (delays != null) ? delays.get(i) : 0; long delay = (delays != null) ? delays.get(i) : 0;
long timeout = System.currentTimeMillis(); long timeout = System.currentTimeMillis();
cursors[i] = new CursorElement(handle, delay, timeout); cursors[i] = new CursorElement(handle, delay, timeout);
@ -175,7 +175,7 @@ public class Cursor {
break; break;
case LWJGLUtil.PLATFORM_LINUX: case LWJGLUtil.PLATFORM_LINUX:
// create our cursor elements // create our cursor elements
Object handle = Display.getImplementation().createCursor(width, height, xHotspot, yHotspot, numImages, images_copy, delays); Object handle = Mouse.getImplementation().createCursor(width, height, xHotspot, yHotspot, numImages, images_copy, delays);
CursorElement cursor_element = new CursorElement(handle, -1, -1); CursorElement cursor_element = new CursorElement(handle, -1, -1);
cursors = new CursorElement[]{cursor_element}; cursors = new CursorElement[]{cursor_element};
break; break;
@ -251,7 +251,7 @@ public class Cursor {
} }
} }
for(int i=0; i<cursors.length; i++) { for(int i=0; i<cursors.length; i++) {
Display.getImplementation().destroyCursor(cursors[i].cursorHandle); Mouse.getImplementation().destroyCursor(cursors[i].cursorHandle);
} }
destroyed = true; destroyed = true;
} }

View File

@ -42,6 +42,7 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import org.lwjgl.Sys; import org.lwjgl.Sys;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayImplementation;
/** /**
* <br> * <br>
@ -263,6 +264,8 @@ public class Keyboard {
/** One time initialization */ /** One time initialization */
private static boolean initialized; private static boolean initialized;
private static DisplayImplementation implementation;
/** /**
* Keyboard cannot be constructed. * Keyboard cannot be constructed.
*/ */
@ -292,7 +295,8 @@ public class Keyboard {
initialize(); initialize();
if (created) if (created)
return; return;
Display.getImplementation().createKeyboard(); implementation = Mouse.getImplementation();
implementation.createKeyboard();
created = true; created = true;
readBuffer = ByteBuffer.allocate(EVENT_SIZE*BUFFER_SIZE); readBuffer = ByteBuffer.allocate(EVENT_SIZE*BUFFER_SIZE);
reset(); reset();
@ -321,7 +325,7 @@ public class Keyboard {
if (!created) if (!created)
return; return;
created = false; created = false;
Display.getImplementation().destroyKeyboard(); implementation.destroyKeyboard();
reset(); reset();
} }
@ -346,13 +350,13 @@ public class Keyboard {
public static void poll() { public static 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");
Display.getImplementation().pollKeyboard(keyDownBuffer); implementation.pollKeyboard(keyDownBuffer);
read(); read();
} }
private static void read() { private static void read() {
readBuffer.compact(); readBuffer.compact();
Display.getImplementation().readKeyboard(readBuffer); implementation.readKeyboard(readBuffer);
readBuffer.flip(); readBuffer.flip();
} }
@ -376,7 +380,7 @@ public class Keyboard {
/* public static int isStateKeySet(int key) { /* public static int isStateKeySet(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 Display.getImplementation().isStateKeySet(key); return implementation.isStateKeySet(key);
} }
*/ */
/** /**

View File

@ -40,8 +40,15 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil; import org.lwjgl.LWJGLUtil;
import org.lwjgl.Sys; import org.lwjgl.Sys;
import org.lwjgl.opengl.DisplayImplementation;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.Display;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException;
/** /**
* <br> * <br>
* A raw Mouse interface. This can be used to poll the current state of the * A raw Mouse interface. This can be used to poll the current state of the
@ -126,6 +133,8 @@ public class Mouse {
private static final int BUFFER_SIZE = 50; private static final int BUFFER_SIZE = 50;
private static boolean isGrabbed; private static boolean isGrabbed;
private static DisplayImplementation implementation;
/** Whether we're running windows - which need to manually update cursor animation */ /** Whether we're running windows - which need to manually update cursor animation */
private static final boolean isWindows = LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_WINDOWS; private static final boolean isWindows = LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_WINDOWS;
@ -163,10 +172,10 @@ public class Mouse {
currentCursor = cursor; currentCursor = cursor;
if (isCreated()) { if (isCreated()) {
if (currentCursor != null) { if (currentCursor != null) {
Display.getImplementation().setNativeCursor(currentCursor.getHandle()); implementation.setNativeCursor(currentCursor.getHandle());
currentCursor.setTimeout(); currentCursor.setTimeout();
} else { } else {
Display.getImplementation().setNativeCursor(null); implementation.setNativeCursor(null);
} }
} }
return oldCursor; return oldCursor;
@ -187,7 +196,7 @@ public class Mouse {
x = event_x = new_x; x = event_x = new_x;
y = event_y = new_y; y = event_y = new_y;
if (!isGrabbed() && (Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) != 0) if (!isGrabbed() && (Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) != 0)
Display.getImplementation().setCursorPosition(x, y); implementation.setCursorPosition(x, y);
} }
/** /**
@ -211,6 +220,23 @@ public class Mouse {
readBuffer.position(readBuffer.limit()); readBuffer.position(readBuffer.limit());
} }
static DisplayImplementation getImplementation() {
/* Use reflection since we can't make Display.getImplementation
* public
*/
try {
return (DisplayImplementation)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception{
Method getImplementation_method = Display.class.getDeclaredMethod("getImplementation", null);
getImplementation_method.setAccessible(true);
return getImplementation_method.invoke(null, null);
}
});
} catch (PrivilegedActionException e) {
throw new Error(e);
}
}
/** /**
* "Create" the mouse. The display must first have been created. * "Create" the mouse. The display must first have been created.
* Initially, the mouse is not grabbed and the delta values are reported * Initially, the mouse is not grabbed and the delta values are reported
@ -224,12 +250,13 @@ public class Mouse {
if (!initialized) if (!initialized)
initialize(); initialize();
if (created) return; if (created) return;
Display.getImplementation().createMouse(); implementation = getImplementation();
hasWheel = Display.getImplementation().hasWheel(); implementation.createMouse();
hasWheel = implementation.hasWheel();
created = true; created = true;
// set mouse buttons // set mouse buttons
buttonCount = Display.getImplementation().getButtonCount(); buttonCount = implementation.getButtonCount();
buttons = BufferUtils.createByteBuffer(buttonCount); buttons = BufferUtils.createByteBuffer(buttonCount);
coord_buffer = BufferUtils.createIntBuffer(3); coord_buffer = BufferUtils.createIntBuffer(3);
if (currentCursor != null) if (currentCursor != null)
@ -255,7 +282,7 @@ public class Mouse {
buttons = null; buttons = null;
coord_buffer = null; coord_buffer = null;
Display.getImplementation().destroyMouse(); implementation.destroyMouse();
} }
/** /**
@ -281,7 +308,7 @@ public class Mouse {
*/ */
public static void poll() { public static 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");
Display.getImplementation().pollMouse(coord_buffer, buttons); implementation.pollMouse(coord_buffer, buttons);
/* If we're grabbed, poll returns mouse deltas, if not it returns absolute coordinates */ /* If we're grabbed, poll returns mouse deltas, if not it returns absolute coordinates */
int poll_coord1 = coord_buffer.get(0); int poll_coord1 = coord_buffer.get(0);
@ -308,7 +335,7 @@ public class Mouse {
private static void read() { private static void read() {
readBuffer.compact(); readBuffer.compact();
Display.getImplementation().readMouse(readBuffer); implementation.readMouse(readBuffer);
readBuffer.flip(); readBuffer.flip();
} }
@ -527,7 +554,7 @@ public class Mouse {
public static void setGrabbed(boolean grab) { public static void setGrabbed(boolean grab) {
isGrabbed = grab; isGrabbed = grab;
if (isCreated()) { if (isCreated()) {
Display.getImplementation().grabMouse(isGrabbed); implementation.grabMouse(isGrabbed);
resetMouse(); resetMouse();
} }
} }

View File

@ -718,7 +718,7 @@ public final class Display {
GL11.glViewport(0, 0, current_mode.getWidth(), current_mode.getHeight()); GL11.glViewport(0, 0, current_mode.getWidth(), current_mode.getHeight());
} }
public static DisplayImplementation getImplementation() { static DisplayImplementation getImplementation() {
return display_impl; return display_impl;
} }