Replaced plain UIEvent[] arrays with UIEventSequence, also send out events on release in reverse order

This commit is contained in:
Ingo Ruhnke 2011-07-12 01:37:18 +02:00
parent 2d1154a171
commit 8dd58e31a4
8 changed files with 83 additions and 149 deletions

View file

@ -39,12 +39,7 @@ KeyAxisEventHandler::from_string(const std::string& str)
{
case 0:
{
int k = 0;
tokenizer ev_tokens(*i, boost::char_separator<char>("+", "", boost::keep_empty_tokens));
for(tokenizer::iterator m = ev_tokens.begin(); m != ev_tokens.end(); ++m, ++k)
{
ev->m_up_codes[k] = str2key_event(*m);
}
ev->m_up_codes = UIEventSequence::from_string(*i);
}
break;
@ -54,21 +49,12 @@ KeyAxisEventHandler::from_string(const std::string& str)
{
// bit of hackery to handle simplified syntax for trigger button that don't need up/down events
ev->m_threshold = boost::lexical_cast<int>(*i);
for(int k = 0; ev->m_up_codes[k].is_valid(); ++k)
{
ev->m_down_codes[k] = ev->m_up_codes[k];
ev->m_up_codes[k] = UIEvent::invalid();
}
ev->m_down_codes = ev->m_up_codes;
ev->m_up_codes.clear();
}
else
{
tokenizer ev_tokens(*i, boost::char_separator<char>("+", "", boost::keep_empty_tokens));
int k = 0;
for(tokenizer::iterator m = ev_tokens.begin(); m != ev_tokens.end(); ++m, ++k)
{
ev->m_down_codes[k] = str2key_event(*m);
}
ev->m_down_codes = UIEventSequence::from_string(*i);
}
}
break;
@ -96,38 +82,25 @@ KeyAxisEventHandler::KeyAxisEventHandler() :
m_down_codes(),
m_threshold(8000) // FIXME: this doesn't work for triggers
{
std::fill_n(m_up_codes, MAX_MODIFIER+1, UIEvent::invalid());
std::fill_n(m_down_codes, MAX_MODIFIER+1, UIEvent::invalid());
}
void
KeyAxisEventHandler::init(UInput& uinput, int slot, bool extra_devices)
{
for(int i = 0; m_up_codes[i].is_valid(); ++i)
{
m_up_codes[i].resolve_device_id(slot, extra_devices);
uinput.add_key(m_up_codes[i].get_device_id(), m_up_codes[i].code);
}
for(int i = 0; m_down_codes[i].is_valid(); ++i)
{
m_down_codes[i].resolve_device_id(slot, extra_devices);
uinput.add_key(m_down_codes[i].get_device_id(), m_down_codes[i].code);
}
m_up_codes.init(uinput, slot, extra_devices);
m_down_codes.init(uinput, slot, extra_devices);
}
void
KeyAxisEventHandler::send_up(UInput& uinput, int value)
{
for(int i = 0; m_up_codes[i].is_valid(); ++i)
uinput.send_key(m_up_codes[i].get_device_id(), m_up_codes[i].code, value);
m_up_codes.send(uinput, value);
}
void
KeyAxisEventHandler::send_down(UInput& uinput, int value)
{
for(int i = 0; m_down_codes[i].is_valid(); ++i)
uinput.send_key(m_down_codes[i].get_device_id(), m_down_codes[i].code, value);
m_down_codes.send(uinput, value);
}
int
@ -188,28 +161,7 @@ std::string
KeyAxisEventHandler::str() const
{
std::ostringstream out;
for(int i = 0; m_up_codes[i].is_valid();)
{
out << m_up_codes[i].get_device_id() << "-" << m_up_codes[i].code;
++i;
if (m_up_codes[i].is_valid())
out << "+";
}
out << ":";
for(int i = 0; m_down_codes[i].is_valid();)
{
out << m_down_codes[i].get_device_id() << "-" << m_down_codes[i].code;
++i;
if (m_down_codes[i].is_valid())
out << "+";
}
out << ":" << m_threshold;
out << m_up_codes.str() << ":" << m_down_codes.str() << ":" << m_threshold;
return out.str();
}

View file

@ -21,6 +21,8 @@
#include "axis_event.hpp"
#include "ui_event_sequence.hpp"
class KeyAxisEventHandler : public AxisEventHandler
{
public:
@ -41,13 +43,11 @@ private:
int get_zone(int value) const;
private:
static const int MAX_MODIFIER = 4;
int m_old_value;
// Array is terminated by -1
UIEvent m_up_codes[MAX_MODIFIER+1];
UIEvent m_down_codes[MAX_MODIFIER+1];
UIEventSequence m_up_codes;
UIEventSequence m_down_codes;
int m_threshold;
};

