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/key_event_injector.h"
17 #if ENABLE_DEBUG
18 #include "gfx_utils/graphic_log.h"
19 
20 namespace {
21 const uint8_t MAX_LIST_SIZE = 100;
22 }
23 
24 namespace OHOS {
~KeyEventInjector()25 KeyEventInjector::~KeyEventInjector()
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 
Read(DeviceData & data)38 bool KeyEventInjector::Read(DeviceData& data)
39 {
40     if (dataList_.IsEmpty()) {
41         lastState_ = INVALID_KEY_STATE;
42     }
43     if (dataList_.Size() > 0) {
44         lastKeyId_ = dataList_.Front()->keyId;
45         lastState_ = dataList_.Front()->state;
46 
47         ListNode<DeviceData*>* node = dataList_.Begin();
48         if (node->data_ != nullptr) {
49             DeviceData* deleteData = node->data_;
50             delete deleteData;
51         }
52         dataList_.PopFront();
53     }
54     data.keyId = lastKeyId_;
55     data.state = lastState_;
56     return false;
57 }
58 
SetKey(const DeviceData & data)59 bool KeyEventInjector::SetKey(const DeviceData& data)
60 {
61     if (dataList_.Size() >= MAX_LIST_SIZE) {
62         GRAPHIC_LOGI("PointEventInjector::SetPointEvent data list is full.");
63         return false;
64     }
65 
66     DeviceData* tmpData = new DeviceData;
67     if (tmpData == nullptr) {
68         GRAPHIC_LOGE("PointEventInjector::SetPointEvent memory allocation failed Err!");
69         return false;
70     }
71     tmpData->keyId = data.keyId;
72     tmpData->state = data.state;
73     dataList_.PushBack(tmpData);
74     return true;
75 }
76 
GetLeftSize() const77 uint8_t KeyEventInjector::GetLeftSize() const
78 {
79     return MAX_LIST_SIZE - dataList_.Size();
80 }
81 }
82 #endif // ENABLE_DEBUG
83