Mir

Merge lp:~dandrader/mir/mergeFakeInputReaders into lp:mir

Proposed by Daniel d'Andrada
Status: Merged
Approved by: Robert Carr
Approved revision: no longer in the source branch.
Merged at revision: 1109
Proposed branch: lp:~dandrader/mir/mergeFakeInputReaders
Merge into: lp:mir
Diff against target: 1422 lines (+534/-600)
3 files modified
include/test/mir_test/fake_event_hub.h (+88/-64)
tests/mir_test_doubles/fake_event_hub.cpp (+432/-117)
tests/unit-tests/android_input/input_reader.cpp (+14/-419)
To merge this branch: bzr merge lp:~dandrader/mir/mergeFakeInputReaders
Reviewer Review Type Date Requested Status
Robert Carr (community) Approve
Alan Griffiths Approve
PS Jenkins bot (community) continuous-integration Approve
Daniel van Vugt Needs Information
Review via email: mp+188662@code.launchpad.net

Commit message

android-input - Merge the two existing FakeEventHub implementations

-Enables the FakeEventHub from tests/unit-tests/android_input/input_reader.cpp
 to be used elsewhere
-No need to keep two distinct implementations.
-Reduces confusion in the test code base.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Daniel van Vugt (vanvugt) wrote :

I thought all 3rd party code had to live under 3rd_party/ ?

Seems like we're not doing that any more. e.g. tests/mir_test_doubles/fake_event_hub.cpp

review: Needs Information
Revision history for this message
Daniel d'Andrada (dandrader) wrote :

> I thought all 3rd party code had to live under 3rd_party/ ?
>
> Seems like we're not doing that any more. e.g.
> tests/mir_test_doubles/fake_event_hub.cpp

So what directory structure you suggest for those tests and fake implementations?

Bear in mind that I'm preparing the ground for my modifications to android-input, so it won't be strictly 3rd party.

Revision history for this message
Daniel van Vugt (vanvugt) wrote :

I don't know. I'm not sure if it's worth blocking on yet... Unless this is the first time we've put 3rd party code outside of 3rd_party. Maybe racarr had a plan in mind?

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

I think we need a discussion about migrating this code into our codebase. We obviously relax our rules for style in 3rd_party, but under include, src or test it ought to conform.

237 +using mir::input::android::FakeEventHub;
238 +using namespace android;

Nit, but project style is to use namespace aliases in preference to bringing names into global namespace.

~~~~

242 + const nsecs_t ARBITRARY_TIME = 1234;

UPPERCASE is reserved for macros - http://unity.ubuntu.com/mir/cppguide/index.html?showone=Constant_Names#Constant_Names

review: Needs Information
Revision history for this message
Daniel d'Andrada (dandrader) wrote :

> 237 +using mir::input::android::FakeEventHub;
> 238 +using namespace android;
>
> Nit, but project style is to use namespace aliases in preference to bringing
> names into global namespace.

But what's the advantage of prepending everything with namespace acronyms? IMHO this is an unnecessary clutter/boilerplate that just hampers readability.

In this specific case we would have:
void mia::FakeEventHub::foo()
As opposed to simply:
void FakeEventHub::foo()

What good is that "mia::" prefix bringing us? We are inside fake_event_hub.cpp and we refer to no other FakeEventHub from any other namespace. Actually no other FakeEventHub even exist in our code base.

It makes all the sense in the world to avoid the "using foo;" in header files as it unwittingly contaminates anyone that includes it but what's the point of avoiding it in .cpp files?

You use namespaces to avoid naming clashes. But if there are no possible clashes to be had, what's the point? It's just clutter and the "using ..." declaration was invented to solve that.

Can we have a discussion on changing the project style regarding that?

Revision history for this message
Daniel d'Andrada (dandrader) wrote :

> 242 + const nsecs_t ARBITRARY_TIME = 1234;
>
> UPPERCASE is reserved for macros - http://unity.ubuntu.com/mir/cppguide/index.
> html?showone=Constant_Names#Constant_Names

Done.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Daniel van Vugt (vanvugt) wrote :

I agree with Daniel. Namespace aliases just hamper readability. I prefer carefully placed "using namespace". There's less syntactical coupling and it's more readable. We have already begun landing code of that style (my own).

Revision history for this message
Daniel d'Andrada (dandrader) wrote :

> I don't know. I'm not sure if it's worth blocking on yet... Unless this is the
> first time we've put 3rd party code outside of 3rd_party.

There's tests/unit-tests/android_input/input_reader.cpp already

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

Better landed and deal with any remaining issues later

review: Approve
Revision history for this message
Robert Carr (robertcarr) wrote :

