• 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_mouse_device.h"
17 
18 namespace OHOS {
19 namespace MMI {
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "ProcessingMouseDevice" };
22 } // namespace
23 
TransformJsonDataToInputData(const DeviceItem & fingerEventArrays,InputEventArray & inputEventArray)24 int32_t ProcessingMouseDevice::TransformJsonDataToInputData(const DeviceItem& fingerEventArrays,
25     InputEventArray& inputEventArray)
26 {
27     CALL_DEBUG_ENTER;
28     std::vector<DeviceEvent> inputData = fingerEventArrays.events;
29     if (inputData.empty()) {
30         MMI_HILOGE("Manage KeyBoard array failed, inputData is empty.");
31         return RET_ERR;
32     }
33     std::vector<MouseEvent> mouseEventArray;
34     if (AnalysisMouseEvent(inputData, mouseEventArray) == RET_ERR) {
35         return RET_ERR;
36     }
37     TransformMouseEventToInputEvent(mouseEventArray, inputEventArray);
38     return RET_OK;
39 }
40 
TransformMouseEventToInputEvent(const std::vector<MouseEvent> & mouseEventArray,InputEventArray & inputEventArray)41 void ProcessingMouseDevice::TransformMouseEventToInputEvent(const std::vector<MouseEvent>& mouseEventArray,
42     InputEventArray& inputEventArray)
43 {
44     for (const auto &item : mouseEventArray) {
45         if (item.eventType == "KEY_EVENT_PRESS") {
46             TransformKeyPressEvent(item, inputEventArray);
47         } else if (item.eventType == "KEY_EVENT_RELEASE") {
48             TransformKeyReleaseEvent(item, inputEventArray);
49         } else if (item.eventType == "KEY_EVENT_CLICK") {
50             TransformKeyClickEvent(item, inputEventArray);
51         } else if (item.eventType == "MOUSE_EVENT_MOVE") {
52             TransformMouseMoveEvent(item, inputEventArray);
53         } else if (item.eventType == "MOUSE_EVENT_WHEEL") {
54             TransformMouseWheelEvent(item, inputEventArray);
55         } else if (item.eventType == "MOUSE_EVENT_HWHEEL") {
56             TransformMouseHwheelEvent(item, inputEventArray);
57         } else {
58             MMI_HILOGW("Json file format error");
59         }
60     }
61 }
62 
AnalysisMouseEvent(const std::vector<DeviceEvent> & inputData,std::vector<MouseEvent> & mouseEventArray)63 int32_t ProcessingMouseDevice::AnalysisMouseEvent(const std::vector<DeviceEvent>& inputData,
64     std::vector<MouseEvent>& mouseEventArray)
65 {
66     MouseEvent mouseEvent = {};
67     for (const auto &item : inputData) {
68         mouseEvent = {};
69         mouseEvent.eventType = item.eventType;
70         mouseEvent.keyValue = item.keyValue;
71         mouseEvent.blockTime = item.blockTime;
72         mouseEvent.xPos = item.xPos;
73         mouseEvent.yPos = item.yPos;
74         mouseEvent.distance = item.distance;
75         mouseEvent.direction = item.direction;
76         mouseEventArray.push_back(mouseEvent);
77     }
78     return RET_OK;
79 }
80 
TransformKeyPressEvent(const MouseEvent & mouseEvent,InputEventArray & inputEventArray)81 void ProcessingMouseDevice::TransformKeyPressEvent(const MouseEvent& mouseEvent,
82     InputEventArray& inputEventArray)
83 {
84     uint16_t keyValue = static_cast<uint16_t>(mouseEvent.keyValue);
85     SetKeyPressEvent(inputEventArray, mouseEvent.blockTime, keyValue);
86     SetSynReport(inputEventArray);
87 }
88 
TransformKeyReleaseEvent(const MouseEvent & mouseEvent,InputEventArray & inputEventArray)89 void ProcessingMouseDevice::TransformKeyReleaseEvent(const MouseEvent& mouseEvent,
90     InputEventArray& inputEventArray)
91 {
92     uint16_t keyValue = static_cast<uint16_t>(mouseEvent.keyValue);
93     SetKeyReleaseEvent(inputEventArray, mouseEvent.blockTime, keyValue);
94     SetSynReport(inputEventArray);
95 }
96 
TransformKeyClickEvent(const MouseEvent & mouseEvent,InputEventArray & inputEventArray)97 void ProcessingMouseDevice::TransformKeyClickEvent(const MouseEvent& mouseEvent,
98                                                    InputEventArray& inputEventArray)
99 {
100     uint16_t keyValue = static_cast<uint16_t>(mouseEvent.keyValue);
101     SetKeyPressEvent(inputEventArray, mouseEvent.blockTime, keyValue);
102     SetSynReport(inputEventArray);
103     SetKeyReleaseEvent(inputEventArray, mouseEvent.blockTime, keyValue);
104     SetSynReport(inputEventArray);
105 }
106 
TransformMouseMoveEvent(const MouseEvent & mouseEvent,InputEventArray & inputEventArray)107 void ProcessingMouseDevice::TransformMouseMoveEvent(const MouseEvent& mouseEvent,
108     InputEventArray& inputEventArray)
109 {
110     SetRelX(inputEventArray, mouseEvent.blockTime, mouseEvent.xPos);
111     SetSynReport(inputEventArray);
112     SetRelY(inputEventArray, mouseEvent.blockTime, mouseEvent.yPos);
113     SetSynReport(inputEventArray);
114 }
115 
TransformMouseWheelEvent(const MouseEvent & mouseEvent,InputEventArray & inputEventArray)116 void ProcessingMouseDevice::TransformMouseWheelEvent(const MouseEvent& mouseEvent,
117     InputEventArray& inputEventArray)
118 {
119     uint32_t distance = static_cast<uint32_t>(mouseEvent.distance);
120     if (mouseEvent.direction == "up") {
121         distance = ~distance + 1;
122     }
123     SetRelWheel(inputEventArray, mouseEvent.blockTime, static_cast<int32_t>(distance));
124     SetSynReport(inputEventArray);
125 }
126 
TransformMouseHwheelEvent(const MouseEvent & mouseEvent,InputEventArray & inputEventArray)127 void ProcessingMouseDevice::TransformMouseHwheelEvent(const MouseEvent& mouseEvent,
128     InputEventArray& inputEventArray)
129 {
130     uint32_t distance = static_cast<uint32_t>(mouseEvent.distance);
131     if (mouseEvent.direction == "left") {
132         distance = ~distance + 1;
133     }
134     SetRelWheel(inputEventArray, mouseEvent.blockTime, static_cast<int32_t>(distance));
135     SetSynReport(inputEventArray);
136 }
137 } // namespace MMI
138 } // namespace OHOS