View file

@ -41,27 +41,13 @@ KeyButtonEventHandler::from_string(const std::string& str)
case 0:
{
ev.reset(new KeyButtonEventHandler());
boost::char_separator<char> plus_sep("+", "", boost::keep_empty_tokens);
tokenizer ev_tokens(*i, plus_sep);
int k = 0;
for(tokenizer::iterator m = ev_tokens.begin(); m != ev_tokens.end() && k < MAX_MODIFIER; ++m, ++k)
{
ev->m_codes[k] = str2key_event(*m);
}
ev->m_codes = UIEventSequence::from_string(*i);
}
break;
case 1:
{
boost::char_separator<char> plus_sep("+", "", boost::keep_empty_tokens);
tokenizer ev_tokens(*i, plus_sep);
int k = 0;
for(tokenizer::iterator m = ev_tokens.begin(); m != ev_tokens.end() && k < MAX_MODIFIER; ++m, ++k)
{
ev->m_secondary_codes[k] = str2key_event(*m);
}
ev->m_secondary_codes = UIEventSequence::from_string(*i);
ev->m_hold_threshold = 250;
}
break;
@ -92,38 +78,25 @@ KeyButtonEventHandler::KeyButtonEventHandler() :
m_hold_threshold(0),
m_hold_counter(0)
{
std::fill_n(m_codes, MAX_MODIFIER + 1, UIEvent::invalid());
std::fill_n(m_secondary_codes, MAX_MODIFIER + 1, UIEvent::invalid());
}
KeyButtonEventHandler::KeyButtonEventHandler(int device_id, int code) :
m_state(false),
m_codes(),
m_codes(UIEvent::create(device_id, EV_KEY, code)),
m_secondary_codes(),
m_hold_threshold(0),
m_hold_counter(0)
{
std::fill_n(m_codes, MAX_MODIFIER + 1, UIEvent::invalid());
std::fill_n(m_secondary_codes, MAX_MODIFIER + 1, UIEvent::invalid());
m_codes[0] = UIEvent::create(device_id, EV_KEY, code);
}
void
KeyButtonEventHandler::init(UInput& uinput, int slot, bool extra_devices)
{
for(int i = 0; m_codes[i].is_valid(); ++i)
{
m_codes[i].resolve_device_id(slot, extra_devices);
uinput.add_key(m_codes[i].get_device_id(), m_codes[i].code);
}
m_codes.init(uinput, slot, extra_devices);
if (m_hold_threshold)
{
for(int i = 0; m_secondary_codes[i].is_valid(); ++i)
{
m_secondary_codes[i].resolve_device_id(slot, extra_devices);
uinput.add_key(m_secondary_codes[i].get_device_id(), m_secondary_codes[i].code);
}
m_secondary_codes.init(uinput, slot, extra_devices);
}
}
@ -136,11 +109,7 @@ KeyButtonEventHandler::send(UInput& uinput, bool value)
if (m_hold_threshold == 0)
{
// FIXME: should handle key releases in reverse order
for(int i = 0; m_codes[i].is_valid(); ++i)
{
uinput.send_key(m_codes[i].get_device_id(), m_codes[i].code, m_state);
}
m_codes.send(uinput, m_state);
}
else
{
@ -154,15 +123,8 @@ KeyButtonEventHandler::send(UInput& uinput, bool value)
else
{
// send both a press and release event after another, aka a "click"
for(int i = 0; m_codes[i].is_valid(); ++i)
{
uinput.send_key(m_codes[i].get_device_id(), m_codes[i].code, true);
}
// FIXME: should do this in reverse order
for(int i = 0; m_codes[i].is_valid(); ++i)
{
uinput.send_key(m_codes[i].get_device_id(), m_codes[i].code, false);
}
m_codes.send(uinput, true);
m_codes.send(uinput, false);
}
}
else
@ -173,11 +135,7 @@ KeyButtonEventHandler::send(UInput& uinput, bool value)
}
else
{
// FIXME: should do in reverse
for(int i = 0; m_secondary_codes[i].is_valid(); ++i)
{
uinput.send_key(m_secondary_codes[i].get_device_id(), m_secondary_codes[i].code, false);
}
m_secondary_codes.send(uinput, false);
}
}
@ -198,10 +156,7 @@ KeyButtonEventHandler::update(UInput& uinput, int msec_delta)
m_hold_counter + msec_delta >= m_hold_threshold)
{
// start sending the secondary events
for(int i = 0; m_secondary_codes[i].is_valid(); ++i)
{
uinput.send_key(m_secondary_codes[i].get_device_id(), m_secondary_codes[i].code, true);
}
m_secondary_codes.send(uinput, true);
uinput.sync();
}
@ -216,14 +171,7 @@ std::string
KeyButtonEventHandler::str() const
{
std::ostringstream out;
for(int i = 0; m_codes[i].is_valid();)
{
out << m_codes[i].get_device_id() << "-" << m_codes[i].code;
++i;
if (m_codes[i].is_valid())
out << "+";
}
out << m_codes.str() << ":" << m_secondary_codes.str() << ":" << m_hold_threshold;
return out.str();
}

