1 /* 2 * Copyright (c) 2022-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 16 #ifndef DEVICE_MANAGER_H 17 #define DEVICE_MANAGER_H 18 19 #include <future> 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <unordered_map> 24 25 #include "nocopyable.h" 26 27 #include "enumerator.h" 28 #include "i_context.h" 29 #include "i_device_mgr.h" 30 #include "epoll_manager.h" 31 #include "monitor.h" 32 33 namespace OHOS { 34 namespace Msdp { 35 namespace DeviceStatus { 36 class DeviceManager final : public IDeviceManager, 37 public IEpollEventSource { 38 public: 39 DeviceManager(); 40 DISALLOW_COPY_AND_MOVE(DeviceManager); 41 ~DeviceManager() = default; 42 43 int32_t Init(IContext *context); 44 int32_t Enable(); 45 int32_t Disable(); 46 int32_t GetFd() const override; 47 void Dispatch(const struct epoll_event &ev) override; 48 std::shared_ptr<IDevice> GetDevice(int32_t id) const override; 49 int32_t AddDeviceObserver(std::weak_ptr<IDeviceObserver> observer) override; 50 void RemoveDeviceObserver(std::weak_ptr<IDeviceObserver> observer) override; 51 void RetriggerHotplug(std::weak_ptr<IDeviceObserver> observer) override; 52 bool AnyOf(std::function<bool(std::shared_ptr<IDevice>)> pred) override; 53 bool HasLocalPointerDevice() override; 54 bool HasLocalKeyboardDevice() override; 55 bool HasKeyboard() override; 56 std::vector<std::shared_ptr<IDevice>> GetKeyboard() override; 57 std::vector<std::shared_ptr<IDevice>> GetPointerDevice() override; 58 std::vector<std::shared_ptr<IDevice>> GetVirTrackPad() override; 59 60 private: 61 class HotplugHandler final : public IDeviceMgr { 62 public: 63 explicit HotplugHandler(DeviceManager &devMgr); 64 ~HotplugHandler() = default; 65 66 void AddDevice(const std::string &devNode) override; 67 void RemoveDevice(const std::string &devNode) override; 68 69 private: 70 DeviceManager &devMgr_; 71 }; 72 73 private: 74 int32_t OnInit(IContext *context); 75 int32_t OnEnable(); 76 int32_t OnDisable(); 77 int32_t OnEpollDispatch(uint32_t events); 78 int32_t ParseDeviceId(const std::string &devNode); 79 void OnDeviceRemoved(std::shared_ptr<IDevice> dev); 80 void OnDeviceAdded(std::shared_ptr<IDevice> dev); 81 void DeviceInfo(std::shared_ptr<IDevice> dev); 82 int32_t OnAddDeviceObserver(std::weak_ptr<IDeviceObserver> observer); 83 int32_t OnRemoveDeviceObserver(std::weak_ptr<IDeviceObserver> observer); 84 int32_t OnRetriggerHotplug(std::weak_ptr<IDeviceObserver> observer); 85 int32_t RunGetDevice(std::packaged_task<std::shared_ptr<IDevice>(int32_t)> &task, int32_t id) const; 86 std::shared_ptr<IDevice> OnGetDevice(int32_t id) const; 87 std::shared_ptr<IDevice> AddDevice(const std::string &devNode); 88 std::shared_ptr<IDevice> RemoveDevice(const std::string &devNode); 89 std::shared_ptr<IDevice> FindDevice(const std::string &devPath); 90 bool IsSpecialPointerDevice(std::shared_ptr<IDevice> dev); 91 bool IsLocalPointerDevice(std::shared_ptr<MMI::InputDevice> dev); 92 bool IsVirtualTrackpad(std::shared_ptr<MMI::InputDevice> dev); 93 94 private: 95 IContext *context_ { nullptr }; 96 Enumerator enumerator_; 97 HotplugHandler hotplug_; 98 EpollManager epollMgr_; 99 std::shared_ptr<Monitor> monitor_ { nullptr }; 100 std::set<std::weak_ptr<IDeviceObserver>> observers_; 101 std::unordered_map<int32_t, std::shared_ptr<IDevice>> devices_; 102 }; 103 GetFd()104inline int32_t DeviceManager::GetFd() const 105 { 106 return epollMgr_.GetFd(); 107 } 108 } // namespace DeviceStatus 109 } // namespace Msdp 110 } // namespace OHOS 111 #endif // DEVICE_MANAGER_H