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 "wakeup_sources.h"
17 #include "power_log.h"
18 #include <string>
19 namespace OHOS {
20 namespace PowerMgr {
21 std::mutex WakeupSources::sourceKeysMutex_;
22 
mapWakeupDeviceType(const std::string & key,uint32_t click)23 WakeupDeviceType WakeupSources::mapWakeupDeviceType(const std::string& key, uint32_t click)
24 {
25     if (key == WakeupSources::POWERKEY_KEY) {
26         return WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON;
27     }
28 
29     if (key == WakeupSources::MOUSE_KEY) {
30         return WakeupDeviceType::WAKEUP_DEVICE_MOUSE;
31     }
32 
33     if (key == WakeupSources::KEYBOARD_KEY) {
34         return WakeupDeviceType::WAKEUP_DEVICE_KEYBOARD;
35     }
36 
37     if (key == WakeupSources::PEN_KEY) {
38         return WakeupDeviceType::WAKEUP_DEVICE_PEN;
39     }
40 
41     if (key == WakeupSources::TOUCHPAD_KEY) {
42         return WakeupDeviceType::WAKEUP_DEVICE_TOUCHPAD;
43     }
44 
45     if (key == WakeupSources::LID_KEY) {
46         return WakeupDeviceType::WAKEUP_DEVICE_LID;
47     }
48 
49     if (key == WakeupSources::SWITCH_KEY) {
50         return WakeupDeviceType::WAKEUP_DEVICE_SWITCH;
51     }
52 
53     if (key == WakeupSources::TOUCHSCREEN_KEY) {
54         if (click == WakeupSources::SINGLE_CLICK) {
55             return WakeupDeviceType::WAKEUP_DEVICE_SINGLE_CLICK;
56         }
57         return WakeupDeviceType::WAKEUP_DEVICE_DOUBLE_CLICK;
58     }
59 
60     if (key == WakeupSources::PICKUP_KEY) {
61         return WakeupDeviceType::WAKEUP_DEVICE_PICKUP;
62     }
63 
64     return WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN;
65 }
66 
getSourceKeys()67 std::vector<std::string> WakeupSources::getSourceKeys()
68 {
69     std::lock_guard<std::mutex> lock(sourceKeysMutex_);
70     std::vector<std::string> sourceKeys;
71     sourceKeys.push_back(WakeupSources::POWERKEY_KEY);
72     sourceKeys.push_back(WakeupSources::MOUSE_KEY);
73     sourceKeys.push_back(WakeupSources::KEYBOARD_KEY);
74     sourceKeys.push_back(WakeupSources::TOUCHSCREEN_KEY);
75     sourceKeys.push_back(WakeupSources::TOUCHPAD_KEY);
76     sourceKeys.push_back(WakeupSources::PEN_KEY);
77     sourceKeys.push_back(WakeupSources::LID_KEY);
78     sourceKeys.push_back(WakeupSources::SWITCH_KEY);
79     sourceKeys.push_back(WakeupSources::PICKUP_KEY);
80     return sourceKeys;
81 }
82 
PutSource(WakeupSource & source)83 void WakeupSources::PutSource(WakeupSource& source)
84 {
85     std::lock_guard<std::mutex> lock(sourceListMutex_);
86     sourceList_.push_back(source);
87 }
88 
GetSourceList()89 std::vector<WakeupSource> WakeupSources::GetSourceList()
90 {
91     std::lock_guard<std::mutex> lock(sourceListMutex_);
92     return sourceList_;
93 }
94 
95 } // namespace PowerMgr
96 } // namespace OHOS