src/SDL_loadso.c
author Patrice Mandin <patmandin@gmail.com>
Mon, 06 Dec 2004 15:01:00 +0000
changeset 1007 335d9376a2cd
parent 774 0c3e00cc9580
child 1135 cf6133247d34
permissions -rw-r--r--
Document new OpenGL feature
     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)
    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 #elif defined(__MINT__) && defined(ENABLE_LDG)
    44 # include <gem.h>
    45 # include <ldg.h>
    46 #else
    47 /*#error Unsupported dynamic link environment*/
    48 #endif /* system type */
    49 
    50 #include "SDL_types.h"
    51 #include "SDL_error.h"
    52 #include "SDL_loadso.h"
    53 
    54 void *SDL_LoadObject(const char *sofile)
    55 {
    56 	void *handle = NULL;
    57 	const char *loaderror = "SDL_LoadObject() not implemented";
    58 #if defined(USE_DLOPEN)
    59 /* * */
    60 	handle = dlopen(sofile, RTLD_NOW);
    61 	loaderror = (char *)dlerror();
    62 #elif defined(WIN32)
    63 /* * */
    64 	char errbuf[512];
    65 
    66 	handle = (void *)LoadLibrary(sofile);
    67 
    68 	/* Generate an error message if all loads failed */
    69 	if ( handle == NULL ) {
    70 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    71 					FORMAT_MESSAGE_FROM_SYSTEM),
    72 				NULL, GetLastError(), 
    73 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    74 				errbuf, SDL_TABLESIZE(errbuf), NULL);
    75 		loaderror = errbuf;
    76 	}
    77 #elif defined(__BEOS__)
    78 /* * */
    79 	image_id library_id;
    80 
    81 	library_id = load_add_on(sofile);
    82 	if ( library_id == B_ERROR ) {
    83 		loaderror = "BeOS error";
    84 	} else {
    85 		handle = (void *)(library_id);
    86 	}
    87 #elif defined(macintosh)
    88 /* * */
    89 	CFragConnectionID library_id;
    90 	Ptr mainAddr;
    91 	Str255 errName;
    92 	OSErr error;
    93 	char psofile[512];
    94 
    95 	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
    96 	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
    97 	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
    98 			kLoadCFrag, &library_id, &mainAddr, errName);
    99 	switch (error) {
   100 		case noErr:
   101 			loaderror = NULL;
   102 			break;
   103 		case cfragNoLibraryErr:
   104 			loaderror = "Library not found";
   105 			break;
   106 		case cfragUnresolvedErr:
   107 			loaderror = "Unabled to resolve symbols";
   108 			break;
   109 		case cfragNoPrivateMemErr:
   110 		case cfragNoClientMemErr:
   111 			loaderror = "Out of memory";
   112 			break;
   113 		default:
   114 			loaderror = "Unknown Code Fragment Manager error";
   115 			break;
   116 	}
   117 	if ( loaderror == NULL ) {
   118 		handle = (void *)(library_id);
   119 	}
   120 #elif defined(__MINT__) && defined(ENABLE_LDG)
   121 /* * */
   122 	handle = (void *)ldg_open((char *)sofile, ldg_global);
   123 #endif /* system type */
   124 
   125 	if ( handle == NULL ) {
   126 		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
   127 	}
   128 	return(handle);
   129 }
   130 
   131 void *SDL_LoadFunction(void *handle, const char *name)
   132 {
   133 	void *symbol = NULL;
   134 	const char *loaderror = "SDL_LoadFunction not implemented";
   135 #if defined(USE_DLOPEN)
   136 /* * */
   137 	symbol = dlsym(handle, name);
   138 	if ( symbol == NULL ) {
   139 		loaderror = (char *)dlerror();
   140 	}
   141 #elif defined(WIN32)
   142 /* * */
   143 	char errbuf[512];
   144 
   145 	symbol = (void *)GetProcAddress((HMODULE)handle, name);
   146 	if ( symbol == NULL ) {
   147 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   148 					FORMAT_MESSAGE_FROM_SYSTEM),
   149 				NULL, GetLastError(), 
   150 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   151 				errbuf, SDL_TABLESIZE(errbuf), NULL);
   152 		loaderror = errbuf;
   153 	}
   154 #elif defined(__BEOS__)
   155 /* * */
   156 	image_id library_id = (image_id)handle;
   157 	if ( get_image_symbol(library_id,
   158 		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
   159 		loaderror = "Symbol not found";
   160 	}
   161 #elif defined(macintosh)
   162 /* * */
   163 	CFragSymbolClass class;
   164 	CFragConnectionID library_id = (CFragConnectionID)handle;
   165 	char pname[512];
   166 
   167 	strncpy(pname, name, SDL_TABLESIZE(pname));
   168 	pname[SDL_TABLESIZE(pname)-1] = '\0';
   169 	if ( FindSymbol(library_id, C2PStr(pname),
   170 	                (char **)&symbol, &class) != noErr ) {
   171 		loaderror = "Symbol not found";
   172 	}
   173 #elif defined(__MINT__) && defined(ENABLE_LDG)
   174 /* * */
   175 	symbol = (void *)ldg_find((char *)name, (LDG *)handle);
   176 #endif /* system type */
   177 
   178 	if ( symbol == NULL ) {
   179 		SDL_SetError("Failed loading %s: %s", name, loaderror);
   180 	}
   181 	return(symbol);
   182 }
   183 
   184 void SDL_UnloadObject(void *handle)
   185 {
   186 #if defined(__BEOS__)
   187 	image_id library_id;
   188 #elif defined(macintosh)
   189 	CFragConnectionID library_id;
   190 #endif
   191 	if ( handle == NULL ) {
   192 		return;
   193 	}
   194 #if defined(USE_DLOPEN)
   195 /* * */
   196 	dlclose(handle);
   197 #elif defined(WIN32)
   198 /* * */
   199 	FreeLibrary((HMODULE)handle);
   200 #elif defined(__BEOS__)
   201 /* * */
   202 	library_id = (image_id)handle;
   203 	unload_add_on(library_id);
   204 #elif defined(macintosh)
   205 /* * */
   206 	library_id = (CFragConnectionID)handle;
   207 	CloseConnection(&library_id);
   208 #elif defined(__MINT__) && defined(ENABLE_LDG)
   209 /* * */
   210 	ldg_close((LDG *)handle, ldg_global);
   211 #endif /* system type */
   212 }