blob: c4830dc815aefcb09307c9dd2d693785845de86f [file] [log] [blame]
Prabir Pradhane0105c92019-12-26 12:32:13 -08001/*
2 * Copyright (C) 2020 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 "UinputDevice.h"
18
19#include <android-base/stringprintf.h>
Siarhei Vishniakou31977182022-09-30 08:51:23 -070020#include <cutils/memory.h>
Michael Wright8e9a8562022-02-09 13:44:29 +000021#include <fcntl.h>
Prabir Pradhane0105c92019-12-26 12:32:13 -080022
23namespace android {
24
25// --- UinputDevice ---
26
27UinputDevice::UinputDevice(const char* name) : mName(name) {}
28
29UinputDevice::~UinputDevice() {
30 if (ioctl(mDeviceFd, UI_DEV_DESTROY)) {
31 ALOGE("Error while destroying uinput device: %s", strerror(errno));
32 }
33 mDeviceFd.reset();
34}
35
36void UinputDevice::init() {
Michael Wright8e9a8562022-02-09 13:44:29 +000037 mDeviceFd = android::base::unique_fd(open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC));
Prabir Pradhane0105c92019-12-26 12:32:13 -080038 if (mDeviceFd < 0) {
39 FAIL() << "Can't open /dev/uinput :" << strerror(errno);
40 }
41
42 struct uinput_user_dev device = {};
43 strlcpy(device.name, mName, UINPUT_MAX_NAME_SIZE);
44 device.id.bustype = BUS_USB;
45 device.id.vendor = 0x01;
46 device.id.product = 0x01;
47 device.id.version = 1;
48
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070049 ASSERT_NO_FATAL_FAILURE(configureDevice(mDeviceFd, &device));
Prabir Pradhane0105c92019-12-26 12:32:13 -080050
51 if (write(mDeviceFd, &device, sizeof(device)) < 0) {
52 FAIL() << "Could not write uinput_user_dev struct into uinput file descriptor: "
53 << strerror(errno);
54 }
55
56 if (ioctl(mDeviceFd, UI_DEV_CREATE)) {
57 FAIL() << "Error in ioctl : UI_DEV_CREATE: " << strerror(errno);
58 }
59}
60
61void UinputDevice::injectEvent(uint16_t type, uint16_t code, int32_t value) {
Colin Cross5b799302022-10-18 21:52:41 -070062 // uinput ignores the timestamp
Prabir Pradhane0105c92019-12-26 12:32:13 -080063 struct input_event event = {};
64 event.type = type;
65 event.code = code;
66 event.value = value;
Prabir Pradhane0105c92019-12-26 12:32:13 -080067
68 if (write(mDeviceFd, &event, sizeof(input_event)) < 0) {
69 std::string msg = base::StringPrintf("Could not write event %" PRIu16 " %" PRIu16
70 " with value %" PRId32 " : %s",
71 type, code, value, strerror(errno));
72 ALOGE("%s", msg.c_str());
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070073 FAIL() << msg.c_str();
Prabir Pradhane0105c92019-12-26 12:32:13 -080074 }
75}
76
77// --- UinputKeyboard ---
78
Prabir Pradhane1a41a82022-10-14 18:06:50 +000079UinputKeyboard::UinputKeyboard(const char* name, std::initializer_list<int> keys)
80 : UinputDevice(name), mKeys(keys.begin(), keys.end()) {}
Prabir Pradhane0105c92019-12-26 12:32:13 -080081
82void UinputKeyboard::configureDevice(int fd, uinput_user_dev* device) {
83 // enable key press/release event
84 if (ioctl(fd, UI_SET_EVBIT, EV_KEY)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070085 FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_KEY: " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080086 }
87
88 // enable set of KEY events
89 std::for_each(mKeys.begin(), mKeys.end(), [fd](int key) {
90 if (ioctl(fd, UI_SET_KEYBIT, key)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070091 FAIL() << "Error in ioctl : UI_SET_KEYBIT : " << key << " : " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080092 }
93 });
94
95 // enable synchronization event
96 if (ioctl(fd, UI_SET_EVBIT, EV_SYN)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070097 FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_SYN: " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080098 }
99}
100
101void UinputKeyboard::pressKey(int key) {
102 if (mKeys.find(key) == mKeys.end()) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700103 FAIL() << mName << ": Cannot inject key press: Key not found: " << key;
Prabir Pradhane0105c92019-12-26 12:32:13 -0800104 }
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700105 injectEvent(EV_KEY, key, 1);
106 injectEvent(EV_SYN, SYN_REPORT, 0);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800107}
108
109void UinputKeyboard::releaseKey(int key) {
110 if (mKeys.find(key) == mKeys.end()) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700111 FAIL() << mName << ": Cannot inject key release: Key not found: " << key;
Prabir Pradhane0105c92019-12-26 12:32:13 -0800112 }
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700113 injectEvent(EV_KEY, key, 0);
114 injectEvent(EV_SYN, SYN_REPORT, 0);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800115}
116
117void UinputKeyboard::pressAndReleaseKey(int key) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700118 pressKey(key);
119 releaseKey(key);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800120}
121
Arthur Hungaab25622020-01-16 11:22:11 +0800122// --- UinputHomeKey ---
Prabir Pradhane0105c92019-12-26 12:32:13 -0800123
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000124UinputHomeKey::UinputHomeKey() : UinputKeyboard("Test Uinput Home Key", {KEY_HOME}) {}
Prabir Pradhane0105c92019-12-26 12:32:13 -0800125
126void UinputHomeKey::pressAndReleaseHomeKey() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700127 pressAndReleaseKey(KEY_HOME);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800128}
129
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -0700130// --- UinputSteamController
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000131UinputSteamController::UinputSteamController()
132 : UinputKeyboard("Test Uinput Steam Controller", {BTN_GEAR_DOWN, BTN_GEAR_UP}) {}
133
134// --- UinputExternalStylus
135UinputExternalStylus::UinputExternalStylus()
136 : UinputKeyboard("Test Uinput External Stylus", {BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}) {}
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -0700137
Arthur Hungaab25622020-01-16 11:22:11 +0800138// --- UinputTouchScreen ---
139UinputTouchScreen::UinputTouchScreen(const Rect* size)
140 : UinputDevice(UinputTouchScreen::DEVICE_NAME), mSize(*size) {}
141
142void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
143 // Setup the touch screen device
144 ioctl(fd, UI_SET_EVBIT, EV_KEY);
145 ioctl(fd, UI_SET_EVBIT, EV_REL);
146 ioctl(fd, UI_SET_EVBIT, EV_ABS);
147 ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
148 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
149 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
150 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
151 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
152 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
153 ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
154 ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000155 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS);
156 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS2);
157 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS3);
Arthur Hungaab25622020-01-16 11:22:11 +0800158
159 device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN;
160 device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX;
161 device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN;
162 device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX;
163 device->absmin[ABS_MT_POSITION_X] = mSize.left;
164 device->absmax[ABS_MT_POSITION_X] = mSize.right - 1;
165 device->absmin[ABS_MT_POSITION_Y] = mSize.top;
166 device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1;
167 device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN;
168 device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000169 device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER;
170 device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX;
Arthur Hungaab25622020-01-16 11:22:11 +0800171}
172
173void UinputTouchScreen::sendSlot(int32_t slot) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700174 injectEvent(EV_ABS, ABS_MT_SLOT, slot);
Arthur Hungaab25622020-01-16 11:22:11 +0800175}
176
177void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700178 injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
Arthur Hungaab25622020-01-16 11:22:11 +0800179}
180
181void UinputTouchScreen::sendDown(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700182 injectEvent(EV_KEY, BTN_TOUCH, 1);
183 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
184 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800185}
186
187void UinputTouchScreen::sendMove(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700188 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
189 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800190}
191
arthurhungcc7f9802020-04-30 17:55:40 +0800192void UinputTouchScreen::sendPointerUp() {
193 sendTrackingId(0xffffffff);
arthurhungcc7f9802020-04-30 17:55:40 +0800194}
195
Arthur Hungaab25622020-01-16 11:22:11 +0800196void UinputTouchScreen::sendUp() {
197 sendTrackingId(0xffffffff);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700198 injectEvent(EV_KEY, BTN_TOUCH, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800199}
200
201void UinputTouchScreen::sendToolType(int32_t toolType) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700202 injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800203}
204
205void UinputTouchScreen::sendSync() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700206 injectEvent(EV_SYN, SYN_REPORT, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800207}
208
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000209void UinputTouchScreen::sendKey(int32_t scanCode, int32_t value) {
210 injectEvent(EV_KEY, scanCode, value);
211}
212
Arthur Hungaab25622020-01-16 11:22:11 +0800213// Get the center x, y base on the range definition.
214const Point UinputTouchScreen::getCenterPoint() {
215 return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
216}
217
Prabir Pradhane0105c92019-12-26 12:32:13 -0800218} // namespace android