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 #include "dscreen_maprelation.h"
17 
18 #include "dscreen_constants.h"
19 #include "dscreen_errcode.h"
20 #include "dscreen_json_util.h"
21 
22 using json = nlohmann::json;
23 
24 namespace OHOS {
25 namespace DistributedHardware {
SetDisplayId(uint64_t displayId)26 void DScreenMapRelation::SetDisplayId(uint64_t displayId)
27 {
28     displayId_ = displayId;
29 }
30 
GetDisplayId() const31 uint64_t DScreenMapRelation::GetDisplayId() const
32 {
33     return displayId_;
34 }
35 
SetScreenId(uint64_t screenId)36 void DScreenMapRelation::SetScreenId(uint64_t screenId)
37 {
38     screenId_ = screenId;
39 }
40 
GetScreenId() const41 uint64_t DScreenMapRelation::GetScreenId() const
42 {
43     return screenId_;
44 }
45 
SetDisplayRect(const DisplayRect & displayRect)46 void DScreenMapRelation::SetDisplayRect(const DisplayRect &displayRect)
47 {
48     displayRect_ = displayRect;
49 }
50 
GetDisplayRect()51 DisplayRect& DScreenMapRelation::GetDisplayRect()
52 {
53     return displayRect_;
54 }
55 
SetScreenRect(const ScreenRect & screenRect)56 void DScreenMapRelation::SetScreenRect(const ScreenRect &screenRect)
57 {
58     screenRect_ = screenRect;
59 }
60 
GetScreenRect()61 ScreenRect& DScreenMapRelation::GetScreenRect()
62 {
63     return screenRect_;
64 }
65 
to_json(json & j,const DScreenMapRelation & dScreenMapRelation)66 void to_json(json &j, const DScreenMapRelation &dScreenMapRelation)
67 {
68     json displayRectJson;
69     json screenRectJson;
70     to_json(displayRectJson, dScreenMapRelation.displayRect_);
71     to_json(screenRectJson, dScreenMapRelation.screenRect_);
72     j = json {
73         {KEY_DISPLAY_ID, dScreenMapRelation.displayId_},
74         {KEY_SCREEN_ID, dScreenMapRelation.screenId_},
75         {KEY_DISPLAY_RECT, displayRectJson},
76         {KEY_SCREEN_RECT, screenRectJson}
77     };
78 }
79 
from_json(const json & j,DScreenMapRelation & dScreenMapRelation)80 void from_json(const json &j, DScreenMapRelation &dScreenMapRelation)
81 {
82     if (!IsUInt64(j, KEY_DISPLAY_ID) || !IsUInt64(j, KEY_SCREEN_ID)) {
83         return;
84     }
85     dScreenMapRelation.displayId_ = j[KEY_DISPLAY_ID].get<uint64_t>();
86     dScreenMapRelation.screenId_ = j[KEY_SCREEN_ID].get<uint64_t>();
87     if (!j.contains(KEY_DISPLAY_RECT) || !j.contains(KEY_SCREEN_RECT)) {
88         return;
89     }
90     from_json(j.at(KEY_DISPLAY_RECT), dScreenMapRelation.displayRect_);
91     from_json(j.at(KEY_SCREEN_RECT), dScreenMapRelation.screenRect_);
92 }
93 
to_json(json & j,const DisplayRect & rect)94 void to_json(json &j, const DisplayRect &rect)
95 {
96     j = json {
97         {KEY_POINT_START_X, rect.startX},
98         {KEY_POINT_START_Y, rect.startY},
99         {KEY_WIDTH, rect.width},
100         {KEY_HEIGHT, rect.height}
101     };
102 }
103 
from_json(const json & j,DisplayRect & rect)104 void from_json(const json &j, DisplayRect &rect)
105 {
106     if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) ||
107         !IsInt32(j, KEY_WIDTH) || !IsInt32(j, KEY_HEIGHT)) {
108         return;
109     }
110     rect.startX = j[KEY_POINT_START_X].get<int32_t>();
111     rect.startY = j[KEY_POINT_START_Y].get<int32_t>();
112     rect.width = j[KEY_WIDTH].get<int32_t>();
113     rect.height = j[KEY_HEIGHT].get<int32_t>();
114 }
115 
to_json(json & j,const ScreenRect & rect)116 void to_json(json &j, const ScreenRect &rect)
117 {
118     j = json {
119         {KEY_POINT_START_X, rect.startX},
120         {KEY_POINT_START_Y, rect.startY},
121         {KEY_WIDTH, rect.width},
122         {KEY_HEIGHT, rect.height}
123     };
124 }
125 
from_json(const json & j,ScreenRect & rect)126 void from_json(const json &j, ScreenRect &rect)
127 {
128     if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) ||
129         !IsUInt32(j, KEY_WIDTH) || !IsUInt32(j, KEY_HEIGHT)) {
130         return;
131     }
132     rect.startX = j[KEY_POINT_START_X].get<int32_t>();
133     rect.startY = j[KEY_POINT_START_Y].get<int32_t>();
134     rect.width = j[KEY_WIDTH].get<uint32_t>();
135     rect.height = j[KEY_HEIGHT].get<uint32_t>();
136 }
137 } // namespace DistributedHardware
138 } // namespace OHOS