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