1885b47fbSopenharmony_ci/*
2885b47fbSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3885b47fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4885b47fbSopenharmony_ci * you may not use this file except in compliance with the License.
5885b47fbSopenharmony_ci * You may obtain a copy of the License at
6885b47fbSopenharmony_ci *
7885b47fbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8885b47fbSopenharmony_ci *
9885b47fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10885b47fbSopenharmony_ci * distributed under the License is distributed On an "AS IS" BASIS,
11885b47fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12885b47fbSopenharmony_ci * See the License for the specific language governing permissions and
13885b47fbSopenharmony_ci * limitations under the License.
14885b47fbSopenharmony_ci */
15885b47fbSopenharmony_ci
16885b47fbSopenharmony_ci
17885b47fbSopenharmony_ci#ifndef ACCESSIBILITY_TOUCHEVENT_INJECTOR_H
18885b47fbSopenharmony_ci#define ACCESSIBILITY_TOUCHEVENT_INJECTOR_H
19885b47fbSopenharmony_ci
20885b47fbSopenharmony_ci#include <time.h>
21885b47fbSopenharmony_ci#include "accessibility_event_transmission.h"
22885b47fbSopenharmony_ci#include "event_handler.h"
23885b47fbSopenharmony_ci#include "event_runner.h"
24885b47fbSopenharmony_ci#include "accessibility_gesture_inject_path.h"
25885b47fbSopenharmony_ci#include "pointer_event.h"
26885b47fbSopenharmony_ci#include "singleton.h"
27885b47fbSopenharmony_ci
28885b47fbSopenharmony_cinamespace OHOS {
29885b47fbSopenharmony_cinamespace Accessibility {
30885b47fbSopenharmony_ciconst int64_t DOUBLE_TAP_MIN_TIME = 50000; // microsecond
31885b47fbSopenharmony_ci
32885b47fbSopenharmony_cistruct SendEventArgs {
33885b47fbSopenharmony_ci    std::shared_ptr<MMI::PointerEvent> event_;
34885b47fbSopenharmony_ci};
35885b47fbSopenharmony_ci
36885b47fbSopenharmony_ciclass TouchEventInjector;
37885b47fbSopenharmony_ciclass TouchInjectHandler : public AppExecFwk::EventHandler {
38885b47fbSopenharmony_cipublic:
39885b47fbSopenharmony_ci    TouchInjectHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, TouchEventInjector &server);
40885b47fbSopenharmony_ci    virtual ~TouchInjectHandler() = default;
41885b47fbSopenharmony_ci    /**
42885b47fbSopenharmony_ci     * @brief Process the event of install system bundles.
43885b47fbSopenharmony_ci     * @param event Indicates the event to be processed.
44885b47fbSopenharmony_ci     */
45885b47fbSopenharmony_ci    virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
46885b47fbSopenharmony_ciprivate:
47885b47fbSopenharmony_ci    TouchEventInjector &server_;
48885b47fbSopenharmony_ci};
49885b47fbSopenharmony_ci
50885b47fbSopenharmony_ciclass TouchEventInjector : public EventTransmission, public AppExecFwk::EventHandler {
51885b47fbSopenharmony_cipublic:
52885b47fbSopenharmony_ci    static constexpr uint32_t SEND_TOUCH_EVENT_MSG = 1;
53885b47fbSopenharmony_ci
54885b47fbSopenharmony_ci    /**
55885b47fbSopenharmony_ci     * @brief A constructor used to create a TouchEventInjector instance.
56885b47fbSopenharmony_ci     */
57885b47fbSopenharmony_ci    TouchEventInjector();
58885b47fbSopenharmony_ci
59885b47fbSopenharmony_ci    /**
60885b47fbSopenharmony_ci     * @brief A destructor used to delete the TouchEventInjector instance.
61885b47fbSopenharmony_ci     */
62885b47fbSopenharmony_ci    ~TouchEventInjector() {}
63885b47fbSopenharmony_ci
64885b47fbSopenharmony_ci    /**
65885b47fbSopenharmony_ci     * @brief Handle pointer events from previous event stream node.
66885b47fbSopenharmony_ci     * @param event the pointer event from Multimodal
67885b47fbSopenharmony_ci     * @return true: the event has been processed and does not need to be passed to the next node;
68885b47fbSopenharmony_ci     *         false: the event is not processed.
69885b47fbSopenharmony_ci     */
70885b47fbSopenharmony_ci    bool OnPointerEvent(MMI::PointerEvent &event) override;
71885b47fbSopenharmony_ci
72885b47fbSopenharmony_ci    /**
73885b47fbSopenharmony_ci     * @brief Destroy event state.
74885b47fbSopenharmony_ci     */
75885b47fbSopenharmony_ci    void DestroyEvents() override;
76885b47fbSopenharmony_ci
77885b47fbSopenharmony_ci    /**
78885b47fbSopenharmony_ci     * @brief Send pointer event to next stream node.
79885b47fbSopenharmony_ci     * @param event the touch event prepared to send
80885b47fbSopenharmony_ci     */
81885b47fbSopenharmony_ci    void SendPointerEvent(MMI::PointerEvent &event);
82885b47fbSopenharmony_ci
83885b47fbSopenharmony_ci    /**
84885b47fbSopenharmony_ci     * @brief Inject simulated gestures.
85885b47fbSopenharmony_ci     * @param gesturePath the gesture path
86885b47fbSopenharmony_ci     */
87885b47fbSopenharmony_ci    void InjectEvents(const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
88885b47fbSopenharmony_ci
89885b47fbSopenharmony_ciprivate:
90885b47fbSopenharmony_ci    /**
91885b47fbSopenharmony_ci     * @brief Cancel the gesture.
92885b47fbSopenharmony_ci     */
93885b47fbSopenharmony_ci    void CancelGesture();
94885b47fbSopenharmony_ci
95885b47fbSopenharmony_ci    /**
96885b47fbSopenharmony_ci     * @brief Cancel the injected events.
97885b47fbSopenharmony_ci     */
98885b47fbSopenharmony_ci    void CancelInjectedEvents();
99885b47fbSopenharmony_ci
100885b47fbSopenharmony_ci    /**
101885b47fbSopenharmony_ci     * @brief create touchevent.
102885b47fbSopenharmony_ci     * @param action the action of event
103885b47fbSopenharmony_ci     * @param point the endpoint of event
104885b47fbSopenharmony_ci     * @return the created touchevent
105885b47fbSopenharmony_ci     */
106885b47fbSopenharmony_ci    std::shared_ptr<MMI::PointerEvent> obtainTouchEvent(int32_t action,
107885b47fbSopenharmony_ci        MMI::PointerEvent::PointerItem point, int64_t actionTime);
108885b47fbSopenharmony_ci
109885b47fbSopenharmony_ci    /**
110885b47fbSopenharmony_ci     * @brief Get the number of microseconds elapsed since the system was booted.
111885b47fbSopenharmony_ci     * @return the number of microseconds elapsed since the system was booted
112885b47fbSopenharmony_ci     */
113885b47fbSopenharmony_ci    int64_t GetSystemTime();
114885b47fbSopenharmony_ci
115885b47fbSopenharmony_ci    /**
116885b47fbSopenharmony_ci     * @brief Parse taps events.
117885b47fbSopenharmony_ci     * @param startTime the start time of gesture injection
118885b47fbSopenharmony_ci     * @param gesturePath the gesture path
119885b47fbSopenharmony_ci     */
120885b47fbSopenharmony_ci    void ParseTapsEvents(int64_t startTime, const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
121885b47fbSopenharmony_ci
122885b47fbSopenharmony_ci    /**
123885b47fbSopenharmony_ci     * @brief Parse move events.
124885b47fbSopenharmony_ci     * @param startTime the start time of gesture injection
125885b47fbSopenharmony_ci     * @param gesturePath the gesture path
126885b47fbSopenharmony_ci     */
127885b47fbSopenharmony_ci    void ParseMovesEvents(int64_t startTime, const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
128885b47fbSopenharmony_ci
129885b47fbSopenharmony_ci    /**
130885b47fbSopenharmony_ci     * @brief Parse touchevents from gesturepath.
131885b47fbSopenharmony_ci     * @param startTime the start time of gesture injection
132885b47fbSopenharmony_ci     * @param gesturePath the gesture path
133885b47fbSopenharmony_ci     */
134885b47fbSopenharmony_ci    void ParseTouchEventsFromGesturePath(int64_t startTime,
135885b47fbSopenharmony_ci        const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
136885b47fbSopenharmony_ci
137885b47fbSopenharmony_ci    bool isGestureUnderway_ = false;
138885b47fbSopenharmony_ci    bool isDestroyEvent_ = false;
139885b47fbSopenharmony_ci    std::shared_ptr<TouchInjectHandler> handler_ = nullptr;
140885b47fbSopenharmony_ci    std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
141885b47fbSopenharmony_ci    std::vector<std::shared_ptr<MMI::PointerEvent>> injectedEvents_;
142885b47fbSopenharmony_ci};
143885b47fbSopenharmony_ci} // namespace Accessibility
144885b47fbSopenharmony_ci} // namespace OHOS
145885b47fbSopenharmony_ci#endif // ACCESSIBILITY_TOUCHEVENT_INJECTOR_H