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 OHOS_DISTRIBUTED_INPUT_CONTEXT_H
17 #define OHOS_DISTRIBUTED_INPUT_CONTEXT_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <unordered_map>
25 
26 #include <refbase.h>
27 
28 #include "single_instance.h"
29 
30 #include "constants.h"
31 #include "device_type.h"
32 #include "distributed_hardware_fwk_kit.h"
33 #include "if_system_ability_manager.h"
34 #include "iservice_registry.h"
35 #include "nlohmann/json.hpp"
36 #include "system_ability_definition.h"
37 
38 #include "dinput_log.h"
39 #include "i_dinput_context.h"
40 
41 namespace OHOS {
42 namespace DistributedHardware {
43 namespace DistributedInput {
44 const nlohmann::json ENABLE_LOW_LATENCY = {
45     {DH_TYPE, DHType::INPUT},
46     {LOW_LATENCY_ENABLE, true},
47 };
48 
49 const nlohmann::json DISABLE_LOW_LATENCY = {
50     {DH_TYPE, DHType::INPUT},
51     {LOW_LATENCY_ENABLE, false},
52 };
53 class DInputContext {
54 DECLARE_SINGLE_INSTANCE_BASE(DInputContext);
55 public:
56     std::string GetScreenInfoKey(const std::string &devId, const uint64_t sourceWinId);
57     int32_t RemoveSinkScreenInfo(const std::string &screenInfoKey);
58     int32_t UpdateSinkScreenInfo(const std::string &screenInfoKey, const SinkScreenInfo &sinkScreenInfo);
59     SinkScreenInfo GetSinkScreenInfo(const std::string &screenInfoKey);
60     const std::unordered_map<std::string, SinkScreenInfo> &GetAllSinkScreenInfo();
61 
62     int32_t RemoveSrcScreenInfo(const std::string &screenInfoKey);
63     int32_t UpdateSrcScreenInfo(const std::string &screenInfoKey, const SrcScreenInfo &srcScreenInfo);
64     SrcScreenInfo GetSrcScreenInfo(const std::string &screenInfoKey);
65 
66     void SetLocalTouchScreenInfo(const LocalTouchScreenInfo &localTouchScreenInfo);
67     LocalTouchScreenInfo GetLocalTouchScreenInfo();
68     std::shared_ptr<DistributedHardwareFwkKit> GetDHFwkKit();
69     sptr<IRemoteObject> GetRemoteObject(const int32_t saId);
70     void AddRemoteObject(const int32_t saId, const sptr<IRemoteObject> &remoteObject);
71     void RemoveRemoteObject(const int32_t saId);
72 
73 private:
74     int32_t CalculateTransformInfo(SinkScreenInfo &sinkScreenInfo);
75 
76 private:
77     DInputContext() = default;
78     ~DInputContext();
79 
80     /* the key is Combination of sink's localDeviceId and windowId, the value is sinkScreenInfo */
81     std::unordered_map<std::string, SinkScreenInfo> sinkScreenInfoMap_;
82 
83     /* the key is Combination of source's localDeviceId and windowId, the value is sourceScreenInfo */
84     std::unordered_map<std::string, SrcScreenInfo> srcScreenInfoMap_;
85     LocalTouchScreenInfo localTouchScreenInfo_;
86     std::mutex srcMapMutex_;
87     std::mutex sinkMapMutex_;
88     std::mutex localTouchScreenInfoMutex_;
89     std::shared_ptr<DistributedHardwareFwkKit> dhFwkKit_;
90     std::mutex dhFwkKitMutex_;
91     std::unordered_map<int32_t, sptr<IRemoteObject>> remoteObjects_;
92     std::mutex remoteObjectsMutex_;
93 };
94 } // namespace DistributedInput
95 } // namespace DistributedHardware
96 } // namespace OHOS
97 
98 #endif // OHOS_DISTRIBUTED_INPUT_CONTEXT_H