View file

@ -21,6 +21,8 @@
#include "button_event.hpp"
#include "ui_event_sequence.hpp"
class KeyButtonEventHandler : public ButtonEventHandler
{
public:
@ -37,12 +39,9 @@ public:
std::string str() const;
private:
static const int MAX_MODIFIER = 4;
bool m_state;
// Array is terminated by !is_valid()
UIEvent m_codes[MAX_MODIFIER+1];
UIEvent m_secondary_codes[MAX_MODIFIER+1];
UIEventSequence m_codes;
UIEventSequence m_secondary_codes;
int m_hold_threshold;
int m_hold_counter;
};

View file

@ -69,15 +69,6 @@ UIEvent::invalid()
return ev;
}
bool
UIEvent::is_valid() const
{
return
m_device_id != DEVICEID_INVALID &&
type != -1 &&
code != -1;
}
bool
UIEvent::operator<(const UIEvent& rhs) const
{

View file

@ -48,7 +48,6 @@ public:
public:
void resolve_device_id(int slot, bool extra_devices);
bool is_valid() const;
bool operator<(const UIEvent& rhs) const;
int type;

View file

@ -40,11 +40,21 @@ UIEventSequence::from_string(const std::string& value)
return sequence;
}
UIEventSequence::UIEventSequence() :
m_sequence()
{
}
UIEventSequence::UIEventSequence(const UIEvents& sequence) :
m_sequence(sequence)
{
}
UIEventSequence::UIEventSequence(const UIEvent& event) :
m_sequence(1, event)
{
}
void
UIEventSequence::init(UInput& uinput, int slot, bool extra_devices)
{
@ -58,19 +68,43 @@ UIEventSequence::init(UInput& uinput, int slot, bool extra_devices)
void
UIEventSequence::send(UInput& uinput, int value)
{
for(UIEvents::iterator i = m_sequence.begin(); i != m_sequence.end(); ++i)
{
uinput.send_key(i->get_device_id(), i->code, value);
if (value)
{
for(UIEvents::iterator i = m_sequence.begin(); i != m_sequence.end(); ++i)
{
uinput.send_key(i->get_device_id(), i->code, value);
}
}
else
{
// on release, send events in reverse order
for(UIEvents::reverse_iterator i = m_sequence.rbegin(); i != m_sequence.rend(); ++i)
{
uinput.send_key(i->get_device_id(), i->code, value);
}
}
}
void
UIEventSequence::send_reverse(UInput& uinput, int value)
UIEventSequence::clear()
{
for(UIEvents::reverse_iterator i = m_sequence.rbegin(); i != m_sequence.rend(); ++i)
{
uinput.send_key(i->get_device_id(), i->code, value);
}
m_sequence.clear();
}
std::string
UIEventSequence::str() const
{
std::ostringstream out;
for(UIEvents::const_iterator i = m_sequence.begin(); i != m_sequence.end(); ++i)
{
out << i->get_device_id() << "-" << i->code;
if (i != m_sequence.end()-1)
out << "+";
}
return out.str();
}
/* EOF */

View file

@ -25,9 +25,17 @@
class UInput;
/**
A sequence of UIEvents (only key events allowed right now)
FIXME: class name is kind of wrong
*/
class UIEventSequence
{
public:
/**
"KEY_LEFTSHIFT+KEY_B"
*/
static UIEventSequence from_string(const std::string& value);
private:
@ -35,13 +43,16 @@ private:
UIEvents m_sequence;
public:
UIEventSequence();
UIEventSequence(const UIEvents& sequence);
UIEventSequence(const UIEvent& event);
void init(UInput& uinput, int slot, bool extra_devices);
void send(UInput& uinput, int value);
/** send the event sequence in reverse order */
void send_reverse(UInput& uinput, int value);
void clear();
std::string str() const;
};
#endif