2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* SDL input redirector; dispatches to multiple handlers and allows
|
|
|
|
* record/playback of evs.
|
2006-04-12 01:59:08 +02:00
|
|
|
*/
|
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "input.h"
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2004-06-04 14:41:53 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
#include "lib/external_libraries/sdl.h"
|
2003-11-03 17:22:45 +01:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
const size_t MAX_HANDLERS = 8;
|
2005-10-20 17:27:39 +02:00
|
|
|
static InHandler handler_stack[MAX_HANDLERS];
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
static size_t handler_stack_top = 0;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2005-10-20 17:27:39 +02:00
|
|
|
void in_add_handler(InHandler handler)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
2005-10-19 05:06:54 +02:00
|
|
|
debug_assert(handler);
|
|
|
|
|
|
|
|
if(handler_stack_top >= MAX_HANDLERS)
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_ERR_RETURN(ERR::LIMIT);
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
handler_stack[handler_stack_top++] = handler;
|
|
|
|
}
|
|
|
|
|
2010-07-07 23:47:31 +02:00
|
|
|
void in_reset_handlers()
|
|
|
|
{
|
|
|
|
handler_stack_top = 0;
|
|
|
|
}
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
// send ev to each handler until one returns IN_HANDLED
|
|
|
|
static void dispatch_ev(const SDL_Event_* ev)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
2005-10-20 17:27:39 +02:00
|
|
|
for(int i = (int)handler_stack_top-1; i >= 0; i--)
|
2004-06-24 16:05:39 +02:00
|
|
|
{
|
2006-08-26 23:52:18 +02:00
|
|
|
debug_assert(handler_stack[i] && ev);
|
|
|
|
InReaction ret = handler_stack[i](ev);
|
2004-06-24 16:05:39 +02:00
|
|
|
// .. done, return
|
2005-10-20 19:44:56 +02:00
|
|
|
if(ret == IN_HANDLED)
|
2003-11-03 17:22:45 +01:00
|
|
|
return;
|
2004-06-24 16:05:39 +02:00
|
|
|
// .. next handler
|
2005-10-20 19:44:56 +02:00
|
|
|
else if(ret == IN_PASS)
|
2004-06-24 16:05:39 +02:00
|
|
|
continue;
|
|
|
|
// .. invalid return value
|
|
|
|
else
|
2007-12-20 21:09:19 +01:00
|
|
|
debug_assert(0); // invalid handler return value
|
2004-06-24 16:05:39 +02:00
|
|
|
}
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-20 17:27:39 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
static enum
|
|
|
|
{
|
2005-01-23 18:54:20 +01:00
|
|
|
INIT, // first call to in_record() or in_playback(): register cleanup routine
|
2003-11-03 17:22:45 +01:00
|
|
|
IDLE,
|
|
|
|
RECORD,
|
|
|
|
PLAYBACK
|
|
|
|
}
|
|
|
|
state = INIT;
|
|
|
|
|
|
|
|
static FILE* f;
|
|
|
|
|
2005-01-07 01:52:05 +01:00
|
|
|
u32 game_ticks;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
static u32 time_adjust = 0;
|
2006-08-26 23:52:18 +02:00
|
|
|
static u32 next_ev_time;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
void in_stop()
|
|
|
|
{
|
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
f = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
LibError in_record(const char* fn)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
|
|
|
if(state == INIT)
|
|
|
|
atexit(in_stop);
|
|
|
|
|
|
|
|
in_stop();
|
|
|
|
|
|
|
|
f = fopen(fn, "wb");
|
|
|
|
if(!f)
|
2007-11-10 18:47:25 +01:00
|
|
|
WARN_RETURN(ERR::FAIL);
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
fwrite(&game_ticks, sizeof(u32), 1, f);
|
|
|
|
|
|
|
|
state = RECORD;
|
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
LibError in_playback(const char* fn)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
|
|
|
if(state == INIT)
|
|
|
|
atexit(in_stop);
|
|
|
|
|
|
|
|
in_stop();
|
|
|
|
|
|
|
|
f = fopen(fn, "rb");
|
|
|
|
if(!f)
|
2007-11-10 18:47:25 +01:00
|
|
|
WARN_RETURN(ERR::FAIL);
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
u32 rec_start_time;
|
|
|
|
fread(&rec_start_time, sizeof(u32), 1, f);
|
|
|
|
time_adjust = game_ticks-rec_start_time;
|
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
fread(&next_ev_time, sizeof(u32), 1, f);
|
|
|
|
next_ev_time += time_adjust;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
state = PLAYBACK;
|
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
void in_dispatch_event(const SDL_Event_* ev)
|
2005-08-15 01:34:37 +02:00
|
|
|
{
|
|
|
|
if(state == RECORD)
|
|
|
|
{
|
|
|
|
fwrite(&game_ticks, sizeof(u32), 1, f);
|
2006-08-26 23:52:18 +02:00
|
|
|
fwrite(ev, sizeof(SDL_Event_), 1, f);
|
2005-08-15 01:34:37 +02:00
|
|
|
}
|
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
dispatch_ev(ev);
|
2005-08-15 01:34:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void in_dispatch_recorded_events()
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
2006-08-26 23:52:18 +02:00
|
|
|
SDL_Event_ ev;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
while(state == PLAYBACK && next_ev_time <= game_ticks)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
2006-08-26 23:52:18 +02:00
|
|
|
fread(&ev, sizeof(SDL_Event_), 1, f);
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
// do this before dispatch_ev(),
|
2005-01-23 18:54:20 +01:00
|
|
|
// in case a handler calls in_stop() (setting f to 0)
|
2006-08-26 23:52:18 +02:00
|
|
|
if(!fread(&next_ev_time, sizeof(u32), 1, f))
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
|
|
|
in_stop();
|
|
|
|
exit(0x73c07d);
|
|
|
|
// TODO: 'disconnect'?
|
|
|
|
}
|
2006-08-26 23:52:18 +02:00
|
|
|
next_ev_time += time_adjust;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
in_dispatch_event(&ev);
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
}
|