src/SDL_loadso.c
author Ryan C. Gordon
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.
slouken@294
     1
/*
slouken@294
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 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>
icculus@1152
    34
#elif defined(WIN32) || defined(_WIN32_WCE)
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>
icculus@1135
    40
#define OLDP2C 1
slouken@294
    41
# include <Strings.h>
slouken@294
    42
# include <CodeFragments.h>
slouken@294
    43
# include <Errors.h>
patmandin@651
    44
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
    45
# include <gem.h>
patmandin@651
    46
# include <ldg.h>
slouken@294
    47
#else
slouken@294
    48
/*#error Unsupported dynamic link environment*/
slouken@294
    49
#endif /* system type */
slouken@294
    50
slouken@296
    51
#include "SDL_types.h"
slouken@294
    52
#include "SDL_error.h"
slouken@294
    53
#include "SDL_loadso.h"
slouken@294
    54
slouken@294
    55
void *SDL_LoadObject(const char *sofile)
slouken@294
    56
{
slouken@294
    57
	void *handle = NULL;
slouken@294
    58
	const char *loaderror = "SDL_LoadObject() not implemented";
slouken@294
    59
#if defined(USE_DLOPEN)
slouken@294
    60
/* * */
slouken@294
    61
	handle = dlopen(sofile, RTLD_NOW);
slouken@294
    62
	loaderror = (char *)dlerror();
icculus@1152
    63
#elif defined(_WIN32_WCE)
icculus@1152
    64
/* * */
icculus@1152
    65
	char errbuf[512];
icculus@1152
    66
icculus@1152
    67
	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
icculus@1152
    68
	wchar_t *sofile_t = malloc((MAX_PATH+1) * sizeof(wchar_t));
icculus@1152
    69
icculus@1152
    70
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sofile, -1, sofile_t, MAX_PATH);
icculus@1152
    71
	handle = (void *)LoadLibrary(sofile_t);
icculus@1152
    72
icculus@1152
    73
	/* Generate an error message if all loads failed */
icculus@1152
    74
	if ( handle == NULL ) {
icculus@1152
    75
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
icculus@1152
    76
					FORMAT_MESSAGE_FROM_SYSTEM),
icculus@1152
    77
				NULL, GetLastError(), 
icculus@1152
    78
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
icculus@1152
    79
				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
icculus@1152
    80
		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
icculus@1152
    81
		loaderror = errbuf;
icculus@1152
    82
	}
icculus@1152
    83
icculus@1152
    84
	free(sofile_t);
icculus@1152
    85
	free(errbuf_t);
icculus@1152
    86
slouken@294
    87
#elif defined(WIN32)
slouken@294
    88
/* * */
slouken@294
    89
	char errbuf[512];
slouken@294
    90
slouken@294
    91
	handle = (void *)LoadLibrary(sofile);
slouken@294
    92
slouken@294
    93
	/* Generate an error message if all loads failed */
slouken@294
    94
	if ( handle == NULL ) {
slouken@294
    95
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
    96
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
    97
				NULL, GetLastError(), 
slouken@294
    98
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
    99
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
   100
		loaderror = errbuf;
slouken@294
   101
	}
slouken@294
   102
#elif defined(__BEOS__)
slouken@294
   103
/* * */
slouken@294
   104
	image_id library_id;
slouken@294
   105
slouken@294
   106
	library_id = load_add_on(sofile);
slouken@294
   107
	if ( library_id == B_ERROR ) {
slouken@294
   108
		loaderror = "BeOS error";
slouken@294
   109
	} else {
slouken@294
   110
		handle = (void *)(library_id);
slouken@294
   111
	}
slouken@294
   112
#elif defined(macintosh)
slouken@294
   113
/* * */
slouken@294
   114
	CFragConnectionID library_id;
slouken@294
   115
	Ptr mainAddr;
slouken@294
   116
	Str255 errName;
slouken@294
   117
	OSErr error;
slouken@294
   118
	char psofile[512];
slouken@294
   119
slouken@294
   120
	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
slouken@294
   121
	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
slouken@294
   122
	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
slouken@294
   123
			kLoadCFrag, &library_id, &mainAddr, errName);
slouken@294
   124
	switch (error) {
slouken@294
   125
		case noErr:
slouken@774
   126
			loaderror = NULL;
slouken@294
   127
			break;
slouken@294
   128
		case cfragNoLibraryErr:
slouken@294
   129
			loaderror = "Library not found";
slouken@294
   130
			break;
slouken@294
   131
		case cfragUnresolvedErr:
slouken@294
   132
			loaderror = "Unabled to resolve symbols";
slouken@294
   133
			break;
slouken@294
   134
		case cfragNoPrivateMemErr:
slouken@294
   135
		case cfragNoClientMemErr:
slouken@294
   136
			loaderror = "Out of memory";
slouken@294
   137
			break;
slouken@294
   138
		default:
slouken@294
   139
			loaderror = "Unknown Code Fragment Manager error";
slouken@294
   140
			break;
slouken@294
   141
	}
slouken@294
   142
	if ( loaderror == NULL ) {
slouken@294
   143
		handle = (void *)(library_id);
slouken@294
   144
	}
patmandin@651
   145
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   146
/* * */
patmandin@651
   147
	handle = (void *)ldg_open((char *)sofile, ldg_global);
slouken@294
   148
#endif /* system type */
slouken@294
   149
