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
31namespace OHOS {
32namespace MMI {
33class 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    };
39public:
40    class CancelCmp {
41    public:
42        bool operator()(const std::shared_ptr<WindowInfo> &windowX, const std::shared_ptr<WindowInfo> &windowY)
43        {
44            return windowX->id < windowY->id;
45        }
46    };
47    EventDispatchHandler() = default;
48    DISALLOW_COPY_AND_MOVE(EventDispatchHandler);
49    ~EventDispatchHandler() override = default;
50#ifdef OHOS_BUILD_ENABLE_KEYBOARD
51    void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
52#endif // OHOS_BUILD_ENABLE_KEYBOARD
53#ifdef OHOS_BUILD_ENABLE_POINTER
54    void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
55#endif // OHOS_BUILD_ENABLE_POINTER
56#ifdef OHOS_BUILD_ENABLE_TOUCH
57    void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
58#endif // OHOS_BUILD_ENABLE_TOUCH
59#ifdef OHOS_BUILD_ENABLE_KEYBOARD
60    int32_t DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key);
61    int32_t DispatchKeyEvent(int32_t fd, UDSServer& udsServer, std::shared_ptr<KeyEvent> key);
62#endif // OHOS_BUILD_ENABLE_KEYBOARD
63#if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
64    void HandlePointerEventInner(const std::shared_ptr<PointerEvent> point);
65    void NotifyPointerEventToRS(int32_t pointAction, const std::string& programName, uint32_t pid, int32_t pointCnt);
66#endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
67    std::chrono::time_point<std::chrono::high_resolution_clock> LasteventBeginTime_ =
68    std::chrono::high_resolution_clock::now();
69    void SendWindowStateError(int32_t pid, int32_t windowId);
70private:
71    void DispatchPointerEventInner(std::shared_ptr<PointerEvent> point, int32_t fd);
72    void HandleMultiWindowPointerEvent(std::shared_ptr<PointerEvent> point,
73        PointerEvent::PointerItem pointerItem);
74    bool ReissueEvent(std::shared_ptr<PointerEvent> &point, int32_t windowId, std::optional<WindowInfo> &windowInfo);
75    std::shared_ptr<WindowInfo> SearchCancelList(int32_t pointerId, int32_t windowId);
76
77    int32_t eventTime_ { 0 };
78    int32_t currentTime_ { 0 };
79    bool enableMark_ { true };
80    struct {
81        int32_t pid { -1 };
82        int32_t windowId { -1 };
83        int64_t startTime { -1 };
84    } windowStateErrorInfo_;
85    std::map<int32_t, std::set<std::shared_ptr<WindowInfo>, CancelCmp>> cancelEventList_;
86#if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
87    void FilterInvalidPointerItem(const std::shared_ptr<PointerEvent> pointEvent, int32_t fd);
88#endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
89    bool AcquireEnableMark(std::shared_ptr<PointerEvent> event);
90};
91} // namespace MMI
92} // namespace OHOS
93#endif // EVENT_DISPATCH_HANDLER_H
94