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> |
| 20 | |
| 21 | namespace android { |
| 22 | |
| 23 | // --- UinputDevice --- |
| 24 | |
| 25 | UinputDevice::UinputDevice(const char* name) : mName(name) {} |
| 26 | |
| 27 | UinputDevice::~UinputDevice() { |
| 28 | if (ioctl(mDeviceFd, UI_DEV_DESTROY)) { |
| 29 | ALOGE("Error while destroying uinput device: %s", strerror(errno)); |
| 30 | } |
| 31 | mDeviceFd.reset(); |
| 32 | } |
| 33 | |
| 34 | void UinputDevice::init() { |
| 35 | mDeviceFd = android::base::unique_fd(open("/dev/uinput", O_WRONLY | O_NONBLOCK)); |
| 36 | if (mDeviceFd < 0) { |
| 37 | FAIL() << "Can't open /dev/uinput :" << strerror(errno); |
| 38 | } |
| 39 | |
| 40 | struct uinput_user_dev device = {}; |
| 41 | strlcpy(device.name, mName, UINPUT_MAX_NAME_SIZE); |
| 42 | device.id.bustype = BUS_USB; |
| 43 | device.id.vendor = 0x01; |
| 44 | device.id.product = 0x01; |
| 45 | device.id.version = 1; |
| 46 | |
| 47 | // Using EXPECT instead of ASSERT to allow the device creation to continue even when |
| 48 | // some failures are reported when configuring the device. |
| 49 | EXPECT_NO_FATAL_FAILURE(configureDevice(mDeviceFd, &device)); |
| 50 | |
| 51 | if (write(mDeviceFd, &device, sizeof(device)) < 0) { |
| 52 | FAIL() << "Could not write uinput_user_dev struct into uinput file descriptor: " |
| 53 | << strerror(errno); |
| 54 | } |
| 55 | |
| 56 | if (ioctl(mDeviceFd, UI_DEV_CREATE)) { |
| 57 | FAIL() << "Error in ioctl : UI_DEV_CREATE: " << strerror(errno); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void UinputDevice::injectEvent(uint16_t type, uint16_t code, int32_t value) { |
| 62 | struct input_event event = {}; |
| 63 | event.type = type; |
| 64 | event.code = code; |
| 65 | event.value = value; |
| 66 | event.time = {}; // uinput ignores the timestamp |
| 67 | |
| 68 | if (write(mDeviceFd, &event, sizeof(input_event)) < 0) { |
| 69 | std::string msg = base::StringPrintf("Could not write event %" PRIu16 " %" PRIu16 |
| 70 | " with value %" PRId32 " : %s", |
| 71 | type, code, value, strerror(errno)); |
| 72 | ALOGE("%s", msg.c_str()); |
| 73 | ADD_FAILURE() << msg.c_str(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // --- UinputKeyboard --- |
| 78 | |
| 79 | UinputKeyboard::UinputKeyboard(std::initializer_list<int> keys) |
| 80 | : UinputDevice(UinputKeyboard::KEYBOARD_NAME), mKeys(keys.begin(), keys.end()) {} |
| 81 | |
| 82 | void UinputKeyboard::configureDevice(int fd, uinput_user_dev* device) { |
| 83 | // enable key press/release event |
| 84 | if (ioctl(fd, UI_SET_EVBIT, EV_KEY)) { |
| 85 | ADD_FAILURE() << "Error in ioctl : UI_SET_EVBIT : EV_KEY: " << strerror(errno); |
| 86 | } |
| 87 | |
| 88 | // enable set of KEY events |
| 89 | std::for_each(mKeys.begin(), mKeys.end(), [fd](int key) { |
| 90 | if (ioctl(fd, UI_SET_KEYBIT, key)) { |
| 91 | ADD_FAILURE() << "Error in ioctl : UI_SET_KEYBIT : " << key << " : " << strerror(errno); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | // enable synchronization event |
| 96 | if (ioctl(fd, UI_SET_EVBIT, EV_SYN)) { |
| 97 | ADD_FAILURE() << "Error in ioctl : UI_SET_EVBIT : EV_SYN: " << strerror(errno); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void UinputKeyboard::pressKey(int key) { |
| 102 | if (mKeys.find(key) == mKeys.end()) { |
| 103 | ADD_FAILURE() << mName << ": Cannot inject key press: Key not found: " << key; |
| 104 | } |
| 105 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_KEY, key, 1)); |
| 106 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_SYN, SYN_REPORT, 0)); |
| 107 | } |
| 108 | |
| 109 | void UinputKeyboard::releaseKey(int key) { |
| 110 | if (mKeys.find(key) == mKeys.end()) { |
| 111 | ADD_FAILURE() << mName << ": Cannot inject key release: Key not found: " << key; |
| 112 | } |
| 113 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_KEY, key, 0)); |
| 114 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_SYN, SYN_REPORT, 0)); |
| 115 | } |
| 116 | |
| 117 | void UinputKeyboard::pressAndReleaseKey(int key) { |
| 118 | EXPECT_NO_FATAL_FAILURE(pressKey(key)); |
| 119 | EXPECT_NO_FATAL_FAILURE(releaseKey(key)); |
| 120 | } |
| 121 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame^] | 122 | // --- UinputHomeKey --- |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 123 | |
| 124 | UinputHomeKey::UinputHomeKey() : UinputKeyboard({KEY_HOME}) {} |
| 125 | |
| 126 | void UinputHomeKey::pressAndReleaseHomeKey() { |
| 127 | EXPECT_NO_FATAL_FAILURE(pressAndReleaseKey(KEY_HOME)); |
| 128 | } |
| 129 | |
Arthur Hung | aab2562 | 2020-01-16 11:22:11 +0800 | [diff] [blame^] | 130 | // --- UinputTouchScreen --- |
| 131 | UinputTouchScreen::UinputTouchScreen(const Rect* size) |
| 132 | : UinputDevice(UinputTouchScreen::DEVICE_NAME), mSize(*size) {} |
| 133 | |
| 134 | void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) { |
| 135 | // Setup the touch screen device |
| 136 | ioctl(fd, UI_SET_EVBIT, EV_KEY); |
| 137 | ioctl(fd, UI_SET_EVBIT, EV_REL); |
| 138 | ioctl(fd, UI_SET_EVBIT, EV_ABS); |
| 139 | ioctl(fd, UI_SET_ABSBIT, ABS_MT_SLOT); |
| 140 | ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR); |
| 141 | ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_X); |
| 142 | ioctl(fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y); |
| 143 | ioctl(fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID); |
| 144 | ioctl(fd, UI_SET_ABSBIT, ABS_MT_TOOL_TYPE); |
| 145 | ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT); |
| 146 | ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH); |
| 147 | |
| 148 | device->absmin[ABS_MT_SLOT] = RAW_SLOT_MIN; |
| 149 | device->absmax[ABS_MT_SLOT] = RAW_SLOT_MAX; |
| 150 | device->absmin[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MIN; |
| 151 | device->absmax[ABS_MT_TOUCH_MAJOR] = RAW_TOUCH_MAX; |
| 152 | device->absmin[ABS_MT_POSITION_X] = mSize.left; |
| 153 | device->absmax[ABS_MT_POSITION_X] = mSize.right - 1; |
| 154 | device->absmin[ABS_MT_POSITION_Y] = mSize.top; |
| 155 | device->absmax[ABS_MT_POSITION_Y] = mSize.bottom - 1; |
| 156 | device->absmin[ABS_MT_TRACKING_ID] = RAW_ID_MIN; |
| 157 | device->absmax[ABS_MT_TRACKING_ID] = RAW_ID_MAX; |
| 158 | } |
| 159 | |
| 160 | void UinputTouchScreen::sendSlot(int32_t slot) { |
| 161 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_ABS, ABS_MT_SLOT, slot)); |
| 162 | } |
| 163 | |
| 164 | void UinputTouchScreen::sendTrackingId(int32_t trackingId) { |
| 165 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId)); |
| 166 | } |
| 167 | |
| 168 | void UinputTouchScreen::sendDown(const Point& point) { |
| 169 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_KEY, BTN_TOUCH, 1)); |
| 170 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x)); |
| 171 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y)); |
| 172 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_SYN, SYN_REPORT, 0)); |
| 173 | } |
| 174 | |
| 175 | void UinputTouchScreen::sendMove(const Point& point) { |
| 176 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x)); |
| 177 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y)); |
| 178 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_SYN, SYN_REPORT, 0)); |
| 179 | } |
| 180 | |
| 181 | void UinputTouchScreen::sendUp() { |
| 182 | sendTrackingId(0xffffffff); |
| 183 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_KEY, BTN_TOUCH, 0)); |
| 184 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_SYN, SYN_REPORT, 0)); |
| 185 | } |
| 186 | |
| 187 | void UinputTouchScreen::sendToolType(int32_t toolType) { |
| 188 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType)); |
| 189 | EXPECT_NO_FATAL_FAILURE(injectEvent(EV_SYN, SYN_REPORT, 0)); |
| 190 | } |
| 191 | |
| 192 | // Get the center x, y base on the range definition. |
| 193 | const Point UinputTouchScreen::getCenterPoint() { |
| 194 | return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2); |
| 195 | } |
| 196 | |
Prabir Pradhan | e0105c9 | 2019-12-26 12:32:13 -0800 | [diff] [blame] | 197 | } // namespace android |