1 /* 2 * Copyright (c) 2022 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_SCREEN_ROTATION_CONTROLLER_H 17 #define OHOS_ROSEN_SCREEN_ROTATION_CONTROLLER_H 18 19 #include <map> 20 #include <refbase.h> 21 22 #include "sensor_agent.h" 23 24 #include "dm_common.h" 25 #include "window_manager_hilog.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 enum class SensorRotation: int32_t { 30 INVALID = -1, 31 ROTATION_0 = 0, 32 ROTATION_90, 33 ROTATION_180, 34 ROTATION_270, 35 }; 36 37 enum class DeviceRotation: int32_t { 38 INVALID = -1, 39 ROTATION_PORTRAIT = 0, 40 ROTATION_LANDSCAPE, 41 ROTATION_PORTRAIT_INVERTED, 42 ROTATION_LANDSCAPE_INVERTED, 43 }; 44 45 class ScreenRotationController : public RefBase { 46 public: 47 ScreenRotationController() = delete; 48 ~ScreenRotationController() = default; 49 static void Init(); 50 static void HandleSensorEventInput(DeviceRotation deviceRotation); 51 static bool IsScreenRotationLocked(); 52 static void SetScreenRotationLocked(bool isLocked); 53 static void SetDefaultDeviceRotationOffset(uint32_t defaultDeviceRotationOffset); 54 static void ProcessOrientationSwitch(Orientation orientation); 55 56 static bool IsDefaultDisplayRotationPortrait(); 57 static bool IsDisplayRotationVertical(Rotation rotation); 58 static bool IsDisplayRotationHorizontal(Rotation rotation); 59 static DeviceRotation ConvertSensorToDeviceRotation(SensorRotation sensorRotation); 60 private: 61 static void HandleGravitySensorEventCallback(SensorEvent *event); 62 static Rotation GetCurrentDisplayRotation(); 63 static Orientation GetPreferredOrientation(); 64 static void SetScreenRotation(Rotation targetRotation); 65 static Rotation CalcTargetDisplayRotation(Orientation requestedOrientation, 66 DeviceRotation sensorRotationConverted); 67 static DeviceRotation CalcDeviceRotation(SensorRotation sensorRotation); 68 static Rotation ConvertDeviceToDisplayRotation(DeviceRotation sensorRotationConverted); 69 70 static bool IsDeviceRotationVertical(DeviceRotation deviceRotation); 71 static bool IsDeviceRotationHorizontal(DeviceRotation deviceRotation); 72 static bool IsCurrentDisplayVertical(); 73 static bool IsCurrentDisplayHorizontal(); 74 static bool IsSensorRelatedOrientation(Orientation orientation); 75 76 static void ProcessRotationMapping(); 77 static void ProcessSwitchToAutoRotationPortrait(DeviceRotation rotation); 78 static void ProcessSwitchToAutoRotationLandscape(DeviceRotation rotation); 79 static void ProcessSwitchToAutoRotation(DeviceRotation rotation); 80 static void ProcessSwitchToAutoRotationPortraitRestricted(); 81 static void ProcessSwitchToAutoRotationLandscapeRestricted(); 82 static void ProcessSwitchToSensorRelatedOrientation(Orientation orientation, DeviceRotation deviceRotation); 83 static void ProcessSwitchToSensorUnrelatedOrientation(Orientation orientation); 84 static Rotation ProcessAutoRotationPortraitOrientation(DeviceRotation sensorRotationConveted); 85 static Rotation ProcessAutoRotationLandscapeOrientation(DeviceRotation sensorRotationConveted); 86 87 static DisplayId defaultDisplayId_; 88 static uint32_t defaultDeviceRotationOffset_; 89 static uint32_t defaultDeviceRotation_; 90 static std::map<SensorRotation, DeviceRotation> sensorToDeviceRotationMap_; 91 static std::map<DeviceRotation, Rotation> deviceToDisplayRotationMap_; 92 static Orientation lastOrientationType_; 93 static Rotation currentDisplayRotation_; 94 static Rotation lastSensorDecidedRotation_; 95 static Rotation rotationLockedRotation_; 96 static bool isScreenRotationLocked_; 97 static DeviceRotation lastSensorRotationConverted_; 98 }; 99 } // Rosen 100 } // OHOS 101 #endif // OHOS_ROSEN_SCREEN_ROTATION_CONTROLLER_H 102