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 #pragma once 18 19 #include <android-base/unique_fd.h> 20 21 namespace android { 22 23 enum class UinputAction { 24 RELEASE = 0, 25 PRESS = 1, 26 MOVE = 2, 27 CANCEL = 3, 28 }; 29 30 class VirtualInputDevice { 31 public: 32 VirtualInputDevice(android::base::unique_fd fd); 33 virtual ~VirtualInputDevice(); 34 35 protected: 36 const android::base::unique_fd mFd; 37 bool writeInputEvent(uint16_t type, uint16_t code, int32_t value, 38 std::chrono::nanoseconds eventTime); 39 bool writeEvKeyEvent(int32_t androidCode, int32_t androidAction, 40 const std::map<int, int>& evKeyCodeMapping, 41 const std::map<int, UinputAction>& actionMapping, 42 std::chrono::nanoseconds eventTime); 43 }; 44 45 class VirtualKeyboard : public VirtualInputDevice { 46 public: 47 static const std::map<int, int> KEY_CODE_MAPPING; 48 // Expose to share with VirtualDpad. 49 static const std::map<int, UinputAction> KEY_ACTION_MAPPING; 50 VirtualKeyboard(android::base::unique_fd fd); 51 virtual ~VirtualKeyboard() override; 52 bool writeKeyEvent(int32_t androidKeyCode, int32_t androidAction, 53 std::chrono::nanoseconds eventTime); 54 }; 55 56 class VirtualDpad : public VirtualInputDevice { 57 public: 58 static const std::map<int, int> DPAD_KEY_CODE_MAPPING; 59 VirtualDpad(android::base::unique_fd fd); 60 virtual ~VirtualDpad() override; 61 bool writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction, 62 std::chrono::nanoseconds eventTime); 63 }; 64 65 class VirtualMouse : public VirtualInputDevice { 66 public: 67 VirtualMouse(android::base::unique_fd fd); 68 virtual ~VirtualMouse() override; 69 bool writeButtonEvent(int32_t androidButtonCode, int32_t androidAction, 70 std::chrono::nanoseconds eventTime); 71 // TODO(b/259554911): changing float parameters to int32_t. 72 bool writeRelativeEvent(float relativeX, float relativeY, std::chrono::nanoseconds eventTime); 73 bool writeScrollEvent(float xAxisMovement, float yAxisMovement, 74 std::chrono::nanoseconds eventTime); 75 76 private: 77 static const std::map<int, UinputAction> BUTTON_ACTION_MAPPING; 78 static const std::map<int, int> BUTTON_CODE_MAPPING; 79 }; 80 81 class VirtualTouchscreen : public VirtualInputDevice { 82 public: 83 VirtualTouchscreen(android::base::unique_fd fd); 84 virtual ~VirtualTouchscreen() override; 85 // TODO(b/259554911): changing float parameters to int32_t. 86 bool writeTouchEvent(int32_t pointerId, int32_t toolType, int32_t action, float locationX, 87 float locationY, float pressure, float majorAxisSize, 88 std::chrono::nanoseconds eventTime); 89 90 private: 91 static const std::map<int, UinputAction> TOUCH_ACTION_MAPPING; 92 static const std::map<int, int> TOOL_TYPE_MAPPING; 93 94 /* The set of active touch pointers on this device. 95 * We only allow pointer id to go up to MAX_POINTERS because the maximum slots of virtual 96 * touchscreen is set up with MAX_POINTERS. Note that in other cases Android allows pointer id 97 * to go up to MAX_POINTERS_ID. 98 */ 99 std::bitset<MAX_POINTERS> mActivePointers{}; 100 bool isValidPointerId(int32_t pointerId, UinputAction uinputAction); 101 bool handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime); 102 bool handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime); 103 }; 104 } // namespace android 105