ea249644a387a04e2cd1faaef1b05c709f5c8c19
[mirrors/FD230.git] /
1 /*
2  * Copyright (c) 2022 CompanyNameMagicTag.
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 "hals/lite_input_distributer.h"
17 #include "hals/display_dev.h"
18 #include "gfx_utils/graphic_log.h"
19 #include "dfx/dfx_record.h"
20 #include "common/low_power_manager.h"
21 #ifdef CONFIG_SUPPORTED_POWERMGR
22 #include "power_mgr_policy.h"
23 #include "power_mgr_service.h"
24 #include "samgr_lite.h"
25 #include "commu_feature.h"
26 #endif
27 namespace OHOS {
28 static DeviceData g_lastData = {0};
29 LiteInputDistributer::LiteInputDistributer()
30     : deviceData_({}) {}
31
32 LiteInputDistributer::~LiteInputDistributer() {}
33
34 void LiteInputDistributer::SendRawEvent(const RawEvent& rawEvent)
35 {
36     RawEvent event = rawEvent;
37     DisplayDev* displayDev = DisplayDev::GetInstance();
38     if ((event.type == InputDevType::INDEV_TYPE_TOUCH) &&
39         (displayDev->GetLayerRotateType(LAYER_0) == LayerRotateType::LAYER_ROTATE_90)) {
40         LayerBuffer* buffer = displayDev->GetCurrentBuffer(LAYER_0);
41         if (buffer == nullptr) {
42             return;
43         }
44
45         int16_t tmp = buffer->height - event.x;
46         event.x = event.y;
47         event.y = tmp;
48     }
49 #ifdef CONFIG_SUPPORTED_POWERMGR
50     SendEventToPM(event);
51 #endif
52     SetEventData(event);
53 }
54
55 void LiteInputDistributer::GetEventData(DeviceData& data, InputDevType type)
56 {
57     eventLock_.Lock();
58     
59     if (type == InputDevType::INDEV_TYPE_ENCODER) {
60          if (encoderDataList_.IsEmpty()) {
61             data.rotate = 0;
62             eventLock_.Unlock();
63             return;
64         }
65         DeviceData* tmp = encoderDataList_.Front();
66         encoderDataList_.PopFront();
67         data.rotate = tmp->rotate;
68         data.state = tmp->state;
69         data.timestamp = tmp->timestamp;
70         delete tmp;
71         tmp = nullptr;
72     }else if (type == InputDevType::INDEV_TYPE_TOUCH) {
73         g_lastData = deviceData_;
74         if (IsShowPoint() && (deviceData_.timestamp != g_lastData.timestamp)) {
75             DfxTPGetEvent();
76         }
77         data.point.x = deviceData_.point.x;
78         data.point.y = deviceData_.point.y;
79         data.state = deviceData_.state;
80         data.timestamp = deviceData_.timestamp;
81     }else if (type == InputDevType::INDEV_TYPE_KEY) {
82         if (keyList_.IsEmpty()) {
83             data.keyId = INVALID_KEY_ID;
84             eventLock_.Unlock();
85             return;
86         }
87         DeviceData* tmp = keyList_.Front();
88         keyList_.PopFront();
89         data.keyId = tmp->keyId;
90         data.state = tmp->state;
91         data.timestamp = tmp->timestamp;
92         delete tmp;
93         tmp = nullptr;
94     }
95     eventLock_.Unlock();
96 }
97
98 void LiteInputDistributer::SetEventData(const RawEvent& event)
99 {
100     LowPowerManager::GetInstance()->ExitLowPower();
101     eventLock_.Lock();
102     
103     if (event.type == InputDevType::INDEV_TYPE_TOUCH) {
104         if (IsShowPoint()) {
105             DfxTPSetEvent(event.timestamp - deviceData_.timestamp);
106         }
107         deviceData_.point.x = event.x;
108         deviceData_.point.y = event.y;
109         deviceData_.state = event.state;
110         deviceData_.timestamp = event.timestamp;
111         
112     }else if (event.type == InputDevType::INDEV_TYPE_ENCODER) {
113         // if (IsShowPoint()) {
114         //     DfxTPSetEvent(event.timestamp - encoderDataList_.timestamp);
115         // }
116         DeviceData* data = new DeviceData();
117         data->rotate = event.rotate;
118         data->state = event.state;
119         data->timestamp = event.timestamp;
120         if (encoderDataList_.Size() >= MAX_ENCODER_DATA_LIST_SIZE) {
121             DeviceData* tmp = encoderDataList_.Front();
122             encoderDataList_.PopFront();
123             delete tmp;
124             tmp = nullptr;
125         }
126         encoderDataList_.PushBack(data);
127
128     }else if (event.type == InputDevType::INDEV_TYPE_KEY) {
129         DeviceData* data = new DeviceData();
130         data->keyId = event.keyId;
131         data->state = event.state;
132         data->timestamp = event.timestamp;
133         if (keyList_.Size() >= MAX_KEY_LIST_SIZE) {
134             DeviceData* tmp = keyList_.Front();
135             keyList_.PopFront();
136             delete tmp;
137             tmp = nullptr;
138         }
139         keyList_.PushBack(data);
140     }
141     eventLock_.Unlock();
142 }
143
144 bool LiteInputDistributer::IsEventEmpty(InputDevType type)
145 {
146     if (type == InputDevType::INDEV_TYPE_TOUCH) {
147         return ((g_lastData.state == 0) && (deviceData_.state == 0));
148     } else if (type == InputDevType::INDEV_TYPE_KEY) {
149         return keyList_.IsEmpty();
150     } else if (type == InputDevType::INDEV_TYPE_ENCODER){
151         return encoderDataList_.IsEmpty();
152     }
153     return true;
154 }
155
156 #ifdef CONFIG_SUPPORTED_POWERMGR
157 void LiteInputDistributer::SendEventToPM(const RawEvent& event)
158 {
159     IUnknown *api = SAMGR_GetInstance()->GetDefaultFeatureApiById(POWER_SVR_ID);
160     if (api == nullptr) {
161         return;
162     }
163
164     uint16_t msgId = POWERMSG_USER_EVENT;
165     uint8_t userEvent;
166     if (event.type == InputDevType::INDEV_TYPE_TOUCH) {
167         userEvent = USER_EVENT_TOUCH;
168     }
169     if (event.type == InputDevType::INDEV_TYPE_KEY) {
170         userEvent = USER_EVENT_KEY;
171     }
172     uint32_t msgValue = (POWER_DISPLAY_POLICY << EVENT_TYPE_BITS_SIZE) | userEvent;
173
174     Request *request = nullptr;
175     request = SAMGR_AllocRequestMsg(0, CORES_APPLICATION_CORE, POWER_SVR_ID, 0);
176     if (request == nullptr) {
177         return;
178     }
179     request->serviceId = UI_SVR_ID;
180     request->msgId = msgId;
181     request->msgValue = msgValue;
182
183     CommuFeature *feature = GET_OBJECT(api, CommuFeature, iUnknown);
184     Identity identity = feature->id;
185     identity.featureId = -1;
186     int32 ret = SAMGR_SendRequest(&identity, request, nullptr);
187     if (ret != 0) {
188         SAMGR_FreeRequestMsg(request);
189         return;
190     }
191 }
192 #endif
193 }