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
30namespace OHOS {
31namespace Msdp {
32namespace DeviceStatus {
33namespace Cooperate {
34class MouseLocation {
35struct LocationInfo {
36    int32_t displayX { -1 };
37    int32_t displayY { -1 };
38    int32_t displayWidth { -1 };
39    int32_t displayHeight { -1 };
40};
41
42public:
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    void OnSoftbusSessionClosed(const DSoftbusSessionClosed &notice);
56
57private:
58    int32_t SubscribeMouseLocation(const DSoftbusSubscribeMouseLocation &event);
59    int32_t UnSubscribeMouseLocation(const DSoftbusUnSubscribeMouseLocation &event);
60    int32_t SyncMouseLocation(const DSoftbusSyncMouseLocation &event);
61    int32_t ReplySubscribeMouseLocation(const DSoftbusReplySubscribeMouseLocation &event);
62    int32_t ReplyUnSubscribeMouseLocation(const DSoftbusReplyUnSubscribeMouseLocation &event);
63    int32_t SendPacket(const std::string &remoteNetworkId, NetPacket &packet);
64    void ReportMouseLocationToListener(const std::string &networkId, const LocationInfo &locationInfo, int32_t pid);
65    void TransferToLocationInfo(std::shared_ptr<MMI::PointerEvent> pointerEvent, LocationInfo &locationInfo);
66    void SyncLocationToRemote(const std::string &remoteNetworkId, const LocationInfo &locationInfo);
67    bool HasRemoteSubscriber();
68    bool HasLocalListener();
69
70private:
71    std::mutex mutex_;
72    IContext *context_ { nullptr };
73    std::string localNetworkId_;
74    std::set<int32_t> localListeners_;
75    std::set<std::string> remoteSubscribers_;
76    std::unordered_map<std::string, std::set<int32_t>> listeners_;
77};
78} // namespace Cooperate
79} // namespace DeviceStatus
80} // namespace Msdp
81} // namespace OHOS
82#endif // COOPERATE_MOUSE_LOCATION_H
83