1/*
2 * Copyright (c) 2022 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 <cstdio>
17#include "gfx_utils/graphic_log.h"
18#include "graphic_config.h"
19#include "input_dev.h"
20
21namespace OHOS {
22constexpr const uint8_t INPUT_DEV_ID = 2;
23constexpr const uint32_t MESSAGE_QUEUE_SIZE = 8;
24
25InputDev *InputDev::GetInstance()
26{
27    static InputDev instance;
28    if (!instance.init) {
29        uint32_t ret = GetInputInterface(&instance.inputInterface);
30        if (ret != INPUT_SUCCESS) {
31            GRAPHIC_LOGE("Get input interfaces failed");
32            return nullptr;
33        }
34
35        ret = instance.inputInterface->iInputManager->OpenInputDevice(INPUT_DEV_ID);
36        if (ret != INPUT_SUCCESS) {
37            GRAPHIC_LOGE("Open input device failed");
38            return nullptr;
39        }
40
41        instance.inputEventCb.EventPkgCallback = ReportEventPkgCallback;
42        ret = instance.inputInterface->iInputReporter->RegisterReportCallback(INPUT_DEV_ID, &instance.inputEventCb);
43        if (ret != INPUT_SUCCESS) {
44            GRAPHIC_LOGE("Register input device report callback failed");
45            return nullptr;
46        }
47
48        instance.init = true;
49    }
50    return &instance;
51}
52
53void InputDev::ReportEventPkgCallback(const InputEventPackage **pkgs, uint32_t count, uint32_t devIndex)
54{
55    if (pkgs == NULL) {
56        return;
57    }
58
59    DeviceData data = {0};
60    GetInstance()->GetEventData(&data);
61
62    for (uint32_t i = 0; i < count; i++) {
63        if (pkgs[i]->type == EV_REL) {
64            if (pkgs[i]->code == REL_X)
65                data.point.x += pkgs[i]->value;
66            else if (pkgs[i]->code == REL_Y)
67                data.point.y += pkgs[i]->value;
68        } else if (pkgs[i]->type == EV_ABS) {
69            if (pkgs[i]->code == ABS_X)
70                data.point.x =pkgs[i]->value / (0x7fff / HORIZONTAL_RESOLUTION);
71            else if (pkgs[i]->code == ABS_Y)
72                data.point.y =pkgs[i]->value / (0x7fff / VERTICAL_RESOLUTION);
73        } else if (pkgs[i]->type == EV_KEY) {
74            if (pkgs[i]->code == BTN_MOUSE || pkgs[i]->code == BTN_TOUCH) {
75                if (pkgs[i]->value == 0)
76                    data.state = 0;
77                else if (pkgs[i]->value == 1)
78                    data.state = 1;
79            }
80        } else if (pkgs[i]->type == EV_SYN) {
81            if (pkgs[i]->code == SYN_REPORT) {
82                break;
83            }
84        }
85    }
86
87    GetInstance()->SetEventData(data);
88}
89
90bool InputDev::Read(DeviceData &data)
91{
92    GetEventData(&data);
93    return false;
94}
95} // namespace OHOS