2005-03-24 20:08:18 -05:00
|
|
|
/* Handle to devil Library */
|
2004-12-08 14:15:50 -05:00
|
|
|
#ifdef _WIN32
|
2005-03-24 20:08:18 -05:00
|
|
|
static HMODULE devILhandle;
|
|
|
|
#endif
|
|
|
|
#ifdef _X11
|
|
|
|
static void* devILhandle;
|
|
|
|
#endif
|
|
|
|
#ifdef _MACOSX
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-03-29 05:32:42 -05:00
|
|
|
const struct mach_header* devILhandle = NULL;
|
2004-12-08 14:15:50 -05:00
|
|
|
#endif
|
2005-05-17 03:43:40 -04:00
|
|
|
#include "extil.h"
|
2004-12-08 14:15:50 -05:00
|
|
|
|
2005-03-24 20:08:18 -05:00
|
|
|
/**
|
|
|
|
* Retrieves a function pointer from the devil library
|
|
|
|
* @param function Name of function to retrieve
|
|
|
|
*/
|
|
|
|
static void *NativeGetFunctionPointer(const char *function) {
|
2004-12-08 14:15:50 -05:00
|
|
|
#ifdef _WIN32
|
2005-03-24 20:08:18 -05:00
|
|
|
return GetProcAddress(devILhandle, function);
|
|
|
|
#endif
|
|
|
|
#ifdef _X11
|
|
|
|
return dlsym(devILhandle, function);
|
|
|
|
#endif
|
|
|
|
#ifdef _MACOSX
|
|
|
|
char *mac_symbol_name = (char *)malloc((strlen(function) + 2)*sizeof(char));
|
|
|
|
if (mac_symbol_name == NULL)
|
|
|
|
return NULL;
|
|
|
|
mac_symbol_name[0] = '_';
|
|
|
|
strcpy(&(mac_symbol_name[1]), function);
|
|
|
|
NSSymbol symbol = NSLookupSymbolInImage(devILhandle, mac_symbol_name, NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
|
|
|
|
free(mac_symbol_name);
|
|
|
|
if (symbol == NULL)
|
|
|
|
return NULL;
|
|
|
|
return NSAddressOfSymbol(symbol);
|
2004-12-08 14:15:50 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-03-24 20:08:18 -05:00
|
|
|
/**
|
|
|
|
* Retrieves a pointer to the named function
|
|
|
|
*
|
|
|
|
* @param function Name of function
|
|
|
|
* @return pointer to named function, or NULL if not found
|
|
|
|
*/
|
|
|
|
static void* extil_GetProcAddress(const char* function) {
|
|
|
|
void *p = NativeGetFunctionPointer(function);
|
|
|
|
if (p == NULL) {
|
|
|
|
printfDebug("Could not locate symbol %s\n", function);
|
|
|
|
}
|
|
|
|
return p;
|
2004-12-08 14:15:50 -05:00
|
|
|
}
|
|
|
|
|
2005-03-24 20:08:18 -05:00
|
|
|
/**
|
|
|
|
* Initializes all functions for class
|
|
|
|
*/
|
|
|
|
void extil_InitializeClass(JNIEnv *env, jclass clazz, int num_functions, JavaMethodAndExtFunction *functions) {
|
|
|
|
ext_InitializeClass(env, clazz, &extil_GetProcAddress, num_functions, functions);
|
2004-12-08 14:15:50 -05:00
|
|
|
}
|
|
|
|
|
2005-03-24 20:08:18 -05:00
|
|
|
/**
|
|
|
|
* Opens the native library
|
|
|
|
*/
|
|
|
|
bool extil_Open(JNIEnv *env, jobjectArray ilPaths) {
|
|
|
|
jsize pathcount = (*env)->GetArrayLength(env, ilPaths);
|
|
|
|
int i;
|
|
|
|
jstring path;
|
|
|
|
char *path_str;
|
|
|
|
|
|
|
|
printfDebug("Found %d devil paths\n", (int)pathcount);
|
|
|
|
for(i=0;i<pathcount;i++) {
|
|
|
|
path = (jstring) (*env)->GetObjectArrayElement(env, ilPaths, i);
|
|
|
|
path_str = GetStringNativeChars(env, path);
|
|
|
|
printfDebug("Testing '%s'\n", path_str);
|
2004-12-08 14:15:50 -05:00
|
|
|
#ifdef _WIN32
|
2005-03-24 20:08:18 -05:00
|
|
|
devILhandle = LoadLibrary(path_str);
|
|
|
|
#endif
|
|
|
|
#ifdef _X11
|
|
|
|
devILhandle = dlopen(path_str, RTLD_LAZY);
|
|
|
|
#endif
|
|
|
|
#ifdef _MACOSX
|
|
|
|
devILhandle = NSAddImage(path_str, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
|
|
|
|
#endif
|
|
|
|
if (devILhandle != NULL) {
|
|
|
|
printfDebug("Found devil at '%s'\n", path_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path_str);
|
|
|
|
if (devILhandle != NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throwException(env, "Could not load devil library.");
|
|
|
|
return false;
|
2004-12-08 14:15:50 -05:00
|
|
|
}
|
|
|
|
|
2005-03-24 20:08:18 -05:00
|
|
|
/**
|
|
|
|
* Closes the native library
|
|
|
|
*/
|
2004-12-12 05:13:55 -05:00
|
|
|
void extil_Close(void) {
|
2004-12-08 14:15:50 -05:00
|
|
|
#ifdef _WIN32
|
2005-03-24 20:08:18 -05:00
|
|
|
FreeLibrary(devILhandle);
|
2004-12-08 14:15:50 -05:00
|
|
|
#endif
|
2005-03-24 20:08:18 -05:00
|
|
|
#ifdef _X11
|
|
|
|
if (devILhandle != NULL) {
|
|
|
|
dlclose(devILhandle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _MACOSX
|
|
|
|
// Cannot remove the image
|
|
|
|
#endif
|
|
|
|
devILhandle = NULL;
|
2005-03-25 05:25:07 -05:00
|
|
|
}
|
|
|
|
|