1 /*
2 * Copyright (c) 2024-2025 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 "remote_control_transform_processor.h"
17
18 #include <linux/input.h>
19
20 #include "event_log_helper.h"
21 #include "input_device_manager.h"
22 #include "i_input_windows_manager.h"
23
24 #undef MMI_LOG_DOMAIN
25 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "Remote_ControlTransformProcessor"
28
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 constexpr int32_t POINTER_MOVEFLAG { 7 };
33 constexpr int32_t PRINT_INTERVAL_COUNT { 100 };
34 }
35
Remote_ControlTransformProcessor(int32_t deviceId)36 Remote_ControlTransformProcessor::Remote_ControlTransformProcessor(int32_t deviceId)
37 : deviceId_(deviceId)
38 {
39 InitToolTypes();
40 }
41
DumpInner()42 bool Remote_ControlTransformProcessor::DumpInner()
43 {
44 static std::string deviceName("default");
45 auto deviceId = pointerEvent_->GetDeviceId();
46 auto device = INPUT_DEV_MGR->GetInputDevice(deviceId);
47 CHKPF(device);
48 deviceName = device->GetName();
49 EventLogHelper::PrintEventData(pointerEvent_, MMI_LOG_FREEZE);
50 aggregator_.Record(MMI_LOG_FREEZE, deviceName + ", TW: " +
51 std::to_string(pointerEvent_->GetTargetWindowId()), std::to_string(pointerEvent_->GetId()));
52 return true;
53 }
54
OnEvent(struct libinput_event * event)55 std::shared_ptr<PointerEvent> Remote_ControlTransformProcessor::OnEvent(struct libinput_event *event)
56 {
57 CALL_DEBUG_ENTER;
58 CHKPP(event);
59 if (pointerEvent_ == nullptr) {
60 pointerEvent_ = PointerEvent::Create();
61 CHKPP(pointerEvent_);
62 }
63 const int32_t type = libinput_event_get_type(event);
64 pointerEvent_->ClearAxisValue();
65 switch (type) {
66 case LIBINPUT_EVENT_TOUCH_DOWN: {
67 MMI_HILOGD("Tv Touch event is not Motion");
68 return pointerEvent_;
69 }
70 case LIBINPUT_EVENT_TOUCH_UP: {
71 MMI_HILOGD("Tv Touch event is not Motion");
72 return pointerEvent_;
73 }
74 case LIBINPUT_EVENT_TOUCH_MOTION: {
75 processedCount_++;
76 if (!OnEventTouchMotion(event) && (processedCount_ == PRINT_INTERVAL_COUNT)) {
77 MMI_HILOGE("Get OnEventTvTouchMotion failed");
78 processedCount_ = 0;
79 }
80 break;
81 }
82 default: {
83 MMI_HILOGE("Unknown event type, touchType:%{public}d", type);
84 return nullptr;
85 }
86 }
87
88 if (!HandlePostInner(event)) {
89 CHKPP(pointerEvent_);
90 return nullptr;
91 }
92 MMI_HILOGD("TW:%{public}d", pointerEvent_->GetTargetWindowId());
93 WIN_MGR->UpdateTargetPointer(pointerEvent_);
94 MMI_HILOGD("TW:%{public}d", pointerEvent_->GetTargetWindowId());
95 DumpInner();
96 return pointerEvent_;
97 }
98
OnEventTouchMotion(struct libinput_event * event)99 bool Remote_ControlTransformProcessor::OnEventTouchMotion(struct libinput_event* event)
100 {
101 CALL_DEBUG_ENTER;
102 CHKPF(pointerEvent_);
103 #ifndef OHOS_BUILD_ENABLE_WATCH
104 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
105 pointerEvent_->SetButtonId(buttonId_);
106 auto touch = libinput_event_get_touch_event(event);
107 CHKPF(touch);
108 EventTouch touchInfo;
109 int32_t logicalDisplayId = pointerEvent_->GetTargetDisplayId();
110 if (!WIN_MGR->TouchPointToDisplayPoint(deviceId_, touch, touchInfo, logicalDisplayId)) {
111 MMI_HILOGE("Get TouchMotionPointToDisplayPoint failed");
112 return false;
113 }
114 double x = touchInfo.point.x;
115 double y = touchInfo.point.y;
116 MMI_HILOGD("Change coordinate: x:%.2f, y:%.2f, currentDisplayId:%d",
117 x, y, logicalDisplayId);
118 WIN_MGR->UpdateAndAdjustMouseLocation(logicalDisplayId, x, y);
119 pointerEvent_->SetTargetDisplayId(logicalDisplayId);
120 MMI_HILOGD("Change coordinate: x:%.2f, y:%.2f, currentDisplayId:%d",
121 x, y, logicalDisplayId);
122 #endif // OHOS_BUILD_ENABLE_WATCH
123 return true;
124 }
125
HandlePostInner(struct libinput_event * event)126 bool Remote_ControlTransformProcessor::HandlePostInner(struct libinput_event* event)
127 {
128 CALL_DEBUG_ENTER;
129 CHKPF(pointerEvent_);
130 auto mouseInfo = WIN_MGR->GetMouseInfo();
131 PointerEvent::PointerItem pointerItem;
132 pointerItem.SetDisplayX(mouseInfo.physicalX);
133 pointerItem.SetDisplayY(mouseInfo.physicalY);
134 pointerItem.SetWindowX(0);
135 pointerItem.SetWindowY(0);
136 pointerItem.SetPointerId(0);
137 pointerItem.SetPressed(isPressed_);
138
139 int64_t time = GetSysClockTime();
140 pointerItem.SetDownTime(time);
141 pointerItem.SetWidth(0);
142 pointerItem.SetHeight(0);
143 pointerItem.SetDeviceId(deviceId_);
144 auto touch = libinput_event_get_touch_event(event);
145 CHKPF(touch);
146 int32_t moveFlag = libinput_event_touch_get_move_flag(touch);
147 double pressure = libinput_event_touch_get_pressure(touch);
148 int32_t longAxis = libinput_event_get_touch_contact_long_axis(touch);
149 int32_t shortAxis = libinput_event_get_touch_contact_short_axis(touch);
150 int32_t seatSlot = libinput_event_touch_get_seat_slot(touch);
151 pointerItem.SetMoveFlag(POINTER_MOVEFLAG);
152 pointerItem.SetPressure(pressure);
153 pointerItem.SetLongAxis(longAxis);
154 pointerItem.SetShortAxis(shortAxis);
155
156 pointerEvent_->UpdateId();
157 StartLogTraceId(pointerEvent_->GetId(), pointerEvent_->GetEventType(), pointerEvent_->GetPointerAction());
158 pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
159 pointerEvent_->SetActionTime(time);
160 pointerEvent_->SetActionStartTime(time);
161 pointerEvent_->SetDeviceId(deviceId_);
162 pointerEvent_->SetPointerId(seatSlot);
163 pointerEvent_->SetTargetDisplayId(mouseInfo.displayId);
164 pointerEvent_->SetTargetWindowId(-1);
165 pointerEvent_->SetAgentWindowId(-1);
166 pointerItem.SetToolType(PointerEvent::TOOL_TYPE_MOUSE);
167 pointerEvent_->UpdatePointerItem(pointerEvent_->GetPointerId(), pointerItem);
168 return true;
169 }
InitToolTypes()170 void Remote_ControlTransformProcessor::InitToolTypes()
171 {
172 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_PEN, PointerEvent::TOOL_TYPE_PEN));
173 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_RUBBER, PointerEvent::TOOL_TYPE_RUBBER));
174 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_BRUSH, PointerEvent::TOOL_TYPE_BRUSH));
175 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_PENCIL, PointerEvent::TOOL_TYPE_PENCIL));
176 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_AIRBRUSH, PointerEvent::TOOL_TYPE_AIRBRUSH));
177 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_FINGER, PointerEvent::TOOL_TYPE_FINGER));
178 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_MOUSE, PointerEvent::TOOL_TYPE_MOUSE));
179 vecToolType_.emplace_back(std::make_pair(BTN_TOOL_LENS, PointerEvent::TOOL_TYPE_LENS));
180 }
181 } // namespace MMI
182 } // namespace OHOS
183