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     constexpr int32_t MOTION_ACTION_TENT_MODE_OFF = 0;
30     constexpr int32_t MOTION_ACTION_TENT_MODE_ON = 1;
31 #endif
32 }
33 
34 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
35 bool MotionSubscriber::isMotionSensorSubscribed_ = false;
36 sptr<RotationMotionEventCallback> MotionSubscriber::motionEventCallback_ = nullptr;
37 bool MotionTentSubscriber::isMotionSensorSubscribed_ = false;
38 sptr<TentMotionEventCallback> MotionTentSubscriber::motionEventCallback_ = nullptr;
39 #endif
40 
SubscribeRotationSensor()41 void ScreenSensorConnector::SubscribeRotationSensor()
42 {
43     TLOGD(WmsLogTag::DMS, "dms: subscribe rotation-related sensor");
44 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
45     MotionSubscriber::SubscribeMotionSensor();
46     if (MotionSubscriber::isMotionSensorSubscribed_) {
47         return;
48     }
49 #endif
50 }
51 
UnsubscribeRotationSensor()52 void ScreenSensorConnector::UnsubscribeRotationSensor()
53 {
54 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
55     MotionSubscriber::UnsubscribeMotionSensor();
56 #endif
57 }
58 
SubscribeTentSensor()59 void ScreenSensorConnector::SubscribeTentSensor()
60 {
61     TLOGD(WmsLogTag::DMS, "start");
62 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
63     MotionTentSubscriber::SubscribeMotionSensor();
64 #endif
65 }
66 
UnsubscribeTentSensor()67 void ScreenSensorConnector::UnsubscribeTentSensor()
68 {
69 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
70     MotionTentSubscriber::UnsubscribeMotionSensor();
71 #endif
72 }
73 
74 // Motion
75 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
SubscribeMotionSensor()76 void MotionSubscriber::SubscribeMotionSensor()
77 {
78     TLOGI(WmsLogTag::DMS, "dms: Subscribe motion Sensor");
79     if (isMotionSensorSubscribed_) {
80         TLOGE(WmsLogTag::DMS, "dms: motion sensor's already subscribed");
81         return;
82     }
83     sptr<RotationMotionEventCallback> callback = new (std::nothrow) RotationMotionEventCallback();
84     if (callback == nullptr) {
85         return;
86     }
87     int32_t ret = OHOS::Msdp::SubscribeCallback(OHOS::Msdp::MOTION_TYPE_ROTATION, callback);
88     if (ret != 0) {
89         return;
90     }
91     motionEventCallback_ = callback;
92     isMotionSensorSubscribed_ = true;
93 }
94 
UnsubscribeMotionSensor()95 void MotionSubscriber::UnsubscribeMotionSensor()
96 {
97     if (!isMotionSensorSubscribed_) {
98         TLOGI(WmsLogTag::DMS, "dms: Unsubscribe motion sensor");
99         return;
100     }
101     int32_t ret = OHOS::Msdp::UnsubscribeCallback(OHOS::Msdp::MOTION_TYPE_ROTATION, motionEventCallback_);
102     if (ret != static_cast<int32_t>(OHOS::Msdp::MotionErrorCode::MOTION_SUCCESS)
103         && ret != static_cast<int32_t>(OHOS::Msdp::MotionErrorCode::MOTION_NO_SUBSCRIBE)) {
104         return;
105     }
106     isMotionSensorSubscribed_ = false;
107 }
108 
OnMotionChanged(const MotionEvent & motionData)109 void RotationMotionEventCallback::OnMotionChanged(const MotionEvent& motionData)
110 {
111     DeviceRotation motionRotation = DeviceRotation::INVALID;
112     switch (motionData.status) {
113         case MOTION_ACTION_PORTRAIT: {
114             motionRotation = DeviceRotation::ROTATION_PORTRAIT;
115             break;
116         }
117         case MOTION_ACTION_LEFT_LANDSCAPE: {
118             motionRotation = DeviceRotation::ROTATION_LANDSCAPE_INVERTED;
119             break;
120         }
121         case MOTION_ACTION_PORTRAIT_INVERTED: {
122             motionRotation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
123             break;
124         }
125         case MOTION_ACTION_RIGHT_LANDSCAPE: {
126             motionRotation = DeviceRotation::ROTATION_LANDSCAPE;
127             break;
128         }
129         default: {
130             break;
131         }
132     }
133     ScreenRotationProperty::HandleSensorEventInput(motionRotation);
134 }
135 
SubscribeMotionSensor()136 void MotionTentSubscriber::SubscribeMotionSensor()
137 {
138     TLOGI(WmsLogTag::DMS, "dms: Subscribe tent motion Sensor");
139     if (isMotionSensorSubscribed_) {
140         TLOGE(WmsLogTag::DMS, "dms: tent motion sensor's already subscribed");
141         return;
142     }
143     sptr<TentMotionEventCallback> callback = new (std::nothrow) TentMotionEventCallback();
144     if (callback == nullptr) {
145         TLOGE(WmsLogTag::DMS, "dms: malloc tent motion callback failed");
146         return;
147     }
148     int32_t ret = OHOS::Msdp::SubscribeCallback(OHOS::Msdp::MOTION_TYPE_TENT, callback);
149     if (ret != 0) {
150         TLOGE(WmsLogTag::DMS, "dms: SubscribeCallback type:%{public}d failed", OHOS::Msdp::MOTION_TYPE_TENT);
151         return;
152     }
153     motionEventCallback_ = callback;
154     isMotionSensorSubscribed_ = true;
155 }
156 
UnsubscribeMotionSensor()157 void MotionTentSubscriber::UnsubscribeMotionSensor()
158 {
159     if (!isMotionSensorSubscribed_) {
160         TLOGI(WmsLogTag::DMS, "dms: Unsubscribe tent motion sensor");
161         return;
162     }
163     int32_t ret = OHOS::Msdp::UnsubscribeCallback(OHOS::Msdp::MOTION_TYPE_TENT, motionEventCallback_);
164     if (ret != static_cast<int32_t>(OHOS::Msdp::MotionErrorCode::MOTION_SUCCESS)
165         && ret != static_cast<int32_t>(OHOS::Msdp::MotionErrorCode::MOTION_NO_SUBSCRIBE)) {
166         return;
167     }
168     isMotionSensorSubscribed_ = false;
169 }
170 
OnMotionChanged(const MotionEvent & motionData)171 void TentMotionEventCallback::OnMotionChanged(const MotionEvent& motionData)
172 {
173     if (motionData.status == MOTION_ACTION_TENT_MODE_ON) {
174         ScreenTentProperty::HandleSensorEventInput(true);
175     } else if (motionData.status == MOTION_ACTION_TENT_MODE_OFF) {
176         ScreenTentProperty::HandleSensorEventInput(false);
177     } else {
178         TLOGI(WmsLogTag::DMS, "dms: tent motion:%{public}d invalid", motionData.status);
179     }
180 }
181 #endif
182 } // Rosen
183 } // OHOS