1 /*
2  * Copyright (c) 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 #ifndef EVENT_NORMALIZE_HANDLER_H
17 #define EVENT_NORMALIZE_HANDLER_H
18 
19 #include <memory>
20 
21 #include "i_input_event_handler.h"
22 #include "key_event_normalize.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 class EventNormalizeHandler : public IInputEventHandler {
27 public:
28     EventNormalizeHandler() = default;
29     ~EventNormalizeHandler() = default;
30     void HandleEvent(libinput_event* event, int64_t frameTime);
31 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
32     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
33 #endif // OHOS_BUILD_ENABLE_KEYBOARD
34 #ifdef OHOS_BUILD_ENABLE_POINTER
35     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
36 #endif // OHOS_BUILD_ENABLE_POINTER
37 #ifdef OHOS_BUILD_ENABLE_TOUCH
38     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
39 #endif // OHOS_BUILD_ENABLE_TOUCH
40     int32_t AddHandleTimer(int32_t timeout = 300);
41 #ifdef OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
42     int32_t SetMoveEventFilters(bool flag);
43 #endif // OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
44 
45 private:
46     int32_t OnEventDeviceAdded(libinput_event *event);
47     int32_t OnEventDeviceRemoved(libinput_event *event);
48     int32_t HandleKeyboardEvent(libinput_event* event);
49     void Repeat(const std::shared_ptr<KeyEvent> keyEvent);
50     int32_t HandleTouchPadEvent(libinput_event* event);
51     bool HandleTouchPadTripleTapEvent(std::shared_ptr<PointerEvent> pointerEvent);
52     int32_t HandleGestureEvent(libinput_event* event);
53     int32_t HandleMouseEvent(libinput_event* event);
54     int32_t HandleTouchEvent(libinput_event* event, int64_t frameTime);
55     int32_t HandleSwitchInputEvent(libinput_event* event);
56     int32_t HandleTableToolEvent(libinput_event* event);
57     int32_t HandleJoystickEvent(libinput_event* event);
58     void HandlePalmEvent(libinput_event* event, std::shared_ptr<PointerEvent> pointerEvent);
59     bool JudgeIfSwipeInward(std::shared_ptr<PointerEvent> pointerEvent,
60         enum libinput_event_type type, libinput_event* event);
61     void SwipeInwardProcess(std::shared_ptr<PointerEvent> pointerEvent,
62         enum libinput_event_type type, libinput_event* event, int32_t* angleTolerance, int32_t lastDirection);
63 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
64     void UpdateKeyEventHandlerChain(const std::shared_ptr<KeyEvent> keyEvent);
65 #endif // OHOS_BUILD_ENABLE_KEYBOARD
66 #ifdef OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
67     bool HandleTouchEventWithFlag(const std::shared_ptr<PointerEvent> pointerEvent);
68     double CalcTouchOffset(const std::shared_ptr<PointerEvent> touchMoveEvent);
69 #endif // OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
70     int32_t SetOriginPointerId(std::shared_ptr<PointerEvent> pointerEvent);
71     void PointerEventSetPressedKeys(std::shared_ptr<PointerEvent> pointerEvent);
72     bool TouchPadKnuckleDoubleClickHandle(libinput_event* event);
73 
74 private:
75     int32_t timerId_ { -1 };
76     bool isShield_ { false };
77     std::set<int32_t> buttonIds_ {};
78 #ifdef OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
79     bool moveEventFilterFlag_ { false };
80     std::list<PointerEvent::PointerItem> lastTouchDownItems_;
81 #endif // OHOS_BUILD_ENABLE_MOVE_EVENT_FILTERS
82     void ResetTouchUpEvent(std::shared_ptr<PointerEvent> pointerEvent, struct libinput_event *event);
83     bool ProcessNullEvent(libinput_event *event, int64_t frameTime);
84 #ifdef OHOS_BUILD_ENABLE_SWITCH
85     void RestoreTouchPadStatus();
86 #endif // OHOS_BUILD_ENABLE_SWITCH
87     void TerminateRotate(libinput_event* event);
88     void TerminateAxis(libinput_event* event);
89     void CancelTwoFingerAxis(libinput_event* event);
90 };
91 } // namespace MMI
92 } // namespace OHOS
93 #endif // EVENT_NORMALIZE_HANDLER_H