1 /*
2 * Copyright (c) 2025 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 #include <iostream>
16
17 #include "input_method_ability_interface.h"
18 #include "sys_cfg_parser.h"
19
20 using namespace std;
21 using namespace OHOS::MiscServices;
22 class InputMethodEngineListenerImpl : public InputMethodEngineListener {
23 public:
24 InputMethodEngineListenerImpl() = default;
25 ~InputMethodEngineListenerImpl() = default;
OnKeyboardStatus(bool isShow)26 void OnKeyboardStatus(bool isShow) override { };
OnInputStart()27 void OnInputStart() override
28 {
29 inputStartCount_++;
30 IMSA_HILOGI("[ImeMirrorLog] OnInputStart count, inputStartCount_:%{public}u", inputStartCount_.load());
31 }
OnInputStop()32 int32_t OnInputStop() override
33 {
34 inputStopCount_++;
35 IMSA_HILOGI("[ImeMirrorLog] OnInputStop, inputStopCount_:%{public}u", inputStopCount_.load());
36 return 0;
37 }
OnInputFinish()38 void OnInputFinish() override
39 {
40 inputFinishCount_++;
41 IMSA_HILOGI("[ImeMirrorLog] OnInputFinish inputFinishCount_:%{public}u", inputFinishCount_.load());
42 }
OnSetCallingWindow(uint32_t windowId)43 void OnSetCallingWindow(uint32_t windowId) override { }
OnSetSubtype(const SubProperty & property)44 void OnSetSubtype(const SubProperty &property) override { }
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)45 void ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override { }
PrintCount()46 void PrintCount()
47 {
48 IMSA_HILOGI(
49 "[ImeMirrorLog] inputStartCount_:%{public}u, inputFinishCount_:%{public}u, inputStopCount_:%{public}u",
50 inputStartCount_.load(), inputFinishCount_.load(), inputStopCount_.load());
51 };
52
ResetCount()53 void ResetCount()
54 {
55 inputStartCount_ = 0;
56 inputFinishCount_ = 0;
57 inputStopCount_ = 0;
58 }
59
60 private:
61 std::atomic<uint32_t> inputStartCount_ = 0;
62 std::atomic<uint32_t> inputFinishCount_ = 0;
63 std::atomic<uint32_t> inputStopCount_ = 0;
64 };
65 class KeyboardListenerImpl : public KeyboardListener {
66 public:
67 KeyboardListenerImpl() = default;
68 ~KeyboardListenerImpl() = default;
OnDealKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent,uint64_t cbId,const OHOS::sptr<OHOS::IRemoteObject> & channelObject)69 bool OnDealKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent, uint64_t cbId,
70 const OHOS::sptr<OHOS::IRemoteObject> &channelObject) override
71 {
72 return false;
73 }
OnKeyEvent(int32_t keyCode,int32_t keyStatus,OHOS::sptr<KeyEventConsumerProxy> & consumer)74 bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, OHOS::sptr<KeyEventConsumerProxy> &consumer) override
75 {
76 return false;
77 }
OnKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent,OHOS::sptr<KeyEventConsumerProxy> & consumer)78 bool OnKeyEvent(
79 const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent, OHOS::sptr<KeyEventConsumerProxy> &consumer) override
80 {
81 return false;
82 }
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)83 void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override
84 {
85 cursorUpdateCount_++;
86 IMSA_HILOGI("[ImeMirrorLog] OnCursorUpdate positionX:%{public}d,positionY:%{public}d,height:%{public}d, "
87 "cursorUpdateCount_:%{public}u",
88 positionX, positionY, height, cursorUpdateCount_.load());
89 }
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)90 void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override
91 {
92 selectionChangeCount_++;
93 IMSA_HILOGI("[ImeMirrorLog] OnSelectionChange "
94 "oldBegin:%{public}d,oldEnd:%{public}d,newBegin:%{public}d,newEnd:%{public}d, "
95 "selectionChangeCount_:%{public}u",
96 oldBegin, oldEnd, newBegin, newEnd, selectionChangeCount_.load());
97 }
OnTextChange(const std::string & text)98 void OnTextChange(const std::string &text) override
99 {
100 textChangeCount_++;
101 IMSA_HILOGI("[ImeMirrorLog] OnTextChange text:%{public}s, textChangeCount_:%{public}u", text.c_str(),
102 textChangeCount_.load());
103 }
OnEditorAttributeChange(const InputAttribute & inputAttribute)104 void OnEditorAttributeChange(const InputAttribute &inputAttribute) override
105 {
106 IMSA_HILOGI(
107 "[ImeMirrorLog] OnEditorAttributeChange inputAttribute:%{public}s", inputAttribute.ToString().c_str());
108 }
109
OnFunctionKey(int32_t funcKey)110 void OnFunctionKey(int32_t funcKey) override
111 {
112 if (funcKey < 0 || funcKey > static_cast<int32_t>(EnterKeyType::NEW_LINE)) {
113 IMSA_HILOGE("[ImeMirrorLog] invalid funcKey:%{public}d", funcKey);
114 return;
115 }
116 functionKeyCount_[funcKey]++;
117 IMSA_HILOGI("[ImeMirrorLog] OnFunctionKey funcKey:%{public}d, functionKeyCount_:%{public}u", funcKey,
118 functionKeyCount_[funcKey].load());
119 }
120
PrintCount()121 void PrintCount()
122 {
123 IMSA_HILOGI("[ImeMirrorLog] selectionChangeCount_:%{public}u, textChangeCount_:%{public}u, "
124 "cursorUpdateCount_:%{public}u",
125 selectionChangeCount_.load(), textChangeCount_.load(), cursorUpdateCount_.load());
126 for (size_t index = 0; index <= static_cast<size_t>(EnterKeyType::NEW_LINE); index++) {
127 IMSA_HILOGI("[ImeMirrorLog] funcKey:%{public}zu, count:%{public}u", index, functionKeyCount_[index].load());
128 }
129 }
130
ResetCount()131 void ResetCount()
132 {
133 selectionChangeCount_ = 0;
134 textChangeCount_ = 0;
135 cursorUpdateCount_ = 0;
136 for (size_t index = 0; index <= static_cast<size_t>(EnterKeyType::NEW_LINE); index++) {
137 functionKeyCount_[index] = 0;
138 }
139 }
140
141 private:
142 std::atomic<uint32_t> selectionChangeCount_ = 0;
143 std::atomic<uint32_t> textChangeCount_ = 0;
144 std::atomic<uint32_t> functionKeyCount_[static_cast<size_t>(EnterKeyType::NEW_LINE) + 1] = { 0 };
145 std::atomic<uint32_t> cursorUpdateCount_ = 0;
146 };
147
GetAgentUid()148 int32_t GetAgentUid()
149 {
150 SystemConfig systemConfig;
151 SysCfgParser::ParseSystemConfig(systemConfig);
152 bool isImeMirrorFeatureEnabled =
153 systemConfig.supportedCapacityList.find("ime_mirror") != systemConfig.supportedCapacityList.end();
154 if (!isImeMirrorFeatureEnabled) {
155 return -1;
156 }
157
158 if (systemConfig.proxyImeUidList.empty()) {
159 return -1;
160 }
161 return *systemConfig.proxyImeUidList.begin();
162 }
163
main()164 int main()
165 {
166 std::shared_ptr<InputMethodEngineListenerImpl> imeListener = make_shared<InputMethodEngineListenerImpl>();
167 auto instance = InputMethodAbilityInterface::GetInstance();
168 instance.SetImeListener(imeListener);
169 std::shared_ptr<KeyboardListenerImpl> kdListener = make_shared<KeyboardListenerImpl>();
170 instance.SetKdListener(kdListener);
171 char input = '0';
172 int32_t ret = 0;
173 int32_t uid = GetAgentUid();
174 if (uid == -1) {
175 IMSA_HILOGE("[ImeMirrorLog] GetAgentUid failed, please check system config");
176 return 0;
177 }
178 setuid(uid);
179 while (input != 'q') {
180 cin >> input;
181 switch (input) {
182 case 'b':
183 ret = instance.BindImeMirror();
184 IMSA_HILOGI("[ImeMirrorLog] BindImeMirror finish ret = %{public}d", ret);
185 break;
186 case 'u':
187 ret = instance.UnbindImeMirror();
188 IMSA_HILOGI("[ImeMirrorLog] UnbindImeMirror finish ret = %{public}d", ret);
189 break;
190 case 'i':
191 ret = instance.InsertText("ime mirror demo");
192 IMSA_HILOGI("[ImeMirrorLog] InsertText finish ret = %{public}d", ret);
193 break;
194 case 'c':
195 imeListener->PrintCount();
196 kdListener->PrintCount();
197 break;
198 case 'r':
199 imeListener->ResetCount();
200 kdListener->ResetCount();
201 IMSA_HILOGI("[ImeMirrorLog] retset count success");
202 break;
203 default:
204 IMSA_HILOGE("[ImeMirrorLog] input error");
205 }
206 }
207 IMSA_HILOGI("[ImeMirrorLog] quit...");
208 return 0;
209 }