• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void SetPencilAirMouse(bool existAirMouse) override;
60     bool HasPencilAirMouse() override;
61 
62 private:
63     class HotplugHandler final : public IDeviceMgr {
64     public:
65         explicit HotplugHandler(DeviceManager &devMgr);
66         ~HotplugHandler() = default;
67 
68         void AddDevice(const std::string &devNode) override;
69         void RemoveDevice(const std::string &devNode) override;
70 
71     private:
72         DeviceManager &devMgr_;
73     };
74 
75 private:
76     int32_t OnInit(IContext *context);
77     int32_t OnEnable();
78     int32_t OnDisable();
79     int32_t OnEpollDispatch(uint32_t events);
80     int32_t ParseDeviceId(const std::string &devNode);
81     void OnDeviceRemoved(std::shared_ptr<IDevice> dev);
82     void OnDeviceAdded(std::shared_ptr<IDevice> dev);
83     void DeviceInfo(std::shared_ptr<IDevice> dev);
84     int32_t OnAddDeviceObserver(std::weak_ptr<IDeviceObserver> observer);
85     int32_t OnRemoveDeviceObserver(std::weak_ptr<IDeviceObserver> observer);
86     int32_t OnRetriggerHotplug(std::weak_ptr<IDeviceObserver> observer);
87     int32_t RunGetDevice(std::packaged_task<std::shared_ptr<IDevice>(int32_t)> &task, int32_t id) const;
88     std::shared_ptr<IDevice> OnGetDevice(int32_t id) const;
89     std::shared_ptr<IDevice> AddDevice(const std::string &devNode);
90     std::shared_ptr<IDevice> RemoveDevice(const std::string &devNode);
91     std::shared_ptr<IDevice> FindDevice(const std::string &devPath);
92     bool IsFakePointerDevice(std::shared_ptr<IDevice> dev);
93     bool IsLocalPointerDevice(std::shared_ptr<MMI::InputDevice> dev);
94     bool IsVirtualTrackpad(std::shared_ptr<MMI::InputDevice> dev);
95 
96 private:
97     IContext *context_ { nullptr };
98     Enumerator enumerator_;
99     HotplugHandler hotplug_;
100     EpollManager epollMgr_;
101     std::atomic_bool hasPencilAirMouse_ { false };
102     std::shared_ptr<Monitor> monitor_ { nullptr };
103     std::set<std::weak_ptr<IDeviceObserver>> observers_;
104     std::unordered_map<int32_t, std::shared_ptr<IDevice>> devices_;
105 };
106 
GetFd()107 inline int32_t DeviceManager::GetFd() const
108 {
109     return epollMgr_.GetFd();
110 }
111 } // namespace DeviceStatus
112 } // namespace Msdp
113 } // namespace OHOS
114 #endif // DEVICE_MANAGER_H