blob: acc7023fc83014fb50afb235abaf6b2e7e4ef3fd [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
Prabir Pradhanb7d434e2022-10-14 22:41:38 +000027UinputDevice::UinputDevice(const char* name, int16_t productId)
28 : mName(name), mProductId(productId) {}
Prabir Pradhane0105c92019-12-26 12:32:13 -080029
30UinputDevice::~UinputDevice() {
31 if (ioctl(mDeviceFd, UI_DEV_DESTROY)) {
32 ALOGE("Error while destroying uinput device: %s", strerror(errno));
33 }
34 mDeviceFd.reset();
35}
36
37void UinputDevice::init() {
Michael Wright8e9a8562022-02-09 13:44:29 +000038 mDeviceFd = android::base::unique_fd(open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC));
Prabir Pradhane0105c92019-12-26 12:32:13 -080039 if (mDeviceFd < 0) {
40 FAIL() << "Can't open /dev/uinput :" << strerror(errno);
41 }
42
43 struct uinput_user_dev device = {};
44 strlcpy(device.name, mName, UINPUT_MAX_NAME_SIZE);
45 device.id.bustype = BUS_USB;
46 device.id.vendor = 0x01;
Prabir Pradhanb7d434e2022-10-14 22:41:38 +000047 device.id.product = mProductId;
Prabir Pradhane0105c92019-12-26 12:32:13 -080048 device.id.version = 1;
49
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070050 ASSERT_NO_FATAL_FAILURE(configureDevice(mDeviceFd, &device));
Prabir Pradhane0105c92019-12-26 12:32:13 -080051
52 if (write(mDeviceFd, &device, sizeof(device)) < 0) {
53 FAIL() << "Could not write uinput_user_dev struct into uinput file descriptor: "
54 << strerror(errno);
55 }
56
57 if (ioctl(mDeviceFd, UI_DEV_CREATE)) {
58 FAIL() << "Error in ioctl : UI_DEV_CREATE: " << strerror(errno);
59 }
60}
61
62void UinputDevice::injectEvent(uint16_t type, uint16_t code, int32_t value) {
Colin Cross5b799302022-10-18 21:52:41 -070063 // uinput ignores the timestamp
Prabir Pradhane0105c92019-12-26 12:32:13 -080064 struct input_event event = {};
65 event.type = type;
66 event.code = code;
67 event.value = value;
Prabir Pradhane0105c92019-12-26 12:32:13 -080068
69 if (write(mDeviceFd, &event, sizeof(input_event)) < 0) {
70 std::string msg = base::StringPrintf("Could not write event %" PRIu16 " %" PRIu16
71 " with value %" PRId32 " : %s",
72 type, code, value, strerror(errno));
73 ALOGE("%s", msg.c_str());
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070074 FAIL() << msg.c_str();
Prabir Pradhane0105c92019-12-26 12:32:13 -080075 }
76}
77
78// --- UinputKeyboard ---
79
Prabir Pradhanb7d434e2022-10-14 22:41:38 +000080UinputKeyboard::UinputKeyboard(const char* name, int16_t productId, std::initializer_list<int> keys)
81 : UinputDevice(name, productId), mKeys(keys.begin(), keys.end()) {}
Prabir Pradhane0105c92019-12-26 12:32:13 -080082
83void UinputKeyboard::configureDevice(int fd, uinput_user_dev* device) {
84 // enable key press/release event
85 if (ioctl(fd, UI_SET_EVBIT, EV_KEY)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070086 FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_KEY: " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080087 }
88
89 // enable set of KEY events
90 std::for_each(mKeys.begin(), mKeys.end(), [fd](int key) {
91 if (ioctl(fd, UI_SET_KEYBIT, key)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070092 FAIL() << "Error in ioctl : UI_SET_KEYBIT : " << key << " : " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080093 }
94 });
95
96 // enable synchronization event
97 if (ioctl(fd, UI_SET_EVBIT, EV_SYN)) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -070098 FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_SYN: " << strerror(errno);
Prabir Pradhane0105c92019-12-26 12:32:13 -080099 }
100}
101
102void UinputKeyboard::pressKey(int key) {
103 if (mKeys.find(key) == mKeys.end()) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700104 FAIL() << mName << ": Cannot inject key press: Key not found: " << key;
Prabir Pradhane0105c92019-12-26 12:32:13 -0800105 }
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700106 injectEvent(EV_KEY, key, 1);
107 injectEvent(EV_SYN, SYN_REPORT, 0);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800108}
109
110void UinputKeyboard::releaseKey(int key) {
111 if (mKeys.find(key) == mKeys.end()) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700112 FAIL() << mName << ": Cannot inject key release: Key not found: " << key;
Prabir Pradhane0105c92019-12-26 12:32:13 -0800113 }
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700114 injectEvent(EV_KEY, key, 0);
115 injectEvent(EV_SYN, SYN_REPORT, 0);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800116}
117
118void UinputKeyboard::pressAndReleaseKey(int key) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700119 pressKey(key);
120 releaseKey(key);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800121}
122
Arthur Hungaab25622020-01-16 11:22:11 +0800123// --- UinputHomeKey ---
Prabir Pradhane0105c92019-12-26 12:32:13 -0800124
Prabir Pradhanb7d434e2022-10-14 22:41:38 +0000125UinputHomeKey::UinputHomeKey() : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {KEY_HOME}) {}
Prabir Pradhane0105c92019-12-26 12:32:13 -0800126
127void UinputHomeKey::pressAndReleaseHomeKey() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700128 pressAndReleaseKey(KEY_HOME);
Prabir Pradhane0105c92019-12-26 12:32:13 -0800129}
130
Prabir Pradhanb7d434e2022-10-14 22:41:38 +0000131// --- UinputSteamController ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000132
Prabir Pradhanb7d434e2022-10-14 22:41:38 +0000133UinputSteamController::UinputSteamController()
134 : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_GEAR_DOWN, BTN_GEAR_UP}) {}
135
136// --- UinputExternalStylus ---
137
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000138UinputExternalStylus::UinputExternalStylus()
Prabir Pradhanb7d434e2022-10-14 22:41:38 +0000139 : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}) {}
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -0700140
Prabir Pradhan484d55a2022-10-14 23:17:16 +0000141// --- UinputExternalStylusWithPressure ---
142
143UinputExternalStylusWithPressure::UinputExternalStylusWithPressure()
144 : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}) {}
145
146void UinputExternalStylusWithPressure::configureDevice(int fd, uinput_user_dev* device) {
147 UinputKeyboard::configureDevice(fd, device);
148
149 ioctl(fd, UI_SET_EVBIT, EV_ABS);
150 ioctl(fd, UI_SET_ABSBIT, ABS_PRESSURE);
151 device->absmin[ABS_PRESSURE] = RAW_PRESSURE_MIN;
152 device->absmax[ABS_PRESSURE] = RAW_PRESSURE_MAX;
153}
154
155void UinputExternalStylusWithPressure::setPressure(int32_t pressure) {
156 injectEvent(EV_ABS, ABS_PRESSURE, pressure);
157 injectEvent(EV_SYN, SYN_REPORT, 0);
158}
159
Prabir Pradhan37a819b2023-08-22 23:20:16 +0000160// --- UinputKeyboardWithHidUsage ---
161
162UinputKeyboardWithHidUsage::UinputKeyboardWithHidUsage(std::initializer_list<int> keys)
163 : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, keys) {}
164
165void UinputKeyboardWithHidUsage::configureDevice(int fd, uinput_user_dev* device) {
166 UinputKeyboard::configureDevice(fd, device);
167
168 ioctl(fd, UI_SET_EVBIT, EV_MSC);
169 ioctl(fd, UI_SET_MSCBIT, MSC_SCAN);
170}
171
Arthur Hungaab25622020-01-16 11:22:11 +0800172// --- UinputTouchScreen ---
Prabir Pradhanb7d434e2022-10-14 22:41:38 +0000173
Arpit Singh440bf652023-08-09 09:23:43 +0000174UinputTouchScreen::UinputTouchScreen(const Rect& size, const std::string& physicalPort)
Prabir Pradhan124ea442022-10-28 20:27:44 +0000175 : UinputKeyboard(DEVICE_NAME, PRODUCT_ID,
176 {BTN_TOUCH, BTN_TOOL_PEN, BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}),
Arpit Singh440bf652023-08-09 09:23:43 +0000177 mSize(size),
178 mPhysicalPort(physicalPort) {}
Arthur Hungaab25622020-01-16 11:22:11 +0800179
180void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
Prabir Pradhan124ea442022-10-28 20:27:44 +0000181 UinputKeyboard::configureDevice(fd, device);
182
Arthur Hungaab25622020-01-16 11:22:11 +0800183 // Setup the touch screen device
Arthur Hungaab25622020-01-16 11:22:11 +0800184 ioctl(fd, UI_SET_EVBIT, EV_REL);
185 ioctl(fd, UI_SET_EVBIT, EV_ABS);
186 ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
187 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
188 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
189 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
190 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
191 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000192 ioctl(fd, UI_SET_ABSBIT, ABS_MT_PRESSURE);
Arthur Hungaab25622020-01-16 11:22:11 +0800193 ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
Arpit Singh440bf652023-08-09 09:23:43 +0000194 if (!mPhysicalPort.empty()) {
195 ioctl(fd, UI_SET_PHYS, mPhysicalPort.c_str());
196 }
Arthur Hungaab25622020-01-16 11:22:11 +0800197
198 device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN;
199 device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX;
200 device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN;
201 device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX;
202 device->absmin[ABS_MT_POSITION_X] = mSize.left;
203 device->absmax[ABS_MT_POSITION_X] = mSize.right - 1;
204 device->absmin[ABS_MT_POSITION_Y] = mSize.top;
205 device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1;
206 device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN;
207 device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000208 device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER;
209 device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX;
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000210 device->absmin[ABS_MT_PRESSURE] = RAW_PRESSURE_MIN;
211 device->absmax[ABS_MT_PRESSURE] = RAW_PRESSURE_MAX;
Arthur Hungaab25622020-01-16 11:22:11 +0800212}
213
214void UinputTouchScreen::sendSlot(int32_t slot) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700215 injectEvent(EV_ABS, ABS_MT_SLOT, slot);
Arthur Hungaab25622020-01-16 11:22:11 +0800216}
217
218void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700219 injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
Arthur Hungaab25622020-01-16 11:22:11 +0800220}
221
222void UinputTouchScreen::sendDown(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700223 injectEvent(EV_KEY, BTN_TOUCH, 1);
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000224 injectEvent(EV_ABS, ABS_MT_PRESSURE, RAW_PRESSURE_MAX);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700225 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
226 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800227}
228
229void UinputTouchScreen::sendMove(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700230 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
231 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800232}
233
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000234void UinputTouchScreen::sendPressure(int32_t pressure) {
235 injectEvent(EV_ABS, ABS_MT_PRESSURE, pressure);
236}
237
arthurhungcc7f9802020-04-30 17:55:40 +0800238void UinputTouchScreen::sendPointerUp() {
239 sendTrackingId(0xffffffff);
arthurhungcc7f9802020-04-30 17:55:40 +0800240}
241
Arthur Hungaab25622020-01-16 11:22:11 +0800242void UinputTouchScreen::sendUp() {
243 sendTrackingId(0xffffffff);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700244 injectEvent(EV_KEY, BTN_TOUCH, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800245}
246
247void UinputTouchScreen::sendToolType(int32_t toolType) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700248 injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800249}
250
251void UinputTouchScreen::sendSync() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700252 injectEvent(EV_SYN, SYN_REPORT, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800253}
254
255// Get the center x, y base on the range definition.
256const Point UinputTouchScreen::getCenterPoint() {
257 return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
258}
259
Prabir Pradhane0105c92019-12-26 12:32:13 -0800260} // namespace android