123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2022 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 "mouse_style_ohos.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "input_manager.h"
1923b3eb3cSopenharmony_ci#include "struct_multimodal.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_ci#include "core/common/container.h"
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_cinamespace OHOS::Ace {
2423b3eb3cSopenharmony_ci
2523b3eb3cSopenharmony_ciRefPtr<MouseStyle> MouseStyle::CreateMouseStyle()
2623b3eb3cSopenharmony_ci{
2723b3eb3cSopenharmony_ci    return AceType::MakeRefPtr<MouseStyleOhos>();
2823b3eb3cSopenharmony_ci}
2923b3eb3cSopenharmony_ci
3023b3eb3cSopenharmony_cibool MouseStyleOhos::SetPointerStyle(int32_t windowId, MouseFormat pointerStyle) const
3123b3eb3cSopenharmony_ci{
3223b3eb3cSopenharmony_ci    auto container = Container::Current();
3323b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(container, false);
3423b3eb3cSopenharmony_ci    auto isUIExtension = container->IsUIExtensionWindow() && pointerStyle != MouseFormat::DEFAULT;
3523b3eb3cSopenharmony_ci    auto inputManager = MMI::InputManager::GetInstance();
3623b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(inputManager, false);
3723b3eb3cSopenharmony_ci    static const LinearEnumMapNode<MouseFormat, int32_t> mouseFormatMap[] = {
3823b3eb3cSopenharmony_ci        { MouseFormat::DEFAULT, MMI::DEFAULT },
3923b3eb3cSopenharmony_ci        { MouseFormat::EAST, MMI::EAST },
4023b3eb3cSopenharmony_ci        { MouseFormat::WEST, MMI::WEST },
4123b3eb3cSopenharmony_ci        { MouseFormat::SOUTH, MMI::SOUTH },
4223b3eb3cSopenharmony_ci        { MouseFormat::NORTH, MMI::NORTH },
4323b3eb3cSopenharmony_ci        { MouseFormat::WEST_EAST, MMI::WEST_EAST },
4423b3eb3cSopenharmony_ci        { MouseFormat::NORTH_SOUTH, MMI::NORTH_SOUTH },
4523b3eb3cSopenharmony_ci        { MouseFormat::NORTH_EAST, MMI::NORTH_EAST },
4623b3eb3cSopenharmony_ci        { MouseFormat::NORTH_WEST, MMI::NORTH_WEST },
4723b3eb3cSopenharmony_ci        { MouseFormat::SOUTH_EAST, MMI::SOUTH_EAST },
4823b3eb3cSopenharmony_ci        { MouseFormat::SOUTH_WEST, MMI::SOUTH_WEST },
4923b3eb3cSopenharmony_ci        { MouseFormat::NORTH_EAST_SOUTH_WEST, MMI::NORTH_EAST_SOUTH_WEST },
5023b3eb3cSopenharmony_ci        { MouseFormat::NORTH_WEST_SOUTH_EAST, MMI::NORTH_WEST_SOUTH_EAST },
5123b3eb3cSopenharmony_ci        { MouseFormat::CROSS, MMI::CROSS },
5223b3eb3cSopenharmony_ci        { MouseFormat::CURSOR_COPY, MMI::CURSOR_COPY },
5323b3eb3cSopenharmony_ci        { MouseFormat::CURSOR_FORBID, MMI::CURSOR_FORBID },
5423b3eb3cSopenharmony_ci        { MouseFormat::COLOR_SUCKER, MMI::COLOR_SUCKER },
5523b3eb3cSopenharmony_ci        { MouseFormat::HAND_GRABBING, MMI::HAND_GRABBING },
5623b3eb3cSopenharmony_ci        { MouseFormat::HAND_OPEN, MMI::HAND_OPEN },
5723b3eb3cSopenharmony_ci        { MouseFormat::HAND_POINTING, MMI::HAND_POINTING },
5823b3eb3cSopenharmony_ci        { MouseFormat::HELP, MMI::HELP },
5923b3eb3cSopenharmony_ci        { MouseFormat::CURSOR_MOVE, MMI::CURSOR_MOVE },
6023b3eb3cSopenharmony_ci        { MouseFormat::RESIZE_LEFT_RIGHT, MMI::RESIZE_LEFT_RIGHT },
6123b3eb3cSopenharmony_ci        { MouseFormat::RESIZE_UP_DOWN, MMI::RESIZE_UP_DOWN },
6223b3eb3cSopenharmony_ci        { MouseFormat::SCREENSHOT_CHOOSE, MMI::SCREENSHOT_CHOOSE },
6323b3eb3cSopenharmony_ci        { MouseFormat::SCREENSHOT_CURSOR, MMI::SCREENSHOT_CURSOR },
6423b3eb3cSopenharmony_ci        { MouseFormat::TEXT_CURSOR, MMI::TEXT_CURSOR },
6523b3eb3cSopenharmony_ci        { MouseFormat::ZOOM_IN, MMI::ZOOM_IN },
6623b3eb3cSopenharmony_ci        { MouseFormat::ZOOM_OUT, MMI::ZOOM_OUT },
6723b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_EAST, MMI::MIDDLE_BTN_EAST },
6823b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_WEST, MMI::MIDDLE_BTN_WEST },
6923b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_SOUTH, MMI::MIDDLE_BTN_SOUTH },
7023b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_NORTH, MMI::MIDDLE_BTN_NORTH },
7123b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_NORTH_SOUTH, MMI::MIDDLE_BTN_NORTH_SOUTH },
7223b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_NORTH_EAST, MMI::MIDDLE_BTN_NORTH_EAST },
7323b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_NORTH_WEST, MMI::MIDDLE_BTN_NORTH_WEST },
7423b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_SOUTH_EAST, MMI::MIDDLE_BTN_SOUTH_EAST },
7523b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_SOUTH_WEST, MMI::MIDDLE_BTN_SOUTH_WEST },
7623b3eb3cSopenharmony_ci        { MouseFormat::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST, MMI::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST },
7723b3eb3cSopenharmony_ci        { MouseFormat::HORIZONTAL_TEXT_CURSOR, MMI::HORIZONTAL_TEXT_CURSOR },
7823b3eb3cSopenharmony_ci        { MouseFormat::CURSOR_CROSS, MMI::CURSOR_CROSS },
7923b3eb3cSopenharmony_ci        { MouseFormat::CURSOR_CIRCLE, MMI::CURSOR_CIRCLE },
8023b3eb3cSopenharmony_ci        { MouseFormat::LOADING, MMI::LOADING },
8123b3eb3cSopenharmony_ci        { MouseFormat::RUNNING, MMI::RUNNING },
8223b3eb3cSopenharmony_ci    };
8323b3eb3cSopenharmony_ci    int32_t MMIPointStyle = MMI::DEFAULT;
8423b3eb3cSopenharmony_ci    int64_t idx = BinarySearchFindIndex(mouseFormatMap, ArraySize(mouseFormatMap), pointerStyle);
8523b3eb3cSopenharmony_ci    if (idx >= 0) {
8623b3eb3cSopenharmony_ci        MMIPointStyle = mouseFormatMap[idx].value;
8723b3eb3cSopenharmony_ci    }
8823b3eb3cSopenharmony_ci    MMI::PointerStyle style;
8923b3eb3cSopenharmony_ci    style.id = MMIPointStyle;
9023b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_MOUSE, "SetPointerStyle windowId=%{public}d style=%{public}d isUIExtension=%{public}d",
9123b3eb3cSopenharmony_ci        windowId, static_cast<int32_t>(pointerStyle), isUIExtension);
9223b3eb3cSopenharmony_ci    int32_t setResult = inputManager->SetPointerStyle(windowId, style, isUIExtension);
9323b3eb3cSopenharmony_ci    if (setResult == -1) {
9423b3eb3cSopenharmony_ci        LOGW("SetPointerStyle result is false");
9523b3eb3cSopenharmony_ci        return false;
9623b3eb3cSopenharmony_ci    }
9723b3eb3cSopenharmony_ci    return true;
9823b3eb3cSopenharmony_ci}
9923b3eb3cSopenharmony_ci
10023b3eb3cSopenharmony_ciint32_t MouseStyleOhos::GetPointerStyle(int32_t windowId, int32_t& pointerStyle) const
10123b3eb3cSopenharmony_ci{
10223b3eb3cSopenharmony_ci    auto container = Container::Current();
10323b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(container, -1);
10423b3eb3cSopenharmony_ci    auto isUIExtension = container->IsUIExtensionWindow();
10523b3eb3cSopenharmony_ci    auto inputManager = MMI::InputManager::GetInstance();
10623b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(inputManager, -1);
10723b3eb3cSopenharmony_ci    MMI::PointerStyle style;
10823b3eb3cSopenharmony_ci    int32_t getResult = inputManager->GetPointerStyle(windowId, style, isUIExtension);
10923b3eb3cSopenharmony_ci    if (getResult == -1) {
11023b3eb3cSopenharmony_ci        LOGW("GetPointerStyle result is false");
11123b3eb3cSopenharmony_ci        return -1;
11223b3eb3cSopenharmony_ci    }
11323b3eb3cSopenharmony_ci    pointerStyle = style.id;
11423b3eb3cSopenharmony_ci    return getResult;
11523b3eb3cSopenharmony_ci}
11623b3eb3cSopenharmony_ci
11723b3eb3cSopenharmony_cibool MouseStyleOhos::ChangePointerStyle(int32_t windowId, MouseFormat mouseFormat) const
11823b3eb3cSopenharmony_ci{
11923b3eb3cSopenharmony_ci    int32_t curPointerStyle = -1;
12023b3eb3cSopenharmony_ci    if (GetPointerStyle(windowId, curPointerStyle) == -1) {
12123b3eb3cSopenharmony_ci        LOGW("ChangePointerStyle: GetPointerStyle return failed");
12223b3eb3cSopenharmony_ci        return false;
12323b3eb3cSopenharmony_ci    }
12423b3eb3cSopenharmony_ci    if (curPointerStyle == static_cast<int32_t>(mouseFormat)) {
12523b3eb3cSopenharmony_ci        return true;
12623b3eb3cSopenharmony_ci    }
12723b3eb3cSopenharmony_ci
12823b3eb3cSopenharmony_ci    return SetPointerStyle(windowId, mouseFormat);
12923b3eb3cSopenharmony_ci}
13023b3eb3cSopenharmony_ci
13123b3eb3cSopenharmony_civoid MouseStyleOhos::SetMouseIcon(
13223b3eb3cSopenharmony_ci    int32_t windowId, MouseFormat pointerStyle, std::shared_ptr<Media::PixelMap> pixelMap) const
13323b3eb3cSopenharmony_ci{
13423b3eb3cSopenharmony_ci    auto inputManager = MMI::InputManager::GetInstance();
13523b3eb3cSopenharmony_ci    if (pointerStyle == MouseFormat::CONTEXT_MENU) {
13623b3eb3cSopenharmony_ci        inputManager->SetPointerVisible(true);
13723b3eb3cSopenharmony_ci        inputManager->SetMouseIcon(windowId, static_cast<void*>(pixelMap.get()));
13823b3eb3cSopenharmony_ci    } else if (pointerStyle == MouseFormat::ALIAS) {
13923b3eb3cSopenharmony_ci        inputManager->SetMouseIcon(windowId, static_cast<void*>(pixelMap.get()));
14023b3eb3cSopenharmony_ci        inputManager->SetPointerVisible(true);
14123b3eb3cSopenharmony_ci    }
14223b3eb3cSopenharmony_ci}
14323b3eb3cSopenharmony_ci
14423b3eb3cSopenharmony_civoid MouseStyleOhos::SetCustomCursor(
14523b3eb3cSopenharmony_ci    int32_t windowId, int32_t focusX, int32_t focusY, std::shared_ptr<Media::PixelMap> pixelMap) const
14623b3eb3cSopenharmony_ci{
14723b3eb3cSopenharmony_ci    auto inputManager = MMI::InputManager::GetInstance();
14823b3eb3cSopenharmony_ci    CHECK_NULL_VOID(inputManager);
14923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(pixelMap);
15023b3eb3cSopenharmony_ci
15123b3eb3cSopenharmony_ci    int32_t status = inputManager->SetCustomCursor(windowId, static_cast<void*>(pixelMap.get()), focusX, focusY);
15223b3eb3cSopenharmony_ci    if (status != 0) {
15323b3eb3cSopenharmony_ci        TAG_LOGE(AceLogTag::ACE_WEB, "set custom cursor failed %{public}u", status);
15423b3eb3cSopenharmony_ci        return;
15523b3eb3cSopenharmony_ci    }
15623b3eb3cSopenharmony_ci    inputManager->SetPointerVisible(true);
15723b3eb3cSopenharmony_ci}
15823b3eb3cSopenharmony_ci
15923b3eb3cSopenharmony_civoid MouseStyleOhos::SetPointerVisible(MouseFormat pointerStyle) const
16023b3eb3cSopenharmony_ci{
16123b3eb3cSopenharmony_ci    auto inputManager = MMI::InputManager::GetInstance();
16223b3eb3cSopenharmony_ci    if (pointerStyle != MouseFormat::CURSOR_NONE) {
16323b3eb3cSopenharmony_ci        inputManager->SetPointerVisible(true);
16423b3eb3cSopenharmony_ci    } else {
16523b3eb3cSopenharmony_ci        inputManager->SetPointerVisible(false);
16623b3eb3cSopenharmony_ci    }
16723b3eb3cSopenharmony_ci}
16823b3eb3cSopenharmony_ci} // namespace OHOS::Ace