src/SDL_loadso.c
author Ryan C. Gordon
Thu, 08 Sep 2005 06:34:28 +0000
changeset 1135 cf6133247d34
parent 774 0c3e00cc9580
child 1152 51a8702d8ecd
permissions -rw-r--r--
Mac Classic and CodeWarrior patches.

--ryan.



From: =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb@algonet.se>
Subject: Re: [SDL] Updated Mac patch
Date: Tue, 6 Sep 2005 15:21:27 +0200
To: A list for developers using the SDL library <sdl@libsdl.org>

Earlier, I wrote:

> Updated the previous Mac patch to disable Carbon by default.
> Also "fixed" the SDL.spec again, so that it builds on Darwin.
>
> http://www.algonet.se/~afb/SDL-1.2.9-mac.patch
> Also applied fine to SDL12 CVS, when I tried it.
>
> Haven't completed any new packaging or projects for Xcode/PB,
> but it seems to build and install fine here (in development).

Tested the new patch to build with old CodeWarrior and MPW,
and it seems it needed some hacks with those old headers...

Just in case you want to support the archeological versions -
here is a small add-on to the above patch, to fix those...

http://www.algonet.se/~afb/SDL-1.2.9-classic.patch


I couldn't get the old CW5 projects to build without a few
modifications - such as deleting the stray old header in:
"CWprojects/Support/Carbon/Include/ConditionalMacros.h" ?

But I updated both projects to CW6 too and built for Carbon,
and it ran all of the Mac test projects without any problems.
The MPW file seems to have compiled, with a small order change.


As long as you're still shipping the CWProjects and MPWmake
with the download, they should probably be updated/fixed ?
(another "solution" would of course be to just delete them)

I'll post my new projects along with the new Xcode projects
later on, along with XML exports of the various .mcp files.
(CW5 builds for Classic / "PPC", and CW6 builds for Carbon)


It'll be packaged as a part of the next SpriteWorld X release...
http://spriteworldx.sourceforge.net/ [Classic/Carbon/Win/X11]

--anders
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>
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>
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();
slouken@294
    63
#elif defined(WIN32)
slouken@294
    64
/* * */
slouken@294
    65
	char errbuf[512];
slouken@294
    66
slouken@294
    67
	handle = (void *)LoadLibrary(sofile);
slouken@294
    68
slouken@294
    69
	/* Generate an error message if all loads failed */
slouken@294
    70
	if ( handle == NULL ) {
slouken@294
    71
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
    72
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
    73
				NULL, GetLastError(), 
slouken@294
    74
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
    75
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
    76
		loaderror = errbuf;
slouken@294
    77
	}
slouken@294
    78
#elif defined(__BEOS__)
slouken@294
    79
/* * */
slouken@294
    80
	image_id library_id;
slouken@294
    81
slouken@294
    82
	library_id = load_add_on(sofile);
slouken@294
    83
	if ( library_id == B_ERROR ) {
slouken@294
    84
		loaderror = "BeOS error";
slouken@294
    85
	} else {
slouken@294
    86
		handle = (void *)(library_id);
slouken@294
    87
	}
slouken@294
    88
#elif defined(macintosh)
slouken@294
    89
/* * */
slouken@294
    90
	CFragConnectionID library_id;
slouken@294
    91
	Ptr mainAddr;
slouken@294
    92
	Str255 errName;
slouken@294
    93
	OSErr error;
slouken@294
    94
	char psofile[512];
slouken@294
    95
slouken@294
    96
	strncpy(psofile, sofile, SDL_TABLESIZE(psofile));
slouken@294
    97
	psofile[SDL_TABLESIZE(psofile)-1] = '\0';
slouken@294
    98
	error = GetSharedLibrary(C2PStr(psofile), kCompiledCFragArch,
slouken@294
    99
			kLoadCFrag, &library_id, &mainAddr, errName);
slouken@294
   100
	switch (error) {
slouken@294
   101
		case noErr:
slouken@774
   102
			loaderror = NULL;
slouken@294
   103
			break;
slouken@294
   104
		case cfragNoLibraryErr:
slouken@294
   105
			loaderror = "Library not found";
slouken@294
   106
			break;
slouken@294
   107
		case cfragUnresolvedErr:
slouken@294
   108
			loaderror = "Unabled to resolve symbols";
slouken@294
   109
			break;
slouken@294
   110
		case cfragNoPrivateMemErr:
slouken@294
   111
		case cfragNoClientMemErr:
slouken@294
   112
			loaderror = "Out of memory";
slouken@294
   113
			break;
slouken@294
   114
		default:
slouken@294
   115
			loaderror = "Unknown Code Fragment Manager error";
slouken@294
   116
			break;
slouken@294
   117
	}
slouken@294
   118
	if ( loaderror == NULL ) {
slouken@294
   119
		handle = (void *)(library_id);
slouken@294
   120
	}
