162b8cbc9Sopenharmony_ci/*
262b8cbc9Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
362b8cbc9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
462b8cbc9Sopenharmony_ci * you may not use this file except in compliance with the License.
562b8cbc9Sopenharmony_ci * You may obtain a copy of the License at
662b8cbc9Sopenharmony_ci *
762b8cbc9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
862b8cbc9Sopenharmony_ci *
962b8cbc9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1062b8cbc9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1162b8cbc9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1262b8cbc9Sopenharmony_ci * See the License for the specific language governing permissions and
1362b8cbc9Sopenharmony_ci * limitations under the License.
1462b8cbc9Sopenharmony_ci */
1562b8cbc9Sopenharmony_ci
1662b8cbc9Sopenharmony_ci#ifndef GRAPHIC_LITE_LITE_WM_H
1762b8cbc9Sopenharmony_ci#define GRAPHIC_LITE_LITE_WM_H
1862b8cbc9Sopenharmony_ci
1962b8cbc9Sopenharmony_ci#include <pthread.h>
2062b8cbc9Sopenharmony_ci
2162b8cbc9Sopenharmony_ci#include "surface.h"
2262b8cbc9Sopenharmony_ci
2362b8cbc9Sopenharmony_ci#include "gfx_utils/geometry2d.h"
2462b8cbc9Sopenharmony_ci#include "gfx_utils/list.h"
2562b8cbc9Sopenharmony_ci#include "graphic_locker.h"
2662b8cbc9Sopenharmony_ci#include "hals/hi_fbdev.h"
2762b8cbc9Sopenharmony_ci#include "input_manager_service.h"
2862b8cbc9Sopenharmony_ci
2962b8cbc9Sopenharmony_ci#include "lite_win.h"
3062b8cbc9Sopenharmony_ci
3162b8cbc9Sopenharmony_cinamespace OHOS {
3262b8cbc9Sopenharmony_ciconstexpr uint8_t MAX_UPDATE_SIZE = 8;
3362b8cbc9Sopenharmony_cistruct UpdateRegions {
3462b8cbc9Sopenharmony_ci    int num;
3562b8cbc9Sopenharmony_ci    Rect updates[MAX_UPDATE_SIZE];
3662b8cbc9Sopenharmony_ci    Rect bound;
3762b8cbc9Sopenharmony_ci};
3862b8cbc9Sopenharmony_ci
3962b8cbc9Sopenharmony_cistruct CursorInfo {
4062b8cbc9Sopenharmony_ci    Rect rect;
4162b8cbc9Sopenharmony_ci    bool needRedraw;
4262b8cbc9Sopenharmony_ci    bool enableCursor;
4362b8cbc9Sopenharmony_ci};
4462b8cbc9Sopenharmony_ci
4562b8cbc9Sopenharmony_ciclass LiteWM : public InputEventDistributer::RawEventListener {
4662b8cbc9Sopenharmony_cipublic:
4762b8cbc9Sopenharmony_ci    static LiteWM* GetInstance();
4862b8cbc9Sopenharmony_ci
4962b8cbc9Sopenharmony_ci    void MainTaskHandler();
5062b8cbc9Sopenharmony_ci
5162b8cbc9Sopenharmony_ci    void InitMouseCursor();
5262b8cbc9Sopenharmony_ci    void UpdateMouseCursor();
5362b8cbc9Sopenharmony_ci    void DrawMouseCursor();
5462b8cbc9Sopenharmony_ci
5562b8cbc9Sopenharmony_ci    bool CheckWinIdIsAvailable();
5662b8cbc9Sopenharmony_ci    int32_t GetUniqueWinId();
5762b8cbc9Sopenharmony_ci    void RecycleWinId(int32_t id);
5862b8cbc9Sopenharmony_ci
5962b8cbc9Sopenharmony_ci    void GetLayerInfo(LiteLayerInfo& layerInfo);
6062b8cbc9Sopenharmony_ci    Surface* GetSurface(int32_t id);
6162b8cbc9Sopenharmony_ci    void Show(int32_t id);
6262b8cbc9Sopenharmony_ci    void Hide(int32_t id);
6362b8cbc9Sopenharmony_ci    void RaiseToTop(int32_t id);
6462b8cbc9Sopenharmony_ci    void LowerToBottom(int32_t id);
6562b8cbc9Sopenharmony_ci    void MoveTo(int32_t id, int16_t x, int16_t y);
6662b8cbc9Sopenharmony_ci    void Resize(int32_t id, int16_t width, int16_t height);
6762b8cbc9Sopenharmony_ci    void UpdateWindow(int32_t id);
6862b8cbc9Sopenharmony_ci
6962b8cbc9Sopenharmony_ci    void UpdateWindowRegion(const LiteWindow* window, const Rect& rect);
7062b8cbc9Sopenharmony_ci    void CalculateUpdateRegion(const ListNode<LiteWindow*>* winNode, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
7162b8cbc9Sopenharmony_ci    void ProcessUpdates();
7262b8cbc9Sopenharmony_ci    void DrawRegion(const ListNode<LiteWindow *>* winNode, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
7362b8cbc9Sopenharmony_ci    void DrawBackground(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
7462b8cbc9Sopenharmony_ci    void AddUpdateRegion(const Rect& rect);
7562b8cbc9Sopenharmony_ci
7662b8cbc9Sopenharmony_ci    LiteWindow* CreateWindow(const LiteWinConfig& config, pid_t pid);
7762b8cbc9Sopenharmony_ci    void RemoveWindow(int32_t id);
7862b8cbc9Sopenharmony_ci    LiteWindow* GetWindowById(int32_t id);
7962b8cbc9Sopenharmony_ci    ListNode<LiteWindow*>* GetWindowNodeById(int32_t id);
8062b8cbc9Sopenharmony_ci    LiteWindow* FindTargetWindow(const RawEvent& event);
8162b8cbc9Sopenharmony_ci
8262b8cbc9Sopenharmony_ci    void OnRawEvent(const RawEvent& rawEvent) override;
8362b8cbc9Sopenharmony_ci
8462b8cbc9Sopenharmony_ci    void Screenshot();
8562b8cbc9Sopenharmony_ci    bool OnScreenshot(Surface* surface);
8662b8cbc9Sopenharmony_ci
8762b8cbc9Sopenharmony_ci    void OnClientDeathNotify(pid_t pid);
8862b8cbc9Sopenharmony_ci
8962b8cbc9Sopenharmony_ci    Point GetMousePosition()
9062b8cbc9Sopenharmony_ci    {
9162b8cbc9Sopenharmony_ci        GraphicLocker lock(mouseLock_);
9262b8cbc9Sopenharmony_ci        return mousePosition_;
9362b8cbc9Sopenharmony_ci    }
9462b8cbc9Sopenharmony_ci
9562b8cbc9Sopenharmony_ci    void SetMousePosition(int16_t x, int16_t y)
9662b8cbc9Sopenharmony_ci    {
9762b8cbc9Sopenharmony_ci        x = (x < 0) ? 0 : (x > layerData_->width - 1) ? (layerData_->width - 1) : x;
9862b8cbc9Sopenharmony_ci        y = (y < 0) ? 0 : (y > layerData_->height - 1) ? (layerData_->height - 1) : y;
9962b8cbc9Sopenharmony_ci
10062b8cbc9Sopenharmony_ci        GraphicLocker lock(mouseLock_);
10162b8cbc9Sopenharmony_ci        mousePosition_.x = x;
10262b8cbc9Sopenharmony_ci        mousePosition_.y = y;
10362b8cbc9Sopenharmony_ci    }
10462b8cbc9Sopenharmony_ci
10562b8cbc9Sopenharmony_ci    void GetEventData(DeviceData* data)
10662b8cbc9Sopenharmony_ci    {
10762b8cbc9Sopenharmony_ci        GraphicLocker lock(eventLock_);
10862b8cbc9Sopenharmony_ci        if (data != nullptr) {
10962b8cbc9Sopenharmony_ci            *data = deviceData_;
11062b8cbc9Sopenharmony_ci        }
11162b8cbc9Sopenharmony_ci    }
11262b8cbc9Sopenharmony_ci
11362b8cbc9Sopenharmony_ci    void SetEventData(const LiteWindow* window, const RawEvent& event)
11462b8cbc9Sopenharmony_ci    {
11562b8cbc9Sopenharmony_ci        GraphicLocker lock(eventLock_);
11662b8cbc9Sopenharmony_ci        deviceData_.point.x = event.x;
11762b8cbc9Sopenharmony_ci        deviceData_.point.y = event.y;
11862b8cbc9Sopenharmony_ci        deviceData_.state = event.state;
11962b8cbc9Sopenharmony_ci        if (window != nullptr) {
12062b8cbc9Sopenharmony_ci            deviceData_.winId = window->GetWindowId();
12162b8cbc9Sopenharmony_ci        }
12262b8cbc9Sopenharmony_ci    }
12362b8cbc9Sopenharmony_ci
12462b8cbc9Sopenharmony_ciprivate:
12562b8cbc9Sopenharmony_ci    LiteWM();
12662b8cbc9Sopenharmony_ci    virtual ~LiteWM();
12762b8cbc9Sopenharmony_ci    void InitMutex(pthread_mutex_t& mutex, int type);
12862b8cbc9Sopenharmony_ci
12962b8cbc9Sopenharmony_ci    List<LiteWindow*> winList_;
13062b8cbc9Sopenharmony_ci    UpdateRegions updates_;
13162b8cbc9Sopenharmony_ci    LiteSurfaceData* layerData_;
13262b8cbc9Sopenharmony_ci    CursorInfo cursorInfo_;
13362b8cbc9Sopenharmony_ci    Point mousePosition_;
13462b8cbc9Sopenharmony_ci
13562b8cbc9Sopenharmony_ci    pthread_mutex_t stackLock_;
13662b8cbc9Sopenharmony_ci    pthread_mutex_t mouseLock_;
13762b8cbc9Sopenharmony_ci    pthread_mutex_t eventLock_;
13862b8cbc9Sopenharmony_ci    pthread_mutex_t screenshotMutex_;
13962b8cbc9Sopenharmony_ci
14062b8cbc9Sopenharmony_ci    DeviceData deviceData_;
14162b8cbc9Sopenharmony_ci
14262b8cbc9Sopenharmony_ci    bool needScreenshot_;
14362b8cbc9Sopenharmony_ci    Surface* screenshotSurface_;
14462b8cbc9Sopenharmony_ci
14562b8cbc9Sopenharmony_ci    // Store window ID by bit
14662b8cbc9Sopenharmony_ci    uint32_t winIdStorage;
14762b8cbc9Sopenharmony_ci};
14862b8cbc9Sopenharmony_ci}
14962b8cbc9Sopenharmony_ci#endif
15062b8cbc9Sopenharmony_ci
151