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 JOYSTICK_BUTTON_ID = 25;
57 constexpr int32_t TWO_MORE_COMMAND = 2;
58 constexpr int32_t THREE_MORE_COMMAND = 3;
59 constexpr int32_t MAX_PRESSED_COUNT = 30;
60 constexpr int32_t ACTION_TIME = 3000;
61 constexpr int32_t BLOCK_TIME_MS = 10;
62 constexpr int32_t TIME_TRANSITION = 1000;
63 constexpr int64_t MIN_TAKTTIME_MS = 1;
64 constexpr int64_t MAX_TAKTTIME_MS = 15000;
65 constexpr int32_t DEFAULT_DELAY = 200;
66 constexpr int32_t KNUCKLE_PARAM_SIZE = 9;
67 enum JoystickEvent {
68 JOYSTICK_BUTTON_UP,
69 JOYSTICK_BUTTON_PRESS,
70 JOYSTICK_MOVE,
71 JOYSTICK_CLICK,
72 JOYSTICK_INTERVAL
73 };
74 struct JoystickInfo {
75 int32_t buttonId { -1 };
76 int32_t absValue { -1 };
77 int32_t taktTime { 0 };
78 PointerEvent::AxisType absType;
79 };
80 } // namespace
81
SleepAndUpdateTime(int64_t & currentTimeMs)82 void InputManagerCommand::SleepAndUpdateTime(int64_t ¤tTimeMs)
83 {
84 int64_t nowEndSysTimeMs = GetSysClockTime() / TIME_TRANSITION;
85 int64_t sleepTimeMs = BLOCK_TIME_MS - (nowEndSysTimeMs - currentTimeMs) % BLOCK_TIME_MS;
86 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs));
87 currentTimeMs = nowEndSysTimeMs + sleepTimeMs;
88 }
89
NextPos(int64_t begTimeMs,int64_t curtTimeMs,int32_t totalTimeMs,int32_t begPos,int32_t endPos)90 int32_t InputManagerCommand::NextPos(int64_t begTimeMs, int64_t curtTimeMs, int32_t totalTimeMs,
91 int32_t begPos, int32_t endPos)
92 {
93 int64_t endTimeMs = 0;
94 if (!AddInt64(begTimeMs, totalTimeMs, endTimeMs)) {
95 return begPos;
96 }
97 if (curtTimeMs < begTimeMs || curtTimeMs > endTimeMs) {
98 return begPos;
99 }
100 if (totalTimeMs == 0) {
101 std::cout << "invalid totalTimeMs" << std::endl;
102 return begPos;
103 }
104 double tmpTimeMs = static_cast<double>(curtTimeMs - begTimeMs) / totalTimeMs;
105 int32_t offsetPos = std::ceil(tmpTimeMs * (endPos - begPos));
106 int32_t retPos = 0;
107 if (offsetPos == 0) {
108 return begPos;
109 } else if (offsetPos > 0) {
110 if (!AddInt32(begPos, offsetPos, retPos)) {
111 return begPos;
112 }
113 return retPos > endPos ? endPos : retPos;
114 }
115 if (!AddInt32(begPos, offsetPos, retPos)) {
116 return begPos;
117 }
118 return retPos < endPos ? endPos : retPos;
119 }
120
ParseCommand(int32_t argc,char * argv[])121 int32_t InputManagerCommand::ParseCommand(int32_t argc, char *argv[])
122 {
123 struct option headOptions[] = {
124 {"mouse", no_argument, nullptr, 'M'},
125 {"keyboard", no_argument, nullptr, 'K'},
126 {"touch", no_argument, nullptr, 'T'},
127 {"joystick", no_argument, nullptr, 'J'},
128 {"help", no_argument, nullptr, '?'},
129 {nullptr, 0, nullptr, 0}
130 };
131
132 struct option mouseSensorOptions[] = {
133 {"move", required_argument, nullptr, 'm'},
134 {"click", required_argument, nullptr, 'c'},
135 {"double_click", required_argument, nullptr, 'b'},
136 {"down", required_argument, nullptr, 'd'},
137 {"up", required_argument, nullptr, 'u'},
138 {"scroll", required_argument, nullptr, 's'},
139 {"drag", required_argument, nullptr, 'g'},
140 {"interval", required_argument, nullptr, 'i'},
141 {nullptr, 0, nullptr, 0}
142 };
143 struct option keyboardSensorOptions[] = {
144 {"down", required_argument, nullptr, 'd'},
145 {"up", required_argument, nullptr, 'u'},
146 {"long_press", required_argument, nullptr, 'l'},
147 {"interval", required_argument, nullptr, 'i'},
148 {nullptr, 0, nullptr, 0}
149 };
150 struct option touchSensorOptions[] = {
151 {"move", required_argument, nullptr, 'm'},
152 {"down", required_argument, nullptr, 'd'},
153 {"up", required_argument, nullptr, 'u'},
154 {"click", required_argument, nullptr, 'c'},
155 {"interval", required_argument, nullptr, 'i'},
156 {"drag", required_argument, nullptr, 'g'},
157 {"knuckle", no_argument, nullptr, 'k'},
158 {nullptr, 0, nullptr, 0}
159 };
160 struct option joystickSensorOptions[] = {
161 {"move", required_argument, nullptr, 'm'},
162 {"down", required_argument, nullptr, 'd'},
163 {"up", required_argument, nullptr, 'u'},
164 {"click", required_argument, nullptr, 'c'},
165 {"interval", required_argument, nullptr, 'i'},
166 {nullptr, 0, nullptr, 0}
167 };
168 int32_t c = 0;
169 int32_t optionIndex = 0;
170 optind = 0;
171 if ((c = getopt_long(argc, argv, "MKTJ?", headOptions, &optionIndex)) != -1) {
172 switch (c) {
173 case 'M': {
174 int32_t px = 0;
175 int32_t py = 0;
176 int32_t buttonId;
177 int32_t scrollValue;
178 while ((c = getopt_long(argc, argv, "m:d:u:c:b:s:g:i:", mouseSensorOptions, &optionIndex)) != -1) {
179 switch (c) {
180 case 'm': {
181 if (argc - optind < 1) {
182 std::cout << "too few arguments to function" << std::endl;
183 return RET_ERR;
184 }
185 auto isTraceOption = [](const std::string &opt1) {
186 return opt1 == std::string("--trace");
187 };
188 auto traceMode = [isTraceOption](int32_t argCount, char *argvOffset[]) -> bool {
189 if (argCount <= 3) {
190 return false;
191 }
192 std::string arg3 = argvOffset[2];
193 if (!arg3.empty() && arg3.at(0) == '-') {
194 return false;
195 }
196 if ((argCount >= 5) && isTraceOption(std::string(argvOffset[4]))) {
197 return true;
198 }
199 if ((argCount >= 6) && isTraceOption(std::string(argvOffset[5]))) {
200 return true;
201 }
202 return false;
203 }(argc - optind + 1, &argv[optind - 1]);
204 if (!traceMode) {
205 if (!StrToInt(optarg, px) || !StrToInt(argv[optind], py)) {
206 std::cout << "invalid parameter to move mouse" << std::endl;
207 return RET_ERR;
208 }
209 if ((px < 0) || (py < 0)) {
210 std::cout << "Coordinate value must be greater than 0" << std::endl;
211 return RET_ERR;
212 }
213 std::cout << "move to " << px << " " << py << std::endl;
214 auto pointerEvent = PointerEvent::Create();
215 CHKPR(pointerEvent, ERROR_NULL_POINTER);
216 PointerEvent::PointerItem item;
217 item.SetPointerId(0);
218 item.SetDisplayX(px);
219 item.SetDisplayY(py);
220 pointerEvent->AddPointerItem(item);
221 pointerEvent->SetPointerId(0);
222 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
223 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
224 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
225 optind++;
226 } else {
227 int32_t px1 = 0;
228 int32_t py1 = 0;
229 int32_t px2 = 0;
230 int32_t py2 = 0;
231 int32_t totalTimeMs = 1000;
232 bool foundTraceOption = false;
233 if (argc - optind >= 3) {
234 if ((!StrToInt(optarg, px1)) ||
235 (!StrToInt(argv[optind], py1)) ||
236 (!StrToInt(argv[optind + 1], px2)) ||
237 (!StrToInt(argv[optind + 2], py2))) {
238 std::cout << "invalid coordinate value" << std::endl;
239 return RET_ERR;
240 }
241 optind += 3;
242 }
243 if ((px1 < 0) || (py1 < 0) || (px2 < 0) || (py2 < 0)) {
244 std::cout << "Coordinate value must be greater than 0" << std::endl;
245 return RET_ERR;
246 }
247 if (argc - optind >= 1) {
248 std::string arg5 = argv[optind];
249 if (!arg5.empty() && arg5.at(0) == '-') {
250 if (isTraceOption(arg5)) {
251 foundTraceOption = true;
252 } else {
253 std::cout << "invalid option, the 5th position parameter must be --trace"
254 << std::endl;
255 return RET_ERR;
256 }
257 } else if (!StrToInt(arg5, totalTimeMs)) {
258 std::cout << "invalid total times" << std::endl;
259 return RET_ERR;
260 }
261 optind++;
262 }
263 if (!foundTraceOption) {
264 if (argc - optind < 1) {
265 std::cout << "missing 6th position parameter --trace" << std::endl;
266 return RET_ERR;
267 }
268 std::string arg6 = argv[optind];
269 if (!isTraceOption(arg6)) {
270 std::cout << "invalid option, the 6th position parameter must be --trace"
271 << std::endl;
272 return RET_ERR;
273 }
274 optind++;
275 foundTraceOption = true;
276 }
277 static const int64_t minTotalTimeMs = 1;
278 static const int64_t maxTotalTimeMs = 15000;
279 if ((totalTimeMs < minTotalTimeMs) || (totalTimeMs > maxTotalTimeMs)) {
280 std::cout << "total time is out of range:"
281 << minTotalTimeMs << " <= " << totalTimeMs << " <= " << maxTotalTimeMs
282 << std::endl;
283 return RET_ERR;
284 }
285 std::cout << "start coordinate: (" << px1 << ", " << py1 << ")" << std::endl;
286 std::cout << " end coordinate: (" << px2 << ", " << py2 << ")" << std::endl;
287 std::cout << " total times: " << totalTimeMs << " ms" << std::endl;
288 std::cout << " trace mode: " << std::boolalpha << foundTraceOption << std::endl;
289 auto pointerEvent = PointerEvent::Create();
290 CHKPR(pointerEvent, ERROR_NULL_POINTER);
291 px = px1;
292 py = py1;
293 PointerEvent::PointerItem item;
294 item.SetPointerId(0);
295 item.SetDisplayX(px);
296 item.SetDisplayY(py);
297 pointerEvent->SetPointerId(0);
298 pointerEvent->AddPointerItem(item);
299 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
300 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
301 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
302
303 int64_t startTimeUs = GetSysClockTime();
304 int64_t startTimeMs = startTimeUs / TIME_TRANSITION;
305 int64_t endTimeMs = 0;
306 if (!AddInt64(startTimeMs, totalTimeMs, endTimeMs)) {
307 std::cout << "system time error" << std::endl;
308 return RET_ERR;
309 }
310 int64_t currentTimeMs = startTimeMs;
311 while (currentTimeMs < endTimeMs) {
312 item.SetDisplayX(NextPos(startTimeMs, currentTimeMs, totalTimeMs, px1, px2));
313 item.SetDisplayY(NextPos(startTimeMs, currentTimeMs, totalTimeMs, py1, py2));
314 pointerEvent->SetActionTime(currentTimeMs);
315 pointerEvent->UpdatePointerItem(0, item);
316 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
317 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
318 SleepAndUpdateTime(currentTimeMs);
319 }
320
321 px = px2;
322 py = py2;
323 item.SetDisplayX(px);
324 item.SetDisplayY(py);
325 pointerEvent->SetActionTime(endTimeMs);
326 pointerEvent->UpdatePointerItem(0, item);
327 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
328 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
329 }
330 break;
331 }
332 case 'd': {
333 if (!StrToInt(optarg, buttonId)) {
334 std::cout << "invalid button press command" << std::endl;
335 return EVENT_REG_FAIL;
336 }
337 if (buttonId > MOUSE_ID) {
338 std::cout << "invalid button press command" << std::endl;
339 return EVENT_REG_FAIL;
340 }
341 std::cout << "press down" << buttonId << std::endl;
342 auto pointerEvent = PointerEvent::Create();
343 CHKPR(pointerEvent, ERROR_NULL_POINTER);
344 PointerEvent::PointerItem item;
345 item.SetPointerId(0);
346 item.SetDisplayX(px);
347 item.SetDisplayY(py);
348 item.SetPressed(true);
349 pointerEvent->SetPointerId(0);
350 pointerEvent->AddPointerItem(item);
351 pointerEvent->SetButtonId(buttonId);
352 pointerEvent->SetButtonPressed(buttonId);
353 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
354 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
355 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
356 break;
357 }
358 case 'u': {
359 if (!StrToInt(optarg, buttonId)) {
360 std::cout << "invalid raise button command" << std::endl;
361 return EVENT_REG_FAIL;
362 }
363 if (buttonId > MOUSE_ID) {
364 std::cout << "invalid raise button command" << std::endl;
365 return EVENT_REG_FAIL;
366 }
367 std::cout << "lift up button " << buttonId << std::endl;
368 auto pointerEvent = PointerEvent::Create();
369 CHKPR(pointerEvent, ERROR_NULL_POINTER);
370 PointerEvent::PointerItem item;
371 item.SetPointerId(0);
372 item.SetDisplayX(px);
373 item.SetDisplayY(py);
374 item.SetPressed(false);
375 pointerEvent->SetPointerId(0);
376 pointerEvent->AddPointerItem(item);
377 pointerEvent->SetButtonPressed(buttonId);
378 pointerEvent->SetButtonId(buttonId);
379 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
380 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
381 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
382 break;
383 }
384 case 's': {
385 if (!StrToInt(optarg, scrollValue)) {
386 std::cout << "invalid scroll button command" << std::endl;
387 return EVENT_REG_FAIL;
388 }
389 std::cout << "scroll wheel " << scrollValue << std::endl;
390 auto pointerEvent = PointerEvent::Create();
391 CHKPR(pointerEvent, ERROR_NULL_POINTER);
392 PointerEvent::PointerItem item;
393 item.SetPointerId(0);
394 item.SetDisplayX(px);
395 item.SetDisplayY(py);
396 item.SetPressed(false);
397 int64_t time = pointerEvent->GetActionStartTime();
398 pointerEvent->SetActionTime(time + ACTION_TIME);
399 pointerEvent->SetPointerId(0);
400 pointerEvent->AddPointerItem(item);
401 pointerEvent->SetButtonPressed(buttonId);
402 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_BEGIN);
403 pointerEvent->SetAxisValue(PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL,
404 scrollValue);
405 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
406 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
407
408 time = pointerEvent->GetActionStartTime();
409 pointerEvent->SetActionTime(time + ACTION_TIME);
410 pointerEvent->SetPointerId(0);
411 pointerEvent->AddPointerItem(item);
412 pointerEvent->SetButtonPressed(buttonId);
413 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
414 pointerEvent->SetAxisValue(PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL,
415 scrollValue);
416 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
417 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
418
419 time = pointerEvent->GetActionStartTime();
420 pointerEvent->SetActionTime(time + ACTION_TIME);
421 pointerEvent->SetPointerId(0);
422 pointerEvent->AddPointerItem(item);
423 pointerEvent->SetButtonPressed(buttonId);
424 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
425 pointerEvent->SetAxisValue(PointerEvent::AxisType::AXIS_TYPE_SCROLL_VERTICAL,
426 scrollValue);
427 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
428 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
429 break;
430 }
431 case 'c': {
432 if (!StrToInt(optarg, buttonId)) {
433 std::cout << "invalid click button command" << std::endl;
434 return EVENT_REG_FAIL;
435 }
436 if (buttonId > MOUSE_ID) {
437 std::cout << "invalid button press command" << std::endl;
438 return EVENT_REG_FAIL;
439 }
440 std::cout << "click " << buttonId << std::endl;
441 auto pointerEvent = PointerEvent::Create();
442 CHKPR(pointerEvent, ERROR_NULL_POINTER);
443 PointerEvent::PointerItem item;
444 item.SetDisplayY(py);
445 item.SetPressed(true);
446 item.SetPointerId(0);
447 item.SetDisplayX(px);
448 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
449 pointerEvent->AddPointerItem(item);
450 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
451 pointerEvent->SetButtonId(buttonId);
452 pointerEvent->SetButtonPressed(buttonId);
453 pointerEvent->SetPointerId(0);
454 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
455 item.SetPointerId(0);
456 item.SetPressed(false);
457 item.SetDisplayX(px);
458 item.SetDisplayY(py);
459 pointerEvent->SetPointerId(0);
460 pointerEvent->UpdatePointerItem(0, item);
461 pointerEvent->SetButtonPressed(buttonId);
462 pointerEvent->SetButtonId(buttonId);
463 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
464 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
465 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
466 break;
467 }
468 case 'b': {
469 int32_t pressTimeMs = 50;
470 int32_t clickIntervalTimeMs = 300;
471 static constexpr int32_t minButtonId = 0;
472 static constexpr int32_t maxButtonId = 7;
473 static constexpr int32_t minPressTimeMs = 1;
474 static constexpr int32_t maxPressTimeMs = 300;
475 static constexpr int32_t minClickIntervalTimeMs = 1;
476 static constexpr int32_t maxClickIntervalTimeMs = 450;
477 if (argc < 6 || argc > 8) {
478 std::cout << "wrong number of parameters" << std::endl;
479 return RET_ERR;
480 }
481 if (!StrToInt(optarg, px) ||
482 !StrToInt(argv[optind], py)) {
483 std::cout << "invalid coordinate value" << std::endl;
484 return RET_ERR;
485 }
486 if ((px < 0) || (py < 0)) {
487 std::cout << "Coordinate value must be greater than 0" << std::endl;
488 return RET_ERR;
489 }
490 if (!StrToInt(argv[optind + 1], buttonId)) {
491 std::cout << "invalid key" << std::endl;
492 return RET_ERR;
493 }
494 if (argc >= 7) {
495 if (!StrToInt(argv[optind + 2], pressTimeMs)) {
496 std::cout << "invalid press time" << std::endl;
497 return RET_ERR;
498 }
499 }
500 if (argc == 8) {
501 if (!StrToInt(argv[optind + 3], clickIntervalTimeMs)) {
502 std::cout << "invalid interval between hits" << std::endl;
503 return RET_ERR;
504 }
505 }
506 if ((buttonId < minButtonId) || (buttonId > maxButtonId)) {
507 std::cout << "button is out of range:" << minButtonId << " < " << buttonId << " < "
508 << maxButtonId << std::endl;
509 return RET_ERR;
510 }
511 if ((pressTimeMs < minPressTimeMs) || (pressTimeMs > maxPressTimeMs)) {
512 std::cout << "press time is out of range:" << minPressTimeMs << " ms" << " < "
513 << pressTimeMs << " < " << maxPressTimeMs << " ms" << std::endl;
514 return RET_ERR;
515 }
516 if ((clickIntervalTimeMs < minClickIntervalTimeMs) ||
517 (clickIntervalTimeMs > maxClickIntervalTimeMs)) {
518 std::cout << "click interval time is out of range:" << minClickIntervalTimeMs << " ms"
519 " < " << clickIntervalTimeMs << " < " << maxClickIntervalTimeMs << " ms"
520 << std::endl;
521 return RET_ERR;
522 }
523 std::cout << " coordinate: ("<< px << ", " << py << ")" << std::endl;
524 std::cout << " button id: " << buttonId << std::endl;
525 std::cout << " press time: " << pressTimeMs << " ms" << std::endl;
526 std::cout << "interval time: " << clickIntervalTimeMs << " ms" << std::endl;
527 auto pointerEvent = PointerEvent::Create();
528 CHKPR(pointerEvent, ERROR_NULL_POINTER);
529 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
530 PointerEvent::PointerItem item;
531 item.SetPressed(true);
532 item.SetPointerId(0);
533 item.SetDisplayX(px);
534 item.SetDisplayY(py);
535 pointerEvent->SetPointerId(0);
536 pointerEvent->SetButtonId(buttonId);
537 pointerEvent->SetButtonPressed(buttonId);
538 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
539 pointerEvent->AddPointerItem(item);
540 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
541 std::this_thread::sleep_for(std::chrono::milliseconds(pressTimeMs));
542 item.SetPressed(false);
543 pointerEvent->UpdatePointerItem(0, item);
544 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
545 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
546 std::this_thread::sleep_for(std::chrono::milliseconds(clickIntervalTimeMs));
547
548 item.SetPressed(true);
549 pointerEvent->UpdatePointerItem(0, item);
550 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
551 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
552 std::this_thread::sleep_for(std::chrono::milliseconds(pressTimeMs));
553 item.SetPressed(false);
554 pointerEvent->UpdatePointerItem(0, item);
555 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
556 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
557 break;
558 }
559 case 'g': {
560 int32_t px1 = 0;
561 int32_t py1 = 0;
562 int32_t px2 = 0;
563 int32_t py2 = 0;
564 int32_t buttonsId = 0;
565 int32_t totalTimeMs = 1000;
566 if (argc < 7) {
567 std::cout << "argc:" << argc << std::endl;
568 std::cout << "Wrong number of parameters" << std::endl;
569 return RET_ERR;
570 }
571 if (argc >= 7) {
572 if ((!StrToInt(optarg, px1)) ||
573 (!StrToInt(argv[optind], py1)) ||
574 (!StrToInt(argv[optind + 1], px2)) ||
575 (!StrToInt(argv[optind + 2], py2))) {
576 std::cout << "Invalid coordinate value" << std::endl;
577 return RET_ERR;
578 }
579 }
580 if ((px1 < 0) || (py1 < 0) || (px2 < 0) || (py2 < 0)) {
581 std::cout << "Coordinate value must be greater than 0" << std::endl;
582 return RET_ERR;
583 }
584 if (argc >= 8) {
585 if (!StrToInt(argv[optind + 3], totalTimeMs)) {
586 std::cout << "Invalid total times" << std::endl;
587 return RET_ERR;
588 }
589 }
590 static const int64_t minTotalTimeMs = 1;
591 static const int64_t maxTotalTimeMs = 15000;
592 if ((totalTimeMs < minTotalTimeMs) || (totalTimeMs > maxTotalTimeMs)) {
593 std::cout << "Total time is out of range:"
594 << minTotalTimeMs << "ms" << " <= " << totalTimeMs << "ms" << " <= "
595 << maxTotalTimeMs << "ms" << std::endl;
596 return RET_ERR;
597 }
598 std::cout << "start coordinate: (" << px1 << ", " << py1 << ")" << std::endl;
599 std::cout << " end coordinate: (" << px2 << ", " << py2 << ")" << std::endl;
600 std::cout << " total time: " << totalTimeMs << "ms" << std::endl;
601 auto pointerEvent = PointerEvent::Create();
602 CHKPR(pointerEvent, ERROR_NULL_POINTER);
603 PointerEvent::PointerItem item;
604 item.SetDisplayY(py1);
605 item.SetDisplayX(px1);
606 item.SetPressed(false);
607 item.SetPointerId(0);
608 pointerEvent->SetButtonPressed(0);
609 pointerEvent->AddPointerItem(item);
610 pointerEvent->SetButtonId(buttonsId);
611 pointerEvent->SetButtonPressed(buttonsId);
612 pointerEvent->SetPointerId(0);
613 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
614 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
615 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
616
617 int64_t startTimeMs = GetSysClockTime() / TIME_TRANSITION;
618 int64_t endTimeMs = 0;
619 if (!AddInt64(startTimeMs, totalTimeMs, endTimeMs)) {
620 std::cout << "System time error" << std::endl;
621 return RET_ERR;
622 }
623 int64_t currentTimeMs = startTimeMs;
624 while (currentTimeMs < endTimeMs) {
625 item.SetDisplayX(NextPos(startTimeMs, currentTimeMs, totalTimeMs, px1, px2));
626 item.SetDisplayY(NextPos(startTimeMs, currentTimeMs, totalTimeMs, py1, py2));
627 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
628 pointerEvent->UpdatePointerItem(0, item);
629 pointerEvent->SetActionTime(currentTimeMs);
630 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
631 SleepAndUpdateTime(currentTimeMs);
632 }
633 item.SetDisplayY(py2);
634 item.SetDisplayX(px2);
635 pointerEvent->UpdatePointerItem(0, item);
636 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
637 pointerEvent->SetActionTime(endTimeMs);
638 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
639 std::this_thread::sleep_for(std::chrono::milliseconds(BLOCK_TIME_MS));
640
641 item.SetPressed(true);
642 item.SetDisplayY(py2);
643 item.SetDisplayX(px2);
644 pointerEvent->UpdatePointerItem(0, item);
645 pointerEvent->SetActionTime(endTimeMs);
646 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
647 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
648 break;
649 }
650 case 'i': {
651 int32_t tookTime = 0;
652 if (!StrToInt(optarg, tookTime)) {
653 std::cout << "invalid command to interval time" << std::endl;
654 return EVENT_REG_FAIL;
655 }
656 const int64_t minTaktTimeMs = 1;
657 const int64_t maxTaktTimeMs = 15000;
658 if ((minTaktTimeMs > tookTime) || (maxTaktTimeMs < tookTime)) {
659 std::cout << "tookTime is out of range" << std::endl;
660 std::cout << minTaktTimeMs << " < tookTime < " << maxTaktTimeMs;
661 std::cout << std::endl;
662 return EVENT_REG_FAIL;
663 }
664 std::this_thread::sleep_for(std::chrono::milliseconds(tookTime));
665 break;
666 }
667 default: {
668 std::cout << "invalid command to virtual mouse" << std::endl;
669 ShowUsage();
670 return EVENT_REG_FAIL;
671 }
672 }
673 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
674 }
675 break;
676 }
677 case 'K': {
678 std::vector<int32_t> downKey;
679 int32_t keyCode = 0;
680 int32_t isCombinationKey = 0;
681 int64_t time = GetSysClockTime();
682 while ((c = getopt_long(argc, argv, "d:u:l:i:", keyboardSensorOptions, &optionIndex)) != -1) {
683 switch (c) {
684 case 'd': {
685 if (!StrToInt(optarg, keyCode)) {
686 std::cout << "invalid command to down key" << std::endl;
687 }
688 if (optind == isCombinationKey + TWO_MORE_COMMAND) {
689 downKey.push_back(keyCode);
690 isCombinationKey = optind;
691 auto KeyEvent = KeyEvent::Create();
692 CHKPR(KeyEvent, ERROR_NULL_POINTER);
693 if (downKey.size() > MAX_PRESSED_COUNT) {
694 std::cout << "pressed button count should less than 30" << std::endl;
695 return EVENT_REG_FAIL;
696 }
697 KeyEvent::KeyItem item[downKey.size()];
698 for (size_t i = 0; i < downKey.size(); i++) {
699 KeyEvent->SetKeyCode(keyCode);
700 KeyEvent->SetActionTime(time);
701 KeyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
702 item[i].SetKeyCode(downKey[i]);
703 item[i].SetDownTime(time);
704 item[i].SetPressed(true);
705 KeyEvent->AddKeyItem(item[i]);
706 }
707 InputManager::GetInstance()->SimulateInputEvent(KeyEvent);
708 break;
709 }
710 downKey.push_back(keyCode);
711 auto KeyEvent = KeyEvent::Create();
712 CHKPR(KeyEvent, ERROR_NULL_POINTER);
713 KeyEvent->SetKeyCode(keyCode);
714 KeyEvent->SetActionTime(time);
715 KeyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
716 KeyEvent::KeyItem item1;
717 item1.SetPressed(true);
718 item1.SetKeyCode(keyCode);
719 item1.SetDownTime(time);
720 KeyEvent->AddKeyItem(item1);
721 InputManager::GetInstance()->SimulateInputEvent(KeyEvent);
722 isCombinationKey = optind;
723 break;
724 }
725 case 'u': {
726 if (!StrToInt(optarg, keyCode)) {
727 std::cout << "invalid button press command" << std::endl;
728 return EVENT_REG_FAIL;
729 }
730 std::vector<int32_t>::iterator iter = std::find(downKey.begin(), downKey.end(), keyCode);
731 if (iter != downKey.end()) {
732 std::cout << "you raised the key " << keyCode << std::endl;
733 auto KeyEvent = KeyEvent::Create();
734 CHKPR(KeyEvent, ERROR_NULL_POINTER);
735 KeyEvent->SetKeyCode(keyCode);
736 KeyEvent->SetActionTime(time);
737 KeyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
738 KeyEvent::KeyItem item1;
739 item1.SetPressed(false);
740 item1.SetKeyCode(keyCode);
741 item1.SetDownTime(time);
742 KeyEvent->AddKeyItem(item1);
743 InputManager::GetInstance()->SimulateInputEvent(KeyEvent);
744 iter = downKey.erase(iter);
745 break;
746 } else {
747 std::cout << "please press the " << keyCode << " key first "<< std::endl;
748 return EVENT_REG_FAIL;
749 }
750 }
751 case 'l': {
752 if (argc < 4) {
753 std::cout << "argc:" << argc << std::endl;
754 std::cout << "wrong number of parameters" << std::endl;
755 return RET_ERR;
756 }
757 if (argc >= 4) {
758 if (!StrToInt(optarg, keyCode)) {
759 std::cout << "invalid key code value" << std::endl;
760 return RET_ERR;
761 }
762 }
763 int32_t pressTimeMs = 3000;
764 if (argc >= 5) {
765 if (!StrToInt(argv[optind], pressTimeMs)) {
766 std::cout << "invalid key code value or press time" << std::endl;
767 return RET_ERR;
768 }
769 }
770 static constexpr int32_t minKeyCode = 0;
771 static constexpr int32_t maxKeyCode = 5000;
772 if ((keyCode < minKeyCode) || (keyCode > maxKeyCode)) {
773 std::cout << "key code is out of range:" << minKeyCode << " <= "
774 << keyCode << " <= " << maxKeyCode << std::endl;
775 return RET_ERR;
776 }
777 static constexpr int32_t minPressTimeMs = 3000;
778 static constexpr int32_t maxPressTimeMs = 15000;
779 if ((pressTimeMs < minPressTimeMs) || (pressTimeMs > maxPressTimeMs)) {
780 std::cout << "press time is out of range:" << minPressTimeMs << " ms" << " <= "
781 << pressTimeMs << " <= " << maxPressTimeMs << " ms" << std::endl;
782 return RET_ERR;
783 }
784 std::cout << " key code: " << keyCode << std::endl
785 << "long press time: " << pressTimeMs << " ms" << std::endl;
786 auto keyEvent = KeyEvent::Create();
787 if (keyEvent == nullptr) {
788 std::cout << "failed to create input event object" << std::endl;
789 return RET_ERR;
790 }
791 keyEvent->SetKeyCode(keyCode);
792 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
793 KeyEvent::KeyItem item;
794 item.SetKeyCode(keyCode);
795 item.SetPressed(true);
796 auto keyEventTemp = KeyEvent::Clone(keyEvent);
797 if (keyEventTemp == nullptr) {
798 std::cout << "failed to clone key event object" << std::endl;
799 return RET_ERR;
800 }
801 keyEventTemp->AddKeyItem(item);
802 InputManager::GetInstance()->SimulateInputEvent(keyEventTemp);
803 std::this_thread::sleep_for(std::chrono::milliseconds(pressTimeMs));
804
805 keyEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
806 item.SetPressed(false);
807 keyEvent->AddKeyItem(item);
808 InputManager::GetInstance()->SimulateInputEvent(keyEvent);
809 break;
810 }
811 case 'i': {
812 int32_t taktTime = 0;
813 if (!StrToInt(optarg, taktTime)) {
814 std::cout << "invalid command to interval time" << std::endl;
815 return EVENT_REG_FAIL;
816 }
817 const int64_t minTaktTimeMs = 1;
818 const int64_t maxTaktTimeMs = 15000;
819 if ((minTaktTimeMs > taktTime) || (maxTaktTimeMs < taktTime)) {
820 std::cout << "taktTime is error" << std::endl;
821 std::cout << minTaktTimeMs << " < taktTime < " << maxTaktTimeMs;
822 std::cout << std::endl;
823 return EVENT_REG_FAIL;
824 }
825 std::this_thread::sleep_for(std::chrono::milliseconds(taktTime));
826 break;
827 }
828 default: {
829 std::cout << "invalid command to keyboard key" << std::endl;
830 ShowUsage();
831 return EVENT_REG_FAIL;
832 }
833 }
834 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
835 }
836 for (size_t i = 0; i < downKey.size(); i++) {
837 std::cout << "you have a key " << downKey[i] << " not release" << std::endl;
838 }
839 break;
840 }
841 case 'T': {
842 int32_t px1 = 0;
843 int32_t py1 = 0;
844 int32_t px2 = 0;
845 int32_t py2 = 0;
846 int32_t totalTimeMs = 0;
847 int32_t moveArgcSeven = 7;
848 while ((c = getopt_long(argc, argv, "m:d:u:c:i:g:k", touchSensorOptions, &optionIndex)) != -1) {
849 switch (c) {
850 case 'm': {
851 if (argc < moveArgcSeven) {
852 std::cout << "argc:" << argc << std::endl;
853 std::cout << "wrong number of parameters" << std::endl;
854 return EVENT_REG_FAIL;
855 }
856 if (argv[optind + 3] == nullptr || argv[optind + 3][0] == '-') {
857 totalTimeMs = 1000;
858 if ((!StrToInt(optarg, px1)) ||
859 (!StrToInt(argv[optind], py1)) ||
860 (!StrToInt(argv[optind + 1], px2)) ||
861 (!StrToInt(argv[optind + 2], py2))) {
862 std::cout << "invalid coordinate value" << std::endl;
863 return EVENT_REG_FAIL;
864 }
865 } else {
866 if ((!StrToInt(optarg, px1)) ||
867 (!StrToInt(argv[optind], py1)) ||
868 (!StrToInt(argv[optind + 1], px2)) ||
869 (!StrToInt(argv[optind + 2], py2)) ||
870 (!StrToInt(argv[optind + 3], totalTimeMs))) {
871 std::cout << "invalid coordinate value or total times" << std::endl;
872 return EVENT_REG_FAIL;
873 }
874 }
875 if ((px1 < 0) || (py1 < 0) || (px2 < 0) || (py2 < 0)) {
876 std::cout << "Coordinate value must be greater than 0" << std::endl;
877 return RET_ERR;
878 }
879 const int64_t minTotalTimeMs = 1;
880 const int64_t maxTotalTimeMs = 15000;
881 if ((totalTimeMs < minTotalTimeMs) || (totalTimeMs > maxTotalTimeMs)) {
882 std::cout << "total time is out of range:" << std::endl;
883 std::cout << minTotalTimeMs << " <= " << "total times" << " <= " << maxTotalTimeMs;
884 std::cout << std::endl;
885 return EVENT_REG_FAIL;
886 }
887 std::cout << "start coordinate: ("<< px1 << ", " << py1 << ")" << std::endl;
888 std::cout << " end coordinate: ("<< px2 << ", " << py2 << ")" << std::endl;
889 std::cout << " total times: " << totalTimeMs << " ms" << std::endl;
890 auto pointerEvent = PointerEvent::Create();
891 CHKPR(pointerEvent, ERROR_NULL_POINTER);
892 PointerEvent::PointerItem item;
893 item.SetDisplayY(py1);
894 item.SetDisplayX(px1);
895 item.SetPointerId(0);
896 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
897 pointerEvent->AddPointerItem(item);
898 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
899 pointerEvent->SetPointerId(0);
900 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
901
902 int64_t startTimeUs = pointerEvent->GetActionStartTime();
903 int64_t startTimeMs = startTimeUs / TIME_TRANSITION;
904 int64_t endTimeMs = 0;
905 if (!AddInt64(startTimeMs, totalTimeMs, endTimeMs)) {
906 std::cout << "system time error." << std::endl;
907 return EVENT_REG_FAIL;
908 }
909 int64_t currentTimeMs = startTimeMs;
910 int64_t nowSysTimeUs = 0;
911 int64_t nowSysTimeMs = 0;
912 int64_t sleepTimeMs = 0;
913 while (currentTimeMs < endTimeMs) {
914 item.SetDisplayY(NextPos(startTimeMs, currentTimeMs, totalTimeMs, py1, py2));
915 item.SetDisplayX(NextPos(startTimeMs, currentTimeMs, totalTimeMs, px1, px2));
916 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
917 pointerEvent->SetActionTime(currentTimeMs * TIME_TRANSITION);
918 pointerEvent->UpdatePointerItem(0, item);
919 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
920 nowSysTimeUs = GetSysClockTime();
921 nowSysTimeMs = nowSysTimeUs / TIME_TRANSITION;
922 sleepTimeMs = (currentTimeMs + BLOCK_TIME_MS) - nowSysTimeMs;
923 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTimeMs));
924 currentTimeMs += BLOCK_TIME_MS;
925 }
926
927 item.SetDisplayY(py2);
928 item.SetDisplayX(px2);
929 pointerEvent->SetActionTime(endTimeMs * TIME_TRANSITION);
930 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
931 pointerEvent->UpdatePointerItem(0, item);
932 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
933 std::this_thread::sleep_for(std::chrono::milliseconds(BLOCK_TIME_MS));
934
935 item.SetDisplayX(px2);
936 item.SetDisplayY(py2);
937 pointerEvent->UpdatePointerItem(0, item);
938 pointerEvent->SetActionTime((endTimeMs + BLOCK_TIME_MS) * TIME_TRANSITION);
939 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
940 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
941 optind = optind + THREE_MORE_COMMAND;
942 break;
943 }
944 case 'd': {
945 if (optind >= argc) {
946 std::cout << "too few arguments to function" << std::endl;
947 return EVENT_REG_FAIL;
948 }
949 if (!StrToInt(optarg, px1) || !StrToInt(argv[optind], py1)) {
950 std::cout << "invalid coordinate value" << std::endl;
951 return EVENT_REG_FAIL;
952 }
953 if ((px1 < 0) || (py1 < 0)) {
954 std::cout << "Coordinate value must be greater than 0" << std::endl;
955 return RET_ERR;
956 }
957 std::cout << "touch down " << px1 << " " << py1 << std::endl;
958 auto pointerEvent = PointerEvent::Create();
959 CHKPR(pointerEvent, ERROR_NULL_POINTER);
960 PointerEvent::PointerItem item;
961 item.SetDisplayY(py1);
962 item.SetPointerId(0);
963 item.SetDisplayX(px1);
964 pointerEvent->SetPointerId(0);
965 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
966 pointerEvent->AddPointerItem(item);
967 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
968 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
969 optind++;
970 break;
971 }
972 case 'u': {
973 if (optind >= argc) {
974 std::cout << "too few arguments to function" << std::endl;
975 return EVENT_REG_FAIL;
976 }
977 if (!StrToInt(optarg, px1) || !StrToInt(argv[optind], py1)) {
978 std::cout << "invalid coordinate value" << std::endl;
979 return EVENT_REG_FAIL;
980 }
981 if ((px1 < 0) || (py1 < 0)) {
982 std::cout << "Coordinate value must be greater than 0" << std::endl;
983 return RET_ERR;
984 }
985 std::cout << "touch up " << px1 << " " << py1 << std::endl;
986 auto pointerEvent = PointerEvent::Create();
987 CHKPR(pointerEvent, ERROR_NULL_POINTER);
988 PointerEvent::PointerItem item;
989 item.SetDisplayY(py1);
990 item.SetPointerId(0);
991 item.SetDisplayX(px1);
992 pointerEvent->SetPointerId(0);
993 pointerEvent->AddPointerItem(item);
994 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
995 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
996 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
997 optind++;
998 break;
999 }
1000 case 'c': {
1001 int32_t intervalTimeMs = 0;
1002 if (argc == 5) {
1003 if (!StrToInt(optarg, px1) ||
1004 !StrToInt(argv[optind], py1)) {
1005 std::cout << "input coordinate error" << std::endl;
1006 return RET_ERR;
1007 }
1008 intervalTimeMs = 100;
1009 } else if (argc == 6) {
1010 if (!StrToInt(optarg, px1) ||
1011 !StrToInt(argv[optind], py1) ||
1012 !StrToInt(argv[optind + 1], intervalTimeMs)) {
1013 std::cout << "input coordinate or time error" << std::endl;
1014 return RET_ERR;
1015 }
1016 const int64_t minIntervalTimeMs = 1;
1017 const int64_t maxIntervalTimeMs = 450;
1018 if ((minIntervalTimeMs > intervalTimeMs) || (maxIntervalTimeMs < intervalTimeMs)) {
1019 std::cout << "interval time is out of range: " << minIntervalTimeMs << "ms";
1020 std::cout << " < interval time < " << maxIntervalTimeMs << "ms" << std::endl;
1021 return RET_ERR;
1022 }
1023 } else {
1024 std::cout << "parameter error, unable to run" << std::endl;
1025 return RET_ERR;
1026 }
1027 if ((px1 < 0) || (py1 < 0)) {
1028 std::cout << "Coordinate value must be greater than 0" << std::endl;
1029 return RET_ERR;
1030 }
1031 std::cout << " click coordinate: ("<< px1 << ", " << py1 << ")" << std::endl;
1032 std::cout << "click interval time: " << intervalTimeMs << "ms" << std::endl;
1033 auto pointerEvent = PointerEvent::Create();
1034 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1035 PointerEvent::PointerItem item;
1036 item.SetPointerId(0);
1037 item.SetDisplayX(px1);
1038 item.SetDisplayY(py1);
1039 item.SetPressed(true);
1040 pointerEvent->SetPointerId(0);
1041 pointerEvent->AddPointerItem(item);
1042 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
1043 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
1044 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1045 std::this_thread::sleep_for(std::chrono::milliseconds(intervalTimeMs));
1046
1047 item.SetPressed(false);
1048 item.SetDisplayY(py1);
1049 item.SetDisplayX(px1);
1050 pointerEvent->UpdatePointerItem(0, item);
1051 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
1052 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1053 break;
1054 }
1055 case 'i': {
1056 int32_t takeTime = 0;
1057 if (!StrToInt(optarg, takeTime)) {
1058 std::cout << "invalid command to interval time" << std::endl;
1059 return EVENT_REG_FAIL;
1060 }
1061 const int64_t minTakeTimeMs = 1;
1062 const int64_t maxTakeTimeMs = 15000;
1063 if ((minTakeTimeMs > takeTime) || (maxTakeTimeMs < takeTime)) {
1064 std::cout << "takeTime is out of range. ";
1065 std::cout << minTakeTimeMs << " < takeTime < " << maxTakeTimeMs;
1066 std::cout << std::endl;
1067 return EVENT_REG_FAIL;
1068 }
1069 std::this_thread::sleep_for(std::chrono::milliseconds(takeTime));
1070 break;
1071 }
1072 case 'g': {
1073 const int32_t dragArgcSeven = 7;
1074 const int32_t dragArgcCommandNine = 9;
1075 if ((argc != dragArgcSeven) && (argc != dragArgcCommandNine)) {
1076 std::cout << "argc:" << argc << std::endl;
1077 std::cout << "wrong number of parameters" << std::endl;
1078 return RET_ERR;
1079 }
1080 totalTimeMs = 1000;
1081 int32_t pressTimems = 500;
1082 if (argc == moveArgcSeven) {
1083 if ((!StrToInt(optarg, px1)) ||
1084 (!StrToInt(argv[optind], py1)) ||
1085 (!StrToInt(argv[optind + 1], px2)) ||
1086 (!StrToInt(argv[optind + 2], py2))) {
1087 std::cout << "invalid coordinate value" << std::endl;
1088 return RET_ERR;
1089 }
1090 } else {
1091 if ((!StrToInt(optarg, px1)) ||
1092 (!StrToInt(argv[optind], py1)) ||
1093 (!StrToInt(argv[optind + 1], px2)) ||
1094 (!StrToInt(argv[optind + 2], py2)) ||
1095 (!StrToInt(argv[optind + 3], pressTimems)) ||
1096 (!StrToInt(argv[optind + 4], totalTimeMs))) {
1097 std::cout << "invalid input coordinate or time" << std::endl;
1098 return RET_ERR;
1099 }
1100 }
1101 if ((px1 < 0) || (py1 < 0) || (px2 < 0) || (py2 < 0)) {
1102 std::cout << "Coordinate value must be greater than 0" << std::endl;
1103 return RET_ERR;
1104 }
1105 const int32_t minTotalTimeMs = 1000;
1106 const int32_t maxTotalTimeMs = 15000;
1107 if ((minTotalTimeMs > totalTimeMs) || (maxTotalTimeMs < totalTimeMs)) {
1108 std::cout << "total time input is error" << std::endl;
1109 return RET_ERR;
1110 }
1111 const int32_t minPressTimeMs = 500;
1112 const int32_t maxPressTimeMs = 14500;
1113 if ((minPressTimeMs > pressTimems) || (maxPressTimeMs < pressTimems)) {
1114 std::cout << "press time is out of range" << std::endl;
1115 return RET_ERR;
1116 }
1117 const int32_t minMoveTimeMs = 500;
1118 if ((totalTimeMs - pressTimems) < minMoveTimeMs) {
1119 std::cout << "move time is out of range" << std::endl;
1120 return RET_ERR;
1121 }
1122 auto pointerEvent = PointerEvent::Create();
1123 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1124 PointerEvent::PointerItem item;
1125 item.SetDisplayY(py1);
1126 item.SetDisplayX(px1);
1127 pointerEvent->AddPointerItem(item);
1128 pointerEvent->SetPointerId(0);
1129 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
1130 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
1131 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1132 const int32_t conversionRate = 1000;
1133 int64_t startTimeMs = GetSysClockTime() / conversionRate;
1134 int64_t endTimeMs = 0;
1135 if (!AddInt64(startTimeMs, totalTimeMs, endTimeMs)) {
1136 std::cout << "end time count error" << std::endl;
1137 return RET_ERR;
1138 }
1139 int64_t downTimeMs = 0;
1140 if (!AddInt64(startTimeMs, pressTimems, downTimeMs)) {
1141 std::cout << "down time count error" << std::endl;
1142 return RET_ERR;
1143 }
1144 int64_t currentTimeMs = startTimeMs;
1145 const int32_t moveTimeMs = totalTimeMs - pressTimems;
1146 while ((currentTimeMs < endTimeMs)) {
1147 if (currentTimeMs > downTimeMs) {
1148 item.SetDisplayX(NextPos(downTimeMs, currentTimeMs, moveTimeMs, px1, px2));
1149 item.SetDisplayY(NextPos(downTimeMs, currentTimeMs, moveTimeMs, py1, py2));
1150 pointerEvent->UpdatePointerItem(0, item);
1151 pointerEvent->SetActionTime(currentTimeMs);
1152 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
1153 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1154 }
1155 std::this_thread::sleep_for(std::chrono::milliseconds(BLOCK_TIME_MS));
1156 currentTimeMs = GetSysClockTime() / conversionRate;
1157 }
1158 item.SetDisplayX(px2);
1159 item.SetDisplayY(py2);
1160 pointerEvent->UpdatePointerItem(0, item);
1161 pointerEvent->SetActionTime(endTimeMs);
1162 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
1163 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1164 break;
1165 }
1166 case 'k': {
1167 KnuckleGestureInputProcess(argc, argv, c, optionIndex);
1168 break;
1169 }
1170 default: {
1171 std::cout << "invalid command" << std::endl;
1172 ShowUsage();
1173 return EVENT_REG_FAIL;
1174 }
1175 }
1176 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
1177 }
1178 break;
1179 }
1180 case 'J': {
1181 JoystickInfo joyInfo;
1182 std::vector<std::pair<int32_t, JoystickInfo>> state;
1183 while ((c = getopt_long(argc, argv, "m:d:u:c:i:", joystickSensorOptions, &optionIndex)) != -1) {
1184 switch (c) {
1185 case 'm': {
1186 std::string arg(optarg);
1187 std::string::size_type pos = arg.find('=');
1188 if (pos == std::string::npos) {
1189 std::cout << "Parameter format is error" << std::endl;
1190 return EVENT_REG_FAIL;
1191 }
1192 std::string absAction = arg.substr(0, pos);
1193 if (absAction == "x") {
1194 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_X;
1195 } else if (absAction == "y") {
1196 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_Y;
1197 } else if (absAction == "z") {
1198 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_Z;
1199 } else if (absAction == "rz") {
1200 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_RZ;
1201 } else if (absAction == "gas") {
1202 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_GAS;
1203 } else if (absAction == "brake") {
1204 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_BRAKE;
1205 } else if (absAction == "hat0x") {
1206 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_HAT0X;
1207 } else if (absAction == "hat0y") {
1208 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_HAT0Y;
1209 } else if (absAction == "throttle") {
1210 joyInfo.absType = PointerEvent::AxisType::AXIS_TYPE_ABS_THROTTLE;
1211 } else {
1212 std::cout << "Invalid abstype" << std::endl;
1213 return RET_ERR;
1214 }
1215 if (!StrToInt(arg.substr(pos + 1), joyInfo.absValue)) {
1216 std::cout << "Invalid parameter to move absValue" << std::endl;
1217 return EVENT_REG_FAIL;
1218 }
1219 state.push_back(std::pair<int32_t, JoystickInfo>(JOYSTICK_MOVE, joyInfo));
1220 break;
1221 }
1222 case 'd': {
1223 if (!StrToInt(optarg, joyInfo.buttonId)) {
1224 std::cout << "Invalid button press command" << std::endl;
1225 return EVENT_REG_FAIL;
1226 }
1227 if (joyInfo.buttonId > JOYSTICK_BUTTON_ID) {
1228 std::cout << "Pressed button value is greater than the max value" << std::endl;
1229 return EVENT_REG_FAIL;
1230 }
1231 state.push_back(std::pair<int32_t, JoystickInfo>(JOYSTICK_BUTTON_PRESS, joyInfo));
1232 break;
1233 }
1234 case 'u': {
1235 if (!StrToInt(optarg, joyInfo.buttonId)) {
1236 std::cout << "Invalid raise button command" << std::endl;
1237 return EVENT_REG_FAIL;
1238 }
1239 if (joyInfo.buttonId > JOYSTICK_BUTTON_ID) {
1240 std::cout << "Raise button value is greater than the max value" << std::endl;
1241 return EVENT_REG_FAIL;
1242 }
1243 state.push_back(std::pair<int32_t, JoystickInfo>(JOYSTICK_BUTTON_UP, joyInfo));
1244 break;
1245 }
1246 case 'c': {
1247 if (!StrToInt(optarg, joyInfo.buttonId)) {
1248 std::cout << "Invalid click button command" << std::endl;
1249 return EVENT_REG_FAIL;
1250 }
1251 if (joyInfo.buttonId > JOYSTICK_BUTTON_ID) {
1252 std::cout << "Click button value is greater than the max value" << std::endl;
1253 return EVENT_REG_FAIL;
1254 }
1255 state.push_back(std::pair<int32_t, JoystickInfo>(JOYSTICK_CLICK, joyInfo));
1256 break;
1257 }
1258 case 'i': {
1259 if (!StrToInt(optarg, joyInfo.taktTime)) {
1260 std::cout << "Invalid command to interval time" << std::endl;
1261 return EVENT_REG_FAIL;
1262 }
1263 state.push_back(std::pair<int32_t, JoystickInfo>(JOYSTICK_INTERVAL, joyInfo));
1264 break;
1265 }
1266 default: {
1267 std::cout << "Invalid options" << std::endl;
1268 ShowUsage();
1269 return EVENT_REG_FAIL;
1270 }
1271 }
1272 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
1273 }
1274 auto pointerEvent = PointerEvent::Create();
1275 if (pointerEvent != nullptr) {
1276 if (optind < argc) {
1277 std::cout << "non-option argv elements: ";
1278 while (optind < argc) {
1279 std::cout << argv[optind++] << "\t";
1280 }
1281 std::cout << std::endl;
1282 return EVENT_REG_FAIL;
1283 }
1284 if (state.empty()) {
1285 std::cout << "Injection failed" << std::endl;
1286 return EVENT_REG_FAIL;
1287 }
1288 for (const auto &it : state) {
1289 if (it.first == JOYSTICK_BUTTON_PRESS) {
1290 std::cout << "Press down " << it.second.buttonId <<std::endl;
1291 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
1292 pointerEvent->SetButtonId(it.second.buttonId);
1293 pointerEvent->SetButtonPressed(it.second.buttonId);
1294 } else if (it.first == JOYSTICK_BUTTON_UP) {
1295 std::cout << "Lift up button " << it.second.buttonId << std::endl;
1296 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
1297 pointerEvent->SetButtonPressed(it.second.buttonId);
1298 pointerEvent->SetButtonId(it.second.buttonId);
1299 } else if (it.first == JOYSTICK_MOVE) {
1300 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
1301 pointerEvent->SetAxisValue(it.second.absType, it.second.absValue);
1302 } else if (it.first == JOYSTICK_CLICK) {
1303 std::cout << "Click " << it.second.buttonId << std::endl;
1304 pointerEvent->SetButtonId(it.second.buttonId);
1305 pointerEvent->SetPointerId(0);
1306 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
1307 pointerEvent->SetButtonPressed(it.second.buttonId);
1308 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_JOYSTICK);
1309 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1310
1311 pointerEvent->SetButtonPressed(it.second.buttonId);
1312 pointerEvent->SetButtonId(it.second.buttonId);
1313 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
1314 } else if (it.first == JOYSTICK_INTERVAL) {
1315 if ((MIN_TAKTTIME_MS > joyInfo.taktTime) || (MAX_TAKTTIME_MS < joyInfo.taktTime)) {
1316 std::cout << "TaktTime is out of range" << std::endl;
1317 return EVENT_REG_FAIL;
1318 }
1319 std::this_thread::sleep_for(std::chrono::milliseconds(joyInfo.taktTime));
1320 continue;
1321 }
1322 pointerEvent->SetPointerId(0);
1323 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_JOYSTICK);
1324 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1325 }
1326 }
1327 break;
1328 }
1329 case '?': {
1330 ShowUsage();
1331 return ERR_OK;
1332 }
1333 default: {
1334 std::cout << "invalid command" << std::endl;
1335 ShowUsage();
1336 return EVENT_REG_FAIL;
1337 }
1338 }
1339 } else {
1340 std::cout << "too few arguments to function" << std::endl;
1341 ShowUsage();
1342 return EVENT_REG_FAIL;
1343 }
1344 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEPTIME));
1345 return ERR_OK;
1346 }
1347
KnuckleGestureInputProcess(int32_t argc,char * argv[],int32_t c,int32_t optionIndex)1348 int32_t InputManagerCommand::KnuckleGestureInputProcess(int32_t argc, char *argv[], int32_t c, int32_t optionIndex)
1349 {
1350 struct option knuckleGestureSensorOptions[] = {
1351 {"single_finger_double_click", required_argument, nullptr, 's'},
1352 {"double_finger_double_click", required_argument, nullptr, 'd'},
1353 {nullptr, 0, nullptr, 0}
1354 };
1355
1356 while ((c = getopt_long(argc, argv, "s:d:", knuckleGestureSensorOptions, &optionIndex)) != -1) {
1357 switch (c) {
1358 case 's': {
1359 SingleKnuckleGestureProcesser(argc, argv);
1360 break;
1361 }
1362 case 'd': {
1363 DoubleKnuckleGestureProcesser(argc, argv);
1364 break;
1365 }
1366 default: {
1367 std::cout << "invalid command" << std::endl;
1368 ShowUsage();
1369 return EVENT_REG_FAIL;
1370 }
1371 }
1372 }
1373 return ERR_OK;
1374 }
1375
SingleKnuckleGestureProcesser(int32_t argc,char * argv[])1376 int32_t InputManagerCommand::SingleKnuckleGestureProcesser(int32_t argc, char *argv[])
1377 {
1378 int32_t knuckleUinputArgc = 8;
1379 int32_t intervalTimeMs = 0;
1380 int32_t firstDownX = 0;
1381 int32_t firstDownY = 0;
1382 int32_t secondDownX = 0;
1383 int32_t secondDownY = 0;
1384 if (optind < 0 || optind > argc) {
1385 std::cout << "wrong optind pointer index" << std::endl;
1386 return EVENT_REG_FAIL;
1387 }
1388 if (argc == knuckleUinputArgc) {
1389 if ((!StrToInt(optarg, firstDownX)) || !StrToInt(argv[optind], firstDownY) ||
1390 !StrToInt(argv[optind + 1], secondDownX) || !StrToInt(argv[optind + TWO_MORE_COMMAND], secondDownY)) {
1391 std::cout << "invalid coordinate value" << std::endl;
1392 return EVENT_REG_FAIL;
1393 }
1394 intervalTimeMs = DEFAULT_DELAY;
1395 } else if (argc == KNUCKLE_PARAM_SIZE) {
1396 if ((!StrToInt(optarg, firstDownX)) || !StrToInt(argv[optind], firstDownY) ||
1397 !StrToInt(argv[optind + 1], secondDownX) || !StrToInt(argv[optind + TWO_MORE_COMMAND], secondDownY) ||
1398 !StrToInt(argv[optind + THREE_MORE_COMMAND], intervalTimeMs)) {
1399 std::cout << "input coordinate or time error" << std::endl;
1400 return RET_ERR;
1401 }
1402 const int64_t minIntervalTimeMs = 1;
1403 const int64_t maxIntervalTimeMs = 250;
1404 if ((minIntervalTimeMs > intervalTimeMs) || (maxIntervalTimeMs < intervalTimeMs)) {
1405 std::cout << "interval time is out of range: " << minIntervalTimeMs << "ms";
1406 std::cout << " < interval time < " << maxIntervalTimeMs << "ms" << std::endl;
1407 return RET_ERR;
1408 }
1409 } else {
1410 std::cout << "wrong number of parameters:" << argc << std::endl;
1411 return EVENT_REG_FAIL;
1412 }
1413 if (IsCoordinateInvalid(firstDownX, firstDownY, secondDownX, secondDownY)) {
1414 std::cout << "Coordinate value must be greater than 0" << std::endl;
1415 return RET_ERR;
1416 }
1417 std::cout << "single knuckle first down coordinate: ("<< firstDownX << ", " << firstDownY << ")" << std::endl;
1418 std::cout << "single knuckle second down coordinate: ("<< secondDownX << ", " << secondDownY << ")" << std::endl;
1419 std::cout << "single knuckle interval time: " << intervalTimeMs << "ms" << std::endl;
1420 SingleKnuckleClickEvent(firstDownX, firstDownY);
1421 std::this_thread::sleep_for(std::chrono::milliseconds(intervalTimeMs));
1422 SingleKnuckleClickEvent(secondDownX, secondDownY);
1423 return ERR_OK;
1424 }
1425
DoubleKnuckleGestureProcesser(int32_t argc,char * argv[])1426 int32_t InputManagerCommand::DoubleKnuckleGestureProcesser(int32_t argc, char *argv[])
1427 {
1428 int32_t knuckleUinputArgc = 8;
1429 int32_t intervalTimeMs = 0;
1430 int32_t firstDownX = 0;
1431 int32_t firstDownY = 0;
1432 int32_t secondDownX = 0;
1433 int32_t secondDownY = 0;
1434 if (optind < 0 || optind > argc) {
1435 std::cout << "wrong optind pointer index" << std::endl;
1436 return EVENT_REG_FAIL;
1437 }
1438 if (argc == knuckleUinputArgc) {
1439 if (!StrToInt(optarg, firstDownX) || !StrToInt(argv[optind], firstDownY) ||
1440 !StrToInt(argv[optind + 1], secondDownX) || !StrToInt(argv[optind + TWO_MORE_COMMAND], secondDownY)) {
1441 std::cout << "invalid coordinate value" << std::endl;
1442 return EVENT_REG_FAIL;
1443 }
1444 intervalTimeMs = DEFAULT_DELAY;
1445 } else if (argc == KNUCKLE_PARAM_SIZE) {
1446 if ((!StrToInt(optarg, firstDownX)) || !StrToInt(argv[optind], firstDownY) ||
1447 !StrToInt(argv[optind + 1], secondDownX) || !StrToInt(argv[optind + TWO_MORE_COMMAND], secondDownY) ||
1448 !StrToInt(argv[optind + THREE_MORE_COMMAND], intervalTimeMs)) {
1449 std::cout << "input coordinate or time error" << std::endl;
1450 return RET_ERR;
1451 }
1452 const int64_t minIntervalTimeMs = 1;
1453 const int64_t maxIntervalTimeMs = 250;
1454 if ((minIntervalTimeMs > intervalTimeMs) || (maxIntervalTimeMs < intervalTimeMs)) {
1455 std::cout << "interval time is out of range: " << minIntervalTimeMs << "ms";
1456 std::cout << " < interval time < " << maxIntervalTimeMs << "ms" << std::endl;
1457 return RET_ERR;
1458 }
1459 } else {
1460 std::cout << "wrong number of parameters: " << argc << std::endl;
1461 return EVENT_REG_FAIL;
1462 }
1463 if (IsCoordinateInvalid(firstDownX, firstDownY, secondDownX, secondDownY)) {
1464 std::cout << "Coordinate value must be greater than 0" << std::endl;
1465 return RET_ERR;
1466 }
1467 std::cout << "double knukle first click coordinate: ("<< firstDownX << ", " << firstDownY << ")" << std::endl;
1468 std::cout << "double knukle second click coordinate: ("<< secondDownX << ", " << secondDownY << ")" << std::endl;
1469 std::cout << "double knuckle interval time: " << intervalTimeMs << "ms" << std::endl;
1470
1471 DoubleKnuckleClickEvent(firstDownX, firstDownY);
1472 std::this_thread::sleep_for(std::chrono::milliseconds(intervalTimeMs));
1473 DoubleKnuckleClickEvent(secondDownX, secondDownY);
1474 return ERR_OK;
1475 }
1476
IsCoordinateInvalid(int32_t firstDownX,int32_t firstDownY,int32_t secondDownX,int32_t secondDownY)1477 bool InputManagerCommand::IsCoordinateInvalid(int32_t firstDownX, int32_t firstDownY, int32_t secondDownX,
1478 int32_t secondDownY)
1479 {
1480 return firstDownX < 0 || firstDownY < 0 || secondDownX < 0 || secondDownY < 0;
1481 }
1482
SingleKnuckleClickEvent(int32_t downX,int32_t downY)1483 int32_t InputManagerCommand::SingleKnuckleClickEvent(int32_t downX, int32_t downY)
1484 {
1485 auto pointerEvent = PointerEvent::Create();
1486 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1487 PointerEvent::PointerItem item;
1488 item.SetPointerId(0);
1489 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE);
1490 item.SetDisplayX(downX);
1491 item.SetDisplayY(downY);
1492 item.SetPressed(true);
1493 pointerEvent->SetPointerId(0);
1494 pointerEvent->AddPointerItem(item);
1495 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
1496 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
1497 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1498
1499 item.SetPressed(false);
1500 item.SetDisplayY(downY);
1501 item.SetDisplayX(downX);
1502 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE);
1503 pointerEvent->UpdatePointerItem(0, item);
1504 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
1505 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1506 return ERR_OK;
1507 }
1508
DoubleKnuckleClickEvent(int32_t downX,int32_t downY)1509 int32_t InputManagerCommand::DoubleKnuckleClickEvent(int32_t downX, int32_t downY)
1510 {
1511 auto pointerEvent = PointerEvent::Create();
1512 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1513 PointerEvent::PointerItem item;
1514 PointerEvent::PointerItem item2;
1515 item.SetPointerId(0);
1516 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE);
1517 item.SetDisplayX(downX);
1518 item.SetDisplayY(downY);
1519 item.SetPressed(true);
1520 pointerEvent->SetPointerId(0);
1521 pointerEvent->AddPointerItem(item);
1522
1523 item2.SetPointerId(1);
1524 item2.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE);
1525 item2.SetDisplayX(downX);
1526 item2.SetDisplayY(downY);
1527 item2.SetPressed(true);
1528 pointerEvent->SetPointerId(1);
1529 pointerEvent->AddPointerItem(item2);
1530 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
1531 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
1532 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1533
1534 item.SetPressed(false);
1535 item.SetDisplayY(downY);
1536 item.SetDisplayX(downX);
1537 item.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE);
1538 item2.SetPressed(false);
1539 item2.SetDisplayY(downY);
1540 item2.SetDisplayX(downX);
1541 item2.SetToolType(PointerEvent::TOOL_TYPE_KNUCKLE);
1542 pointerEvent->UpdatePointerItem(0, item);
1543 pointerEvent->UpdatePointerItem(1, item2);
1544 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
1545 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
1546 return ERR_OK;
1547 }
1548
PrintMouseUsage()1549 void InputManagerCommand::PrintMouseUsage()
1550 {
1551 std::cout << "-m <dx> <dy> --move <dx> <dy> -move to relative position (dx,dy)," << std::endl;
1552 std::cout << " <dx1> <dy1> <dx2> <dy2> [smooth time] --trace -dx1 dy1 to dx2 dy2 smooth movement" << std::endl;
1553 std::cout << "-d <key> --down key -press down a button, " << std::endl;
1554 std::cout << " 0 is the left button, 1 is the right," << std::endl;
1555 std::cout << " 2 is the middle" << std::endl;
1556 std::cout << "-u <key> --up <key> -release a button " << std::endl;
1557 std::cout << "-c <key> --click <key> -press the left button down,then raise" << std::endl;
1558 std::cout << "-b <dx1> <dy1> <id> [press time] [click interval time] --double click" << std::endl;
1559 std::cout << " [press time] the time range is more than 1ms but less than 300ms, " << std::endl;
1560 std::cout << " [click interval time] the time range is more than 1ms but less than 450ms, " << std::endl;
1561 std::cout << " Otherwise the operation result may produce error or invalid operation" << std::endl;
1562 std::cout << " -press the left button down,then raise" << std::endl;
1563 std::cout << " key value:0 - button left" << std::endl;
1564 std::cout << " key value:1 - button right" << std::endl;
1565 std::cout << " key value:2 - button middle" << std::endl;
1566 std::cout << " key value:3 - button side" << std::endl;
1567 std::cout << " key value:4 - button extra" << std::endl;
1568 std::cout << " key value:5 - button forward" << std::endl;
1569 std::cout << " key value:6 - button back" << std::endl;
1570 std::cout << " key value:7 - button task" << std::endl;
1571 std::cout << "-s <key> --scroll <key> -positive values are sliding backwards" << std::endl;
1572 std::cout << "-g <dx1> <dy1> <dx2> <dy2> [total time] --drag <dx1> <dy1> <dx2> <dy2> [total time],";
1573 std::cout << std::endl;
1574 std::cout << " dx1 dy1 to dx2 dy2 smooth drag" << std::endl;
1575 std::cout << "-i <time> --interval <time> -the program interval for the (time) milliseconds";
1576 std::cout << std::endl;
1577 std::cout << " negative values are sliding forwards" << std::endl;
1578 }
1579
PrintKeyboardUsage()1580 void InputManagerCommand::PrintKeyboardUsage()
1581 {
1582 std::cout << "-d <key> --down <key> -press down a key" << std::endl;
1583 std::cout << "-u <key> --up <key> -release a key " << std::endl;
1584 std::cout << "-l <key> [long press time] --long_press <key> [long press time] -press and hold the key";
1585 std::cout << std::endl;
1586 std::cout << "-i <time> --interval <time> -the program interval for the (time) milliseconds";
1587 std::cout << std::endl;
1588 }
1589
PrintTouchUsage()1590 void InputManagerCommand::PrintTouchUsage()
1591 {
1592 std::cout << "-d <dx1> <dy1> --down <dx1> <dy1> -press down a position dx1 dy1, " << std::endl;
1593 std::cout << "-u <dx1> <dy1> --up <dx1> <dy1> -release a position dx1 dy1, " << std::endl;
1594 std::cout << "-m <dx1> <dy1> <dx2> <dy2> [smooth time] --smooth movement" << std::endl;
1595 std::cout << " <dx1> <dy1> <dx2> <dy2> [smooth time] -smooth movement, " << std::endl;
1596 std::cout << " dx1 dy1 to dx2 dy2 smooth movement" << std::endl;
1597 std::cout << "-c <dx1> <dy1> [click interval] -touch screen click dx1 dy1" << std::endl;
1598 }
1599
PrintKnuckleUsage()1600 void InputManagerCommand::PrintKnuckleUsage()
1601 {
1602 std::cout << "-s <dx1> <dy1> <dx2> <dy2> [interval time] --single knuckle double click interval time" << std::endl;
1603 std::cout << "-d <dx1> <dy1> <dx2> <dy2> [interval time] --double knuckle double click interval time" << std::endl;
1604 std::cout << "-i <time> --interval <time> -the program interval for the (time) milliseconds";
1605 }
1606
ShowUsage()1607 void InputManagerCommand::ShowUsage()
1608 {
1609 std::cout << "Usage: uinput <option> <command> <arg>..." << std::endl;
1610 std::cout << "The option are: " << std::endl;
1611 std::cout << "-M --mouse " << std::endl;
1612 std::cout << "commands for mouse: " << std::endl;
1613 PrintMouseUsage();
1614 std::cout << std::endl;
1615 std::cout << "-K --keyboard " << std::endl;
1616 std::cout << "commands for keyboard: " << std::endl;
1617 PrintKeyboardUsage();
1618 std::cout << std::endl;
1619 std::cout << "-T --touch " << std::endl;
1620 std::cout << "commands for touch: " << std::endl;
1621 PrintTouchUsage();
1622 std::cout << "-k --knuckle " << std::endl;
1623 std::cout << "commands for knucle: " << std::endl;
1624 PrintKnuckleUsage();
1625 std::cout << std::endl;
1626 std::cout << "-g <dx1> <dy1> <dx2> <dy2> [press time] [total time] -drag, " << std::endl;
1627 std::cout << " [Press time] not less than 500ms and [total time] - [Press time] not less than 500ms" << std::endl;
1628 std::cout << " Otherwise the operation result may produce error or invalid operation" << std::endl;
1629 std::cout << " " << std::endl;
1630 std::cout << "-? --help " << std::endl;
1631 }
1632 } // namespace MMI
1633 } // namespace OHOS
1634