1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef EVENT_DISPATCH_HANDLER_H
17c29fa5a6Sopenharmony_ci#define EVENT_DISPATCH_HANDLER_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include <mutex>
20c29fa5a6Sopenharmony_ci#include <chrono>
21c29fa5a6Sopenharmony_ci
22c29fa5a6Sopenharmony_ci#include "nocopyable.h"
23c29fa5a6Sopenharmony_ci
24c29fa5a6Sopenharmony_ci#include "i_input_event_handler.h"
25c29fa5a6Sopenharmony_ci#include "key_event.h"
26c29fa5a6Sopenharmony_ci#include "key_event_value_transformation.h"
27c29fa5a6Sopenharmony_ci#include "pointer_event.h"
28c29fa5a6Sopenharmony_ci#include "uds_server.h"
29c29fa5a6Sopenharmony_ci#include "window_info.h"
30c29fa5a6Sopenharmony_ci
31c29fa5a6Sopenharmony_cinamespace OHOS {
32c29fa5a6Sopenharmony_cinamespace MMI {
33c29fa5a6Sopenharmony_ciclass EventDispatchHandler final : public IInputEventHandler {
34c29fa5a6Sopenharmony_ci    struct DinputSimulateEvent {
35c29fa5a6Sopenharmony_ci        uint32_t type { PointerEvent::SOURCE_TYPE_UNKNOWN };
36c29fa5a6Sopenharmony_ci        uint32_t code { PointerEvent::BUTTON_NONE };
37c29fa5a6Sopenharmony_ci        int32_t value { PointerEvent::POINTER_ACTION_UNKNOWN };
38c29fa5a6Sopenharmony_ci    };
39c29fa5a6Sopenharmony_cipublic:
40c29fa5a6Sopenharmony_ci    class CancelCmp {
41c29fa5a6Sopenharmony_ci    public:
42c29fa5a6Sopenharmony_ci        bool operator()(const std::shared_ptr<WindowInfo> &windowX, const std::shared_ptr<WindowInfo> &windowY)
43c29fa5a6Sopenharmony_ci        {
44c29fa5a6Sopenharmony_ci            return windowX->id < windowY->id;
45c29fa5a6Sopenharmony_ci        }
46c29fa5a6Sopenharmony_ci    };
47c29fa5a6Sopenharmony_ci    EventDispatchHandler() = default;
48c29fa5a6Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(EventDispatchHandler);
49c29fa5a6Sopenharmony_ci    ~EventDispatchHandler() override = default;
50c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_KEYBOARD
51c29fa5a6Sopenharmony_ci    void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
52c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_KEYBOARD
53c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_POINTER
54c29fa5a6Sopenharmony_ci    void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
55c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_POINTER
56c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_TOUCH
57c29fa5a6Sopenharmony_ci    void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
58c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_TOUCH
59c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_KEYBOARD
60c29fa5a6Sopenharmony_ci    int32_t DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key);
61c29fa5a6Sopenharmony_ci    int32_t DispatchKeyEvent(int32_t fd, UDSServer& udsServer, std::shared_ptr<KeyEvent> key);
62c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_KEYBOARD
63c29fa5a6Sopenharmony_ci#if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
64c29fa5a6Sopenharmony_ci    void HandlePointerEventInner(const std::shared_ptr<PointerEvent> point);
65c29fa5a6Sopenharmony_ci    void NotifyPointerEventToRS(int32_t pointAction, const std::string& programName, uint32_t pid, int32_t pointCnt);
66c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
67c29fa5a6Sopenharmony_ci    std::chrono::time_point<std::chrono::high_resolution_clock> LasteventBeginTime_ =
68c29fa5a6Sopenharmony_ci    std::chrono::high_resolution_clock::now();
69c29fa5a6Sopenharmony_ci    void SendWindowStateError(int32_t pid, int32_t windowId);
70c29fa5a6Sopenharmony_ciprivate:
71c29fa5a6Sopenharmony_ci    void DispatchPointerEventInner(std::shared_ptr<PointerEvent> point, int32_t fd);
72c29fa5a6Sopenharmony_ci    void HandleMultiWindowPointerEvent(std::shared_ptr<PointerEvent> point,
73c29fa5a6Sopenharmony_ci        PointerEvent::PointerItem pointerItem);
74c29fa5a6Sopenharmony_ci    bool ReissueEvent(std::shared_ptr<PointerEvent> &point, int32_t windowId, std::optional<WindowInfo> &windowInfo);
75c29fa5a6Sopenharmony_ci    std::shared_ptr<WindowInfo> SearchCancelList(int32_t pointerId, int32_t windowId);
76c29fa5a6Sopenharmony_ci
77c29fa5a6Sopenharmony_ci    int32_t eventTime_ { 0 };
78c29fa5a6Sopenharmony_ci    int32_t currentTime_ { 0 };
79c29fa5a6Sopenharmony_ci    bool enableMark_ { true };
80c29fa5a6Sopenharmony_ci    struct {
81c29fa5a6Sopenharmony_ci        int32_t pid { -1 };
82c29fa5a6Sopenharmony_ci        int32_t windowId { -1 };
83c29fa5a6Sopenharmony_ci        int64_t startTime { -1 };
84c29fa5a6Sopenharmony_ci    } windowStateErrorInfo_;
85c29fa5a6Sopenharmony_ci    std::map<int32_t, std::set<std::shared_ptr<WindowInfo>, CancelCmp>> cancelEventList_;
86c29fa5a6Sopenharmony_ci#if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
87c29fa5a6Sopenharmony_ci    void FilterInvalidPointerItem(const std::shared_ptr<PointerEvent> pointEvent, int32_t fd);
88c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
89c29fa5a6Sopenharmony_ci    bool AcquireEnableMark(std::shared_ptr<PointerEvent> event);
90c29fa5a6Sopenharmony_ci};
91c29fa5a6Sopenharmony_ci} // namespace MMI
92c29fa5a6Sopenharmony_ci} // namespace OHOS
93c29fa5a6Sopenharmony_ci#endif // EVENT_DISPATCH_HANDLER_H
94