• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #define LOG_TAG "VirtualInputDevice"
18 
19 #include <android/input.h>
20 #include <android/keycodes.h>
21 #include <fcntl.h>
22 #include <input/Input.h>
23 #include <input/VirtualInputDevice.h>
24 #include <linux/uinput.h>
25 #include <math.h>
26 #include <utils/Log.h>
27 
28 #include <map>
29 #include <string>
30 
31 using android::base::unique_fd;
32 
33 /**
34  * Log debug messages about native virtual input devices.
35  * Enable this via "adb shell setprop log.tag.VirtualInputDevice DEBUG"
36  */
isDebug()37 static bool isDebug() {
38     return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);
39 }
40 
41 namespace android {
VirtualInputDevice(unique_fd fd)42 VirtualInputDevice::VirtualInputDevice(unique_fd fd) : mFd(std::move(fd)) {}
~VirtualInputDevice()43 VirtualInputDevice::~VirtualInputDevice() {
44     ioctl(mFd, UI_DEV_DESTROY);
45 }
46 
writeInputEvent(uint16_t type,uint16_t code,int32_t value,std::chrono::nanoseconds eventTime)47 bool VirtualInputDevice::writeInputEvent(uint16_t type, uint16_t code, int32_t value,
48                                          std::chrono::nanoseconds eventTime) {
49     std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(eventTime);
50     std::chrono::microseconds microseconds =
51             std::chrono::duration_cast<std::chrono::microseconds>(eventTime - seconds);
52     struct input_event ev = {.type = type, .code = code, .value = value};
53     ev.input_event_sec = static_cast<decltype(ev.input_event_sec)>(seconds.count());
54     ev.input_event_usec = static_cast<decltype(ev.input_event_usec)>(microseconds.count());
55 
56     return TEMP_FAILURE_RETRY(write(mFd, &ev, sizeof(struct input_event))) == sizeof(ev);
57 }
58 
59 /** Utility method to write keyboard key events or mouse button events. */
writeEvKeyEvent(int32_t androidCode,int32_t androidAction,const std::map<int,int> & evKeyCodeMapping,const std::map<int,UinputAction> & actionMapping,std::chrono::nanoseconds eventTime)60 bool VirtualInputDevice::writeEvKeyEvent(int32_t androidCode, int32_t androidAction,
61                                          const std::map<int, int>& evKeyCodeMapping,
62                                          const std::map<int, UinputAction>& actionMapping,
63                                          std::chrono::nanoseconds eventTime) {
64     auto evKeyCodeIterator = evKeyCodeMapping.find(androidCode);
65     if (evKeyCodeIterator == evKeyCodeMapping.end()) {
66         ALOGE("Unsupported native EV keycode for android code %d", androidCode);
67         return false;
68     }
69     auto actionIterator = actionMapping.find(androidAction);
70     if (actionIterator == actionMapping.end()) {
71         return false;
72     }
73     if (!writeInputEvent(EV_KEY, static_cast<uint16_t>(evKeyCodeIterator->second),
74                          static_cast<int32_t>(actionIterator->second), eventTime)) {
75         return false;
76     }
77     if (!writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime)) {
78         return false;
79     }
80     return true;
81 }
82 
83 // --- VirtualKeyboard ---
84 const std::map<int, UinputAction> VirtualKeyboard::KEY_ACTION_MAPPING = {
85         {AKEY_EVENT_ACTION_DOWN, UinputAction::PRESS},
86         {AKEY_EVENT_ACTION_UP, UinputAction::RELEASE},
87 };
88 // Keycode mapping from https://source.android.com/devices/input/keyboard-devices
89 const std::map<int, int> VirtualKeyboard::KEY_CODE_MAPPING = {
90         {AKEYCODE_0, KEY_0},
91         {AKEYCODE_1, KEY_1},
92         {AKEYCODE_2, KEY_2},
93         {AKEYCODE_3, KEY_3},
94         {AKEYCODE_4, KEY_4},
95         {AKEYCODE_5, KEY_5},
96         {AKEYCODE_6, KEY_6},
97         {AKEYCODE_7, KEY_7},
98         {AKEYCODE_8, KEY_8},
99         {AKEYCODE_9, KEY_9},
100         {AKEYCODE_A, KEY_A},
101         {AKEYCODE_B, KEY_B},
102         {AKEYCODE_C, KEY_C},
103         {AKEYCODE_D, KEY_D},
104         {AKEYCODE_E, KEY_E},
105         {AKEYCODE_F, KEY_F},
106         {AKEYCODE_G, KEY_G},
107         {AKEYCODE_H, KEY_H},
108         {AKEYCODE_I, KEY_I},
109         {AKEYCODE_J, KEY_J},
110         {AKEYCODE_K, KEY_K},
111         {AKEYCODE_L, KEY_L},
112         {AKEYCODE_M, KEY_M},
113         {AKEYCODE_N, KEY_N},
114         {AKEYCODE_O, KEY_O},
115         {AKEYCODE_P, KEY_P},
116         {AKEYCODE_Q, KEY_Q},
117         {AKEYCODE_R, KEY_R},
118         {AKEYCODE_S, KEY_S},
119         {AKEYCODE_T, KEY_T},
120         {AKEYCODE_U, KEY_U},
121         {AKEYCODE_V, KEY_V},
122         {AKEYCODE_W, KEY_W},
123         {AKEYCODE_X, KEY_X},
124         {AKEYCODE_Y, KEY_Y},
125         {AKEYCODE_Z, KEY_Z},
126         {AKEYCODE_GRAVE, KEY_GRAVE},
127         {AKEYCODE_MINUS, KEY_MINUS},
128         {AKEYCODE_EQUALS, KEY_EQUAL},
129         {AKEYCODE_LEFT_BRACKET, KEY_LEFTBRACE},
130         {AKEYCODE_RIGHT_BRACKET, KEY_RIGHTBRACE},
131         {AKEYCODE_BACKSLASH, KEY_BACKSLASH},
132         {AKEYCODE_SEMICOLON, KEY_SEMICOLON},
133         {AKEYCODE_APOSTROPHE, KEY_APOSTROPHE},
134         {AKEYCODE_COMMA, KEY_COMMA},
135         {AKEYCODE_PERIOD, KEY_DOT},
136         {AKEYCODE_SLASH, KEY_SLASH},
137         {AKEYCODE_ALT_LEFT, KEY_LEFTALT},
138         {AKEYCODE_ALT_RIGHT, KEY_RIGHTALT},
139         {AKEYCODE_CTRL_LEFT, KEY_LEFTCTRL},
140         {AKEYCODE_CTRL_RIGHT, KEY_RIGHTCTRL},
141         {AKEYCODE_SHIFT_LEFT, KEY_LEFTSHIFT},
142         {AKEYCODE_SHIFT_RIGHT, KEY_RIGHTSHIFT},
143         {AKEYCODE_META_LEFT, KEY_LEFTMETA},
144         {AKEYCODE_META_RIGHT, KEY_RIGHTMETA},
145         {AKEYCODE_CAPS_LOCK, KEY_CAPSLOCK},
146         {AKEYCODE_SCROLL_LOCK, KEY_SCROLLLOCK},
147         {AKEYCODE_NUM_LOCK, KEY_NUMLOCK},
148         {AKEYCODE_ENTER, KEY_ENTER},
149         {AKEYCODE_TAB, KEY_TAB},
150         {AKEYCODE_SPACE, KEY_SPACE},
151         {AKEYCODE_DPAD_DOWN, KEY_DOWN},
152         {AKEYCODE_DPAD_UP, KEY_UP},
153         {AKEYCODE_DPAD_LEFT, KEY_LEFT},
154         {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
155         {AKEYCODE_MOVE_END, KEY_END},
156         {AKEYCODE_MOVE_HOME, KEY_HOME},
157         {AKEYCODE_PAGE_DOWN, KEY_PAGEDOWN},
158         {AKEYCODE_PAGE_UP, KEY_PAGEUP},
159         {AKEYCODE_DEL, KEY_BACKSPACE},
160         {AKEYCODE_FORWARD_DEL, KEY_DELETE},
161         {AKEYCODE_INSERT, KEY_INSERT},
162         {AKEYCODE_ESCAPE, KEY_ESC},
163         {AKEYCODE_BREAK, KEY_PAUSE},
164         {AKEYCODE_F1, KEY_F1},
165         {AKEYCODE_F2, KEY_F2},
166         {AKEYCODE_F3, KEY_F3},
167         {AKEYCODE_F4, KEY_F4},
168         {AKEYCODE_F5, KEY_F5},
169         {AKEYCODE_F6, KEY_F6},
170         {AKEYCODE_F7, KEY_F7},
171         {AKEYCODE_F8, KEY_F8},
172         {AKEYCODE_F9, KEY_F9},
173         {AKEYCODE_F10, KEY_F10},
174         {AKEYCODE_F11, KEY_F11},
175         {AKEYCODE_F12, KEY_F12},
176         {AKEYCODE_BACK, KEY_BACK},
177         {AKEYCODE_FORWARD, KEY_FORWARD},
178         {AKEYCODE_NUMPAD_1, KEY_KP1},
179         {AKEYCODE_NUMPAD_2, KEY_KP2},
180         {AKEYCODE_NUMPAD_3, KEY_KP3},
181         {AKEYCODE_NUMPAD_4, KEY_KP4},
182         {AKEYCODE_NUMPAD_5, KEY_KP5},
183         {AKEYCODE_NUMPAD_6, KEY_KP6},
184         {AKEYCODE_NUMPAD_7, KEY_KP7},
185         {AKEYCODE_NUMPAD_8, KEY_KP8},
186         {AKEYCODE_NUMPAD_9, KEY_KP9},
187         {AKEYCODE_NUMPAD_0, KEY_KP0},
188         {AKEYCODE_NUMPAD_ADD, KEY_KPPLUS},
189         {AKEYCODE_NUMPAD_SUBTRACT, KEY_KPMINUS},
190         {AKEYCODE_NUMPAD_MULTIPLY, KEY_KPASTERISK},
191         {AKEYCODE_NUMPAD_DIVIDE, KEY_KPSLASH},
192         {AKEYCODE_NUMPAD_DOT, KEY_KPDOT},
193         {AKEYCODE_NUMPAD_ENTER, KEY_KPENTER},
194         {AKEYCODE_NUMPAD_EQUALS, KEY_KPEQUAL},
195         {AKEYCODE_NUMPAD_COMMA, KEY_KPCOMMA},
196 };
VirtualKeyboard(unique_fd fd)197 VirtualKeyboard::VirtualKeyboard(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
~VirtualKeyboard()198 VirtualKeyboard::~VirtualKeyboard() {}
199 
writeKeyEvent(int32_t androidKeyCode,int32_t androidAction,std::chrono::nanoseconds eventTime)200 bool VirtualKeyboard::writeKeyEvent(int32_t androidKeyCode, int32_t androidAction,
201                                     std::chrono::nanoseconds eventTime) {
202     return writeEvKeyEvent(androidKeyCode, androidAction, KEY_CODE_MAPPING, KEY_ACTION_MAPPING,
203                            eventTime);
204 }
205 
206 // --- VirtualDpad ---
207 // Dpad keycode mapping from https://source.android.com/devices/input/keyboard-devices
208 const std::map<int, int> VirtualDpad::DPAD_KEY_CODE_MAPPING = {
209         // clang-format off
210         {AKEYCODE_DPAD_DOWN, KEY_DOWN},
211         {AKEYCODE_DPAD_UP, KEY_UP},
212         {AKEYCODE_DPAD_LEFT, KEY_LEFT},
213         {AKEYCODE_DPAD_RIGHT, KEY_RIGHT},
214         {AKEYCODE_DPAD_CENTER, KEY_SELECT},
215         {AKEYCODE_BACK, KEY_BACK},
216         // clang-format on
217 };
218 
VirtualDpad(unique_fd fd)219 VirtualDpad::VirtualDpad(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
220 
~VirtualDpad()221 VirtualDpad::~VirtualDpad() {}
222 
writeDpadKeyEvent(int32_t androidKeyCode,int32_t androidAction,std::chrono::nanoseconds eventTime)223 bool VirtualDpad::writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction,
224                                     std::chrono::nanoseconds eventTime) {
225     return writeEvKeyEvent(androidKeyCode, androidAction, DPAD_KEY_CODE_MAPPING,
226                            VirtualKeyboard::KEY_ACTION_MAPPING, eventTime);
227 }
228 
229 // --- VirtualMouse ---
230 const std::map<int, UinputAction> VirtualMouse::BUTTON_ACTION_MAPPING = {
231         {AMOTION_EVENT_ACTION_BUTTON_PRESS, UinputAction::PRESS},
232         {AMOTION_EVENT_ACTION_BUTTON_RELEASE, UinputAction::RELEASE},
233 };
234 
235 // Button code mapping from https://source.android.com/devices/input/touch-devices
236 const std::map<int, int> VirtualMouse::BUTTON_CODE_MAPPING = {
237         // clang-format off
238         {AMOTION_EVENT_BUTTON_PRIMARY, BTN_LEFT},
239         {AMOTION_EVENT_BUTTON_SECONDARY, BTN_RIGHT},
240         {AMOTION_EVENT_BUTTON_TERTIARY, BTN_MIDDLE},
241         {AMOTION_EVENT_BUTTON_BACK, BTN_BACK},
242         {AMOTION_EVENT_BUTTON_FORWARD, BTN_FORWARD},
243         // clang-format on
244 };
245 
VirtualMouse(unique_fd fd)246 VirtualMouse::VirtualMouse(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
247 
~VirtualMouse()248 VirtualMouse::~VirtualMouse() {}
249 
writeButtonEvent(int32_t androidButtonCode,int32_t androidAction,std::chrono::nanoseconds eventTime)250 bool VirtualMouse::writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
251                                     std::chrono::nanoseconds eventTime) {
252     return writeEvKeyEvent(androidButtonCode, androidAction, BUTTON_CODE_MAPPING,
253                            BUTTON_ACTION_MAPPING, eventTime);
254 }
255 
writeRelativeEvent(float relativeX,float relativeY,std::chrono::nanoseconds eventTime)256 bool VirtualMouse::writeRelativeEvent(float relativeX, float relativeY,
257                                       std::chrono::nanoseconds eventTime) {
258     return writeInputEvent(EV_REL, REL_X, relativeX, eventTime) &&
259             writeInputEvent(EV_REL, REL_Y, relativeY, eventTime) &&
260             writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
261 }
262 
writeScrollEvent(float xAxisMovement,float yAxisMovement,std::chrono::nanoseconds eventTime)263 bool VirtualMouse::writeScrollEvent(float xAxisMovement, float yAxisMovement,
264                                     std::chrono::nanoseconds eventTime) {
265     return writeInputEvent(EV_REL, REL_HWHEEL, xAxisMovement, eventTime) &&
266             writeInputEvent(EV_REL, REL_WHEEL, yAxisMovement, eventTime) &&
267             writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
268 }
269 
270 // --- VirtualTouchscreen ---
271 const std::map<int, UinputAction> VirtualTouchscreen::TOUCH_ACTION_MAPPING = {
272         {AMOTION_EVENT_ACTION_DOWN, UinputAction::PRESS},
273         {AMOTION_EVENT_ACTION_UP, UinputAction::RELEASE},
274         {AMOTION_EVENT_ACTION_MOVE, UinputAction::MOVE},
275         {AMOTION_EVENT_ACTION_CANCEL, UinputAction::CANCEL},
276 };
277 // Tool type mapping from https://source.android.com/devices/input/touch-devices
278 const std::map<int, int> VirtualTouchscreen::TOOL_TYPE_MAPPING = {
279         {AMOTION_EVENT_TOOL_TYPE_FINGER, MT_TOOL_FINGER},
280         {AMOTION_EVENT_TOOL_TYPE_PALM, MT_TOOL_PALM},
281 };
282 
VirtualTouchscreen(unique_fd fd)283 VirtualTouchscreen::VirtualTouchscreen(unique_fd fd) : VirtualInputDevice(std::move(fd)) {}
284 
~VirtualTouchscreen()285 VirtualTouchscreen::~VirtualTouchscreen() {}
286 
isValidPointerId(int32_t pointerId,UinputAction uinputAction)287 bool VirtualTouchscreen::isValidPointerId(int32_t pointerId, UinputAction uinputAction) {
288     if (pointerId < -1 || pointerId >= (int)MAX_POINTERS) {
289         ALOGE("Virtual touch event has invalid pointer id %d; value must be between -1 and %zu",
290               pointerId, MAX_POINTERS - 0);
291         return false;
292     }
293 
294     if (uinputAction == UinputAction::PRESS && mActivePointers.test(pointerId)) {
295         ALOGE("Repetitive action DOWN event received on a pointer %d that is already down.",
296               pointerId);
297         return false;
298     }
299     if (uinputAction == UinputAction::RELEASE && !mActivePointers.test(pointerId)) {
300         ALOGE("PointerId %d action UP received with no prior action DOWN on touchscreen %d.",
301               pointerId, mFd.get());
302         return false;
303     }
304     return true;
305 }
306 
writeTouchEvent(int32_t pointerId,int32_t toolType,int32_t action,float locationX,float locationY,float pressure,float majorAxisSize,std::chrono::nanoseconds eventTime)307 bool VirtualTouchscreen::writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action,
308                                          float locationX, float locationY, float pressure,
309                                          float majorAxisSize, std::chrono::nanoseconds eventTime) {
310     auto actionIterator = TOUCH_ACTION_MAPPING.find(action);
311     if (actionIterator == TOUCH_ACTION_MAPPING.end()) {
312         return false;
313     }
314     UinputAction uinputAction = actionIterator->second;
315     if (!isValidPointerId(pointerId, uinputAction)) {
316         return false;
317     }
318     if (!writeInputEvent(EV_ABS, ABS_MT_SLOT, pointerId, eventTime)) {
319         return false;
320     }
321     auto toolTypeIterator = TOOL_TYPE_MAPPING.find(toolType);
322     if (toolTypeIterator == TOOL_TYPE_MAPPING.end()) {
323         return false;
324     }
325     if (!writeInputEvent(EV_ABS, ABS_MT_TOOL_TYPE, static_cast<int32_t>(toolTypeIterator->second),
326                          eventTime)) {
327         return false;
328     }
329     if (uinputAction == UinputAction::PRESS && !handleTouchDown(pointerId, eventTime)) {
330         return false;
331     }
332     if (uinputAction == UinputAction::RELEASE && !handleTouchUp(pointerId, eventTime)) {
333         return false;
334     }
335     if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_X, locationX, eventTime)) {
336         return false;
337     }
338     if (!writeInputEvent(EV_ABS, ABS_MT_POSITION_Y, locationY, eventTime)) {
339         return false;
340     }
341     if (!isnan(pressure)) {
342         if (!writeInputEvent(EV_ABS, ABS_MT_PRESSURE, pressure, eventTime)) {
343             return false;
344         }
345     }
346     if (!isnan(majorAxisSize)) {
347         if (!writeInputEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, majorAxisSize, eventTime)) {
348             return false;
349         }
350     }
351     return writeInputEvent(EV_SYN, SYN_REPORT, 0, eventTime);
352 }
353 
handleTouchUp(int32_t pointerId,std::chrono::nanoseconds eventTime)354 bool VirtualTouchscreen::handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime) {
355     if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(-1), eventTime)) {
356         return false;
357     }
358     // When a pointer is no longer in touch, remove the pointer id from the corresponding
359     // entry in the unreleased touches map.
360     mActivePointers.reset(pointerId);
361     ALOGD_IF(isDebug(), "Pointer %d erased from the touchscreen %d", pointerId, mFd.get());
362 
363     // Only sends the BTN UP event when there's no pointers on the touchscreen.
364     if (mActivePointers.none()) {
365         if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::RELEASE),
366                              eventTime)) {
367             return false;
368         }
369         ALOGD_IF(isDebug(), "No pointers on touchscreen %d, BTN UP event sent.", mFd.get());
370     }
371     return true;
372 }
373 
handleTouchDown(int32_t pointerId,std::chrono::nanoseconds eventTime)374 bool VirtualTouchscreen::handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime) {
375     // When a new pointer is down on the touchscreen, add the pointer id in the corresponding
376     // entry in the unreleased touches map.
377     if (mActivePointers.none()) {
378         // Only sends the BTN Down event when the first pointer on the touchscreen is down.
379         if (!writeInputEvent(EV_KEY, BTN_TOUCH, static_cast<int32_t>(UinputAction::PRESS),
380                              eventTime)) {
381             return false;
382         }
383         ALOGD_IF(isDebug(), "First pointer %d down under touchscreen %d, BTN DOWN event sent",
384                  pointerId, mFd.get());
385     }
386 
387     mActivePointers.set(pointerId);
388     ALOGD_IF(isDebug(), "Added pointer %d under touchscreen %d in the map", pointerId, mFd.get());
389     if (!writeInputEvent(EV_ABS, ABS_MT_TRACKING_ID, static_cast<int32_t>(pointerId), eventTime)) {
390         return false;
391     }
392     return true;
393 }
394 
395 } // namespace android
396