1 /*
2  * Copyright (c) 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_ROSEN_SESSION_HELPER_H
17 #define OHOS_ROSEN_SESSION_HELPER_H
18 
19 #include <pointer_event.h>
20 
21 #include <string>
22 #include "ws_common.h"
23 #include "ws_common_inner.h"
24 #include "wm_common.h"
25 #include "wm_common_inner.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class SessionHelper {
30 public:
GetOverlap(const WSRect & rect1,const WSRect & rect2,int offsetX,int offsetY)31     static WSRect GetOverlap(const WSRect& rect1, const WSRect& rect2, int offsetX, int offsetY)
32     {
33         int32_t x_begin = std::max(rect1.posX_, rect2.posX_);
34         int32_t x_end = std::min(rect1.posX_ + static_cast<int32_t>(rect1.width_),
35             rect2.posX_ + static_cast<int32_t>(rect2.width_));
36         int32_t y_begin = std::max(rect1.posY_, rect2.posY_);
37         int32_t y_end = std::min(rect1.posY_ + static_cast<int32_t>(rect1.height_),
38             rect2.posY_ + static_cast<int32_t>(rect2.height_));
39         if (y_begin >= y_end || x_begin >= x_end) {
40             return { 0, 0, 0, 0 };
41         }
42         return { x_begin - offsetX, y_begin - offsetY,
43             static_cast<uint32_t>(x_end - x_begin), static_cast<uint32_t>(y_end - y_begin) };
44     }
45 
IsEmptyRect(const WSRect & r)46     static inline bool IsEmptyRect(const WSRect& r)
47     {
48         return (r.posX_ == 0 && r.posY_ == 0 && r.width_ == 0 && r.height_ == 0);
49     }
50 
IsPointInRect(int32_t pointPosX,int32_t pointPosY,const Rect & rect)51     static bool IsPointInRect(int32_t pointPosX, int32_t pointPosY, const Rect& rect)
52     {
53         if ((pointPosX > rect.posX_) &&
54             (pointPosX < (rect.posX_ + static_cast<int32_t>(rect.width_)) - 1) &&
55             (pointPosY > rect.posY_) &&
56             (pointPosY < (rect.posY_ + static_cast<int32_t>(rect.height_)) - 1)) {
57             return true;
58         }
59         return false;
60     }
61 
TransferToWSRect(const Rect & rect)62     static inline WSRect TransferToWSRect(const Rect& rect)
63     {
64         WSRect r;
65         r.height_ = rect.height_;
66         r.width_ = rect.width_;
67         r.posX_ = rect.posX_;
68         r.posY_ = rect.posY_;
69         return r;
70     }
71 
TransferToRect(const WSRect & rect)72     static inline Rect TransferToRect(const WSRect& rect)
73     {
74         Rect r;
75         r.height_ = rect.height_;
76         r.width_ = rect.width_;
77         r.posX_ = rect.posX_;
78         r.posY_ = rect.posY_;
79         return r;
80     }
81 
IsBelowSystemWindow(WindowType type)82     static inline bool IsBelowSystemWindow(WindowType type)
83     {
84         return (type >= WindowType::BELOW_APP_SYSTEM_WINDOW_BASE && type < WindowType::BELOW_APP_SYSTEM_WINDOW_END);
85     }
86 
IsAboveSystemWindow(WindowType type)87     static inline bool IsAboveSystemWindow(WindowType type)
88     {
89         return (type >= WindowType::ABOVE_APP_SYSTEM_WINDOW_BASE && type < WindowType::ABOVE_APP_SYSTEM_WINDOW_END);
90     }
91 
IsSystemSubWindow(WindowType type)92     static inline bool IsSystemSubWindow(WindowType type)
93     {
94         return (type >= WindowType::SYSTEM_SUB_WINDOW_BASE && type < WindowType::SYSTEM_SUB_WINDOW_END);
95     }
96 
IsSystemWindow(WindowType type)97     static inline bool IsSystemWindow(WindowType type)
98     {
99         return (IsBelowSystemWindow(type) || IsAboveSystemWindow(type) || IsSystemSubWindow(type));
100     }
101 
IsMainWindow(WindowType type)102     static inline bool IsMainWindow(WindowType type)
103     {
104         return (type >= WindowType::APP_MAIN_WINDOW_BASE && type < WindowType::APP_MAIN_WINDOW_END);
105     }
106 
IsSubWindow(WindowType type)107     static inline bool IsSubWindow(WindowType type)
108     {
109         return (type >= WindowType::APP_SUB_WINDOW_BASE && type < WindowType::APP_SUB_WINDOW_END);
110     }
111 
IsNonSecureToUIExtension(WindowType type)112     static inline bool IsNonSecureToUIExtension(WindowType type)
113     {
114         return IsSubWindow(type) || type == WindowType::WINDOW_TYPE_DIALOG;
115     }
116 
GetAreaType(int32_t pointWinX,int32_t pointWinY,int32_t sourceType,int outside,float vpr,const WSRect & rect)117     static AreaType GetAreaType(int32_t pointWinX, int32_t pointWinY,
118         int32_t sourceType, int outside, float vpr, const WSRect& rect)
119     {
120         int32_t insideCorner = WINDOW_FRAME_CORNER_WIDTH * vpr;
121         int32_t insideEdge = WINDOW_FRAME_WIDTH * vpr;
122         int32_t leftOut = -outside;
123         int32_t leftIn = insideEdge;
124         int32_t leftCorner = insideCorner;
125         int32_t rightCorner = rect.width_ - insideCorner;
126         int32_t rightIn = rect.width_ - insideEdge;
127         int32_t rightOut = rect.width_ + outside;
128         int32_t topOut = -outside;
129         int32_t topIn = insideEdge;
130         int32_t topCorner = insideCorner;
131         int32_t bottomCorner = rect.height_ - insideCorner;
132         int32_t bottomIn = rect.height_ - insideEdge;
133         int32_t bottomOut = rect.height_ + outside;
134 
135         auto isInRange = [](int32_t min, int32_t max, int32_t value) { return min <= value && value <= max; };
136 
137         AreaType type;
138         if (isInRange(leftOut, leftCorner, pointWinX) && isInRange(topOut, topCorner, pointWinY)) {
139             type = AreaType::LEFT_TOP;
140         } else if (isInRange(rightCorner, rightOut, pointWinX) && isInRange(topOut, topCorner, pointWinY)) {
141             type = AreaType::RIGHT_TOP;
142         } else if (isInRange(rightCorner, rightOut, pointWinX) && isInRange(bottomCorner, bottomOut, pointWinY)) {
143             type = AreaType::RIGHT_BOTTOM;
144         } else if (isInRange(leftOut, leftCorner, pointWinX) && isInRange(bottomCorner, bottomOut, pointWinY)) {
145             type = AreaType::LEFT_BOTTOM;
146         } else if (isInRange(leftOut, leftIn, pointWinX)) {
147             type = AreaType::LEFT;
148         } else if (isInRange(topOut, topIn, pointWinY)) {
149             type = AreaType::TOP;
150         } else if (isInRange(rightIn, rightOut, pointWinX)) {
151             type = AreaType::RIGHT;
152         } else if (isInRange(bottomIn, bottomOut, pointWinY)) {
153             type = AreaType::BOTTOM;
154         } else {
155             type = AreaType::UNDEFINED;
156         }
157         return type;
158     }
159 };
160 } // Rosen
161 } // OHOS
162 #endif // OHOS_ROSEN_SESSION_HELPER_H