Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fake_event_hub.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012-2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Robert Carr <robert.carr@canonical.com>
17  * Daniel d'Andrada <daniel.dandrada@canonical.com>
18  */
19 
20 #ifndef MIR_TEST_FAKE_EVENT_HUB_H_
21 #define MIR_TEST_FAKE_EVENT_HUB_H_
22 
23 #include "mir_test/event_factory.h"
24 
25 #include <atomic>
26 #include <mutex>
27 
28 // from android-input
29 #include <EventHub.h>
30 
31 #include <list>
32 #include <map>
33 
34 #include <androidfw/Keyboard.h>
35 namespace droidinput = android;
36 
37 namespace mir
38 {
39 namespace input
40 {
41 namespace android
42 {
43 // An EventHub implementation that generates fake raw events.
44 class FakeEventHub : public droidinput::EventHubInterface
45 {
46 public:
47  struct KeyInfo {
48  int32_t keyCode;
49  uint32_t flags;
50  };
51 
52  typedef struct FakeDevice
53  {
54  uint32_t classes;
55  droidinput::InputDeviceIdentifier identifier;
56  droidinput::PropertyMap configuration;
57  droidinput::KeyedVector<int, droidinput::RawAbsoluteAxisInfo> absoluteAxes;
58  droidinput::KeyedVector<int, bool> relativeAxes;
59  droidinput::KeyedVector<int32_t, int32_t> keyCodeStates;
60  droidinput::KeyedVector<int32_t, int32_t> scanCodeStates;
61  droidinput::KeyedVector<int32_t, int32_t> switchStates;
62  droidinput::KeyedVector<int32_t, int32_t> absoluteAxisValue;
63  droidinput::KeyedVector<int32_t, KeyInfo> keysByScanCode;
64  droidinput::KeyedVector<int32_t, KeyInfo> keysByUsageCode;
65  droidinput::KeyedVector<int32_t, bool> leds;
66  droidinput::Vector<droidinput::VirtualKeyDefinition> virtualKeys;
67  std::map<int, bool> input_properties;
68  } FakeDevice;
69 
70  FakeEventHub();
71  virtual ~FakeEventHub();
72 
73  static const int BuiltInKeyboardID = droidinput::BUILT_IN_KEYBOARD_ID;
74  // Any positive int besides BUILT_IN_KEYBOARD_ID (which has
75  // special meaning) will do. There is no notion of a builtin
76  // cursor device in the android input stack.
77  static const int BuiltInCursorID = droidinput::BUILT_IN_KEYBOARD_ID + 1;
78 
79  // From EventHubInterface
80  uint32_t getDeviceClasses(int32_t deviceId) const override;
81  droidinput::InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override;
82  void getConfiguration(int32_t deviceId, droidinput::PropertyMap* outConfiguration) const override;
83  droidinput::status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
84  droidinput::RawAbsoluteAxisInfo* outAxisInfo) const override;
85  bool hasRelativeAxis(int32_t deviceId, int axis) const override;
86  bool hasInputProperty(int32_t deviceId, int property) const override;
87  droidinput::status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t* outKeycode,
88  uint32_t* outFlags) const override;
89  droidinput::status_t mapAxis(int32_t deviceId, int32_t scanCode,
90  droidinput::AxisInfo* outAxisInfo) const override;
91  void setExcludedDevices(const droidinput::Vector<droidinput::String8>& devices) override;
92  size_t getEvents(int timeoutMillis, droidinput::RawEvent* buffer, size_t bufferSize) override;
93  int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override;
94  int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override;
95  int32_t getSwitchState(int32_t deviceId, int32_t sw) const override;
96  droidinput::status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const override;
97  bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
98  uint8_t* outFlags) const override;
99  bool hasScanCode(int32_t deviceId, int32_t scanCode) const override;
100  bool hasLed(int32_t deviceId, int32_t led) const override;
101  void setLedState(int32_t deviceId, int32_t led, bool on) override;
102  void getVirtualKeyDefinitions(int32_t deviceId,
103  droidinput::Vector<droidinput::VirtualKeyDefinition>& outVirtualKeys) const override;
104  droidinput::sp<droidinput::KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override;
105  bool setKeyboardLayoutOverlay(int32_t deviceId,
106  const droidinput::sp<droidinput::KeyCharacterMap>& map) override;
107  void vibrate(int32_t deviceId, nsecs_t duration) override;
108  void cancelVibrate(int32_t deviceId) override;
109  void requestReopenDevices() override;
110  void wake() override;
111  void dump(droidinput::String8& dump) override;
112  void monitor() override;
113  void flush() override;
114 
115  void synthesize_builtin_keyboard_added();
116  void synthesize_builtin_cursor_added();
117  void synthesize_device_scan_complete();
118 
119  void synthesize_event(const synthesis::KeyParameters &parameters);
120  void synthesize_event(const synthesis::ButtonParameters &parameters);
121  void synthesize_event(const synthesis::MotionParameters &parameters);
122  void synthesize_event(nsecs_t when, int32_t deviceId, int32_t type, int32_t code, int32_t value);
123 
124  void addDevice(int32_t deviceId, const std::string& name, uint32_t classes);
125  void removeDevice(int32_t deviceId);
126  void finishDeviceScan();
127  void addConfigurationProperty(int32_t device_id, const std::string& key, const std::string& value);
128  void addConfigurationMap(int32_t device_id, const droidinput::PropertyMap* configuration);
129  void addAbsoluteAxis(int32_t device_id, int axis, int32_t minValue, int32_t maxValue,
130  int flat, int fuzz, int resolution = 0);
131  void addRelativeAxis(int32_t device_id, int32_t axis);
132  void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state);
133  void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state);
134  void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state);
135  void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value);
136  void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t keyCode, uint32_t flags);
137  void addLed(int32_t deviceId, int32_t led, bool initialState);
138  bool getLedState(int32_t deviceId, int32_t led);
139  droidinput::Vector<std::string>& getExcludedDevices();
140  void addVirtualKeyDefinition(int32_t deviceId, const droidinput::VirtualKeyDefinition& definition);
141  const FakeDevice* getDevice(int32_t deviceId) const;
142  FakeDevice* getDevice(int32_t deviceId);
143  size_t eventsQueueSize() const;
144 
145  // list of RawEvents available for consumption via getEvents
146  std::mutex guard;
147  std::list<droidinput::RawEvent> events_available;
148 
149 
150  std::map<int32_t, FakeDevice> device_from_id;
151 
152  droidinput::KeyMap keymap;
153 
154  droidinput::Vector<droidinput::String8> excluded_devices;
155 
156 private:
157  const KeyInfo* getKey(const FakeDevice* device, int32_t scanCode, int32_t usageCode) const;
158 
159 };
160 }
161 }
162 } // namespace mir
163 
164 #endif // MIR_TEST_FAKE_EVENT_HUB_H_
int64_t nsecs_t
Definition: event.h:36

Copyright © 2012,2013 Canonical Ltd.
Generated on Fri Apr 11 21:14:53 UTC 2014