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