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 "i_pointer_drawing_manager.h"
22 #include "input_device_manager.h"
23 #include "input_event_handler.h"
24 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
25 #include "i_input_windows_manager.h"
26 #ifdef OHOS_BUILD_ENABLE_COMBINATION_KEY
27 #include "key_command_handler.h"
28 #endif // OHOS_BUILD_ENABLE_COMBINATION_KEY
29 #include "key_subscriber_handler.h"
30 #endif // OHOS_BUILD_ENABLE_KEYBOARD
31 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
32 #include "touch_drawing_manager.h"
33 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
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 return;
110 }
111 for (size_t i = 0; i < args.size(); ++i) {
112 argv[i] = new (std::nothrow) char[args[i].size() + 1];
113 if (argv[i] == nullptr) {
114 MMI_HILOGE("Failed to allocate memory");
115 goto RELEASE_RES;
116 }
117 if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) {
118 MMI_HILOGE("strcpy_s error");
119 goto RELEASE_RES;
120 }
121 }
122 optind = 1;
123 int32_t c;
124 while ((c = getopt_long (args.size(), argv, "hdlwusoifmcke", dumpOptions, &optionIndex)) != -1) {
125 switch (c) {
126 case 'h': {
127 DumpEventHelp(fd, args);
128 break;
129 }
130 case 'd': {
131 INPUT_DEV_MGR->Dump(fd, args);
132 break;
133 }
134 case 'l': {
135 INPUT_DEV_MGR->DumpDeviceList(fd, args);
136 break;
137 }
138 case 'w': {
139 WIN_MGR->Dump(fd, args);
140 break;
141 }
142 case 'u': {
143 auto udsServer = InputHandler->GetUDSServer();
144 CHKPV(udsServer);
145 udsServer->Dump(fd, args);
146 break;
147 }
148 case 's': {
149 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
150 auto subscriberHandler = InputHandler->GetSubscriberHandler();
151 CHKPV(subscriberHandler);
152 subscriberHandler->Dump(fd, args);
153 #else
154 mprintf(fd, "Keyboard device does not support");
155 #endif // OHOS_BUILD_ENABLE_KEYBOARD
156 break;
157 }
158 case 'o': {
159 #ifdef OHOS_BUILD_ENABLE_MONITOR
160 auto monitorHandler = InputHandler->GetMonitorHandler();
161 CHKPV(monitorHandler);
162 monitorHandler->Dump(fd, args);
163 #else
164 mprintf(fd, "Monitor function does not support");
165 #endif // OHOS_BUILD_ENABLE_MONITOR
166 break;
167 }
168 case 'i': {
169 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
170 auto interceptorHandler = InputHandler->GetInterceptorHandler();
171 CHKPV(interceptorHandler);
172 interceptorHandler->Dump(fd, args);
173 #else
174 mprintf(fd, "Interceptor function does not support");
175 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
176 break;
177 }
178 case 'f': {
179 auto filterHandler = InputHandler->GetFilterHandler();
180 CHKPV(filterHandler);
181 filterHandler->Dump(fd, args);
182 break;
183 }
184 case 'm': {
185 #ifdef OHOS_BUILD_ENABLE_POINTER
186 MouseEventHdr->Dump(fd, args);
187 #else
188 mprintf(fd, "Pointer device does not support");
189 #endif // OHOS_BUILD_ENABLE_POINTER
190 break;
191 }
192 case 'c': {
193 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
194 IPointerDrawingManager::GetInstance()->Dump(fd, args);
195 #else
196 mprintf(fd, "Pointer device does not support");
197 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
198 #ifdef OHOS_BUILD_ENABLE_TOUCH
199 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
200 TOUCH_DRAWING_MGR->Dump(fd, args);
201 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
202 #else
203 mprintf(fd, "Pointer device does not support");
204 #endif // OHOS_BUILD_ENABLE_TOUCH
205 break;
206 }
207 case 'k': {
208 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
209 auto keyHandler = InputHandler->GetKeyCommandHandler();
210 CHKPV(keyHandler);
211 keyHandler->Dump(fd, args);
212 #else
213 mprintf(fd, "Combination key does not support");
214 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
215 break;
216 }
217 case 'e': {
218 EventStatistic::Dump(fd, args);
219 break;
220 }
221 default: {
222 mprintf(fd, "cmd param is error\n");
223 DumpHelp(fd);
224 break;
225 }
226 }
227 }
228 RELEASE_RES:
229 for (size_t i = 0; i < args.size(); ++i) {
230 if (argv[i] != nullptr) {
231 delete[] argv[i];
232 }
233 }
234 delete[] argv;
235 }
236
DumpEventHelp(int32_t fd,const std::vector<std::string> & args)237 void EventDump::DumpEventHelp(int32_t fd, const std::vector<std::string> &args)
238 {
239 DumpHelp(fd);
240 }
241
DumpHelp(int32_t fd)242 void EventDump::DumpHelp(int32_t fd)
243 {
244 mprintf(fd, "Usage:\t");
245 mprintf(fd, " -h, --help: dump help\t");
246 mprintf(fd, " -d, --device: dump the device information\t");
247 mprintf(fd, " -l, --devicelist: dump the device list information\t");
248 mprintf(fd, " -w, --windows: dump the windows information\t");
249 mprintf(fd, " -u, --udsserver: dump the uds_server information\t");
250 mprintf(fd, " -o, --monitor: dump the monitor information\t");
251 mprintf(fd, " -s, --subscriber: dump the subscriber information\t");
252 mprintf(fd, " -i, --interceptor: dump the interceptor information\t");
253 mprintf(fd, " -f, --filter: dump the filter information\t");
254 mprintf(fd, " -m, --mouse: dump the mouse information\t");
255 mprintf(fd, " -c, --cursor: dump the cursor draw information\t");
256 mprintf(fd, " -k, --keycommand: dump the key command information\t");
257 mprintf(fd, " -e, --event: dump the libinput event information\t");
258 }
259 } // namespace MMI
260 } // namespace OHOS
261