Split UIEventCollector and UIEventEmitter into subclasses for EV_ABS, EV_REL and EV_KEY to allow more flexible merging

This commit is contained in:
Ingo Ruhnke 2011-07-13 05:26:37 +02:00
parent 9975da3cd6
commit 0f5810ee55
18 changed files with 629 additions and 78 deletions

View file

@ -0,0 +1,46 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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 */

View file

@ -0,0 +1,44 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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<UIAbsEventEmitterPtr> 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 */

View file

@ -0,0 +1,38 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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 */

View file

@ -0,0 +1,44 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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<UIAbsEventEmitter> UIAbsEventEmitterPtr;
#endif
/* EOF */

View file

@ -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 */

View file

@ -32,27 +32,22 @@ typedef boost::shared_ptr<UIEventCollector> 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<UIEventEmitterPtr> 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&);

View file

@ -19,24 +19,8 @@
#include "ui_event_emitter.hpp"
#include <assert.h>
#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 */

View file

@ -24,27 +24,33 @@
class UInput;
class UIEventEmitter;
class UIEventCollector;
typedef boost::shared_ptr<UIEventEmitter> 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 */

View file

@ -0,0 +1,54 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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 */

View file

@ -0,0 +1,48 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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 <vector>
class UIKeyEventCollector : public UIEventCollector
{
private:
typedef std::vector<UIKeyEventEmitterPtr> 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 */

View file

@ -0,0 +1,38 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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 */

View file

@ -0,0 +1,44 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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<UIKeyEventEmitter> UIKeyEventEmitterPtr;
#endif
/* EOF */

View file

@ -0,0 +1,53 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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 */

View file

@ -0,0 +1,44 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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<UIRelEventEmitterPtr> 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 */

View file

@ -0,0 +1,44 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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 */

View file

@ -0,0 +1,46 @@
/*
** Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2011 Ingo Ruhnke <grumbel@gmx.de>
**
** 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 <http://www.gnu.org/licenses/>.
*/
#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<UIRelEventEmitter> UIRelEventEmitterPtr;
#endif
/* EOF */

View file

@ -24,6 +24,10 @@
#include <stdexcept>
#include <stdio.h>
#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<int>((*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();

View file

@ -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<uint32_t, struct input_id> DeviceUSBId;
DeviceUSBId m_device_usbids;
typedef std::vector<UIEventCollectorPtr> Collectors;
Collectors m_collectors;
struct RelRepeat
{
UIEvent code;
@ -131,6 +135,8 @@ private:
return static_cast<UInput*>(data)->on_timeout();
}
UIEventEmitterPtr create_emitter(int device_id, int type, int code);
private:
UInput(const UInput&);
UInput& operator=(const UInput&);