1 /*
2 * Copyright (c) 2021-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 "event_dump.h"
17
18 #include <getopt.h>
19
20 #include "event_statistic.h"
21 #include "input_device_manager.h"
22 #include "input_event_handler.h"
23 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
24 #include "i_input_windows_manager.h"
25 #ifdef OHOS_BUILD_ENABLE_COMBINATION_KEY
26 #include "key_command_handler.h"
27 #endif // OHOS_BUILD_ENABLE_COMBINATION_KEY
28 #include "key_subscriber_handler.h"
29 #endif // OHOS_BUILD_ENABLE_KEYBOARD
30 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
31 #include "touch_drawing_manager.h"
32 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
33 #include "cursor_drawing_component.h"
34 #include "util_ex.h"
35
36 #undef MMI_LOG_DOMAIN
37 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
38 #undef MMI_LOG_TAG
39 #define MMI_LOG_TAG "EventDump"
40
41 namespace OHOS {
42 namespace MMI {
43 namespace {
44 constexpr int32_t MAX_COMMAND_COUNT { 32 };
45 } // namespace
46
EventDump()47 EventDump::EventDump() {}
~EventDump()48 EventDump::~EventDump() {}
49
ChkConfig(int32_t fd)50 void ChkConfig(int32_t fd)
51 {
52 mprintf(fd, "ChkMMIConfig: ");
53 mprintf(fd, "DEF_MMI_DATA_ROOT:%s\n", DEF_MMI_DATA_ROOT);
54 mprintf(fd, "EXP_CONFIG:%s\n", DEF_EXP_CONFIG);
55 mprintf(fd, "EXP_SOPATH:%s\n", DEF_EXP_SOPATH);
56 }
57
CheckCount(int32_t fd,const std::vector<std::string> & args,int32_t & count)58 void EventDump::CheckCount(int32_t fd, const std::vector<std::string> &args, int32_t &count)
59 {
60 CALL_DEBUG_ENTER;
61 for (const auto &str : args) {
62 if (str.find("--") == 0) {
63 ++count;
64 continue;
65 }
66 if (str.find("-") == 0) {
67 count += static_cast<int32_t>(str.size()) - 1;
68 continue;
69 }
70 }
71 }
72
ParseCommand(int32_t fd,const std::vector<std::string> & args)73 void EventDump::ParseCommand(int32_t fd, const std::vector<std::string> &args)
74 {
75 CALL_DEBUG_ENTER;
76 int32_t count = 0;
77 CheckCount(fd, args, count);
78 if (count > MAX_COMMAND_COUNT) {
79 MMI_HILOGE("cmd param number not more than 32");
80 mprintf(fd, "cmd param number not more than 32\n");
81 return;
82 }
83 int32_t optionIndex = 0;
84 struct option dumpOptions[] = {
85 { "help", no_argument, 0, 'h' },
86 { "device", no_argument, 0, 'd' },
87 { "devicelist", no_argument, 0, 'l' },
88 { "windows", no_argument, 0, 'w' },
89 { "udsserver", no_argument, 0, 'u' },
90 { "subscriber", no_argument, 0, 's' },
91 { "monitor", no_argument, 0, 'o' },
92 { "interceptor", no_argument, 0, 'i' },
93 { "filter", no_argument, 0, 'f' },
94 { "mouse", no_argument, 0, 'm' },
95 { "cursor", no_argument, 0, 'c' },
96 { "keycommand", no_argument, 0, 'k' },
97 { "event", no_argument, 0, 'e' },
98 { nullptr, 0, 0, 0 }
99 };
100 if (args.empty()) {
101 MMI_HILOGE("size of args can't be zero");
102 return;
103 }
104 char **argv = new (std::nothrow) char *[args.size()];
105 CHKPV(argv);
106 if (memset_s(argv, args.size() * sizeof(char*), 0, args.size() * sizeof(char*)) != EOK) {
107 MMI_HILOGE("Call memset_s failed");
108 delete[] argv;
109 argv = nullptr;
110 return;
111 }
112 for (size_t i = 0; i < args.size(); ++i) {
113 argv[i] = new (std::nothrow) char[args[i].size() + 1];
114 if (argv[i] == nullptr) {
115 MMI_HILOGE("Failed to allocate memory");
116 goto RELEASE_RES;
117 }
118 if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) {
119 MMI_HILOGE("strcpy_s error");
120 goto RELEASE_RES;
121 }
122 }
123 optind = 1;
124 int32_t c;
125 while ((c = getopt_long (args.size(), argv, "hdlwusoifmcke", dumpOptions, &optionIndex)) != -1) {
126 switch (c) {
127 case 'h': {
128 DumpEventHelp(fd, args);
129 break;
130 }
131 case 'd': {
132 INPUT_DEV_MGR->Dump(fd, args);
133 break;
134 }
135 case 'l': {
136 INPUT_DEV_MGR->DumpDeviceList(fd, args);
137 break;
138 }
139 case 'w': {
140 WIN_MGR->Dump(fd, args);
141 break;
142 }
143 case 'u': {
144 auto udsServer = InputHandler->GetUDSServer();
145 CHKPV(udsServer);
146 udsServer->Dump(fd, args);
147 break;
148 }
149 case 's': {
150 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
151 auto subscriberHandler = InputHandler->GetSubscriberHandler();
152 CHKPV(subscriberHandler);
153 subscriberHandler->Dump(fd, args);
154 #else
155 mprintf(fd, "Keyboard device does not support");
156 #endif // OHOS_BUILD_ENABLE_KEYBOARD
157 break;
158 }
159 case 'o': {
160 #ifdef OHOS_BUILD_ENABLE_MONITOR
161 auto monitorHandler = InputHandler->GetMonitorHandler();
162 CHKPV(monitorHandler);
163 monitorHandler->Dump(fd, args);
164 #else
165 mprintf(fd, "Monitor function does not support");
166 #endif // OHOS_BUILD_ENABLE_MONITOR
167 break;
168 }
169 case 'i': {
170 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
171 auto interceptorHandler = InputHandler->GetInterceptorHandler();
172 CHKPV(interceptorHandler);
173 interceptorHandler->Dump(fd, args);
174 #else
175 mprintf(fd, "Interceptor function does not support");
176 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
177 break;
178 }
179 case 'f': {
180 auto filterHandler = InputHandler->GetFilterHandler();
181 CHKPV(filterHandler);
182 filterHandler->Dump(fd, args);
183 break;
184 }
185 case 'm': {
186 #ifdef OHOS_BUILD_ENABLE_POINTER
187 MouseEventHdr->Dump(fd, args);
188 #else
189 mprintf(fd, "Pointer device does not support");
190 #endif // OHOS_BUILD_ENABLE_POINTER
191 break;
192 }
193 case 'c': {
194 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
195 CursorDrawingComponent::GetInstance().Dump(fd, args);
196 #else
197 mprintf(fd, "Pointer device does not support");
198 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
199 #ifdef OHOS_BUILD_ENABLE_TOUCH
200 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
201 TOUCH_DRAWING_MGR->Dump(fd, args);
202 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
203 #else
204 mprintf(fd, "Pointer device does not support");
205 #endif // OHOS_BUILD_ENABLE_TOUCH
206 break;
207 }
208 case 'k': {
209 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
210 auto keyHandler = InputHandler->GetKeyCommandHandler();
211 CHKPV(keyHandler);
212 keyHandler->Dump(fd, args);
213 #else
214 mprintf(fd, "Combination key does not support");
215 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
216 break;
217 }
218 case 'e': {
219 EventStatistic::Dump(fd, args);
220 break;
221 }
222 default: {
223 mprintf(fd, "cmd param is error\n");
224 DumpHelp(fd);
225 break;
226 }
227 }
228 }
229 RELEASE_RES:
230 for (size_t i = 0; i < args.size(); ++i) {
231 if (argv[i] != nullptr) {
232 delete[] argv[i];
233 }
234 }
235 delete[] argv;
236 }
237
DumpEventHelp(int32_t fd,const std::vector<std::string> & args)238 void EventDump::DumpEventHelp(int32_t fd, const std::vector<std::string> &args)
239 {
240 DumpHelp(fd);
241 }
242
DumpHelp(int32_t fd)243 void EventDump::DumpHelp(int32_t fd)
244 {
245 mprintf(fd, "Usage:\t");
246 mprintf(fd, " -h, --help: dump help\t");
247 mprintf(fd, " -d, --device: dump the device information\t");
248 mprintf(fd, " -l, --devicelist: dump the device list information\t");
249 mprintf(fd, " -w, --windows: dump the windows information\t");
250 mprintf(fd, " -u, --udsserver: dump the uds_server information\t");
251 mprintf(fd, " -o, --monitor: dump the monitor information\t");
252 mprintf(fd, " -s, --subscriber: dump the subscriber information\t");
253 mprintf(fd, " -i, --interceptor: dump the interceptor information\t");
254 mprintf(fd, " -f, --filter: dump the filter information\t");
255 mprintf(fd, " -m, --mouse: dump the mouse information\t");
256 mprintf(fd, " -c, --cursor: dump the cursor draw information\t");
257 mprintf(fd, " -k, --keycommand: dump the key command information\t");
258 mprintf(fd, " -e, --event: dump the libinput event information\t");
259 }
260 } // namespace MMI
261 } // namespace OHOS
262