Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 1 | /* |
| 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 Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 20 | #include <cutils/memory.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 21 | #include <fcntl.h> |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // --- UinputDevice --- |
| 26 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 27 | UinputDevice::UinputDevice(const char* name, int16_t productId) |
| 28 | : mName(name), mProductId(productId) {} |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 29 | |
| 30 | UinputDevice::~UinputDevice() { |
| 31 | if (ioctl(mDeviceFd, UI_DEV_DESTROY)) { |
| 32 | ALOGE("Error while destroying uinput device: %s", strerror(errno)); |
| 33 | } |
| 34 | mDeviceFd.reset(); |
| 35 | } |
| 36 | |
| 37 | void UinputDevice::init() { |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 38 | mDeviceFd = android::base::unique_fd(open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC)); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 39 | 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 Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 47 | device.id.product = mProductId; |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 48 | device.id.version = 1; |
| 49 | |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 50 | ASSERT_NO_FATAL_FAILURE(configureDevice(mDeviceFd, &device)); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 51 | |
| 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 | |
| 62 | void UinputDevice::injectEvent(uint16_t type, uint16_t code, int32_t value) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 63 | // uinput ignores the timestamp |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 64 | struct input_event event = {}; |
| 65 | event.type = type; |
| 66 | event.code = code; |
| 67 | event.value = value; |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 68 | |
| 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 Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 74 | FAIL() << msg.c_str(); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | // --- UinputKeyboard --- |
| 79 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 80 | UinputKeyboard::UinputKeyboard(const char* name, int16_t productId, std::initializer_list<int> keys) |
| 81 | : UinputDevice(name, productId), mKeys(keys.begin(), keys.end()) {} |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 82 | |
| 83 | void UinputKeyboard::configureDevice(int fd, uinput_user_dev* device) { |
| 84 | // enable key press/release event |
| 85 | if (ioctl(fd, UI_SET_EVBIT, EV_KEY)) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 86 | FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_KEY: " << strerror(errno); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 87 | } |
| 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 Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 92 | FAIL() << "Error in ioctl : UI_SET_KEYBIT : " << key << " : " << strerror(errno); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 93 | } |
| 94 | }); |
| 95 | |
| 96 | // enable synchronization event |
| 97 | if (ioctl(fd, UI_SET_EVBIT, EV_SYN)) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 98 | FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_SYN: " << strerror(errno); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | void UinputKeyboard::pressKey(int key) { |
| 103 | if (mKeys.find(key) == mKeys.end()) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 104 | FAIL() << mName << ": Cannot inject key press: Key not found: " << key; |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 105 | } |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 106 | injectEvent(EV_KEY, key, 1); |
| 107 | injectEvent(EV_SYN, SYN_REPORT, 0); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void UinputKeyboard::releaseKey(int key) { |
| 111 | if (mKeys.find(key) == mKeys.end()) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 112 | FAIL() << mName << ": Cannot inject key release: Key not found: " << key; |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 113 | } |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 114 | injectEvent(EV_KEY, key, 0); |
| 115 | injectEvent(EV_SYN, SYN_REPORT, 0); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void UinputKeyboard::pressAndReleaseKey(int key) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 119 | pressKey(key); |
| 120 | releaseKey(key); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 123 | // --- UinputHomeKey --- |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 124 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 125 | UinputHomeKey::UinputHomeKey() : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {KEY_HOME}) {} |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 126 | |
| 127 | void UinputHomeKey::pressAndReleaseHomeKey() { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 128 | pressAndReleaseKey(KEY_HOME); |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 131 | // --- UinputSteamController --- |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 132 | |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 133 | UinputSteamController::UinputSteamController() |
| 134 | : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_GEAR_DOWN, BTN_GEAR_UP}) {} |
| 135 | |
| 136 | // --- UinputExternalStylus --- |
| 137 | |
Prabir Pradhan | e1a41a8 | 2022-10-14 18:06:50 +0000 | [diff] [blame] | 138 | UinputExternalStylus::UinputExternalStylus() |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 139 | : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}) {} |
Siarhei Vishniakou | a0d2b80 | 2020-05-13 14:00:31 -0700 | [diff] [blame] | 140 | |
Prabir Pradhan | 484d55a | 2022-10-14 23:17:16 +0000 | [diff] [blame] | 141 | // --- UinputExternalStylusWithPressure --- |
| 142 | |
| 143 | UinputExternalStylusWithPressure::UinputExternalStylusWithPressure() |
| 144 | : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}) {} |
| 145 | |
| 146 | void 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 | |
| 155 | void UinputExternalStylusWithPressure::setPressure(int32_t pressure) { |
| 156 | injectEvent(EV_ABS, ABS_PRESSURE, pressure); |
| 157 | injectEvent(EV_SYN, SYN_REPORT, 0); |
| 158 | } |
| 159 | |
Prabir Pradhan | 37a819b | 2023-08-22 23:20:16 +0000 | [diff] [blame] | 160 | // --- UinputKeyboardWithHidUsage --- |
| 161 | |
| 162 | UinputKeyboardWithHidUsage::UinputKeyboardWithHidUsage(std::initializer_list<int> keys) |
| 163 | : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, keys) {} |
| 164 | |
| 165 | void 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 Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 172 | // --- UinputTouchScreen --- |
Prabir Pradhan | b7d434e | 2022-10-14 22:41:38 +0000 | [diff] [blame] | 173 | |
Arpit Singh | 440bf65 | 2023-08-09 09:23:43 +0000 | [diff] [blame] | 174 | UinputTouchScreen::UinputTouchScreen(const Rect& size, const std::string& physicalPort) |
Prabir Pradhan | 124ea44 | 2022-10-28 20:27:44 +0000 | [diff] [blame] | 175 | : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, |
| 176 | {BTN_TOUCH, BTN_TOOL_PEN, BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}), |
Arpit Singh | 440bf65 | 2023-08-09 09:23:43 +0000 | [diff] [blame] | 177 | mSize(size), |
| 178 | mPhysicalPort(physicalPort) {} |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 179 | |
| 180 | void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) { |
Prabir Pradhan | 124ea44 | 2022-10-28 20:27:44 +0000 | [diff] [blame] | 181 | UinputKeyboard::configureDevice(fd, device); |
| 182 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 183 | // Setup the touch screen device |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 184 | 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 Pradhan | c09ec6d | 2023-08-14 22:31:43 +0000 | [diff] [blame] | 192 | ioctl(fd, UI_SET_ABSBIT, ABS_MT_PRESSURE); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 193 | ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT); |
Arpit Singh | 440bf65 | 2023-08-09 09:23:43 +0000 | [diff] [blame] | 194 | if (!mPhysicalPort.empty()) { |
| 195 | ioctl(fd, UI_SET_PHYS, mPhysicalPort.c_str()); |
| 196 | } |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 197 | |
| 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 Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 208 | device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER; |
| 209 | device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX; |
Prabir Pradhan | c09ec6d | 2023-08-14 22:31:43 +0000 | [diff] [blame] | 210 | device->absmin[ABS_MT_PRESSURE] = RAW_PRESSURE_MIN; |
| 211 | device->absmax[ABS_MT_PRESSURE] = RAW_PRESSURE_MAX; |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void UinputTouchScreen::sendSlot(int32_t slot) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 215 | injectEvent(EV_ABS, ABS_MT_SLOT, slot); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void UinputTouchScreen::sendTrackingId(int32_t trackingId) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 219 | injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void UinputTouchScreen::sendDown(const Point& point) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 223 | injectEvent(EV_KEY, BTN_TOUCH, 1); |
Prabir Pradhan | c09ec6d | 2023-08-14 22:31:43 +0000 | [diff] [blame] | 224 | injectEvent(EV_ABS, ABS_MT_PRESSURE, RAW_PRESSURE_MAX); |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 225 | injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x); |
| 226 | injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void UinputTouchScreen::sendMove(const Point& point) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 230 | injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x); |
| 231 | injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Prabir Pradhan | c09ec6d | 2023-08-14 22:31:43 +0000 | [diff] [blame] | 234 | void UinputTouchScreen::sendPressure(int32_t pressure) { |
| 235 | injectEvent(EV_ABS, ABS_MT_PRESSURE, pressure); |
| 236 | } |
| 237 | |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 238 | void UinputTouchScreen::sendPointerUp() { |
| 239 | sendTrackingId(0xffffffff); |
arthurhung | cc7f980 | 2020-04-30 17:55:40 +0800 | [diff] [blame] | 240 | } |
| 241 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 242 | void UinputTouchScreen::sendUp() { |
| 243 | sendTrackingId(0xffffffff); |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 244 | injectEvent(EV_KEY, BTN_TOUCH, 0); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void UinputTouchScreen::sendToolType(int32_t toolType) { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 248 | injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType); |
Siarhei Vishniakou | fd97e9d | 2022-01-04 16:59:04 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void UinputTouchScreen::sendSync() { |
Siarhei Vishniakou | e5b5e45 | 2020-04-02 17:59:16 -0700 | [diff] [blame] | 252 | injectEvent(EV_SYN, SYN_REPORT, 0); |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Get the center x, y base on the range definition. |
| 256 | const Point UinputTouchScreen::getCenterPoint() { |
| 257 | return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2); |
| 258 | } |
| 259 | |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 260 | } // namespace android |