2012-01-13 00:32:27 +01:00
|
|
|
/* Copyright (c) 2012 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
|
|
|
/*
|
2011-04-30 02:06:30 +02:00
|
|
|
* SDL input redirector; dispatches to multiple handlers.
|
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>
|
|
|
|
|
2012-01-13 00:32:27 +01:00
|
|
|
#include "lib/external_libraries/libsdl.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
|
|
|
|
Improve correctness of hotkeys at low framerates.
SDL queues up all the input events received in a frame. When the hotkey
system saw a key up/down event, it immediately updated its
HotkeyIsPressed state and then pushed a hotkey event onto the end of the
queue.
If the initial queue was e.g. [key-down shift, key-press Z, key-up
shift], the hotkey event triggered by Z would be processed after the
key-up shift had updated the HotkeyIsPressed state, so the handler of
the Z hotkey would not think the shift hotkey was pressed.
If the initial queue was e.g. [key-press Z, mouse-click], the hotkey
triggered by Z would be processed after the mouse-click event, so it
could apply to the wrong building selection.
Fix by pushing the hotkey events onto a special queue that gets
processed before any subsequent SDL input events.
Also update the HotkeyIsPressed status when the HOTKEYDOWN/HOTKEYUP
events are processed, not when they are generated, to guarantee they are
consistent with the DOWN/UP events.
Fixes #1869.
This was SVN commit r14057.
2013-10-30 02:38:32 +01:00
|
|
|
static std::list<SDL_Event_> priority_events;
|
|
|
|
|
2005-10-20 17:27:39 +02:00
|
|
|
void in_add_handler(InHandler handler)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(handler);
|
2005-10-19 05:06:54 +02:00
|
|
|
|
|
|
|
if(handler_stack_top >= MAX_HANDLERS)
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_IF_ERR(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
|
2011-04-30 02:06:30 +02:00
|
|
|
void in_dispatch_event(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
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(handler_stack[i] && ev);
|
2006-08-26 23:52:18 +02:00
|
|
|
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
|
2011-05-04 14:10:17 +02:00
|
|
|
DEBUG_WARN_ERR(ERR::LOGIC); // invalid handler return value
|
2004-06-24 16:05:39 +02:00
|
|
|
}
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
Improve correctness of hotkeys at low framerates.
SDL queues up all the input events received in a frame. When the hotkey
system saw a key up/down event, it immediately updated its
HotkeyIsPressed state and then pushed a hotkey event onto the end of the
queue.
If the initial queue was e.g. [key-down shift, key-press Z, key-up
shift], the hotkey event triggered by Z would be processed after the
key-up shift had updated the HotkeyIsPressed state, so the handler of
the Z hotkey would not think the shift hotkey was pressed.
If the initial queue was e.g. [key-press Z, mouse-click], the hotkey
triggered by Z would be processed after the mouse-click event, so it
could apply to the wrong building selection.
Fix by pushing the hotkey events onto a special queue that gets
processed before any subsequent SDL input events.
Also update the HotkeyIsPressed status when the HOTKEYDOWN/HOTKEYUP
events are processed, not when they are generated, to guarantee they are
consistent with the DOWN/UP events.
Fixes #1869.
This was SVN commit r14057.
2013-10-30 02:38:32 +01:00
|
|
|
|
|
|
|
void in_push_priority_event(const SDL_Event_* event)
|
|
|
|
{
|
|
|
|
priority_events.push_back(*event);
|
|
|
|
}
|
|
|
|
|
|
|
|
int in_poll_event(SDL_Event_* event)
|
|
|
|
{
|
|
|
|
if (!priority_events.empty())
|
|
|
|
{
|
|
|
|
*event = priority_events.front();
|
|
|
|
priority_events.pop_front();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SDL_PollEvent(&event->ev);
|
|
|
|
}
|