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