1 /*
2 * Copyright (c) 2021-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 "processing_game_pad_device.h"
17
18 namespace OHOS {
19 namespace MMI {
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "ProcessingGamePadDevice" };
22 } // namespace
23
TransformJsonDataToInputData(const DeviceItem & originalEvent,InputEventArray & inputEventArray)24 int32_t ProcessingGamePadDevice::TransformJsonDataToInputData(const DeviceItem& originalEvent,
25 InputEventArray& inputEventArray)
26 {
27 CALL_DEBUG_ENTER;
28 if (originalEvent.events.empty()) {
29 MMI_HILOGE("Manage game pad array failed, inputData is empty");
30 return RET_ERR;
31 }
32 std::vector<DeviceEvent> inputData = originalEvent.events;
33 if (inputData.empty()) {
34 MMI_HILOGE("Manage finger array failed, inputData is empty");
35 return RET_ERR;
36 }
37 TransformPadEventToInputEvent(inputData, inputEventArray);
38 return RET_OK;
39 }
40
TransformPadEventToInputEvent(const std::vector<DeviceEvent> & inputData,InputEventArray & inputEventArray)41 void ProcessingGamePadDevice::TransformPadEventToInputEvent(const std::vector<DeviceEvent>& inputData,
42 InputEventArray& inputEventArray)
43 {
44 for (const auto &item : inputData) {
45 if (item.eventType.empty()) {
46 MMI_HILOGW("Not find eventType");
47 return;
48 }
49 if (item.eventType == "KEY_EVENT_PRESS") {
50 TransformKeyPressEvent(item, inputEventArray);
51 } else if (item.eventType == "KEY_EVENT_RELEASE") {
52 TransformKeyReleaseEvent(item, inputEventArray);
53 } else if (item.eventType == "KEY_EVENT_CLICK") {
54 TransformKeyClickEvent(item, inputEventArray);
55 } else if (item.eventType == "DIRECTION_KEY") {
56 TransformDirectionKeyEvent(item, inputEventArray);
57 } else if (item.eventType == "ROCKER_1") {
58 TransformRocker1Event(item, inputEventArray);
59 } else if (item.eventType == "ROCKER_2") {
60 TransformRocker2Event(item, inputEventArray);
61 } else {
62 MMI_HILOGW("Format json file error");
63 }
64 }
65 }
66
TransformKeyPressEvent(const DeviceEvent & padEvent,InputEventArray & inputEventArray)67 void ProcessingGamePadDevice::TransformKeyPressEvent(const DeviceEvent& padEvent, InputEventArray& inputEventArray)
68 {
69 uint16_t keyValue = static_cast<uint16_t>(padEvent.keyValue);
70 SetKeyPressEvent(inputEventArray, padEvent.blockTime, keyValue);
71 SetSynReport(inputEventArray);
72 }
73
TransformKeyReleaseEvent(const DeviceEvent & padEvent,InputEventArray & inputEventArray)74 void ProcessingGamePadDevice::TransformKeyReleaseEvent(const DeviceEvent& padEvent, InputEventArray& inputEventArray)
75 {
76 uint16_t keyValue = static_cast<uint16_t>(padEvent.keyValue);
77 SetKeyReleaseEvent(inputEventArray, padEvent.blockTime, keyValue);
78 SetSynReport(inputEventArray);
79 }
80
TransformKeyClickEvent(const DeviceEvent & padEvent,InputEventArray & inputEventArray)81 void ProcessingGamePadDevice::TransformKeyClickEvent(const DeviceEvent& padEvent, InputEventArray& inputEventArray)
82 {
83 uint16_t keyValue = static_cast<uint16_t>(padEvent.keyValue);
84 SetKeyPressEvent(inputEventArray, padEvent.blockTime, keyValue);
85 SetSynReport(inputEventArray);
86 SetKeyReleaseEvent(inputEventArray, padEvent.blockTime, keyValue);
87 SetSynReport(inputEventArray);
88 }
89
TransformRocker1Event(const DeviceEvent & padEvent,InputEventArray & inputEventArray)90 void ProcessingGamePadDevice::TransformRocker1Event(const DeviceEvent& padEvent, InputEventArray& inputEventArray)
91 {
92 if (padEvent.direction.empty()) {
93 MMI_HILOGW("Direction is empty");
94 return;
95 }
96 if (padEvent.event.empty()) {
97 MMI_HILOGW("Event is empty");
98 return;
99 }
100 std::string direction = padEvent.direction;
101 for (const auto &item : padEvent.event) {
102 uint32_t value;
103 if (direction == "left") {
104 value = ~item + 1;
105 SetEvAbsX(inputEventArray, 0, value);
106 } else if (direction == "up") {
107 value = ~item + 1;
108 SetEvAbsY(inputEventArray, 0, value);
109 } else if (direction == "right") {
110 value = item;
111 SetEvAbsX(inputEventArray, 0, value);
112 } else if (direction == "down") {
113 value = item;
114 SetEvAbsY(inputEventArray, 0, value);
115 } else if (direction == "lt") {
116 value = item;
117 SetEvAbsZ(inputEventArray, 0, value);
118 } else {
119 MMI_HILOGW("Unknown direction move type");
120 }
121 SetSynReport(inputEventArray);
122 }
123
124 if (direction == "left") {
125 SetEvAbsX(inputEventArray, 0, 0);
126 } else if (direction == "right") {
127 SetEvAbsX(inputEventArray, 0, 0);
128 } else if (direction == "up") {
129 SetEvAbsY(inputEventArray, 0, -1);
130 } else if (direction == "down") {
131 SetEvAbsY(inputEventArray, 0, -1);
132 } else if (direction == "lt") {
133 SetEvAbsZ(inputEventArray, 0, 0);
134 } else {
135 MMI_HILOGW("Unknown direction type");
136 }
137 SetSynReport(inputEventArray);
138 }
139
TransformRocker2Event(const DeviceEvent & padEvent,InputEventArray & inputEventArray)140 void ProcessingGamePadDevice::TransformRocker2Event(const DeviceEvent& padEvent, InputEventArray& inputEventArray)
141 {
142 if (padEvent.direction.empty()) {
143 MMI_HILOGW("Not find direction");
144 return;
145 }
146 if (padEvent.event.empty()) {
147 MMI_HILOGW("Not find event");
148 return;
149 }
150 std::string direction = padEvent.direction;
151 for (uint32_t item : padEvent.event) {
152 uint32_t value;
153 if (direction == "left") {
154 value = ~item + 1;
155 SetEvAbsRx(inputEventArray, 0, value);
156 } else if (direction == "right") {
157 value = item;
158 SetEvAbsRx(inputEventArray, 0, value);
159 } else if (direction == "up") {
160 value = ~item + 1;
161 SetEvAbsRy(inputEventArray, 0, value);
162 } else if (direction == "down") {
163 value = item;
164 SetEvAbsRy(inputEventArray, 0, value);
165 } else if (direction == "rt") {
166 value = item;
167 SetEvAbsRz(inputEventArray, 0, value);
168 } else {
169 MMI_HILOGW("Unknown direction move type");
170 }
171 SetSynReport(inputEventArray);
172 }
173
174 if (direction == "left") {
175 SetEvAbsRx(inputEventArray, 0, 0);
176 } else if (direction == "right") {
177 SetEvAbsRx(inputEventArray, 0, 0);
178 } else if (direction == "up") {
179 SetEvAbsRy(inputEventArray, 0, -1);
180 } else if (direction == "down") {
181 SetEvAbsRy(inputEventArray, 0, -1);
182 } else if (direction == "rt") {
183 SetEvAbsRz(inputEventArray, 0, 0);
184 } else {
185 MMI_HILOGW("Unknown direction type");
186 }
187 SetSynReport(inputEventArray);
188 }
189
TransformDirectionKeyEvent(const DeviceEvent & padEvent,InputEventArray & inputEventArray)190 void ProcessingGamePadDevice::TransformDirectionKeyEvent(const DeviceEvent& padEvent, InputEventArray& inputEventArray)
191 {
192 if (padEvent.direction.empty()) {
193 MMI_HILOGW("Not find direction");
194 return;
195 }
196 std::string direction = padEvent.direction;
197 if (direction == "left") {
198 SetEvAbsHat0X(inputEventArray, padEvent.blockTime, -1);
199 SetSynReport(inputEventArray);
200 SetEvAbsHat0X(inputEventArray, padEvent.blockTime, 0);
201 SetSynReport(inputEventArray);
202 } else if (direction == "right") {
203 SetEvAbsHat0X(inputEventArray, padEvent.blockTime, 1);
204 SetSynReport(inputEventArray);
205 SetEvAbsHat0X(inputEventArray, padEvent.blockTime, 0);
206 SetSynReport(inputEventArray);
207 } else if (direction == "up") {
208 SetEvAbsHat0Y(inputEventArray, padEvent.blockTime, -1);
209 SetSynReport(inputEventArray);
210 SetEvAbsHat0Y(inputEventArray, padEvent.blockTime, 0);
211 SetSynReport(inputEventArray);
212 } else if (direction == "down") {
213 SetEvAbsHat0Y(inputEventArray, padEvent.blockTime, 1);
214 SetSynReport(inputEventArray);
215 SetEvAbsHat0Y(inputEventArray, padEvent.blockTime, 0);
216 SetSynReport(inputEventArray);
217 } else {
218 MMI_HILOGW("Unknown direction type");
219 }
220 }
221 } // namespace MMI
222 } // namespace OHOS
223