2004-11-25 23:20:45 +01:00
|
|
|
/*
|
2004-06-12 22:28:34 +02:00
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
2002-08-15 17:46:18 +02:00
|
|
|
* All rights reserved.
|
2004-11-25 23:20:45 +01:00
|
|
|
*
|
2002-08-15 17:46:18 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2004-11-25 23:20:45 +01:00
|
|
|
* modification, are permitted provided that the following conditions are
|
2002-08-15 17:46:18 +02:00
|
|
|
* met:
|
2004-11-25 23:20:45 +01:00
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
2002-08-15 17:46:18 +02:00
|
|
|
* 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.
|
|
|
|
|
*
|
2004-11-25 23:20:45 +01:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
2002-08-15 17:46:18 +02:00
|
|
|
* from this software without specific prior written permission.
|
2004-11-25 23:20:45 +01:00
|
|
|
*
|
2002-08-15 17:46:18 +02:00
|
|
|
* 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
|
2004-11-25 23:20:45 +01:00
|
|
|
* 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
|
2002-08-15 17:46:18 +02:00
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
2004-11-25 23:20:45 +01:00
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
2002-08-15 17:46:18 +02:00
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
|
|
|
|
package org.lwjgl;
|
|
|
|
|
|
2004-11-22 22:38:08 +01:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URL;
|
2003-10-28 20:26:51 +01:00
|
|
|
|
2004-08-13 17:24:47 +02:00
|
|
|
import org.lwjgl.input.Mouse;
|
|
|
|
|
|
2002-08-09 12:56:30 +02:00
|
|
|
/**
|
2002-08-15 17:46:18 +02:00
|
|
|
* $Id$
|
|
|
|
|
*
|
2002-08-09 20:50:58 +02:00
|
|
|
* System class (named Sys so as not to conflict with java.lang.System)
|
2004-11-25 23:20:45 +01:00
|
|
|
*
|
2002-08-15 17:46:18 +02:00
|
|
|
* @author cix_foo <cix_foo@users.sourceforge.net>
|
|
|
|
|
* @version $Revision$
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
|
|
|
|
public final class Sys {
|
2004-11-25 23:20:45 +01:00
|
|
|
|
2005-01-25 07:31:35 +01:00
|
|
|
public static final String VERSION = "0.95";
|
2004-03-27 15:09:55 +01:00
|
|
|
|
2002-08-09 12:56:30 +02:00
|
|
|
/** The native library name */
|
2005-01-18 21:23:05 +01:00
|
|
|
private static final String LIBRARY_NAME = "lwjgl";
|
2004-03-27 15:09:55 +01:00
|
|
|
|
2004-05-05 16:28:40 +02:00
|
|
|
/** The platform adapter class name */
|
2005-01-18 21:23:05 +01:00
|
|
|
private static final String PLATFORM;
|
2004-11-25 23:20:45 +01:00
|
|
|
|
2002-12-15 23:12:46 +01:00
|
|
|
/**
|
2003-12-20 23:07:19 +01:00
|
|
|
* Debug flag.
|
2002-12-15 23:12:46 +01:00
|
|
|
*/
|
2003-12-20 23:17:52 +01:00
|
|
|
public static final boolean DEBUG = Boolean.getBoolean("org.lwjgl.Sys.debug");
|
2003-04-03 22:20:29 +02:00
|
|
|
|
2005-01-18 21:23:05 +01:00
|
|
|
/**
|
|
|
|
|
* The implementation instance to delegate platform specific behavior to
|
|
|
|
|
*/
|
|
|
|
|
private final static SysImplementation implementation;
|
2004-03-27 14:48:58 +01:00
|
|
|
|
2002-08-19 13:29:42 +02:00
|
|
|
static {
|
2005-01-18 21:23:05 +01:00
|
|
|
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");
|
2002-08-19 13:29:42 +02:00
|
|
|
}
|
2003-03-21 17:53:19 +01:00
|
|
|
|
2005-01-18 21:23:05 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2002-08-19 16:01:23 +02:00
|
|
|
}
|
2002-08-09 12:56:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* No constructor for Sys.
|
|
|
|
|
*/
|
|
|
|
|
private Sys() {
|
|
|
|
|
}
|
2003-12-20 15:01:31 +01:00
|
|
|
|
|
|
|
|
/**
|
2003-12-20 23:03:25 +01:00
|
|
|
* Prints the given message to System.err if isDebugEnabled()
|
2003-12-20 15:01:31 +01:00
|
|
|
* is true.
|
|
|
|
|
*/
|
2003-12-20 23:03:25 +01:00
|
|
|
public static void log(String msg) {
|
2003-12-20 23:28:00 +01:00
|
|
|
if (Sys.DEBUG) {
|
2003-12-20 15:01:31 +01:00
|
|
|
System.err.println(msg);
|
2003-12-20 23:28:00 +01:00
|
|
|
}
|
2003-12-20 15:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
2002-08-09 12:56:30 +02:00
|
|
|
/**
|
2005-01-18 21:23:05 +01:00
|
|
|
* Initialization. This is just a dummy method to trigger the static constructor.
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
2004-03-27 14:48:58 +01:00
|
|
|
public static void initialize() {
|
2003-07-17 11:16:51 +02:00
|
|
|
}
|
2002-08-09 12:56:30 +02:00
|
|
|
|
|
|
|
|
/**
|
2002-08-11 13:36:18 +02:00
|
|
|
* Obtains the number of ticks that the hires timer does in a second.
|
2002-08-09 12:56:30 +02:00
|
|
|
*
|
2002-08-11 13:36:18 +02:00
|
|
|
* @return timer resolution in ticks per second or 0 if no timer is present.
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
2005-01-18 21:23:05 +01:00
|
|
|
public static long getTimerResolution() {
|
|
|
|
|
return implementation.getTimerResolution();
|
|
|
|
|
}
|
2004-11-25 23:20:45 +01:00
|
|
|
|
2002-08-09 12:56:30 +02:00
|
|
|
/**
|
2002-08-11 13:36:18 +02:00
|
|
|
* Gets the current value of the hires timer, in ticks. When the Sys class is first loaded
|
|
|
|
|
* the hires timer is reset to 0. If no hires timer is present then this method will always
|
2004-11-25 23:20:45 +01:00
|
|
|
* return 0.<p><strong>NOTEZ BIEN</strong> that the hires timer WILL wrap around.
|
2002-08-09 12:56:30 +02:00
|
|
|
*
|
2004-05-05 16:28:40 +02:00
|
|
|
* @return the current hires time, in ticks (always >= 0)
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
2004-05-05 16:28:40 +02:00
|
|
|
public static long getTime() {
|
2005-01-18 21:23:05 +01:00
|
|
|
return implementation.getTime() & 0x7FFFFFFFFFFFFFFFL;
|
2004-05-05 16:28:40 +02:00
|
|
|
}
|
2004-11-25 23:20:45 +01:00
|
|
|
|
2002-12-15 23:12:46 +01:00
|
|
|
/**
|
|
|
|
|
* Attempt to display a modal alert to the user. This method should be used
|
|
|
|
|
* when a game fails to initialize properly or crashes out losing its display
|
|
|
|
|
* in the process. It is provided because AWT may not be available on the target
|
2004-02-24 00:42:58 +01:00
|
|
|
* platform, although on Mac and Linux and other platforms supporting AWT we
|
|
|
|
|
* delegate the task to AWT instead of doing it ourselves.
|
|
|
|
|
* <p>
|
2002-12-15 23:12:46 +01:00
|
|
|
* The alert should display the title and the message and then the current
|
|
|
|
|
* thread should block until the user dismisses the alert - typically with an
|
|
|
|
|
* OK button click.
|
2004-02-24 00:42:58 +01:00
|
|
|
* <p>
|
2002-12-15 23:12:46 +01:00
|
|
|
* It may be that the user's system has no windowing system installed for some
|
|
|
|
|
* reason, in which case this method may do nothing at all, or attempt to provide
|
|
|
|
|
* some console output.
|
|
|
|
|
*
|
|
|
|
|
* @param title The title of the alert. We suggest using the title of your game.
|
|
|
|
|
* @param message The message text for the alert.
|
|
|
|
|
*/
|
2004-02-24 00:42:58 +01:00
|
|
|
public static void alert(String title, String message) {
|
2004-08-13 17:24:47 +02:00
|
|
|
boolean grabbed = Mouse.isGrabbed();
|
|
|
|
|
if (grabbed) {
|
|
|
|
|
Mouse.setGrabbed(false);
|
|
|
|
|
}
|
2005-01-15 17:30:17 +01:00
|
|
|
if (title == null)
|
|
|
|
|
title = "";
|
|
|
|
|
if (message == null)
|
|
|
|
|
message = "";
|
2004-02-24 00:42:58 +01:00
|
|
|
String osName = System.getProperty("os.name");
|
2005-01-18 21:23:05 +01:00
|
|
|
implementation.alert(title, message);
|
2004-08-13 17:24:47 +02:00
|
|
|
if (grabbed) {
|
|
|
|
|
Mouse.setGrabbed(true);
|
|
|
|
|
}
|
2004-02-24 00:42:58 +01:00
|
|
|
}
|
|
|
|
|
|
2003-10-28 17:23:17 +01:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* a shutdown hook, as the screen resolution will not be reset when the browser is
|
|
|
|
|
* brought into view.
|
2004-02-24 00:42:58 +01:00
|
|
|
* <p>
|
2003-10-28 17:23:17 +01:00
|
|
|
* There is no guarantee that this will work, nor that we can detect if it has
|
|
|
|
|
* failed - hence we don't return success code or throw an Exception. This is just a
|
|
|
|
|
* best attempt at opening the URL given - don't rely on it to work!
|
2004-02-24 00:42:58 +01:00
|
|
|
* <p>
|
2004-12-14 13:54:25 +01:00
|
|
|
* @param url The URL. Ensure that the URL is properly encoded.
|
2004-02-24 00:42:58 +01:00
|
|
|
* @return false if we are CERTAIN the call has failed
|
2003-10-28 17:23:17 +01:00
|
|
|
*/
|
2004-02-24 00:42:58 +01:00
|
|
|
public static boolean openURL(String url) {
|
2004-11-22 22:38:08 +01:00
|
|
|
// Attempt to use Webstart if we have it available
|
|
|
|
|
try {
|
|
|
|
|
// Lookup the javax.jnlp.BasicService object
|
|
|
|
|
Class serviceManagerClass = Class.forName("javax.jnlp.ServiceManager");
|
|
|
|
|
Method lookupMethod = serviceManagerClass.getMethod("lookup", new Class[] {String.class});
|
|
|
|
|
Object basicService = lookupMethod.invoke(serviceManagerClass, new Object[] {"javax.jnlp.BasicService"});
|
|
|
|
|
Class basicServiceClass = Class.forName("javax.jnlp.BasicService");
|
|
|
|
|
Method showDocumentMethod = basicServiceClass.getMethod("showDocument", new Class[] {URL.class});
|
|
|
|
|
try {
|
2004-12-14 13:54:25 +01:00
|
|
|
Boolean ret = (Boolean) showDocumentMethod.invoke(basicService, new Object[] {new URL(url)});
|
2004-11-22 22:38:08 +01:00
|
|
|
return ret.booleanValue();
|
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
|
e.printStackTrace(System.err);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ue) {
|
2005-01-18 21:23:05 +01:00
|
|
|
return implementation.openURL(url);
|
2004-11-22 22:38:08 +01:00
|
|
|
}
|
2003-10-28 20:26:51 +01:00
|
|
|
}
|
2004-08-12 16:30:13 +02:00
|
|
|
|
|
|
|
|
/**
|
2004-11-22 22:38:08 +01:00
|
|
|
* 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 "".
|
2004-11-25 23:20:45 +01:00
|
|
|
*
|
2004-08-12 16:30:13 +02:00
|
|
|
* @return a String, or null if there is no system clipboard.
|
|
|
|
|
*/
|
2004-08-12 17:22:48 +02:00
|
|
|
public static String getClipboard() {
|
2005-01-18 21:23:05 +01:00
|
|
|
return implementation.getClipboard();
|
2004-08-12 17:22:48 +02:00
|
|
|
}
|
2004-11-25 23:20:45 +01:00
|
|
|
}
|