lwjgl2-arm64/src/java/org/lwjgl/Sys.java

275 lines
8.9 KiB
Java
Raw Normal View History

2002-08-15 17:46:18 +02:00
/*
2002-12-21 13:37:20 +01:00
* Copyright (c) 2002 Lightweight Java Game Library Project
2002-08-15 17:46:18 +02:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
2002-08-09 12:56:30 +02:00
*
2002-08-15 17:46:18 +02:00
* * 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 'Light Weight Java Game Library' 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.
2002-08-09 12:56:30 +02:00
*/
2002-08-15 17:46:18 +02:00
2002-08-09 12:56:30 +02:00
package org.lwjgl;
2003-10-28 20:26:51 +01:00
import java.io.IOException;
2003-08-17 18:14:36 +02:00
import org.lwjgl.input.Controller;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
2002-08-09 12:56:30 +02:00
/**
2002-08-15 17:46:18 +02:00
* $Id$
*
* System class (named Sys so as not to conflict with java.lang.System)
2002-08-09 12:56:30 +02: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 {
/** Low process priority. @see #setProcessPriority() */
public static final int LOW_PRIORITY = -1;
/**
* Normal process priority. This priority equates to the priority that the
* JVM has when it is started up normally. Note that if the JVM is started
* inside a process which is already a different priority then this will not
* be the initial priority.
*
2002-08-23 18:14:38 +02:00
* @see #setProcessPriority(int)
*/
public static final int NORMAL_PRIORITY = 0;
/** High process priority. @see #setProcessPriority() */
public static final int HIGH_PRIORITY = 1;
/**
* Realtime priority. Use at your own risk. This will set the java process
* priority to the highest priority the OS will normally allow. It is likely
* that this puts it at a higher priority than many OS critical tasks, such
* as disk writes or mouse input and the like. Hence it is quite possible to
* completely freeze your machine if you have an errant thread.
*
* This priority is <strong>not</strong> recommended for gaming applications.
*
2002-08-23 18:14:38 +02:00
* @see #setProcessPriority(int)
*/
public static final int REALTIME_PRIORITY = 2;
2002-08-09 12:56:30 +02:00
/** The native library name */
private static String LIBRARY_NAME = "lwjgl";
/** The platform being executed on */
private static String PLATFORM;
2002-08-19 13:29:42 +02: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
2002-08-19 13:29:42 +02:00
static {
2003-12-20 15:01:31 +01:00
initialize();
// check platform name, and default to awt
PLATFORM = System.getProperty("org.lwjgl.Sys.platform");
if(PLATFORM == null) {
PLATFORM = "org.lwjgl.SwingAdapter";
}
2002-08-19 13:29:42 +02:00
}
2003-03-21 17:53:19 +01:00
2002-08-19 16:01:23 +02:00
/**
* @return the name of the native library to load
*/
public static String getLibraryName() {
return LIBRARY_NAME;
}
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
/**
* Initialization.
*/
private static void initialize() {
System.loadLibrary(LIBRARY_NAME);
2003-12-20 23:07:19 +01:00
setDebug(DEBUG);
setTime(0);
2003-08-17 18:14:36 +02:00
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
Display.resetDisplayMode();
if (Keyboard.isCreated())
Keyboard.destroy();
if (Mouse.isCreated())
Mouse.destroy();
if (Controller.isCreated())
Controller.destroy();
}
});
}
2002-08-09 12:56:30 +02:00
/**
2003-12-20 23:28:00 +01:00
* Set the debug level of the native library
*/
2003-12-20 23:03:25 +01:00
private static native void setDebug(boolean debug);
2002-08-09 12:56:30 +02:00
/**
* Obtains the number of ticks that the hires timer does in a second.
2002-08-09 12:56:30 +02:00
*
* @return timer resolution in ticks per second or 0 if no timer is present.
2002-08-09 12:56:30 +02:00
*/
public static native long getTimerResolution();
2002-08-09 12:56:30 +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
2002-08-09 12:56:30 +02:00
* return whatever value the timer was last set to.
*
* @return the current hires time, in ticks.
2002-08-09 12:56:30 +02:00
*/
public static native long getTime();
2002-08-09 12:56:30 +02:00
/**
* Sets the hires timer to a new time, specified in ticks.
*
* @param time The new time, in ticks
2002-08-09 12:56:30 +02:00
* @see #getTime()
* @see #getTimerResolution()
*/
public static native void setTime(long time);
2002-08-09 12:56:30 +02:00
/**
* Set the process priority in a system independent way. Because of the various
* differences in operating systems this might or might not have any effect or
* the correct effect.
*
* The default process priority is NORMAL_PRIORITY.
*
* REALTIME_PRIORITY processes should theoretically be the maximum priority of
* any process on the system and may have side effects on I/O and other fundamental
* operating system functions - use with caution.
*
* It is unlikely that any games will want to change the priority of the Java
* process; but there are some other applications for this library which require
* process priority adjustments, such as in soft-realtime graphics rendering
* for broadcast television.
*
* @param priority a priority class, which will be one of REALTIME_PRIORITY,
* HIGH_PRIORITY, NORMAL_PRIORITY, or LOW_PRIORITY.
*/
public static native void setProcessPriority(int priority);
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
* 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.
* <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.
*/
public static void alert(String title, String 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);
}
}
}
private static native void nAlert(String title, String message);
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.
* <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!
* <p>
2003-10-28 17:23:17 +01:00
* @param url The URL
* @return false if we are CERTAIN the call has failed
2003-10-28 17:23:17 +01:00
*/
public static boolean openURL(String url) {
2003-10-28 20:26:51 +01:00
String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS") || osName.startsWith("Windows")) {
// Mac and Windows both do this nicely from native code.
nOpenURL(url);
return true;
2003-10-28 20:26:51 +01:00
}
// Linux may as well resort to pure Java hackery, as there's no Linux native way of doing it
// right anyway.
2003-10-28 22:44:46 +01:00
String[] browsers = {"mozilla", "opera", "konqueror", "nautilus", "galeon", "netscape"};
2003-10-28 20:26:51 +01:00
for (int i = 0; i < browsers.length; i ++) {
try {
Runtime.getRuntime().exec(new String[] { browsers[i], url });
return true;
2003-10-28 20:26:51 +01:00
} catch (IOException e) {
// Ignore
e.printStackTrace(System.err);
}
}
// Seems to have failed
return false;
2003-10-28 20:26:51 +01:00
}
/*
* Where necessary, we use a native implementation of openURL.
*/
private static native void nOpenURL(String url);
2002-08-09 12:56:30 +02:00
}