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