point_event_injector.cpp 2.5 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dfx/point_event_injector.h"
#if ENABLE_DEBUG
Z
zhangguyuan 已提交
18
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

namespace {
const uint16_t MAX_LIST_SIZE = 500;
}

namespace OHOS {
PointEventInjector::~PointEventInjector()
{
    ListNode<DeviceData*>* node = dataList_.Begin();
    while (node != dataList_.End()) {
        if (node->data_ != nullptr) {
            DeviceData* deleteData = node->data_;
            delete deleteData;
        }
        node = node->next_;
    }
    dataList_.Clear();
}

bool PointEventInjector::SetPointEvent(const DeviceData& data)
{
    if (dataList_.Size() >= MAX_LIST_SIZE) {
D
dengzhihao 已提交
41
        HILOG_INFO(HILOG_MODULE_GRAPHIC, "PointEventInjector::SetPointEvent data list is full.");
M
mamingshuai 已提交
42 43 44 45 46
        return false;
    }

    DeviceData* tmpData = new DeviceData;
    if (tmpData == nullptr) {
D
dengzhihao 已提交
47
        HILOG_ERROR(HILOG_MODULE_GRAPHIC, "PointEventInjector::SetPointEvent memory allocation failed Err!");
M
mamingshuai 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        return false;
    }
    tmpData->point = data.point;
    tmpData->state = data.state;
    dataList_.PushBack(tmpData);
    return true;
}

bool PointEventInjector::Read(DeviceData& data)
{
    if (dataList_.Size() > 0) {
        lastX_ = dataList_.Front()->point.x;
        lastY_ = dataList_.Front()->point.y;
        lastState_ = dataList_.Front()->state;

        ListNode<DeviceData*>* node = dataList_.Begin();
        if (node->data_ != nullptr) {
            DeviceData* deleteData = node->data_;
            delete deleteData;
        }
        dataList_.PopFront();
    }
    data.point = {lastX_, lastY_};
    data.state = lastState_;
#if ENABLE_WINDOW
    if (windowId_ >= 0) {
        data.winId = windowId_;
    }
#endif
    return false;
}

uint16_t PointEventInjector::GetLeftSize() const
{
    return MAX_LIST_SIZE - dataList_.Size();
}

#if ENABLE_WINDOW
void PointEventInjector::SetWindowId(int32_t windowId)
{
    windowId_ = windowId;
}
#endif
}
#endif // ENABLE_DEBUG