1 /* 2 * Copyright (c) 2020-2021 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 "dfx/point_event_injector.h" 17 #if ENABLE_DEBUG 18 #include "gfx_utils/graphic_log.h" 19 20 namespace { 21 const uint16_t MAX_LIST_SIZE = 500; 22 } 23 24 namespace OHOS { ~PointEventInjector()25PointEventInjector::~PointEventInjector() 26 { 27 ListNode<DeviceData*>* node = dataList_.Begin(); 28 while (node != dataList_.End()) { 29 if (node->data_ != nullptr) { 30 DeviceData* deleteData = node->data_; 31 delete deleteData; 32 } 33 node = node->next_; 34 } 35 dataList_.Clear(); 36 } 37 SetPointEvent(const DeviceData & data)38bool PointEventInjector::SetPointEvent(const DeviceData& data) 39 { 40 if (dataList_.Size() >= MAX_LIST_SIZE) { 41 GRAPHIC_LOGI("PointEventInjector::SetPointEvent data list is full."); 42 return false; 43 } 44 45 DeviceData* tmpData = new DeviceData; 46 if (tmpData == nullptr) { 47 GRAPHIC_LOGE("PointEventInjector::SetPointEvent memory allocation failed Err!"); 48 return false; 49 } 50 tmpData->point = data.point; 51 tmpData->state = data.state; 52 dataList_.PushBack(tmpData); 53 return true; 54 } 55 Read(DeviceData & data)56bool PointEventInjector::Read(DeviceData& data) 57 { 58 if (dataList_.Size() > 0) { 59 lastX_ = dataList_.Front()->point.x; 60 lastY_ = dataList_.Front()->point.y; 61 lastState_ = dataList_.Front()->state; 62 63 ListNode<DeviceData*>* node = dataList_.Begin(); 64 if (node->data_ != nullptr) { 65 DeviceData* deleteData = node->data_; 66 delete deleteData; 67 } 68 dataList_.PopFront(); 69 } 70 data.point = {lastX_, lastY_}; 71 data.state = lastState_; 72 #if ENABLE_WINDOW 73 if (windowId_ >= 0) { 74 data.winId = windowId_; 75 } 76 #endif 77 return false; 78 } 79 GetLeftSize() const80uint16_t PointEventInjector::GetLeftSize() const 81 { 82 return MAX_LIST_SIZE - dataList_.Size(); 83 } 84 85 #if ENABLE_WINDOW SetWindowId(int32_t windowId)86void PointEventInjector::SetWindowId(int32_t windowId) 87 { 88 windowId_ = windowId; 89 } 90 #endif 91 } 92 #endif // ENABLE_DEBUG