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