1 /*
2 * Copyright (c) 2024 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.h"
17
18 #include "base/log/log_wrapper.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21
22 namespace OHOS::Ace {
23 namespace {
24 constexpr int32_t MOUSESTYLE_LOG_LIMIT = 10;
25 }
SetMouseFormat(int32_t windowId,int32_t nodeId,MouseFormat mouseFormat,bool isByPass,MouseStyleChangeReason reason)26 bool MouseStyleManager::SetMouseFormat(int32_t windowId, int32_t nodeId, MouseFormat mouseFormat,
27 bool isByPass, MouseStyleChangeReason reason)
28 {
29 TAG_LOGD(AceLogTag::ACE_MOUSE, "SetMouseFormat windowId = %{public}d, "
30 "nodeId = " SEC_PLD(%{public}d) ", "
31 "mouseFormat = %{public}d, reason = %{public}d", windowId,
32 SEC_PARAM(nodeId), mouseFormat, reason);
33 auto pipelineContext = PipelineContext::GetCurrentContextSafelyWithCheck();
34 CHECK_NULL_RETURN(pipelineContext, false);
35 if (isByPass) {
36 return false;
37 }
38 if (userSetCursor_ && reason != MouseStyleChangeReason::USER_SET_MOUSESTYLE) {
39 return false;
40 }
41 if (!userSetCursor_ && (!mouseStyleNodeId_.has_value() ||
42 mouseStyleNodeId_.value() != nodeId)) {
43 return false;
44 }
45 if (!windowId) {
46 windowId = static_cast<int32_t>(pipelineContext->GetFocusWindowId());
47 }
48
49 MouseStyleChangeLog mouseStyleChangeLog;
50 mouseStyleChangeLog.windowId = windowId;
51 mouseStyleChangeLog.changeNodeId = nodeId;
52 if (vsyncMouseStyleChanges_.empty()) {
53 mouseStyleChangeLog.beforeMouseStyle = mouseFormat_;
54 } else {
55 mouseStyleChangeLog.beforeMouseStyle = vsyncMouseStyleChanges_.back().afterMouseStyle;
56 }
57 mouseStyleChangeLog.afterMouseStyle = mouseFormat;
58 mouseStyleChangeLog.reason = reason;
59 vsyncMouseStyleChanges_.push_back(mouseStyleChangeLog);
60 return true;
61 }
62
VsyncMouseFormat()63 void MouseStyleManager::VsyncMouseFormat()
64 {
65 if (vsyncMouseStyleChanges_.empty()) {
66 return;
67 }
68
69 MouseStyleChangeLog mouseStyleChangeLog;
70 mouseStyleChangeLog.beforeMouseStyle = lastVsyncMouseFormat_;
71 lastVsyncMouseFormat_ = mouseFormat_;
72 MouseStyleChangeReason changeReason = MouseStyleChangeReason::INNER_SET_MOUSESTYLE;
73 int32_t changeNodeId = -1;
74 int32_t windowId = -1;
75 for (const auto& iter : vsyncMouseStyleChanges_) {
76 if (iter.reason >= changeReason) {
77 mouseFormat_ = iter.afterMouseStyle;
78 windowId = iter.windowId;
79 changeNodeId = iter.changeNodeId;
80 changeReason = iter.reason;
81 }
82 }
83 mouseStyleChangeLog.afterMouseStyle = mouseFormat_;
84 mouseStyleChangeLog.changeNodeId = changeNodeId;
85 mouseStyleChangeLog.reason = changeReason;
86
87 if (mouseFormat_ == lastVsyncMouseFormat_) {
88 TAG_LOGI(AceLogTag::ACE_MOUSE, "VsyncMouseFormat mouseFormat_ and "
89 "lastVsyncMouseFormat_ = %{public}d is same.", mouseFormat_);
90 vsyncMouseStyleChanges_.clear();
91 return;
92 }
93
94 TAG_LOGD(AceLogTag::ACE_MOUSE, "VsyncMouseFormat setPointerStyle, windowId is %{public}d, "
95 "nodeId is %{public}d, last vsync mouseFormat is %{public}d, set mouseFormat is %{public}d",
96 windowId, changeNodeId, lastVsyncMouseFormat_, mouseFormat_);
97 auto mouseStyle = MouseStyle::CreateMouseStyle();
98 mouseStyle->SetPointerStyle(static_cast<int32_t>(windowId), mouseFormat_);
99
100 mouseStyleChangeLogs_.push_back(mouseStyleChangeLog);
101 if (mouseStyleChangeLogs_.size() > MOUSESTYLE_LOG_LIMIT) {
102 mouseStyleChangeLogs_.pop_front();
103 }
104 vsyncMouseStyleChanges_.clear();
105 }
106
DumpMouseStyleChangeLog()107 void MouseStyleManager::DumpMouseStyleChangeLog()
108 {
109 for (const auto& iter : mouseStyleChangeLogs_) {
110 TAG_LOGI(AceLogTag::ACE_MOUSE, "MouseStyleChangeLog: windowId is %{public}d, "
111 "nodeId is %{public}d, beforeMouseStyle is %{public}d, afterMouseStyle is %{public}d",
112 iter.windowId, iter.changeNodeId,
113 iter.beforeMouseStyle, iter.afterMouseStyle);
114 }
115 }
116
117 } // namespace OHOS::Ace