1 /*
2  * Copyright (c) 2024 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 COOPERATE_INPUT_DEVICE_MANAGER_H
17 #define COOPERATE_INPUT_DEVICE_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <unordered_map>
23 
24 #include "nocopyable.h"
25 
26 #include "channel.h"
27 #include "cooperate_events.h"
28 #include "i_context.h"
29 #include "net_packet.h"
30 
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 namespace Cooperate {
35 class InputDeviceMgr {
36 public:
37     InputDeviceMgr(IContext *context);
38     ~InputDeviceMgr() = default;
39     DISALLOW_COPY_AND_MOVE(InputDeviceMgr);
40 
41 public:
42     void Enable();
43     void Disable();
44     void OnSoftbusSessionOpened(const DSoftbusSessionOpened &notice);
45     void OnSoftbusSessionClosed(const DSoftbusSessionClosed &notice);
46     void OnLocalHotPlug(const InputHotplugEvent &notice);
47     void AddVirtualInputDevice(const std::string &networkId);
48     void RemoveVirtualInputDevice(const std::string &networkId);
49     void HandleRemoteHotPlug(const DSoftbusHotPlugEvent &notice);
50     void OnRemoteInputDevice(const DSoftbusSyncInputDevice &notice);
51     void OnRemoteHotPlug(const DSoftbusHotPlugEvent &notice);
52 
53 private:
54     void NotifyInputDeviceToRemote(const std::string &remoteNetworkId);
55     void BroadcastHotPlugToRemote(const InputHotplugEvent &notice);
56     void AddRemoteInputDevice(const std::string &networkId, std::shared_ptr<IDevice> device);
57     void RemoveRemoteInputDevice(const std::string &networkId, std::shared_ptr<IDevice> device);
58     void RemoveAllRemoteInputDevice(const std::string &networkId);
59     void DumpRemoteInputDevice(const std::string &networkId);
60     int32_t SerializeDevice(std::shared_ptr<IDevice> device, NetPacket &packet);
61     std::shared_ptr<MMI::InputDevice> Transform(std::shared_ptr<IDevice> device);
62     void AddVirtualInputDevice(const std::string &networkId, int32_t remoteDeviceId);
63     void RemoveVirtualInputDevice(const std::string &networkId, int32_t remoteDeviceId);
64     void DispDeviceInfo(std::shared_ptr<IDevice> device);
65     std::shared_ptr<IDevice> GetRemoteDeviceById(const std::string &networkId, int32_t remoteDeviceId);
66 
67 private:
68     bool enable_ { false };
69     IContext *env_ { nullptr };
70     struct IDeviceCmp {
operatorIDeviceCmp71         bool operator()(const std::shared_ptr<IDevice> &one, const std::shared_ptr<IDevice> &other) const
72         {
73             return one->GetId() < other->GetId();
74         }
75     };
76     std::unordered_map<std::string, std::set<std::shared_ptr<IDevice>, IDeviceCmp>> remoteDevices_;
77     std::unordered_map<std::string, std::set<int32_t>> virtualInputDevicesAdded_;
78     std::unordered_map<int32_t, int32_t> remote2VirtualIds_;
79 };
80 
81 } // namespace Cooperate
82 } // namespace DeviceStatus
83 } // namespace Msdp
84 } // namespace OHOS
85 #endif // COOPERATE_INPUT_DEVICE_MANAGER_H