2011-06-12 15:17:40 -06:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
2011-06-12 15:17:40 -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.
|
|
|
|
*/
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
#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>
|
2011-06-14 08:13:28 -06:00
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
#include "DFHack.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
|
|
|
#include "Hooks.h"
|
2011-06-12 15:17:40 -06:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* SDL part starts here *
|
|
|
|
*******************************************************************************/
|
2011-07-26 21:59:09 -06:00
|
|
|
// hook - called for each game tick (or more often)
|
2012-05-29 12:44:37 -06:00
|
|
|
DFhackCExport int SDL_NumJoysticks(void)
|
2011-06-12 15:17:40 -06:00
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
2011-07-26 21:59:09 -06:00
|
|
|
return c.Update();
|
2011-06-16 15:53:39 -06:00
|
|
|
}
|
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
// hook - called at program exit
|
2011-07-26 21:59:09 -06:00
|
|
|
static void (*_SDL_Quit)(void) = 0;
|
2012-05-29 12:44:37 -06:00
|
|
|
DFhackCExport void SDL_Quit(void)
|
2011-06-12 15:17:40 -06:00
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
c.Shutdown();
|
2012-05-29 12:44:37 -06:00
|
|
|
if(_SDL_Quit)
|
2011-06-12 15:17:40 -06:00
|
|
|
{
|
|
|
|
_SDL_Quit();
|
2012-05-29 12:44:37 -06:00
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:55:37 -06:00
|
|
|
// called by DF to check input events
|
2012-05-29 12:44:37 -06:00
|
|
|
static int (*_SDL_PollEvent)(SDL::Event* event) = 0;
|
|
|
|
DFhackCExport int SDL_PollEvent(SDL::Event* event)
|
2011-07-07 19:55:37 -06:00
|
|
|
{
|
2012-02-28 04:59:02 -07:00
|
|
|
pollevent_again:
|
|
|
|
// if SDL returns 0 here, it means there are no more events. return 0
|
2012-05-29 12:44:37 -06:00
|
|
|
int orig_return = _SDL_PollEvent(event);
|
2012-02-28 04:59:02 -07:00
|
|
|
if(!orig_return)
|
|
|
|
return 0;
|
|
|
|
// otherwise we have an event to filter
|
|
|
|
else if( event != 0 )
|
2011-07-07 19:55:37 -06:00
|
|
|
{
|
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
2012-02-28 04:59:02 -07:00
|
|
|
// if we consume the event, ask SDL for more.
|
2012-05-29 12:44:37 -06:00
|
|
|
if(!c.SDL_Event(event))
|
2012-02-28 04:59:02 -07:00
|
|
|
goto pollevent_again;
|
2011-07-07 19:55:37 -06:00
|
|
|
}
|
2011-07-09 03:33:58 -06:00
|
|
|
return orig_return;
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:40:23 -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
// hook - called at program start, initialize some stuffs we'll use later
|
2011-07-26 21:59:09 -06:00
|
|
|
static int (*_SDL_Init)(uint32_t flags) = 0;
|
2012-05-29 12:44:37 -06:00
|
|
|
DFhackCExport int SDL_Init(uint32_t flags)
|
2011-06-12 15:17:40 -06:00
|
|
|
{
|
2011-11-04 02:08:29 -06:00
|
|
|
// reroute stderr
|
|
|
|
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
|
|
|
|
|
2012-05-29 12:44:37 -06:00
|
|
|
// find real functions
|
|
|
|
_SDL_Init = (int (*)( uint32_t )) dlsym(RTLD_NEXT, "SDL_Init");
|
|
|
|
_SDL_Quit = (void (*)( void )) dlsym(RTLD_NEXT, "SDL_Quit");
|
|
|
|
_SDL_PollEvent = (int (*)(SDL::Event*))dlsym(RTLD_NEXT,"SDL_PollEvent");
|
2011-06-12 15:17:40 -06:00
|
|
|
|
|
|
|
// check if we got them
|
2012-05-29 12:44:37 -06:00
|
|
|
if(_SDL_Init && _SDL_Quit && _SDL_PollEvent)
|
2011-06-12 15:17:40 -06:00
|
|
|
{
|
|
|
|
fprintf(stderr,"dfhack: hooking successful\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// bail, this would be a disaster otherwise
|
|
|
|
fprintf(stderr,"dfhack: something went horribly wrong\n");
|
|
|
|
exit(1);
|
2012-05-29 12:44:37 -06:00
|
|
|
}
|
2011-07-31 19:31:52 -06:00
|
|
|
/*
|
2011-07-26 21:59:09 -06:00
|
|
|
DFHack::Core & c = DFHack::Core::getInstance();
|
|
|
|
c.Init();
|
2011-07-31 19:31:52 -06:00
|
|
|
*/
|
2012-05-29 12:44:37 -06:00
|
|
|
int ret = _SDL_Init(flags);
|
2011-07-07 19:55:37 -06:00
|
|
|
return ret;
|
2012-05-29 12:44:37 -06:00
|
|
|
}
|