• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "screen_sensor_connector.h"
17 
18 #include <chrono>
19 #include <securec.h>
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace {
24     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "ScreenSensorConnector"};
25 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
26     constexpr int32_t MOTION_ACTION_PORTRAIT = 0;
27     constexpr int32_t MOTION_ACTION_LEFT_LANDSCAPE = 1;
28     constexpr int32_t MOTION_ACTION_PORTRAIT_INVERTED = 2;
29     constexpr int32_t MOTION_ACTION_RIGHT_LANDSCAPE = 3;
30 #endif
31 }
32 
33 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
34 bool MotionSubscriber::isMotionSensorSubscribed_ = false;
35 sptr<RotationMotionEventCallback> MotionSubscriber::motionEventCallback_ = nullptr;
36 #endif
37 
SubscribeRotationSensor()38 void ScreenSensorConnector::SubscribeRotationSensor()
39 {
40     WLOGFI("dms: subscribe rotation-related sensor");
41     ScreenRotationProperty::Init();
42 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
43     MotionSubscriber::SubscribeMotionSensor();
44     if (MotionSubscriber::isMotionSensorSubscribed_) {
45         return;
46     }
47 #endif
48 }
49 
UnsubscribeRotationSensor()50 void ScreenSensorConnector::UnsubscribeRotationSensor()
51 {
52 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
53     MotionSubscriber::UnsubscribeMotionSensor();
54 #endif
55 }
56 
57 // Motion
58 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
SubscribeMotionSensor()59 void MotionSubscriber::SubscribeMotionSensor()
60 {
61     WLOGFI("dms: Subscribe motion Sensor");
62     if (isMotionSensorSubscribed_) {
63         WLOGFE("dms: motion sensor's already subscribed");
64         return;
65     }
66     sptr<RotationMotionEventCallback> callback = new (std::nothrow) RotationMotionEventCallback();
67     if (callback == nullptr) {
68         return;
69     }
70     int32_t ret = OHOS::Msdp::SubscribeCallback(OHOS::Msdp::MOTION_TYPE_ROTATION, callback);
71     if (ret != 0) {
72         return;
73     }
74     motionEventCallback_ = callback;
75     isMotionSensorSubscribed_ = true;
76 }
77 
UnsubscribeMotionSensor()78 void MotionSubscriber::UnsubscribeMotionSensor()
79 {
80     if (!isMotionSensorSubscribed_) {
81         WLOGFI("dms: Unsubscribe motion sensor");
82         return;
83     }
84     int32_t ret = OHOS::Msdp::UnsubscribeCallback(OHOS::Msdp::MOTION_TYPE_ROTATION, motionEventCallback_);
85     if (ret != 0) {
86         return;
87     }
88     isMotionSensorSubscribed_ = false;
89 }
90 
OnMotionChanged(const MotionEvent & motionData)91 void RotationMotionEventCallback::OnMotionChanged(const MotionEvent& motionData)
92 {
93     DeviceRotation motionRotation = DeviceRotation::INVALID;
94     switch (motionData.status) {
95         case MOTION_ACTION_PORTRAIT: {
96             motionRotation = DeviceRotation::ROTATION_PORTRAIT;
97             break;
98         }
99         case MOTION_ACTION_LEFT_LANDSCAPE: {
100             motionRotation = ScreenRotationProperty::IsDefaultDisplayRotationPortrait() ?
101                 DeviceRotation::ROTATION_LANDSCAPE_INVERTED : DeviceRotation::ROTATION_LANDSCAPE;
102             break;
103         }
104         case MOTION_ACTION_PORTRAIT_INVERTED: {
105             motionRotation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
106             break;
107         }
108         case MOTION_ACTION_RIGHT_LANDSCAPE: {
109             motionRotation = ScreenRotationProperty::IsDefaultDisplayRotationPortrait() ?
110                 DeviceRotation::ROTATION_LANDSCAPE : DeviceRotation::ROTATION_LANDSCAPE_INVERTED;
111             break;
112         }
113         default: {
114             break;
115         }
116     }
117     ScreenRotationProperty::HandleSensorEventInput(motionRotation);
118 }
119 #endif
120 } // Rosen
121 } // OHOS