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 #ifndef EVENT_DISPATCH_HANDLER_H
17 #define EVENT_DISPATCH_HANDLER_H
18 
19 #include <mutex>
20 #include <chrono>
21 
22 #include "nocopyable.h"
23 
24 #include "i_input_event_handler.h"
25 #include "key_event.h"
26 #include "key_event_value_transformation.h"
27 #include "pointer_event.h"
28 #include "uds_server.h"
29 #include "window_info.h"
30 
31 namespace OHOS {
32 namespace MMI {
33 class EventDispatchHandler final : public IInputEventHandler {
34     struct DinputSimulateEvent {
35         uint32_t type { PointerEvent::SOURCE_TYPE_UNKNOWN };
36         uint32_t code { PointerEvent::BUTTON_NONE };
37         int32_t value { PointerEvent::POINTER_ACTION_UNKNOWN };
38     };
39 public:
40     EventDispatchHandler() = default;
41     DISALLOW_COPY_AND_MOVE(EventDispatchHandler);
42     ~EventDispatchHandler() override = default;
43 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
44     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
45 #endif // OHOS_BUILD_ENABLE_KEYBOARD
46 #ifdef OHOS_BUILD_ENABLE_POINTER
47     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
48 #endif // OHOS_BUILD_ENABLE_POINTER
49 #ifdef OHOS_BUILD_ENABLE_TOUCH
50     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
51 #endif // OHOS_BUILD_ENABLE_TOUCH
52 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
53     int32_t DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key);
54     int32_t DispatchKeyEvent(int32_t fd, UDSServer& udsServer, std::shared_ptr<KeyEvent> key);
55 #endif // OHOS_BUILD_ENABLE_KEYBOARD
56 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
57     void HandlePointerEventInner(const std::shared_ptr<PointerEvent> point);
58     void NotifyPointerEventToRS(int32_t pointAction, const std::string& programName, uint32_t pid, int32_t pointCnt);
59 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
60     std::chrono::time_point<std::chrono::high_resolution_clock> LasteventBeginTime_ =
61     std::chrono::high_resolution_clock::now();
62     void SendWindowStateError(int32_t pid, int32_t windowId);
63 private:
64     void DispatchPointerEventInner(std::shared_ptr<PointerEvent> point, int32_t fd);
65     void HandleMultiWindowPointerEvent(std::shared_ptr<PointerEvent> point,
66         PointerEvent::PointerItem pointerItem);
67     bool ReissueEvent(std::shared_ptr<PointerEvent> &point, int32_t windowId, std::optional<WindowInfo> &windowInfo);
68     std::shared_ptr<WindowInfo> SearchCancelList(int32_t pointerId, int32_t windowId);
69     bool SearchWindow(std::vector<std::shared_ptr<WindowInfo>> &windowList, std::shared_ptr<WindowInfo> targetWindow);
70     int32_t GetClientFd(int32_t pid, std::shared_ptr<PointerEvent> point);
71 
72     int32_t eventTime_ { 0 };
73     int32_t currentTime_ { 0 };
74     bool enableMark_ { true };
75     std::map<int32_t, std::vector<std::shared_ptr<WindowInfo>>> cancelEventList_;
76     struct {
77         int32_t pid { -1 };
78         int32_t windowId { -1 };
79         int64_t startTime { -1 };
80     } windowStateErrorInfo_;
81 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
82     void FilterInvalidPointerItem(const std::shared_ptr<PointerEvent> pointEvent, int32_t fd);
83 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
84     bool AcquireEnableMark(std::shared_ptr<PointerEvent> event);
85 };
86 } // namespace MMI
87 } // namespace OHOS
88 #endif // EVENT_DISPATCH_HANDLER_H
89