• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "input_manager_command.h"
17 
18 #include <getopt.h>
19 
20 #include <algorithm>
21 #include <chrono>
22 #include <cmath>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <ctime>
26 #include <iostream>
27 #include <limits>
28 #include <thread>
29 
30 #include <sys/time.h>
31 #include <unistd.h>
32 
33 #include "string_ex.h"
34 
35 #include "error_multimodal.h"
36 #include "input_manager.h"
37 #include "mmi_log.h"
38 #include "multimodal_event_handler.h"
39 #include "pointer_event.h"
40 #include "util.h"
41 
42 class InputManagerCommand {
43 public:
44     int32_t ParseCommand(int32_t argc, char *argv[]);
45     int32_t ConnectService();
46     void ShowUsage();
47 private:
48     void InitializeMouseDeathStub();
49 };
50 namespace OHOS {
51 namespace MMI {
52 namespace {
53 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "InputManagerCommand"};
54 constexpr int32_t SLEEPTIME = 20;
55 constexpr int32_t MOUSE_ID = 7;
56 constexpr int32_t TWO_MORE_COMMAND = 2;
57 constexpr int32_t THREE_MORE_COMMAND = 3;
58 constexpr int32_t MAX_PRESSED_COUNT = 30;
59 constexpr int32_t ACTION_TIME = 3000;
60 constexpr int32_t BLOCK_TIME_MS = 10;
61 constexpr int32_t MS_TO_US = 1000;
62 } // namespace
63 
SleepAndUpdateTime(int64_t & currentTimeMs)64 void InputManagerCommand::SleepAndUpdateTime(int64_t &currentTimeMs)
65 {
66     int64_t nowEndSysTimeMs = GetSysClockTime() / 1000;
67     int64_t sleepTimeMs = BLOCK_TIME_MS - (nowEndSysTimeMs - currentTimeMs) % BLOCK_TIME_MS;
68     std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs));
69     currentTimeMs = nowEndSysTimeMs + sleepTimeMs;
70 }
71 
NextPos(int64_t begTimeMs,int64_t curtTimeMs,int32_t totalTimeMs,int32_t begPos,int32_t endPos)72 int32_t InputManagerCommand::NextPos(int64_t begTimeMs, int64_t curtTimeMs, int32_t totalTimeMs,
73     int32_t begPos, int32_t endPos)
74 {
75     int64_t endTimeMs = 0;
76     if (!AddInt64(begTimeMs, totalTimeMs, endTimeMs)) {
77         return begPos;
78     }
79     if (curtTimeMs < begTimeMs || curtTimeMs > endTimeMs) {
80         return begPos;
81     }
82     if (totalTimeMs == 0) {
83         std::cout << "invalid totalTimeMs" << std::endl;
84         return begPos;
85     }
86     double tmpTimeMs = static_cast<double>(curtTimeMs - begTimeMs) / totalTimeMs;
87     int32_t offsetPos = std::ceil(tmpTimeMs * (endPos - begPos));
88     int32_t retPos = 0;
89     if (offsetPos == 0) {
90         return begPos;
91     } else if (offsetPos > 0) {
92         if (!AddInt32(begPos, offsetPos, retPos)) {
93             return begPos;
94         }
95         return retPos > endPos ? endPos : retPos;
96     }
97     if (!AddInt32(begPos, offsetPos, retPos)) {
98         return begPos;
99     }
100     return retPos < endPos ? endPos : retPos;
101 }
102 
ParseCommand(int32_t argc,char * argv[])103 int32_t InputManagerCommand::ParseCommand(int32_t argc, char *argv[])
104 {
105     struct option headOptions[] = {
106         {"mouse", no_argument, NULL, 'M'},
107         {"keyboard", no_argument, NULL, 'K'},
108         {"touch", no_argument, NULL, 'T'},
109         {"help", no_argument, NULL, '?'},
110         {NULL, 0, NULL, 0}
111     };
112 
113     struct option mouseSensorOptions[] = {
114         {"move", required_argument, NULL, 'm'},
115         {"click", required_argument, NULL, 'c'},
116         {"double_click", required_argument, NULL, 'b'},
117         {"down", required_argument, NULL, 'd'},
118         {"up", required_argument, NULL, 'u'},
119         {"scroll", required_argument, NULL, 's'},
120         {"interval", required_argument, NULL, 'i'},
121         {NULL, 0, NULL, 0}
122     };
123     struct option keyboardSensorOptions[] = {
124         {"down", required_argument, NULL, 'd'},
125         {"up", required_argument, NULL, 'u'},
126         {"long_press", required_argument, NULL, 'l'},
127         {"interval", required_argument, NULL, 'i'},
128         {NULL, 0, NULL, 0}
129     };
130     struct option touchSensorOptions[] = {
131         {"move", required_argument, NULL, 'm'},
132         {"down", required_argument, NULL, 'd'},
133         {"up", required_argument, NULL, 'u'},
134         {"click", required_argument, NULL, 'c'},
135         {"interval", required_argument, NULL, 'i'},
136         {"drag", required_argument, NULL, 'g'},
137         {NULL, 0, NULL, 0}
138     };
139     int32_t c = 0;
140     int32_t optionIndex = 0;
141     optind = 0;
142     if ((c = getopt_long(argc, argv, "MKT?", headOptions, &optionIndex)) != -1) {
143         switch (c) {
144             case 'M': {
145                 int32_t px = 0;
146                 int32_t py = 0;
147                 int32_t buttonId;
148                 int32_t scrollValue;
149                 while ((c = getopt_long(argc, argv, "m:d:u:c:b:s:i:", mouseSensorOptions, &optionIndex)) != -1) {
150                     switch (c) {
151                         case 'm': {
152                             if (argc - optind < 1) {
153                                 std::cout << "too few arguments to function" << std::endl;
154                                 return RET_ERR;
155                             }
156                             auto isTraceOption = [](const std::string &opt1) {
157                                 return opt1 == std::string("--trace");
158                             };
159                             auto traceMode = [isTraceOption](int32_t argCount, char *argvOffset[]) -> bool {
160                                 if (argCount <= 3) {
161                                     return false;
162                                 }
163                                 std::string arg3 = argvOffset[2];
164                                 if (!arg3.empty() && arg3.at(0) == '-') {
165                                     return false;
166                                 }
167                                 if ((argCount >= 5) && isTraceOption(std::string(argvOffset[4]))) {
168                                     return true;
169                                 }
170                                 if ((argCount >= 6) && isTraceOption(std::string(argvOffset[5]))) {
171                                     return true;
172                                 }
173                                 return false;
174                             }(argc - optind + 1, &argv[optind - 1]);
175                             if (!traceMode) {
176                                 if (!StrToInt(optarg, px) || !StrToInt(argv[optind], py)) {
177                                     std::cout << "invalid parameter to move mouse" << std::endl;
178                                     return RET_ERR;
179                                 }
180                                 if ((px < 0) || (py < 0)) {
181                                     std::cout << "Coordinate value must be greater than 0" << std::endl;
182                                     return RET_ERR;
183                                 }
184                                 std::cout << "move to " << px << " " << py << std::endl;
185                                 auto pointerEvent = PointerEvent::Create();
186                                 CHKPR(pointerEvent, ERROR_NULL_POINTER);
187                                 PointerEvent::PointerItem item;
188                                 item.SetPointerId(0);
189                                 item.SetDisplayX(px);
190                                 item.SetDisplayY(py);
191                                 pointerEvent->AddPointerItem(item);
192                                 pointerEvent->SetPointerId(0);
193                                 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
194                                 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
195                                 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
196                                 optind++;
197                             } else {
198                                 int32_t px1 = 0;
199                                 int32_t py1 = 0;
200                                 int32_t px2 = 0;
201                                 int32_t py2 = 0;
202                                 int32_t totalTimeMs = 1000;
203                                 bool foundTraceOption = false;
204                                 if (argc - optind >= 3) {
205                                     if ((!StrToInt(optarg, px1)) ||
206                                         (!StrToInt(argv[optind], py1)) ||
207                                         (!StrToInt(argv[optind + 1], px2)) ||
208                                         (!StrToInt(argv[optind + 2], py2))) {
209                                             std::cout << "invalid coordinate value" << std::endl;
210                                             return RET_ERR;
211                                     }
212                                     optind += 3;
213                                 }
214                                 if ((px1 < 0) || (py1 < 0) || (px2 < 0) || (py2 < 0)) {
215                                     std::cout << "Coordinate value must be greater than 0" << std::endl;
216                                     return RET_ERR;
217                                 }
218                                 if (argc - optind >= 1) {
219                                     std::string arg5 = argv[optind];
220                                     if (!arg5.empty() && arg5.at(0) == '-') {
221                                         if (isTraceOption(arg5)) {
222                                             foundTraceOption = true;
223                                         } else {
224                                             std::cout << "invalid option, the 5th position parameter must be --trace"
225                                                 << std::endl;
226                                             return RET_ERR;
227                                         }
228                                     } else if (!StrToInt(arg5, totalTimeMs)) {
229                                         std::cout << "invalid total times" << std::endl;
230                                         return RET_ERR;
231                                     }
232                                     optind++;
233                                 }
234                                 if (!foundTraceOption) {
235                                     if (argc - optind < 1) {
236                                         std::cout << "missing 6th position parameter --trace" << std::endl;
237                                         return RET_ERR;
238                                     }
239                                     std::string arg6 = argv[optind];
240                                     if (!isTraceOption(arg6)) {
241                                         std::cout << "invalid option, the 6th position parameter must be --trace"
242                                             << std::endl;
243                                         return RET_ERR;
244                                     }
245                                     optind++;
246                                     foundTraceOption = true;
247                                 }
248                                 static const int64_t minTotalTimeMs = 1;
249                                 static const int64_t maxTotalTimeMs = 15000;
250                                 if ((totalTimeMs < minTotalTimeMs) || (totalTimeMs > maxTotalTimeMs)) {
251                                     std::cout << "total time is out of range:"
252                                         << minTotalTimeMs << " <= " << totalTimeMs << " <= " << maxTotalTimeMs
253                                         << std::endl;
254                                     return RET_ERR;
255                                 }
256                                 std::cout << "start coordinate: (" << px1 << ", "  << py1 << ")" << std::endl;
257                                 std::cout << "  end coordinate: (" << px2 << ", "  << py2 << ")" << std::endl;
258                                 std::cout << "     total times: "  << totalTimeMs  << " ms"      << std::endl;
259                                 std::cout << "      trace mode: " << std::boolalpha << foundTraceOption << std::endl;
260                                 auto pointerEvent = PointerEvent::Create();
261                                 CHKPR(pointerEvent, ERROR_NULL_POINTER);
262                                 px = px1;
263                                 py = py1;
264                                 PointerEvent::PointerItem item;
265                                 item.SetPointerId(0);
266                                 item.SetDisplayX(px);
267                                 item.SetDisplayY(py);
268                                 pointerEvent->SetPointerId(0);
269                                 pointerEvent->AddPointerItem(item);
270                                 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
271                                 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
272                                 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
273 
274                                 int64_t startTimeUs = GetSysClockTime();
275                                 int64_t startTimeMs = startTimeUs / 1000;
276                                 int64_t endTimeMs = 0;
277                                 if (!AddInt64(startTimeMs, totalTimeMs, endTimeMs)) {
278                                     std::cout << "system time error" << std::endl;
279                                     return RET_ERR;
280                                 }
281                                 int64_t currentTimeMs = startTimeMs;
282                                 while (currentTimeMs < endTimeMs) {
283                                     item.SetDisplayX(NextPos(startTimeMs, currentTimeMs, totalTimeMs, px1, px2));
284                                     item.SetDisplayY(NextPos(startTimeMs, currentTimeMs, totalTimeMs, py1, py2));
285                                     pointerEvent->SetActionTime(currentTimeMs);
286                                     pointerEvent->UpdatePointerItem(0, item);
287                                     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
288                                     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
289                                     SleepAndUpdateTime(currentTimeMs);
290                                 }
291 
292                                 px = px2;
293                                 py = py2;
294                                 item.SetDisplayX(px);
295                                 item.SetDisplayY(py);
296                                 pointerEvent->SetActionTime(endTimeMs);
297                                 pointerEvent->UpdatePointerItem(0, item);
298                                 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
299                                 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
300                             }
301                             break;
302                         }
303                         case 'd': {
304                             if (!StrToInt(optarg, buttonId)) {
305                                 std::cout << "invalid button press command" << std::endl;
306                                 return EVENT_REG_FAIL;
307                             }
308                             if (buttonId > MOUSE_ID) {
309                                 std::cout << "invalid button press command" << std::endl;
310                                 return EVENT_REG_FAIL;
311                             }
312                             std::cout << "press down" << buttonId << std::endl;
313                             auto pointerEvent = PointerEvent::Create();
314                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
315                             PointerEvent::PointerItem item;
316                             item.SetPointerId(0);
317                             item.SetDisplayX(px);
318                             item.SetDisplayY(py);
319                             item.SetPressed(true);
320                             pointerEvent->SetPointerId(0);
321                             pointerEvent->AddPointerItem(item);
322                             pointerEvent->SetButtonId(buttonId);
323                             pointerEvent->SetButtonPressed(buttonId);
324                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
325                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
326                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
327                             break;
328                         }
329                         case 'u': {
330                             if (!StrToInt(optarg, buttonId)) {
331                                 std::cout << "invalid raise button command" << std::endl;
332                                 return EVENT_REG_FAIL;
333                             }
334                             if (buttonId > MOUSE_ID) {
335                                 std::cout << "invalid raise button command" << std::endl;
336                                 return EVENT_REG_FAIL;
337                             }
338                             std::cout << "lift up button " << buttonId << std::endl;
339                             auto pointerEvent = PointerEvent::Create();
340                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
341                             PointerEvent::PointerItem item;
342                             item.SetPointerId(0);
343                             item.SetDisplayX(px);
344                             item.SetDisplayY(py);
345                             item.SetPressed(false);
346                             pointerEvent->SetPointerId(0);
347                             pointerEvent->AddPointerItem(item);
348                             pointerEvent->SetButtonPressed(buttonId);
349                             pointerEvent->SetButtonId(buttonId);
350                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
351                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
352                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
353                             break;
354                         }
355                         case 's': {
356                             if (!StrToInt(optarg, scrollValue)) {
357                                 std::cout << "invalid  scroll button command" << std::endl;
358                                 return EVENT_REG_FAIL;
359                             }
360                             std::cout << "scroll wheel " << scrollValue << std::endl;
361                             auto pointerEvent = PointerEvent::Create();
362                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
363                             PointerEvent::PointerItem item;
364                             item.SetPointerId(0);
365                             item.SetDisplayX(px);
366                             item.SetDisplayY(py);
367                             item.SetPressed(false);
368                             int64_t time = pointerEvent->GetActionStartTime();
369                             pointerEvent->SetActionTime(time + ACTION_TIME);
370                             pointerEvent->SetPointerId(0);
371                             pointerEvent->AddPointerItem(item);
372                             pointerEvent->SetButtonPressed(buttonId);
373                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_BEGIN);
374                             pointerEvent->SetAxisValue(PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL,
375                                 scrollValue);
376                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
377                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
378                             time = pointerEvent->GetActionStartTime();
379 
380                             time = pointerEvent->GetActionStartTime();
381                             pointerEvent->SetActionTime(time + ACTION_TIME);
382                             pointerEvent->SetPointerId(0);
383                             pointerEvent->AddPointerItem(item);
384                             pointerEvent->SetButtonPressed(buttonId);
385                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
386                             pointerEvent->SetAxisValue(PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL,
387                                 scrollValue);
388                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
389                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
390 
391                             time = pointerEvent->GetActionStartTime();
392                             pointerEvent->SetActionTime(time + ACTION_TIME);
393                             pointerEvent->SetPointerId(0);
394                             pointerEvent->AddPointerItem(item);
395                             pointerEvent->SetButtonPressed(buttonId);
396                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
397                             pointerEvent->SetAxisValue(PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL,
398                                 scrollValue);
399                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
400                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
401                             break;
402                         }
403                         case 'c': {
404                             if (!StrToInt(optarg, buttonId)) {
405                                 std::cout << "invalid click button command" << std::endl;
406                                 return EVENT_REG_FAIL;
407                             }
408                             if (buttonId > MOUSE_ID) {
409                                 std::cout << "invalid button press command" << std::endl;
410                                 return EVENT_REG_FAIL;
411                             }
412                             std::cout << "click   " << buttonId << std::endl;
413                             auto pointerEvent = PointerEvent::Create();
414                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
415                             PointerEvent::PointerItem item;
416                             item.SetPointerId(0);
417                             item.SetPressed(true);
418                             item.SetDisplayX(px);
419                             item.SetDisplayY(py);
420                             pointerEvent->SetPointerId(0);
421                             pointerEvent->AddPointerItem(item);
422                             pointerEvent->SetButtonId(buttonId);
423                             pointerEvent->SetButtonPressed(buttonId);
424                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
425                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
426                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
427                             item.SetPointerId(0);
428                             item.SetPressed(false);
429                             item.SetDisplayX(px);
430                             item.SetDisplayY(py);
431                             pointerEvent->SetPointerId(0);
432                             pointerEvent->UpdatePointerItem(0, item);
433                             pointerEvent->SetButtonPressed(buttonId);
434                             pointerEvent->SetButtonId(buttonId);
435                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
436                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
437                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
438                             break;
439                         }
440                         case 'b': {
441                             int32_t pressTimeMs = 50;
442                             int32_t clickIntervalTimeMs = 300;
443                             static constexpr int32_t minButtonId = 0;
444                             static constexpr int32_t maxButtonId = 7;
445                             static constexpr int32_t minPressTimeMs = 1;
446                             static constexpr int32_t maxPressTimeMs = 300;
447                             static constexpr int32_t minClickIntervalTimeMs = 1;
448                             static constexpr int32_t maxClickIntervalTimeMs = 450;
449                             if (argc < 6 || argc > 8) {
450                                 std::cout << "wrong number of parameters" << std::endl;
451                                 return RET_ERR;
452                             }
453                             if (!StrToInt(optarg, px) ||
454                                 !StrToInt(argv[optind], py)) {
455                                 std::cout << "invalid coordinate value" << std::endl;
456                                 return RET_ERR;
457                             }
458                             if ((px < 0) || (py < 0)) {
459                                 std::cout << "Coordinate value must be greater than 0" << std::endl;
460                                 return RET_ERR;
461                             }
462                             if (!StrToInt(argv[optind + 1], buttonId)) {
463                                 std::cout << "invalid key" << std::endl;
464                                 return RET_ERR;
465                             }
466                             if (argc >= 7) {
467                                 if (!StrToInt(argv[optind + 2], pressTimeMs)) {
468                                     std::cout << "invalid press time" << std::endl;
469                                     return RET_ERR;
470                                 }
471                             }
472                             if (argc == 8) {
473                                 if (!StrToInt(argv[optind + 3], clickIntervalTimeMs)) {
474                                     std::cout << "invalid interval between hits" << std::endl;
475                                     return RET_ERR;
476                                 }
477                             }
478                             if ((buttonId < minButtonId) || (buttonId > maxButtonId)) {
479                                 std::cout << "button is out of range:" << minButtonId << " < " << buttonId << " < "
480                                     << maxButtonId << std::endl;
481                                 return RET_ERR;
482                             }
483                             if ((pressTimeMs < minPressTimeMs) || (pressTimeMs > maxPressTimeMs)) {
484                                 std::cout << "press time is out of range:" << minPressTimeMs << " ms" << " < "
485                                     << pressTimeMs << " < " << maxPressTimeMs << " ms" << std::endl;
486                                 return RET_ERR;
487                             }
488                             if ((clickIntervalTimeMs < minClickIntervalTimeMs) ||
489                                 (clickIntervalTimeMs > maxClickIntervalTimeMs)) {
490                                 std::cout << "click interval time is out of range:" << minClickIntervalTimeMs << " ms"
491                                     " < " << clickIntervalTimeMs << " < " << maxClickIntervalTimeMs << " ms"
492                                     << std::endl;
493                                 return RET_ERR;
494                             }
495                             std::cout << "   coordinate: ("<< px << ", "  << py << ")" << std::endl;
496                             std::cout << "    button id: " << buttonId    << std::endl;
497                             std::cout << "   press time: " << pressTimeMs << " ms" << std::endl;
498                             std::cout << "interval time: " << clickIntervalTimeMs  << " ms" << std::endl;
499                             auto pointerEvent = PointerEvent::Create();
500                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
501                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
502                             PointerEvent::PointerItem item;
503                             item.SetPointerId(0);
504                             item.SetPressed(true);
505                             item.SetDisplayX(px);
506                             item.SetDisplayY(py);
507                             pointerEvent->SetPointerId(0);
508                             pointerEvent->AddPointerItem(item);
509                             pointerEvent->SetButtonId(buttonId);
510                             pointerEvent->SetButtonPressed(buttonId);
511                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
512                             InputMgr->SimulateInputEvent(pointerEvent);
513                             std::this_thread::sleep_for(std::chrono::milliseconds(pressTimeMs));
514                             item.SetPressed(false);
515                             pointerEvent->UpdatePointerItem(0, item);
516                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
517                             InputMgr->SimulateInputEvent(pointerEvent);
518                             std::this_thread::sleep_for(std::chrono::milliseconds(clickIntervalTimeMs));
519 
520                             item.SetPressed(true);
521                             pointerEvent->UpdatePointerItem(0, item);
522                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
523                             InputMgr->SimulateInputEvent(pointerEvent);
524                             std::this_thread::sleep_for(std::chrono::milliseconds(pressTimeMs));
525                             item.SetPressed(false);
526                             pointerEvent->UpdatePointerItem(0, item);
527                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
528                             InputMgr->SimulateInputEvent(pointerEvent);
529                             break;
530                         }
531                         case 'i': {
532                             int32_t taktTime = 0;
533                             if (!StrToInt(optarg, taktTime)) {
534                                 std::cout << "invalid command to interval time" << std::endl;
535                                 return EVENT_REG_FAIL;
536                             }
537                             const int64_t minTaktTimeMs = 1;
538                             const int64_t maxTaktTimeMs = 15000;
539                             if ((minTaktTimeMs > taktTime) || (maxTaktTimeMs < taktTime)) {
540                                 std::cout << "taktTime is out of range" << std::endl;
541                                 std::cout << minTaktTimeMs << " < taktTime < " << maxTaktTimeMs;
542                                 std::cout << std::endl;
543                                 return EVENT_REG_FAIL;
544                             }
545                             std::this_thread::sleep_for(std::chrono::milliseconds(taktTime));
546                             break;
547                         }
548                         default: {
549                             std::cout << "invalid command to virtual mouse" << std::endl;
550                             ShowUsage();
551                             return EVENT_REG_FAIL;
552                         }
553                     }
554                     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
555                 }
556                 break;
557             }
558             case 'K': {
559                 std::vector<int32_t> downKey;
560                 int32_t keyCode = 0;
561                 int32_t isCombinationKey = 0;
562                 int64_t time = GetSysClockTime();
563                 while ((c = getopt_long(argc, argv, "d:u:l:i:", keyboardSensorOptions, &optionIndex)) != -1) {
564                     switch (c) {
565                         case 'd': {
566                             if (!StrToInt(optarg, keyCode)) {
567                                 std::cout << "invalid command to down key" << std::endl;
568                             }
569                             if (optind == isCombinationKey + TWO_MORE_COMMAND) {
570                                 downKey.push_back(keyCode);
571                                 isCombinationKey = optind;
572                                 auto KeyEvent = KeyEvent::Create();
573                                 CHKPR(KeyEvent, ERROR_NULL_POINTER);
574                                 if (downKey.size() > MAX_PRESSED_COUNT) {
575                                     std::cout << "pressed button count should less than 30" << std::endl;
576                                     return EVENT_REG_FAIL;
577                                 }
578                                 KeyEvent::KeyItem item[downKey.size()];
579                                 for (size_t i = 0; i < downKey.size(); i++) {
580                                     KeyEvent->SetKeyCode(keyCode);
581                                     KeyEvent->SetActionTime(time);
582                                     KeyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
583                                     item[i].SetKeyCode(downKey[i]);
584                                     item[i].SetDownTime(time);
585                                     item[i].SetPressed(true);
586                                     KeyEvent->AddKeyItem(item[i]);
587                                 }
588                                 InputManager::GetInstance()->SimulateInputEvent(KeyEvent);
589                                 break;
590                             }
591                             downKey.push_back(keyCode);
592                             auto KeyEvent = KeyEvent::Create();
593                             CHKPR(KeyEvent, ERROR_NULL_POINTER);
594                             KeyEvent->SetKeyCode(keyCode);
595                             KeyEvent->SetActionTime(time);
596                             KeyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
597                             KeyEvent::KeyItem item1;
598                             item1.SetKeyCode(keyCode);
599                             item1.SetDownTime(time);
600                             item1.SetPressed(true);
601                             KeyEvent->AddKeyItem(item1);
602                             InputManager::GetInstance()->SimulateInputEvent(KeyEvent);
603                             isCombinationKey = optind;
604                             break;
605                         }
606                         case 'u': {
607                             if (!StrToInt(optarg, keyCode)) {
608                                 std::cout << "invalid button press command" << std::endl;
609                                 return EVENT_REG_FAIL;
610                             }
611                             std::vector<int32_t>::iterator iter = std::find(downKey.begin(), downKey.end(), keyCode);
612                             if (iter != downKey.end()) {
613                                 std::cout << "you raised the key " << keyCode << std::endl;
614                                 auto KeyEvent = KeyEvent::Create();
615                                 CHKPR(KeyEvent, ERROR_NULL_POINTER);
616                                 KeyEvent->SetKeyCode(keyCode);
617                                 KeyEvent->SetActionTime(time);
618                                 KeyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
619                                 KeyEvent::KeyItem item1;
620                                 item1.SetKeyCode(keyCode);
621                                 item1.SetDownTime(time);
622                                 item1.SetPressed(false);
623                                 KeyEvent->AddKeyItem(item1);
624                                 InputManager::GetInstance()->SimulateInputEvent(KeyEvent);
625                                 iter = downKey.erase(iter);
626                                 break;
627                             } else {
628                                 std::cout << "please press the " << keyCode << " key first "<< std::endl;
629                                 return EVENT_REG_FAIL;
630                             }
631                         }
632                         case 'l': {
633                             if (argc < 4) {
634                                 std::cout << "argc:" << argc << std::endl;
635                                 std::cout << "wrong number of parameters" << std::endl;
636                                 return RET_ERR;
637                             }
638                             if (argc >= 4) {
639                                 if (!StrToInt(optarg, keyCode)) {
640                                     std::cout << "invalid key code value" << std::endl;
641                                     return RET_ERR;
642                                 }
643                             }
644                             int32_t pressTimeMs = 3000;
645                             if (argc >= 5) {
646                                 if (!StrToInt(argv[optind], pressTimeMs)) {
647                                     std::cout << "invalid key code value or press time" << std::endl;
648                                     return RET_ERR;
649                                 }
650                             }
651                             static constexpr int32_t minKeyCode = 0;
652                             static constexpr int32_t maxKeyCode = 5000;
653                             if ((keyCode < minKeyCode) || (keyCode > maxKeyCode)) {
654                                 std::cout << "key code is out of range:" << minKeyCode << " <= "
655                                     << keyCode << " <= " << maxKeyCode << std::endl;
656                                 return RET_ERR;
657                             }
658                             static constexpr int32_t minPressTimeMs = 3000;
659                             static constexpr int32_t maxPressTimeMs = 15000;
660                             if ((pressTimeMs < minPressTimeMs) || (pressTimeMs > maxPressTimeMs)) {
661                                 std::cout << "press time is out of range:" << minPressTimeMs << " ms" << " <= "
662                                     << pressTimeMs << " <= " << maxPressTimeMs << " ms" << std::endl;
663                                 return RET_ERR;
664                             }
665                             std::cout << " key code: " << keyCode << std::endl
666                                 << "long press time: " << pressTimeMs << " ms" << std::endl;
667                             auto keyEvent = KeyEvent::Create();
668                             if (keyEvent == nullptr) {
669                                 std::cout << "failed to create input event object" << std::endl;
670                                 return RET_ERR;
671                             }
672                             keyEvent->SetKeyCode(keyCode);
673                             keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
674                             KeyEvent::KeyItem item;
675                             item.SetKeyCode(keyCode);
676                             item.SetPressed(true);
677                             auto keyEventTemp = KeyEvent::Clone(keyEvent);
678                             if (keyEventTemp == nullptr) {
679                                 std::cout << "failed to clone key event object" << std::endl;
680                                 return RET_ERR;
681                             }
682                             keyEventTemp->AddKeyItem(item);
683                             InputMgr->SimulateInputEvent(keyEventTemp);
684                             std::this_thread::sleep_for(std::chrono::milliseconds(pressTimeMs));
685 
686                             keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
687                             item.SetPressed(false);
688                             keyEvent->AddKeyItem(item);
689                             InputMgr->SimulateInputEvent(keyEvent);
690                             break;
691                         }
692                         case 'i': {
693                             int32_t taktTime = 0;
694                             if (!StrToInt(optarg, taktTime)) {
695                                 std::cout << "invalid command to interval time" << std::endl;
696                                 return EVENT_REG_FAIL;
697                             }
698                             const int64_t minTaktTimeMs = 1;
699                             const int64_t maxTaktTimeMs = 15000;
700                             if ((minTaktTimeMs > taktTime) || (maxTaktTimeMs < taktTime)) {
701                                 std::cout << "taktTime is error" << std::endl;
702                                 std::cout << minTaktTimeMs << " < taktTime < " << maxTaktTimeMs;
703                                 std::cout << std::endl;
704                                 return EVENT_REG_FAIL;
705                             }
706                             std::this_thread::sleep_for(std::chrono::milliseconds(taktTime));
707                             break;
708                         }
709                         default: {
710                             std::cout << "invalid command to keyboard key" << std::endl;
711                             ShowUsage();
712                             return EVENT_REG_FAIL;
713                         }
714                     }
715                     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
716                 }
717                 for (size_t i = 0; i < downKey.size(); i++) {
718                     std::cout << "you have a key " << downKey[i] << " not release" << std::endl;
719                 }
720                 break;
721             }
722             case 'T': {
723                 int32_t px1 = 0;
724                 int32_t py1 = 0;
725                 int32_t px2 = 0;
726                 int32_t py2 = 0;
727                 int32_t totalTimeMs = 0;
728                 int32_t moveArgcSeven = 7;
729                 while ((c = getopt_long(argc, argv, "m:d:u:c:i:g:", touchSensorOptions, &optionIndex)) != -1) {
730                     switch (c) {
731                         case 'm': {
732                             if (argc < moveArgcSeven) {
733                                 std::cout << "argc:" << argc << std::endl;
734                                 std::cout << "wrong number of parameters" << std::endl;
735                                 return EVENT_REG_FAIL;
736                             }
737                             if (argv[optind + 3] == nullptr || argv[optind + 3][0] == '-') {
738                                 totalTimeMs = 1000;
739                                 if ((!StrToInt(optarg, px1)) ||
740                                     (!StrToInt(argv[optind], py1)) ||
741                                     (!StrToInt(argv[optind + 1], px2)) ||
742                                     (!StrToInt(argv[optind + 2], py2))) {
743                                         std::cout << "invalid coordinate value" << std::endl;
744                                         return EVENT_REG_FAIL;
745                                 }
746                             } else {
747                                 if ((!StrToInt(optarg, px1)) ||
748                                     (!StrToInt(argv[optind], py1)) ||
749                                     (!StrToInt(argv[optind + 1], px2)) ||
750                                     (!StrToInt(argv[optind + 2], py2)) ||
751                                     (!StrToInt(argv[optind + 3], totalTimeMs))) {
752                                         std::cout << "invalid coordinate value or total times" << std::endl;
753                                         return EVENT_REG_FAIL;
754                                 }
755                             }
756                             if ((px1 < 0) || (py1 < 0) || (px2 < 0) || (py2 < 0)) {
757                                 std::cout << "Coordinate value must be greater than 0" << std::endl;
758                                 return RET_ERR;
759                             }
760                             const int64_t minTotalTimeMs = 1;
761                             const int64_t maxTotalTimeMs = 15000;
762                             if ((totalTimeMs < minTotalTimeMs) || (totalTimeMs > maxTotalTimeMs)) {
763                                 std::cout << "total time is out of range:" << std::endl;
764                                 std::cout << minTotalTimeMs << " <= " << "total times" << " <= " << maxTotalTimeMs;
765                                 std::cout << std::endl;
766                                 return EVENT_REG_FAIL;
767                             }
768                             std::cout << "start coordinate: ("<< px1 << ", "  << py1 << ")" << std::endl;
769                             std::cout << "  end coordinate: ("<< px2 << ", "  << py2 << ")" << std::endl;
770                             std::cout << "     total times: " << totalTimeMs << " ms" << std::endl;
771                             auto pointerEvent = PointerEvent::Create();
772                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
773                             PointerEvent::PointerItem item;
774                             item.SetPointerId(0);
775                             item.SetDisplayX(px1);
776                             item.SetDisplayY(py1);
777                             pointerEvent->SetPointerId(0);
778                             pointerEvent->AddPointerItem(item);
779                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
780                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
781                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
782 
783                             int64_t startTimeUs = pointerEvent->GetActionStartTime();
784                             int64_t startTimeMs = startTimeUs / 1000;
785                             int64_t endTimeMs = 0;
786                             if (!AddInt64(startTimeMs, totalTimeMs, endTimeMs)) {
787                                 std::cout << "system time error." << std::endl;
788                                 return EVENT_REG_FAIL;
789                             }
790                             int64_t currentTimeMs = startTimeMs;
791                             int64_t nowSysTimeUs = 0;
792                             int64_t nowSysTimeMs = 0;
793                             int64_t sleepTimeMs = 0;
794                             while (currentTimeMs < endTimeMs) {
795                                 item.SetDisplayX(NextPos(startTimeMs, currentTimeMs, totalTimeMs, px1, px2));
796                                 item.SetDisplayY(NextPos(startTimeMs, currentTimeMs, totalTimeMs, py1, py2));
797                                 pointerEvent->SetActionTime(currentTimeMs * MS_TO_US);
798                                 pointerEvent->UpdatePointerItem(0, item);
799                                 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
800                                 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
801                                 nowSysTimeUs = GetSysClockTime();
802                                 nowSysTimeMs = nowSysTimeUs / 1000;
803                                 sleepTimeMs = (currentTimeMs + BLOCK_TIME_MS) - nowSysTimeMs;
804                                 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs));
805                                 currentTimeMs += BLOCK_TIME_MS;
806                             }
807 
808                             item.SetDisplayX(px2);
809                             item.SetDisplayY(py2);
810                             pointerEvent->SetActionTime(endTimeMs * MS_TO_US);
811                             pointerEvent->UpdatePointerItem(0, item);
812                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
813                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
814                             std::this_thread::sleep_for(std::chrono::milliseconds(BLOCK_TIME_MS));
815 
816                             item.SetDisplayX(px2);
817                             item.SetDisplayY(py2);
818                             pointerEvent->SetActionTime((endTimeMs + BLOCK_TIME_MS) * MS_TO_US);
819                             pointerEvent->UpdatePointerItem(0, item);
820                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
821                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
822                             optind =  optind + THREE_MORE_COMMAND;
823                             break;
824                         }
825                         case 'd': {
826                             if (optind >= argc) {
827                                 std::cout << "too few arguments to function" << std::endl;
828                                 return EVENT_REG_FAIL;
829                             }
830                             if (!StrToInt(optarg, px1) || !StrToInt(argv[optind], py1)) {
831                                 std::cout << "invalid coordinate value" << std::endl;
832                                 return EVENT_REG_FAIL;
833                             }
834                             if ((px1 < 0) || (py1 < 0)) {
835                                 std::cout << "Coordinate value must be greater than 0" << std::endl;
836                                 return RET_ERR;
837                             }
838                             std::cout << "touch down " << px1 << " " << py1 << std::endl;
839                             auto pointerEvent = PointerEvent::Create();
840                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
841                             PointerEvent::PointerItem item;
842                             item.SetPointerId(0);
843                             item.SetDisplayX(px1);
844                             item.SetDisplayY(py1);
845                             pointerEvent->SetPointerId(0);
846                             pointerEvent->AddPointerItem(item);
847                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
848                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
849                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
850                             optind++;
851                             break;
852                         }
853                         case 'u': {
854                             if (optind >= argc) {
855                                 std::cout << "too few arguments to function" << std::endl;
856                                 return EVENT_REG_FAIL;
857                             }
858                             if (!StrToInt(optarg, px1) || !StrToInt(argv[optind], py1)) {
859                                 std::cout << "invalid coordinate value" << std::endl;
860                                 return EVENT_REG_FAIL;
861                             }
862                             if ((px1 < 0) || (py1 < 0)) {
863                                 std::cout << "Coordinate value must be greater than 0" << std::endl;
864                                 return RET_ERR;
865                             }
866                             std::cout << "touch up " << px1 << " " << py1 << std::endl;
867                             auto pointerEvent = PointerEvent::Create();
868                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
869                             PointerEvent::PointerItem item;
870                             item.SetPointerId(0);
871                             item.SetDisplayX(px1);
872                             item.SetDisplayY(py1);
873                             pointerEvent->SetPointerId(0);
874                             pointerEvent->AddPointerItem(item);
875                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
876                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
877                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
878                             optind++;
879                             break;
880                         }
881                         case 'c': {
882                             int32_t intervalTimeMs = 0;
883                             if (argc == 5) {
884                                 if (!StrToInt(optarg, px1) ||
885                                     !StrToInt(argv[optind], py1)) {
886                                     std::cout << "input coordinate error" << std::endl;
887                                     return RET_ERR;
888                                 }
889                                 intervalTimeMs = 100;
890                             } else if (argc == 6) {
891                                 if (!StrToInt(optarg, px1) ||
892                                     !StrToInt(argv[optind], py1) ||
893                                     !StrToInt(argv[optind + 1], intervalTimeMs)) {
894                                     std::cout << "input coordinate or time error" << std::endl;
895                                     return RET_ERR;
896                                 }
897                                 const int64_t minIntervalTimeMs = 1;
898                                 const int64_t maxIntervalTimeMs = 450;
899                                 if ((minIntervalTimeMs > intervalTimeMs) || (maxIntervalTimeMs < intervalTimeMs)) {
900                                     std::cout << "interval time is out of range: " << minIntervalTimeMs << "ms";
901                                     std::cout << " < interval time < " << maxIntervalTimeMs << "ms" << std::endl;
902                                     return RET_ERR;
903                                 }
904                             } else {
905                                 std::cout << "parameter error, unable to run" << std::endl;
906                                 return RET_ERR;
907                             }
908                             if ((px1 < 0) || (py1 < 0)) {
909                                 std::cout << "Coordinate value must be greater than 0" << std::endl;
910                                 return RET_ERR;
911                             }
912                             std::cout << "   click coordinate: ("<< px1 << ", "  << py1 << ")" << std::endl;
913                             std::cout << "click interval time: " << intervalTimeMs      << "ms" << std::endl;
914                             auto pointerEvent = PointerEvent::Create();
915                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
916                             PointerEvent::PointerItem item;
917                             item.SetPointerId(0);
918                             item.SetPressed(true);
919                             item.SetDisplayX(px1);
920                             item.SetDisplayY(py1);
921                             pointerEvent->SetPointerId(0);
922                             pointerEvent->AddPointerItem(item);
923                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
924                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
925                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
926                             std::this_thread::sleep_for(std::chrono::milliseconds(intervalTimeMs));
927 
928                             item.SetPressed(false);
929                             item.SetDisplayX(px1);
930                             item.SetDisplayY(py1);
931                             pointerEvent->UpdatePointerItem(0, item);
932                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
933                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
934                             break;
935                         }
936                         case 'i': {
937                             int32_t taktTime = 0;
938                             if (!StrToInt(optarg, taktTime)) {
939                                 std::cout << "invalid command to interval time" << std::endl;
940                                 return EVENT_REG_FAIL;
941                             }
942                             const int64_t minTaktTimeMs = 1;
943                             const int64_t maxTaktTimeMs = 15000;
944                             if ((minTaktTimeMs > taktTime) || (maxTaktTimeMs < taktTime)) {
945                                 std::cout << "taktTime is out of range. ";
946                                 std::cout << minTaktTimeMs << " < taktTime < " << maxTaktTimeMs;
947                                 std::cout << std::endl;
948                                 return EVENT_REG_FAIL;
949                             }
950                             std::this_thread::sleep_for(std::chrono::milliseconds(taktTime));
951                             break;
952                         }
953                         case 'g': {
954                             const int32_t dragArgcSeven = 7;
955                             const int32_t dragArgcCommandNine = 9;
956                             if ((argc != dragArgcSeven) && (argc != dragArgcCommandNine)) {
957                                 std::cout << "argc:" << argc << std::endl;
958                                 std::cout << "wrong number of parameters" << std::endl;
959                                 return RET_ERR;
960                             }
961                             totalTimeMs = 1000;
962                             int32_t pressTimems = 500;
963                             if (argc == moveArgcSeven) {
964                                 if ((!StrToInt(optarg, px1)) ||
965                                     (!StrToInt(argv[optind], py1)) ||
966                                     (!StrToInt(argv[optind + 1], px2)) ||
967                                     (!StrToInt(argv[optind + 2], py2))) {
968                                         std::cout << "invalid coordinate value" << std::endl;
969                                         return RET_ERR;
970                                 }
971                             } else {
972                                 if ((!StrToInt(optarg, px1)) ||
973                                     (!StrToInt(argv[optind], py1)) ||
974                                     (!StrToInt(argv[optind + 1], px2)) ||
975                                     (!StrToInt(argv[optind + 2], py2)) ||
976                                     (!StrToInt(argv[optind + 3], pressTimems)) ||
977                                     (!StrToInt(argv[optind + 4], totalTimeMs))) {
978                                         std::cout << "invalid input coordinate or time" << std::endl;
979                                         return RET_ERR;
980                                 }
981                             }
982                             if ((px1 < 0) || (py1 < 0) || (px2 < 0) || (py2 < 0)) {
983                                 std::cout << "Coordinate value must be greater than 0" << std::endl;
984                                 return RET_ERR;
985                             }
986                             const int32_t minTotalTimeMs = 1000;
987                             const int32_t maxTotalTimeMs = 15000;
988                             if ((minTotalTimeMs > totalTimeMs) || (maxTotalTimeMs < totalTimeMs)) {
989                                 std::cout << "total time input is error" << std::endl;
990                                 return RET_ERR;
991                             }
992                             const int32_t minPressTimeMs = 500;
993                             const int32_t maxPressTimeMs = 14500;
994                             if ((minPressTimeMs > pressTimems) || (maxPressTimeMs < pressTimems)) {
995                                 std::cout << "press time is out of range" << std::endl;
996                                 return RET_ERR;
997                             }
998                             const int32_t minMoveTimeMs = 500;
999                             if ((totalTimeMs -  pressTimems) <  minMoveTimeMs) {
1000                                 std::cout << "move time is out of range" << std::endl;
1001                                 return RET_ERR;
1002                             }
1003                             auto pointerEvent = PointerEvent::Create();
1004                             CHKPR(pointerEvent, ERROR_NULL_POINTER);
1005                             PointerEvent::PointerItem item;
1006                             item.SetDisplayX(px1);
1007                             item.SetDisplayY(py1);
1008                             pointerEvent->AddPointerItem(item);
1009                             pointerEvent->SetPointerId(0);
1010                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
1011                             pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
1012                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1013                             const int32_t conversionRate = 1000;
1014                             int64_t startTimeMs = GetSysClockTime() / conversionRate;
1015                             int64_t endTimeMs = 0;
1016                             if (!AddInt64(startTimeMs, totalTimeMs, endTimeMs)) {
1017                                 std::cout << "end time count error" << std::endl;
1018                                 return RET_ERR;
1019                             }
1020                             int64_t downTimeMs = 0;
1021                             if (!AddInt64(startTimeMs, pressTimems, downTimeMs)) {
1022                                 std::cout << "down time count error" << std::endl;
1023                                 return RET_ERR;
1024                             }
1025                             int64_t currentTimeMs = startTimeMs;
1026                             const int32_t moveTimeMs = totalTimeMs - pressTimems;
1027                             while ((currentTimeMs < endTimeMs)) {
1028                                 if (currentTimeMs > downTimeMs) {
1029                                     item.SetDisplayX(NextPos(downTimeMs, currentTimeMs, moveTimeMs, px1, px2));
1030                                     item.SetDisplayY(NextPos(downTimeMs, currentTimeMs, moveTimeMs, py1, py2));
1031                                     pointerEvent->UpdatePointerItem(0, item);
1032                                     pointerEvent->SetActionTime(currentTimeMs);
1033                                     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
1034                                     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1035                                 }
1036                                 std::this_thread::sleep_for(std::chrono::milliseconds(BLOCK_TIME_MS));
1037                                 currentTimeMs = GetSysClockTime() / conversionRate;
1038                             }
1039                             item.SetDisplayX(px2);
1040                             item.SetDisplayY(py2);
1041                             pointerEvent->UpdatePointerItem(0, item);
1042                             pointerEvent->SetActionTime(endTimeMs);
1043                             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
1044                             InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1045                             break;
1046                         }
1047                         default: {
1048                             std::cout << "invalid command" << std::endl;
1049                             ShowUsage();
1050                             return EVENT_REG_FAIL;
1051                         }
1052                     }
1053                     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
1054                 }
1055                 break;
1056             }
1057             case '?': {
1058                 ShowUsage();
1059                 return ERR_OK;
1060             }
1061             default: {
1062                 std::cout << "invalid command" << std::endl;
1063                 ShowUsage();
1064                 return EVENT_REG_FAIL;
1065             }
1066         }
1067     } else {
1068         std::cout << "too few arguments to function" << std::endl;
1069         ShowUsage();
1070         return EVENT_REG_FAIL;
1071     }
1072     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
1073     return ERR_OK;
1074 }
1075 
ShowUsage()1076 void InputManagerCommand::ShowUsage()
1077 {
1078     std::cout << "Usage: uinput <option> <command> <arg>..." << std::endl;
1079     std::cout << "The option are:                                " << std::endl;
1080     std::cout << "-M  --mouse                                    " << std::endl;
1081     std::cout << "commands for mouse:                            " << std::endl;
1082     std::cout << "-m <dx> <dy>              --move   <dx> <dy>  -move to relative position (dx,dy),"    << std::endl;
1083     std::cout << "   <dx1> <dy1> <dx2> <dy2> [soomth time] --trace -dx1 dy1 to dx2 dy2 smooth movement" << std::endl;
1084     std::cout << "-d <key>                  --down   key        -press down a button, "                 << std::endl;
1085     std::cout << "                                               0 is the left button, 1 is the right," << std::endl;
1086     std::cout << "                                               2 is the middle"   << std::endl;
1087     std::cout << "-u <key>                  --up     <key>      -release a button " << std::endl;
1088     std::cout << "-c <key>                  --click  <key>      -press the left button down,then raise" << std::endl;
1089     std::cout << "-b <dx1> <dy1> <id> [press time] [click interval time]                --double click" << std::endl;
1090     std::cout << "  [press time] the time range is more than 1ms but less than 300ms, "       << std::endl;
1091     std::cout << "  [click interval time] the time range is more than 1ms but less than 450ms, " << std::endl;
1092     std::cout << "  Otherwise the operation result may produce error or invalid operation"       << std::endl;
1093     std::cout << " -press the left button down,then raise" << std::endl;
1094     std::cout << "   key value:0 - button left"     << std::endl;
1095     std::cout << "   key value:1 - button right"    << std::endl;
1096     std::cout << "   key value:2 - button middle"   << std::endl;
1097     std::cout << "   key value:3 - button side"     << std::endl;
1098     std::cout << "   key value:4 - button extra"    << std::endl;
1099     std::cout << "   key value:5 - button forward"  << std::endl;
1100     std::cout << "   key value:6 - button back"     << std::endl;
1101     std::cout << "   key value:7 - button task"     << std::endl;
1102     std::cout << "-s <key>                  --scroll <key>      -positive values are sliding backwards" << std::endl;
1103     std::cout << "-i <time>                 --interval <time>   -the program interval for the (time) milliseconds";
1104     std::cout << std::endl;
1105     std::cout << "                                               negative values are sliding forwards"  << std::endl;
1106     std::cout << std::endl;
1107     std::cout << "-K  --keyboard                                                " << std::endl;
1108     std::cout << "commands for keyboard:                                        " << std::endl;
1109     std::cout << "-d <key>                   --down   <key>     -press down a key" << std::endl;
1110     std::cout << "-u <key>                   --up     <key>     -release a key   " << std::endl;
1111     std::cout << "-l <key> [long press time] --long_press <key> [long press time] -press and hold the key";
1112     std::cout << std::endl;
1113     std::cout << "-i <time>                  --interval <time>  -the program interval for the (time) milliseconds";
1114     std::cout << std::endl;
1115     std::cout << std::endl;
1116     std::cout << "-T  --touch                                                   " << std::endl;
1117     std::cout << "commands for touch:                                           " << std::endl;
1118     std::cout << "-d <dx1> <dy1>             --down   <dx1> <dy1> -press down a position  dx1 dy1, " << std::endl;
1119     std::cout << "-u <dx1> <dy1>             --up     <dx1> <dy1> -release a position dx1 dy1, "     << std::endl;
1120     std::cout << "-m <dx1> <dy1> <dx2> <dy2> [smooth time]      --smooth movement"   << std::endl;
1121     std::cout << "   <dx1> <dy1> <dx2> <dy2> [smooth time]      -smooth movement, "  << std::endl;
1122     std::cout << "                                              dx1 dy1 to dx2 dy2 smooth movement"  << std::endl;
1123     std::cout << "-c <dx1> <dy1> [click interval]               -touch screen click dx1 dy1"         << std::endl;
1124     std::cout << "-i <time>                  --interval <time>  -the program interval for the (time) milliseconds";
1125     std::cout << std::endl;
1126     std::cout << "-g <dx1> <dy1> <dx2> <dy2> [press time] [total time]     -drag, "                       << std::endl;
1127     std::cout << "  [Press time] not less than 500ms and [total time] - [Press time] not less than 500ms" << std::endl;
1128     std::cout << "  Otherwise the operation result may produce error or invalid operation"                << std::endl;
1129     std::cout << "                                                              " << std::endl;
1130     std::cout << "-?  --help                                                    " << std::endl;
1131 }
1132 } // namespace MMI
1133 } // namespace OHOS
1134