1 #include "DeviceImpl/TizenImpl.h"
13 TizenImpl::TizenImpl()
14 : mFakeTouchHandle{0}, mFakeKeyboardHandle{0}, mFakeWheelHandle{0}, isTimerStarted{false}
16 LOG_SCOPE_F(INFO, "device implementation init");
17 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
18 TizenImpl *obj = static_cast<TizenImpl*>(data);
19 obj->mFakeTouchHandle = efl_util_input_initialize_generator(EFL_UTIL_INPUT_DEVTYPE_TOUCHSCREEN);
20 obj->mFakeKeyboardHandle =
21 efl_util_input_initialize_generator(EFL_UTIL_INPUT_DEVTYPE_KEYBOARD);
22 obj->mFakeWheelHandle = efl_util_input_initialize_generator(EFL_UTIL_INPUT_DEVTYPE_POINTER);
27 TizenImpl::~TizenImpl()
29 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
30 TizenImpl *obj = static_cast<TizenImpl*>(data);
31 efl_util_input_deinitialize_generator(obj->mFakeTouchHandle);
32 efl_util_input_deinitialize_generator(obj->mFakeKeyboardHandle);
33 efl_util_input_deinitialize_generator(obj->mFakeWheelHandle);
38 bool TizenImpl::click(const int x, const int y)
40 return click(x, y, INTV_CLICK);
43 bool TizenImpl::click(const int x, const int y, const unsigned int intv)
46 usleep(intv * MSEC_PER_SEC);
53 bool TizenImpl::touchDown(const int x, const int y)
55 LOG_F(INFO, "touch down %d %d", x, y);
56 auto args = std::make_tuple(this, x, y);
57 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
60 std::tie(obj, x, y) = *static_cast<std::tuple<TizenImpl*, int, int>*>(data);
61 efl_util_input_generate_touch(obj->mFakeTouchHandle, 0, EFL_UTIL_INPUT_TOUCH_BEGIN,
69 bool TizenImpl::touchMove(const int x, const int y)
71 LOG_F(INFO, "touch move %d %d", x, y);
72 auto args = std::make_tuple(this, x, y);
73 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
76 std::tie(obj, x, y) = *static_cast<std::tuple<TizenImpl*, int, int>*>(data);
78 efl_util_input_generate_touch(obj->mFakeTouchHandle, 0, EFL_UTIL_INPUT_TOUCH_UPDATE,
86 bool TizenImpl::touchUp(const int x, const int y)
88 LOG_F(INFO, "touch up %d %d", x, y);
89 auto args = std::make_tuple(this, x, y);
90 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
93 std::tie(obj, x, y) = *static_cast<std::tuple<TizenImpl*, int, int>*>(data);
94 efl_util_input_generate_touch(obj->mFakeTouchHandle, 0, EFL_UTIL_INPUT_TOUCH_END,
101 bool TizenImpl::wheelUp(int amount, const int durationMs)
103 LOG_F(INFO, "wheel up %d for %d", amount, durationMs);
104 auto args = std::make_tuple(this);
105 for (int i = 0; i < amount; i++){
106 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
108 std::tie(obj) = *static_cast<std::tuple<TizenImpl*>*>(data);
109 efl_util_input_generate_wheel(obj->mFakeWheelHandle, EFL_UTIL_INPUT_POINTER_WHEEL_HORZ, 1);
112 usleep(durationMs*MSEC_PER_SEC/amount);
118 bool TizenImpl::wheelDown(int amount, const int durationMs)
120 LOG_F(INFO, "wheel down %d for %d", amount, durationMs);
121 auto args = std::make_tuple(this);
122 for (int i = 0; i < amount; i++){
123 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
125 std::tie(obj) = *static_cast<std::tuple<TizenImpl*>*>(data);
126 efl_util_input_generate_wheel(obj->mFakeWheelHandle, EFL_UTIL_INPUT_POINTER_WHEEL_HORZ, -1);
129 usleep(durationMs*MSEC_PER_SEC/amount);
134 void TizenImpl::startTimer(void)
136 isTimerStarted = true;
137 clock_gettime(CLOCK_MONOTONIC, &tStart);
140 int TizenImpl::stopTimer(void)
142 struct timespec tEnd;
143 if (!isTimerStarted) return 0;
144 isTimerStarted = false;
145 clock_gettime(CLOCK_MONOTONIC, &tEnd);
146 return ((tEnd.tv_sec + tEnd.tv_nsec/1000000000.0) - (tStart.tv_sec + tStart.tv_nsec/1000000000.0)) * 1000000;
149 bool TizenImpl::drag(const int sx, const int sy, const int ex, const int ey,
150 const int steps, const int durationMs)
153 int _durationUs, _steps, _stepUs;
157 if (steps <= 0) _steps = 1;
160 if (durationMs < MINIMUM_DURATION_DRAG) _durationUs = INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC;
161 else _durationUs = durationMs * MSEC_PER_SEC;
163 _stepUs = _durationUs / (_steps +1);
165 if (_stepUs < (INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC)) {
166 _steps = (int)(_durationUs / (INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC)) - 1;
167 _stepUs = (INTV_MINIMUM_DRAG_MS * MSEC_PER_SEC);
169 LOG_SCOPE_F(INFO, "flicking (%d, %d) -> (%d, %d) for (%d ms)", sx, sy, ex, ey, durationMs);
172 consumptionUs = stopTimer();
174 for ( int s = 1; s <= _steps + 1; s++) {
175 usleep((_stepUs - consumptionUs)>INTV_MINIMUM_USLEEP?(_stepUs - consumptionUs):INTV_MINIMUM_USLEEP);
177 touchMove(sx + (ex - sx) * s / (steps + 1), sy + (ey - sy) * s / (steps + 1));
178 consumptionUs = stopTimer();
180 usleep((_stepUs - consumptionUs)>INTV_MINIMUM_USLEEP?(_stepUs - consumptionUs):INTV_MINIMUM_USLEEP);
187 bool TizenImpl::pressBack()
189 return pressKeyCode("XF86Back");
192 bool TizenImpl::pressHome()
194 return pressKeyCode("XF86Home");
197 bool TizenImpl::pressMenu()
199 return pressKeyCode("XF86Menu");
202 bool TizenImpl::pressVolUp()
204 return pressKeyCode("XF86AudioRaiseVolume");
207 bool TizenImpl::pressVolDown()
209 return pressKeyCode("XF86AudioLowerVolume");
212 bool TizenImpl::pressPower()
214 return pressKeyCode("XF86PowerOff");
217 bool TizenImpl::pressKeyCode(std::string keycode)
219 return pressKeyCode(keycode, INTV_KEYPRESS);
222 bool TizenImpl::pressKeyCode(std::string keycode, unsigned int intv)
224 auto args = std::make_tuple(this, keycode);
225 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
228 std::tie(obj, keycode) = *static_cast<std::tuple<TizenImpl*, std::string>*>(data);
229 efl_util_input_generate_key(obj->mFakeKeyboardHandle, keycode.c_str(), 1);
233 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{return NULL;}, NULL);
236 ecore_main_loop_thread_safe_call_sync([](void *data)->void*{
239 std::tie(obj, keycode) = *static_cast<std::tuple<TizenImpl*, std::string>*>(data);
240 efl_util_input_generate_key(obj->mFakeKeyboardHandle, keycode.c_str(), 0);
247 bool TizenImpl::takeScreenshot(std::string path, float scale, int quality)
257 virtual long long getTime() = 0;
259 long long convertTime(struct timespec t){
260 return (long long)t.tv_sec * 1000L + (long long)(t.tv_nsec / 1000000);
264 class MonotonicClock : public Clock {
266 virtual ~MonotonicClock(){};
268 long long getTime() override {
270 clock_gettime(CLOCK_MONOTONIC, &t);
271 return convertTime(t);
276 class WallClock : public Clock {
278 virtual ~WallClock(){};
280 long long getTime() override {
282 clock_gettime(CLOCK_REALTIME, &t);
283 return convertTime(t);
287 long long TizenImpl::getSystemTime(TypeRequestType type)
289 std::unique_ptr<Clock> clock;
290 if (type == TypeRequestType::MONOTONIC)
291 clock = std::make_unique<MonotonicClock>();
292 else if (type == TypeRequestType::WALLCLOCK)
293 clock = std::make_unique<WallClock>();
295 return clock->getTime();