1 #include "MockDeviceImpl.h"
18 #define NANO_SEC 1000000000.0
19 #define MICRO_SEC 1000000
21 MockDeviceImpl::MockDeviceImpl()
22 :tStart{}, isTimerStarted{false}, mTouchRelease{}, mTouchDown{}, mKeyDevice{}, mWheelDevice{0}
24 mTouchRelease.emplace(1, TouchData{0,0,0,0});
25 mTouchRelease.emplace(2, TouchData{0,0,0,0});
28 MockDeviceImpl::~MockDeviceImpl()
32 bool MockDeviceImpl::click(const int x, const int y)
34 return click(x, y, INTV_CLICK);
37 bool MockDeviceImpl::click(const int x, const int y, const unsigned int intv)
39 int seq = touchDown(x, y);
40 if (seq < 0) return false;
41 std::this_thread::sleep_for(std::chrono::milliseconds{intv});
42 return touchUp(x, y, seq);
45 bool MockDeviceImpl::drag(const int sx, const int sy, const int ex, const int ey,
46 const int steps, const int durationMs)
49 int _durationUs, _steps;
52 if (steps <= 0) _steps = 1;
55 if (durationMs < MINIMUM_DURATION_DRAG) _durationUs = INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC;
56 else _durationUs = durationMs * MSEC_PER_SEC;
58 _stepUs = _durationUs / (_steps +1);
60 if (_stepUs < (INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC)) {
61 _steps = (int)(_durationUs / (INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC)) - 1;
62 _stepUs = (INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC);
65 int seq = touchDown(sx, sy);
66 if (seq < 0) return false;
67 consumptionUs = stopTimer();
69 for ( int s = 1; s <= _steps + 1; s++) {
70 usleep((_stepUs - consumptionUs)>INTV_MINIMUM_USLEEP?(_stepUs - consumptionUs):INTV_MINIMUM_USLEEP);
72 touchMove(sx + (ex - sx) * s / (steps + 1), sy + (ey - sy) * s / (steps + 1), seq);
73 consumptionUs = stopTimer();
75 usleep((_stepUs - consumptionUs)>INTV_MINIMUM_USLEEP?(_stepUs - consumptionUs):INTV_MINIMUM_USLEEP);
76 touchMove(ex, ey, seq);
82 int MockDeviceImpl::touchDown(const int x, const int y)
84 if (mTouchRelease.size() <= 0) return -1;
85 int seq = mTouchRelease.begin()->first;
86 mTouchDown[seq] = TouchData{x, y, timeStamp(), -1};
87 mTouchRelease.erase(seq);
91 bool MockDeviceImpl::touchMove(const int x, const int y, const int seq)
93 if (mTouchDown.count(seq) <= 0) return false;
94 mTouchDown[seq] = TouchData{x, y, mTouchDown[seq].stamp1, timeStamp()};
98 bool MockDeviceImpl::touchUp(const int x, const int y, const int seq)
100 if (mTouchDown.count(seq) <= 0) return false;
101 mTouchRelease[seq] = TouchData{x, y, mTouchDown[seq].stamp1, timeStamp()};
102 mTouchDown.erase(seq);
106 bool MockDeviceImpl::wheelUp(int amount, const int durationMs)
108 for (int i = 0; i < amount; i++){
110 usleep(durationMs * MSEC_PER_SEC/amount);
116 bool MockDeviceImpl::wheelDown(int amount, const int durationMs)
118 for (int i = 0; i < amount; i++){
120 usleep(durationMs * MSEC_PER_SEC/(double)amount);
127 bool MockDeviceImpl::pressBack(KeyRequestType type)
129 mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::BACK, type, ""));
134 bool MockDeviceImpl::pressHome(KeyRequestType type)
136 mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::HOME, type, ""));
141 bool MockDeviceImpl::pressMenu(KeyRequestType type)
143 mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::MENU, type, ""));
147 bool MockDeviceImpl::pressVolUp(KeyRequestType type)
149 mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::VOLUP, type, ""));
153 bool MockDeviceImpl::pressVolDown(KeyRequestType type)
155 mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::VOLDOWN, type, ""));
159 bool MockDeviceImpl::pressPower(KeyRequestType type)
161 mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::POWER, type, ""));
165 bool MockDeviceImpl::pressKeyCode(std::string keycode, KeyRequestType type)
167 mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::KEY, type, keycode));
171 bool MockDeviceImpl::takeScreenshot(std::string path, float scale, int quality)
176 long long MockDeviceImpl::getSystemTime(TimeRequestType type)
180 if (type == TimeRequestType::MONOTONIC) {
181 clock_gettime(CLOCK_MONOTONIC, &t);
182 } else if (type == TimeRequestType::WALLCLOCK) {
183 clock_gettime(CLOCK_REALTIME, &t);
186 return (long long)t.tv_sec * 1000L + (long long)(t.tv_nsec / MICRO_SEC);
189 bool MockDeviceImpl::strokeKeyCode(std::string keycode, unsigned int intv)
194 bool MockDeviceImpl::pressKeyCode(std::string keycode)
199 bool MockDeviceImpl::releaseKeyCode(std::string keycode)
204 int MockDeviceImpl::grabTouchSeqNumber()
209 bool MockDeviceImpl::releaseTouchSeqNumber(int seq)
214 void MockDeviceImpl::startTimer(void)
216 isTimerStarted = true;
217 clock_gettime(CLOCK_MONOTONIC, &tStart);
220 int MockDeviceImpl::stopTimer(void)
222 struct timespec tEnd;
223 if (!isTimerStarted) return 0;
224 isTimerStarted = false;
225 clock_gettime(CLOCK_MONOTONIC, &tEnd);
226 return ((tEnd.tv_sec + tEnd.tv_nsec/NANO_SEC) - (tStart.tv_sec + tStart.tv_nsec/NANO_SEC)) * MICRO_SEC;
229 long long MockDeviceImpl::timeStamp(void)
232 clock_gettime(CLOCK_MONOTONIC, &t);
233 long long rtn = (t.tv_sec + t.tv_nsec/NANO_SEC) * MICRO_SEC;