1 /*
2  * Copyright (c) 2020-2021 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 GRAPHIC_LITE_LITE_WM_H
17 #define GRAPHIC_LITE_LITE_WM_H
18 
19 #include <pthread.h>
20 
21 #include "surface.h"
22 
23 #include "gfx_utils/geometry2d.h"
24 #include "gfx_utils/list.h"
25 #include "graphic_locker.h"
26 #include "hals/hi_fbdev.h"
27 #include "input_manager_service.h"
28 
29 #include "lite_win.h"
30 
31 namespace OHOS {
32 constexpr uint8_t MAX_UPDATE_SIZE = 8;
33 struct UpdateRegions {
34     int num;
35     Rect updates[MAX_UPDATE_SIZE];
36     Rect bound;
37 };
38 
39 struct CursorInfo {
40     Rect rect;
41     bool needRedraw;
42     bool enableCursor;
43 };
44 
45 class LiteWM : public InputEventDistributer::RawEventListener {
46 public:
47     static LiteWM* GetInstance();
48 
49     void MainTaskHandler();
50 
51     void InitMouseCursor();
52     void UpdateMouseCursor();
53     void DrawMouseCursor();
54 
55     bool CheckWinIdIsAvailable();
56     int32_t GetUniqueWinId();
57     void RecycleWinId(int32_t id);
58 
59     void GetLayerInfo(LiteLayerInfo& layerInfo);
60     Surface* GetSurface(int32_t id);
61     void Show(int32_t id);
62     void Hide(int32_t id);
63     void RaiseToTop(int32_t id);
64     void LowerToBottom(int32_t id);
65     void MoveTo(int32_t id, int16_t x, int16_t y);
66     void Resize(int32_t id, int16_t width, int16_t height);
67     void UpdateWindow(int32_t id);
68 
69     void UpdateWindowRegion(const LiteWindow* window, const Rect& rect);
70     void CalculateUpdateRegion(const ListNode<LiteWindow*>* winNode, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
71     void ProcessUpdates();
72     void DrawRegion(const ListNode<LiteWindow *>* winNode, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
73     void DrawBackground(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
74     void AddUpdateRegion(const Rect& rect);
75 
76     LiteWindow* CreateWindow(const LiteWinConfig& config, pid_t pid);
77     void RemoveWindow(int32_t id);
78     LiteWindow* GetWindowById(int32_t id);
79     ListNode<LiteWindow*>* GetWindowNodeById(int32_t id);
80     LiteWindow* FindTargetWindow(const RawEvent& event);
81 
82     void OnRawEvent(const RawEvent& rawEvent) override;
83 
84     void Screenshot();
85     bool OnScreenshot(Surface* surface);
86 
87     void OnClientDeathNotify(pid_t pid);
88 
GetMousePosition()89     Point GetMousePosition()
90     {
91         GraphicLocker lock(mouseLock_);
92         return mousePosition_;
93     }
94 
SetMousePosition(int16_t x,int16_t y)95     void SetMousePosition(int16_t x, int16_t y)
96     {
97         x = (x < 0) ? 0 : (x > layerData_->width - 1) ? (layerData_->width - 1) : x;
98         y = (y < 0) ? 0 : (y > layerData_->height - 1) ? (layerData_->height - 1) : y;
99 
100         GraphicLocker lock(mouseLock_);
101         mousePosition_.x = x;
102         mousePosition_.y = y;
103     }
104 
GetEventData(DeviceData * data)105     void GetEventData(DeviceData* data)
106     {
107         GraphicLocker lock(eventLock_);
108         if (data != nullptr) {
109             *data = deviceData_;
110         }
111     }
112 
SetEventData(const LiteWindow * window,const RawEvent & event)113     void SetEventData(const LiteWindow* window, const RawEvent& event)
114     {
115         GraphicLocker lock(eventLock_);
116         deviceData_.point.x = event.x;
117         deviceData_.point.y = event.y;
118         deviceData_.state = event.state;
119         if (window != nullptr) {
120             deviceData_.winId = window->GetWindowId();
121         }
122     }
123 
124 private:
125     LiteWM();
126     virtual ~LiteWM();
127     void InitMutex(pthread_mutex_t& mutex, int type);
128 
129     List<LiteWindow*> winList_;
130     UpdateRegions updates_;
131     LiteSurfaceData* layerData_;
132     CursorInfo cursorInfo_;
133     Point mousePosition_;
134 
135     pthread_mutex_t stackLock_;
136     pthread_mutex_t mouseLock_;
137     pthread_mutex_t eventLock_;
138     pthread_mutex_t screenshotMutex_;
139 
140     DeviceData deviceData_;
141 
142     bool needScreenshot_;
143     Surface* screenshotSurface_;
144 
145     // Store window ID by bit
146     uint32_t winIdStorage;
147 };
148 }
149 #endif
150 
151