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