blob: 19f7bb44097c6b95406e2579d6170351f6eef2b6 [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
Arthur Hungaab25622020-01-16 11:22:11 +0800160// --- UinputTouchScreen ---
Prabir Pradhanb7d434e2022-10-14 22:41:38 +0000161
Arpit Singh440bf652023-08-09 09:23:43 +0000162UinputTouchScreen::UinputTouchScreen(const Rect& size, const std::string& physicalPort)
Prabir Pradhan124ea442022-10-28 20:27:44 +0000163 : UinputKeyboard(DEVICE_NAME, PRODUCT_ID,
164 {BTN_TOUCH, BTN_TOOL_PEN, BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}),
Arpit Singh440bf652023-08-09 09:23:43 +0000165 mSize(size),
166 mPhysicalPort(physicalPort) {}
Arthur Hungaab25622020-01-16 11:22:11 +0800167
168void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
Prabir Pradhan124ea442022-10-28 20:27:44 +0000169 UinputKeyboard::configureDevice(fd, device);
170
Arthur Hungaab25622020-01-16 11:22:11 +0800171 // Setup the touch screen device
Arthur Hungaab25622020-01-16 11:22:11 +0800172 ioctl(fd, UI_SET_EVBIT, EV_REL);
173 ioctl(fd, UI_SET_EVBIT, EV_ABS);
174 ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
175 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
176 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
177 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
178 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
179 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000180 ioctl(fd, UI_SET_ABSBIT, ABS_MT_PRESSURE);
Arthur Hungaab25622020-01-16 11:22:11 +0800181 ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
Arpit Singh440bf652023-08-09 09:23:43 +0000182 if (!mPhysicalPort.empty()) {
183 ioctl(fd, UI_SET_PHYS, mPhysicalPort.c_str());
184 }
Arthur Hungaab25622020-01-16 11:22:11 +0800185
186 device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN;
187 device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX;
188 device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN;
189 device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX;
190 device->absmin[ABS_MT_POSITION_X] = mSize.left;
191 device->absmax[ABS_MT_POSITION_X] = mSize.right - 1;
192 device->absmin[ABS_MT_POSITION_Y] = mSize.top;
193 device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1;
194 device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN;
195 device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000196 device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER;
197 device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX;
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000198 device->absmin[ABS_MT_PRESSURE] = RAW_PRESSURE_MIN;
199 device->absmax[ABS_MT_PRESSURE] = RAW_PRESSURE_MAX;
Arthur Hungaab25622020-01-16 11:22:11 +0800200}
201
202void UinputTouchScreen::sendSlot(int32_t slot) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700203 injectEvent(EV_ABS, ABS_MT_SLOT, slot);
Arthur Hungaab25622020-01-16 11:22:11 +0800204}
205
206void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700207 injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
Arthur Hungaab25622020-01-16 11:22:11 +0800208}
209
210void UinputTouchScreen::sendDown(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700211 injectEvent(EV_KEY, BTN_TOUCH, 1);
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000212 injectEvent(EV_ABS, ABS_MT_PRESSURE, RAW_PRESSURE_MAX);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700213 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
214 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800215}
216
217void UinputTouchScreen::sendMove(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700218 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
219 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800220}
221
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +0000222void UinputTouchScreen::sendPressure(int32_t pressure) {
223 injectEvent(EV_ABS, ABS_MT_PRESSURE, pressure);
224}
225
arthurhungcc7f9802020-04-30 17:55:40 +0800226void UinputTouchScreen::sendPointerUp() {
227 sendTrackingId(0xffffffff);
arthurhungcc7f9802020-04-30 17:55:40 +0800228}
229
Arthur Hungaab25622020-01-16 11:22:11 +0800230void UinputTouchScreen::sendUp() {
231 sendTrackingId(0xffffffff);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700232 injectEvent(EV_KEY, BTN_TOUCH, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800233}
234
235void UinputTouchScreen::sendToolType(int32_t toolType) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700236 injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800237}
238
239void UinputTouchScreen::sendSync() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700240 injectEvent(EV_SYN, SYN_REPORT, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800241}
242
243// Get the center x, y base on the range definition.
244const Point UinputTouchScreen::getCenterPoint() {
245 return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
246}
247
Prabir Pradhane0105c92019-12-26 12:32:13 -0800248} // namespace android