1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef INPUT_INTERFACE_DEVICE_INFO_H 16 #define INPUT_INTERFACE_DEVICE_INFO_H 17 18 #include <set> 19 #include <thread> 20 #include <unordered_map> 21 #include "libinput.h" 22 #include "input_type.h" 23 #include "input_interface_reporter.h" 24 25 namespace OHOS { 26 namespace Input { 27 constexpr int32_t INVALID_FD = -1; 28 constexpr int32_t MAX_REPORT_EVENT_SIZE = 100; 29 const std::string TOUCH_DEVICE_NAME = "input_mt_wrapper"; 30 31 enum class ThreadState {STOP, RUNNING}; 32 33 class DeviceInfo final { 34 public: DeviceInfo(std::shared_ptr<InputIfReporter> & reporter)35 DeviceInfo(std::shared_ptr<InputIfReporter> &reporter) : reporterSptr(reporter){}; 36 ~DeviceInfo() = default; 37 DeviceInfo(const DeviceInfo &other) = delete; 38 DeviceInfo(DeviceInfo &&other) = delete; 39 DeviceInfo &operator=(const DeviceInfo &other) = delete; 40 DeviceInfo &operator=(DeviceInfo &&other) = delete; 41 42 RetStatus Init(); 43 void Stop(); 44 45 private: 46 RetStatus InitHotPlug(); 47 RetStatus EpollCreate(); 48 RetStatus CreateLibinputContext(); 49 RetStatus AddToEpoll(int32_t fd); 50 RetStatus RemoveEpoll(int32_t fd); 51 void EpollWaitThread(); 52 RetStatus Scan(); 53 void OnHotPlugEvent(); 54 void OnLibinputEvent(); 55 void ProcessPendingEvents(); 56 void ReportLibinputEvent(int eventNum); 57 void OnDeviceAdded(const std::string &path); 58 void OnDeviceRemoved(const std::string &path); 59 RetStatus IsDeviceSupported(const std::string &path, bool &supported); 60 61 int32_t epollFd_{INVALID_FD}; 62 int32_t libinputFd_{INVALID_FD}; 63 int32_t inotifyFd_{INVALID_FD}; 64 65 libinput *input_{nullptr}; 66 std::thread t_; 67 ThreadState threadState_{ThreadState::STOP}; 68 std::shared_ptr<InputIfReporter> reporterSptr{nullptr}; 69 70 std::unordered_map<std::string, libinput_device*> devices_; 71 std::mutex devicesMtx_; 72 std::set<std::string> supportedDevice = {TOUCH_DEVICE_NAME}; 73 IfInputEvent eventInfo_[MAX_REPORT_EVENT_SIZE]; 74 }; 75 } // namespace Input 76 } // namespace OHOS 77 #endif // INPUT_INTERFACE_DEVICE_INFO_H