• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(Channel<CooperateEvent>::Sender sender);
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     void RemoveAllVirtualInputDevice();
53 
54 private:
55     void NotifyInputDeviceToRemote(const std::string &remoteNetworkId);
56     void BroadcastHotPlugToRemote(const InputHotplugEvent &notice);
57     void AddRemoteInputDevice(const std::string &networkId, std::shared_ptr<IDevice> device);
58     void RemoveRemoteInputDevice(const std::string &networkId, std::shared_ptr<IDevice> device);
59     void RemoveAllRemoteInputDevice(const std::string &networkId);
60     void DumpRemoteInputDevice(const std::string &networkId);
61     int32_t SerializeDevice(std::shared_ptr<IDevice> device, NetPacket &packet);
62     std::shared_ptr<MMI::InputDevice> Transform(std::shared_ptr<IDevice> device);
63     void AddVirtualInputDevice(const std::string &networkId, int32_t remoteDeviceId);
64     void RemoveVirtualInputDevice(const std::string &networkId, int32_t remoteDeviceId);
65     void DispDeviceInfo(std::shared_ptr<IDevice> device);
66     std::shared_ptr<IDevice> GetRemoteDeviceById(const std::string &networkId, int32_t remoteDeviceId);
67     void UpdateVirtualDeviceIdMap();
68 
69 private:
70     bool enable_ { false };
71     Channel<CooperateEvent>::Sender sender_;
72     IContext *env_ { nullptr };
73     struct IDeviceCmp {
operatorIDeviceCmp74         bool operator()(const std::shared_ptr<IDevice> &one, const std::shared_ptr<IDevice> &other) const
75         {
76             return one->GetId() < other->GetId();
77         }
78     };
79     std::unordered_map<std::string, std::set<std::shared_ptr<IDevice>, IDeviceCmp>> remoteDevices_;
80     std::unordered_map<std::string, std::set<int32_t>> virtualInputDevicesAdded_;
81     std::unordered_map<int32_t, int32_t> remote2VirtualIds_;
82 };
83 
84 } // namespace Cooperate
85 } // namespace DeviceStatus
86 } // namespace Msdp
87 } // namespace OHOS
88 #endif // COOPERATE_INPUT_DEVICE_MANAGER_H
89