1 /*
2 * Copyright (c) 2021-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 "event_dump.h"
17
18 #include <getopt.h>
19
20 #include <climits>
21 #include <cstdarg>
22 #include <cstring>
23
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27
28 #include "event_interceptor_handler.h"
29 #include "event_monitor_handler.h"
30 #ifdef OHOS_BUILD_ENABLE_COOPERATE
31 #include "input_device_cooperate_sm.h"
32 #endif // OHOS_BUILD_ENABLE_COOPERATE
33 #include "input_device_manager.h"
34 #include "input_event_handler.h"
35 #include "input_windows_manager.h"
36 #include "key_subscriber_handler.h"
37 #include "mouse_event_normalize.h"
38 #include "securec.h"
39 #include "util_ex.h"
40 #include "util.h"
41
42 namespace OHOS {
43 namespace MMI {
44 namespace {
45 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "EventDump" };
46 constexpr size_t MAX_COMMAND_COUNT { 32 };
47 } // namespace
48
EventDump()49 EventDump::EventDump() {}
~EventDump()50 EventDump::~EventDump() {}
51
ChkConfig(int32_t fd)52 void ChkConfig(int32_t fd)
53 {
54 mprintf(fd, "ChkMMIConfig: ");
55 mprintf(fd, "DEF_MMI_DATA_ROOT: %s\n", DEF_MMI_DATA_ROOT);
56 mprintf(fd, "EXP_CONFIG: %s\n", DEF_EXP_CONFIG);
57 mprintf(fd, "EXP_SOPATH: %s\n", DEF_EXP_SOPATH);
58 }
59
ParseCommand(int32_t fd,const std::vector<std::string> & args)60 void EventDump::ParseCommand(int32_t fd, const std::vector<std::string> &args)
61 {
62 CALL_DEBUG_ENTER;
63 size_t count = 0;
64 for (const auto &str : args) {
65 if (str.find("--") == 0) {
66 ++count;
67 continue;
68 }
69 if (str.find("-") == 0) {
70 count += str.size() - 1;
71 continue;
72 }
73 }
74 if (count > MAX_COMMAND_COUNT) {
75 MMI_HILOGE("cmd param number not more than 32");
76 mprintf(fd, "cmd param number not more than 32\n");
77 return;
78 }
79 int32_t optionIndex = 0;
80 struct option dumpOptions[] = {
81 {"help", no_argument, 0, 'h'},
82 {"device", no_argument, 0, 'd'},
83 {"devicelist", no_argument, 0, 'l'},
84 {"windows", no_argument, 0, 'w'},
85 {"udsserver", no_argument, 0, 'u'},
86 {"subscriber", no_argument, 0, 's'},
87 {"monitor", no_argument, 0, 'o'},
88 {"interceptor", no_argument, 0, 'i'},
89 {"mouse", no_argument, 0, 'm'},
90 #ifdef OHOS_BUILD_ENABLE_COOPERATE
91 {"inputdevcoosm", no_argument, 0, 'k'},
92 #endif // OHOS_BUILD_ENABLE_COOPERATE
93 {NULL, 0, 0, 0}
94 };
95 char **argv = new (std::nothrow) char *[args.size()];
96 CHKPV(argv);
97 if (memset_s(argv, args.size() * sizeof(char*), 0, args.size() * sizeof(char*)) != EOK) {
98 MMI_HILOGE("Call memset_s failed");
99 delete[] argv;
100 return;
101 }
102 for (size_t i = 0; i < args.size(); ++i) {
103 argv[i] = new (std::nothrow) char[args[i].size() + 1];
104 if (argv[i] == nullptr) {
105 MMI_HILOGE("Failed to allocate memory");
106 goto RELEASE_RES;
107 }
108 if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) {
109 MMI_HILOGE("strcpy_s error");
110 goto RELEASE_RES;
111 }
112 }
113 optind = 1;
114 int32_t c;
115 while ((c = getopt_long (args.size(), argv, "hdlwusoimc", dumpOptions, &optionIndex)) != -1) {
116 switch (c) {
117 case 'h': {
118 DumpEventHelp(fd, args);
119 break;
120 }
121 case 'd': {
122 InputDevMgr->Dump(fd, args);
123 break;
124 }
125 case 'l': {
126 InputDevMgr->DumpDeviceList(fd, args);
127 break;
128 }
129 case 'w': {
130 WinMgr->Dump(fd, args);
131 break;
132 }
133 case 'u': {
134 auto udsServer = InputHandler->GetUDSServer();
135 CHKPV(udsServer);
136 udsServer->Dump(fd, args);
137 break;
138 }
139 case 'c': {
140 #ifdef OHOS_BUILD_ENABLE_COOPERATE
141 InputDevCooSM->Dump(fd, args);
142 #else
143 mprintf(fd, "Input device cooperate does not support");
144 #endif // OHOS_BUILD_ENABLE_COOPERATE
145 break;
146 }
147 case 's': {
148 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
149 auto subscriberHandler = InputHandler->GetSubscriberHandler();
150 CHKPV(subscriberHandler);
151 subscriberHandler->Dump(fd, args);
152 #else
153 mprintf(fd, "Keyboard device does not support");
154 #endif // OHOS_BUILD_ENABLE_KEYBOARD
155 break;
156 }
157 case 'o': {
158 #ifdef OHOS_BUILD_ENABLE_MONITOR
159 auto monitorHandler = InputHandler->GetMonitorHandler();
160 CHKPV(monitorHandler);
161 monitorHandler->Dump(fd, args);
162 #else
163 mprintf(fd, "Monitor function does not support");
164 #endif // OHOS_BUILD_ENABLE_MONITOR
165 break;
166 }
167 case 'i': {
168 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
169 auto interceptorHandler = InputHandler->GetInterceptorHandler();
170 CHKPV(interceptorHandler);
171 interceptorHandler->Dump(fd, args);
172 #else
173 mprintf(fd, "Interceptor function does not support");
174 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
175 break;
176 }
177 case 'm': {
178 #ifdef OHOS_BUILD_ENABLE_POINTER
179 MouseEventHdr->Dump(fd, args);
180 #else
181 mprintf(fd, "Pointer device does not support");
182 #endif // OHOS_BUILD_ENABLE_POINTER
183 break;
184 }
185 default: {
186 mprintf(fd, "cmd param is error\n");
187 DumpHelp(fd);
188 break;
189 }
190 }
191 }
192 RELEASE_RES:
193 for (size_t i = 0; i < args.size(); ++i) {
194 delete[] argv[i];
195 }
196 delete[] argv;
197 }
198
DumpEventHelp(int32_t fd,const std::vector<std::string> & args)199 void EventDump::DumpEventHelp(int32_t fd, const std::vector<std::string> &args)
200 {
201 DumpHelp(fd);
202 }
203
DumpHelp(int32_t fd)204 void EventDump::DumpHelp(int32_t fd)
205 {
206 mprintf(fd, "Usage:\t");
207 mprintf(fd, " -h, --help: dump help\t");
208 mprintf(fd, " -d, --device: dump the device information\t");
209 mprintf(fd, " -l, --devicelist: dump the device list information\t");
210 mprintf(fd, " -w, --windows: dump the windows information\t");
211 mprintf(fd, " -u, --udsserver: dump the uds_server information\t");
212 mprintf(fd, " -o, --monitor: dump the monitor information\t");
213 mprintf(fd, " -s, --subscriber: dump the subscriber information\t");
214 mprintf(fd, " -i, --interceptor: dump the interceptor information\t");
215 mprintf(fd, " -m, --mouse: dump the mouse information\t");
216 mprintf(fd, " -c, --dump Keyboard and mouse crossing information\t");
217 }
218 } // namespace MMI
219 } // namespace OHOS
220