Sorry! Looked at this yesterday afternoon but forgot to comment

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'include/test/mir_test/fake_event_hub.h'
--- include/test/mir_test/fake_event_hub.h 2013-05-13 09:35:44 +0000
+++ include/test/mir_test/fake_event_hub.h 2013-10-02 11:08:26 +0000
@@ -1,5 +1,5 @@
1/*1/*
2 * Copyright © 2012 Canonical Ltd.2 * Copyright © 2012-2013 Canonical Ltd.
3 *3 *
4 * This program is free software: you can redistribute it and/or modify it4 * 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,5 * under the terms of the GNU General Public License version 3,
@@ -13,7 +13,8 @@
13 * You should have received a copy of the GNU General Public License13 * 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/>.14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *15 *
16 * Authored by: Robert Carr <robert.carr@canonical.com16 * Authored by: Robert Carr <robert.carr@canonical.com>
17 * Daniel d'Andrada <daniel.dandrada@canonical.com>
17 */18 */
1819
19#ifndef MIR_TEST_FAKE_EVENT_HUB_H_20#ifndef MIR_TEST_FAKE_EVENT_HUB_H_
@@ -43,6 +44,29 @@
43class FakeEventHub : public droidinput::EventHubInterface44class FakeEventHub : public droidinput::EventHubInterface
44{45{
45public:46public:
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
46 FakeEventHub();70 FakeEventHub();
47 virtual ~FakeEventHub();71 virtual ~FakeEventHub();
4872
@@ -52,62 +76,41 @@
52 // cursor device in the android input stack.76 // cursor device in the android input stack.
53 static const int BuiltInCursorID = droidinput::BUILT_IN_KEYBOARD_ID + 1;77 static const int BuiltInCursorID = droidinput::BUILT_IN_KEYBOARD_ID + 1;
5478
55 virtual uint32_t getDeviceClasses(int32_t deviceId) const;79 // From EventHubInterface
5680 uint32_t getDeviceClasses(int32_t deviceId) const override;
57 virtual droidinput::InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const;81 droidinput::InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override;
5882 void getConfiguration(int32_t deviceId, droidinput::PropertyMap* outConfiguration) const override;
59 virtual void getConfiguration(int32_t deviceId,83 droidinput::status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
60 droidinput::PropertyMap* outConfiguration) const;84 droidinput::RawAbsoluteAxisInfo* outAxisInfo) const override;
6185 bool hasRelativeAxis(int32_t deviceId, int axis) const override;
62 virtual droidinput::status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,86 bool hasInputProperty(int32_t deviceId, int property) const override;
63 droidinput::RawAbsoluteAxisInfo* outAxisInfo) const;87 droidinput::status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t* outKeycode,
6488 uint32_t* outFlags) const override;
65 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const;89 droidinput::status_t mapAxis(int32_t deviceId, int32_t scanCode,
6690 droidinput::AxisInfo* outAxisInfo) const override;
67 virtual bool hasInputProperty(int32_t deviceId, int property) const;91 void setExcludedDevices(const droidinput::Vector<droidinput::String8>& devices) override;
6892 size_t getEvents(int timeoutMillis, droidinput::RawEvent* buffer, size_t bufferSize) override;
69 virtual droidinput::status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,93 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override;
70 int32_t* outKeycode, uint32_t* outFlags) const;94 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override;
7195 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override;
72 virtual droidinput::status_t mapAxis(int32_t deviceId, int32_t scanCode,96 droidinput::status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const override;
73 droidinput::AxisInfo* outAxisInfo) const;97 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
7498 uint8_t* outFlags) const override;
75 virtual void setExcludedDevices(const droidinput::Vector<droidinput::String8>& devices);99 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override;
76100 bool hasLed(int32_t deviceId, int32_t led) const override;
77 virtual size_t getEvents(int timeoutMillis, droidinput::RawEvent* buffer,101 void setLedState(int32_t deviceId, int32_t led, bool on) override;
78 size_t bufferSize);102 void getVirtualKeyDefinitions(int32_t deviceId,
79103 droidinput::Vector<droidinput::VirtualKeyDefinition>& outVirtualKeys) const override;
80 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const;104 droidinput::sp<droidinput::KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override;
81 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const;105 bool setKeyboardLayoutOverlay(int32_t deviceId,
82 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const;106 const droidinput::sp<droidinput::KeyCharacterMap>& map) override;
83 virtual droidinput::status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,107 void vibrate(int32_t deviceId, nsecs_t duration) override;
84 int32_t* outValue) const;108 void cancelVibrate(int32_t deviceId) override;
85109 void requestReopenDevices() override;
86 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes,110 void wake() override;
87 const int32_t* keyCodes, uint8_t* outFlags) const;111 void dump(droidinput::String8& dump) override;
88112 void monitor() override;
89 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const;113 void flush() override;
90 virtual bool hasLed(int32_t deviceId, int32_t led) const;
91 virtual void setLedState(int32_t deviceId, int32_t led, bool on);
92
93 virtual void getVirtualKeyDefinitions(int32_t deviceId,
94 droidinput::Vector<droidinput::VirtualKeyDefinition>& outVirtualKeys) const;
95
96 virtual droidinput::sp<droidinput::KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const;
97 virtual bool setKeyboardLayoutOverlay(int32_t deviceId,
98 const droidinput::sp<droidinput::KeyCharacterMap>& map);
99
100 virtual void vibrate(int32_t deviceId, nsecs_t duration);
101 virtual void cancelVibrate(int32_t deviceId);
102
103 virtual void requestReopenDevices();
104
105 virtual void wake();
106
107 virtual void dump(droidinput::String8& dump);
108
109 virtual void monitor();
110 virtual void flush();
111114
112 void synthesize_builtin_keyboard_added();115 void synthesize_builtin_keyboard_added();
113 void synthesize_builtin_cursor_added();116 void synthesize_builtin_cursor_added();
@@ -116,22 +119,43 @@
116 void synthesize_event(const synthesis::KeyParameters &parameters);119 void synthesize_event(const synthesis::KeyParameters &parameters);
117 void synthesize_event(const synthesis::ButtonParameters &parameters);120 void synthesize_event(const synthesis::ButtonParameters &parameters);
118 void synthesize_event(const synthesis::MotionParameters &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;
119144
120 // list of RawEvents available for consumption via getEvents145 // list of RawEvents available for consumption via getEvents
121 std::mutex guard;146 std::mutex guard;
122 std::list<droidinput::RawEvent> events_available;147 std::list<droidinput::RawEvent> events_available;
123148
124 typedef struct FakeDevice
125 {
126 uint32_t classes;
127 droidinput::InputDeviceIdentifier identifier;
128 std::map<int, bool> input_properties;
129 } FakeDevice;
130149
131 std::map<int32_t, FakeDevice> device_from_id;150 std::map<int32_t, FakeDevice> device_from_id;
132151
133 droidinput::KeyMap keymap;152 droidinput::KeyMap keymap;
134153
154 droidinput::Vector<droidinput::String8> excluded_devices;
155
156private:
157 const KeyInfo* getKey(const FakeDevice* device, int32_t scanCode, int32_t usageCode) const;
158
135};159};
136}160}
137}161}
138162
=== modified file 'tests/mir_test_doubles/fake_event_hub.cpp'
--- tests/mir_test_doubles/fake_event_hub.cpp 2013-05-13 09:35:44 +0000
+++ tests/mir_test_doubles/fake_event_hub.cpp 2013-10-02 11:08:26 +0000
@@ -1,6 +1,25 @@
1/*
2 * Copyright (C) 2012-2013 Canonical Ltd.
3 * Copyright (C) 2010 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
1#include "mir_test/fake_event_hub.h"17#include "mir_test/fake_event_hub.h"
218
19// from android-input
3#include <androidfw/Keyboard.h>20#include <androidfw/Keyboard.h>
21#include <std/Errors.h>
22
4#include <thread>23#include <thread>
5#include <chrono>24#include <chrono>
625
@@ -17,19 +36,26 @@
17using droidinput::VirtualKeyDefinition;36using droidinput::VirtualKeyDefinition;
1837
19namespace mi = mir::input;38namespace mi = mir::input;
20namespace mia = mir::input::android;
21namespace mis = mir::input::synthesis;39namespace mis = mir::input::synthesis;
2240
23mia::FakeEventHub::FakeEventHub()41using mir::input::android::FakeEventHub;
42using namespace android;
43
44namespace {
45 // An arbitrary time value.
46 const nsecs_t arbitrary_time = 1234;
47} // anonymous namespace
48
49FakeEventHub::FakeEventHub()
24{50{
25 keymap.loadGenericMaps();51 keymap.loadGenericMaps();
26}52}
2753
28mia::FakeEventHub::~FakeEventHub()54FakeEventHub::~FakeEventHub()
29{55{
30}56}
3157
32uint32_t mia::FakeEventHub::getDeviceClasses(int32_t deviceId) const58uint32_t FakeEventHub::getDeviceClasses(int32_t deviceId) const
33{59{
34 if (deviceId == BuiltInKeyboardID)60 if (deviceId == BuiltInKeyboardID)
35 {61 {
@@ -52,7 +78,7 @@
52 }78 }
53}79}
5480
55InputDeviceIdentifier mia::FakeEventHub::getDeviceIdentifier(int32_t deviceId) const81InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const
56{82{
57 auto fake_device_iterator = device_from_id.find(deviceId);83 auto fake_device_iterator = device_from_id.find(deviceId);
5884
@@ -66,38 +92,51 @@
66 }92 }
67}93}
6894
69void mia::FakeEventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const95void FakeEventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const
70{96{
71 (void)deviceId;97 auto device_iterator = device_from_id.find(deviceId);
72 (void)outConfiguration;98 if (device_iterator != device_from_id.end())
99 {
100 *outConfiguration = device_iterator->second.configuration;
101 }
73}102}
74103
75status_t mia::FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,104status_t FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
76 RawAbsoluteAxisInfo* outAxisInfo) const105 RawAbsoluteAxisInfo* outAxisInfo) const
77{106{
78 (void)deviceId;107 outAxisInfo->clear();
79 (void)axis;108 const FakeDevice* device = getDevice(deviceId);
80 (void)outAxisInfo;109 if (device)
110 {
111 ssize_t index = device->absoluteAxes.indexOfKey(axis);
112 if (index >= 0)
113 {
114 *outAxisInfo = device->absoluteAxes.valueAt(index);
115 return OK;
116 }
117 }
81 return -1;118 return -1;
82}119}
83120
84bool mia::FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const121bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const
85{122{
86 (void)deviceId;123 const FakeDevice* device = getDevice(deviceId);
87 (void)axis;124 if (device)
125 {
126 return device->relativeAxes.indexOfKey(axis) >= 0;
127 }
88 return false;128 return false;
89}129}
90130
91bool mia::FakeEventHub::hasInputProperty(int32_t deviceId, int property) const131bool FakeEventHub::hasInputProperty(int32_t deviceId, int property) const
92{132{
93 auto fake_device_iterator = device_from_id.find(deviceId);133 const FakeDevice* device = getDevice(deviceId);
94134
95 if (fake_device_iterator != device_from_id.end())135 if (device)
96 {136 {
97 auto property_iterator =137 auto property_iterator = device->input_properties.find(property);
98 fake_device_iterator->second.input_properties.find(property);
99138
100 if (property_iterator != fake_device_iterator->second.input_properties.end())139 if (property_iterator != device->input_properties.end())
101 {140 {
102 return property_iterator->second;141 return property_iterator->second;
103 }142 }
@@ -112,30 +151,71 @@
112 }151 }
113}152}
114153
115status_t mia::FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,154status_t FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
116 int32_t* outKeycode, uint32_t* outFlags) const155 int32_t* outKeycode, uint32_t* outFlags) const
117{156{
118 (void)deviceId;157 const FakeDevice* device = getDevice(deviceId);
119 (void)usageCode;158 if (device)
120 keymap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags);159 {
121 return droidinput::OK;160 const KeyInfo* key = getKey(device, scanCode, usageCode);
122}161 if (key)
123162 {
124status_t mia::FakeEventHub::mapAxis(int32_t deviceId, int32_t scanCode,163 if (outKeycode)
164 {
165 *outKeycode = key->keyCode;
166 }
167 if (outFlags)
168 {
169 *outFlags = key->flags;
170 }
171 return OK;
172 }
173 return NAME_NOT_FOUND;
174 }
175 else
176 {
177 keymap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags);
178 return droidinput::OK;
179 }
180}
181
182const FakeEventHub::KeyInfo* FakeEventHub::getKey(const FakeDevice* device,
183 int32_t scanCode, int32_t usageCode) const
184{
185 if (usageCode)
186 {
187 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
188 if (index >= 0)
189 {
190 return &device->keysByUsageCode.valueAt(index);
191 }
192 }
193 if (scanCode)
194 {
195 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
196 if (index >= 0)
197 {
198 return &device->keysByScanCode.valueAt(index);
199 }
200 }
201 return NULL;
202}
203
204status_t FakeEventHub::mapAxis(int32_t deviceId, int32_t scanCode,
125 AxisInfo* outAxisInfo) const205 AxisInfo* outAxisInfo) const
126{206{
127 (void)deviceId;207 (void)deviceId;
128 (void)scanCode;208 (void)scanCode;
129 (void)outAxisInfo;209 (void)outAxisInfo;
130 return -1;210 return NAME_NOT_FOUND;
131}211}
132212
133void mia::FakeEventHub::setExcludedDevices(const Vector<String8>& devices)213void FakeEventHub::setExcludedDevices(const Vector<String8>& devices)
134{214{
135 (void)devices;215 excluded_devices = devices;
136}216}
137217
138size_t mia::FakeEventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize)218size_t FakeEventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize)
139{219{
140 size_t num_events_obtained = 0;220 size_t num_events_obtained = 0;
141 (void) timeoutMillis;221 (void) timeoutMillis;
@@ -155,82 +235,145 @@
155 return num_events_obtained;235 return num_events_obtained;
156}236}
157237
158int32_t mia::FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const238int32_t FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const
159{239{
160 (void)deviceId;240 const FakeDevice* device = getDevice(deviceId);
161 (void)scanCode;241 if (device)
162 return AKEY_STATE_UNKNOWN;242 {
163}243 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
164244 if (index >= 0)
165int32_t mia::FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const245 {
166{246 return device->scanCodeStates.valueAt(index);
167 (void)deviceId;247 }
168 (void)keyCode;248 }
169 return AKEY_STATE_UNKNOWN;249 return AKEY_STATE_UNKNOWN;
170}250}
171251
172int32_t mia::FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const252int32_t FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const
173{253{
174 (void)deviceId;254 const FakeDevice* device = getDevice(deviceId);
175 (void)sw;255 if (device)
176 return AKEY_STATE_UNKNOWN;256 {
177}257 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
178258 if (index >= 0)
179status_t mia::FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis,259 {
260 return device->keyCodeStates.valueAt(index);
261 }
262 }
263 return AKEY_STATE_UNKNOWN;
264}
265
266int32_t FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const
267{
268 const FakeDevice* device = getDevice(deviceId);
269 if (device)
270 {
271 ssize_t index = device->switchStates.indexOfKey(sw);
272 if (index >= 0)
273 {
274 return device->switchStates.valueAt(index);
275 }
276 }
277 return AKEY_STATE_UNKNOWN;
278}
279
280status_t FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
180 int32_t* outValue) const281 int32_t* outValue) const
181{282{
182 (void)deviceId;283 const FakeDevice* device = getDevice(deviceId);
183 (void)axis;284 if (device)
184 (void)outValue;285 {
286 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
287 if (index >= 0)
288 {
289 *outValue = device->absoluteAxisValue.valueAt(index);
290 return OK;
291 }
292 }
293 *outValue = 0;
185 return -1;294 return -1;
186}295}
187296
188bool mia::FakeEventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,297bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
189 const int32_t* keyCodes,298 const int32_t* keyCodes,
190 uint8_t* outFlags) const299 uint8_t* outFlags) const
191{300{
192 (void)deviceId;301 bool result = false;
193 (void)numCodes;302 const FakeDevice* device = getDevice(deviceId);
194 (void)keyCodes;303 if (device)
195 (void)outFlags;304 {
196 return true;305 for (size_t i = 0; i < numCodes; i++)
197}306 {
198307 for (size_t j = 0; j < device->keysByScanCode.size(); j++)
199bool mia::FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const308 {
200{309 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode)
201 (void)deviceId;310 {
202 (void)scanCode;311 outFlags[i] = 1;
203 return false;312 result = true;
204}313 }
205314 }
206bool mia::FakeEventHub::hasLed(int32_t deviceId, int32_t led) const315 for (size_t j = 0; j < device->keysByUsageCode.size(); j++)
207{316 {
208 (void)deviceId;317 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode)
209 (void)led;318 {
210 return false;319 outFlags[i] = 1;
211}320 result = true;
212321 }
213void mia::FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on)322 }
214{323 }
215 (void)deviceId;324 }
216 (void)led;325 return result;
217 (void)on;326}
218}327
219328bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const
220void mia::FakeEventHub::getVirtualKeyDefinitions(int32_t deviceId,329{
330 const FakeDevice* device = getDevice(deviceId);
331 if (device)
332 {
333 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
334 return index >= 0;
335 }
336 return false;
337}
338
339bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const
340{
341 const FakeDevice* device = getDevice(deviceId);
342 return device && device->leds.indexOfKey(led) >= 0;
343}
344
345void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on)
346{
347 FakeDevice* device = getDevice(deviceId);
348 if (device)
349 {
350 ssize_t index = device->leds.indexOfKey(led);
351 if (index >= 0)
352 {
353 device->leds.replaceValueAt(led, on);
354 }
355 }
356}
357
358void FakeEventHub::getVirtualKeyDefinitions(int32_t deviceId,
221 Vector<VirtualKeyDefinition>& outVirtualKeys) const359 Vector<VirtualKeyDefinition>& outVirtualKeys) const
222{360{
223 (void)deviceId;361 outVirtualKeys.clear();
224 (void)outVirtualKeys;362
363 const FakeDevice* device = getDevice(deviceId);
364 if (device)
365 {
366 outVirtualKeys.appendVector(device->virtualKeys);
367 }
225}368}
226369
227sp<KeyCharacterMap> mia::FakeEventHub::getKeyCharacterMap(int32_t deviceId) const370sp<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t deviceId) const
228{371{
229 (void)deviceId;372 (void)deviceId;
230 return sp<KeyCharacterMap>();373 return sp<KeyCharacterMap>();
231}374}
232375
233bool mia::FakeEventHub::setKeyboardLayoutOverlay(int32_t deviceId,376bool FakeEventHub::setKeyboardLayoutOverlay(int32_t deviceId,
234 const sp<KeyCharacterMap>& map)377 const sp<KeyCharacterMap>& map)
235{378{
236 (void)deviceId;379 (void)deviceId;
@@ -238,39 +381,39 @@
238 return true;381 return true;
239}382}
240383
241void mia::FakeEventHub::vibrate(int32_t deviceId, nsecs_t duration)384void FakeEventHub::vibrate(int32_t deviceId, nsecs_t duration)
242{385{
243 (void)deviceId;386 (void)deviceId;
244 (void)duration;387 (void)duration;
245}388}
246389
247void mia::FakeEventHub::cancelVibrate(int32_t deviceId)390void FakeEventHub::cancelVibrate(int32_t deviceId)
248{391{
249 (void)deviceId;392 (void)deviceId;
250}393}
251394
252void mia::FakeEventHub::requestReopenDevices()395void FakeEventHub::requestReopenDevices()
253{396{
254}397}
255398
256void mia::FakeEventHub::wake()399void FakeEventHub::wake()
257{400{
258}401}
259402
260void mia::FakeEventHub::dump(droidinput::String8& dump)403void FakeEventHub::dump(droidinput::String8& dump)
261{404{
262 (void)dump;405 (void)dump;
263}406}
264407
265void mia::FakeEventHub::monitor()408void FakeEventHub::monitor()
266{409{
267}410}
268411
269void mia::FakeEventHub::flush()412void FakeEventHub::flush()
270{413{
271}414}
272415
273void mia::FakeEventHub::synthesize_builtin_keyboard_added()416void FakeEventHub::synthesize_builtin_keyboard_added()
274{417{
275 RawEvent event;418 RawEvent event;
276 event.when = 0;419 event.when = 0;
@@ -281,7 +424,7 @@
281 events_available.push_back(event);424 events_available.push_back(event);
282}425}
283426
284void mia::FakeEventHub::synthesize_builtin_cursor_added()427void FakeEventHub::synthesize_builtin_cursor_added()
285{428{
286 RawEvent event;429 RawEvent event;
287 event.when = 0;430 event.when = 0;
@@ -292,7 +435,7 @@
292 events_available.push_back(event);435 events_available.push_back(event);
293}436}
294437
295void mia::FakeEventHub::synthesize_device_scan_complete()438void FakeEventHub::synthesize_device_scan_complete()
296{439{
297 RawEvent event;440 RawEvent event;
298 event.when = 0;441 event.when = 0;
@@ -302,7 +445,7 @@
302 events_available.push_back(event);445 events_available.push_back(event);
303}446}
304447
305void mia::FakeEventHub::synthesize_event(const mis::KeyParameters &parameters)448void FakeEventHub::synthesize_event(const mis::KeyParameters &parameters)
306{449{
307 RawEvent event;450 RawEvent event;
308 event.when = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();451 event.when = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
@@ -323,7 +466,7 @@
323 events_available.push_back(event);466 events_available.push_back(event);
324}467}
325468
326void mia::FakeEventHub::synthesize_event(const mis::ButtonParameters &parameters)469void FakeEventHub::synthesize_event(const mis::ButtonParameters &parameters)
327{470{
328 RawEvent event;471 RawEvent event;
329 event.when = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();472 event.when = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
@@ -349,7 +492,7 @@
349 events_available.push_back(event);492 events_available.push_back(event);
350}493}
351494
352void mia::FakeEventHub::synthesize_event(const mis::MotionParameters &parameters)495void FakeEventHub::synthesize_event(const mis::MotionParameters &parameters)
353{496{
354 RawEvent event;497 RawEvent event;
355 event.when = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();498 event.when = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
@@ -373,3 +516,175 @@
373 event.code = SYN_REPORT;516 event.code = SYN_REPORT;
374 events_available.push_back(event);517 events_available.push_back(event);
375}518}
519
520void FakeEventHub::synthesize_event(nsecs_t when, int32_t device_id, int32_t type, int32_t code, int32_t value)
521{
522 RawEvent event;
523 event.when = when;
524 event.deviceId = device_id;
525 event.type = type;
526 event.code = code;
527 event.value = value;
528
529 {
530 std::lock_guard<std::mutex> lg(guard);
531 events_available.push_back(event);
532 }
533
534 if (type == EV_ABS)
535 {
536 setAbsoluteAxisValue(device_id, code, value);
537 }
538}
539
540void FakeEventHub::addDevice(int32_t deviceId, const std::string& name, uint32_t classes)
541{
542 FakeDevice device;
543 device.classes = classes;
544 device.identifier.name = name;
545 device_from_id.insert(std::pair<int32_t, FakeDevice>(deviceId, device));
546
547 synthesize_event(arbitrary_time, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
548}
549
550void FakeEventHub::removeDevice(int32_t device_id)
551{
552 device_from_id.erase(device_id);
553
554 synthesize_event(arbitrary_time, device_id, EventHubInterface::DEVICE_REMOVED, 0, 0);
555}
556
557void FakeEventHub::finishDeviceScan()
558{
559 synthesize_event(arbitrary_time, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
560}
561
562void FakeEventHub::addConfigurationProperty(int32_t device_id, const std::string& key, const std::string& value)
563{
564 FakeDevice& device = device_from_id.at(device_id);
565 device.configuration.addProperty(key, value);
566}
567
568void FakeEventHub::addConfigurationMap(int32_t device_id, const PropertyMap* configuration)
569{
570 FakeDevice& device = device_from_id.at(device_id);
571 device.configuration.addAll(configuration);
572}
573
574void FakeEventHub::addAbsoluteAxis(int32_t device_id, int axis,
575 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution)
576{
577 FakeDevice& device = device_from_id.at(device_id);
578
579 RawAbsoluteAxisInfo info;
580 info.valid = true;
581 info.minValue = minValue;
582 info.maxValue = maxValue;
583 info.flat = flat;
584 info.fuzz = fuzz;
585 info.resolution = resolution;
586 device.absoluteAxes.add(axis, info);
587}
588
589void FakeEventHub::addRelativeAxis(int32_t device_id, int32_t axis)
590{
591 FakeDevice& device = device_from_id.at(device_id);
592 device.relativeAxes.add(axis, true);
593}
594
595void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state)
596{
597 FakeDevice& device = device_from_id.at(deviceId);
598 device.keyCodeStates.replaceValueFor(keyCode, state);
599}
600
601void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state)
602{
603 FakeDevice& device = device_from_id.at(deviceId);
604 device.scanCodeStates.replaceValueFor(scanCode, state);
605}
606
607void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state)
608{
609 FakeDevice& device = device_from_id.at(deviceId);
610 device.switchStates.replaceValueFor(switchCode, state);
611}
612
613void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value)
614{
615 FakeDevice& device = device_from_id.at(deviceId);
616 device.absoluteAxisValue.replaceValueFor(axis, value);
617}
618
619void FakeEventHub::addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
620 int32_t keyCode, uint32_t flags)
621{
622 FakeDevice& device = device_from_id.at(deviceId);
623 KeyInfo info;
624 info.keyCode = keyCode;
625 info.flags = flags;
626 if (scanCode)
627 {
628 device.keysByScanCode.add(scanCode, info);
629 }
630 if (usageCode)
631 {
632 device.keysByUsageCode.add(usageCode, info);
633 }
634}
635
636void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState)
637{
638 FakeDevice& device = device_from_id.at(deviceId);
639 device.leds.add(led, initialState);
640}
641
642bool FakeEventHub::getLedState(int32_t deviceId, int32_t led)
643{
644 FakeDevice& device = device_from_id.at(deviceId);
645 return device.leds.valueFor(led);
646}
647
648Vector<std::string>& FakeEventHub::getExcludedDevices()
649{
650 return excluded_devices;
651}
652
653void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition)
654{
655 FakeDevice& device = device_from_id.at(deviceId);
656 device.virtualKeys.push(definition);
657}
658
659FakeEventHub::FakeDevice* FakeEventHub::getDevice(int32_t deviceId)
660{
661 auto fake_device_iterator = device_from_id.find(deviceId);
662
663 if (fake_device_iterator != device_from_id.end())
664 {
665 return &(fake_device_iterator->second);
666 }
667 else
668 {
669 return nullptr;
670 }
671}
672
673const FakeEventHub::FakeDevice* FakeEventHub::getDevice(int32_t deviceId) const
674{
675 auto fake_device_iterator = device_from_id.find(deviceId);
676
677 if (fake_device_iterator != device_from_id.end())
678 {
679 return &(fake_device_iterator->second);
680 }
681 else
682 {
683 return nullptr;
684 }
685}
686
687size_t FakeEventHub::eventsQueueSize() const
688{
689 return events_available.size();
690}
376691
=== modified file 'tests/unit-tests/android_input/input_reader.cpp'
--- tests/unit-tests/android_input/input_reader.cpp 2013-09-26 08:25:01 +0000
+++ tests/unit-tests/android_input/input_reader.cpp 2013-10-02 11:08:26 +0000
@@ -1,4 +1,5 @@
1/*1/*
2 * Copyright (C) 2013 Canonical Ltd.
2 * Copyright (C) 2010 The Android Open Source Project3 * Copyright (C) 2010 The Android Open Source Project
3 *4 *
4 * Licensed under the Apache License, Version 2.0 (the "License");5 * Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,10 +26,12 @@
2526
26#include <mir/logging/logger.h>27#include <mir/logging/logger.h>
27#include <mir/logging/input_report.h>28#include <mir/logging/input_report.h>
29#include <mir_test/fake_event_hub.h>
2830
29namespace ml = mir::logging;31namespace ml = mir::logging;
3032
31using std::string;33using std::string;
34using mir::input::android::FakeEventHub;
3235
33namespace36namespace
34{37{
@@ -298,415 +301,6 @@
298 }301 }
299};302};
300303
301
302// --- FakeEventHub ---
303
304class FakeEventHub : public EventHubInterface {
305 struct KeyInfo {
306 int32_t keyCode;
307 uint32_t flags;
308 };
309
310 struct Device {
311 InputDeviceIdentifier identifier;
312 uint32_t classes;
313 PropertyMap configuration;
314 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
315 KeyedVector<int, bool> relativeAxes;
316 KeyedVector<int32_t, int32_t> keyCodeStates;
317 KeyedVector<int32_t, int32_t> scanCodeStates;
318 KeyedVector<int32_t, int32_t> switchStates;
319 KeyedVector<int32_t, int32_t> absoluteAxisValue;
320 KeyedVector<int32_t, KeyInfo> keysByScanCode;
321 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
322 KeyedVector<int32_t, bool> leds;
323 Vector<VirtualKeyDefinition> virtualKeys;
324
325 Device(uint32_t classes) :
326 classes(classes) {
327 }
328 };
329
330 KeyedVector<int32_t, Device*> mDevices;
331 Vector<String8> mExcludedDevices;
332 List<RawEvent> mEvents;
333
334protected:
335 virtual ~FakeEventHub() {
336 for (size_t i = 0; i < mDevices.size(); i++) {
337 delete mDevices.valueAt(i);
338 }
339 }
340
341public:
342 FakeEventHub() { }
343
344 void addDevice(int32_t deviceId, const String8& name, uint32_t classes) {
345 Device* device = new Device(classes);
346 device->identifier.name = name;
347 mDevices.add(deviceId, device);
348
349 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
350 }
351
352 void removeDevice(int32_t deviceId) {
353 delete mDevices.valueFor(deviceId);
354 mDevices.removeItem(deviceId);
355
356 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
357 }
358
359 void finishDeviceScan() {
360 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
361 }
362
363 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
364 Device* device = getDevice(deviceId);
365 device->configuration.addProperty(key, value);
366 }
367
368 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
369 Device* device = getDevice(deviceId);
370 device->configuration.addAll(configuration);
371 }
372
373 void addAbsoluteAxis(int32_t deviceId, int axis,
374 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
375 Device* device = getDevice(deviceId);
376
377 RawAbsoluteAxisInfo info;
378 info.valid = true;
379 info.minValue = minValue;
380 info.maxValue = maxValue;
381 info.flat = flat;
382 info.fuzz = fuzz;
383 info.resolution = resolution;
384 device->absoluteAxes.add(axis, info);
385 }
386
387 void addRelativeAxis(int32_t deviceId, int32_t axis) {
388 Device* device = getDevice(deviceId);
389 device->relativeAxes.add(axis, true);
390 }
391
392 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
393 Device* device = getDevice(deviceId);
394 device->keyCodeStates.replaceValueFor(keyCode, state);
395 }
396
397 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
398 Device* device = getDevice(deviceId);
399 device->scanCodeStates.replaceValueFor(scanCode, state);
400 }
401
402 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
403 Device* device = getDevice(deviceId);
404 device->switchStates.replaceValueFor(switchCode, state);
405 }
406
407 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
408 Device* device = getDevice(deviceId);
409 device->absoluteAxisValue.replaceValueFor(axis, value);
410 }
411
412 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
413 int32_t keyCode, uint32_t flags) {
414 Device* device = getDevice(deviceId);
415 KeyInfo info;
416 info.keyCode = keyCode;
417 info.flags = flags;
418 if (scanCode) {
419 device->keysByScanCode.add(scanCode, info);
420 }
421 if (usageCode) {
422 device->keysByUsageCode.add(usageCode, info);
423 }
424 }
425
426 void addLed(int32_t deviceId, int32_t led, bool initialState) {
427 Device* device = getDevice(deviceId);
428 device->leds.add(led, initialState);
429 }
430
431 bool getLedState(int32_t deviceId, int32_t led) {
432 Device* device = getDevice(deviceId);
433 return device->leds.valueFor(led);
434 }
435
436 Vector<String8>& getExcludedDevices() {
437 return mExcludedDevices;
438 }
439
440 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
441 Device* device = getDevice(deviceId);
442 device->virtualKeys.push(definition);
443 }
444
445 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
446 int32_t code, int32_t value) {
447 RawEvent event;
448 event.when = when;
449 event.deviceId = deviceId;
450 event.type = type;
451 event.code = code;
452 event.value = value;
453 mEvents.push_back(event);
454
455 if (type == EV_ABS) {
456 setAbsoluteAxisValue(deviceId, code, value);
457 }
458 }
459
460 void assertQueueIsEmpty() {
461 ASSERT_EQ(size_t(0), mEvents.size())
462 << "Expected the event queue to be empty (fully consumed).";
463 }
464
465private:
466 Device* getDevice(int32_t deviceId) const {
467 ssize_t index = mDevices.indexOfKey(deviceId);
468 return index >= 0 ? mDevices.valueAt(index) : NULL;
469 }
470
471 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
472 Device* device = getDevice(deviceId);
473 return device ? device->classes : 0;
474 }
475
476 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
477 Device* device = getDevice(deviceId);
478 return device ? device->identifier : InputDeviceIdentifier();
479 }
480
481 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
482 Device* device = getDevice(deviceId);
483 if (device) {
484 *outConfiguration = device->configuration;
485 }
486 }
487
488 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
489 RawAbsoluteAxisInfo* outAxisInfo) const {
490 outAxisInfo->clear();
491 Device* device = getDevice(deviceId);
492 if (device) {
493 ssize_t index = device->absoluteAxes.indexOfKey(axis);
494 if (index >= 0) {
495 *outAxisInfo = device->absoluteAxes.valueAt(index);
496 return OK;
497 }
498 }
499 return -1;
500 }
501
502 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
503 Device* device = getDevice(deviceId);
504 if (device) {
505 return device->relativeAxes.indexOfKey(axis) >= 0;
506 }
507 return false;
508 }
509
510 bool hasInputProperty(int32_t deviceId, int property) const override {
511 (void)deviceId;
512 (void)property;
513 return false;
514 }
515
516 virtual status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
517 int32_t* outKeycode, uint32_t* outFlags) const {
518 Device* device = getDevice(deviceId);
519 if (device) {
520 const KeyInfo* key = getKey(device, scanCode, usageCode);
521 if (key) {
522 if (outKeycode) {
523 *outKeycode = key->keyCode;
524 }
525 if (outFlags) {
526 *outFlags = key->flags;
527 }
528 return OK;
529 }
530 }
531 return NAME_NOT_FOUND;
532 }
533
534 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
535 if (usageCode) {
536 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
537 if (index >= 0) {
538 return &device->keysByUsageCode.valueAt(index);
539 }
540 }
541 if (scanCode) {
542 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
543 if (index >= 0) {
544 return &device->keysByScanCode.valueAt(index);
545 }
546 }
547 return NULL;
548 }
549
550 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
551 (void)deviceId;
552 (void)scanCode;
553 (void)outAxisInfo;
554 return NAME_NOT_FOUND;
555 }
556
557 virtual void setExcludedDevices(const Vector<String8>& devices) {
558 mExcludedDevices = devices;
559 }
560
561 size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override {
562 (void)timeoutMillis;
563 (void)bufferSize;
564
565 if (mEvents.empty()) {
566 return 0;
567 }
568
569 *buffer = *mEvents.begin();
570 mEvents.erase(mEvents.begin());
571 return 1;
572 }
573
574 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
575 Device* device = getDevice(deviceId);
576 if (device) {
577 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
578 if (index >= 0) {
579 return device->scanCodeStates.valueAt(index);
580 }
581 }
582 return AKEY_STATE_UNKNOWN;
583 }
584
585 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
586 Device* device = getDevice(deviceId);
587 if (device) {
588 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
589 if (index >= 0) {
590 return device->keyCodeStates.valueAt(index);
591 }
592 }
593 return AKEY_STATE_UNKNOWN;
594 }
595
596 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
597 Device* device = getDevice(deviceId);
598 if (device) {
599 ssize_t index = device->switchStates.indexOfKey(sw);
600 if (index >= 0) {
601 return device->switchStates.valueAt(index);
602 }
603 }
604 return AKEY_STATE_UNKNOWN;
605 }
606
607 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
608 int32_t* outValue) const {
609 Device* device = getDevice(deviceId);
610 if (device) {
611 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
612 if (index >= 0) {
613 *outValue = device->absoluteAxisValue.valueAt(index);
614 return OK;
615 }
616 }
617 *outValue = 0;
618 return -1;
619 }
620
621 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
622 uint8_t* outFlags) const {
623 bool result = false;
624 Device* device = getDevice(deviceId);
625 if (device) {
626 for (size_t i = 0; i < numCodes; i++) {
627 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
628 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
629 outFlags[i] = 1;
630 result = true;
631 }
632 }
633 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
634 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
635 outFlags[i] = 1;
636 result = true;
637 }
638 }
639 }
640 }
641 return result;
642 }
643
644 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
645 Device* device = getDevice(deviceId);
646 if (device) {
647 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
648 return index >= 0;
649 }
650 return false;
651 }
652
653 virtual bool hasLed(int32_t deviceId, int32_t led) const {
654 Device* device = getDevice(deviceId);
655 return device && device->leds.indexOfKey(led) >= 0;
656 }
657
658 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
659 Device* device = getDevice(deviceId);
660 if (device) {
661 ssize_t index = device->leds.indexOfKey(led);
662 if (index >= 0) {
663 device->leds.replaceValueAt(led, on);
664 } else {
665 ADD_FAILURE()
666 << "Attempted to set the state of an LED that the EventHub declared "
667 "was not present. led=" << led;
668 }
669 }
670 }
671
672 void getVirtualKeyDefinitions(int32_t deviceId,
673 Vector<VirtualKeyDefinition>& outVirtualKeys) const override {
674 outVirtualKeys.clear();
675
676 Device* device = getDevice(deviceId);
677 if (device) {
678 outVirtualKeys.appendVector(device->virtualKeys);
679 }
680 }
681
682 sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
683 (void)deviceId;
684 return NULL;
685 }
686
687 bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) override {
688 (void)deviceId;
689 (void)map;
690 return false;
691 }
692
693 void vibrate(int32_t /*deviceId*/, nsecs_t /*duration*/) override {}
694
695 void cancelVibrate(int32_t /*deviceId*/) override {}
696
697 void wake() override {}
698
699 void dump(String8& /*dump*/) override {}
700
701 void monitor() override {}
702
703 void flush() override {}
704
705 void requestReopenDevices() override {}
706
707};
708
709
710// --- FakeInputReaderContext ---304// --- FakeInputReaderContext ---
711305
712class FakeInputReaderContext : public InputReaderContext {306class FakeInputReaderContext : public InputReaderContext {
@@ -1001,7 +595,8 @@
1001 mFakeEventHub->finishDeviceScan();595 mFakeEventHub->finishDeviceScan();
1002 mReader->loopOnce();596 mReader->loopOnce();
1003 mReader->loopOnce();597 mReader->loopOnce();
1004 mFakeEventHub->assertQueueIsEmpty();598 ASSERT_EQ(size_t(0), mFakeEventHub->eventsQueueSize())
599 << "Expected the event queue to be empty (fully consumed).";
1005 }600 }
1006601
1007 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId,602 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId,
@@ -1017,16 +612,16 @@
1017};612};
1018613
1019TEST_F(InputReaderTest, GetInputDevices) {614TEST_F(InputReaderTest, GetInputDevices) {
1020 ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),615 ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("keyboard"),
1021 INPUT_DEVICE_CLASS_KEYBOARD, NULL));616 INPUT_DEVICE_CLASS_KEYBOARD, NULL));
1022 ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("ignored"),617 ASSERT_NO_FATAL_FAILURE(addDevice(3, String8("ignored"),
1023 0, NULL)); // no classes so device will be ignored618 0, NULL)); // no classes so device will be ignored
1024619
1025 Vector<InputDeviceInfo> inputDevices;620 Vector<InputDeviceInfo> inputDevices;
1026 mReader->getInputDevices(inputDevices);621 mReader->getInputDevices(inputDevices);
1027622
1028 ASSERT_EQ(1U, inputDevices.size());623 ASSERT_EQ(1U, inputDevices.size());
1029 ASSERT_EQ(1, inputDevices[0].getId());624 ASSERT_EQ(2, inputDevices[0].getId());
1030 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());625 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
1031 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());626 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1032 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());627 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
@@ -1035,7 +630,7 @@
1035 // Should also have received a notification describing the new input devices.630 // Should also have received a notification describing the new input devices.
1036 inputDevices = mFakePolicy->getInputDevices();631 inputDevices = mFakePolicy->getInputDevices();
1037 ASSERT_EQ(1U, inputDevices.size());632 ASSERT_EQ(1U, inputDevices.size());
1038 ASSERT_EQ(1, inputDevices[0].getId());633 ASSERT_EQ(2, inputDevices[0].getId());
1039 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());634 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
1040 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());635 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1041 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());636 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
@@ -1159,7 +754,7 @@
1159}754}
1160755
1161TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {756TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
1162 addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);757 addDevice(2, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);
1163758
1164 NotifyConfigurationChangedArgs args{0};759 NotifyConfigurationChangedArgs args{0};
1165760
@@ -1172,9 +767,9 @@
1172 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),767 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
1173 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));768 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1174769
1175 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);770 mFakeEventHub->synthesize_event(0, 1, EV_KEY, KEY_A, 1);
1176 mReader->loopOnce();771 mReader->loopOnce();
1177 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());772 EXPECT_EQ(size_t(0), mFakeEventHub->eventsQueueSize());
1178773
1179 RawEvent event;774 RawEvent event;
1180 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));775 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
@@ -1227,7 +822,7 @@
1227};822};
1228823
1229const char* InputDeviceTest::DEVICE_NAME = "device";824const char* InputDeviceTest::DEVICE_NAME = "device";
1230const int32_t InputDeviceTest::DEVICE_ID = 1;825const int32_t InputDeviceTest::DEVICE_ID = 2;
1231const int32_t InputDeviceTest::DEVICE_GENERATION = 2;826const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1232const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD827const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1233 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;828 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
@@ -1479,7 +1074,7 @@
1479};1074};
14801075
1481const char* InputMapperTest::DEVICE_NAME = "device";1076const char* InputMapperTest::DEVICE_NAME = "device";
1482const int32_t InputMapperTest::DEVICE_ID = 1;1077const int32_t InputMapperTest::DEVICE_ID = 2;
1483const int32_t InputMapperTest::DEVICE_GENERATION = 2;1078const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1484const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests1079const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
14851080

Subscribers

People subscribed via source and target branches