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 "gesture_transform_processor.h"
17 
18 #include "mmi_log.h"
19 #include "mouse_device_state.h"
20 
21 #undef MMI_LOG_DOMAIN
22 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "GestureTransformProcessor"
25 
26 namespace OHOS {
27 namespace MMI {
GestureTransformProcessor(int32_t deviceId)28 GestureTransformProcessor::GestureTransformProcessor(int32_t deviceId)
29     : deviceId_(deviceId) {}
30 
OnEventTouchPadPinchBegin(libinput_event_gesture * gesture)31 void GestureTransformProcessor::OnEventTouchPadPinchBegin(libinput_event_gesture *gesture)
32 {
33     CALL_DEBUG_ENTER;
34     CHKPV(gesture);
35     int64_t time = static_cast<int64_t>(libinput_event_gesture_get_time(gesture));
36     double scale = libinput_event_gesture_get_scale(gesture);
37     pointerEvent_->SetActionTime(GetSysClockTime());
38     pointerEvent_->SetActionStartTime(time);
39 
40     PointerEvent::PointerItem pointerItem;
41     pointerItem.SetDownTime(time);
42     pointerItem.SetDisplayX(MouseState->GetMouseCoordsX());
43     pointerItem.SetDisplayY(MouseState->GetMouseCoordsY());
44     pointerItem.SetDeviceId(deviceId_);
45     pointerItem.SetPointerId(defaultPointerId);
46     pointerItem.SetWidth(0);
47     pointerItem.SetHeight(0);
48     pointerItem.SetPressed(MouseState->IsLeftBtnPressed());
49     pointerEvent_->AddPointerItem(pointerItem);
50 
51     pointerEvent_->ClearButtonPressed();
52     std::vector<int32_t> pressedButtons;
53     MouseState->GetPressedButtons(pressedButtons);
54     for (const auto &item : pressedButtons) {
55         pointerEvent_->SetButtonPressed(item);
56     }
57 
58     pointerEvent_->SetDeviceId(deviceId_);
59     pointerEvent_->SetTargetDisplayId(0);
60     pointerEvent_->SetPointerId(defaultPointerId);
61     pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, scale);
62 }
63 
OnEventTouchPadPinchUpdate(libinput_event_gesture * gesture)64 void GestureTransformProcessor::OnEventTouchPadPinchUpdate(libinput_event_gesture *gesture)
65 {
66     CALL_DEBUG_ENTER;
67     CHKPV(gesture);
68     int64_t time = static_cast<int64_t>(libinput_event_gesture_get_time(gesture));
69     double scale = libinput_event_gesture_get_scale(gesture);
70     pointerEvent_->SetActionTime(GetSysClockTime());
71     pointerEvent_->SetActionStartTime(time);
72 
73     PointerEvent::PointerItem pointerItem;
74     pointerEvent_->GetPointerItem(defaultPointerId, pointerItem);
75     pointerItem.SetDisplayX(MouseState->GetMouseCoordsX());
76     pointerItem.SetDisplayY(MouseState->GetMouseCoordsY());
77     pointerItem.SetPressed(MouseState->IsLeftBtnPressed());
78     pointerEvent_->UpdatePointerItem(defaultPointerId, pointerItem);
79 
80     pointerEvent_->ClearButtonPressed();
81     std::vector<int32_t> pressedButtons;
82     MouseState->GetPressedButtons(pressedButtons);
83     for (const auto &item : pressedButtons) {
84         pointerEvent_->SetButtonPressed(item);
85     }
86     pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, scale);
87 }
88 
OnEventTouchPadPinchEnd(libinput_event_gesture * gesture)89 void GestureTransformProcessor::OnEventTouchPadPinchEnd(libinput_event_gesture *gesture)
90 {
91     CALL_DEBUG_ENTER;
92     CHKPV(gesture);
93     int64_t time = static_cast<int64_t>(libinput_event_gesture_get_time(gesture));
94     double scale = libinput_event_gesture_get_scale(gesture);
95     pointerEvent_->SetActionTime(GetSysClockTime());
96     pointerEvent_->SetActionStartTime(time);
97 
98     PointerEvent::PointerItem pointerItem;
99     pointerEvent_->GetPointerItem(defaultPointerId, pointerItem);
100     pointerItem.SetDisplayX(MouseState->GetMouseCoordsX());
101     pointerItem.SetDisplayY(MouseState->GetMouseCoordsY());
102     pointerItem.SetPressed(MouseState->IsLeftBtnPressed());
103     pointerEvent_->UpdatePointerItem(defaultPointerId, pointerItem);
104 
105     pointerEvent_->ClearButtonPressed();
106     std::vector<int32_t> pressedButtons;
107     MouseState->GetPressedButtons(pressedButtons);
108     for (const auto &item : pressedButtons) {
109         pointerEvent_->SetButtonPressed(item);
110     }
111     pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, scale);
112 }
113 
OnEvent(struct libinput_event * event)114 std::shared_ptr<PointerEvent> GestureTransformProcessor::OnEvent(struct libinput_event *event)
115 {
116     CALL_DEBUG_ENTER;
117     CHKPP(event);
118     auto gesture = libinput_event_get_gesture_event(event);
119     CHKPP(gesture);
120     if (pointerEvent_ == nullptr) {
121         pointerEvent_ = PointerEvent::Create();
122         CHKPP(pointerEvent_);
123     }
124     auto type = libinput_event_get_type(event);
125     switch (type) {
126         case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN: {
127             pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_BEGIN);
128             OnEventTouchPadPinchBegin(gesture);
129             break;
130         }
131         case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE: {
132             pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
133             OnEventTouchPadPinchUpdate(gesture);
134             break;
135         }
136         case LIBINPUT_EVENT_GESTURE_PINCH_END: {
137             pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
138             OnEventTouchPadPinchEnd(gesture);
139             break;
140         }
141         case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN:
142         case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE:
143         case LIBINPUT_EVENT_GESTURE_SWIPE_END: {
144             MMI_HILOGW("Three refers to the need to use, preserve the code");
145             return nullptr;
146         }
147         default: {
148             MMI_HILOGE("Unknown event_type of pointer class has been reported!\n");
149             return nullptr;
150         }
151     }
152     pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
153     pointerEvent_->UpdateId();
154     StartLogTraceId(pointerEvent_->GetId(), pointerEvent_->GetEventType(), pointerEvent_->GetPointerAction());
155     return pointerEvent_;
156 }
157 } // namespace MMI
158 } // namespace OHOS