From 0f5810ee551a5acffb21c3b968e4fa0945913772 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Wed, 13 Jul 2011 05:26:37 +0200 Subject: [PATCH] Split UIEventCollector and UIEventEmitter into subclasses for EV_ABS, EV_REL and EV_KEY to allow more flexible merging --- src/ui_abs_event_collector.cpp | 46 +++++++++++++++++++++++++ src/ui_abs_event_collector.hpp | 44 ++++++++++++++++++++++++ src/ui_abs_event_emitter.cpp | 38 +++++++++++++++++++++ src/ui_abs_event_emitter.hpp | 44 ++++++++++++++++++++++++ src/ui_event_collector.cpp | 42 ++--------------------- src/ui_event_collector.hpp | 13 +++----- src/ui_event_emitter.cpp | 20 ++--------- src/ui_event_emitter.hpp | 22 +++++++----- src/ui_key_event_collector.cpp | 54 ++++++++++++++++++++++++++++++ src/ui_key_event_collector.hpp | 48 ++++++++++++++++++++++++++ src/ui_key_event_emitter.cpp | 38 +++++++++++++++++++++ src/ui_key_event_emitter.hpp | 44 ++++++++++++++++++++++++ src/ui_rel_event_collector.cpp | 53 +++++++++++++++++++++++++++++ src/ui_rel_event_collector.hpp | 44 ++++++++++++++++++++++++ src/ui_rel_event_emitter.cpp | 44 ++++++++++++++++++++++++ src/ui_rel_event_emitter.hpp | 46 +++++++++++++++++++++++++ src/uinput.cpp | 61 ++++++++++++++++++++++++++++++++-- src/uinput.hpp | 6 ++++ 18 files changed, 629 insertions(+), 78 deletions(-) create mode 100644 src/ui_abs_event_collector.cpp create mode 100644 src/ui_abs_event_collector.hpp create mode 100644 src/ui_abs_event_emitter.cpp create mode 100644 src/ui_abs_event_emitter.hpp create mode 100644 src/ui_key_event_collector.cpp create mode 100644 src/ui_key_event_collector.hpp create mode 100644 src/ui_key_event_emitter.cpp create mode 100644 src/ui_key_event_emitter.hpp create mode 100644 src/ui_rel_event_collector.cpp create mode 100644 src/ui_rel_event_collector.hpp create mode 100644 src/ui_rel_event_emitter.cpp create mode 100644 src/ui_rel_event_emitter.hpp diff --git a/src/ui_abs_event_collector.cpp b/src/ui_abs_event_collector.cpp new file mode 100644 index 0000000..5aaf63c --- /dev/null +++ b/src/ui_abs_event_collector.cpp @@ -0,0 +1,46 @@ +/* +** 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 "ui_abs_event_collector.hpp" + +#include "uinput.hpp" + +UIAbsEventCollector::UIAbsEventCollector(UInput& uinput, uint32_t device_id, int type, int code) : + UIEventCollector(uinput, device_id, type, code), + m_emitters() +{ +} + +UIEventEmitterPtr +UIAbsEventCollector::create_emitter() +{ + UIAbsEventEmitterPtr emitter(new UIAbsEventEmitter); + m_emitters.push_back(emitter); + return m_emitters.back(); +} + +void +UIAbsEventCollector::sync() +{ + for(Emitters::iterator i = m_emitters.begin(); i != m_emitters.end(); ++i) + { + m_uinput.send(get_device_id(), get_type(), get_code(), (*i)->get_value()); + } +} + +/* EOF */ diff --git a/src/ui_abs_event_collector.hpp b/src/ui_abs_event_collector.hpp new file mode 100644 index 0000000..140c110 --- /dev/null +++ b/src/ui_abs_event_collector.hpp @@ -0,0 +1,44 @@ +/* +** 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_UI_ABS_EVENT_COLLECTOR_CPP +#define HEADER_XBOXDRV_UI_ABS_EVENT_COLLECTOR_CPP + +#include "ui_event_collector.hpp" +#include "ui_abs_event_emitter.hpp" + +class UIAbsEventCollector : public UIEventCollector +{ +private: + typedef std::vector Emitters; + Emitters m_emitters; + +public: + UIAbsEventCollector(UInput& uinput, uint32_t device_id, int type, int code); + + UIEventEmitterPtr create_emitter(); + void sync(); + +private: + UIAbsEventCollector(const UIAbsEventCollector&); + UIAbsEventCollector& operator=(const UIAbsEventCollector&); +}; + +#endif + +/* EOF */ diff --git a/src/ui_abs_event_emitter.cpp b/src/ui_abs_event_emitter.cpp new file mode 100644 index 0000000..de29358 --- /dev/null +++ b/src/ui_abs_event_emitter.cpp @@ -0,0 +1,38 @@ +/* +** 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 "ui_abs_event_emitter.hpp" + +UIAbsEventEmitter::UIAbsEventEmitter() : + m_value(0) +{ +} + +void +UIAbsEventEmitter::send(int value) +{ + m_value = value; +} + +int +UIAbsEventEmitter::get_value() const +{ + return m_value; +} + +/* EOF */ diff --git a/src/ui_abs_event_emitter.hpp b/src/ui_abs_event_emitter.hpp new file mode 100644 index 0000000..476080a --- /dev/null +++ b/src/ui_abs_event_emitter.hpp @@ -0,0 +1,44 @@ +/* +** 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_UI_ABS_EVENT_EMITTER_HPP +#define HEADER_XBOXDRV_UI_ABS_EVENT_EMITTER_HPP + +#include "ui_event_emitter.hpp" + +class UIAbsEventEmitter : public UIEventEmitter +{ +private: + int m_value; + +public: + UIAbsEventEmitter(); + + void send(int value); + int get_value() const; + +private: + UIAbsEventEmitter(const UIAbsEventEmitter&); + UIAbsEventEmitter& operator=(const UIAbsEventEmitter&); +}; + +typedef boost::shared_ptr UIAbsEventEmitterPtr; + +#endif + +/* EOF */ diff --git a/src/ui_event_collector.cpp b/src/ui_event_collector.cpp index 3db09b6..a73df57 100644 --- a/src/ui_event_collector.cpp +++ b/src/ui_event_collector.cpp @@ -30,51 +30,13 @@ UIEventCollector::UIEventCollector(UInput& uinput, m_uinput(uinput), m_device_id(device_id), m_type(type), - m_code(code), - m_value(0), - m_emitters() + m_code(code) { assert(m_code != -1); } -UIEventEmitterPtr -UIEventCollector::create_emitter() +UIEventCollector::~UIEventCollector() { - UIEventEmitterPtr emitter(new UIEventEmitter(*this)); - m_emitters.push_back(emitter); - return emitter; -} - -void -UIEventCollector::sync() -{ - int value = 0; - for(Emitters::iterator i = m_emitters.begin(); i != m_emitters.end(); ++i) - { - switch(m_type) - { - case EV_KEY: - value = value || (*i)->get_value(); - break; - - case EV_REL: - value += (*i)->get_value(); - break; - - case EV_ABS: - value = (*i)->get_value(); - break; - - default: - assert(!"unknown type"); - } - } - - if (value != m_value) - { - m_value = value; - m_uinput.send(m_device_id, m_type, m_code, m_value); - } } /* EOF */ diff --git a/src/ui_event_collector.hpp b/src/ui_event_collector.hpp index 6ba426b..4c09887 100644 --- a/src/ui_event_collector.hpp +++ b/src/ui_event_collector.hpp @@ -32,27 +32,22 @@ typedef boost::shared_ptr UIEventCollectorPtr; class UIEventCollector { -private: +protected: UInput& m_uinput; uint32_t m_device_id; int m_type; int m_code; - - int m_value; - - typedef std::vector Emitters; - Emitters m_emitters; public: UIEventCollector(UInput& uinput, uint32_t device_id, int type, int code); + virtual ~UIEventCollector(); uint32_t get_device_id() const { return m_device_id; } int get_type() const { return m_type; } int get_code() const { return m_code; } - UIEventEmitterPtr create_emitter(); - - void sync(); + virtual UIEventEmitterPtr create_emitter() = 0; + virtual void sync() = 0; private: UIEventCollector(const UIEventCollector&); diff --git a/src/ui_event_emitter.cpp b/src/ui_event_emitter.cpp index c514fb5..f4da6d1 100644 --- a/src/ui_event_emitter.cpp +++ b/src/ui_event_emitter.cpp @@ -19,24 +19,8 @@ #include "ui_event_emitter.hpp" #include + +#include "log.hpp" #include "uinput.hpp" -UIEventEmitter::UIEventEmitter(UInput& uinput, - uint32_t device_id, - int type, - int code) : - m_uinput(uinput), - m_device_id(device_id), - m_type(type), - m_code(code) -{ - assert(m_code != -1); -} - -void -UIEventEmitter::send(int value) -{ - m_uinput.send(m_device_id, m_type, m_code, value); -} - /* EOF */ diff --git a/src/ui_event_emitter.hpp b/src/ui_event_emitter.hpp index 7396175..894715b 100644 --- a/src/ui_event_emitter.hpp +++ b/src/ui_event_emitter.hpp @@ -24,27 +24,33 @@ class UInput; class UIEventEmitter; +class UIEventCollector; typedef boost::shared_ptr UIEventEmitterPtr; class UIEventEmitter { -private: - UInput& m_uinput; - uint32_t m_device_id; - int m_type; - int m_code; - public: - UIEventEmitter(UInput& uinput, uint32_t device_id, int type, int code); + UIEventEmitter() {} + virtual ~UIEventEmitter() {} - void send(int value); + virtual void send(int value) = 0; + virtual int get_value() const = 0; private: UIEventEmitter(const UIEventEmitter&); UIEventEmitter& operator=(const UIEventEmitter&); }; +class UIRelEmitter +{ +private: + +public: + UIRelEmitter(); + +}; + #endif /* EOF */ diff --git a/src/ui_key_event_collector.cpp b/src/ui_key_event_collector.cpp new file mode 100644 index 0000000..d88d507 --- /dev/null +++ b/src/ui_key_event_collector.cpp @@ -0,0 +1,54 @@ +/* +** 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 "ui_key_event_collector.hpp" + +#include "uinput.hpp" + +UIKeyEventCollector::UIKeyEventCollector(UInput& uinput, uint32_t device_id, int type, int code) : + UIEventCollector(uinput, device_id, type, code), + m_emitters(), + m_value(0) +{ +} + +UIEventEmitterPtr +UIKeyEventCollector::create_emitter() +{ + UIKeyEventEmitterPtr emitter(new UIKeyEventEmitter); + m_emitters.push_back(emitter); + return m_emitters.back(); +} + +void +UIKeyEventCollector::sync() +{ + int value = 0; + for(Emitters::iterator i = m_emitters.begin(); i != m_emitters.end(); ++i) + { + value = value || (*i)->get_value(); + } + + if (value != m_value) + { + m_value = value; + m_uinput.send(get_device_id(), get_type(), get_code(), m_value); + } +} + +/* EOF */ diff --git a/src/ui_key_event_collector.hpp b/src/ui_key_event_collector.hpp new file mode 100644 index 0000000..6c34a43 --- /dev/null +++ b/src/ui_key_event_collector.hpp @@ -0,0 +1,48 @@ +/* +** 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_UI_KEY_EVENT_COLLECTOR_HPP +#define HEADER_XBOXDRV_UI_KEY_EVENT_COLLECTOR_HPP + +#include "ui_event_collector.hpp" +#include "ui_key_event_emitter.hpp" + +#include + +class UIKeyEventCollector : public UIEventCollector +{ +private: + typedef std::vector Emitters; + Emitters m_emitters; + + int m_value; + +public: + UIKeyEventCollector(UInput& uinput, uint32_t device_id, int type, int code); + + UIEventEmitterPtr create_emitter(); + void sync(); + +private: + UIKeyEventCollector(const UIKeyEventCollector&); + UIKeyEventCollector& operator=(const UIKeyEventCollector&); +}; + +#endif + +/* EOF */ diff --git a/src/ui_key_event_emitter.cpp b/src/ui_key_event_emitter.cpp new file mode 100644 index 0000000..8ee6482 --- /dev/null +++ b/src/ui_key_event_emitter.cpp @@ -0,0 +1,38 @@ +/* +** 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 "ui_key_event_emitter.hpp" + +UIKeyEventEmitter::UIKeyEventEmitter() : + m_value(0) +{ +} + +void +UIKeyEventEmitter::send(int value) +{ + m_value = value; +} + +int +UIKeyEventEmitter::get_value() const +{ + return m_value; +} + +/* EOF */ diff --git a/src/ui_key_event_emitter.hpp b/src/ui_key_event_emitter.hpp new file mode 100644 index 0000000..7344b3a --- /dev/null +++ b/src/ui_key_event_emitter.hpp @@ -0,0 +1,44 @@ +/* +** 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_UI_KEY_EVENT_EMITTER_HPP +#define HEADER_XBOXDRV_UI_KEY_EVENT_EMITTER_HPP + +#include "ui_event_emitter.hpp" + +class UIKeyEventEmitter : public UIEventEmitter +{ +private: + int m_value; + +public: + UIKeyEventEmitter(); + + void send(int value); + int get_value() const; + +private: + UIKeyEventEmitter(const UIKeyEventEmitter&); + UIKeyEventEmitter& operator=(const UIKeyEventEmitter&); +}; + +typedef boost::shared_ptr UIKeyEventEmitterPtr; + +#endif + +/* EOF */ diff --git a/src/ui_rel_event_collector.cpp b/src/ui_rel_event_collector.cpp new file mode 100644 index 0000000..af13864 --- /dev/null +++ b/src/ui_rel_event_collector.cpp @@ -0,0 +1,53 @@ +/* +** 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 "ui_rel_event_collector.hpp" + +#include "uinput.hpp" + +UIRelEventCollector::UIRelEventCollector(UInput& uinput, uint32_t device_id, int type, int code) : + UIEventCollector(uinput, device_id, type, code), + m_emitters() +{ +} + +UIEventEmitterPtr +UIRelEventCollector::create_emitter() +{ + UIRelEventEmitterPtr emitter(new UIRelEventEmitter); + m_emitters.push_back(emitter); + return m_emitters.back(); +} + +void +UIRelEventCollector::sync() +{ + int value = 0; + for(Emitters::iterator i = m_emitters.begin(); i != m_emitters.end(); ++i) + { + value += (*i)->get_value(); + (*i)->clear(); + } + + if (value != 0) + { + m_uinput.send(get_device_id(), get_type(), get_code(), value); + } +} + +/* EOF */ diff --git a/src/ui_rel_event_collector.hpp b/src/ui_rel_event_collector.hpp new file mode 100644 index 0000000..341b32a --- /dev/null +++ b/src/ui_rel_event_collector.hpp @@ -0,0 +1,44 @@ +/* +** 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_UI_REL_EVENT_COLLECTOR_HPP +#define HEADER_XBOXDRV_UI_REL_EVENT_COLLECTOR_HPP + +#include "ui_event_collector.hpp" +#include "ui_rel_event_emitter.hpp" + +class UIRelEventCollector : public UIEventCollector +{ +private: + typedef std::vector Emitters; + Emitters m_emitters; + +public: + UIRelEventCollector(UInput& uinput, uint32_t device_id, int type, int code); + + UIEventEmitterPtr create_emitter(); + void sync(); + +private: + UIRelEventCollector(const UIRelEventCollector&); + UIRelEventCollector& operator=(const UIRelEventCollector&); +}; + +#endif + +/* EOF */ diff --git a/src/ui_rel_event_emitter.cpp b/src/ui_rel_event_emitter.cpp new file mode 100644 index 0000000..d88e809 --- /dev/null +++ b/src/ui_rel_event_emitter.cpp @@ -0,0 +1,44 @@ +/* +** 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 "ui_rel_event_emitter.hpp" + +UIRelEventEmitter::UIRelEventEmitter() : + m_value() +{ +} + +void +UIRelEventEmitter::send(int value) +{ + m_value = value; +} + +int +UIRelEventEmitter::get_value() const +{ + return m_value; +} + +void +UIRelEventEmitter::clear() +{ + m_value = 0; +} + +/* EOF */ diff --git a/src/ui_rel_event_emitter.hpp b/src/ui_rel_event_emitter.hpp new file mode 100644 index 0000000..677467a --- /dev/null +++ b/src/ui_rel_event_emitter.hpp @@ -0,0 +1,46 @@ +/* +** 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_UI_REL_EVENT_EMITTER_HPP +#define HEADER_XBOXDRV_UI_REL_EVENT_EMITTER_HPP + +#include "ui_event_emitter.hpp" + +class UIRelEventEmitter : public UIEventEmitter +{ +private: + int m_value; + +public: + UIRelEventEmitter(); + + void send(int value); + int get_value() const; + + void clear(); + +private: + UIRelEventEmitter(const UIRelEventEmitter&); + UIRelEventEmitter& operator=(const UIRelEventEmitter&); +}; + +typedef boost::shared_ptr UIRelEventEmitterPtr; + +#endif + +/* EOF */ diff --git a/src/uinput.cpp b/src/uinput.cpp index 43354dc..26c84b8 100644 --- a/src/uinput.cpp +++ b/src/uinput.cpp @@ -24,6 +24,10 @@ #include #include +#include "ui_abs_event_collector.hpp" +#include "ui_key_event_collector.hpp" +#include "ui_rel_event_collector.hpp" + #include "helper.hpp" #include "log.hpp" #include "raise_exception.hpp" @@ -103,6 +107,7 @@ UInput::UInput(bool extra_events) : m_uinput_devs(), m_device_names(), m_device_usbids(), + m_collectors(), m_rel_repeat_lst(), m_extra_events(extra_events), m_timeout_id(), @@ -324,7 +329,7 @@ UInput::add_key(uint32_t device_id, int ev_code) LinuxUinput* dev = create_uinput_device(device_id); dev->add_key(ev_code); - return UIEventEmitterPtr(new UIEventEmitter(*this, device_id, EV_KEY, ev_code)); + return create_emitter(device_id, EV_KEY, ev_code); } UIEventEmitterPtr @@ -333,7 +338,7 @@ UInput::add_rel(uint32_t device_id, int ev_code) LinuxUinput* dev = create_uinput_device(device_id); dev->add_rel(ev_code); - return UIEventEmitterPtr(new UIEventEmitter(*this, device_id, EV_REL, ev_code)); + return create_emitter(device_id, EV_REL, ev_code); } UIEventEmitterPtr @@ -342,7 +347,7 @@ UInput::add_abs(uint32_t device_id, int ev_code, int min, int max, int fuzz, int LinuxUinput* dev = create_uinput_device(device_id); dev->add_abs(ev_code, min, max, fuzz, flat); - return UIEventEmitterPtr(new UIEventEmitter(*this, device_id, EV_KEY, ev_code)); + return create_emitter(device_id, EV_ABS, ev_code); } void @@ -352,6 +357,51 @@ UInput::add_ff(uint32_t device_id, uint16_t code) dev->add_ff(code); } +UIEventEmitterPtr +UInput::create_emitter(int device_id, int type, int code) +{ + // search for an already existing emitter + for(Collectors::iterator i = m_collectors.begin(); i != m_collectors.end(); ++i) + { + if (static_cast((*i)->get_device_id()) == device_id && + (*i)->get_type() == type && + (*i)->get_code() == code) + { + log_tmp("found collector " << device_id << " " << type << " " << code); + return (*i)->create_emitter(); + } + } + + // no emitter found, create a new one + switch(type) + { + case EV_ABS: + { + UIEventCollectorPtr collector(new UIAbsEventCollector(*this, device_id, type, code)); + m_collectors.push_back(collector); + return collector->create_emitter(); + } + + case EV_KEY: + { + UIEventCollectorPtr collector(new UIKeyEventCollector(*this, device_id, type, code)); + m_collectors.push_back(collector); + return collector->create_emitter(); + } + + case EV_REL: + { + UIEventCollectorPtr collector(new UIRelEventCollector(*this, device_id, type, code)); + m_collectors.push_back(collector); + return collector->create_emitter(); + } + + default: + assert(!"unknown type"); + break; + } +} + void UInput::finish() { @@ -398,6 +448,11 @@ UInput::update(int msec_delta) void UInput::sync() { + for(Collectors::iterator i = m_collectors.begin(); i != m_collectors.end(); ++i) + { + (*i)->sync(); + } + for(UInputDevs::iterator i = m_uinput_devs.begin(); i != m_uinput_devs.end(); ++i) { i->second->sync(); diff --git a/src/uinput.hpp b/src/uinput.hpp index cf59f2e..652ea84 100644 --- a/src/uinput.hpp +++ b/src/uinput.hpp @@ -25,6 +25,7 @@ #include "axis_event.hpp" #include "linux_uinput.hpp" #include "ui_event_emitter.hpp" +#include "ui_event_collector.hpp" struct Xbox360Msg; struct XboxMsg; @@ -61,6 +62,9 @@ private: typedef std::map DeviceUSBId; DeviceUSBId m_device_usbids; + typedef std::vector Collectors; + Collectors m_collectors; + struct RelRepeat { UIEvent code; @@ -131,6 +135,8 @@ private: return static_cast(data)->on_timeout(); } + UIEventEmitterPtr create_emitter(int device_id, int type, int code); + private: UInput(const UInput&); UInput& operator=(const UInput&);