1 /*
2 * Copyright (c) 2021-2024 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 "processing_joystick_device.h"
17
18 #undef MMI_LOG_TAG
19 #define MMI_LOG_TAG "ProcessingJoystickDevice"
20
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 constexpr int32_t DEFAULT_ABSX_VALUE { 8188 };
25 constexpr int32_t DEFAULT_ABSY_VALUE { 8192 };
26 constexpr int32_t DEFAULT_ABSZ_VALUE { 125 };
27 } // namespace
28
TransformJsonDataToInputData(const DeviceItem & originalEvent,InputEventArray & inputEventArray)29 int32_t ProcessingJoystickDevice::TransformJsonDataToInputData(const DeviceItem& originalEvent,
30 InputEventArray& inputEventArray)
31 {
32 CALL_DEBUG_ENTER;
33 if (originalEvent.events.empty()) {
34 MMI_HILOGE("Handle joystick array failed, inputData is empty");
35 return RET_ERR;
36 }
37 std::vector<DeviceEvent> inputData = originalEvent.events;
38 if (inputData.empty()) {
39 MMI_HILOGE("Handle finger array failed, inputData is empty");
40 return RET_ERR;
41 }
42 TransformPadEventToInputEvent(inputData, inputEventArray);
43 return RET_OK;
44 }
45
TransformPadEventToInputEvent(const std::vector<DeviceEvent> & inputData,InputEventArray & inputEventArray)46 void ProcessingJoystickDevice::TransformPadEventToInputEvent(const std::vector<DeviceEvent>& inputData,
47 InputEventArray& inputEventArray)
48 {
49 for (const auto &item : inputData) {
50 if (item.eventType.empty()) {
51 MMI_HILOGW("Not find eventType");
52 return;
53 }
54 if (item.eventType == "KEY_EVENT_PRESS") {
55 TransformKeyPressEvent(item, inputEventArray);
56 } else if (item.eventType == "ROCKER_1") {
57 TransformRocker1Event(item, inputEventArray);
58 } else if (item.eventType == "KEY_EVENT_RELEASE") {
59 TransformKeyReleaseEvent(item, inputEventArray);
60 } else if (item.eventType == "KEY_EVENT_CLICK") {
61 TransformKeyClickEvent(item, inputEventArray);
62 } else if (item.eventType == "DIRECTION_KEY") {
63 TransformDirectionKeyEvent(item, inputEventArray);
64 } else if (item.eventType == "THROTTLE") {
65 TransformThrottle1Event(item, inputEventArray);
66 } else {
67 MMI_HILOGW("Unknown eventType type");
68 }
69 }
70 }
71
TransformKeyPressEvent(const DeviceEvent & joystickEvent,InputEventArray & inputEventArray)72 void ProcessingJoystickDevice::TransformKeyPressEvent(const DeviceEvent& joystickEvent,
73 InputEventArray& inputEventArray)
74 {
75 uint16_t keyValue = static_cast<uint16_t>(joystickEvent.keyValue);
76 SetKeyPressEvent(inputEventArray, joystickEvent.blockTime, keyValue);
77 SetSynReport(inputEventArray);
78 }
79
TransformKeyReleaseEvent(const DeviceEvent & joystickEvent,InputEventArray & inputEventArray)80 void ProcessingJoystickDevice::TransformKeyReleaseEvent(const DeviceEvent& joystickEvent,
81 InputEventArray& inputEventArray)
82 {
83 uint16_t keyValue = static_cast<uint16_t>(joystickEvent.keyValue);
84 SetKeyReleaseEvent(inputEventArray, joystickEvent.blockTime, keyValue);
85 SetSynReport(inputEventArray);
86 }
87
TransformKeyClickEvent(const DeviceEvent & joystickEvent,InputEventArray & inputEventArray)88 void ProcessingJoystickDevice::TransformKeyClickEvent(const DeviceEvent& joystickEvent,
89 InputEventArray& inputEventArray)
90 {
91 uint16_t keyValue = static_cast<uint16_t>(joystickEvent.keyValue);
92 SetKeyPressEvent(inputEventArray, joystickEvent.blockTime, keyValue);
93 SetSynReport(inputEventArray);
94 SetKeyReleaseEvent(inputEventArray, joystickEvent.blockTime, keyValue);
95 SetSynReport(inputEventArray);
96 }
97
TransformRocker1Event(const DeviceEvent & joystickEvent,InputEventArray & inputEventArray)98 void ProcessingJoystickDevice::TransformRocker1Event(const DeviceEvent& joystickEvent,
99 InputEventArray& inputEventArray)
100 {
101 if (joystickEvent.direction.empty()) {
102 MMI_HILOGW("Not find direction");
103 return;
104 }
105 if (joystickEvent.event.empty()) {
106 MMI_HILOGW("Not find event");
107 return;
108 }
109 std::string direction = joystickEvent.direction;
110 for (const auto &item : joystickEvent.event) {
111 if ((direction == "left")||(direction == "right")) {
112 SetEvAbsX(inputEventArray, 0, item);
113 } else if ((direction == "up") || (direction == "down")) {
114 SetEvAbsY(inputEventArray, 0, item);
115 } else if (direction == "lt") {
116 SetEvAbsRz(inputEventArray, 0, item);
117 } else {
118 MMI_HILOGW("Unknown direction move type");
119 }
120 SetSynReport(inputEventArray);
121 }
122
123 if ((direction == "left") || (direction == "right")) {
124 SetEvAbsX(inputEventArray, 0, DEFAULT_ABSX_VALUE);
125 } else if ((direction == "up") || (direction == "down")) {
126 SetEvAbsY(inputEventArray, 0, DEFAULT_ABSY_VALUE);
127 } else if (direction == "lt") {
128 SetEvAbsRz(inputEventArray, 0, DEFAULT_ABSZ_VALUE);
129 } else {
130 MMI_HILOGW("Unknown direction type");
131 }
132 SetSynReport(inputEventArray);
133 }
134
135
TransformDirectionKeyEvent(const DeviceEvent & joystickEvent,InputEventArray & inputEventArray)136 void ProcessingJoystickDevice::TransformDirectionKeyEvent(const DeviceEvent& joystickEvent,
137 InputEventArray& inputEventArray)
138 {
139 if (joystickEvent.direction.empty()) {
140 MMI_HILOGW("Not find direction");
141 return;
142 }
143 std::string direction = joystickEvent.direction;
144 if (direction == "left") {
145 SetEvAbsHat0X(inputEventArray, 0, -1);
146 SetSynReport(inputEventArray);
147 SetEvAbsHat0X(inputEventArray, 0, 0);
148 SetSynReport(inputEventArray);
149 } else if (direction == "right") {
150 SetEvAbsHat0X(inputEventArray, 0, 1);
151 SetSynReport(inputEventArray);
152 SetEvAbsHat0X(inputEventArray, 0, 0);
153 SetSynReport(inputEventArray);
154 } else if (direction == "up") {
155 SetEvAbsHat0Y(inputEventArray, 0, -1);
156 SetSynReport(inputEventArray);
157 SetEvAbsHat0Y(inputEventArray, 0, 0);
158 SetSynReport(inputEventArray);
159 } else if (direction == "down") {
160 SetEvAbsHat0Y(inputEventArray, 0, 1);
161 SetSynReport(inputEventArray);
162 SetEvAbsHat0Y(inputEventArray, 0, 0);
163 SetSynReport(inputEventArray);
164 } else {
165 MMI_HILOGW("Unknown direction type");
166 }
167 }
168
TransformThrottle1Event(const DeviceEvent & joystickEvent,InputEventArray & inputEventArray)169 void ProcessingJoystickDevice::TransformThrottle1Event(const DeviceEvent& joystickEvent,
170 InputEventArray& inputEventArray)
171 {
172 SetThrottle(inputEventArray, joystickEvent.blockTime, joystickEvent.keyValue);
173 SetSynReport(inputEventArray);
174 }
175 } // namespace MMI
176 } // namespace OHOS