Delegate Sys platform dependent methods into SysImplementation instances

This commit is contained in:
Elias Naur 2005-01-18 20:23:05 +00:00
parent 87ce7a85af
commit 66181d1486
14 changed files with 253 additions and 365 deletions

View file

@ -1,64 +1,58 @@
/*
/*
* Copyright (c) 2002-2004 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
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
*
* * 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
* * 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
* 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
* 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;
import java.lang.reflect.Method;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
/**
* $Id$
* <p>
* A Swing adapter for using Swing to take care of things on platforms where we
* know Swing is present.
* <p><em>Note</em> To compile LWJGL applications with Excelsior JET that use JetPerfect
* and that have no dependencies on Swing, do not include this class in your
* JET project.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
final class SwingAdapter implements PlatformAdapter {
/**
* Constructs a new SwingAdapter
*/
SwingAdapter() {
abstract class DefaultSysImplementation implements SysImplementation {
public native String getNativeLibraryVersion();
public native void setDebug(boolean debug);
public long getTimerResolution() {
return 1000;
}
public long getTime() {
return System.currentTimeMillis();
}
/**
* Spawn a "modal" dialog in the centre of the screen with a message in it
* and an OK button. This method blocks until the dialog is dismissed.
* @param title Title of alert
* @param message Message to show in alert
*/
public void alert(String title, String message) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -66,13 +60,7 @@ final class SwingAdapter implements PlatformAdapter {
}
JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE);
}
/**
* Get the contents of the system clipboard. The system might not have a clipboard
* (particularly if it doesn't even have a keyboard) in which case we return null.
* Otherwise we return a String, which may be the empty string "".
* @return a String, or null if there is no system clipboard.
*/
public String getClipboard() {
try {
java.awt.datatransfer.Clipboard clipboard = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2002-2004 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;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.io.IOException;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
class LinuxSysImplementation extends DefaultSysImplementation {
public boolean openURL(String url) {
// Linux may as well resort to pure Java hackery, as there's no Linux native way of doing it
// right anyway.
String[] browsers = {"mozilla", "opera", "konqueror", "nautilus", "galeon", "netscape"};
for (int i = 0; i < browsers.length; i ++) {
try {
Runtime.getRuntime().exec(new String[] { browsers[i], url });
return true;
} catch (IOException e) {
// Ignore
e.printStackTrace(System.err);
}
}
// Seems to have failed
return false;
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2002-2004 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;
import java.lang.reflect.Method;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
class MacOSXSysImplementation extends DefaultSysImplementation {
public boolean openURL(String url) {
try {
Class com_apple_eio_FileManager = Class.forName("com.apple.eio.FileManager");
Method openURL_method = com_apple_eio_FileManager.getMethod("openURL", new Class[]{String.class});
openURL_method.invoke(null, new Object[]{url});
return true;
} catch (Exception e) {
return false;
}
}
}

View file

@ -51,27 +51,54 @@ public final class Sys {
public static final String VERSION = "0.94";
/** The native library name */
private static String LIBRARY_NAME = "lwjgl";
private static final String LIBRARY_NAME = "lwjgl";
/** The platform adapter class name */
private static String PLATFORM;
private static final String PLATFORM;
/**
* Debug flag.
*/
public static final boolean DEBUG = Boolean.getBoolean("org.lwjgl.Sys.debug");
private static boolean initialized;
/**
* The implementation instance to delegate platform specific behavior to
*/
private final static SysImplementation implementation;
static {
initialize();
System.loadLibrary(LIBRARY_NAME);
implementation = createImplementation();
String native_version = implementation.getNativeLibraryVersion();
if (!native_version.equals(VERSION))
throw new LinkageError("Version mismatch: jar version is '" + VERSION +
"', native libary version is '" + native_version + "'");
implementation.setDebug(DEBUG);
PLATFORM = System.getProperty("org.lwjgl.Sys.platform", "org.lwjgl.SwingAdapter");
}
/**
* @return the name of the native library to load
*/
private static String getLibraryName() {
return LIBRARY_NAME;
private static SysImplementation createImplementation() {
String class_name;
String os_name = System.getProperty("os.name");
if (os_name.startsWith("Linux")) {
class_name = "org.lwjgl.LinuxSysImplementation";
} else if (os_name.startsWith("Windows")) {
class_name = "org.lwjgl.Win32SysImplementation";
} else if (os_name.startsWith("Mac")) {
class_name = "org.lwjgl.MacOSXSysImplementation";
} else
throw new IllegalStateException("The platform " + os_name + " is not supported");
try {
Class impl_class = Class.forName(class_name);
return (SysImplementation)impl_class.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
/**
@ -91,39 +118,19 @@ public final class Sys {
}
/**
* Initialization.
* Initialization. This is just a dummy method to trigger the static constructor.
*/
public static void initialize() {
if (initialized)
return;
initialized = true;
System.loadLibrary(LIBRARY_NAME);
String native_version = getNativeLibraryVersion();
if (!native_version.equals(VERSION))
throw new LinkageError("Version mismatch: jar version is '" + VERSION +
"', native libary version is '" + native_version + "'");
setDebug(DEBUG);
PLATFORM = System.getProperty("org.lwjgl.Sys.platform", "org.lwjgl.SwingAdapter");
}
/**
* Return the version of the native library
*/
private static native String getNativeLibraryVersion();
/**
* Set the debug level of the native library
*/
private static native void setDebug(boolean debug);
/**
* Obtains the number of ticks that the hires timer does in a second.
*
* @return timer resolution in ticks per second or 0 if no timer is present.
*/
public static native long getTimerResolution();
public static long getTimerResolution() {
return implementation.getTimerResolution();
}
/**
* Gets the current value of the hires timer, in ticks. When the Sys class is first loaded
@ -133,9 +140,8 @@ public final class Sys {
* @return the current hires time, in ticks (always >= 0)
*/
public static long getTime() {
return ngetTime() & 0x7FFFFFFFFFFFFFFFL;
return implementation.getTime() & 0x7FFFFFFFFFFFFFFFL;
}
private static native long ngetTime();
/**
* Attempt to display a modal alert to the user. This method should be used
@ -165,23 +171,12 @@ public final class Sys {
if (message == null)
message = "";
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) {
nAlert(title, message);
} else {
try {
PlatformAdapter adapter = (PlatformAdapter) Class.forName(PLATFORM).newInstance(); // This avoids a Jet error message
adapter.alert(title, message);
} catch (Exception e) {
Sys.log("Unable to display alert using: " + PLATFORM);
}
}
implementation.alert(title, message);
if (grabbed) {
Mouse.setGrabbed(true);
}
}
private static native void nAlert(String title, String message);
/**
* Open the system web browser and point it at the specified URL. It is recommended
* that this not be called whilst your game is running, but on application exit in
@ -212,7 +207,7 @@ public final class Sys {
return false;
}
} catch (Exception ue) {
return Display.getImplementation().openURL(url);
return implementation.openURL(url);
}
}
@ -225,15 +220,6 @@ public final class Sys {
* @return a String, or null if there is no system clipboard.
*/
public static String getClipboard() {
try {
PlatformAdapter adapter = (PlatformAdapter) Class.forName(PLATFORM).newInstance(); // This avoids a Jet error message
return adapter.getClipboard();
} catch (Throwable e) {
Sys.log("Unable to get clipboard contents: " + e);
// ignore exception and use native implementation
return nGetClipboard();
}
return implementation.getClipboard();
}
private static native String nGetClipboard();
}

View file

@ -31,30 +31,35 @@
*/
package org.lwjgl;
/**
* $Id$
*
* Interface for adapting to window environments.
* System class platform specific method interface
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
public interface PlatformAdapter {
interface SysImplementation {
/**
* Return the version of the native library
*/
public String getNativeLibraryVersion();
public void setDebug(boolean debug);
/**
* Spawn a "modal" dialog in the centre of the screen with a message in it
* and an OK button. This method blocks until the dialog is dismissed.
* @param title
* @param message
* Obtains the number of ticks that the hires timer does in a second.
*
* @return timer resolution in ticks per second or 0 if no timer is present.
*/
void alert(String title, String message);
public long getTimerResolution();
/**
* Get the contents of the system clipboard. The system might not have a clipboard
* (particularly if it doesn't even have a keyboard) in which case we return null.
* Otherwise we return a String, which may be the empty string "".
* @return a String, or null if there is no system clipboard.
*/
String getClipboard();
public long getTime();
public void alert(String title, String message);
boolean openURL(String url);
public String getClipboard();
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2002-2004 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;
/**
* $Id$
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
class Win32SysImplementation extends DefaultSysImplementation {
public native long getTimerResolution();
public native long getTime();
public native void alert(String title, String message);
public native boolean openURL(String url);
public native String getClipboard();
}

View file

@ -248,6 +248,4 @@ public interface DisplayImplementation {
public void bindTexImageToPbuffer(ByteBuffer handle, int buffer);
public void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer);
boolean openURL(String url);
}

View file

@ -393,24 +393,4 @@ final class LinuxDisplay implements DisplayImplementation {
public void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer) {
throw new UnsupportedOperationException();
}
public boolean openURL(String url) {
// Linux may as well resort to pure Java hackery, as there's no Linux native way of doing it
// right anyway.
String[] browsers = {"mozilla", "opera", "konqueror", "nautilus", "galeon", "netscape"};
for (int i = 0; i < browsers.length; i ++) {
try {
Runtime.getRuntime().exec(new String[] { browsers[i], url });
return true;
} catch (IOException e) {
// Ignore
e.printStackTrace(System.err);
}
}
// Seems to have failed
return false;
}
}

View file

@ -368,18 +368,6 @@ final class MacOSXDisplay implements DisplayImplementation {
return GL11.glGetString(GL11.GL_EXTENSIONS).indexOf("GL_APPLE_pixel_buffer") != -1 ? Pbuffer.PBUFFER_SUPPORTED : 0;
}
/* Use the com.apple.eio.FileManager Mac OS X extension to show the given URL */
public boolean openURL(String url) {
try {
Class com_apple_eio_FileManager = Class.forName("com.apple.eio.FileManager");
Method openURL_method = com_apple_eio_FileManager.getMethod("openURL", new Class[]{String.class});
openURL_method.invoke(null, new Object[]{url});
return true;
} catch (Exception e) {
return false;
}
}
/**
* This class captures com.apple.eawt.ApplicationEvents through reflection
* to enable compilation on other platforms than Mac OS X

View file

@ -126,11 +126,4 @@ final class Win32Display implements DisplayImplementation {
public native void setPbufferAttrib(ByteBuffer handle, int attrib, int value);
public native void bindTexImageToPbuffer(ByteBuffer handle, int buffer);
public native void releaseTexImageFromPbuffer(ByteBuffer handle, int buffer);
public boolean openURL(String url) {
nOpenURL(url);
return true;
}
private native void nOpenURL(String url);
}