1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci#include "pointers_input_device.h"
16fb299fa2Sopenharmony_ci#include <unistd.h>
17fb299fa2Sopenharmony_ci#include "log/log.h"
18fb299fa2Sopenharmony_ci#include "common/input_device_manager.h"
19fb299fa2Sopenharmony_ci#include "ui_rotation.h"
20fb299fa2Sopenharmony_ci
21fb299fa2Sopenharmony_cinamespace Updater {
22fb299fa2Sopenharmony_civoid AddInputDevice()
23fb299fa2Sopenharmony_ci{
24fb299fa2Sopenharmony_ci    OHOS::InputDeviceManager::GetInstance()->Add(&PointersInputDevice::GetInstance());
25fb299fa2Sopenharmony_ci}
26fb299fa2Sopenharmony_ci
27fb299fa2Sopenharmony_civoid HandlePointersEvent(const input_event &ev, uint32_t type)
28fb299fa2Sopenharmony_ci{
29fb299fa2Sopenharmony_ci    PointersInputDevice::GetInstance().HandlePointEvent(ev, type);
30fb299fa2Sopenharmony_ci}
31fb299fa2Sopenharmony_ci
32fb299fa2Sopenharmony_ciPointersInputDevice &PointersInputDevice::GetInstance()
33fb299fa2Sopenharmony_ci{
34fb299fa2Sopenharmony_ci    static PointersInputDevice instance;
35fb299fa2Sopenharmony_ci    return instance;
36fb299fa2Sopenharmony_ci}
37fb299fa2Sopenharmony_ci
38fb299fa2Sopenharmony_cibool PointersInputDevice::Read(OHOS::DeviceData& data)
39fb299fa2Sopenharmony_ci{
40fb299fa2Sopenharmony_ci    ReadTouch(data);
41fb299fa2Sopenharmony_ci    return false;
42fb299fa2Sopenharmony_ci}
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_cibool PointersInputDevice::ReadPoint(OHOS::DeviceData& data, OHOS::Point pos, bool fingerPressDown)
45fb299fa2Sopenharmony_ci{
46fb299fa2Sopenharmony_ci    auto [x, y] = UiRotation::GetInstance().RotateXY(pos.x, pos.y);
47fb299fa2Sopenharmony_ci    data.point.x = x;
48fb299fa2Sopenharmony_ci    data.point.y = y;
49fb299fa2Sopenharmony_ci    data.state = fingerPressDown ? OHOS::InputDevice::STATE_PRESS : OHOS::InputDevice::STATE_RELEASE;
50fb299fa2Sopenharmony_ci    return false;
51fb299fa2Sopenharmony_ci}
52fb299fa2Sopenharmony_ci
53fb299fa2Sopenharmony_cibool PointersInputDevice::ReadTouch(OHOS::DeviceData &data)
54fb299fa2Sopenharmony_ci{
55fb299fa2Sopenharmony_ci    ReadPoint(data, reportAbsMt_, touchFingerDown_);
56fb299fa2Sopenharmony_ci    return false;
57fb299fa2Sopenharmony_ci}
58fb299fa2Sopenharmony_ci
59fb299fa2Sopenharmony_civoid PointersInputDevice::SetFingerDown(bool isPressed)
60fb299fa2Sopenharmony_ci{
61fb299fa2Sopenharmony_ci    touchFingerDown_ = isPressed;
62fb299fa2Sopenharmony_ci}
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_civoid PointersInputDevice::HandleEvAbsMt(const input_event &ev)
65fb299fa2Sopenharmony_ci{
66fb299fa2Sopenharmony_ci    const static std::unordered_map<int, std::function<void(int)>> evMap {
67fb299fa2Sopenharmony_ci        {ABS_MT_POSITION_X, [this] (int value) {
68fb299fa2Sopenharmony_ci            reportAbsMt_.x = value;
69fb299fa2Sopenharmony_ci            SetFingerDown(true);
70fb299fa2Sopenharmony_ci        }},
71fb299fa2Sopenharmony_ci        {ABS_MT_POSITION_Y, [this] (int value) {
72fb299fa2Sopenharmony_ci            reportAbsMt_.y = value;
73fb299fa2Sopenharmony_ci            SetFingerDown(true);
74fb299fa2Sopenharmony_ci        }},
75fb299fa2Sopenharmony_ci        {ABS_MT_TRACKING_ID, [this] (int value) {
76fb299fa2Sopenharmony_ci            // Protocol B: -1 marks lifting the contact.
77fb299fa2Sopenharmony_ci            if (value < 0) {
78fb299fa2Sopenharmony_ci                SetFingerDown(false);
79fb299fa2Sopenharmony_ci            }
80fb299fa2Sopenharmony_ci        }}
81fb299fa2Sopenharmony_ci    };
82fb299fa2Sopenharmony_ci    if (auto it = evMap.find(ev.code); it != evMap.end()) {
83fb299fa2Sopenharmony_ci        it->second(ev.value);
84fb299fa2Sopenharmony_ci    }
85fb299fa2Sopenharmony_ci}
86fb299fa2Sopenharmony_ci
87fb299fa2Sopenharmony_ciint PointersInputDevice::HandlePointEvent(const input_event &ev, uint32_t type)
88fb299fa2Sopenharmony_ci{
89fb299fa2Sopenharmony_ci    if (ev.type == EV_ABS) {
90fb299fa2Sopenharmony_ci        HandleEvAbsMt(ev);
91fb299fa2Sopenharmony_ci        return 0;
92fb299fa2Sopenharmony_ci    }
93fb299fa2Sopenharmony_ci    if (ev.type == EV_KEY && ev.code == BTN_TOUCH) {
94fb299fa2Sopenharmony_ci        SetFingerDown(ev.value == 1);
95fb299fa2Sopenharmony_ci    }
96fb299fa2Sopenharmony_ci    return 0;
97fb299fa2Sopenharmony_ci}
98fb299fa2Sopenharmony_ci} // namespace Updater
99