123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "adapter/preview/entrance/event_dispatcher.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include <map>
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_ci#include "adapter/preview/entrance/ace_container.h"
2123b3eb3cSopenharmony_ci#include "adapter/preview/entrance/ace_view_preview.h"
2223b3eb3cSopenharmony_ci#include "adapter/preview/entrance/editing/text_input_client_mgr.h"
2323b3eb3cSopenharmony_ci#include "base/log/ace_trace.h"
2423b3eb3cSopenharmony_ci#include "base/log/log.h"
2523b3eb3cSopenharmony_ci#include "core/common/container_scope.h"
2623b3eb3cSopenharmony_ci#include "core/event/key_event.h"
2723b3eb3cSopenharmony_ci#include "core/event/touch_event.h"
2823b3eb3cSopenharmony_ci
2923b3eb3cSopenharmony_cinamespace OHOS::Ace::Platform {
3023b3eb3cSopenharmony_cinamespace {
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_ciconst wchar_t UPPER_CASE_A = L'A';
3323b3eb3cSopenharmony_ciconst wchar_t LOWER_CASE_A = L'a';
3423b3eb3cSopenharmony_ciconst wchar_t CASE_0 = L'0';
3523b3eb3cSopenharmony_ciconst std::wstring NUM_SYMBOLS = L")!@#$%^&*(";
3623b3eb3cSopenharmony_ciconst std::map<MMI::KeyCode, wchar_t> PRINTABEL_SYMBOLS = {
3723b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_GRAVE, L'`' },
3823b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_MINUS, L'-' },
3923b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_EQUALS, L'=' },
4023b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_LEFT_BRACKET, L'[' },
4123b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_RIGHT_BRACKET, L']' },
4223b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_BACKSLASH, L'\\' },
4323b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_SEMICOLON, L';' },
4423b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_APOSTROPHE, L'\'' },
4523b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_COMMA, L',' },
4623b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_PERIOD, L'.' },
4723b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_SLASH, L'/' },
4823b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_SPACE, L' ' },
4923b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_NUMPAD_DIVIDE, L'/' },
5023b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_NUMPAD_MULTIPLY, L'*' },
5123b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_NUMPAD_SUBTRACT, L'-' },
5223b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_NUMPAD_ADD, L'+' },
5323b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_NUMPAD_DOT, L'.' },
5423b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_NUMPAD_COMMA, L',' },
5523b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_NUMPAD_EQUALS, L'=' },
5623b3eb3cSopenharmony_ci};
5723b3eb3cSopenharmony_ci
5823b3eb3cSopenharmony_ciconst std::map<MMI::KeyCode, wchar_t> SHIFT_PRINTABEL_SYMBOLS = {
5923b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_GRAVE, L'~' },
6023b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_MINUS, L'_' },
6123b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_EQUALS, L'+' },
6223b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_LEFT_BRACKET, L'{' },
6323b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_RIGHT_BRACKET, L'}' },
6423b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_BACKSLASH, L'|' },
6523b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_SEMICOLON, L':' },
6623b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_APOSTROPHE, L'\"' },
6723b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_COMMA, L'<' },
6823b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_PERIOD, L'>' },
6923b3eb3cSopenharmony_ci    { MMI::KeyCode::KEY_SLASH, L'?' },
7023b3eb3cSopenharmony_ci};
7123b3eb3cSopenharmony_ci
7223b3eb3cSopenharmony_civoid ConvertTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, TouchEvent& event)
7323b3eb3cSopenharmony_ci{
7423b3eb3cSopenharmony_ci    event.id = pointerEvent->id;
7523b3eb3cSopenharmony_ci    event.x = pointerEvent->x;
7623b3eb3cSopenharmony_ci    event.y = pointerEvent->y;
7723b3eb3cSopenharmony_ci    event.screenX = pointerEvent->screenX;
7823b3eb3cSopenharmony_ci    event.screenY = pointerEvent->screenY;
7923b3eb3cSopenharmony_ci    event.type = static_cast<TouchType>(static_cast<size_t>(pointerEvent->type));
8023b3eb3cSopenharmony_ci    event.pullType = static_cast<TouchType>(static_cast<size_t>(pointerEvent->pullType));
8123b3eb3cSopenharmony_ci    event.time = pointerEvent->time;
8223b3eb3cSopenharmony_ci    event.size = pointerEvent->size;
8323b3eb3cSopenharmony_ci    event.force = pointerEvent->force;
8423b3eb3cSopenharmony_ci    event.tiltX = pointerEvent->tiltX;
8523b3eb3cSopenharmony_ci    event.tiltY = pointerEvent->tiltY;
8623b3eb3cSopenharmony_ci    event.deviceId = pointerEvent->deviceId;
8723b3eb3cSopenharmony_ci    event.sourceType = static_cast<SourceType>(static_cast<int32_t>(pointerEvent->sourceType));
8823b3eb3cSopenharmony_ci    event.sourceTool = static_cast<SourceTool>(static_cast<int32_t>(pointerEvent->sourceTool));
8923b3eb3cSopenharmony_ci    event.pointerEvent = pointerEvent;
9023b3eb3cSopenharmony_ci    for (auto& item : pointerEvent->pointers) {
9123b3eb3cSopenharmony_ci        TouchPoint pointer;
9223b3eb3cSopenharmony_ci        pointer.id = item.id;
9323b3eb3cSopenharmony_ci        pointer.x = item.x;
9423b3eb3cSopenharmony_ci        pointer.y = item.y;
9523b3eb3cSopenharmony_ci        pointer.screenX = item.screenX;
9623b3eb3cSopenharmony_ci        pointer.screenY = item.screenY;
9723b3eb3cSopenharmony_ci        pointer.downTime = item.downTime;
9823b3eb3cSopenharmony_ci        pointer.size = item.size;
9923b3eb3cSopenharmony_ci        pointer.force = item.force;
10023b3eb3cSopenharmony_ci        pointer.tiltX = item.tiltX;
10123b3eb3cSopenharmony_ci        pointer.tiltY = item.tiltY;
10223b3eb3cSopenharmony_ci        pointer.sourceTool = static_cast<SourceTool>(static_cast<int32_t>(item.sourceTool));
10323b3eb3cSopenharmony_ci        pointer.isPressed = item.isPressed;
10423b3eb3cSopenharmony_ci        event.pointers.emplace_back(pointer);
10523b3eb3cSopenharmony_ci    }
10623b3eb3cSopenharmony_ci}
10723b3eb3cSopenharmony_ci
10823b3eb3cSopenharmony_civoid ConvertKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent, KeyEvent& event)
10923b3eb3cSopenharmony_ci{
11023b3eb3cSopenharmony_ci    event.code = static_cast<KeyCode>(static_cast<int32_t>(keyEvent->code));
11123b3eb3cSopenharmony_ci    event.key = keyEvent->key;
11223b3eb3cSopenharmony_ci    event.action = static_cast<KeyAction>(static_cast<int32_t>(keyEvent->action));
11323b3eb3cSopenharmony_ci    for (auto& item : keyEvent->pressedCodes) {
11423b3eb3cSopenharmony_ci        event.pressedCodes.push_back(static_cast<KeyCode>(static_cast<int32_t>(item)));
11523b3eb3cSopenharmony_ci    }
11623b3eb3cSopenharmony_ci    event.repeatTime = keyEvent->repeatTime;
11723b3eb3cSopenharmony_ci    event.timeStamp = keyEvent->timeStamp;
11823b3eb3cSopenharmony_ci    event.metaKey = keyEvent->metaKey;
11923b3eb3cSopenharmony_ci    event.deviceId = keyEvent->deviceId;
12023b3eb3cSopenharmony_ci    event.sourceType = static_cast<SourceType>(static_cast<int32_t>(keyEvent->sourceType));
12123b3eb3cSopenharmony_ci    event.rawKeyEvent = keyEvent;
12223b3eb3cSopenharmony_ci    event.enableCapsLock = keyEvent->enableCapsLock_;
12323b3eb3cSopenharmony_ci}
12423b3eb3cSopenharmony_ci
12523b3eb3cSopenharmony_ci} // namespace
12623b3eb3cSopenharmony_ci
12723b3eb3cSopenharmony_ciOffset GetTouchEventOriginOffset(const TouchEvent& event)
12823b3eb3cSopenharmony_ci{
12923b3eb3cSopenharmony_ci    if (event.pointerEvent) {
13023b3eb3cSopenharmony_ci        for (auto& item : event.pointerEvent->pointers) {
13123b3eb3cSopenharmony_ci            return Offset(item.x, item.y);
13223b3eb3cSopenharmony_ci        }
13323b3eb3cSopenharmony_ci    }
13423b3eb3cSopenharmony_ci    return Offset();
13523b3eb3cSopenharmony_ci}
13623b3eb3cSopenharmony_ci
13723b3eb3cSopenharmony_ciTimeStamp GetTouchEventOriginTimeStamp(const TouchEvent& event)
13823b3eb3cSopenharmony_ci{
13923b3eb3cSopenharmony_ci    if (event.pointerEvent) {
14023b3eb3cSopenharmony_ci        return event.pointerEvent->time;
14123b3eb3cSopenharmony_ci    }
14223b3eb3cSopenharmony_ci    return event.time;
14323b3eb3cSopenharmony_ci}
14423b3eb3cSopenharmony_ci
14523b3eb3cSopenharmony_civoid UpdatePressedKeyCodes(std::vector<KeyCode>& pressedKeyCodes) {}
14623b3eb3cSopenharmony_ci
14723b3eb3cSopenharmony_ciEventDispatcher::EventDispatcher() {}
14823b3eb3cSopenharmony_ci
14923b3eb3cSopenharmony_ciEventDispatcher::~EventDispatcher() = default;
15023b3eb3cSopenharmony_ci
15123b3eb3cSopenharmony_civoid EventDispatcher::Initialize()
15223b3eb3cSopenharmony_ci{
15323b3eb3cSopenharmony_ci    LOGI("Initialize event dispatcher");
15423b3eb3cSopenharmony_ci    // Initial the proxy of Input method
15523b3eb3cSopenharmony_ci    TextInputClientMgr::GetInstance().InitTextInputProxy();
15623b3eb3cSopenharmony_ci    // Register the idle event callback function.
15723b3eb3cSopenharmony_ci#ifndef ENABLE_ROSEN_BACKEND
15823b3eb3cSopenharmony_ci    IdleCallback idleNoticeCallback = [](int64_t deadline) {
15923b3eb3cSopenharmony_ci        EventDispatcher::GetInstance().DispatchIdleEvent(deadline);
16023b3eb3cSopenharmony_ci    };
16123b3eb3cSopenharmony_ci    FlutterDesktopSetIdleCallback(controller_, idleNoticeCallback);
16223b3eb3cSopenharmony_ci#else
16323b3eb3cSopenharmony_ci    // rosen process idle
16423b3eb3cSopenharmony_ci#endif
16523b3eb3cSopenharmony_ci}
16623b3eb3cSopenharmony_ci
16723b3eb3cSopenharmony_civoid EventDispatcher::DispatchIdleEvent(int64_t deadline)
16823b3eb3cSopenharmony_ci{
16923b3eb3cSopenharmony_ci    ACE_SCOPED_TRACE("DispatchIdleEvent");
17023b3eb3cSopenharmony_ci    auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
17123b3eb3cSopenharmony_ci    if (!container) {
17223b3eb3cSopenharmony_ci        return;
17323b3eb3cSopenharmony_ci    }
17423b3eb3cSopenharmony_ci
17523b3eb3cSopenharmony_ci    auto aceView = AceType::DynamicCast<AceViewPreview>(container->GetAceView());
17623b3eb3cSopenharmony_ci    if (!aceView) {
17723b3eb3cSopenharmony_ci        return;
17823b3eb3cSopenharmony_ci    }
17923b3eb3cSopenharmony_ci
18023b3eb3cSopenharmony_ci    aceView->ProcessIdleEvent(deadline);
18123b3eb3cSopenharmony_ci}
18223b3eb3cSopenharmony_ci
18323b3eb3cSopenharmony_cistatic void GetMouseEventAction(int32_t action, OHOS::Ace::MouseEvent& mouseEvent)
18423b3eb3cSopenharmony_ci{
18523b3eb3cSopenharmony_ci    switch (action) {
18623b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN:
18723b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::PRESS;
18823b3eb3cSopenharmony_ci            break;
18923b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
19023b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::RELEASE;
19123b3eb3cSopenharmony_ci            break;
19223b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW:
19323b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::WINDOW_ENTER;
19423b3eb3cSopenharmony_ci            break;
19523b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW:
19623b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::WINDOW_LEAVE;
19723b3eb3cSopenharmony_ci            break;
19823b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_MOVE:
19923b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::MOVE;
20023b3eb3cSopenharmony_ci            break;
20123b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_DOWN:
20223b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::PRESS;
20323b3eb3cSopenharmony_ci            break;
20423b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_MOVE:
20523b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::MOVE;
20623b3eb3cSopenharmony_ci            break;
20723b3eb3cSopenharmony_ci        case OHOS::MMI::PointerEvent::POINTER_ACTION_PULL_UP:
20823b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::RELEASE;
20923b3eb3cSopenharmony_ci            break;
21023b3eb3cSopenharmony_ci        default:
21123b3eb3cSopenharmony_ci            mouseEvent.action = MouseAction::NONE;
21223b3eb3cSopenharmony_ci            break;
21323b3eb3cSopenharmony_ci    }
21423b3eb3cSopenharmony_ci}
21523b3eb3cSopenharmony_ci
21623b3eb3cSopenharmony_cistatic void GetMouseEventButton(int32_t button, Ace::MouseEvent& mouseEvent)
21723b3eb3cSopenharmony_ci{
21823b3eb3cSopenharmony_ci    switch (button) {
21923b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_LEFT:
22023b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::LEFT_BUTTON;
22123b3eb3cSopenharmony_ci            break;
22223b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_RIGHT:
22323b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::RIGHT_BUTTON;
22423b3eb3cSopenharmony_ci            break;
22523b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_MIDDLE:
22623b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::MIDDLE_BUTTON;
22723b3eb3cSopenharmony_ci            break;
22823b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_SIDE:
22923b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::BACK_BUTTON;
23023b3eb3cSopenharmony_ci            break;
23123b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_EXTRA:
23223b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::SIDE_BUTTON;
23323b3eb3cSopenharmony_ci            break;
23423b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_FORWARD:
23523b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::FORWARD_BUTTON;
23623b3eb3cSopenharmony_ci            break;
23723b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_BACK:
23823b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::BACK_BUTTON;
23923b3eb3cSopenharmony_ci            break;
24023b3eb3cSopenharmony_ci        case MMI::PointerEvent::MOUSE_BUTTON_TASK:
24123b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::TASK_BUTTON;
24223b3eb3cSopenharmony_ci            break;
24323b3eb3cSopenharmony_ci        default:
24423b3eb3cSopenharmony_ci            mouseEvent.button = MouseButton::NONE_BUTTON;
24523b3eb3cSopenharmony_ci            break;
24623b3eb3cSopenharmony_ci    }
24723b3eb3cSopenharmony_ci}
24823b3eb3cSopenharmony_ci
24923b3eb3cSopenharmony_cistatic void ConvertMouseEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, Ace::MouseEvent& mouseEvent)
25023b3eb3cSopenharmony_ci{
25123b3eb3cSopenharmony_ci    mouseEvent.id = pointerEvent->id;
25223b3eb3cSopenharmony_ci    mouseEvent.x = pointerEvent->x;
25323b3eb3cSopenharmony_ci    mouseEvent.y = pointerEvent->y;
25423b3eb3cSopenharmony_ci    mouseEvent.screenX = pointerEvent->screenX;
25523b3eb3cSopenharmony_ci    mouseEvent.screenY = pointerEvent->screenY;
25623b3eb3cSopenharmony_ci    GetMouseEventAction(pointerEvent->pointerAction_, mouseEvent);
25723b3eb3cSopenharmony_ci    GetMouseEventButton(pointerEvent->buttonId_, mouseEvent);
25823b3eb3cSopenharmony_ci    mouseEvent.sourceType = SourceType::MOUSE;
25923b3eb3cSopenharmony_ci    mouseEvent.targetDisplayId = pointerEvent->targetDisplayId_;
26023b3eb3cSopenharmony_ci    mouseEvent.deviceId = pointerEvent->deviceId;
26123b3eb3cSopenharmony_ci    std::set<int32_t> buttonSet = pointerEvent->pressedButtons_;
26223b3eb3cSopenharmony_ci    if (pointerEvent->pressedButtons_.empty()) {
26323b3eb3cSopenharmony_ci        pointerEvent->pressedButtons_.insert(pointerEvent->buttonId_);
26423b3eb3cSopenharmony_ci    }
26523b3eb3cSopenharmony_ci    uint32_t buttons = 0;
26623b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_LEFT)) {
26723b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::LEFT_BUTTON);
26823b3eb3cSopenharmony_ci    }
26923b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_RIGHT)) {
27023b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::RIGHT_BUTTON);
27123b3eb3cSopenharmony_ci    }
27223b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_MIDDLE)) {
27323b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::MIDDLE_BUTTON);
27423b3eb3cSopenharmony_ci    }
27523b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_SIDE)) {
27623b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::SIDE_BUTTON);
27723b3eb3cSopenharmony_ci    }
27823b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_EXTRA)) {
27923b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::EXTRA_BUTTON);
28023b3eb3cSopenharmony_ci    }
28123b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_FORWARD)) {
28223b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::FORWARD_BUTTON);
28323b3eb3cSopenharmony_ci    }
28423b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_BACK)) {
28523b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::BACK_BUTTON);
28623b3eb3cSopenharmony_ci    }
28723b3eb3cSopenharmony_ci    if (buttonSet.end() != buttonSet.find(MMI::PointerEvent::MOUSE_BUTTON_TASK)) {
28823b3eb3cSopenharmony_ci        buttons &= static_cast<uint32_t>(MouseButton::TASK_BUTTON);
28923b3eb3cSopenharmony_ci    }
29023b3eb3cSopenharmony_ci    mouseEvent.pressedButtons = static_cast<int32_t>(buttons);
29123b3eb3cSopenharmony_ci}
29223b3eb3cSopenharmony_ci
29323b3eb3cSopenharmony_cistatic void GetAxisEventAction(int32_t action, Ace::AxisEvent& event)
29423b3eb3cSopenharmony_ci{
29523b3eb3cSopenharmony_ci    switch (action) {
29623b3eb3cSopenharmony_ci        case MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN:
29723b3eb3cSopenharmony_ci            event.action = Ace::AxisAction::BEGIN;
29823b3eb3cSopenharmony_ci            break;
29923b3eb3cSopenharmony_ci        case MMI::PointerEvent::POINTER_ACTION_AXIS_UPDATE:
30023b3eb3cSopenharmony_ci            event.action = Ace::AxisAction::UPDATE;
30123b3eb3cSopenharmony_ci            break;
30223b3eb3cSopenharmony_ci        case MMI::PointerEvent::POINTER_ACTION_AXIS_END:
30323b3eb3cSopenharmony_ci            event.action = Ace::AxisAction::END;
30423b3eb3cSopenharmony_ci            break;
30523b3eb3cSopenharmony_ci        default:
30623b3eb3cSopenharmony_ci            event.action = Ace::AxisAction::NONE;
30723b3eb3cSopenharmony_ci            break;
30823b3eb3cSopenharmony_ci    }
30923b3eb3cSopenharmony_ci}
31023b3eb3cSopenharmony_ci
31123b3eb3cSopenharmony_cistatic double GetAxisValue(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, MMI::PointerEvent::AxisType axis)
31223b3eb3cSopenharmony_ci{
31323b3eb3cSopenharmony_ci    double axisValue {};
31423b3eb3cSopenharmony_ci    if ((axis >= MMI::PointerEvent::AXIS_TYPE_UNKNOWN) && (axis < MMI::PointerEvent::AXIS_TYPE_MAX)) {
31523b3eb3cSopenharmony_ci        axisValue = pointerEvent->axisValues_[axis];
31623b3eb3cSopenharmony_ci    }
31723b3eb3cSopenharmony_ci    return axisValue;
31823b3eb3cSopenharmony_ci}
31923b3eb3cSopenharmony_ci
32023b3eb3cSopenharmony_cistatic void ConvertAxisEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent, Ace::AxisEvent& event)
32123b3eb3cSopenharmony_ci{
32223b3eb3cSopenharmony_ci    event.id = pointerEvent->id;
32323b3eb3cSopenharmony_ci    event.x = pointerEvent->x;
32423b3eb3cSopenharmony_ci    event.y = pointerEvent->y;
32523b3eb3cSopenharmony_ci    event.screenX = pointerEvent->screenX;
32623b3eb3cSopenharmony_ci    event.screenY = pointerEvent->screenY;
32723b3eb3cSopenharmony_ci    event.horizontalAxis = GetAxisValue(pointerEvent, MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_HORIZONTAL);
32823b3eb3cSopenharmony_ci    event.verticalAxis = GetAxisValue(pointerEvent, MMI::PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL);
32923b3eb3cSopenharmony_ci    event.pinchAxisScale = GetAxisValue(pointerEvent, MMI::PointerEvent::AxisType::AXIS_TYPE_PINCH);
33023b3eb3cSopenharmony_ci    event.rotateAxisAngle = GetAxisValue(pointerEvent, MMI::PointerEvent::AxisType::AXIS_TYPE_ROTATE);
33123b3eb3cSopenharmony_ci    GetAxisEventAction(pointerEvent->pointerAction_, event);
33223b3eb3cSopenharmony_ci    event.isRotationEvent = (pointerEvent->pointerAction_ >= MMI::PointerEvent::POINTER_ACTION_ROTATE_BEGIN) &&
33323b3eb3cSopenharmony_ci                            (pointerEvent->pointerAction_ <= MMI::PointerEvent::POINTER_ACTION_ROTATE_END);
33423b3eb3cSopenharmony_ci    event.sourceType = SourceType::MOUSE;
33523b3eb3cSopenharmony_ci    event.sourceTool = SourceTool::MOUSE;
33623b3eb3cSopenharmony_ci    event.pointerEvent = pointerEvent;
33723b3eb3cSopenharmony_ci}
33823b3eb3cSopenharmony_ci
33923b3eb3cSopenharmony_cibool EventDispatcher::DispatchTouchEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
34023b3eb3cSopenharmony_ci{
34123b3eb3cSopenharmony_ci    ACE_SCOPED_TRACE("DispatchTouchEvent");
34223b3eb3cSopenharmony_ci    auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
34323b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(container, false);
34423b3eb3cSopenharmony_ci    auto aceView = AceType::DynamicCast<AceViewPreview>(container->GetAceView());
34523b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(aceView, false);
34623b3eb3cSopenharmony_ci    if (pointerEvent->sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
34723b3eb3cSopenharmony_ci        if (pointerEvent->pointerAction_ >= MMI::PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
34823b3eb3cSopenharmony_ci            pointerEvent->pointerAction_ <= MMI::PointerEvent::POINTER_ACTION_AXIS_END) {
34923b3eb3cSopenharmony_ci            OHOS::Ace::AxisEvent axisEvent;
35023b3eb3cSopenharmony_ci            ConvertAxisEvent(pointerEvent, axisEvent);
35123b3eb3cSopenharmony_ci            return aceView->HandleAxisEvent(axisEvent);
35223b3eb3cSopenharmony_ci        } else {
35323b3eb3cSopenharmony_ci            OHOS::Ace::MouseEvent mouseEvent;
35423b3eb3cSopenharmony_ci            ConvertMouseEvent(pointerEvent, mouseEvent);
35523b3eb3cSopenharmony_ci            return aceView->HandleMouseEvent(mouseEvent);
35623b3eb3cSopenharmony_ci        }
35723b3eb3cSopenharmony_ci    }
35823b3eb3cSopenharmony_ci
35923b3eb3cSopenharmony_ci    TouchEvent touchEvent;
36023b3eb3cSopenharmony_ci    ConvertTouchEvent(pointerEvent, touchEvent);
36123b3eb3cSopenharmony_ci    return aceView->HandleTouchEvent(touchEvent);
36223b3eb3cSopenharmony_ci}
36323b3eb3cSopenharmony_ci
36423b3eb3cSopenharmony_cibool EventDispatcher::DispatchBackPressedEvent()
36523b3eb3cSopenharmony_ci{
36623b3eb3cSopenharmony_ci    ACE_SCOPED_TRACE("DispatchBackPressedEvent");
36723b3eb3cSopenharmony_ci    auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
36823b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(container, false);
36923b3eb3cSopenharmony_ci    auto context = container->GetPipelineContext();
37023b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(context, false);
37123b3eb3cSopenharmony_ci
37223b3eb3cSopenharmony_ci    std::promise<bool> backPromise;
37323b3eb3cSopenharmony_ci    std::future<bool> backFuture = backPromise.get_future();
37423b3eb3cSopenharmony_ci    auto weak = AceType::WeakClaim(AceType::RawPtr(context));
37523b3eb3cSopenharmony_ci    container->GetTaskExecutor()->PostTask(
37623b3eb3cSopenharmony_ci        [weak, &backPromise]() {
37723b3eb3cSopenharmony_ci            auto context = weak.Upgrade();
37823b3eb3cSopenharmony_ci            if (context == nullptr) {
37923b3eb3cSopenharmony_ci                return;
38023b3eb3cSopenharmony_ci            }
38123b3eb3cSopenharmony_ci            bool canBack = false;
38223b3eb3cSopenharmony_ci            if (!context->IsLastPage()) {
38323b3eb3cSopenharmony_ci                canBack = context->CallRouterBackToPopPage();
38423b3eb3cSopenharmony_ci            }
38523b3eb3cSopenharmony_ci            backPromise.set_value(canBack);
38623b3eb3cSopenharmony_ci        },
38723b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUICallRouterBackToPopPage");
38823b3eb3cSopenharmony_ci    return backFuture.get();
38923b3eb3cSopenharmony_ci}
39023b3eb3cSopenharmony_ci
39123b3eb3cSopenharmony_cibool EventDispatcher::DispatchInputMethodEvent(unsigned int codePoint)
39223b3eb3cSopenharmony_ci{
39323b3eb3cSopenharmony_ci    ACE_SCOPED_TRACE("DispatchInputMethodEvent");
39423b3eb3cSopenharmony_ci    return TextInputClientMgr::GetInstance().AddCharacter(static_cast<wchar_t>(codePoint));
39523b3eb3cSopenharmony_ci}
39623b3eb3cSopenharmony_ci
39723b3eb3cSopenharmony_cibool EventDispatcher::DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
39823b3eb3cSopenharmony_ci{
39923b3eb3cSopenharmony_ci    ACE_SCOPED_TRACE("DispatchKeyEvent");
40023b3eb3cSopenharmony_ci    if (HandleTextKeyEvent(keyEvent)) {
40123b3eb3cSopenharmony_ci        return true;
40223b3eb3cSopenharmony_ci    }
40323b3eb3cSopenharmony_ci    auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
40423b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(container, false);
40523b3eb3cSopenharmony_ci    auto aceView = AceType::DynamicCast<AceViewPreview>(container->GetAceView());
40623b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(aceView, false);
40723b3eb3cSopenharmony_ci
40823b3eb3cSopenharmony_ci    KeyEvent event;
40923b3eb3cSopenharmony_ci    ConvertKeyEvent(keyEvent, event);
41023b3eb3cSopenharmony_ci    event.isPreIme = true;
41123b3eb3cSopenharmony_ci    if (!aceView->HandleKeyEvent(event)) {
41223b3eb3cSopenharmony_ci        event.isPreIme = false;
41323b3eb3cSopenharmony_ci        return aceView->HandleKeyEvent(event);
41423b3eb3cSopenharmony_ci    }
41523b3eb3cSopenharmony_ci    return true;
41623b3eb3cSopenharmony_ci}
41723b3eb3cSopenharmony_ci
41823b3eb3cSopenharmony_cibool EventDispatcher::HandleTextKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
41923b3eb3cSopenharmony_ci{
42023b3eb3cSopenharmony_ci    // Only the keys involved in the input component are processed here, and the other keys will be forwarded.
42123b3eb3cSopenharmony_ci    if (!TextInputClientMgr::GetInstance().IsValidClientId()) {
42223b3eb3cSopenharmony_ci        return false;
42323b3eb3cSopenharmony_ci    }
42423b3eb3cSopenharmony_ci
42523b3eb3cSopenharmony_ci    const static size_t maxKeySizes = 2;
42623b3eb3cSopenharmony_ci    wchar_t keyChar;
42723b3eb3cSopenharmony_ci    if (keyEvent->pressedCodes.size() == 1) {
42823b3eb3cSopenharmony_ci        auto iterCode = PRINTABEL_SYMBOLS.find(keyEvent->code);
42923b3eb3cSopenharmony_ci        if (iterCode != PRINTABEL_SYMBOLS.end()) {
43023b3eb3cSopenharmony_ci            keyChar = iterCode->second;
43123b3eb3cSopenharmony_ci        } else if (MMI::KeyCode::KEY_0 <= keyEvent->code && keyEvent->code <= MMI::KeyCode::KEY_9) {
43223b3eb3cSopenharmony_ci            keyChar = static_cast<wchar_t>(keyEvent->code) - static_cast<wchar_t>(MMI::KeyCode::KEY_0) + CASE_0;
43323b3eb3cSopenharmony_ci        } else if (MMI::KeyCode::KEY_NUMPAD_0 <= keyEvent->code && keyEvent->code <= MMI::KeyCode::KEY_NUMPAD_9) {
43423b3eb3cSopenharmony_ci            if (!keyEvent->enableNumLock_) {
43523b3eb3cSopenharmony_ci                return true;
43623b3eb3cSopenharmony_ci            }
43723b3eb3cSopenharmony_ci            keyChar = static_cast<wchar_t>(keyEvent->code) - static_cast<wchar_t>(MMI::KeyCode::KEY_NUMPAD_0) + CASE_0;
43823b3eb3cSopenharmony_ci        } else if (MMI::KeyCode::KEY_A <= keyEvent->code && keyEvent->code <= MMI::KeyCode::KEY_Z) {
43923b3eb3cSopenharmony_ci            keyChar = static_cast<wchar_t>(keyEvent->code) - static_cast<wchar_t>(MMI::KeyCode::KEY_A);
44023b3eb3cSopenharmony_ci            keyChar += (keyEvent->enableCapsLock_ ? UPPER_CASE_A : LOWER_CASE_A);
44123b3eb3cSopenharmony_ci        } else {
44223b3eb3cSopenharmony_ci            return false;
44323b3eb3cSopenharmony_ci        }
44423b3eb3cSopenharmony_ci    } else if (keyEvent->pressedCodes.size() == maxKeySizes &&
44523b3eb3cSopenharmony_ci               keyEvent->pressedCodes[0] == MMI::KeyCode::KEY_SHIFT_LEFT) {
44623b3eb3cSopenharmony_ci        auto iterCode = SHIFT_PRINTABEL_SYMBOLS.find(keyEvent->code);
44723b3eb3cSopenharmony_ci        if (iterCode != SHIFT_PRINTABEL_SYMBOLS.end()) {
44823b3eb3cSopenharmony_ci            keyChar = iterCode->second;
44923b3eb3cSopenharmony_ci        } else if (MMI::KeyCode::KEY_A <= keyEvent->code && keyEvent->code <= MMI::KeyCode::KEY_Z) {
45023b3eb3cSopenharmony_ci            keyChar = static_cast<wchar_t>(keyEvent->code) - static_cast<wchar_t>(MMI::KeyCode::KEY_A);
45123b3eb3cSopenharmony_ci            keyChar += (keyEvent->enableCapsLock_ ? LOWER_CASE_A : UPPER_CASE_A);
45223b3eb3cSopenharmony_ci        } else if (MMI::KeyCode::KEY_0 <= keyEvent->code && keyEvent->code <= MMI::KeyCode::KEY_9) {
45323b3eb3cSopenharmony_ci            keyChar = NUM_SYMBOLS[static_cast<int32_t>(keyEvent->code) - static_cast<int32_t>(MMI::KeyCode::KEY_0)];
45423b3eb3cSopenharmony_ci        } else {
45523b3eb3cSopenharmony_ci            return false;
45623b3eb3cSopenharmony_ci        }
45723b3eb3cSopenharmony_ci    } else {
45823b3eb3cSopenharmony_ci        return false;
45923b3eb3cSopenharmony_ci    }
46023b3eb3cSopenharmony_ci    if (keyEvent->action != MMI::KeyAction::DOWN) {
46123b3eb3cSopenharmony_ci        return true;
46223b3eb3cSopenharmony_ci    }
46323b3eb3cSopenharmony_ci    return TextInputClientMgr::GetInstance().AddCharacter(keyChar);
46423b3eb3cSopenharmony_ci}
46523b3eb3cSopenharmony_ci
46623b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Platform
467