patmandin@651
   121
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   122
/* * */
patmandin@651
   123
	handle = (void *)ldg_open((char *)sofile, ldg_global);
slouken@294
   124
#endif /* system type */
slouken@294
   125
slouken@294
   126
	if ( handle == NULL ) {
slouken@294
   127
		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
slouken@294
   128
	}
slouken@294
   129
	return(handle);
slouken@294
   130
}
slouken@294
   131
slouken@294
   132
void *SDL_LoadFunction(void *handle, const char *name)
slouken@294
   133
{
slouken@294
   134
	void *symbol = NULL;
slouken@294
   135
	const char *loaderror = "SDL_LoadFunction not implemented";
slouken@294
   136
#if defined(USE_DLOPEN)
slouken@294
   137
/* * */
slouken@294
   138
	symbol = dlsym(handle, name);
slouken@294
   139
	if ( symbol == NULL ) {
slouken@294
   140
		loaderror = (char *)dlerror();
slouken@294
   141
	}
slouken@294
   142
#elif defined(WIN32)
slouken@294
   143
/* * */
slouken@294
   144
	char errbuf[512];
slouken@294
   145
slouken@294
   146
	symbol = (void *)GetProcAddress((HMODULE)handle, name);
slouken@294
   147
	if ( symbol == NULL ) {
slouken@294
   148
		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
slouken@294
   149
					FORMAT_MESSAGE_FROM_SYSTEM),
slouken@294
   150
				NULL, GetLastError(), 
slouken@294
   151
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
slouken@294
   152
				errbuf, SDL_TABLESIZE(errbuf), NULL);
slouken@294
   153
		loaderror = errbuf;
slouken@294
   154
	}
slouken@294
   155
#elif defined(__BEOS__)
slouken@294
   156
/* * */
slouken@294
   157
	image_id library_id = (image_id)handle;
slouken@294
   158
	if ( get_image_symbol(library_id,
slouken@294
   159
		name, B_SYMBOL_TYPE_TEXT, &symbol) != B_NO_ERROR ) {
slouken@294
   160
		loaderror = "Symbol not found";
slouken@294
   161
	}
slouken@294
   162
#elif defined(macintosh)
slouken@294
   163
/* * */
slouken@294
   164
	CFragSymbolClass class;
slouken@294
   165
	CFragConnectionID library_id = (CFragConnectionID)handle;
slouken@294
   166
	char pname[512];
slouken@294
   167
slouken@294
   168
	strncpy(pname, name, SDL_TABLESIZE(pname));
slouken@294
   169
	pname[SDL_TABLESIZE(pname)-1] = '\0';
slouken@294
   170
	if ( FindSymbol(library_id, C2PStr(pname),
slouken@294
   171
	                (char **)&symbol, &class) != noErr ) {
slouken@294
   172
		loaderror = "Symbol not found";
slouken@294
   173
	}
patmandin@651
   174
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   175
/* * */
patmandin@651
   176
	symbol = (void *)ldg_find((char *)name, (LDG *)handle);
slouken@294
   177
#endif /* system type */
slouken@294
   178
slouken@294
   179
	if ( symbol == NULL ) {
slouken@294
   180
		SDL_SetError("Failed loading %s: %s", name, loaderror);
slouken@294
   181
	}
slouken@294
   182
	return(symbol);
slouken@294
   183
}
slouken@294
   184
slouken@294
   185
void SDL_UnloadObject(void *handle)
slouken@294
   186
{
slouken@336
   187
#if defined(__BEOS__)
slouken@336
   188
	image_id library_id;
slouken@707
   189
#elif defined(macintosh)
slouken@707
   190
	CFragConnectionID library_id;
slouken@336
   191
#endif
slouken@294
   192
	if ( handle == NULL ) {
slouken@294
   193
		return;
slouken@294
   194
	}
slouken@294
   195
#if defined(USE_DLOPEN)
slouken@294
   196
/* * */
slouken@294
   197
	dlclose(handle);
slouken@294
   198
#elif defined(WIN32)
slouken@294
   199
/* * */
slouken@294
   200
	FreeLibrary((HMODULE)handle);
slouken@294
   201
#elif defined(__BEOS__)
slouken@294
   202
/* * */
slouken@336
   203
	library_id = (image_id)handle;
slouken@294
   204
	unload_add_on(library_id);
slouken@294
   205
#elif defined(macintosh)
slouken@294
   206
/* * */
slouken@707
   207
	library_id = (CFragConnectionID)handle;
slouken@708
   208
	CloseConnection(&library_id);
patmandin@651
   209
#elif defined(__MINT__) && defined(ENABLE_LDG)
patmandin@651
   210
/* * */
patmandin@651
   211
	ldg_close((LDG *)handle, ldg_global);
slouken@294
   212
#endif /* system type */
slouken@294
   213
}