Skip to content

Commit

Permalink
Fix InputMapper issue jahnf#144
Browse files Browse the repository at this point in the history
In case, when two different input sequence of same length and same end
KeyEvent are defined then InputMapper fails to map to correct action.

This commit fixes the issue with InputMapper. The correct action is
decided on the basis of all keys pressed (instead of last key pressed
in earlier version of code).
  • Loading branch information
mayanksuman committed Aug 9, 2021
1 parent 38f91f0 commit d2d6214
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/deviceinput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ struct InputMapper::Impl

std::pair<DeviceKeyMap::Result, const RefPair*> m_lastState;
std::vector<input_event> m_events;
KeyEventSequence m_keyEventSeq;
InputMapConfig m_config;
bool m_recordingMode = false;
};
Expand Down Expand Up @@ -561,7 +562,8 @@ void InputMapper::Impl::sequenceTimeout()
// other sequences could have been possible.
if (m_lastState.second->second)
{
execAction(m_lastState.second->second->action, DeviceKeyMap::Result::PartialHit);
auto action = m_parent->getAction(m_keyEventSeq);
if (action) execAction(action, DeviceKeyMap::Result::PartialHit);
}
else if (m_vdev && m_events.size())
{
Expand All @@ -577,6 +579,7 @@ void InputMapper::Impl::resetState()
{
m_keymap.resetState();
m_events.resize(0);
m_keyEventSeq.clear();
}

// -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -707,6 +710,7 @@ void InputMapper::addEvents(const input_event* input_events, size_t num)
}

const auto res = impl->m_keymap.feed(input_events, num-1); // exclude syn event for keymap feed
impl->m_keyEventSeq.emplace_back(KeyEvent{input_events, input_events + num - 1});

if (res == DeviceKeyMap::Result::Miss)
{ // key sequence miss, send all buffered events so far + current event
Expand Down Expand Up @@ -736,7 +740,8 @@ void InputMapper::addEvents(const input_event* input_events, size_t num)
if (impl->m_vdev)
{
if (impl->m_keymap.state()->second) {
impl->execAction(impl->m_keymap.state()->second->action, DeviceKeyMap::Result::Hit);
auto action = getAction(impl->m_keyEventSeq);
if (action) impl->execAction(action, DeviceKeyMap::Result::Hit);
}
else
{
Expand All @@ -757,6 +762,20 @@ void InputMapper::addEvents(const input_event* input_events, size_t num)
}
}

// -------------------------------------------------------------------------------------------------
std::shared_ptr<Action> InputMapper::getAction(KeyEventSequence kes)
{
if (kes.empty()) return nullptr;

auto conf = configuration();
const auto find_it = std::find_if(conf.cbegin(), conf.cend(), [kes](auto c) {
return kes == c.first;
});

if (find_it != conf.cend() && find_it->second.action) return find_it->second.action;
return nullptr;
}

// -------------------------------------------------------------------------------------------------
void InputMapper::resetState()
{
Expand Down
2 changes: 2 additions & 0 deletions src/deviceinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ class InputMapper : public QObject
void setConfiguration(InputMapConfig&& config);
const InputMapConfig& configuration() const;

std::shared_ptr<Action> getAction(KeyEventSequence kes);

signals:
void configurationChanged();
void recordingModeChanged(bool recording);
Expand Down

0 comments on commit d2d6214

Please sign in to comment.