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
26 namespace OHOS::Ace {
27
CreateMouseStyle()28 RefPtr<MouseStyle> MouseStyle::CreateMouseStyle()
29 {
30 return AceType::MakeRefPtr<MouseStyleOhos>();
31 }
32
SetPointerStyle(int32_t windowId,MouseFormat pointerStyle) const33 bool MouseStyleOhos::SetPointerStyle(int32_t windowId, MouseFormat pointerStyle) const
34 {
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_SOUTH_WEST, MMI::NORTH_EAST_SOUTH_WEST },
46 { MouseFormat::NORTH_WEST_SOUTH_EAST, MMI::NORTH_WEST_SOUTH_EAST },
47 { MouseFormat::CROSS, MMI::CROSS },
48 { MouseFormat::CURSOR_COPY, MMI::CURSOR_COPY },
49 { MouseFormat::CURSOR_FORBID, MMI::CURSOR_FORBID },
50 { MouseFormat::HAND_GRABBING, MMI::HAND_GRABBING },
51 { MouseFormat::HAND_OPEN, MMI::HAND_OPEN },
52 { MouseFormat::HAND_POINTING, MMI::HAND_POINTING },
53 { MouseFormat::HELP, MMI::HELP },
54 { MouseFormat::CURSOR_MOVE, MMI::CURSOR_MOVE },
55 { MouseFormat::RESIZE_LEFT_RIGHT, MMI::RESIZE_LEFT_RIGHT },
56 { MouseFormat::RESIZE_UP_DOWN, MMI::RESIZE_UP_DOWN },
57 { MouseFormat::TEXT_CURSOR, MMI::TEXT_CURSOR },
58 { MouseFormat::ZOOM_IN, MMI::ZOOM_IN },
59 { MouseFormat::ZOOM_OUT, MMI::ZOOM_OUT },
60 { MouseFormat::MIDDLE_BTN_EAST, MMI::MIDDLE_BTN_EAST },
61 { MouseFormat::MIDDLE_BTN_WEST, MMI::MIDDLE_BTN_WEST },
62 { MouseFormat::MIDDLE_BTN_SOUTH, MMI::MIDDLE_BTN_SOUTH },
63 { MouseFormat::MIDDLE_BTN_NORTH, MMI::MIDDLE_BTN_NORTH },
64 { MouseFormat::MIDDLE_BTN_NORTH_SOUTH, MMI::MIDDLE_BTN_NORTH_SOUTH },
65 { MouseFormat::MIDDLE_BTN_NORTH_EAST, MMI::MIDDLE_BTN_NORTH_EAST },
66 { MouseFormat::MIDDLE_BTN_NORTH_WEST, MMI::MIDDLE_BTN_NORTH_WEST },
67 { MouseFormat::MIDDLE_BTN_SOUTH_EAST, MMI::MIDDLE_BTN_SOUTH_EAST },
68 { MouseFormat::MIDDLE_BTN_SOUTH_WEST, MMI::MIDDLE_BTN_SOUTH_WEST },
69 { MouseFormat::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST, MMI::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST },
70 };
71 int32_t MMIPointStyle = MMI::DEFAULT;
72 int64_t idx = BinarySearchFindIndex(mouseFormatMap, ArraySize(mouseFormatMap), pointerStyle);
73 if (idx >= 0) {
74 MMIPointStyle = mouseFormatMap[idx].value;
75 }
76 MMI::PointerStyle style;
77 style.id = MMIPointStyle;
78 int32_t setResult = inputManager->SetPointerStyle(windowId, style);
79 if (setResult == -1) {
80 LOGE("SetPointerStyle result is false");
81 return false;
82 }
83 return true;
84 }
85
GetPointerStyle(int32_t windowId,int32_t & pointerStyle) const86 int32_t MouseStyleOhos::GetPointerStyle(int32_t windowId, int32_t& pointerStyle) const
87 {
88 auto inputManager = MMI::InputManager::GetInstance();
89 CHECK_NULL_RETURN(inputManager, -1);
90 MMI::PointerStyle style;
91 int32_t getResult = inputManager->GetPointerStyle(windowId, style);
92 if (getResult == -1) {
93 LOGE("GetPointerStyle result is false");
94 return -1;
95 }
96 pointerStyle = style.id;
97 return getResult;
98 }
99
ChangePointerStyle(int32_t windowId,MouseFormat mouseFormat) const100 bool MouseStyleOhos::ChangePointerStyle(int32_t windowId, MouseFormat mouseFormat) const
101 {
102 int32_t curPointerStyle = -1;
103 if (GetPointerStyle(windowId, curPointerStyle) == -1) {
104 LOGE("ChangePointerStyle: GetPointerStyle return failed");
105 return false;
106 }
107 if (curPointerStyle == static_cast<int32_t>(mouseFormat)) {
108 return true;
109 }
110
111 LOGD("ChangePointerStyle do SetPointerStyle: %{public}d", mouseFormat);
112 return SetPointerStyle(windowId, mouseFormat);
113 }
114
115 } // namespace OHOS::Ace