1 /*
2  * Copyright (c) 2022-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 INPUT_DISPLAY_BIND_HELPER_H
17 #define INPUT_DISPLAY_BIND_HELPER_H
18 
19 #include <cstdint>
20 #include <list>
21 #include <memory>
22 #include <set>
23 #include <string>
24 
25 #include "window_info.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 class BindInfo {
30 public:
31     int32_t GetInputDeviceId() const;
32     std::string GetInputDeviceName() const;
33     int32_t GetDisplayId() const;
34     std::string GetDisplayName() const;
35     bool IsUnbind() const;
36     bool InputDeviceNotBind() const;
37     bool DisplayNotBind() const;
38     bool AddInputDevice(int32_t deviceId, const std::string &deviceName);
39     void RemoveInputDevice();
40     bool AddDisplay(int32_t id, const std::string &name);
41     void RemoveDisplay();
42     std::string GetDesc() const;
43     friend bool operator < (const BindInfo &l, const BindInfo &r);
44     friend std::ostream &operator << (std::ostream &os, const BindInfo &r);
45     friend std::istream &operator >> (std::istream &is, BindInfo &r);
46 
47 private:
48     int32_t inputDeviceId_ { -1 };
49     std::string inputDeviceName_;
50     int32_t displayId_ { -1 };
51     std::string displayName_;
52 };
53 class BindInfos {
54 public:
55     bool Add(const BindInfo &info);
56     void UnbindInputDevice(int32_t deviceId);
57     void UnbindDisplay(int32_t displayId);
58     BindInfo GetUnbindInputDevice(const std::string &displayName);
59     BindInfo GetUnbindDisplay(const std::string &inputDeviceName);
60     std::string GetDisplayNameByInputDevice(const std::string &name) const;
61     int32_t GetBindDisplayIdByInputDevice(int32_t inputDeviceId) const;
62     std::string GetBindDisplayNameByInputDevice(int32_t inputDeviceId) const;
63     std::string GetInputDeviceByDisplayName(const std::string &name) const;
64     std::string GetDesc() const;
65     const std::list<BindInfo> &GetInfos() const;
66     friend std::ostream &operator << (std::ostream &os, const BindInfos &r);
67     friend std::istream &operator >> (std::istream &is, BindInfos &r);
68 
69 private:
70     BindInfo GetUnbindInputDevice();
71     BindInfo GetUnbindDisplay();
72     std::list<BindInfo> infos_;
73 };
74 class InputDisplayBindHelper {
75 public:
76     InputDisplayBindHelper(const std::string bindCfgFile);
77     std::string GetBindDisplayNameByInputDevice(int32_t inputDeviceId) const;
78     void AddInputDevice(int32_t id, const std::string &name);
79     void RemoveInputDevice(int32_t id);
80     bool IsDisplayAdd(int32_t id, const std::string &name);
81     std::set<std::pair<int32_t, std::string>> GetDisplayIdNames() const;
82     void AddDisplay(int32_t id, const std::string &name);
83     void AddLocalDisplay(int32_t id, const std::string &name);
84     void RemoveDisplay(int32_t id);
85     void Load();
86     std::string Dumps() const;
87     void Store();
88     int32_t GetDisplayBindInfo(DisplayBindInfos &infos);
89     int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg);
90 
91     std::string GetInputDeviceById(int32_t id);
92     std::string GetInputNodeNameByCfg(int32_t id);
93     std::string GetContent(const std::string &fileName);
94     std::string GetInputNode(const std::string &inputNodeName);
95 
96 private:
97     const std::string fileName_;
98     std::shared_ptr<BindInfos> infos_;
99     std::shared_ptr<BindInfos> configFileInfos_;
100 };
101 } // namespace MMI
102 } // namespace OHOS
103 #endif // INPUT_DISPLAY_BIND_HELPER_H
104