blob: 1051aa93e58dbdf0869e030d4c78de9dc33edac2 [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
162UinputTouchScreen::UinputTouchScreen(const Rect& size)
163 : UinputDevice(DEVICE_NAME, PRODUCT_ID), mSize(size) {}
Arthur Hungaab25622020-01-16 11:22:11 +0800164
165void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
166 // Setup the touch screen device
167 ioctl(fd, UI_SET_EVBIT, EV_KEY);
168 ioctl(fd, UI_SET_EVBIT, EV_REL);
169 ioctl(fd, UI_SET_EVBIT, EV_ABS);
170 ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
171 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
172 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
173 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
174 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
175 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
176 ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
177 ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000178 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS);
179 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS2);
180 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS3);
Arthur Hungaab25622020-01-16 11:22:11 +0800181
182 device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN;
183 device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX;
184 device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN;
185 device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX;
186 device->absmin[ABS_MT_POSITION_X] = mSize.left;
187 device->absmax[ABS_MT_POSITION_X] = mSize.right - 1;
188 device->absmin[ABS_MT_POSITION_Y] = mSize.top;
189 device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1;
190 device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN;
191 device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000192 device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER;
193 device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX;
Arthur Hungaab25622020-01-16 11:22:11 +0800194}
195
196void UinputTouchScreen::sendSlot(int32_t slot) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700197 injectEvent(EV_ABS, ABS_MT_SLOT, slot);
Arthur Hungaab25622020-01-16 11:22:11 +0800198}
199
200void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700201 injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
Arthur Hungaab25622020-01-16 11:22:11 +0800202}
203
204void UinputTouchScreen::sendDown(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700205 injectEvent(EV_KEY, BTN_TOUCH, 1);
206 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
207 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800208}
209
210void UinputTouchScreen::sendMove(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700211 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
212 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800213}
214
arthurhungcc7f9802020-04-30 17:55:40 +0800215void UinputTouchScreen::sendPointerUp() {
216 sendTrackingId(0xffffffff);
arthurhungcc7f9802020-04-30 17:55:40 +0800217}
218
Arthur Hungaab25622020-01-16 11:22:11 +0800219void UinputTouchScreen::sendUp() {
220 sendTrackingId(0xffffffff);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700221 injectEvent(EV_KEY, BTN_TOUCH, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800222}
223
224void UinputTouchScreen::sendToolType(int32_t toolType) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700225 injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800226}
227
228void UinputTouchScreen::sendSync() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700229 injectEvent(EV_SYN, SYN_REPORT, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800230}
231
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000232void UinputTouchScreen::sendKey(int32_t scanCode, int32_t value) {
233 injectEvent(EV_KEY, scanCode, value);
234}
235
Arthur Hungaab25622020-01-16 11:22:11 +0800236// Get the center x, y base on the range definition.
237const Point UinputTouchScreen::getCenterPoint() {
238 return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
239}
240
Prabir Pradhane0105c92019-12-26 12:32:13 -0800241} // namespace android