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 #include <cutils/memory.h>
21 #include <fcntl.h>
22 
23 namespace android {
24 
25 // --- UinputDevice ---
26 
UinputDevice(const char * name,int16_t productId)27 UinputDevice::UinputDevice(const char* name, int16_t productId)
28       : mName(name), mProductId(productId) {}
29 
~UinputDevice()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 
init()37 void UinputDevice::init() {
38     mDeviceFd = android::base::unique_fd(open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC));
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;
47     device.id.product = mProductId;
48     device.id.version = 1;
49 
50     ASSERT_NO_FATAL_FAILURE(configureDevice(mDeviceFd, &device));
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 
injectEvent(uint16_t type,uint16_t code,int32_t value)62 void UinputDevice::injectEvent(uint16_t type, uint16_t code, int32_t value) {
63     // uinput ignores the timestamp
64     struct input_event event = {};
65     event.type = type;
66     event.code = code;
67     event.value = value;
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());
74         FAIL() << msg.c_str();
75     }
76 }
77 
78 // --- UinputKeyboard ---
79 
UinputKeyboard(const char * name,int16_t productId,std::initializer_list<int> keys)80 UinputKeyboard::UinputKeyboard(const char* name, int16_t productId, std::initializer_list<int> keys)
81       : UinputDevice(name, productId), mKeys(keys.begin(), keys.end()) {}
82 
configureDevice(int fd,uinput_user_dev * device)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)) {
86         FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_KEY: " << strerror(errno);
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)) {
92             FAIL() << "Error in ioctl : UI_SET_KEYBIT : " << key << " : " << strerror(errno);
93         }
94     });
95 
96     // enable synchronization event
97     if (ioctl(fd, UI_SET_EVBIT, EV_SYN)) {
98         FAIL() << "Error in ioctl : UI_SET_EVBIT : EV_SYN: " << strerror(errno);
99     }
100 }
101 
pressKey(int key)102 void UinputKeyboard::pressKey(int key) {
103     if (mKeys.find(key) == mKeys.end()) {
104         FAIL() << mName << ": Cannot inject key press: Key not found: " << key;
105     }
106     injectEvent(EV_KEY, key, 1);
107     injectEvent(EV_SYN, SYN_REPORT, 0);
108 }
109 
releaseKey(int key)110 void UinputKeyboard::releaseKey(int key) {
111     if (mKeys.find(key) == mKeys.end()) {
112         FAIL() << mName << ": Cannot inject key release: Key not found: " << key;
113     }
114     injectEvent(EV_KEY, key, 0);
115     injectEvent(EV_SYN, SYN_REPORT, 0);
116 }
117 
pressAndReleaseKey(int key)118 void UinputKeyboard::pressAndReleaseKey(int key) {
119     pressKey(key);
120     releaseKey(key);
121 }
122 
123 // --- UinputHomeKey ---
124 
UinputHomeKey()125 UinputHomeKey::UinputHomeKey() : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {KEY_HOME}) {}
126 
pressAndReleaseHomeKey()127 void UinputHomeKey::pressAndReleaseHomeKey() {
128     pressAndReleaseKey(KEY_HOME);
129 }
130 
131 // --- UinputSteamController ---
132 
UinputSteamController()133 UinputSteamController::UinputSteamController()
134       : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_GEAR_DOWN, BTN_GEAR_UP}) {}
135 
136 // --- UinputExternalStylus ---
137 
UinputExternalStylus()138 UinputExternalStylus::UinputExternalStylus()
139       : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}) {}
140 
141 // --- UinputExternalStylusWithPressure ---
142 
UinputExternalStylusWithPressure()143 UinputExternalStylusWithPressure::UinputExternalStylusWithPressure()
144       : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, {BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}) {}
145 
configureDevice(int fd,uinput_user_dev * device)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 
setPressure(int32_t pressure)155 void UinputExternalStylusWithPressure::setPressure(int32_t pressure) {
156     injectEvent(EV_ABS, ABS_PRESSURE, pressure);
157     injectEvent(EV_SYN, SYN_REPORT, 0);
158 }
159 
160 // --- UinputKeyboardWithHidUsage ---
161 
UinputKeyboardWithHidUsage(std::initializer_list<int> keys)162 UinputKeyboardWithHidUsage::UinputKeyboardWithHidUsage(std::initializer_list<int> keys)
163       : UinputKeyboard(DEVICE_NAME, PRODUCT_ID, keys) {}
164 
configureDevice(int fd,uinput_user_dev * device)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 
172 // --- UinputTouchScreen ---
173 
UinputTouchScreen(const Rect & size,const std::string & physicalPort)174 UinputTouchScreen::UinputTouchScreen(const Rect& size, const std::string& physicalPort)
175       : UinputKeyboard(DEVICE_NAME, PRODUCT_ID,
176                        {BTN_TOUCH, BTN_TOOL_PEN, BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3}),
177         mSize(size),
178         mPhysicalPort(physicalPort) {}
179 
configureDevice(int fd,uinput_user_dev * device)180 void UinputTouchScreen::configureDevice(int fd, uinput_user_dev* device) {
181     UinputKeyboard::configureDevice(fd, device);
182 
183     // Setup the touch screen device
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);
192     ioctl(fd, UI_SET_ABSBIT, ABS_MT_PRESSURE);
193     ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
194     if (!mPhysicalPort.empty()) {
195         ioctl(fd, UI_SET_PHYS, mPhysicalPort.c_str());
196     }
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;
208     device->absmin[ABS_MT_TOOL_TYPE] = MT_TOOL_FINGER;
209     device->absmax[ABS_MT_TOOL_TYPE] = MT_TOOL_MAX;
210     device->absmin[ABS_MT_PRESSURE] = RAW_PRESSURE_MIN;
211     device->absmax[ABS_MT_PRESSURE] = RAW_PRESSURE_MAX;
212 }
213 
sendSlot(int32_t slot)214 void UinputTouchScreen::sendSlot(int32_t slot) {
215     injectEvent(EV_ABS, ABS_MT_SLOT, slot);
216 }
217 
sendTrackingId(int32_t trackingId)218 void UinputTouchScreen::sendTrackingId(int32_t trackingId) {
219     injectEvent(EV_ABS, ABS_MT_TRACKING_ID, trackingId);
220 }
221 
sendDown(const Point & point)222 void UinputTouchScreen::sendDown(const Point& point) {
223     injectEvent(EV_KEY, BTN_TOUCH, 1);
224     injectEvent(EV_ABS, ABS_MT_PRESSURE, RAW_PRESSURE_MAX);
225     injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
226     injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
227 }
228 
sendMove(const Point & point)229 void UinputTouchScreen::sendMove(const Point& point) {
230     injectEvent(EV_ABS, ABS_MT_POSITION_X, point.x);
231     injectEvent(EV_ABS, ABS_MT_POSITION_Y, point.y);
232 }
233 
sendPressure(int32_t pressure)234 void UinputTouchScreen::sendPressure(int32_t pressure) {
235     injectEvent(EV_ABS, ABS_MT_PRESSURE, pressure);
236 }
237 
sendPointerUp()238 void UinputTouchScreen::sendPointerUp() {
239     sendTrackingId(0xffffffff);
240 }
241 
sendUp()242 void UinputTouchScreen::sendUp() {
243     sendTrackingId(0xffffffff);
244     injectEvent(EV_KEY, BTN_TOUCH, 0);
245 }
246 
sendToolType(int32_t toolType)247 void UinputTouchScreen::sendToolType(int32_t toolType) {
248     injectEvent(EV_ABS, ABS_MT_TOOL_TYPE, toolType);
249 }
250 
sendSync()251 void UinputTouchScreen::sendSync() {
252     injectEvent(EV_SYN, SYN_REPORT, 0);
253 }
254 
255 // Get the center x, y base on the range definition.
getCenterPoint()256 const Point UinputTouchScreen::getCenterPoint() {
257     return Point(mSize.left + mSize.width() / 2, mSize.top + mSize.height() / 2);
258 }
259 
260 } // namespace android
261