2012-05-25 12:28:59 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
2012-05-25 12:28:59 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
2012-06-29 08:15:48 -06:00
|
|
|
typedef struct interpose_s
|
2012-05-25 12:28:59 -06:00
|
|
|
{
|
|
|
|
void *new_func;
|
|
|
|
void *orig_func;
|
2012-06-29 08:15:48 -06:00
|
|
|
} interpose_t;
|
2012-05-25 12:28:59 -06:00
|
|
|
|
|
|
|
#include "DFHack.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "Hooks.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
2015-01-05 13:57:34 -07:00
|
|
|
/*static const interpose_t interposers[] __attribute__ ((section("__DATA, __interpose"))) =
|
2012-05-25 12:28:59 -06:00
|
|
|
{
|
|
|
|
{ (void *)DFH_SDL_Init, (void *)SDL_Init },
|
|
|
|
{ (void *)DFH_SDL_PollEvent, (void *)SDL_PollEvent },
|
|
|
|
{ (void *)DFH_SDL_Quit, (void *)SDL_Quit },
|
|
|
|
{ (void *)DFH_SDL_NumJoysticks, (void *)SDL_NumJoysticks },
|
2015-01-05 13:57:34 -07:00
|
|
|
|
2012-05-25 12:28:59 -06:00
|
|
|
};*/
|
|
|
|
|
2015-01-05 13:57:34 -07:00
|
|
|
#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 };
|
2012-06-29 08:15:48 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-05-25 12:28:59 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
* SDL part starts here *
|
|
|
|
*******************************************************************************/
|
|
|
|
// hook - called for each game tick (or more often)
|
2012-06-29 08:15:48 -06:00
|
|
|
DFhackCExport int DFH_SDL_NumJoysticks(void)
|
2012-05-25 12:28:59 -06:00
|
|
|
{
|
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
return c.Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// hook - called at program exit
|
|
|
|
static void (*_SDL_Quit)(void) = 0;
|
2012-06-29 08:15:48 -06:00
|
|
|
DFhackCExport void DFH_SDL_Quit(void)
|
2012-05-25 12:28:59 -06:00
|
|
|
{
|
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
c.Shutdown();
|
2015-01-05 13:57:34 -07:00
|
|
|
|
2012-06-29 08:15:48 -06:00
|
|
|
SDL_Quit();
|
2012-05-25 12:28:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// called by DF to check input events
|
2012-06-14 11:42:40 -06:00
|
|
|
static int (*_SDL_PollEvent)(SDL::Event* event) = 0;
|
2012-06-29 08:15:48 -06:00
|
|
|
DFhackCExport int DFH_SDL_PollEvent(SDL::Event* event)
|
2012-05-25 12:28:59 -06:00
|
|
|
{
|
|
|
|
pollevent_again:
|
|
|
|
// if SDL returns 0 here, it means there are no more events. return 0
|
2012-06-29 08:15:48 -06:00
|
|
|
int orig_return = SDL_PollEvent(event);
|
2012-05-25 12:28:59 -06:00
|
|
|
if(!orig_return)
|
|
|
|
return 0;
|
|
|
|
// otherwise we have an event to filter
|
|
|
|
else if( event != 0 )
|
|
|
|
{
|
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
// if we consume the event, ask SDL for more.
|
|
|
|
if(!c.DFH_SDL_Event(event))
|
|
|
|
goto pollevent_again;
|
|
|
|
}
|
|
|
|
return orig_return;
|
|
|
|
}
|
|
|
|
|
2015-02-21 12:39:31 -07:00
|
|
|
static int (*_SDL_PushEvent)(SDL::Event* event) = 0;
|
|
|
|
DFhackCExport int SDL_PushEvent(SDL::Event* event)
|
|
|
|
{
|
|
|
|
return _SDL_PushEvent(event);
|
|
|
|
}
|
|
|
|
|
2012-05-25 12:28:59 -06:00
|
|
|
struct WINDOW;
|
|
|
|
DFhackCExport int wgetch(WINDOW *win)
|
|
|
|
{
|
|
|
|
static int (*_wgetch)(WINDOW * win) = (int (*)( WINDOW * )) dlsym(RTLD_NEXT, "wgetch");
|
|
|
|
if(!_wgetch)
|
|
|
|
{
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
wgetch_again:
|
|
|
|
int in = _wgetch(win);
|
|
|
|
int out;
|
|
|
|
if(c.ncurses_wgetch(in, out))
|
|
|
|
{
|
|
|
|
// not consumed, give to DF
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// consumed, repeat
|
|
|
|
goto wgetch_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 12:12:25 -07:00
|
|
|
void dlsym_bind_or_exit(void **target, const char *name)
|
2012-05-25 12:28:59 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
void *sym = dlsym(RTLD_NEXT, name);
|
|
|
|
if (sym)
|
2012-05-25 12:28:59 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
if (*target && *target != sym)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "warning: rebinding symbol %s from %p to %p\n",
|
|
|
|
name, *target, sym);
|
|
|
|
}
|
|
|
|
*target = sym;
|
2012-05-25 12:28:59 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
fprintf(stderr, "Fatal: Could not find symbol: %s\n", name);
|
|
|
|
fprintf(stdout, "dfhack: something went horribly wrong\n"
|
|
|
|
"Check stderr.log for details\n");
|
2012-05-25 12:28:59 -06:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
2015-02-21 12:12:25 -07:00
|
|
|
|
2014-09-16 19:16:52 -06:00
|
|
|
// New SDL functions starting in r5
|
2015-02-21 12:12:25 -07:00
|
|
|
static vPtr (*_SDL_CreateRGBSurface)(uint32_t flags, int width, int height, int depth,
|
2014-09-16 19:16:52 -06:00
|
|
|
uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport vPtr SDL_CreateRGBSurface(uint32_t flags, int width, int height, int depth,
|
2014-09-16 19:16:52 -06:00
|
|
|
uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask)
|
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
return _SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static vPtr (*_SDL_CreateRGBSurfaceFrom)(vPtr pixels, int width, int height, int depth, int pitch,
|
|
|
|
uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport vPtr SDL_CreateRGBSurfaceFrom(vPtr pixels, int width, int height, int depth, int pitch,
|
2014-09-16 19:16:52 -06:00
|
|
|
uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask)
|
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
return _SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void (*_SDL_FreeSurface)(vPtr surface) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport void SDL_FreeSurface(vPtr surface)
|
2014-09-16 19:16:52 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
_SDL_FreeSurface(surface);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static vPtr (*_SDL_ConvertSurface)(vPtr surface, vPtr format, uint32_t flags) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport vPtr SDL_ConvertSurface(vPtr surface, vPtr format, uint32_t flags)
|
2014-09-16 19:16:52 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
return _SDL_ConvertSurface(surface, format, flags);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int (*_SDL_LockSurface)(vPtr surface) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport int SDL_LockSurface(vPtr surface)
|
2014-09-16 19:16:52 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
return _SDL_LockSurface(surface);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void (*_SDL_UnlockSurface)(vPtr surface) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport void SDL_UnlockSurface(vPtr surface)
|
2014-09-16 19:16:52 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
_SDL_UnlockSurface(surface);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t (*_SDL_GetMouseState)(int *, int *) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport uint8_t SDL_GetMouseState(int *x, int *y)
|
2014-09-16 19:16:52 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
return _SDL_GetMouseState(x,y);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void * (*_SDL_GetVideoSurface)( void ) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport void * SDL_GetVideoSurface(void)
|
2014-09-16 19:16:52 -06:00
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
return _SDL_GetVideoSurface();
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int (*_SDL_UpperBlit)(DFHack::DFSDL_Surface* src, DFHack::DFSDL_Rect* srcrect, DFHack::DFSDL_Surface* dst, DFHack::DFSDL_Rect* dstrect) = 0;
|
2015-02-21 12:12:25 -07:00
|
|
|
DFhackCExport int SDL_UpperBlit(DFHack::DFSDL_Surface* src, DFHack::DFSDL_Rect* srcrect, DFHack::DFSDL_Surface* dst, DFHack::DFSDL_Rect* dstrect)
|
2014-09-16 19:16:52 -06:00
|
|
|
{
|
|
|
|
if ( dstrect != NULL && dstrect->h != 0 && dstrect->w != 0 )
|
|
|
|
{
|
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
DFHack::Graphic* g = c.getGraphic();
|
|
|
|
DFHack::DFTileSurface* ov = g->Call(dstrect->x/dstrect->w, dstrect->y/dstrect->h);
|
|
|
|
|
|
|
|
if ( ov != NULL )
|
|
|
|
{
|
|
|
|
if ( ov->paintOver )
|
|
|
|
{
|
2015-02-21 12:12:25 -07:00
|
|
|
_SDL_UpperBlit(src, srcrect, dst, dstrect);
|
2014-09-16 19:16:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
DFHack::DFSDL_Rect* dstrect2 = new DFHack::DFSDL_Rect;
|
|
|
|
dstrect2->x = dstrect->x;
|
|
|
|
dstrect2->y = dstrect->y;
|
|
|
|
dstrect2->w = dstrect->w;
|
|
|
|
dstrect2->h = dstrect->h;
|
|
|
|
|
|
|
|
if ( ov->dstResize != NULL )
|
|
|
|
{
|
|
|
|
DFHack::DFSDL_Rect* r = (DFHack::DFSDL_Rect*)ov->dstResize;
|
|
|
|
dstrect2->x += r->x;
|
|
|
|
dstrect2->y += r->y;
|
|
|
|
dstrect2->w += r->w;
|
|
|
|
dstrect2->h += r->h;
|
|
|
|
}
|
|
|
|
|
2015-02-21 12:12:25 -07:00
|
|
|
int result = _SDL_UpperBlit(ov->surface, ov->rect, dst, dstrect2);
|
2014-09-16 19:16:52 -06:00
|
|
|
delete dstrect2;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 12:12:25 -07:00
|
|
|
return _SDL_UpperBlit(src, srcrect, dst, dstrect);
|
|
|
|
}
|
|
|
|
|
2015-03-25 14:08:25 -06:00
|
|
|
static int (*_SDL_SemWait)(vPtr) = 0;
|
|
|
|
DFhackCExport int SDL_SemWait(vPtr sem)
|
|
|
|
{
|
|
|
|
return _SDL_SemWait(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int (*_SDL_SemPost)(vPtr) = 0;
|
|
|
|
DFhackCExport int SDL_SemPost(vPtr sem)
|
|
|
|
{
|
|
|
|
return _SDL_SemPost(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-21 12:12:25 -07:00
|
|
|
// hook - called at program start, initialize some stuffs we'll use later
|
|
|
|
static int (*_SDL_Init)(uint32_t flags) = 0;
|
|
|
|
DFhackCExport int DFH_SDL_Init(uint32_t flags)
|
|
|
|
{
|
|
|
|
// reroute stderr
|
|
|
|
fprintf(stderr,"dfhack: attempting to hook in\n");
|
|
|
|
freopen("stderr.log", "w", stderr);
|
|
|
|
// we don't reroute stdout until we figure out if this should be done at all
|
|
|
|
// See: Console-linux.cpp
|
|
|
|
|
|
|
|
// find real functions
|
|
|
|
fprintf(stderr,"dfhack: saving real SDL functions\n");
|
|
|
|
|
|
|
|
#define bind(sym) dlsym_bind_or_exit((void**)&_##sym, #sym)
|
|
|
|
bind(SDL_Init);
|
|
|
|
bind(SDL_Quit);
|
|
|
|
bind(SDL_PollEvent);
|
2015-02-21 12:39:31 -07:00
|
|
|
bind(SDL_PushEvent);
|
2015-02-21 12:12:25 -07:00
|
|
|
|
|
|
|
bind(SDL_UpperBlit);
|
|
|
|
bind(SDL_CreateRGBSurface);
|
|
|
|
bind(SDL_CreateRGBSurfaceFrom);
|
|
|
|
bind(SDL_FreeSurface);
|
|
|
|
bind(SDL_ConvertSurface);
|
|
|
|
bind(SDL_LockSurface);
|
|
|
|
bind(SDL_UnlockSurface);
|
|
|
|
bind(SDL_GetMouseState);
|
|
|
|
bind(SDL_GetVideoSurface);
|
2015-03-25 14:08:25 -06:00
|
|
|
|
|
|
|
bind(SDL_SemWait);
|
|
|
|
bind(SDL_SemPost);
|
2015-02-21 12:12:25 -07:00
|
|
|
#undef bind
|
|
|
|
|
|
|
|
fprintf(stderr, "dfhack: saved real SDL functions\n");
|
|
|
|
assert(_SDL_Init && _SDL_Quit && _SDL_PollEvent);
|
|
|
|
fprintf(stderr, "dfhack: hooking successful\n");
|
|
|
|
|
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
|
|
|
|
int ret = SDL_Init(flags);
|
|
|
|
return ret;
|
2015-01-05 13:57:34 -07:00
|
|
|
}
|