blob: bc695b8bd8b92039db1e89e95039a2d2d5a3e231 [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
Arthur Hungaab25622020-01-16 11:22:11 +0800141// --- UinputTouchScreen ---
Prabir Pradhanb7d434e2022-10-14 22:41:38 +0000142
143UinputTouchScreen::UinputTouchScreen(const Rect& size)
144 : UinputDevice(DEVICE_NAME, PRODUCT_ID), mSize(size) {}
Arthur Hungaab25622020-01-16 11:22:11 +0800145
146void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
147 // Setup the touch screen device
148 ioctl(fd, UI_SET_EVBIT, EV_KEY);
149 ioctl(fd, UI_SET_EVBIT, EV_REL);
150 ioctl(fd, UI_SET_EVBIT, EV_ABS);
151 ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT);
152 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR);
153 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X);
154 ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y);
155 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID);
156 ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE);
157 ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
158 ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000159 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS);
160 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS2);
161 ioctl(fd, UI_SET_KEYBIT, BTN_STYLUS3);
Arthur Hungaab25622020-01-16 11:22:11 +0800162
163 device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN;
164 device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX;
165 device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN;
166 device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX;
167 device->absmin[ABS_MT_POSITION_X] = mSize.left;
168 device->absmax[ABS_MT_POSITION_X] = mSize.right - 1;
169 device->absmin[ABS_MT_POSITION_Y] = mSize.top;
170 device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1;
171 device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN;
172 device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000173 device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER;
174 device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX;
Arthur Hungaab25622020-01-16 11:22:11 +0800175}
176
177void UinputTouchScreen::sendSlot(int32_t slot) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700178 injectEvent(EV_ABS, ABS_MT_SLOT, slot);
Arthur Hungaab25622020-01-16 11:22:11 +0800179}
180
181void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700182 injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
Arthur Hungaab25622020-01-16 11:22:11 +0800183}
184
185void UinputTouchScreen::sendDown(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700186 injectEvent(EV_KEY, BTN_TOUCH, 1);
187 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
188 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800189}
190
191void UinputTouchScreen::sendMove(const Point& point) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700192 injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
193 injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
Arthur Hungaab25622020-01-16 11:22:11 +0800194}
195
arthurhungcc7f9802020-04-30 17:55:40 +0800196void UinputTouchScreen::sendPointerUp() {
197 sendTrackingId(0xffffffff);
arthurhungcc7f9802020-04-30 17:55:40 +0800198}
199
Arthur Hungaab25622020-01-16 11:22:11 +0800200void UinputTouchScreen::sendUp() {
201 sendTrackingId(0xffffffff);
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700202 injectEvent(EV_KEY, BTN_TOUCH, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800203}
204
205void UinputTouchScreen::sendToolType(int32_t toolType) {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700206 injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -0800207}
208
209void UinputTouchScreen::sendSync() {
Siarhei Vishniakoue5b5e452020-04-02 17:59:16 -0700210 injectEvent(EV_SYN, SYN_REPORT, 0);
Arthur Hungaab25622020-01-16 11:22:11 +0800211}
212
Prabir Pradhane1a41a82022-10-14 18:06:50 +0000213void UinputTouchScreen::sendKey(int32_t scanCode, int32_t value) {
214 injectEvent(EV_KEY, scanCode, value);
215}
216
Arthur Hungaab25622020-01-16 11:22:11 +0800217// Get the center x, y base on the range definition.
218const Point UinputTouchScreen::getCenterPoint() {
219 return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
220}
221
Prabir Pradhane0105c92019-12-26 12:32:13 -0800222} // namespace android