slouken@294
   150
	if ( handle == NULL ) {
slouken@294
   151
		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@294
   152
	}
slouken@294
   153
	return(handle);
slouken@294
   154
}
slouken@294
   155
slouken@294
   156
void *SDL_LoadFunction(void *handle, const char *name)
slouken@294
   157
{
slouken@294
   158
	void *symbol = NULL;
slouken@294
   159
	const char *loaderror = "SDL_LoadFunction not implemented";
slouken@294
   160
#if defined(USE_DLOPEN)
slouken@294
   161
/* * */
slouken@294
   162
	symbol = dlsym(handle, name);
slouken@294
   163
	if ( symbol == NULL ) {
slouken@294
   164
		loaderror = (char *)dlerror();
slouken@294
   165
	}
icculus@1152
   166
#elif defined(_WIN32_WCE)
icculus@1152
   167
/* * */
icculus@1152
   168
	char errbuf[512];
icculus@1152
   169
	int length = strlen(name);
icculus@1152
   170
icculus@1152
   171
	wchar_t *name_t = malloc((length + 1) * sizeof(wchar_t));
icculus@1152
   172
	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
icculus@1152
   173
icculus@1152
   174
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, name_t, length);
icculus@1152
   175
icculus@1152
   176
	symbol = (void *)GetProcAddress((HMODULE)handle, name_t);
icculus@1152
   177
	if ( symbol == NULL ) {
icculus@1152
   178
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
icculus@1152
   179
					FORMAT_MESSAGE_FROM_SYSTEM),
icculus@1152
   180
				NULL, GetLastError(), 
icculus@1152
   181
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
icculus@1152
   182
				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
icculus@1152
   183
		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
icculus@1152
   184
		loaderror = errbuf;
icculus@1152
   185
	}
icculus@1152
   186
icculus@1152
   187
	free(name_t);
icculus@1152
   188
	free(errbuf_t);
icculus@1152
   189
slouken@294
   190
#elif defined(WIN32)
slouken@294
   191
/* * */
slouken@294
   192
	char errbuf[512];
slouken@294
   193
slouken@294
   194
	symbol = (void *)GetProcAddress((HMODULE)handle, name);
slouken@294
   195
	if ( symbol == NULL ) {
slouken@294
   196
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
   197
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
   198
				NULL, GetLastError(), 
slouken@294
   199
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
   200
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
   201
		loaderror = errbuf;
slouken@294
   202
	}
slouken@294
   203
#elif defined(__BEOS__)
slouken@294
   204
/* * */
slouken@294
   205
	image_id library_id = (image_id)handle;
slouken@294
   206
	if ( get_image_symbol(library_id,
slouken@294
   207
		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
slouken@294
   208
		loaderror = "Symbol not found";
slouken@294
   209
	}
slouken@294
   210
#elif defined(macintosh)
slouken@294
   211
/* * */
slouken@294
   212
	CFragSymbolClass class;
slouken@294
   213
	CFragConnectionID library_id = (CFragConnectionID)handle;
slouken@294
   214
	char pname[512];
slouken@294
   215
slouken@294
   216
	strncpy(pname, name, SDL_TABLESIZE(pname));
slouken@294
   217
	pname[SDL_TABLESIZE(pname)-1] = '\0';
slouken@294
   218
	if ( FindSymbol(library_id, C2PStr(pname),
slouken@294
   219
	                (char **)&symbol, &class) != noErr ) {
slouken@294
   220
		loaderror = "Symbol not found";
slouken@294
   221
	}
patmandin@651
   222
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   223
/* * */
patmandin@651
   224
	symbol = (void *)ldg_find((char *)name, (LDG *)handle);
slouken@294
   225
#endif /* system type */
slouken@294
   226
slouken@294
   227
	if ( symbol == NULL ) {
slouken@294
   228
		SDL_SetError("Failed loading %s: %s", name, loaderror);
slouken@294
   229
	}
slouken@294
   230
	return(symbol);
slouken@294
   231
}
slouken@294
   232
slouken@294
   233
void SDL_UnloadObject(void *handle)
slouken@294
   234
{
slouken@336
   235
#if defined(__BEOS__)
slouken@336
   236
	image_id library_id;
slouken@707
   237
#elif defined(macintosh)
slouken@707
   238
	CFragConnectionID library_id;
slouken@336
   239
#endif
slouken@294
   240
	if ( handle == NULL ) {
slouken@294
   241
		return;
slouken@294
   242
	}
slouken@294
   243
#if defined(USE_DLOPEN)
slouken@294
   244
/* * */
slouken@294
   245
	dlclose(handle);
slouken@294
   246
#elif defined(WIN32)
slouken@294
   247
/* * */
slouken@294
   248
	FreeLibrary((HMODULE)handle);
slouken@294
   249
#elif defined(__BEOS__)
slouken@294
   250
/* * */
slouken@336
   251
	library_id = (image_id)handle;
slouken@294
   252
	unload_add_on(library_id);
slouken@294
   253
#elif defined(macintosh)
slouken@294
   254
/* * */
slouken@707
   255
	library_id = (CFragConnectionID)handle;
slouken@708
   256
	CloseConnection(&library_id);
patmandin@651
   257
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   258
/* * */
patmandin@651
   259
	ldg_close((LDG *)handle, ldg_global);
slouken@294
   260
#endif /* system type */
slouken@294
   261
}