Move back to using interposition to be more portable

develop
Timothy Collett 2012-06-29 10:15:48 -04:00
parent 4ca3aa878a
commit 6f433ff58f
3 changed files with 30 additions and 9 deletions

@ -38,11 +38,11 @@ distribution.
#include <string>
#include <map>
/*typedef struct interpose_s
typedef struct interpose_s
{
void *new_func;
void *orig_func;
} interpose_t;*/
} interpose_t;
#include "DFHack.h"
#include "Core.h"
@ -58,11 +58,18 @@ distribution.
};*/
#define DYLD_INTERPOSE(_replacment,_replacee) __attribute__((used)) static struct{ const void* replacment; const void* replacee; } _interpose_##_replacee __attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacment, (const void*)(unsigned long)&_replacee };
DYLD_INTERPOSE(DFH_SDL_Init,SDL_Init);
DYLD_INTERPOSE(DFH_SDL_PollEvent,SDL_PollEvent);
DYLD_INTERPOSE(DFH_SDL_Quit,SDL_Quit);
DYLD_INTERPOSE(DFH_SDL_NumJoysticks,SDL_NumJoysticks);
/*******************************************************************************
* SDL part starts here *
*******************************************************************************/
// hook - called for each game tick (or more often)
DFhackCExport int SDL_NumJoysticks(void)
DFhackCExport int DFH_SDL_NumJoysticks(void)
{
DFHack::Core & c = DFHack::Core::getInstance();
return c.Update();
@ -70,7 +77,7 @@ DFhackCExport int SDL_NumJoysticks(void)
// hook - called at program exit
static void (*_SDL_Quit)(void) = 0;
DFhackCExport void SDL_Quit(void)
DFhackCExport void DFH_SDL_Quit(void)
{
DFHack::Core & c = DFHack::Core::getInstance();
c.Shutdown();
@ -79,16 +86,16 @@ DFhackCExport void SDL_Quit(void)
_SDL_Quit();
}*/
_SDL_Quit();
SDL_Quit();
}
// called by DF to check input events
static int (*_SDL_PollEvent)(SDL::Event* event) = 0;
DFhackCExport int SDL_PollEvent(SDL::Event* event)
DFhackCExport int DFH_SDL_PollEvent(SDL::Event* event)
{
pollevent_again:
// if SDL returns 0 here, it means there are no more events. return 0
int orig_return = _SDL_PollEvent(event);
int orig_return = SDL_PollEvent(event);
if(!orig_return)
return 0;
// otherwise we have an event to filter
@ -128,7 +135,7 @@ DFhackCExport int wgetch(WINDOW *win)
// hook - called at program start, initialize some stuffs we'll use later
static int (*_SDL_Init)(uint32_t flags) = 0;
DFhackCExport int SDL_Init(uint32_t flags)
DFhackCExport int DFH_SDL_Init(uint32_t flags)
{
// reroute stderr
fprintf(stderr,"dfhack: attempting to hook in\n");
@ -158,6 +165,7 @@ DFhackCExport int SDL_Init(uint32_t flags)
DFHack::Core & c = DFHack::Core::getInstance();
//c.Init();
int ret = _SDL_Init(flags);
//int ret = _SDL_Init(flags);
int ret = SDL_Init(flags);
return ret;
}

@ -83,10 +83,17 @@ namespace DFHack
// Better than tracking some weird variables all over the place.
class DFHACK_EXPORT Core
{
#ifdef _DARWIN
friend int ::DFH_SDL_NumJoysticks(void);
friend void ::DFH_SDL_Quit(void);
friend int ::DFH_SDL_PollEvent(SDL::Event *);
friend int ::DFH_SDL_Init(uint32_t flags);
#else
friend int ::SDL_NumJoysticks(void);
friend void ::SDL_Quit(void);
friend int ::SDL_PollEvent(SDL::Event *);
friend int ::SDL_Init(uint32_t flags);
#endif
friend int ::wgetch(WINDOW * w);
friend int ::egg_init(void);
friend int ::egg_shutdown(void);

@ -44,6 +44,12 @@ namespace SDL
// these functions are here because they call into DFHack::Core and therefore need to
// be declared as friend functions/known
#ifdef _DARWIN
DFhackCExport int DFH_SDL_NumJoysticks(void);
DFhackCExport void DFH_SDL_Quit(void);
DFhackCExport int DFH_SDL_PollEvent(SDL::Event* event);
DFhackCExport int DFH_SDL_Init(uint32_t flags);
#endif
DFhackCExport int SDL_NumJoysticks(void);
DFhackCExport void SDL_Quit(void);
DFhackCExport int SDL_PollEvent(SDL::Event* event);