blob: 9c939198f3c96f7f4425416a20a60eb3cc763e88 [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>
Michael Wright8e9a8562022-02-09 13:44:29 +000020#include <fcntl.h>
Prabir Pradhane0105c92019-12-26 12:32:13 -080021
22namespace android {
23
24// --- UinputDevice ---
25
26UinputDevice::UinputDevice(const char* name) : mName(name) {}
27
28UinputDevice::~UinputDevice() {
29 if (ioctl(mDeviceFd, UI_DEV_DESTROY)) {
30 ALOGE("Error while destroying uinput device: %s", strerror(errno));
31 }
32 mDeviceFd.reset();
33}
34
35void UinputDevice::init() {
Michael Wright8e9a8562022-02-09 13:44:29 +000036 mDeviceFd = android::base::unique_fd(open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC));
Prabir Pradhane0105c92019-12-26 12:32:13 -080037 if (mDeviceFd < 0) {
38 FAIL() << "Can't open /dev/uinput :" << strerror(errno);
39 }
40
41 struct uinput_user_dev device = {};
42 strlcpy(device.name, mName, UINPUT_MAX_NAME_SIZE);
43 device.id.bustype = BUS_USB;
44 device.id.vendor = 0x01;
45 device.id.product = 0x01;
46 device.id.version = 1;
47
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070048 ASSERT_NO_FATAL_FAILURE(configureDevice(mDeviceFd, &device));
Prabir Pradhane0105c92019-12-26 12:32:13 -080049
50 if (write(mDeviceFd, &device, sizeof(device)) < 0) {
51 FAIL() << "Could not write uinput_user_dev struct into uinput file descriptor: "
52 << strerror(errno);
53 }
54
55 if (ioctl(mDeviceFd, UI_DEV_CREATE)) {
56 FAIL() << "Error in ioctl : UI_DEV_CREATE: " << strerror(errno);
57 }
58}
59
60void UinputDevice::injectEvent(uint16_t type, uint16_t code, int32_t value) {
61 struct input_event event = {};
62 event.type = type;
63 event.code = code;
64 event.value = value;
65 event.time = {}; // uinput ignores the timestamp
66
67 if (write(mDeviceFd, &event, sizeof(input_event)) < 0) {
68 std::string msg = base::StringPrintf("Could not write event %" PRIu16 " %" PRIu16
69 " with value %" PRId32 " : %s",
70 type, code, value, strerror(errno));
71 ALOGE("%s", msg.c_str());
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070072 FAIL() << msg.c_str();
Prabir Pradhane0105c92019-12-26 12:32:13 -080073 }
74}
75
76// --- UinputKeyboard ---
77
78UinputKeyboard::UinputKeyboard(std::initializer_list<int> keys)
79 : UinputDevice(UinputKeyboard::KEYBOARD_NAME), mKeys(keys.begin(), keys.end()) {}
80
81void UinputKeyboard::configureDevice(int fd, uinput_user_dev* device) {
82 // enable key press/release event
83 if (ioctl(fd, UI_SET_EVBIT, EV_KEY)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070084 FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_KEY: " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080085 }
86
87 // enable set of KEY events
88 std::for_each(mKeys.begin(), mKeys.end(), [fd](int key) {
89 if (ioctl(fd, UI_SET_KEYBIT, key)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070090 FAIL() << "Error in ioctl : UI_SET_KEYBIT : " << key << " : " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080091 }
92 });
93
94 // enable synchronization event
95 if (ioctl(fd, UI_SET_EVBIT, EV_SYN)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070096 FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_SYN: " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080097 }
98}
99
100void UinputKeyboard::pressKey(int key) {
101 if (mKeys.find(key) == mKeys.end()) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700102 FAIL() << mName << ": Cannot inject key press: Key not found: " << key;
Prabir Pradhane0105c92019-12-26 12:32:13 -0800103 }
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700104 injectEvent(EV_KEY, key, 1);
105 injectEvent(EV_SYN, SYN_REPORT, 0);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800106}
107
108void UinputKeyboard::releaseKey(int key) {
109 if (mKeys.find(key) == mKeys.end()) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700110 FAIL() << mName << ": Cannot inject key release: Key not found: " << key;
Prabir Pradhane0105c92019-12-26 12:32:13 -0800111 }
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700112 injectEvent(EV_KEY, key, 0);
113 injectEvent(EV_SYN, SYN_REPORT, 0);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800114}
115
116void UinputKeyboard::pressAndReleaseKey(int key) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700117 pressKey(key);
118 releaseKey(key);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800119}
120
Arthur Hungaab25622020-01-16 11:22:11 +0800121// --- UinputHomeKey ---
Prabir Pradhane0105c92019-12-26 12:32:13 -0800122
123UinputHomeKey::UinputHomeKey() : UinputKeyboard({KEY_HOME}) {}
124
125void UinputHomeKey::pressAndReleaseHomeKey() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700126 pressAndReleaseKey(KEY_HOME);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800127}
128
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -0700129// --- UinputSteamController
130UinputSteamController::UinputSteamController() : UinputKeyboard({BTN_GEAR_DOWN, BTN_GEAR_UP}) {}
131
Arthur Hungaab25622020-01-16 11:22:11 +0800132// --- UinputTouchScreen ---
133UinputTouchScreen::UinputTouchScreen(const Rect* size)
134 : UinputDevice(UinputTouchScreen::DEVICE_NAME), mSize(*size) {}
135
136void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
137 // Setup the touch screen device
138 ioctl(fd, UI_SET_EVBIT, EV_KEY);
139 ioctl(fd, UI_SET_EVBIT, EV_REL);
140 ioctl(fd, UI_SET_EVBIT, EV_ABS);
141 ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
142 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
143 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
144 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
145 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
146 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
147 ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
148 ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
149
150 device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN;
151 device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX;
152 device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN;
153 device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX;
154 device->absmin[ABS_MT_POSITION_X] = mSize.left;
155 device->absmax[ABS_MT_POSITION_X] = mSize.right - 1;
156 device->absmin[ABS_MT_POSITION_Y] = mSize.top;
157 device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1;
158 device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN;
159 device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX;
160}
161
162void UinputTouchScreen::sendSlot(int32_t slot) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700163 injectEvent(EV_ABS, ABS_MT_SLOT, slot);
Arthur Hungaab25622020-01-16 11:22:11 +0800164}
165
166void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700167 injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
Arthur Hungaab25622020-01-16 11:22:11 +0800168}
169
170void UinputTouchScreen::sendDown(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700171 injectEvent(EV_KEY, BTN_TOUCH, 1);
172 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
173 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800174}
175
176void UinputTouchScreen::sendMove(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700177 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
178 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800179}
180
arthurhungcc7f9802020-04-30 17:55:40 +0800181void UinputTouchScreen::sendPointerUp() {
182 sendTrackingId(0xffffffff);
arthurhungcc7f9802020-04-30 17:55:40 +0800183}
184
Arthur Hungaab25622020-01-16 11:22:11 +0800185void UinputTouchScreen::sendUp() {
186 sendTrackingId(0xffffffff);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700187 injectEvent(EV_KEY, BTN_TOUCH, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800188}
189
190void UinputTouchScreen::sendToolType(int32_t toolType) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700191 injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800192}
193
194void UinputTouchScreen::sendSync() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700195 injectEvent(EV_SYN, SYN_REPORT, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800196}
197
198// Get the center x, y base on the range definition.
199const Point UinputTouchScreen::getCenterPoint() {
200 return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
201}
202
Prabir Pradhane0105c92019-12-26 12:32:13 -0800203} // namespace android