libaurum: Fix indentation and clean code up
[platform/core/uifw/aurum.git] / libaurum / src / Impl / MockDeviceImpl.cc
1 #include "MockDeviceImpl.h"
2
3 #include <functional>
4 #include <algorithm>
5 #include <tuple>
6 #include <iostream>
7 #include <memory>
8
9 #include <stdlib.h>
10 #include <time.h>
11
12 #include <stdio.h>
13 #include <unistd.h>
14
15 #include <chrono>
16 #include <thread>
17
18 #define NANO_SEC 1000000000.0
19 #define MICRO_SEC 1000000
20
21 MockDeviceImpl::MockDeviceImpl()
22 :tStart{}, isTimerStarted{false}, mTouchRelease{}, mTouchDown{}, mKeyDevice{}, mWheelDevice{0}
23 {
24     mTouchRelease.emplace(1, TouchData{0,0,0,0});
25     mTouchRelease.emplace(2, TouchData{0,0,0,0});
26 }
27
28 MockDeviceImpl::~MockDeviceImpl()
29 {
30 }
31
32 bool MockDeviceImpl::click(const int x, const int y)
33 {
34     return click(x, y, INTV_CLICK);
35 }
36
37 bool MockDeviceImpl::click(const int x, const int y, const unsigned int intv)
38 {
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);
43 }
44
45 bool MockDeviceImpl::drag(const int sx, const int sy, const int ex, const int ey,
46             const int steps, const int durationMs)
47 {
48     unsigned int _stepUs;
49     int _durationUs, _steps;
50     int consumptionUs;
51
52     if (steps <= 0) _steps = 1;
53     else _steps = steps;
54
55     if (durationMs < MINIMUM_DURATION_DRAG) _durationUs = INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC;
56     else _durationUs = durationMs * MSEC_PER_SEC;
57
58     _stepUs = _durationUs / (_steps +1);
59
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);
63     }
64     startTimer();
65     int seq = touchDown(sx, sy);
66     if (seq < 0) return false;
67     consumptionUs = stopTimer();
68
69     for ( int s = 1; s <= _steps + 1; s++) {
70         usleep((_stepUs - consumptionUs)>INTV_MINIMUM_USLEEP?(_stepUs - consumptionUs):INTV_MINIMUM_USLEEP);
71         startTimer();
72         touchMove(sx + (ex - sx) * s / (steps + 1), sy + (ey - sy) * s / (steps + 1), seq);
73         consumptionUs = stopTimer();
74     }
75     usleep((_stepUs - consumptionUs)>INTV_MINIMUM_USLEEP?(_stepUs - consumptionUs):INTV_MINIMUM_USLEEP);
76     touchMove(ex, ey, seq);
77     touchUp(ex, ey, seq);
78
79     return true;
80 }
81
82 int MockDeviceImpl::touchDown(const int x, const int y)
83 {
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);
88     return seq;
89 }
90
91 bool MockDeviceImpl::touchMove(const int x, const int y, const int seq)
92 {
93     if (mTouchDown.count(seq) <= 0) return false;
94     mTouchDown[seq] = TouchData{x, y, mTouchDown[seq].stamp1, timeStamp()};
95     return true;
96 }
97
98 bool MockDeviceImpl::touchUp(const int x, const int y, const int seq)
99 {
100     if (mTouchDown.count(seq) <= 0) return false;
101     mTouchRelease[seq] = TouchData{x, y, mTouchDown[seq].stamp1, timeStamp()};
102     mTouchDown.erase(seq);
103     return true;
104 }
105
106 bool MockDeviceImpl::wheelUp(int amount, const int durationMs)
107 {
108     for (int i = 0; i < amount; i++){
109         mWheelDevice++;
110         usleep(durationMs * MSEC_PER_SEC/amount);
111     }
112
113     return true;
114 }
115
116 bool MockDeviceImpl::wheelDown(int amount, const int durationMs)
117 {
118     for (int i = 0; i < amount; i++){
119         mWheelDevice--;
120         usleep(durationMs * MSEC_PER_SEC/(double)amount);
121     }
122
123     return true;
124
125 }
126
127 bool MockDeviceImpl::pressBack(KeyRequestType type)
128 {
129     mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::BACK, type, ""));
130     return true;
131
132 }
133
134 bool MockDeviceImpl::pressHome(KeyRequestType type)
135 {
136     mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::HOME, type, ""));
137     return true;
138
139 }
140
141 bool MockDeviceImpl::pressMenu(KeyRequestType type)
142 {
143     mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::MENU, type, ""));
144     return true;
145 }
146
147 bool MockDeviceImpl::pressVolUp(KeyRequestType type)
148 {
149     mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::VOLUP, type, ""));
150     return true;
151 }
152
153 bool MockDeviceImpl::pressVolDown(KeyRequestType type)
154 {
155     mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::VOLDOWN, type, ""));
156     return true;
157 }
158
159 bool MockDeviceImpl::pressPower(KeyRequestType type)
160 {
161     mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::POWER, type, ""));
162     return true;
163 }
164
165 bool MockDeviceImpl::pressKeyCode(std::string keycode, KeyRequestType type)
166 {
167     mKeyDevice.push_back(std::tuple<MockKeyType, KeyRequestType, std::string>(MockKeyType::KEY, type, keycode));
168     return true;
169 }
170
171 bool MockDeviceImpl::takeScreenshot(std::string path, float scale, int quality)
172 {
173     return true;
174 }
175
176 long long MockDeviceImpl::getSystemTime(TimeRequestType type)
177 {
178     struct timespec t;
179
180     if (type == TimeRequestType::MONOTONIC) {
181         clock_gettime(CLOCK_MONOTONIC, &t);
182     } else if (type == TimeRequestType::WALLCLOCK) {
183         clock_gettime(CLOCK_REALTIME, &t);
184     }
185
186     return (long long)t.tv_sec * 1000L + (long long)(t.tv_nsec / MICRO_SEC);
187 }
188
189 bool MockDeviceImpl::strokeKeyCode(std::string keycode, unsigned int intv)
190 {
191     return false;
192 }
193
194 bool MockDeviceImpl::pressKeyCode(std::string keycode)
195 {
196     return false;
197 }
198
199 bool MockDeviceImpl::releaseKeyCode(std::string keycode)
200 {
201     return false;
202 }
203
204 int MockDeviceImpl::grabTouchSeqNumber()
205 {
206     return 0;
207 }
208
209 bool MockDeviceImpl::releaseTouchSeqNumber(int seq)
210 {
211     return false;
212 }
213
214 void MockDeviceImpl::startTimer(void)
215 {
216     isTimerStarted = true;
217     clock_gettime(CLOCK_MONOTONIC, &tStart);
218 }
219
220 int MockDeviceImpl::stopTimer(void)
221 {
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;
227 }
228
229 long long MockDeviceImpl::timeStamp(void)
230 {
231     struct timespec t;
232     clock_gettime(CLOCK_MONOTONIC, &t);
233     long long rtn = (t.tv_sec + t.tv_nsec/NANO_SEC) * MICRO_SEC;
234     return rtn;
235 }