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 <android-base/unique_fd.h> 18 #include <jni.h> 19 #include <linux/input.h> 20 21 #include <chrono> 22 #include <memory> 23 #include <vector> 24 25 #include "src/com/android/commands/uinput/InputAbsInfo.h" 26 27 namespace android { 28 namespace uinput { 29 30 class DeviceCallback { 31 public: 32 DeviceCallback(JNIEnv* env, jobject callback); 33 ~DeviceCallback(); 34 35 void onDeviceOpen(); 36 void onDeviceGetReport(uint32_t requestId, uint8_t reportId); 37 void onDeviceOutput(const std::vector<uint8_t>& data); 38 void onDeviceConfigure(int handle); 39 void onDeviceVibrating(int value); 40 void onDeviceError(); 41 42 private: 43 JNIEnv* getJNIEnv(); 44 jobject mCallbackObject; 45 JavaVM* mJavaVM; 46 }; 47 48 class UinputDevice { 49 public: 50 static std::unique_ptr<UinputDevice> open(int32_t id, const char* name, int32_t vendorId, 51 int32_t productId, int32_t versionId, uint16_t bus, 52 uint32_t ff_effects_max, const char* port, 53 std::unique_ptr<DeviceCallback> callback); 54 55 virtual ~UinputDevice(); 56 57 void injectEvent(std::chrono::microseconds timestamp, uint16_t type, uint16_t code, 58 int32_t value); 59 int handleEvents(int events); 60 61 private: 62 UinputDevice(int32_t id, android::base::unique_fd fd, std::unique_ptr<DeviceCallback> callback); 63 64 int32_t mId; 65 android::base::unique_fd mFd; 66 std::unique_ptr<DeviceCallback> mDeviceCallback; 67 }; 68 69 } // namespace uinput 70 } // namespace android 71