src/SDL_loadso.c
changeset 1173 e9cf8c1b4590
parent 1152 51a8702d8ecd
child 1190 173c063d4f55
     1.1 --- a/src/SDL_loadso.c	Thu Nov 17 03:04:47 2005 +0000
     1.2 +++ b/src/SDL_loadso.c	Thu Nov 17 03:15:05 2005 +0000
     1.3 @@ -28,234 +28,23 @@
     1.4  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     1.5  /* System dependent library loading routines                           */
     1.6  
     1.7 -#include <stdio.h>
     1.8 -#if defined(USE_DLOPEN)
     1.9 -# include <dlfcn.h>
    1.10 +/* !!! FIXME: includes so I don't have to update all the project files... */
    1.11 +#define SDL_INTERNAL_BUILDING_LOADSO 1
    1.12 +#if defined(USE_DUMMY_LOADSO)
    1.13 +# include "loadso/dummy/SDL_loadso.c"
    1.14 +#elif defined(MACOSX)
    1.15 +# include "loadso/macosx/SDL_loadso.c"
    1.16 +#elif defined(macintosh)
    1.17 +# include "loadso/macos/SDL_loadso.c"
    1.18 +#elif defined(USE_DLOPEN)
    1.19 +# include "loadso/dlopen/SDL_loadso.c"
    1.20  #elif defined(WIN32) || defined(_WIN32_WCE)
    1.21 -# include <windows.h>
    1.22 +# include "loadso/windows/SDL_loadso.c"
    1.23  #elif defined(__BEOS__)
    1.24 -# include <be/kernel/image.h>
    1.25 -#elif defined(macintosh)
    1.26 -# include <string.h>
    1.27 -#define OLDP2C 1
    1.28 -# include <Strings.h>
    1.29 -# include <CodeFragments.h>
    1.30 -# include <Errors.h>
    1.31 +# include "loadso/beos/SDL_loadso.c"
    1.32  #elif defined(__MINT__) && defined(ENABLE_LDG)
    1.33 -# include <gem.h>
    1.34 -# include <ldg.h>
    1.35 +# include "loadso/mint/SDL_loadso.c"
    1.36  #else
    1.37 -/*#error Unsupported dynamic link environment*/
    1.38 +# include "loadso/dummy/SDL_loadso.c"
    1.39  #endif /* system type */
    1.40  
    1.41 -#include "SDL_types.h"
    1.42 -#include "SDL_error.h"
    1.43 -#include "SDL_loadso.h"
    1.44 -
    1.45 -void *SDL_LoadObject(const char *sofile)
    1.46 -{
    1.47 -	void *handle = NULL;
    1.48 -	const char *loaderror = "SDL_LoadObject() not implemented";
    1.49 -#if defined(USE_DLOPEN)
    1.50 -/* * */
    1.51 -	handle = dlopen(sofile, RTLD_NOW);
    1.52 -	loaderror = (char *)dlerror();
    1.53 -#elif defined(_WIN32_WCE)
    1.54 -/* * */
    1.55 -	char errbuf[512];
    1.56 -
    1.57 -	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
    1.58 -	wchar_t *sofile_t = malloc((MAX_PATH+1) * sizeof(wchar_t));
    1.59 -
    1.60 -	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sofile, -1, sofile_t, MAX_PATH);
    1.61 -	handle = (void *)LoadLibrary(sofile_t);
    1.62 -
    1.63 -	/* Generate an error message if all loads failed */
    1.64 -	if ( handle == NULL ) {
    1.65 -		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    1.66 -					FORMAT_MESSAGE_FROM_SYSTEM),
    1.67 -				NULL, GetLastError(), 
    1.68 -				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    1.69 -				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
    1.70 -		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
    1.71 -		loaderror = errbuf;
    1.72 -	}
    1.73 -
    1.74 -	free(sofile_t);
    1.75 -	free(errbuf_t);
    1.76 -
    1.77 -#elif defined(WIN32)
    1.78 -/* * */
    1.79 -	char errbuf[512];
    1.80 -
    1.81 -	handle = (void *)LoadLibrary(sofile);
    1.82 -
    1.83 -	/* Generate an error message if all loads failed */
    1.84 -	if ( handle == NULL ) {
    1.85 -		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    1.86 -					FORMAT_MESSAGE_FROM_SYSTEM),
    1.87 -				NULL, GetLastError(), 
    1.88 -				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    1.89 -				errbuf, SDL_TABLESIZE(errbuf), NULL);
    1.90 -		loaderror = errbuf;
    1.91 -	}
    1.92 -#elif defined(__BEOS__)
    1.93 -/* * */
    1.94 -	image_id library_id;
    1.95 -
    1.96 -	library_id = load_add_on(sofile);
    1.97 -	if ( library_id == B_ERROR ) {
    1.98 -		loaderror = "BeOS error";
    1.99 -	} else {
   1.100 -		handle = (void *)(library_id);
   1.101 -	}
   1.102 -#elif defined(macintosh)
   1.103 -/* * */
   1.104 -	CFragConnectionID library_id;
   1.105 -	Ptr mainAddr;
   1.106 -	Str255 errName;
   1.107 -	OSErr error;
   1.108 -	char psofile[512];
   1.109 -
   1.110 -	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
   1.111 -	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
   1.112 -	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
   1.113 -			kLoadCFrag, &library_id, &mainAddr, errName);
   1.114 -	switch (error) {
   1.115 -		case noErr:
   1.116 -			loaderror = NULL;
   1.117 -			break;
   1.118 -		case cfragNoLibraryErr:
   1.119 -			loaderror = "Library not found";
   1.120 -			break;
   1.121 -		case cfragUnresolvedErr:
   1.122 -			loaderror = "Unabled to resolve symbols";
   1.123 -			break;
   1.124 -		case cfragNoPrivateMemErr:
   1.125 -		case cfragNoClientMemErr:
   1.126 -			loaderror = "Out of memory";
   1.127 -			break;
   1.128 -		default:
   1.129 -			loaderror = "Unknown Code Fragment Manager error";
   1.130 -			break;
   1.131 -	}
   1.132 -	if ( loaderror == NULL ) {
   1.133 -		handle = (void *)(library_id);
   1.134 -	}
   1.135 -#elif defined(__MINT__) && defined(ENABLE_LDG)
   1.136 -/* * */
   1.137 -	handle = (void *)ldg_open((char *)sofile, ldg_global);
   1.138 -#endif /* system type */
   1.139 -
   1.140 -	if ( handle == NULL ) {
   1.141 -		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
   1.142 -	}
   1.143 -	return(handle);
   1.144 -}
   1.145 -
   1.146 -void *SDL_LoadFunction(void *handle, const char *name)
   1.147 -{
   1.148 -	void *symbol = NULL;
   1.149 -	const char *loaderror = "SDL_LoadFunction not implemented";
   1.150 -#if defined(USE_DLOPEN)
   1.151 -/* * */
   1.152 -	symbol = dlsym(handle, name);
   1.153 -	if ( symbol == NULL ) {
   1.154 -		loaderror = (char *)dlerror();
   1.155 -	}
   1.156 -#elif defined(_WIN32_WCE)
   1.157 -/* * */
   1.158 -	char errbuf[512];
   1.159 -	int length = strlen(name);
   1.160 -
   1.161 -	wchar_t *name_t = malloc((length + 1) * sizeof(wchar_t));
   1.162 -	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
   1.163 -
   1.164 -	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, name_t, length);
   1.165 -
   1.166 -	symbol = (void *)GetProcAddress((HMODULE)handle, name_t);
   1.167 -	if ( symbol == NULL ) {
   1.168 -		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   1.169 -					FORMAT_MESSAGE_FROM_SYSTEM),
   1.170 -				NULL, GetLastError(), 
   1.171 -				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   1.172 -				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
   1.173 -		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
   1.174 -		loaderror = errbuf;
   1.175 -	}
   1.176 -
   1.177 -	free(name_t);
   1.178 -	free(errbuf_t);
   1.179 -
   1.180 -#elif defined(WIN32)
   1.181 -/* * */
   1.182 -	char errbuf[512];
   1.183 -
   1.184 -	symbol = (void *)GetProcAddress((HMODULE)handle, name);
   1.185 -	if ( symbol == NULL ) {
   1.186 -		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   1.187 -					FORMAT_MESSAGE_FROM_SYSTEM),
   1.188 -				NULL, GetLastError(), 
   1.189 -				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   1.190 -				errbuf, SDL_TABLESIZE(errbuf), NULL);
   1.191 -		loaderror = errbuf;
   1.192 -	}
   1.193 -#elif defined(__BEOS__)
   1.194 -/* * */
   1.195 -	image_id library_id = (image_id)handle;
   1.196 -	if ( get_image_symbol(library_id,
   1.197 -		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
   1.198 -		loaderror = "Symbol not found";
   1.199 -	}
   1.200 -#elif defined(macintosh)
   1.201 -/* * */
   1.202 -	CFragSymbolClass class;
   1.203 -	CFragConnectionID library_id = (CFragConnectionID)handle;
   1.204 -	char pname[512];
   1.205 -
   1.206 -	strncpy(pname, name, SDL_TABLESIZE(pname));
   1.207 -	pname[SDL_TABLESIZE(pname)-1] = '\0';
   1.208 -	if ( FindSymbol(library_id, C2PStr(pname),
   1.209 -	                (char **)&symbol, &class) != noErr ) {
   1.210 -		loaderror = "Symbol not found";
   1.211 -	}
   1.212 -#elif defined(__MINT__) && defined(ENABLE_LDG)
   1.213 -/* * */
   1.214 -	symbol = (void *)ldg_find((char *)name, (LDG *)handle);
   1.215 -#endif /* system type */
   1.216 -
   1.217 -	if ( symbol == NULL ) {
   1.218 -		SDL_SetError("Failed loading %s: %s", name, loaderror);
   1.219 -	}
   1.220 -	return(symbol);
   1.221 -}
   1.222 -
   1.223 -void SDL_UnloadObject(void *handle)
   1.224 -{
   1.225 -#if defined(__BEOS__)
   1.226 -	image_id library_id;
   1.227 -#elif defined(macintosh)
   1.228 -	CFragConnectionID library_id;
   1.229 -#endif
   1.230 -	if ( handle == NULL ) {
   1.231 -		return;
   1.232 -	}
   1.233 -#if defined(USE_DLOPEN)
   1.234 -/* * */
   1.235 -	dlclose(handle);
   1.236 -#elif defined(WIN32)
   1.237 -/* * */
   1.238 -	FreeLibrary((HMODULE)handle);
   1.239 -#elif defined(__BEOS__)
   1.240 -/* * */
   1.241 -	library_id = (image_id)handle;
   1.242 -	unload_add_on(library_id);
   1.243 -#elif defined(macintosh)
   1.244 -/* * */
   1.245 -	library_id = (CFragConnectionID)handle;
   1.246 -	CloseConnection(&library_id);
   1.247 -#elif defined(__MINT__) && defined(ENABLE_LDG)
   1.248 -/* * */
   1.249 -	ldg_close((LDG *)handle, ldg_global);
   1.250 -#endif /* system type */
   1.251 -}