blob: 2775d21ce2bcb23fe231cc57ded61f892cb82406 [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>
20
21namespace android {
22
23// --- UinputDevice ---
24
25UinputDevice::UinputDevice(const char* name) : mName(name) {}
26
27UinputDevice::~UinputDevice() {
28 if (ioctl(mDeviceFd, UI_DEV_DESTROY)) {
29 ALOGE("Error while destroying uinput device: %s", strerror(errno));
30 }
31 mDeviceFd.reset();
32}
33
34void 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
61void 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
79UinputKeyboard::UinputKeyboard(std::initializer_list<int> keys)
80 : UinputDevice(UinputKeyboard::KEYBOARD_NAME), mKeys(keys.begin(), keys.end()) {}
81
82void 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
101void 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
109void 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
117void UinputKeyboard::pressAndReleaseKey(int key) {
118 EXPECT_NO_FATAL_FAILURE(pressKey(key));
119 EXPECT_NO_FATAL_FAILURE(releaseKey(key));
120}
121
122// --- UinputHomeKey---
123
124UinputHomeKey::UinputHomeKey() : UinputKeyboard({KEY_HOME}) {}
125
126void UinputHomeKey::pressAndReleaseHomeKey() {
127 EXPECT_NO_FATAL_FAILURE(pressAndReleaseKey(KEY_HOME));
128}
129
130} // namespace android