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