/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ROSEN_WINDOW_IMPL_H #define OHOS_ROSEN_WINDOW_IMPL_H #include #include #include #include #include #include #include #include "input_transfer_station.h" #include "vsync_station.h" #include "window.h" #include "window_property.h" #include "wm_common_inner.h" #include "wm_common.h" namespace OHOS { namespace Rosen { union ColorParam { #if BIG_ENDIANNESS struct { uint8_t alpha; uint8_t red; uint8_t green; uint8_t blue; } argb; #else struct { uint8_t blue; uint8_t green; uint8_t red; uint8_t alpha; } argb; #endif uint32_t value; }; class WindowImpl : public Window { #define CALL_LIFECYCLE_LISTENER(windowLifecycleCb) \ do { \ for (auto& listener : lifecycleListeners_) { \ if (listener != nullptr) { \ listener->windowLifecycleCb(); \ } \ } \ } while (0) #define CALL_UI_CONTENT(uiContentCb) \ do { \ if (uiContent_ != nullptr) { \ uiContent_->uiContentCb(); \ } \ } while (0) public: WindowImpl(const sptr& option); ~WindowImpl(); static sptr Find(const std::string& id); static sptr GetTopWindowWithContext(const std::shared_ptr& context = nullptr); static sptr GetTopWindowWithId(uint32_t mainWinId); static std::vector> GetSubWindow(uint32_t parantId); virtual std::shared_ptr GetSurfaceNode() const override; virtual Rect GetRect() const override; virtual WindowType GetType() const override; virtual WindowMode GetMode() const override; virtual WindowBlurLevel GetWindowBackgroundBlur() const override; virtual float GetAlpha() const override; virtual bool GetShowState() const override; virtual WMError SetFocusable(bool isFocusable) override; virtual bool GetFocusable() const override; virtual WMError SetTouchable(bool isTouchable) override; virtual bool GetTouchable() const override; virtual const std::string& GetWindowName() const override; virtual uint32_t GetWindowId() const override; virtual uint32_t GetWindowFlags() const override; inline NotifyNativeWinDestroyFunc GetNativeDestroyCallback() { return notifyNativefunc_; } virtual SystemBarProperty GetSystemBarPropertyByType(WindowType type) const override; virtual bool IsFullScreen() const override; virtual bool IsLayoutFullScreen() const override; virtual WMError SetWindowType(WindowType type) override; virtual WMError SetWindowMode(WindowMode mode) override; virtual WMError SetWindowBackgroundBlur(WindowBlurLevel level) override; virtual WMError SetAlpha(float alpha) override; virtual WMError AddWindowFlag(WindowFlag flag) override; virtual WMError RemoveWindowFlag(WindowFlag flag) override; virtual WMError SetWindowFlags(uint32_t flags) override; virtual WMError SetSystemBarProperty(WindowType type, const SystemBarProperty& property) override; virtual WMError SetLayoutFullScreen(bool status) override; virtual WMError SetFullScreen(bool status) override; inline void SetWindowState(WindowState state) { state_ = state; } virtual WMError GetAvoidAreaByType(AvoidAreaType type, AvoidArea& avoidArea) override; WMError Create(const std::string& parentName, const std::shared_ptr& context = nullptr); virtual WMError Destroy() override; virtual WMError Show(uint32_t reason = 0) override; virtual WMError Hide(uint32_t reason = 0) override; virtual WMError MoveTo(int32_t x, int32_t y) override; virtual WMError Resize(uint32_t width, uint32_t height) override; virtual WMError SetKeepScreenOn(bool keepScreenOn) override; virtual bool IsKeepScreenOn() const override; virtual WMError SetTurnScreenOn(bool turnScreenOn) override; virtual bool IsTurnScreenOn() const override; virtual WMError SetBackgroundColor(const std::string& color) override; virtual WMError SetTransparent(bool isTransparent) override; virtual bool IsTransparent() const override; virtual WMError SetBrightness(float brightness) override; virtual float GetBrightness() const override; virtual WMError SetCallingWindow(uint32_t windowId) override; virtual void SetPrivacyMode(bool isPrivacyMode) override; virtual bool IsPrivacyMode() const override; virtual bool IsDecorEnable() const override; virtual WMError Maximize() override; virtual WMError Minimize() override; virtual WMError Recover() override; virtual WMError Close() override; virtual void StartMove() override; virtual WMError RequestFocus() const override; virtual void AddInputEventListener(const std::shared_ptr& inputEventListener) override; virtual void RegisterLifeCycleListener(sptr& listener) override; virtual void RegisterWindowChangeListener(sptr& listener) override; virtual void UnregisterLifeCycleListener(sptr& listener) override; virtual void UnregisterWindowChangeListener(sptr& listener) override; virtual void RegisterAvoidAreaChangeListener(sptr& listener) override; virtual void UnregisterAvoidAreaChangeListener(sptr& listener) override; virtual void RegisterDragListener(const sptr& listener) override; virtual void UnregisterDragListener(const sptr& listener) override; virtual void RegisterDisplayMoveListener(sptr& listener) override; virtual void UnregisterDisplayMoveListener(sptr& listener) override; virtual void RegisterWindowDestroyedListener(const NotifyNativeWinDestroyFunc& func) override; virtual void RegisterOccupiedAreaChangeListener(const sptr& listener) override; virtual void UnregisterOccupiedAreaChangeListener(const sptr& listener) override; virtual void SetAceAbilityHandler(const sptr& handler) override; void UpdateRect(const struct Rect& rect, WindowSizeChangeReason reason); void UpdateMode(WindowMode mode); virtual void ConsumeKeyEvent(std::shared_ptr& inputEvent) override; virtual void ConsumePointerEvent(std::shared_ptr& inputEvent) override; virtual void RequestFrame() override; void UpdateFocusStatus(bool focused); virtual void UpdateConfiguration(const std::shared_ptr& configuration) override; void UpdateAvoidArea(const std::vector& avoidAreas); void UpdateWindowState(WindowState state); void UpdateDragEvent(const PointInfo& point, DragEvent event); void UpdateDisplayId(DisplayId from, DisplayId to); void UpdateOccupiedAreaChangeInfo(const sptr& info); void UpdateActiveStatus(bool isActive); virtual WMError SetUIContent(const std::string& contentInfo, NativeEngine* engine, NativeValue* storage, bool isdistributed, AppExecFwk::Ability* ability) override; virtual std::string GetContentInfo() override; virtual const std::shared_ptr GetContext() const override; virtual Ace::UIContent* GetUIContent() const override; virtual void SetRequestedOrientation(Orientation) override; virtual Orientation GetRequestedOrientation() override; // colorspace, gamut virtual bool IsSupportWideGamut() override; virtual void SetColorSpace(ColorSpace colorSpace) override; virtual ColorSpace GetColorSpace() override; virtual void DumpInfo(const std::vector& params, std::vector& info) override; private: inline void NotifyAfterForeground() const { CALL_LIFECYCLE_LISTENER(AfterForeground); CALL_UI_CONTENT(Foreground); } inline void NotifyAfterBackground() const { CALL_LIFECYCLE_LISTENER(AfterBackground); CALL_UI_CONTENT(Background); } inline void NotifyAfterFocused() const { CALL_LIFECYCLE_LISTENER(AfterFocused); CALL_UI_CONTENT(Focus); } inline void NotifyAfterUnfocused() const { CALL_LIFECYCLE_LISTENER(AfterUnfocused); CALL_UI_CONTENT(UnFocus); } inline void NotifyListenerAfterUnfocused() const { CALL_LIFECYCLE_LISTENER(AfterUnfocused); } inline void NotifyBeforeDestroy(std::string windowName) const { CALL_UI_CONTENT(Destroy); if (notifyNativefunc_) { notifyNativefunc_(windowName); } } inline void NotifyBeforeSubWindowDestroy(sptr window) const { auto uiContent = window->GetUIContent(); if (uiContent != nullptr) { uiContent->Destroy(); } if (window->GetNativeDestroyCallback()) { window->GetNativeDestroyCallback()(window->GetWindowName()); } } inline void NotifyAfterActive() const { CALL_LIFECYCLE_LISTENER(AfterActive); } inline void NotifyAfterInactive() const { CALL_LIFECYCLE_LISTENER(AfterInactive); } void DestroyFloatingWindow(); void DestroySubWindow(); void SetDefaultOption(); // for api7 bool IsWindowValid() const; void OnVsync(int64_t timeStamp); static sptr FindTopWindow(uint32_t topWinId); WMError Drag(const Rect& rect); void ConsumeMoveOrDragEvent(std::shared_ptr& pointerEvent); void HandleDragEvent(int32_t posX, int32_t posY, int32_t pointId); void HandleMoveEvent(int32_t posX, int32_t posY, int32_t pointId); void ReadyToMoveOrDragWindow(int32_t globalX, int32_t globalY, int32_t pointId, const Rect& rect); void EndMoveOrDragWindow(int32_t pointId); bool IsPointerEventConsumed(); void AdjustWindowAnimationFlag(); void MapFloatingWindowToAppIfNeeded(); WMError UpdateProperty(PropertyChangeAction action); WMError Destroy(bool needNotifyServer); WMError SetBackgroundColor(uint32_t color); uint32_t GetBackgroundColor() const; // colorspace, gamut using ColorSpaceConvertMap = struct { ColorSpace colorSpace; SurfaceColorGamut sufaceColorGamut; }; static const ColorSpaceConvertMap colorSpaceConvertMap[]; static ColorSpace GetColorSpaceFromSurfaceGamut(SurfaceColorGamut surfaceColorGamut); static SurfaceColorGamut GetSurfaceGamutFromColorSpace(ColorSpace colorSpace); std::shared_ptr callback_ = std::make_shared(VsyncStation::VsyncCallback()); static std::map>> windowMap_; static std::map>> subWindowMap_; static std::map>> appFloatingWindowMap_; sptr property_; WindowState state_ { WindowState::STATE_INITIAL }; WindowTag windowTag_; sptr aceAbilityHandler_; std::vector> lifecycleListeners_; std::vector> windowChangeListeners_; std::vector> avoidAreaChangeListeners_; std::vector> windowDragListeners_; std::vector> displayMoveListeners_; std::vector> occupiedAreaChangeListeners_; NotifyNativeWinDestroyFunc notifyNativefunc_; std::shared_ptr surfaceNode_; std::string name_; std::unique_ptr uiContent_; std::shared_ptr abilityContext_; // give up when context offer getToken std::shared_ptr context_; std::recursive_mutex mutex_; const float SYSTEM_ALARM_WINDOW_WIDTH_RATIO = 0.8; const float SYSTEM_ALARM_WINDOW_HEIGHT_RATIO = 0.3; int32_t startPointPosX_ = 0; int32_t startPointPosY_ = 0; int32_t startPointerId_ = 0; bool startDragFlag_ = false; bool startMoveFlag_ = false; bool pointEventStarted_ = false; Rect startPointRect_ = { 0, 0, 0, 0 }; Rect startRectExceptFrame_ = { 0, 0, 0, 0 }; Rect startRectExceptCorner_ = { 0, 0, 0, 0 }; }; } } #endif // OHOS_ROSEN_WINDOW_IMPL_H