1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "InputCommonConverter.h"
18 
19 using namespace ::aidl::android::hardware::input;
20 
21 namespace android {
22 
23 const static ui::Transform kIdentityTransform;
24 const static std::array<uint8_t, 32> kInvalidHmac{};
25 
getSource(uint32_t source)26 static common::Source getSource(uint32_t source) {
27     static_assert(static_cast<common::Source>(AINPUT_SOURCE_UNKNOWN) == common::Source::UNKNOWN,
28                   "SOURCE_UNKNOWN mismatch");
29     static_assert(static_cast<common::Source>(AINPUT_SOURCE_KEYBOARD) == common::Source::KEYBOARD,
30                   "SOURCE_KEYBOARD mismatch");
31     static_assert(static_cast<common::Source>(AINPUT_SOURCE_DPAD) == common::Source::DPAD,
32                   "SOURCE_DPAD mismatch");
33     static_assert(static_cast<common::Source>(AINPUT_SOURCE_GAMEPAD) == common::Source::GAMEPAD,
34                   "SOURCE_GAMEPAD mismatch");
35     static_assert(static_cast<common::Source>(AINPUT_SOURCE_TOUCHSCREEN) ==
36                           common::Source::TOUCHSCREEN,
37                   "SOURCE_TOUCHSCREEN mismatch");
38     static_assert(static_cast<common::Source>(AINPUT_SOURCE_MOUSE) == common::Source::MOUSE,
39                   "SOURCE_MOUSE mismatch");
40     static_assert(static_cast<common::Source>(AINPUT_SOURCE_STYLUS) == common::Source::STYLUS,
41                   "SOURCE_STYLUS mismatch");
42     static_assert(static_cast<common::Source>(AINPUT_SOURCE_BLUETOOTH_STYLUS) ==
43                           common::Source::BLUETOOTH_STYLUS,
44                   "SOURCE_BLUETOOTH_STYLUS mismatch");
45     static_assert(static_cast<common::Source>(AINPUT_SOURCE_TRACKBALL) == common::Source::TRACKBALL,
46                   "SOURCE_TRACKBALL mismatch");
47     static_assert(static_cast<common::Source>(AINPUT_SOURCE_MOUSE_RELATIVE) ==
48                           common::Source::MOUSE_RELATIVE,
49                   "SOURCE_MOUSE_RELATIVE mismatch");
50     static_assert(static_cast<common::Source>(AINPUT_SOURCE_TOUCHPAD) == common::Source::TOUCHPAD,
51                   "SOURCE_TOUCHPAD mismatch");
52     static_assert(static_cast<common::Source>(AINPUT_SOURCE_TOUCH_NAVIGATION) ==
53                           common::Source::TOUCH_NAVIGATION,
54                   "SOURCE_TOUCH_NAVIGATION mismatch");
55     static_assert(static_cast<common::Source>(AINPUT_SOURCE_JOYSTICK) == common::Source::JOYSTICK,
56                   "SOURCE_JOYSTICK mismatch");
57     static_assert(static_cast<common::Source>(AINPUT_SOURCE_ROTARY_ENCODER) ==
58                           common::Source::ROTARY_ENCODER,
59                   "SOURCE_ROTARY_ENCODER mismatch");
60     static_assert(static_cast<common::Source>(AINPUT_SOURCE_HDMI) == common::Source::HDMI);
61     static_assert(static_cast<common::Source>(AINPUT_SOURCE_SENSOR) == common::Source::SENSOR);
62     static_assert(static_cast<common::Source>(AINPUT_SOURCE_ANY) == common::Source::ANY,
63                   "SOURCE_ANY mismatch");
64     return static_cast<common::Source>(source);
65 }
66 
getAction(int32_t actionMasked)67 static common::Action getAction(int32_t actionMasked) {
68     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_DOWN) == common::Action::DOWN,
69                   "ACTION_DOWN mismatch");
70     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_UP) == common::Action::UP,
71                   "ACTION_UP mismatch");
72     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_MOVE) == common::Action::MOVE,
73                   "ACTION_MOVE mismatch");
74     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_CANCEL) ==
75                           common::Action::CANCEL,
76                   "ACTION_CANCEL mismatch");
77     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_OUTSIDE) ==
78                           common::Action::OUTSIDE,
79                   "ACTION_OUTSIDE mismatch");
80     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_POINTER_DOWN) ==
81                           common::Action::POINTER_DOWN,
82                   "ACTION_POINTER_DOWN mismatch");
83     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_POINTER_UP) ==
84                           common::Action::POINTER_UP,
85                   "ACTION_POINTER_UP mismatch");
86     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_HOVER_MOVE) ==
87                           common::Action::HOVER_MOVE,
88                   "ACTION_HOVER_MOVE mismatch");
89     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_SCROLL) ==
90                           common::Action::SCROLL,
91                   "ACTION_SCROLL mismatch");
92     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_HOVER_ENTER) ==
93                           common::Action::HOVER_ENTER,
94                   "ACTION_HOVER_ENTER mismatch");
95     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_HOVER_EXIT) ==
96                           common::Action::HOVER_EXIT,
97                   "ACTION_HOVER_EXIT mismatch");
98     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_BUTTON_PRESS) ==
99                           common::Action::BUTTON_PRESS,
100                   "ACTION_BUTTON_PRESS mismatch");
101     static_assert(static_cast<common::Action>(AMOTION_EVENT_ACTION_BUTTON_RELEASE) ==
102                           common::Action::BUTTON_RELEASE,
103                   "ACTION_BUTTON_RELEASE mismatch");
104     return static_cast<common::Action>(actionMasked);
105 }
106 
getActionButton(int32_t actionButton)107 static common::Button getActionButton(int32_t actionButton) {
108     static_assert(static_cast<common::Button>(0) == common::Button::NONE, "BUTTON_NONE mismatch");
109     static_assert(static_cast<common::Button>(AMOTION_EVENT_BUTTON_PRIMARY) ==
110                           common::Button::PRIMARY,
111                   "BUTTON_PRIMARY mismatch");
112     static_assert(static_cast<common::Button>(AMOTION_EVENT_BUTTON_SECONDARY) ==
113                           common::Button::SECONDARY,
114                   "BUTTON_SECONDARY mismatch");
115     static_assert(static_cast<common::Button>(AMOTION_EVENT_BUTTON_TERTIARY) ==
116                           common::Button::TERTIARY,
117                   "BUTTON_TERTIARY mismatch");
118     static_assert(static_cast<common::Button>(AMOTION_EVENT_BUTTON_BACK) == common::Button::BACK,
119                   "BUTTON_BACK mismatch");
120     static_assert(static_cast<common::Button>(AMOTION_EVENT_BUTTON_FORWARD) ==
121                           common::Button::FORWARD,
122                   "BUTTON_FORWARD mismatch");
123     static_assert(static_cast<common::Button>(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) ==
124                           common::Button::STYLUS_PRIMARY,
125                   "BUTTON_STYLUS_PRIMARY mismatch");
126     static_assert(static_cast<common::Button>(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY) ==
127                           common::Button::STYLUS_SECONDARY,
128                   "BUTTON_STYLUS_SECONDARY mismatch");
129     return static_cast<common::Button>(actionButton);
130 }
131 
getFlags(int32_t flags)132 static common::Flag getFlags(int32_t flags) {
133     static_assert(static_cast<common::Flag>(AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED) ==
134                   common::Flag::WINDOW_IS_OBSCURED);
135     static_assert(static_cast<common::Flag>(AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE) ==
136                   common::Flag::IS_GENERATED_GESTURE);
137     static_assert(static_cast<common::Flag>(AMOTION_EVENT_FLAG_TAINTED) == common::Flag::TAINTED);
138     return static_cast<common::Flag>(flags);
139 }
140 
getPolicyFlags(int32_t flags)141 static common::PolicyFlag getPolicyFlags(int32_t flags) {
142     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_WAKE) == common::PolicyFlag::WAKE);
143     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_VIRTUAL) ==
144                   common::PolicyFlag::VIRTUAL);
145     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_FUNCTION) ==
146                   common::PolicyFlag::FUNCTION);
147     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_GESTURE) ==
148                   common::PolicyFlag::GESTURE);
149     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_INJECTED) ==
150                   common::PolicyFlag::INJECTED);
151     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_TRUSTED) ==
152                   common::PolicyFlag::TRUSTED);
153     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_FILTERED) ==
154                   common::PolicyFlag::FILTERED);
155     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_DISABLE_KEY_REPEAT) ==
156                   common::PolicyFlag::DISABLE_KEY_REPEAT);
157     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_INTERACTIVE) ==
158                   common::PolicyFlag::INTERACTIVE);
159     static_assert(static_cast<common::PolicyFlag>(POLICY_FLAG_PASS_TO_USER) ==
160                   common::PolicyFlag::PASS_TO_USER);
161     return static_cast<common::PolicyFlag>(flags);
162 }
163 
getEdgeFlags(int32_t flags)164 static common::EdgeFlag getEdgeFlags(int32_t flags) {
165     static_assert(static_cast<common::EdgeFlag>(AMOTION_EVENT_EDGE_FLAG_NONE) ==
166                   common::EdgeFlag::NONE);
167     static_assert(static_cast<common::EdgeFlag>(AMOTION_EVENT_EDGE_FLAG_TOP) ==
168                   common::EdgeFlag::TOP);
169     static_assert(static_cast<common::EdgeFlag>(AMOTION_EVENT_EDGE_FLAG_BOTTOM) ==
170                   common::EdgeFlag::BOTTOM);
171     static_assert(static_cast<common::EdgeFlag>(AMOTION_EVENT_EDGE_FLAG_LEFT) ==
172                   common::EdgeFlag::LEFT);
173     static_assert(static_cast<common::EdgeFlag>(AMOTION_EVENT_EDGE_FLAG_RIGHT) ==
174                   common::EdgeFlag::RIGHT);
175     return static_cast<common::EdgeFlag>(flags);
176 }
177 
getMetastate(int32_t state)178 static common::Meta getMetastate(int32_t state) {
179     static_assert(static_cast<common::Meta>(AMETA_NONE) == common::Meta::NONE);
180     static_assert(static_cast<common::Meta>(AMETA_ALT_ON) == common::Meta::ALT_ON);
181     static_assert(static_cast<common::Meta>(AMETA_ALT_LEFT_ON) == common::Meta::ALT_LEFT_ON);
182     static_assert(static_cast<common::Meta>(AMETA_ALT_RIGHT_ON) == common::Meta::ALT_RIGHT_ON);
183     static_assert(static_cast<common::Meta>(AMETA_SHIFT_ON) == common::Meta::SHIFT_ON);
184     static_assert(static_cast<common::Meta>(AMETA_SHIFT_LEFT_ON) == common::Meta::SHIFT_LEFT_ON);
185     static_assert(static_cast<common::Meta>(AMETA_SHIFT_RIGHT_ON) == common::Meta::SHIFT_RIGHT_ON);
186     static_assert(static_cast<common::Meta>(AMETA_SYM_ON) == common::Meta::SYM_ON);
187     static_assert(static_cast<common::Meta>(AMETA_FUNCTION_ON) == common::Meta::FUNCTION_ON);
188     static_assert(static_cast<common::Meta>(AMETA_CTRL_ON) == common::Meta::CTRL_ON);
189     static_assert(static_cast<common::Meta>(AMETA_CTRL_LEFT_ON) == common::Meta::CTRL_LEFT_ON);
190     static_assert(static_cast<common::Meta>(AMETA_CTRL_RIGHT_ON) == common::Meta::CTRL_RIGHT_ON);
191     static_assert(static_cast<common::Meta>(AMETA_META_ON) == common::Meta::META_ON);
192     static_assert(static_cast<common::Meta>(AMETA_META_LEFT_ON) == common::Meta::META_LEFT_ON);
193     static_assert(static_cast<common::Meta>(AMETA_META_RIGHT_ON) == common::Meta::META_RIGHT_ON);
194     static_assert(static_cast<common::Meta>(AMETA_CAPS_LOCK_ON) == common::Meta::CAPS_LOCK_ON);
195     static_assert(static_cast<common::Meta>(AMETA_NUM_LOCK_ON) == common::Meta::NUM_LOCK_ON);
196     static_assert(static_cast<common::Meta>(AMETA_SCROLL_LOCK_ON) == common::Meta::SCROLL_LOCK_ON);
197     return static_cast<common::Meta>(state);
198 }
199 
getButtonState(int32_t buttonState)200 static common::Button getButtonState(int32_t buttonState) {
201     // No need for static_assert here.
202     // The button values have already been asserted in getActionButton(..) above
203     return static_cast<common::Button>(buttonState);
204 }
205 
getToolType(ToolType toolType)206 static common::ToolType getToolType(ToolType toolType) {
207     static_assert(static_cast<common::ToolType>(ToolType::UNKNOWN) == common::ToolType::UNKNOWN);
208     static_assert(static_cast<common::ToolType>(ToolType::FINGER) == common::ToolType::FINGER);
209     static_assert(static_cast<common::ToolType>(ToolType::STYLUS) == common::ToolType::STYLUS);
210     static_assert(static_cast<common::ToolType>(ToolType::MOUSE) == common::ToolType::MOUSE);
211     static_assert(static_cast<common::ToolType>(ToolType::ERASER) == common::ToolType::ERASER);
212     return static_cast<common::ToolType>(toolType);
213 }
214 
215 // MotionEvent axes asserts
216 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_X) == common::Axis::X);
217 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_Y) == common::Axis::Y);
218 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_PRESSURE) == common::Axis::PRESSURE);
219 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_SIZE) == common::Axis::SIZE);
220 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_TOUCH_MAJOR) ==
221               common::Axis::TOUCH_MAJOR);
222 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_TOUCH_MINOR) ==
223               common::Axis::TOUCH_MINOR);
224 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_TOOL_MAJOR) == common::Axis::TOOL_MAJOR);
225 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_TOOL_MINOR) == common::Axis::TOOL_MINOR);
226 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_ORIENTATION) ==
227               common::Axis::ORIENTATION);
228 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_VSCROLL) == common::Axis::VSCROLL);
229 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_HSCROLL) == common::Axis::HSCROLL);
230 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_Z) == common::Axis::Z);
231 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_RX) == common::Axis::RX);
232 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_RY) == common::Axis::RY);
233 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_RZ) == common::Axis::RZ);
234 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_HAT_X) == common::Axis::HAT_X);
235 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_HAT_Y) == common::Axis::HAT_Y);
236 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_LTRIGGER) == common::Axis::LTRIGGER);
237 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_RTRIGGER) == common::Axis::RTRIGGER);
238 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_THROTTLE) == common::Axis::THROTTLE);
239 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_RUDDER) == common::Axis::RUDDER);
240 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_WHEEL) == common::Axis::WHEEL);
241 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GAS) == common::Axis::GAS);
242 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_BRAKE) == common::Axis::BRAKE);
243 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_DISTANCE) == common::Axis::DISTANCE);
244 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_TILT) == common::Axis::TILT);
245 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_SCROLL) == common::Axis::SCROLL);
246 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_RELATIVE_X) == common::Axis::RELATIVE_X);
247 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_RELATIVE_Y) == common::Axis::RELATIVE_Y);
248 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_1) == common::Axis::GENERIC_1);
249 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_2) == common::Axis::GENERIC_2);
250 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_3) == common::Axis::GENERIC_3);
251 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_4) == common::Axis::GENERIC_4);
252 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_5) == common::Axis::GENERIC_5);
253 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_6) == common::Axis::GENERIC_6);
254 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_7) == common::Axis::GENERIC_7);
255 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_8) == common::Axis::GENERIC_8);
256 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_9) == common::Axis::GENERIC_9);
257 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_10) == common::Axis::GENERIC_10);
258 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_11) == common::Axis::GENERIC_11);
259 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_12) == common::Axis::GENERIC_12);
260 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_13) == common::Axis::GENERIC_13);
261 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_14) == common::Axis::GENERIC_14);
262 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_15) == common::Axis::GENERIC_15);
263 static_assert(static_cast<common::Axis>(AMOTION_EVENT_AXIS_GENERIC_16) == common::Axis::GENERIC_16);
264 // TODO(b/251196347): add GESTURE_{X,Y}_OFFSET, GESTURE_SCROLL_{X,Y}_DISTANCE,
265 // GESTURE_PINCH_SCALE_FACTOR, and GESTURE_SWIPE_FINGER_COUNT.
266 // If you added a new axis, consider whether this should also be exposed as a HAL axis. Update the
267 // static_assert below and add the new axis here, or leave a comment summarizing your decision.
268 static_assert(static_cast<common::Axis>(AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE) ==
269               static_cast<common::Axis>(AMOTION_EVENT_AXIS_GESTURE_SWIPE_FINGER_COUNT));
270 
getHalVideoFrame(const TouchVideoFrame & frame)271 static common::VideoFrame getHalVideoFrame(const TouchVideoFrame& frame) {
272     common::VideoFrame out;
273     out.width = frame.getWidth();
274     out.height = frame.getHeight();
275     std::vector<char16_t> unsignedData(frame.getData().begin(), frame.getData().end());
276     out.data = unsignedData;
277     struct timeval timestamp = frame.getTimestamp();
278     out.timestamp = seconds_to_nanoseconds(timestamp.tv_sec) +
279             microseconds_to_nanoseconds(timestamp.tv_usec);
280     return out;
281 }
282 
convertVideoFrames(const std::vector<TouchVideoFrame> & frames)283 static std::vector<common::VideoFrame> convertVideoFrames(
284         const std::vector<TouchVideoFrame>& frames) {
285     std::vector<common::VideoFrame> out;
286     for (const TouchVideoFrame& frame : frames) {
287         out.push_back(getHalVideoFrame(frame));
288     }
289     return out;
290 }
291 
getHalPropertiesAndCoords(const NotifyMotionArgs & args,std::vector<common::PointerProperties> & outPointerProperties,std::vector<common::PointerCoords> & outPointerCoords)292 static void getHalPropertiesAndCoords(const NotifyMotionArgs& args,
293                                       std::vector<common::PointerProperties>& outPointerProperties,
294                                       std::vector<common::PointerCoords>& outPointerCoords) {
295     outPointerProperties.reserve(args.getPointerCount());
296     outPointerCoords.reserve(args.getPointerCount());
297     for (size_t i = 0; i < args.getPointerCount(); i++) {
298         common::PointerProperties properties;
299         properties.id = args.pointerProperties[i].id;
300         properties.toolType = getToolType(args.pointerProperties[i].toolType);
301         outPointerProperties.push_back(properties);
302 
303         common::PointerCoords coords;
304         // OK to copy bits because we have static_assert for pointerCoords axes
305         coords.bits = args.pointerCoords[i].bits;
306         coords.values = std::vector<float>(args.pointerCoords[i].values.cbegin(),
307                                            args.pointerCoords[i].values.cbegin() +
308                                                    BitSet64::count(args.pointerCoords[i].bits));
309         outPointerCoords.push_back(coords);
310     }
311 }
312 
notifyMotionArgsToHalMotionEvent(const NotifyMotionArgs & args)313 common::MotionEvent notifyMotionArgsToHalMotionEvent(const NotifyMotionArgs& args) {
314     common::MotionEvent event;
315     event.deviceId = args.deviceId;
316     event.source = getSource(args.source);
317     event.displayId = args.displayId.val();
318     event.downTime = args.downTime;
319     event.eventTime = args.eventTime;
320     event.deviceTimestamp = 0;
321     event.action = getAction(args.action & AMOTION_EVENT_ACTION_MASK);
322     event.actionIndex = MotionEvent::getActionIndex(args.action);
323     event.actionButton = getActionButton(args.actionButton);
324     event.flags = getFlags(args.flags);
325     event.policyFlags = getPolicyFlags(args.policyFlags);
326     event.edgeFlags = getEdgeFlags(args.edgeFlags);
327     event.metaState = getMetastate(args.metaState);
328     event.buttonState = getButtonState(args.buttonState);
329     event.xPrecision = args.xPrecision;
330     event.yPrecision = args.yPrecision;
331 
332     std::vector<common::PointerProperties> pointerProperties;
333     std::vector<common::PointerCoords> pointerCoords;
334     getHalPropertiesAndCoords(args, /*out*/ pointerProperties, /*out*/ pointerCoords);
335     event.pointerProperties = pointerProperties;
336     event.pointerCoords = pointerCoords;
337 
338     event.frames = convertVideoFrames(args.videoFrames);
339 
340     return event;
341 }
342 
toMotionEvent(const NotifyMotionArgs & args,const ui::Transform * transform,const ui::Transform * rawTransform,const std::array<uint8_t,32> * hmac)343 MotionEvent toMotionEvent(const NotifyMotionArgs& args, const ui::Transform* transform,
344                           const ui::Transform* rawTransform, const std::array<uint8_t, 32>* hmac) {
345     if (transform == nullptr) transform = &kIdentityTransform;
346     if (rawTransform == nullptr) rawTransform = &kIdentityTransform;
347     if (hmac == nullptr) hmac = &kInvalidHmac;
348 
349     MotionEvent event;
350     event.initialize(args.id, args.deviceId, args.source, args.displayId, *hmac, args.action,
351                      args.actionButton, args.flags, args.edgeFlags, args.metaState,
352                      args.buttonState, args.classification, *transform, args.xPrecision,
353                      args.yPrecision, args.xCursorPosition, args.yCursorPosition, *rawTransform,
354                      args.downTime, args.eventTime, args.getPointerCount(),
355                      args.pointerProperties.data(), args.pointerCoords.data());
356     return event;
357 }
358 
toKeyEvent(const NotifyKeyArgs & args,int32_t repeatCount,const std::array<uint8_t,32> * hmac)359 KeyEvent toKeyEvent(const NotifyKeyArgs& args, int32_t repeatCount,
360                     const std::array<uint8_t, 32>* hmac) {
361     if (hmac == nullptr) hmac = &kInvalidHmac;
362 
363     KeyEvent event;
364     event.initialize(args.id, args.deviceId, args.source, args.displayId, *hmac, args.action,
365                      args.flags, args.keyCode, args.scanCode, args.metaState, repeatCount,
366                      args.downTime, args.eventTime);
367     return event;
368 }
369 
370 } // namespace android
371