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 "touchpad_transform_processor.h"
17
18 #include <sstream>
19
20 #include <linux/input.h>
21
22 #include "event_log_helper.h"
23 #include "input_windows_manager.h"
24 #include "mmi_log.h"
25
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MMI_LOG_DOMAIN, "TouchPadTransformProcessor" };
30 constexpr int32_t MT_TOOL_NONE { -1 };
31 constexpr int32_t BTN_DOWN { 1 };
32 } // namespace
33
TouchPadTransformProcessor(int32_t deviceId)34 TouchPadTransformProcessor::TouchPadTransformProcessor(int32_t deviceId)
35 : deviceId_(deviceId)
36 {
37 InitToolType();
38 }
39
OnEventTouchPadDown(struct libinput_event * event)40 void TouchPadTransformProcessor::OnEventTouchPadDown(struct libinput_event *event)
41 {
42 CALL_DEBUG_ENTER;
43 CHKPV(event);
44 auto touchpad = libinput_event_get_touchpad_event(event);
45 CHKPV(touchpad);
46 auto device = libinput_event_get_device(event);
47 CHKPV(device);
48
49 int64_t time = GetSysClockTime();
50 auto pointIds = pointerEvent_->GetPointerIds();
51 if (pointIds.empty()) {
52 pointerEvent_->SetActionStartTime(time);
53 }
54 pointerEvent_->SetActionTime(time);
55 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
56 PointerEvent::PointerItem item;
57 int32_t longAxis = libinput_event_touchpad_get_touch_contact_long_axis(touchpad);
58 int32_t shortAxis = libinput_event_touchpad_get_touch_contact_short_axis(touchpad);
59 double pressure = libinput_event_touchpad_get_pressure(touchpad);
60 int32_t seatSlot = libinput_event_touchpad_get_seat_slot(touchpad);
61 double logicalX = libinput_event_touchpad_get_x(touchpad);
62 double logicalY = libinput_event_touchpad_get_y(touchpad);
63 double toolPhysicalX = libinput_event_touchpad_get_tool_x(touchpad);
64 double toolPhysicalY = libinput_event_touchpad_get_tool_y(touchpad);
65 double toolWidth = libinput_event_touchpad_get_tool_width(touchpad);
66 double toolHeight = libinput_event_touchpad_get_tool_height(touchpad);
67 int32_t toolType = GetTouchPadToolType(touchpad, device);
68
69 item.SetLongAxis(longAxis);
70 item.SetShortAxis(shortAxis);
71 item.SetPressure(pressure);
72 item.SetToolType(toolType);
73 item.SetPointerId(seatSlot);
74 item.SetDownTime(time);
75 item.SetPressed(true);
76 item.SetDisplayX(static_cast<int32_t>(logicalX));
77 item.SetDisplayY(static_cast<int32_t>(logicalY));
78 item.SetToolDisplayX(static_cast<int32_t>(toolPhysicalX));
79 item.SetToolDisplayY(static_cast<int32_t>(toolPhysicalY));
80 item.SetToolWidth(static_cast<int32_t>(toolWidth));
81 item.SetToolHeight(static_cast<int32_t>(toolHeight));
82 item.SetDeviceId(deviceId_);
83 pointerEvent_->SetDeviceId(deviceId_);
84 pointerEvent_->AddPointerItem(item);
85 pointerEvent_->SetPointerId(seatSlot);
86 }
87
OnEventTouchPadMotion(struct libinput_event * event)88 void TouchPadTransformProcessor::OnEventTouchPadMotion(struct libinput_event *event)
89 {
90 CALL_DEBUG_ENTER;
91 CHKPV(event);
92 auto touchpad = libinput_event_get_touchpad_event(event);
93 CHKPV(touchpad);
94 int32_t seatSlot = libinput_event_touchpad_get_seat_slot(touchpad);
95
96 int64_t time = GetSysClockTime();
97 pointerEvent_->SetActionTime(time);
98 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
99 PointerEvent::PointerItem item;
100 if (!pointerEvent_->GetPointerItem(seatSlot, item)) {
101 MMI_HILOGE("Can't find the pointer item data, seatSlot:%{public}d, errCode:%{public}d",
102 seatSlot, PARAM_INPUT_FAIL);
103 return;
104 }
105 int32_t longAxis = libinput_event_touchpad_get_touch_contact_long_axis(touchpad);
106 int32_t shortAxis = libinput_event_touchpad_get_touch_contact_short_axis(touchpad);
107 double pressure = libinput_event_touchpad_get_pressure(touchpad);
108 double logicalX = libinput_event_touchpad_get_x(touchpad);
109 double logicalY = libinput_event_touchpad_get_y(touchpad);
110 double toolPhysicalX = libinput_event_touchpad_get_tool_x(touchpad);
111 double toolPhysicalY = libinput_event_touchpad_get_tool_y(touchpad);
112 double toolWidth = libinput_event_touchpad_get_tool_width(touchpad);
113 double toolHeight = libinput_event_touchpad_get_tool_height(touchpad);
114
115 item.SetLongAxis(longAxis);
116 item.SetShortAxis(shortAxis);
117 item.SetPressure(pressure);
118 item.SetDisplayX(static_cast<int32_t>(logicalX));
119 item.SetDisplayY(static_cast<int32_t>(logicalY));
120 item.SetToolDisplayX(static_cast<int32_t>(toolPhysicalX));
121 item.SetToolDisplayY(static_cast<int32_t>(toolPhysicalY));
122 item.SetToolWidth(static_cast<int32_t>(toolWidth));
123 item.SetToolHeight(static_cast<int32_t>(toolHeight));
124 pointerEvent_->UpdatePointerItem(seatSlot, item);
125 pointerEvent_->SetPointerId(seatSlot);
126 }
127
OnEventTouchPadUp(struct libinput_event * event)128 void TouchPadTransformProcessor::OnEventTouchPadUp(struct libinput_event *event)
129 {
130 CALL_DEBUG_ENTER;
131 CHKPV(event);
132 auto touchpad = libinput_event_get_touchpad_event(event);
133 CHKPV(touchpad);
134 int32_t seatSlot = libinput_event_touchpad_get_seat_slot(touchpad);
135
136 int64_t time = GetSysClockTime();
137 pointerEvent_->SetActionTime(time);
138 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
139
140 PointerEvent::PointerItem item;
141 if (!pointerEvent_->GetPointerItem(seatSlot, item)) {
142 MMI_HILOGE("Can't find the pointer item data, seatSlot:%{public}d, errCode:%{public}d",
143 seatSlot, PARAM_INPUT_FAIL);
144 return;
145 }
146 item.SetPressed(false);
147 pointerEvent_->UpdatePointerItem(seatSlot, item);
148 pointerEvent_->SetPointerId(seatSlot);
149 }
150
OnEvent(struct libinput_event * event)151 std::shared_ptr<PointerEvent> TouchPadTransformProcessor::OnEvent(struct libinput_event *event)
152 {
153 CALL_DEBUG_ENTER;
154 CHKPP(event);
155 if (pointerEvent_ == nullptr) {
156 pointerEvent_ = PointerEvent::Create();
157 CHKPP(pointerEvent_);
158 }
159 int32_t type = libinput_event_get_type(event);
160 switch (type) {
161 case LIBINPUT_EVENT_TOUCHPAD_DOWN: {
162 OnEventTouchPadDown(event);
163 break;
164 }
165 case LIBINPUT_EVENT_TOUCHPAD_UP: {
166 OnEventTouchPadUp(event);
167 break;
168 }
169 case LIBINPUT_EVENT_TOUCHPAD_MOTION: {
170 OnEventTouchPadMotion(event);
171 break;
172 }
173 default: {
174 return nullptr;
175 }
176 }
177 pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
178 pointerEvent_->UpdateId();
179 MMI_HILOGD("Pointer event dispatcher of server:");
180 EventLogHelper::PrintEventData(pointerEvent_, pointerEvent_->GetPointerAction(),
181 pointerEvent_->GetPointerIds().size());
182 return pointerEvent_;
183 }
184
GetTouchPadToolType(struct libinput_event_touch * touchpad,struct libinput_device * device)185 int32_t TouchPadTransformProcessor::GetTouchPadToolType(
186 struct libinput_event_touch *touchpad, struct libinput_device *device)
187 {
188 int32_t toolType = libinput_event_touchpad_get_tool_type(touchpad);
189 switch (toolType) {
190 case MT_TOOL_NONE: {
191 return GetTouchPadToolType(device);
192 }
193 case MT_TOOL_FINGER: {
194 return PointerEvent::TOOL_TYPE_FINGER;
195 }
196 case MT_TOOL_PEN: {
197 return PointerEvent::TOOL_TYPE_PEN;
198 }
199 default : {
200 MMI_HILOGW("Unknown tool type, identified as finger, toolType:%{public}d", toolType);
201 return PointerEvent::TOOL_TYPE_FINGER;
202 }
203 }
204 }
205
GetTouchPadToolType(struct libinput_device * device)206 int32_t TouchPadTransformProcessor::GetTouchPadToolType(struct libinput_device *device)
207 {
208 for (const auto &item : vecToolType_) {
209 if (libinput_device_touchpad_btn_tool_type_down(device, item.first) == BTN_DOWN) {
210 return item.second;
211 }
212 }
213 MMI_HILOGW("Unknown Btn tool type, identified as finger");
214 return PointerEvent::TOOL_TYPE_FINGER;
215 }
216
InitToolType()217 void TouchPadTransformProcessor::InitToolType()
218 {
219 vecToolType_.push_back(std::make_pair(BTN_TOOL_PEN, PointerEvent::TOOL_TYPE_PEN));
220 vecToolType_.push_back(std::make_pair(BTN_TOOL_RUBBER, PointerEvent::TOOL_TYPE_RUBBER));
221 vecToolType_.push_back(std::make_pair(BTN_TOOL_BRUSH, PointerEvent::TOOL_TYPE_BRUSH));
222 vecToolType_.push_back(std::make_pair(BTN_TOOL_PENCIL, PointerEvent::TOOL_TYPE_PENCIL));
223 vecToolType_.push_back(std::make_pair(BTN_TOOL_AIRBRUSH, PointerEvent::TOOL_TYPE_AIRBRUSH));
224 vecToolType_.push_back(std::make_pair(BTN_TOOL_FINGER, PointerEvent::TOOL_TYPE_FINGER));
225 vecToolType_.push_back(std::make_pair(BTN_TOOL_MOUSE, PointerEvent::TOOL_TYPE_MOUSE));
226 vecToolType_.push_back(std::make_pair(BTN_TOOL_LENS, PointerEvent::TOOL_TYPE_LENS));
227 }
228 } // namespace MMI
229 } // namespace OHOS
230