diff --git a/src/buttonevent/cycle_key_sequence.cpp b/src/buttonevent/cycle_key_sequence.cpp new file mode 100644 index 0000000..cfe6c6e --- /dev/null +++ b/src/buttonevent/cycle_key_sequence.cpp @@ -0,0 +1,121 @@ +/* +** Xbox360 USB Gamepad Userspace Driver +** Copyright (C) 2011 Ingo Ruhnke +** +** This program is free software: you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program. If not, see . +*/ + +#include "buttonevent/cycle_key_sequence.hpp" + +#include + +#include "raise_exception.hpp" + +CycleKeySequencePtr +CycleKeySequence::from_range(std::vector::const_iterator beg, + std::vector::const_iterator end) +{ + Keys keys; + + for(std::vector::const_iterator i = beg; i != end; ++i) + { + keys.push_back(UIEventSequence::from_string(*i)); + } + + if (keys.empty()) + { + raise_exception(std::runtime_error, "no keys found"); + } + else + { + return CycleKeySequencePtr(new CycleKeySequence(keys)); + } +} + +CycleKeySequence::CycleKeySequence(const Keys& keys) : + m_keys(keys), + m_inited(false), + m_current_key(0), + m_last_key(0) +{ + assert(!m_keys.empty()); +} + +void +CycleKeySequence::init(UInput& uinput, int slot, bool extra_devices) +{ + if (!m_inited) + { + for(Keys::iterator i = m_keys.begin(); i != m_keys.end(); ++i) + { + i->init(uinput, slot, extra_devices); + } + m_inited = true; + } +} + +void +CycleKeySequence::send(UInput& uinput, bool value) +{ + int send_key = has_current_key() ? m_current_key : m_last_key; + + m_keys[send_key].send(uinput, value); + + m_last_key = send_key; + m_current_key = -1; +} + +void +CycleKeySequence::next_key() +{ + if (has_current_key()) + { + if (m_current_key == static_cast(m_keys.size() - 1)) + { + m_current_key = 0; + } + else + { + m_current_key += 1; + } + } + else + { + m_current_key = m_last_key; + next_key(); + } +} + +void +CycleKeySequence::prev_key() +{ + if (has_current_key()) + { + if (m_current_key == 0) + { + m_current_key = static_cast(m_keys.size() - 1); + } + else + { + m_current_key -= 1; + } + } + else + { + m_current_key = m_last_key; + prev_key(); + } +} + +/* EOF */ diff --git a/src/buttonevent/cycle_key_sequence.hpp b/src/buttonevent/cycle_key_sequence.hpp new file mode 100644 index 0000000..c5cb3db --- /dev/null +++ b/src/buttonevent/cycle_key_sequence.hpp @@ -0,0 +1,67 @@ +/* +** Xbox360 USB Gamepad Userspace Driver +** Copyright (C) 2011 Ingo Ruhnke +** +** This program is free software: you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation, either version 3 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program. If not, see . +*/ + +#ifndef HEADER_XBOXDRV_BUTTONEVENT_CYCLE_KEY_SEQUENCE_HPP +#define HEADER_XBOXDRV_BUTTONEVENT_CYCLE_KEY_SEQUENCE_HPP + +#include +#include + +#include "ui_event_sequence.hpp" + +class CycleKeySequence; + +typedef boost::shared_ptr CycleKeySequencePtr; + +class CycleKeySequence +{ +public: + static CycleKeySequencePtr from_range(std::vector::const_iterator beg, + std::vector::const_iterator end); + +private: + typedef std::vector Keys; + Keys m_keys; + + bool m_inited; + + /** the position of the cursor in the sequence, if -1, it is unset */ + int m_current_key; + + /** the last key that was send out */ + int m_last_key; + +public: + CycleKeySequence(const Keys& keys); + + bool has_current_key() const { return m_current_key != -1; } + + void next_key(); + void prev_key(); + + void init(UInput& uinput, int slot, bool extra_devices); + void send(UInput& uinput, bool value); + +private: + CycleKeySequence(const CycleKeySequence&); + CycleKeySequence& operator=(const CycleKeySequence&); +}; + +#endif + +/* EOF */