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