Skip to content

Commit

Permalink
Support alternative default library names for EGL/GLES on Windows and…
Browse files Browse the repository at this point in the history
… GL on Linux

This matches the equivalent functionality in GLFW.
  • Loading branch information
Spasi committed Nov 20, 2015
1 parent 378d13c commit b2cf516
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 57 deletions.
33 changes: 17 additions & 16 deletions modules/core/src/main/java/org/lwjgl/egl/EGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,21 @@ private EGL() {}

/** Loads the EGL native library, using the default library name. */
public static void create() {
String libName = Configuration.LIBRARY_NAME_EGL.get();
if ( libName == null ) {
switch ( Platform.get() ) {
case LINUX:
libName = "libEGL.so.1";
break;
case MACOSX:
throw new UnsupportedOperationException("MacOS X does not support EGL");
case WINDOWS:
libName = "libEGL";
break;
default:
throw new IllegalStateException();
}
SharedLibrary EGL;
switch ( Platform.get() ) {
case LINUX:
EGL = Library.loadNative(Configuration.LIBRARY_NAME_EGL, "libEGL.so.1");
break;
case MACOSX:
EGL = Library.loadNative(Configuration.LIBRARY_NAME_EGL, "EGL");
break;
case WINDOWS:
EGL = Library.loadNative(Configuration.LIBRARY_NAME_EGL, "libEGL", "EGL");
break;
default:
throw new IllegalStateException();
}
create(libName);
create(EGL);
}

/**
Expand All @@ -75,8 +74,10 @@ public static void create() {
* @param libName the native library name
*/
public static void create(String libName) {
final SharedLibrary EGL = Library.loadNative(libName);
create(Library.loadNative(libName));
}

private static void create(final SharedLibrary EGL) {
try {
FunctionProvider functionProvider = new FunctionProvider.Default() {
private final long eglGetProcAddress = EGL.getFunctionAddress("eglGetProcAddress");
Expand Down
31 changes: 16 additions & 15 deletions modules/core/src/main/java/org/lwjgl/opencl/CL.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@ public final class CL {
private CL() {}

public static void create() {
create(Configuration.LIBRARY_NAME_OPENCL.get("OpenCL"));
SharedLibrary CL;
switch ( Platform.get() ) {
case LINUX:
case WINDOWS:
CL = Library.loadNative(Configuration.LIBRARY_NAME_OPENCL, "OpenCL");
break;
case MACOSX:
CL = Library.loadNative(Configuration.LIBRARY_NAME_OPENCL, "OpenCL", "/System/Library/Frameworks/OpenCL.framework");
break;
default:
throw new IllegalStateException();
}
create(CL);
}

public static void create(String libName) {
final SharedLibrary OPENCL;

{
SharedLibrary dll;
try {
dll = Library.loadNative(libName);
} catch (Throwable t) {
if ( Platform.get() == Platform.MACOSX )
dll = apiCreateLibrary("/System/Library/Frameworks/OpenCL.framework");
else
throw new RuntimeException(t);
}
OPENCL = dll;
}
create(Library.loadNative(libName));
}

public static void create(final SharedLibrary OPENCL) {
try {
FunctionProviderLocal functionProvider = new FunctionProviderLocal.Default() {

Expand Down
19 changes: 10 additions & 9 deletions modules/core/src/main/java/org/lwjgl/opengl/GL.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,21 @@ private GL() {}

/** Loads the OpenGL native library, using the default library name. */
public static void create() {
String libName;
SharedLibrary GL;
switch ( Platform.get() ) {
case WINDOWS:
libName = "opengl32";
break;
case LINUX:
libName = "libGL.so.1";
GL = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "libGL.so.1", "libGL.so");
break;
case MACOSX:
libName = "/System/Library/Frameworks/OpenGL.framework";
GL = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "/System/Library/Frameworks/OpenGL.framework");
break;
case WINDOWS:
GL = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "opengl32");
break;
default:
throw new IllegalStateException();
}

create(Configuration.LIBRARY_NAME_OPENGL.get(libName));
create(GL);
}

/**
Expand All @@ -89,8 +88,10 @@ public static void create() {
* @param libName the native library name
*/
public static void create(String libName) {
final SharedLibrary OPENGL = Library.loadNative(libName);
create(Library.loadNative(libName));
}

private static void create(final SharedLibrary OPENGL) {
abstract class FunctionProviderGL extends FunctionProvider.Default {
abstract long getExtensionAddress(long name);

Expand Down
16 changes: 9 additions & 7 deletions modules/core/src/main/java/org/lwjgl/opengles/GLES.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ private GLES() {}

/** Loads the OpenGL ES native library, using the default library name. */
public static void create() {
String libName;
SharedLibrary GLES;
switch ( Platform.get() ) {
case LINUX:
libName = "libGLESv2.so.2";
GLES = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "libGLESv2.so.2");
break;
case MACOSX:
throw new UnsupportedOperationException("MacOS X does not support OpenGL ES");
GLES = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "GLESv2");
break;
case WINDOWS:
libName = "libGLESv2";
GLES = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "libGLESv2", "GLESv2");
break;
default:
throw new IllegalStateException();
}

create(Configuration.LIBRARY_NAME_OPENGLES.get(libName));
create(GLES);
}

/**
Expand All @@ -88,8 +88,10 @@ public static void create() {
* @param libName the native library name
*/
public static void create(String libName) {
final SharedLibrary GLES = Library.loadNative(libName);
create(Library.loadNative(libName));
}

private static void create(final SharedLibrary GLES) {
try {
FunctionProvider functionProvider = new FunctionProvider.Default() {
@Override
Expand Down
57 changes: 47 additions & 10 deletions modules/core/src/main/java/org/lwjgl/system/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

import static org.lwjgl.system.APIUtil.*;

/** Initializes the LWJGL native library and handles loading additional shared libraries. */
/** Initializes the LWJGL shared library and handles loading additional shared libraries. */
public final class Library {

/** The LWJGL native library name */
/** The LWJGL shared library name. */
public static final String JNI_LIBRARY_NAME = Configuration.LIBRARY_NAME_LWJGL.get(System.getProperty("os.arch").contains("64") ? "lwjgl" : "lwjgl32");

private static final String JAVA_LIBRARY_PATH = "java.library.path";
Expand All @@ -39,7 +39,9 @@ public SharedLibrary load(File library) {
apiLog("Version: " + Version.getVersion());
apiLog("\t OS: " + System.getProperty("os.name") + " v" + System.getProperty("os.version"));
apiLog("\tJRE: " + System.getProperty("java.version") + " " + System.getProperty("os.arch"));
apiLog("\tJVM: " + System.getProperty("java.vm.name") + " v" + System.getProperty("java.vm.version") + " by " + System.getProperty("java.vm.vendor"));
apiLog(
"\tJVM: " + System.getProperty("java.vm.name") + " v" + System.getProperty("java.vm.version") + " by " + System.getProperty("java.vm.vendor")
);
}

try {
Expand All @@ -60,13 +62,13 @@ public SharedLibrary load(File library) {

private Library() {}

/** Ensures that the LWJGL native library has been loaded. */
/** Ensures that the LWJGL shared library has been loaded. */
public static void initialize() {
// intentionally empty to trigger static initializer
}

/**
* Loads a native library using {@link System}.
* Loads a shared library using {@link System}.
*
* <p>If {@code name} is an absolute path or {@link Configuration#LIBRARY_PATH} is set, {@link System#load} will be used. Otherwise,
* {@link System#loadLibrary} will be used.</p>
Expand Down Expand Up @@ -106,16 +108,15 @@ public static void loadSystem(String name) throws UnsatisfiedLinkError {
}

/**
* Loads a native library using OS-specific APIs (e.g. {@link org.lwjgl.system.windows.WinBase#LoadLibrary LoadLibrary} or
* Loads a shared library using OS-specific APIs (e.g. {@link org.lwjgl.system.windows.WinBase#LoadLibrary LoadLibrary} or
* {@link org.lwjgl.system.linux.DynamicLinkLoader#dlopen dlopen}).
*
* @param name the library name, without an OS specific prefix or file extension (e.g. GL, not libGL.so)
* @param name the library name. OS-specific prefixes and file extensions are optional (e.g. both {@code "GL"} and {@code "libGL.so.1"} are valid on Linux)
*
* @return the native library
* @return the shared library
*
* @throws UnsatisfiedLinkError if the library could not be loaded
* @throws RuntimeException if the library could not be loaded
*/

public static SharedLibrary loadNative(String name) {
if ( new File(name).isAbsolute() )
return apiCreateLibrary(name);
Expand Down Expand Up @@ -148,6 +149,42 @@ public static SharedLibrary loadNative(String name) {
}
}

/**
* Loads a shared library using {@link #loadNative(String)} with the name specified by {@code name}. If {@code name} is not set,
* {@link #loadNative(String)} will be called with the names specified by {@code defaultNames}. The first successful will be returned.
*
* @param name a {@link Configuration} that specifies the library name
* @param defaultNames the default library name
*
* @return the shared library
*
* @throws RuntimeException if the library could not be loaded
*/
public static SharedLibrary loadNative(Configuration name, String... defaultNames) {
if ( defaultNames.length == 1 || name.get() != null )
return loadNative(name.get(defaultNames[0]));
else {
if ( defaultNames.length == 0 )
throw new RuntimeException("No default names specified.");

SharedLibrary library = null;
try {
library = Library.loadNative(defaultNames[0]); // try first
} catch (Exception e) {
for ( int i = 1; i < defaultNames.length; i++ ) { // try alternatives
try {
library = Library.loadNative(defaultNames[i]);
break;
} catch (Exception ignored) {
}
}
if ( library == null )
throw new RuntimeException("Failed to load library.", e); // original exception
}
return library;
}
}

private interface LibraryLoader<T> {
T load(File library);
}
Expand Down

0 comments on commit b2cf516

Please sign in to comment.