1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#include "processing_joystick_device.h"
17c29fa5a6Sopenharmony_ci
18c29fa5a6Sopenharmony_ci#undef MMI_LOG_TAG
19c29fa5a6Sopenharmony_ci#define MMI_LOG_TAG "ProcessingJoystickDevice"
20c29fa5a6Sopenharmony_ci
21c29fa5a6Sopenharmony_cinamespace OHOS {
22c29fa5a6Sopenharmony_cinamespace MMI {
23c29fa5a6Sopenharmony_cinamespace {
24c29fa5a6Sopenharmony_ciconstexpr int32_t DEFAULT_ABSX_VALUE { 8188 };
25c29fa5a6Sopenharmony_ciconstexpr int32_t DEFAULT_ABSY_VALUE { 8192 };
26c29fa5a6Sopenharmony_ciconstexpr int32_t DEFAULT_ABSZ_VALUE { 125 };
27c29fa5a6Sopenharmony_ci} // namespace
28c29fa5a6Sopenharmony_ci
29c29fa5a6Sopenharmony_ciint32_t ProcessingJoystickDevice::TransformJsonDataToInputData(const DeviceItem& originalEvent,
30c29fa5a6Sopenharmony_ci    InputEventArray& inputEventArray)
31c29fa5a6Sopenharmony_ci{
32c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
33c29fa5a6Sopenharmony_ci    if (originalEvent.events.empty()) {
34c29fa5a6Sopenharmony_ci        MMI_HILOGE("Handle joystick array failed, inputData is empty");
35c29fa5a6Sopenharmony_ci        return RET_ERR;
36c29fa5a6Sopenharmony_ci    }
37c29fa5a6Sopenharmony_ci    std::vector<DeviceEvent> inputData = originalEvent.events;
38c29fa5a6Sopenharmony_ci    if (inputData.empty()) {
39c29fa5a6Sopenharmony_ci        MMI_HILOGE("Handle finger array failed, inputData is empty");
40c29fa5a6Sopenharmony_ci        return RET_ERR;
41c29fa5a6Sopenharmony_ci    }
42c29fa5a6Sopenharmony_ci    TransformPadEventToInputEvent(inputData, inputEventArray);
43c29fa5a6Sopenharmony_ci    return RET_OK;
44c29fa5a6Sopenharmony_ci}
45c29fa5a6Sopenharmony_ci
46c29fa5a6Sopenharmony_civoid ProcessingJoystickDevice::TransformPadEventToInputEvent(const std::vector<DeviceEvent>& inputData,
47c29fa5a6Sopenharmony_ci                                                             InputEventArray& inputEventArray)
48c29fa5a6Sopenharmony_ci{
49c29fa5a6Sopenharmony_ci    for (const auto &item : inputData) {
50c29fa5a6Sopenharmony_ci        if (item.eventType.empty()) {
51c29fa5a6Sopenharmony_ci            MMI_HILOGW("Not find eventType");
52c29fa5a6Sopenharmony_ci            return;
53c29fa5a6Sopenharmony_ci        }
54c29fa5a6Sopenharmony_ci        if (item.eventType == "KEY_EVENT_PRESS") {
55c29fa5a6Sopenharmony_ci            TransformKeyPressEvent(item, inputEventArray);
56c29fa5a6Sopenharmony_ci        } else if (item.eventType == "ROCKER_1") {
57c29fa5a6Sopenharmony_ci            TransformRocker1Event(item, inputEventArray);
58c29fa5a6Sopenharmony_ci        } else if (item.eventType == "KEY_EVENT_RELEASE") {
59c29fa5a6Sopenharmony_ci            TransformKeyReleaseEvent(item, inputEventArray);
60c29fa5a6Sopenharmony_ci        } else if (item.eventType == "KEY_EVENT_CLICK") {
61c29fa5a6Sopenharmony_ci            TransformKeyClickEvent(item, inputEventArray);
62c29fa5a6Sopenharmony_ci        } else if (item.eventType == "DIRECTION_KEY") {
63c29fa5a6Sopenharmony_ci            TransformDirectionKeyEvent(item, inputEventArray);
64c29fa5a6Sopenharmony_ci        } else if (item.eventType == "THROTTLE") {
65c29fa5a6Sopenharmony_ci            TransformThrottle1Event(item, inputEventArray);
66c29fa5a6Sopenharmony_ci        } else {
67c29fa5a6Sopenharmony_ci            MMI_HILOGW("Unknown eventType type");
68c29fa5a6Sopenharmony_ci        }
69c29fa5a6Sopenharmony_ci    }
70c29fa5a6Sopenharmony_ci}
71c29fa5a6Sopenharmony_ci
72c29fa5a6Sopenharmony_civoid ProcessingJoystickDevice::TransformKeyPressEvent(const DeviceEvent& joystickEvent,
73c29fa5a6Sopenharmony_ci                                                      InputEventArray& inputEventArray)
74c29fa5a6Sopenharmony_ci{
75c29fa5a6Sopenharmony_ci    uint16_t keyValue = static_cast<uint16_t>(joystickEvent.keyValue);
76c29fa5a6Sopenharmony_ci    SetKeyPressEvent(inputEventArray, joystickEvent.blockTime, keyValue);
77c29fa5a6Sopenharmony_ci    SetSynReport(inputEventArray);
78c29fa5a6Sopenharmony_ci}
79c29fa5a6Sopenharmony_ci
80c29fa5a6Sopenharmony_civoid ProcessingJoystickDevice::TransformKeyReleaseEvent(const DeviceEvent& joystickEvent,
81c29fa5a6Sopenharmony_ci                                                        InputEventArray& inputEventArray)
82c29fa5a6Sopenharmony_ci{
83c29fa5a6Sopenharmony_ci    uint16_t keyValue = static_cast<uint16_t>(joystickEvent.keyValue);
84c29fa5a6Sopenharmony_ci    SetKeyReleaseEvent(inputEventArray, joystickEvent.blockTime, keyValue);
85c29fa5a6Sopenharmony_ci    SetSynReport(inputEventArray);
86c29fa5a6Sopenharmony_ci}
87c29fa5a6Sopenharmony_ci
88c29fa5a6Sopenharmony_civoid ProcessingJoystickDevice::TransformKeyClickEvent(const DeviceEvent& joystickEvent,
89c29fa5a6Sopenharmony_ci                                                      InputEventArray& inputEventArray)
90c29fa5a6Sopenharmony_ci{
91c29fa5a6Sopenharmony_ci    uint16_t keyValue = static_cast<uint16_t>(joystickEvent.keyValue);
92c29fa5a6Sopenharmony_ci    SetKeyPressEvent(inputEventArray, joystickEvent.blockTime, keyValue);
93c29fa5a6Sopenharmony_ci    SetSynReport(inputEventArray);
94c29fa5a6Sopenharmony_ci    SetKeyReleaseEvent(inputEventArray, joystickEvent.blockTime, keyValue);
95c29fa5a6Sopenharmony_ci    SetSynReport(inputEventArray);
96c29fa5a6Sopenharmony_ci}
97c29fa5a6Sopenharmony_ci
98c29fa5a6Sopenharmony_civoid ProcessingJoystickDevice::TransformRocker1Event(const DeviceEvent& joystickEvent,
99c29fa5a6Sopenharmony_ci                                                     InputEventArray& inputEventArray)
100c29fa5a6Sopenharmony_ci{
101c29fa5a6Sopenharmony_ci    if (joystickEvent.direction.empty()) {
102c29fa5a6Sopenharmony_ci        MMI_HILOGW("Not find direction");
103c29fa5a6Sopenharmony_ci        return;
104c29fa5a6Sopenharmony_ci    }
105c29fa5a6Sopenharmony_ci    if (joystickEvent.event.empty()) {
106c29fa5a6Sopenharmony_ci        MMI_HILOGW("Not find event");
107c29fa5a6Sopenharmony_ci        return;
108c29fa5a6Sopenharmony_ci    }
109c29fa5a6Sopenharmony_ci    std::string direction = joystickEvent.direction;
110c29fa5a6Sopenharmony_ci    for (const auto &item : joystickEvent.event) {
111c29fa5a6Sopenharmony_ci        if ((direction == "left")||(direction == "right")) {
112c29fa5a6Sopenharmony_ci            SetEvAbsX(inputEventArray, 0, item);
113c29fa5a6Sopenharmony_ci        } else if ((direction == "up") || (direction == "down")) {
114c29fa5a6Sopenharmony_ci            SetEvAbsY(inputEventArray, 0, item);
115c29fa5a6Sopenharmony_ci        } else if (direction == "lt") {
116c29fa5a6Sopenharmony_ci            SetEvAbsRz(inputEventArray, 0, item);
117c29fa5a6Sopenharmony_ci        } else {
118c29fa5a6Sopenharmony_ci            MMI_HILOGW("Unknown direction move type");
119c29fa5a6Sopenharmony_ci        }
120c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
121c29fa5a6Sopenharmony_ci    }
122c29fa5a6Sopenharmony_ci
123c29fa5a6Sopenharmony_ci    if ((direction == "left") || (direction == "right")) {
124c29fa5a6Sopenharmony_ci        SetEvAbsX(inputEventArray, 0, DEFAULT_ABSX_VALUE);
125c29fa5a6Sopenharmony_ci    } else if ((direction == "up") || (direction == "down")) {
126c29fa5a6Sopenharmony_ci        SetEvAbsY(inputEventArray, 0, DEFAULT_ABSY_VALUE);
127c29fa5a6Sopenharmony_ci    } else if (direction == "lt") {
128c29fa5a6Sopenharmony_ci        SetEvAbsRz(inputEventArray, 0, DEFAULT_ABSZ_VALUE);
129c29fa5a6Sopenharmony_ci    } else {
130c29fa5a6Sopenharmony_ci        MMI_HILOGW("Unknown direction type");
131c29fa5a6Sopenharmony_ci    }
132c29fa5a6Sopenharmony_ci    SetSynReport(inputEventArray);
133c29fa5a6Sopenharmony_ci}
134c29fa5a6Sopenharmony_ci
135c29fa5a6Sopenharmony_ci
136c29fa5a6Sopenharmony_civoid ProcessingJoystickDevice::TransformDirectionKeyEvent(const DeviceEvent& joystickEvent,
137c29fa5a6Sopenharmony_ci                                                          InputEventArray& inputEventArray)
138c29fa5a6Sopenharmony_ci{
139c29fa5a6Sopenharmony_ci    if (joystickEvent.direction.empty()) {
140c29fa5a6Sopenharmony_ci        MMI_HILOGW("Not find direction");
141c29fa5a6Sopenharmony_ci        return;
142c29fa5a6Sopenharmony_ci    }
143c29fa5a6Sopenharmony_ci    std::string direction = joystickEvent.direction;
144c29fa5a6Sopenharmony_ci    if (direction == "left") {
145c29fa5a6Sopenharmony_ci        SetEvAbsHat0X(inputEventArray, 0, -1);
146c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
147c29fa5a6Sopenharmony_ci        SetEvAbsHat0X(inputEventArray, 0, 0);
148c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
149c29fa5a6Sopenharmony_ci    } else if (direction == "right") {
150c29fa5a6Sopenharmony_ci        SetEvAbsHat0X(inputEventArray, 0, 1);
151c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
152c29fa5a6Sopenharmony_ci        SetEvAbsHat0X(inputEventArray, 0, 0);
153c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
154c29fa5a6Sopenharmony_ci    } else if (direction == "up") {
155c29fa5a6Sopenharmony_ci        SetEvAbsHat0Y(inputEventArray, 0, -1);
156c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
157c29fa5a6Sopenharmony_ci        SetEvAbsHat0Y(inputEventArray, 0, 0);
158c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
159c29fa5a6Sopenharmony_ci    } else if (direction == "down") {
160c29fa5a6Sopenharmony_ci        SetEvAbsHat0Y(inputEventArray, 0, 1);
161c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
162c29fa5a6Sopenharmony_ci        SetEvAbsHat0Y(inputEventArray, 0, 0);
163c29fa5a6Sopenharmony_ci        SetSynReport(inputEventArray);
164c29fa5a6Sopenharmony_ci    } else {
165c29fa5a6Sopenharmony_ci        MMI_HILOGW("Unknown direction type");
166c29fa5a6Sopenharmony_ci    }
167c29fa5a6Sopenharmony_ci}
168c29fa5a6Sopenharmony_ci
169c29fa5a6Sopenharmony_civoid ProcessingJoystickDevice::TransformThrottle1Event(const DeviceEvent& joystickEvent,
170c29fa5a6Sopenharmony_ci                                                       InputEventArray& inputEventArray)
171c29fa5a6Sopenharmony_ci{
172c29fa5a6Sopenharmony_ci    SetThrottle(inputEventArray, joystickEvent.blockTime, joystickEvent.keyValue);
173c29fa5a6Sopenharmony_ci    SetSynReport(inputEventArray);
174c29fa5a6Sopenharmony_ci}
175c29fa5a6Sopenharmony_ci} // namespace MMI
176c29fa5a6Sopenharmony_ci} // namespace OHOS