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