diff --git a/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.cpp b/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.cpp index 9765c4eb577b3863f6715cfe504540ae98912858..6b11c317a03671d7386b11af4a890e4778cfd892 100644 --- a/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.cpp +++ b/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.cpp @@ -28,6 +28,8 @@ constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "JsScre const std::string ON_CONNECTION_CALLBACK = "connect"; const std::string ON_DISCONNECTION_CALLBACK = "disconnect"; const std::string ON_PROPERTY_CHANGE_CALLBACK = "propertyChange"; +const std::string ON_SENSOR_ROTATION_CHANGE_CALLBACK = "sensorRotationChange"; +const std::string ON_SCREEN_ORIENTATION_CHANGE_CALLBACK = "screenOrientationChange"; } // namespace NativeValue* JsScreenSession::Create(NativeEngine& engine, const sptr& screenSession) @@ -201,6 +203,82 @@ void JsScreenSession::OnDisconnect() CallJsCallback(ON_DISCONNECTION_CALLBACK); } +void JsScreenSession::OnSensorRotationChange(float sensorRotation) +{ + const std::string callbackType = ON_SENSOR_ROTATION_CHANGE_CALLBACK; + WLOGD("Call js callback: %{public}s.", callbackType.c_str()); + if (mCallback_.count(callbackType) == 0) { + WLOGFE("Callback %{public}s is unregistered!", callbackType.c_str()); + return; + } + + auto jsCallbackRef = mCallback_[callbackType]; + wptr screenSessionWeak(screenSession_); + auto complete = std::make_unique( + [jsCallbackRef, callbackType, screenSessionWeak, sensorRotation]( + NativeEngine& engine, AsyncTask& task, int32_t status) { + if (jsCallbackRef == nullptr) { + WLOGFE("Call js callback %{public}s failed, jsCallbackRef is null!", callbackType.c_str()); + return; + } + auto method = jsCallbackRef->Get(); + if (method == nullptr) { + WLOGFE("Call js callback %{public}s failed, method is null!", callbackType.c_str()); + return; + } + auto screenSession = screenSessionWeak.promote(); + if (screenSession == nullptr) { + WLOGFE("Call js callback %{public}s failed, screenSession is null!", callbackType.c_str()); + return; + } + NativeValue* argv[] = { CreateJsValue(engine, sensorRotation) }; + engine.CallFunction(engine.CreateUndefined(), method, argv, ArraySize(argv)); + }); + + NativeReference* callback = nullptr; + std::unique_ptr execute = nullptr; + AsyncTask::Schedule("JsScreenSession::" + callbackType, engine_, + std::make_unique(callback, std::move(execute), std::move(complete))); +} + +void JsScreenSession::OnScreenOrientationChange(float screenRotation) +{ + const std::string callbackType = ON_SCREEN_ORIENTATION_CHANGE_CALLBACK; + WLOGI("Call js callback: %{public}s.", callbackType.c_str()); + if (mCallback_.count(callbackType) == 0) { + WLOGFE("Callback %{public}s is unregistered!", callbackType.c_str()); + return; + } + + auto jsCallbackRef = mCallback_[callbackType]; + wptr screenSessionWeak(screenSession_); + auto complete = std::make_unique( + [jsCallbackRef, callbackType, screenSessionWeak, screenRotation]( + NativeEngine& engine, AsyncTask& task, int32_t status) { + if (jsCallbackRef == nullptr) { + WLOGFE("Call js callback %{public}s failed, jsCallbackRef is null!", callbackType.c_str()); + return; + } + auto method = jsCallbackRef->Get(); + if (method == nullptr) { + WLOGFE("Call js callback %{public}s failed, method is null!", callbackType.c_str()); + return; + } + auto screenSession = screenSessionWeak.promote(); + if (screenSession == nullptr) { + WLOGFE("Call js callback %{public}s failed, screenSession is null!", callbackType.c_str()); + return; + } + NativeValue* argv[] = { CreateJsValue(engine, screenRotation) }; + engine.CallFunction(engine.CreateUndefined(), method, argv, ArraySize(argv)); + }); + + NativeReference* callback = nullptr; + std::unique_ptr execute = nullptr; + AsyncTask::Schedule("JsScreenSession::" + callbackType, engine_, + std::make_unique(callback, std::move(execute), std::move(complete))); +} + void JsScreenSession::OnPropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason) { const std::string callbackType = ON_PROPERTY_CHANGE_CALLBACK; diff --git a/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.h b/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.h index ab1bcd058bae1d52fe65fc316ad408a69da17706..a357bb8a08cc0f46896fe923507597f3f00f8db9 100644 --- a/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.h +++ b/window_scene/interfaces/kits/napi/screen_session_manager/js_screen_session.h @@ -42,6 +42,8 @@ private: void OnConnect() override; void OnDisconnect() override; void OnPropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason) override; + void OnSensorRotationChange(float sensorRotation) override; + void OnScreenOrientationChange(float screenOrientation) override; NativeEngine& engine_; sptr screenSession_; diff --git a/window_scene/session/screen/include/screen_property.h b/window_scene/session/screen/include/screen_property.h index 6559775a99a401370a14b1c3b37b47d6679f33e9..5a5491c26fe0e8cd55b13c7f6f94ef5091381ac8 100644 --- a/window_scene/session/screen/include/screen_property.h +++ b/window_scene/session/screen/include/screen_property.h @@ -59,6 +59,7 @@ public: void SetScreenRotation(Rotation rotation); Rotation GetScreenRotation() const; + void UpdateScreenRotation(Rotation rotation); void SetOrientation(Orientation orientation); Orientation GetOrientation() const; diff --git a/window_scene/session/screen/include/screen_session.h b/window_scene/session/screen/include/screen_session.h index 278a544383373c58b309d2fceddc5dfa5e528325..b60313bed7347bd18fd9b597c90fd26f9a27dcc3 100644 --- a/window_scene/session/screen/include/screen_session.h +++ b/window_scene/session/screen/include/screen_session.h @@ -40,6 +40,8 @@ public: virtual void OnConnect() = 0; virtual void OnDisconnect() = 0; virtual void OnPropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason) = 0; + virtual void OnSensorRotationChange(float sensorRotation) = 0; + virtual void OnScreenOrientationChange(float screenOrientation) = 0; }; enum class ScreenState : int32_t { @@ -96,6 +98,7 @@ public: void SetPrivateSessionForeground(bool hasPrivate); void SetDisplayBoundary(const RectF& rect, const uint32_t& offsetY); void SetScreenRotationLocked(bool isLocked); + void UpdatePropertyAfterRotation(RRect bounds, int rotation); std::string name_ { "UNKNOW" }; ScreenId screenId_ {}; @@ -113,7 +116,11 @@ public: void Connect(); void Disconnect(); void PropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason); + // notify scb + void SensorRotationChange(Rotation sensorRotation); + void ScreenOrientationChange(Orientation orientation); private: + float ConvertRotationToFloat(Rotation sensorRotation); ScreenProperty property_; std::shared_ptr displayNode_; ScreenState screenState_ { ScreenState::INIT }; diff --git a/window_scene/session/screen/src/screen_property.cpp b/window_scene/session/screen/src/screen_property.cpp index 1a82f5bafb2d12605096fe3d6dc5f3d537a1c255..1a1e8f5f314f86c2a7c27d5bfd5fcf8fb6c7cf36 100644 --- a/window_scene/session/screen/src/screen_property.cpp +++ b/window_scene/session/screen/src/screen_property.cpp @@ -131,6 +131,11 @@ void ScreenProperty::SetScreenRotation(Rotation rotation) screenRotation_ = rotation; } +void ScreenProperty::UpdateScreenRotation(Rotation rotation) +{ + screenRotation_ = rotation; +} + Rotation ScreenProperty::GetScreenRotation() const { return screenRotation_; diff --git a/window_scene/session/screen/src/screen_session.cpp b/window_scene/session/screen/src/screen_session.cpp index c59e76f40fe1729061e8d68eb8a0bea6c1a478bf..341d2b6ffd1ac2ec193fd40ebebb5df2d53a1153 100644 --- a/window_scene/session/screen/src/screen_session.cpp +++ b/window_scene/session/screen/src/screen_session.cpp @@ -172,6 +172,9 @@ void ScreenSession::Disconnect() { screenState_ = ScreenState::DISCONNECTION; for (auto& listener : screenChangeListenerList_) { + if (!listener) { + continue; + } listener->OnDisconnect(); } } @@ -179,10 +182,81 @@ void ScreenSession::Disconnect() void ScreenSession::PropertyChange(const ScreenProperty& newProperty, ScreenPropertyChangeReason reason) { for (auto& listener : screenChangeListenerList_) { + if (!listener) { + continue; + } listener->OnPropertyChange(newProperty, reason); } } +float ScreenSession::ConvertRotationToFloat(Rotation sensorRotation) +{ + float rotation = 0.f; + switch (sensorRotation) { + case Rotation::ROTATION_90: + rotation = 90.f; // degree 90 + break; + case Rotation::ROTATION_180: + rotation = 180.f; // degree 180 + break; + case Rotation::ROTATION_270: + rotation = 270.f; // degree 270 + break; + default: + rotation = 0.f; + break; + } + return rotation; +} + +void ScreenSession::SensorRotationChange(Rotation sensorRotation) +{ + float rotation = ConvertRotationToFloat(sensorRotation); + for (auto& listener : screenChangeListenerList_) { + if (!listener) { + continue; + } + listener->OnSensorRotationChange(rotation); + } +} + +void ScreenSession::ScreenOrientationChange(Orientation orientation) +{ + Rotation rotationAfter = CalcRotation(orientation); + float screenRotation = ConvertRotationToFloat(rotationAfter); + for (auto& listener : screenChangeListenerList_) { + if (!listener) { + continue; + } + listener->OnScreenOrientationChange(screenRotation); + } +} + +void ScreenSession::UpdatePropertyAfterRotation(RRect bounds, int rotation) +{ + Rotation targetRotation = Rotation::ROTATION_0; + switch (rotation) { + case 90: // Rotation 90 degree + targetRotation = Rotation::ROTATION_90; + break; + case 180: // Rotation 180 degree + targetRotation = Rotation::ROTATION_180; + break; + case 270: // Rotation 270 degree + targetRotation = Rotation::ROTATION_270; + break; + default: + targetRotation = Rotation::ROTATION_0; + break; + } + property_.SetBounds(bounds); + property_.SetRotation(static_cast(rotation)); + property_.UpdateScreenRotation(targetRotation); + WLOGFI("bounds:[%{public}f %{public}f %{public}f %{public}f], rotation: %{public}u", + property_.GetBounds().rect_.GetLeft(), property_.GetBounds().rect_.GetTop(), + property_.GetBounds().rect_.GetWidth(), property_.GetBounds().rect_.GetHeight(), targetRotation); +} + sptr ScreenSession::GetActiveScreenMode() const { if (activeIdx_ < 0 || activeIdx_ >= static_cast(modes_.size())) { diff --git a/window_scene/session_manager/src/screen_rotation_property.cpp b/window_scene/session_manager/src/screen_rotation_property.cpp index 4bbf3f3eab4d8ecdbb6f6a2bda7e5a6fbf8fb703..cd8831afb250290bfd5732b65721544571982070 100644 --- a/window_scene/session_manager/src/screen_rotation_property.cpp +++ b/window_scene/session_manager/src/screen_rotation_property.cpp @@ -102,24 +102,16 @@ void ScreenRotationProperty::HandleSensorEventInput(DeviceRotation deviceRotatio return; } auto screenSession = ScreenSessionManager::GetInstance().GetDefaultScreenSession(); - if (!screenSession || screenSession->isScreenLocked_) { - WLOGFW("screenSession is null or isScreenLocked_ is true."); + if (!screenSession) { + WLOGFW("screenSession is null."); return; } - Orientation orientation = GetPreferredOrientation(); - currentDisplayRotation_ = GetCurrentDisplayRotation(); - lastSensorRotationConverted_ = deviceRotation; - if (!IsSensorRelatedOrientation(orientation)) { - WLOGFD("If the current preferred orientation is locked or sensor-independent, return."); + if (lastSensorRotationConverted_ == deviceRotation) { return; } - - if (currentDisplayRotation_ == ConvertDeviceToDisplayRotation(deviceRotation)) { - WLOGFD("If the current display rotation is same to sensor rotation, return."); - return; - } - Rotation targetDisplayRotation = CalcTargetDisplayRotation(orientation, deviceRotation); - SetScreenRotation(targetDisplayRotation); + lastSensorRotationConverted_ = deviceRotation; + Rotation targetSensorRotation = ConvertDeviceToDisplayRotation(deviceRotation); + screenSession->SensorRotationChange(targetSensorRotation); } Rotation ScreenRotationProperty::GetCurrentDisplayRotation() diff --git a/window_scene/session_manager/src/screen_session_manager.cpp b/window_scene/session_manager/src/screen_session_manager.cpp index 7a0497ab68c5fa08a503f058353b33b7f48fab33..1c0ec1071aef06500ae14f90adf34aa17bae2875 100644 --- a/window_scene/session_manager/src/screen_session_manager.cpp +++ b/window_scene/session_manager/src/screen_session_manager.cpp @@ -647,30 +647,14 @@ void ScreenSessionManager::UpdateScreenRotationProperty(ScreenId screenId, RRect WLOGFE("fail to update screen rotation property, cannot find screen %{public}" PRIu64"", screenId); return; } - Rotation targetRotation = Rotation::ROTATION_0; - switch (rotation) { - case 90: // Rotation 90 degree - targetRotation = Rotation::ROTATION_90; - break; - case 180: // Rotation 180 degree - targetRotation = Rotation::ROTATION_180; - break; - case 270: // Rotation 270 degree - targetRotation = Rotation::ROTATION_270; - break; - default: - targetRotation = Rotation::ROTATION_0; - break; - } + screenSession->UpdatePropertyAfterRotation(bounds, rotation); sptr displayInfo = screenSession->ConvertToDisplayInfo(); if (displayInfo == nullptr) { WLOGFE("fail to update screen rotation property, displayInfo is nullptr"); return; } - displayInfo->SetRotation(targetRotation); - displayInfo->SetWidth(bounds.rect_.GetWidth()); - displayInfo->SetHeight(bounds.rect_.GetHeight()); - NotifyDisplayChanged(displayInfo, DisplayChangeEvent::DISPLAY_SIZE_CHANGED); + NotifyDisplayChanged(displayInfo, DisplayChangeEvent::UPDATE_ROTATION); + NotifyScreenChanged(screenSession->ConvertToScreenInfo(), ScreenChangeEvent::UPDATE_ROTATION); } void ScreenSessionManager::NotifyDisplayChanged(sptr displayInfo, DisplayChangeEvent event) @@ -703,7 +687,15 @@ DMError ScreenSessionManager::SetOrientation(ScreenId screenId, Orientation orie return DMError::DM_ERROR_INVALID_PARAM; } HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "ssm:SetOrientation"); - return SetOrientationController(screenId, orientation, false); + sptr screenSession = GetScreenSession(screenId); + if (screenSession == nullptr) { + WLOGFE("fail to set orientation, cannot find screen %{public}" PRIu64"", screenId); + return DMError::DM_ERROR_NULLPTR; + } + // just for get orientation test + screenSession->SetOrientation(orientation); + screenSession->ScreenOrientationChange(orientation); + return DMError::DM_OK; } DMError ScreenSessionManager::SetOrientationFromWindow(DisplayId displayId, Orientation orientation) diff --git a/wm/include/window_session_impl.h b/wm/include/window_session_impl.h index e0244351ba781d92c4c0e3d9e6e1819bac0a844f..6693c31982dd8ddcdc35c84e7d74edf3f229b9a4 100644 --- a/wm/include/window_session_impl.h +++ b/wm/include/window_session_impl.h @@ -117,6 +117,7 @@ public: void SetInputEventConsumer(const std::shared_ptr& inputEventConsumer) override; WMError SetBackgroundColor(const std::string& color) override; + virtual Orientation GetRequestedOrientation() override; int32_t GetParentId() const; int32_t GetPersistentId() const override; diff --git a/wm/src/window_session_impl.cpp b/wm/src/window_session_impl.cpp index 8585696c6e1f6db56597d5cc1dcfca5c7a20fbb7..5bba6db4daa037352685446db92da99d149f76e3 100755 --- a/wm/src/window_session_impl.cpp +++ b/wm/src/window_session_impl.cpp @@ -694,6 +694,8 @@ float WindowSessionImpl::GetBrightness() const void WindowSessionImpl::SetRequestedOrientation(Orientation orientation) { + WLOGFI("lastReqOrientation: %{public}u target:%{public}u state_:%{public}u", + property_->GetRequestedOrientation(), orientation, state_); if (property_->GetRequestedOrientation() == orientation) { return; } @@ -703,6 +705,15 @@ void WindowSessionImpl::SetRequestedOrientation(Orientation orientation) } } +Orientation WindowSessionImpl::GetRequestedOrientation() +{ + if (!property_) { + WLOGFE("property_ is nullptr id: %{public}d", GetPersistentId()); + return Orientation::UNSPECIFIED; + } + return property_->GetRequestedOrientation(); +} + std::string WindowSessionImpl::GetContentInfo() { WLOGFD("GetContentInfo");