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