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