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_MOUSE_LOCATION_H
17 #define COOPERATE_MOUSE_LOCATION_H
18 
19 #include <mutex>
20 #include <set>
21 #include <unordered_map>
22 
23 #include "nocopyable.h"
24 #include "pointer_event.h"
25 
26 #include "cooperate_events.h"
27 #include "i_context.h"
28 #include "i_event_listener.h"
29 
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 namespace Cooperate {
34 class MouseLocation {
35 struct LocationInfo {
36     int32_t displayX { -1 };
37     int32_t displayY { -1 };
38     int32_t displayWidth { -1 };
39     int32_t displayHeight { -1 };
40 };
41 
42 public:
43     MouseLocation(IContext *context);
44     ~MouseLocation() = default;
45     DISALLOW_COPY_AND_MOVE(MouseLocation);
46     void AddListener(const RegisterEventListenerEvent &event);
47     void RemoveListener(const UnregisterEventListenerEvent &event);
48     void ProcessData(std::shared_ptr<MMI::PointerEvent> pointerEvent);
49     void OnSubscribeMouseLocation(const DSoftbusSubscribeMouseLocation &notice);
50     void OnUnSubscribeMouseLocation(const DSoftbusUnSubscribeMouseLocation &notice);
51     void OnReplySubscribeMouseLocation(const DSoftbusReplySubscribeMouseLocation &notice);
52     void OnReplyUnSubscribeMouseLocation(const DSoftbusReplyUnSubscribeMouseLocation &notice);
53     void OnRemoteMouseLocation(const DSoftbusSyncMouseLocation &notice);
54     void OnClientDied(const ClientDiedEvent &event);
55 
56 private:
57     int32_t SubscribeMouseLocation(const DSoftbusSubscribeMouseLocation &event);
58     int32_t UnSubscribeMouseLocation(const DSoftbusUnSubscribeMouseLocation &event);
59     int32_t SyncMouseLocation(const DSoftbusSyncMouseLocation &event);
60     int32_t ReplySubscribeMouseLocation(const DSoftbusReplySubscribeMouseLocation &event);
61     int32_t ReplyUnSubscribeMouseLocation(const DSoftbusReplyUnSubscribeMouseLocation &event);
62     int32_t SendPacket(const std::string &remoteNetworkId, NetPacket &packet);
63     void ReportMouseLocationToListener(const std::string &networkId, const LocationInfo &locationInfo, int32_t pid);
64     void TransferToLocationInfo(std::shared_ptr<MMI::PointerEvent> pointerEvent, LocationInfo &locationInfo);
65     void SyncLocationToRemote(const std::string &remoteNetworkId, const LocationInfo &locationInfo);
66     bool HasRemoteSubscriber();
67     bool HasLocalListener();
68 
69 private:
70     std::mutex mutex_;
71     IContext *context_ { nullptr };
72     std::string localNetworkId_;
73     std::set<int32_t> localListeners_;
74     std::set<std::string> remoteSubscribers_;
75     std::unordered_map<std::string, std::set<int32_t>> listeners_;
76 };
77 } // namespace Cooperate
78 } // namespace DeviceStatus
79 } // namespace Msdp
80 } // namespace OHOS
81 #endif // COOPERATE_MOUSE_LOCATION_H