1 /*
2  * Copyright (c) 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 #include "mouse_style_ohos.h"
17 
18 #include "input_manager.h"
19 #include "struct_multimodal.h"
20 
21 #include "core/common/container.h"
22 
23 namespace OHOS::Ace {
24 
CreateMouseStyle()25 RefPtr<MouseStyle> MouseStyle::CreateMouseStyle()
26 {
27     return AceType::MakeRefPtr<MouseStyleOhos>();
28 }
29 
SetPointerStyle(int32_t windowId, MouseFormat pointerStyle) const30 bool MouseStyleOhos::SetPointerStyle(int32_t windowId, MouseFormat pointerStyle) const
31 {
32     auto container = Container::Current();
33     CHECK_NULL_RETURN(container, false);
34     auto isUIExtension = container->IsUIExtensionWindow() && pointerStyle != MouseFormat::DEFAULT;
35     auto inputManager = MMI::InputManager::GetInstance();
36     CHECK_NULL_RETURN(inputManager, false);
37     static const LinearEnumMapNode<MouseFormat, int32_t> mouseFormatMap[] = {
38         { MouseFormat::DEFAULT, MMI::DEFAULT },
39         { MouseFormat::EAST, MMI::EAST },
40         { MouseFormat::WEST, MMI::WEST },
41         { MouseFormat::SOUTH, MMI::SOUTH },
42         { MouseFormat::NORTH, MMI::NORTH },
43         { MouseFormat::WEST_EAST, MMI::WEST_EAST },
44         { MouseFormat::NORTH_SOUTH, MMI::NORTH_SOUTH },
45         { MouseFormat::NORTH_EAST, MMI::NORTH_EAST },
46         { MouseFormat::NORTH_WEST, MMI::NORTH_WEST },
47         { MouseFormat::SOUTH_EAST, MMI::SOUTH_EAST },
48         { MouseFormat::SOUTH_WEST, MMI::SOUTH_WEST },
49         { MouseFormat::NORTH_EAST_SOUTH_WEST, MMI::NORTH_EAST_SOUTH_WEST },
50         { MouseFormat::NORTH_WEST_SOUTH_EAST, MMI::NORTH_WEST_SOUTH_EAST },
51         { MouseFormat::CROSS, MMI::CROSS },
52         { MouseFormat::CURSOR_COPY, MMI::CURSOR_COPY },
53         { MouseFormat::CURSOR_FORBID, MMI::CURSOR_FORBID },
54         { MouseFormat::COLOR_SUCKER, MMI::COLOR_SUCKER },
55         { MouseFormat::HAND_GRABBING, MMI::HAND_GRABBING },
56         { MouseFormat::HAND_OPEN, MMI::HAND_OPEN },
57         { MouseFormat::HAND_POINTING, MMI::HAND_POINTING },
58         { MouseFormat::HELP, MMI::HELP },
59         { MouseFormat::CURSOR_MOVE, MMI::CURSOR_MOVE },
60         { MouseFormat::RESIZE_LEFT_RIGHT, MMI::RESIZE_LEFT_RIGHT },
61         { MouseFormat::RESIZE_UP_DOWN, MMI::RESIZE_UP_DOWN },
62         { MouseFormat::SCREENSHOT_CHOOSE, MMI::SCREENSHOT_CHOOSE },
63         { MouseFormat::SCREENSHOT_CURSOR, MMI::SCREENSHOT_CURSOR },
64         { MouseFormat::TEXT_CURSOR, MMI::TEXT_CURSOR },
65         { MouseFormat::ZOOM_IN, MMI::ZOOM_IN },
66         { MouseFormat::ZOOM_OUT, MMI::ZOOM_OUT },
67         { MouseFormat::MIDDLE_BTN_EAST, MMI::MIDDLE_BTN_EAST },
68         { MouseFormat::MIDDLE_BTN_WEST, MMI::MIDDLE_BTN_WEST },
69         { MouseFormat::MIDDLE_BTN_SOUTH, MMI::MIDDLE_BTN_SOUTH },
70         { MouseFormat::MIDDLE_BTN_NORTH, MMI::MIDDLE_BTN_NORTH },
71         { MouseFormat::MIDDLE_BTN_NORTH_SOUTH, MMI::MIDDLE_BTN_NORTH_SOUTH },
72         { MouseFormat::MIDDLE_BTN_NORTH_EAST, MMI::MIDDLE_BTN_NORTH_EAST },
73         { MouseFormat::MIDDLE_BTN_NORTH_WEST, MMI::MIDDLE_BTN_NORTH_WEST },
74         { MouseFormat::MIDDLE_BTN_SOUTH_EAST, MMI::MIDDLE_BTN_SOUTH_EAST },
75         { MouseFormat::MIDDLE_BTN_SOUTH_WEST, MMI::MIDDLE_BTN_SOUTH_WEST },
76         { MouseFormat::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST, MMI::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST },
77         { MouseFormat::HORIZONTAL_TEXT_CURSOR, MMI::HORIZONTAL_TEXT_CURSOR },
78         { MouseFormat::CURSOR_CROSS, MMI::CURSOR_CROSS },
79         { MouseFormat::CURSOR_CIRCLE, MMI::CURSOR_CIRCLE },
80         { MouseFormat::LOADING, MMI::LOADING },
81         { MouseFormat::RUNNING, MMI::RUNNING },
82     };
83     int32_t MMIPointStyle = MMI::DEFAULT;
84     int64_t idx = BinarySearchFindIndex(mouseFormatMap, ArraySize(mouseFormatMap), pointerStyle);
85     if (idx >= 0) {
86         MMIPointStyle = mouseFormatMap[idx].value;
87     }
88     MMI::PointerStyle style;
89     style.id = MMIPointStyle;
90     TAG_LOGI(AceLogTag::ACE_MOUSE, "SetPointerStyle windowId=%{public}d style=%{public}d isUIExtension=%{public}d",
91         windowId, static_cast<int32_t>(pointerStyle), isUIExtension);
92     int32_t setResult = inputManager->SetPointerStyle(windowId, style, isUIExtension);
93     if (setResult == -1) {
94         LOGW("SetPointerStyle result is false");
95         return false;
96     }
97     return true;
98 }
99 
GetPointerStyle(int32_t windowId, int32_t& pointerStyle) const100 int32_t MouseStyleOhos::GetPointerStyle(int32_t windowId, int32_t& pointerStyle) const
101 {
102     auto container = Container::Current();
103     CHECK_NULL_RETURN(container, -1);
104     auto isUIExtension = container->IsUIExtensionWindow();
105     auto inputManager = MMI::InputManager::GetInstance();
106     CHECK_NULL_RETURN(inputManager, -1);
107     MMI::PointerStyle style;
108     int32_t getResult = inputManager->GetPointerStyle(windowId, style, isUIExtension);
109     if (getResult == -1) {
110         LOGW("GetPointerStyle result is false");
111         return -1;
112     }
113     pointerStyle = style.id;
114     return getResult;
115 }
116 
ChangePointerStyle(int32_t windowId, MouseFormat mouseFormat) const117 bool MouseStyleOhos::ChangePointerStyle(int32_t windowId, MouseFormat mouseFormat) const
118 {
119     int32_t curPointerStyle = -1;
120     if (GetPointerStyle(windowId, curPointerStyle) == -1) {
121         LOGW("ChangePointerStyle: GetPointerStyle return failed");
122         return false;
123     }
124     if (curPointerStyle == static_cast<int32_t>(mouseFormat)) {
125         return true;
126     }
127 
128     return SetPointerStyle(windowId, mouseFormat);
129 }
130 
SetMouseIcon( int32_t windowId, MouseFormat pointerStyle, std::shared_ptr<Media::PixelMap> pixelMap) const131 void MouseStyleOhos::SetMouseIcon(
132     int32_t windowId, MouseFormat pointerStyle, std::shared_ptr<Media::PixelMap> pixelMap) const
133 {
134     auto inputManager = MMI::InputManager::GetInstance();
135     if (pointerStyle == MouseFormat::CONTEXT_MENU) {
136         inputManager->SetPointerVisible(true);
137         inputManager->SetMouseIcon(windowId, static_cast<void*>(pixelMap.get()));
138     } else if (pointerStyle == MouseFormat::ALIAS) {
139         inputManager->SetMouseIcon(windowId, static_cast<void*>(pixelMap.get()));
140         inputManager->SetPointerVisible(true);
141     }
142 }
143 
SetCustomCursor( int32_t windowId, int32_t focusX, int32_t focusY, std::shared_ptr<Media::PixelMap> pixelMap) const144 void MouseStyleOhos::SetCustomCursor(
145     int32_t windowId, int32_t focusX, int32_t focusY, std::shared_ptr<Media::PixelMap> pixelMap) const
146 {
147     auto inputManager = MMI::InputManager::GetInstance();
148     CHECK_NULL_VOID(inputManager);
149     CHECK_NULL_VOID(pixelMap);
150 
151     int32_t status = inputManager->SetCustomCursor(windowId, static_cast<void*>(pixelMap.get()), focusX, focusY);
152     if (status != 0) {
153         TAG_LOGE(AceLogTag::ACE_WEB, "set custom cursor failed %{public}u", status);
154         return;
155     }
156     inputManager->SetPointerVisible(true);
157 }
158 
SetPointerVisible(MouseFormat pointerStyle) const159 void MouseStyleOhos::SetPointerVisible(MouseFormat pointerStyle) const
160 {
161     auto inputManager = MMI::InputManager::GetInstance();
162     if (pointerStyle != MouseFormat::CURSOR_NONE) {
163         inputManager->SetPointerVisible(true);
164     } else {
165         inputManager->SetPointerVisible(false);
166     }
167 }
168 } // namespace OHOS::Ace