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