blob: 21a28770b60e3ea737eb114d78aa7698e82c93fb [file] [log] [blame]
Zixuan Qudd0635d2023-02-06 04:52:38 +00001/*
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
21namespace android {
22
23enum class UinputAction {
24 RELEASE = 0,
25 PRESS = 1,
26 MOVE = 2,
27 CANCEL = 3,
28};
29
30class VirtualInputDevice {
31public:
32 VirtualInputDevice(android::base::unique_fd fd);
33 virtual ~VirtualInputDevice();
34
35protected:
36 const android::base::unique_fd mFd;
Biswarup Pale9fe2df2023-04-05 18:20:54 +000037 bool writeInputEvent(uint16_t type, uint16_t code, int32_t value,
38 std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +000039 bool writeEvKeyEvent(int32_t androidCode, int32_t androidAction,
40 const std::map<int, int>& evKeyCodeMapping,
Biswarup Pale9fe2df2023-04-05 18:20:54 +000041 const std::map<int, UinputAction>& actionMapping,
42 std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +000043};
44
45class VirtualKeyboard : public VirtualInputDevice {
46public:
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;
Biswarup Pale9fe2df2023-04-05 18:20:54 +000052 bool writeKeyEvent(int32_t androidKeyCode, int32_t androidAction,
53 std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +000054};
55
56class VirtualDpad : public VirtualInputDevice {
57public:
58 static const std::map<int, int> DPAD_KEY_CODE_MAPPING;
59 VirtualDpad(android::base::unique_fd fd);
60 virtual ~VirtualDpad() override;
Biswarup Pale9fe2df2023-04-05 18:20:54 +000061 bool writeDpadKeyEvent(int32_t androidKeyCode, int32_t androidAction,
62 std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +000063};
64
65class VirtualMouse : public VirtualInputDevice {
66public:
67 VirtualMouse(android::base::unique_fd fd);
68 virtual ~VirtualMouse() override;
Biswarup Pale9fe2df2023-04-05 18:20:54 +000069 bool writeButtonEvent(int32_t androidButtonCode, int32_t androidAction,
70 std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +000071 // TODO(b/259554911): changing float parameters to int32_t.
Biswarup Pale9fe2df2023-04-05 18:20:54 +000072 bool writeRelativeEvent(float relativeX, float relativeY, std::chrono::nanoseconds eventTime);
73 bool writeScrollEvent(float xAxisMovement, float yAxisMovement,
74 std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +000075
76private:
77 static const std::map<int, UinputAction> BUTTON_ACTION_MAPPING;
78 static const std::map<int, int> BUTTON_CODE_MAPPING;
79};
80
81class VirtualTouchscreen : public VirtualInputDevice {
82public:
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,
Biswarup Pale9fe2df2023-04-05 18:20:54 +000087 float locationY, float pressure, float majorAxisSize,
88 std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +000089
90private:
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);
Biswarup Pale9fe2df2023-04-05 18:20:54 +0000101 bool handleTouchDown(int32_t pointerId, std::chrono::nanoseconds eventTime);
102 bool handleTouchUp(int32_t pointerId, std::chrono::nanoseconds eventTime);
Zixuan Qudd0635d2023-02-06 04:52:38 +0000103};
104} // namespace android