blob: 626ad67c717cbc0fea4b4411a746f9ec6987e6e2 [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
79UinputKeyboard::UinputKeyboard(std::initializer_list<int> keys)
80 : UinputDevice(UinputKeyboard::KEYBOARD_NAME), mKeys(keys.begin(), keys.end()) {}
81
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
124UinputHomeKey::UinputHomeKey() : UinputKeyboard({KEY_HOME}) {}
125
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
131UinputSteamController::UinputSteamController() : UinputKeyboard({BTN_GEAR_DOWN, BTN_GEAR_UP}) {}
132
Arthur Hungaab25622020-01-16 11:22:11 +0800133// --- UinputTouchScreen ---
134UinputTouchScreen::UinputTouchScreen(const Rect* size)
135 : UinputDevice(UinputTouchScreen::DEVICE_NAME), mSize(*size) {}
136
137void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
138 // Setup the touch screen device
139 ioctl(fd, UI_SET_EVBIT, EV_KEY);
140 ioctl(fd, UI_SET_EVBIT, EV_REL);
141 ioctl(fd, UI_SET_EVBIT, EV_ABS);
142 ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
143 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
144 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
145 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
146 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
147 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
148 ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
149 ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
150
151 device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN;
152 device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX;
153 device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN;
154 device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX;
155 device->absmin[ABS_MT_POSITION_X] = mSize.left;
156 device->absmax[ABS_MT_POSITION_X] = mSize.right - 1;
157 device->absmin[ABS_MT_POSITION_Y] = mSize.top;
158 device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1;
159 device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN;
160 device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000161 device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER;
162 device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX;
Arthur Hungaab25622020-01-16 11:22:11 +0800163}
164
165void UinputTouchScreen::sendSlot(int32_t slot) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700166 injectEvent(EV_ABS, ABS_MT_SLOT, slot);
Arthur Hungaab25622020-01-16 11:22:11 +0800167}
168
169void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700170 injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
Arthur Hungaab25622020-01-16 11:22:11 +0800171}
172
173void UinputTouchScreen::sendDown(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700174 injectEvent(EV_KEY, BTN_TOUCH, 1);
175 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
176 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800177}
178
179void UinputTouchScreen::sendMove(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700180 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
181 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800182}
183
arthurhungcc7f9802020-04-30 17:55:40 +0800184void UinputTouchScreen::sendPointerUp() {
185 sendTrackingId(0xffffffff);
arthurhungcc7f9802020-04-30 17:55:40 +0800186}
187
Arthur Hungaab25622020-01-16 11:22:11 +0800188void UinputTouchScreen::sendUp() {
189 sendTrackingId(0xffffffff);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700190 injectEvent(EV_KEY, BTN_TOUCH, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800191}
192
193void UinputTouchScreen::sendToolType(int32_t toolType) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700194 injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800195}
196
197void UinputTouchScreen::sendSync() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700198 injectEvent(EV_SYN, SYN_REPORT, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800199}
200
201// Get the center x, y base on the range definition.
202const Point UinputTouchScreen::getCenterPoint() {
203 return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
204}
205
Prabir Pradhane0105c92019-12-26 12:32:13 -0800206} // namespace android