src/SDL_loadso.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 16 Sep 2002 07:09:04 +0000
changeset 496 864a66f028d8
parent 336 745873ea091f
child 651 35ff0890ac4e
permissions -rw-r--r--
Don't be fooled by unclutter - the mouse isn't really leaving our window
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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)
    35 # include <windows.h>
    36 #elif defined(__BEOS__)
    37 # include <be/kernel/image.h>
    38 #elif defined(macintosh)
    39 # include <string.h>
    40 # include <Strings.h>
    41 # include <CodeFragments.h>
    42 # include <Errors.h>
    43 #else
    44 /*#error Unsupported dynamic link environment*/
    45 #endif /* system type */
    46 
    47 #include "SDL_types.h"
    48 #include "SDL_error.h"
    49 #include "SDL_loadso.h"
    50 
    51 void *SDL_LoadObject(const char *sofile)
    52 {
    53 	void *handle = NULL;
    54 	const char *loaderror = "SDL_LoadObject() not implemented";
    55 #if defined(USE_DLOPEN)
    56 /* * */
    57 	handle = dlopen(sofile, RTLD_NOW);
    58 	loaderror = (char *)dlerror();
    59 #elif defined(WIN32)
    60 /* * */
    61 	char errbuf[512];
    62 
    63 	handle = (void *)LoadLibrary(sofile);
    64 
    65 	/* Generate an error message if all loads failed */
    66 	if ( handle == NULL ) {
    67 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    68 					FORMAT_MESSAGE_FROM_SYSTEM),
    69 				NULL, GetLastError(), 
    70 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    71 				errbuf, SDL_TABLESIZE(errbuf), NULL);
    72 		loaderror = errbuf;
    73 	}
    74 #elif defined(__BEOS__)
    75 /* * */
    76 	image_id library_id;
    77 
    78 	library_id = load_add_on(sofile);
    79 	if ( library_id == B_ERROR ) {
    80 		loaderror = "BeOS error";
    81 	} else {
    82 		handle = (void *)(library_id);
    83 	}
    84 #elif defined(macintosh)
    85 /* * */
    86 	CFragConnectionID library_id;
    87 	Ptr mainAddr;
    88 	Str255 errName;
    89 	OSErr error;
    90 	char psofile[512];
    91 
    92 	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
    93 	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
    94 	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
    95 			kLoadCFrag, &library_id, &mainAddr, errName);
    96 	switch (error) {
    97 		case noErr:
    98 			break;
    99 		case cfragNoLibraryErr:
   100 			loaderror = "Library not found";
   101 			break;
   102 		case cfragUnresolvedErr:
   103 			loaderror = "Unabled to resolve symbols";
   104 			break;
   105 		case cfragNoPrivateMemErr:
   106 		case cfragNoClientMemErr:
   107 			loaderror = "Out of memory";
   108 			break;
   109 		default:
   110 			loaderror = "Unknown Code Fragment Manager error";
   111 			break;
   112 	}
   113 	if ( loaderror == NULL ) {
   114 		handle = (void *)(library_id);
   115 	}
   116 #endif /* system type */
   117 
   118 	if ( handle == NULL ) {
   119 		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
   120 	}
   121 	return(handle);
   122 }
   123 
   124 void *SDL_LoadFunction(void *handle, const char *name)
   125 {
   126 	void *symbol = NULL;
   127 	const char *loaderror = "SDL_LoadFunction not implemented";
   128 #if defined(USE_DLOPEN)
   129 /* * */
   130 	symbol = dlsym(handle, name);
   131 	if ( symbol == NULL ) {
   132 		loaderror = (char *)dlerror();
   133 	}
   134 #elif defined(WIN32)
   135 /* * */
   136 	char errbuf[512];
   137 
   138 	symbol = (void *)GetProcAddress((HMODULE)handle, name);
   139 	if ( symbol == NULL ) {
   140 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   141 					FORMAT_MESSAGE_FROM_SYSTEM),
   142 				NULL, GetLastError(), 
   143 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   144 				errbuf, SDL_TABLESIZE(errbuf), NULL);
   145 		loaderror = errbuf;
   146 	}
   147 #elif defined(__BEOS__)
   148 /* * */
   149 	image_id library_id = (image_id)handle;
   150 	if ( get_image_symbol(library_id,
   151 		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
   152 		loaderror = "Symbol not found";
   153 	}
   154 #elif defined(macintosh)
   155 /* * */
   156 	CFragSymbolClass class;
   157 	CFragConnectionID library_id = (CFragConnectionID)handle;
   158 	char pname[512];
   159 
   160 	strncpy(pname, name, SDL_TABLESIZE(pname));
   161 	pname[SDL_TABLESIZE(pname)-1] = '\0';
   162 	if ( FindSymbol(library_id, C2PStr(pname),
   163 	                (char **)&symbol, &class) != noErr ) {
   164 		loaderror = "Symbol not found";
   165 	}
   166 #endif /* system type */
   167 
   168 	if ( symbol == NULL ) {
   169 		SDL_SetError("Failed loading %s: %s", name, loaderror);
   170 	}
   171 	return(symbol);
   172 }
   173 
   174 void SDL_UnloadObject(void *handle)
   175 {
   176 #if defined(__BEOS__)
   177 	image_id library_id;
   178 #endif
   179 	if ( handle == NULL ) {
   180 		return;
   181 	}
   182 #if defined(USE_DLOPEN)
   183 /* * */
   184 	dlclose(handle);
   185 #elif defined(WIN32)
   186 /* * */
   187 	FreeLibrary((HMODULE)handle);
   188 #elif defined(__BEOS__)
   189 /* * */
   190 	library_id = (image_id)handle;
   191 	unload_add_on(library_id);
   192 #elif defined(macintosh)
   193 /* * */
   194 	CFragConnectionID library_id = (CFragConnectionID)handle;
   195 	CloseConnection(library_id);
   196 #endif /* system type */
   197 }