src/SDL_loadso.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 29 Sep 2005 09:43:00 +0000
changeset 1152 51a8702d8ecd
parent 1135 cf6133247d34
child 1173 e9cf8c1b4590
permissions -rw-r--r--
Updates to PocketPC (WinCE) support, thanks to Dmitry Yakimov at
activekitten.com.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    29 /* System dependent library loading routines                           */
    30 
    31 #include <stdio.h>
    32 #if defined(USE_DLOPEN)
    33 # include <dlfcn.h>
    34 #elif defined(WIN32) || defined(_WIN32_WCE)
    35 # include <windows.h>
    36 #elif defined(__BEOS__)
    37 # include <be/kernel/image.h>
    38 #elif defined(macintosh)
    39 # include <string.h>
    40 #define OLDP2C 1
    41 # include <Strings.h>
    42 # include <CodeFragments.h>
    43 # include <Errors.h>
    44 #elif defined(__MINT__) && defined(ENABLE_LDG)
    45 # include <gem.h>
    46 # include <ldg.h>
    47 #else
    48 /*#error Unsupported dynamic link environment*/
    49 #endif /* system type */
    50 
    51 #include "SDL_types.h"
    52 #include "SDL_error.h"
    53 #include "SDL_loadso.h"
    54 
    55 void *SDL_LoadObject(const char *sofile)
    56 {
    57 	void *handle = NULL;
    58 	const char *loaderror = "SDL_LoadObject() not implemented";
    59 #if defined(USE_DLOPEN)
    60 /* * */
    61 	handle = dlopen(sofile, RTLD_NOW);
    62 	loaderror = (char *)dlerror();
    63 #elif defined(_WIN32_WCE)
    64 /* * */
    65 	char errbuf[512];
    66 
    67 	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
    68 	wchar_t *sofile_t = malloc((MAX_PATH+1) * sizeof(wchar_t));
    69 
    70 	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sofile, -1, sofile_t, MAX_PATH);
    71 	handle = (void *)LoadLibrary(sofile_t);
    72 
    73 	/* Generate an error message if all loads failed */
    74 	if ( handle == NULL ) {
    75 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    76 					FORMAT_MESSAGE_FROM_SYSTEM),
    77 				NULL, GetLastError(), 
    78 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    79 				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
    80 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
    81 		loaderror = errbuf;
    82 	}
    83 
    84 	free(sofile_t);
    85 	free(errbuf_t);
    86 
    87 #elif defined(WIN32)
    88 /* * */
    89 	char errbuf[512];
    90 
    91 	handle = (void *)LoadLibrary(sofile);
    92 
    93 	/* Generate an error message if all loads failed */
    94 	if ( handle == NULL ) {
    95 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    96 					FORMAT_MESSAGE_FROM_SYSTEM),
    97 				NULL, GetLastError(), 
    98 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    99 				errbuf, SDL_TABLESIZE(errbuf), NULL);
   100 		loaderror = errbuf;
   101 	}
   102 #elif defined(__BEOS__)
   103 /* * */
   104 	image_id library_id;
   105 
   106 	library_id = load_add_on(sofile);
   107 	if ( library_id == B_ERROR ) {
   108 		loaderror = "BeOS error";
   109 	} else {
   110 		handle = (void *)(library_id);
   111 	}
   112 #elif defined(macintosh)
   113 /* * */
   114 	CFragConnectionID library_id;
   115 	Ptr mainAddr;
   116 	Str255 errName;
   117 	OSErr error;
   118 	char psofile[512];
   119 
   120 	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
   121 	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
   122 	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
   123 			kLoadCFrag, &library_id, &mainAddr, errName);
   124 	switch (error) {
   125 		case noErr:
   126 			loaderror = NULL;
   127 			break;
   128 		case cfragNoLibraryErr:
   129 			loaderror = "Library not found";
   130 			break;
   131 		case cfragUnresolvedErr:
   132 			loaderror = "Unabled to resolve symbols";
   133 			break;
   134 		case cfragNoPrivateMemErr:
   135 		case cfragNoClientMemErr:
   136 			loaderror = "Out of memory";
   137 			break;
   138 		default:
   139 			loaderror = "Unknown Code Fragment Manager error";
   140 			break;
   141 	}
   142 	if ( loaderror == NULL ) {
   143 		handle = (void *)(library_id);
   144 	}
   145 #elif defined(__MINT__) && defined(ENABLE_LDG)
   146 /* * */
   147 	handle = (void *)ldg_open((char *)sofile, ldg_global);
   148 #endif /* system type */
   149 
   150 	if ( handle == NULL ) {
   151 		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
   152 	}
   153 	return(handle);
   154 }
   155 
   156 void *SDL_LoadFunction(void *handle, const char *name)
   157 {
   158 	void *symbol = NULL;
   159 	const char *loaderror = "SDL_LoadFunction not implemented";
   160 #if defined(USE_DLOPEN)
   161 /* * */
   162 	symbol = dlsym(handle, name);
   163 	if ( symbol == NULL ) {
   164 		loaderror = (char *)dlerror();
   165 	}
   166 #elif defined(_WIN32_WCE)
   167 /* * */
   168 	char errbuf[512];
   169 	int length = strlen(name);
   170 
   171 	wchar_t *name_t = malloc((length + 1) * sizeof(wchar_t));
   172 	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
   173 
   174 	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, name_t, length);
   175 
   176 	symbol = (void *)GetProcAddress((HMODULE)handle, name_t);
   177 	if ( symbol == NULL ) {
   178 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   179 					FORMAT_MESSAGE_FROM_SYSTEM),
   180 				NULL, GetLastError(), 
   181 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   182 				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
   183 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
   184 		loaderror = errbuf;
   185 	}
   186 
   187 	free(name_t);
   188 	free(errbuf_t);
   189 
   190 #elif defined(WIN32)
   191 /* * */
   192 	char errbuf[512];
   193 
   194 	symbol = (void *)GetProcAddress((HMODULE)handle, name);
   195 	if ( symbol == NULL ) {
   196 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   197 					FORMAT_MESSAGE_FROM_SYSTEM),
   198 				NULL, GetLastError(), 
   199 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   200 				errbuf, SDL_TABLESIZE(errbuf), NULL);
   201 		loaderror = errbuf;
   202 	}
   203 #elif defined(__BEOS__)
   204 /* * */
   205 	image_id library_id = (image_id)handle;
   206 	if ( get_image_symbol(library_id,
   207 		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
   208 		loaderror = "Symbol not found";
   209 	}
   210 #elif defined(macintosh)
   211 /* * */
   212 	CFragSymbolClass class;
   213 	CFragConnectionID library_id = (CFragConnectionID)handle;
   214 	char pname[512];
   215 
   216 	strncpy(pname, name, SDL_TABLESIZE(pname));
   217 	pname[SDL_TABLESIZE(pname)-1] = '\0';
   218 	if ( FindSymbol(library_id, C2PStr(pname),
   219 	                (char **)&symbol, &class) != noErr ) {
   220 		loaderror = "Symbol not found";
   221 	}
   222 #elif defined(__MINT__) && defined(ENABLE_LDG)
   223 /* * */
   224 	symbol = (void *)ldg_find((char *)name, (LDG *)handle);
   225 #endif /* system type */
   226 
   227 	if ( symbol == NULL ) {
   228 		SDL_SetError("Failed loading %s: %s", name, loaderror);
   229 	}
   230 	return(symbol);
   231 }
   232 
   233 void SDL_UnloadObject(void *handle)
   234 {
   235 #if defined(__BEOS__)
   236 	image_id library_id;
   237 #elif defined(macintosh)
   238 	CFragConnectionID library_id;
   239 #endif
   240 	if ( handle == NULL ) {
   241 		return;
   242 	}
   243 #if defined(USE_DLOPEN)
   244 /* * */
   245 	dlclose(handle);
   246 #elif defined(WIN32)
   247 /* * */
   248 	FreeLibrary((HMODULE)handle);
   249 #elif defined(__BEOS__)
   250 /* * */
   251 	library_id = (image_id)handle;
   252 	unload_add_on(library_id);
   253 #elif defined(macintosh)
   254 /* * */
   255 	library_id = (CFragConnectionID)handle;
   256 	CloseConnection(&library_id);
   257 #elif defined(__MINT__) && defined(ENABLE_LDG)
   258 /* * */
   259 	ldg_close((LDG *)handle, ldg_global);
   260 #endif /* system type */
   261 }