From a1d6e1de80bda99c53901263fd084f8a18d81052 Mon Sep 17 00:00:00 2001 From: Lukasz Wlazly Date: Tue, 26 Sep 2017 17:04:09 +0200 Subject: [PATCH] Usage of efl_util to generate key events Change-Id: I14f6e8b22dee2fc1b15679911dbf86caa0afccdd --- org.tizen.universal-switch.xml | 1 + src/ecore.cpp | 64 ------------------------------ src/ecore.hpp | 20 ---------- src/utils.cpp | 59 +++++++++++++++++++++++++++ src/utils.hpp | 33 +++++++++++++++ tests/org.tizen.universal-switch-tests.xml | 1 + tests/ui-scenarios/ConfigurationTests.hpp | 17 +++++--- 7 files changed, 106 insertions(+), 89 deletions(-) create mode 100644 src/utils.cpp diff --git a/org.tizen.universal-switch.xml b/org.tizen.universal-switch.xml index e45f611..fbe05dd 100644 --- a/org.tizen.universal-switch.xml +++ b/org.tizen.universal-switch.xml @@ -15,6 +15,7 @@ http://tizen.org/privilege/systemsettings.admin http://tizen.org/privilege/appmanager.launch http://tizen.org/privilege/keygrab + http://tizen.org/privilege/inputgenerator diff --git a/src/ecore.cpp b/src/ecore.cpp index 0998ab3..e146b1d 100644 --- a/src/ecore.cpp +++ b/src/ecore.cpp @@ -37,7 +37,6 @@ namespace ecore timerHandler = ecore_timer_add(times.front(), callbackMethod, this); } - bool Timer::isSet() { return timerHandler; @@ -69,67 +68,4 @@ namespace ecore auto t = static_cast(data); return t->onCallback(); } - - struct GeneratorData { - char *key; - unsigned multiplicity; - double releaseDelay; - }; - - void EventGenerator::generateKeyPress(const std::string &keyId, unsigned multiplicity, PressType type) - { - auto delay = 0.0; - switch (type) { - case PressType::SHORT: - delay = SHORT_DELAY; - break; - - case PressType::LONG: - delay = LONG_DELAY; - } - - auto genData = new GeneratorData{strdup(keyId.c_str()), multiplicity, delay}; - ecore_timer_add(NO_DELAY, keyDownTimerCb, genData); - } - - Eina_Bool EventGenerator::keyDownTimerCb(void *data) - { - auto genData = static_cast(data); - - auto eventDown = new Ecore_Event_Key; - eventDown->key = strdup(genData->key); - - ecore_event_add(ECORE_EVENT_KEY_DOWN, eventDown, onEventDeletion, NULL); - ecore_timer_add(genData->releaseDelay, keyUpTimerCb, genData); - - return ECORE_CALLBACK_CANCEL; - } - - Eina_Bool EventGenerator::keyUpTimerCb(void *data) - { - auto genData = static_cast(data); - - auto eventUp = new Ecore_Event_Key; - eventUp->key = strdup(genData->key); - - ecore_event_add(ECORE_EVENT_KEY_UP, eventUp, onEventDeletion, NULL); - - --(genData->multiplicity); - if (genData->multiplicity > 0) { - ecore_timer_add(SHORT_DELAY, keyDownTimerCb, genData); - } else { - free(genData->key); - delete genData; - } - - return ECORE_CALLBACK_CANCEL; - } - - void EventGenerator::onEventDeletion(void *user_data, void *func_data) - { - auto event = static_cast(func_data); - free(const_cast(event->key)); - - delete event; - } } diff --git a/src/ecore.hpp b/src/ecore.hpp index df450ad..8433676 100644 --- a/src/ecore.hpp +++ b/src/ecore.hpp @@ -79,26 +79,6 @@ namespace ecore Ecore_Event_Handler *eventHandler; std::function method; }; - - class EventGenerator - { - public: - enum class PressType { - SHORT, - LONG - }; - - static void generateKeyPress(const std::string &keyId, unsigned multiplicity = SINGLE_PRESS, PressType type = PressType::SHORT); - - private: - static Eina_Bool keyDownTimerCb(void *data); - static Eina_Bool keyUpTimerCb(void *data); - static void onEventDeletion(void *user_data, void *func_data); - static constexpr unsigned SINGLE_PRESS = 1; - static constexpr double SHORT_DELAY = 0.1; - static constexpr double LONG_DELAY = 1.0; - static constexpr double NO_DELAY = 0.0; - }; } template diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..ce3c4eb --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,59 @@ +#include "utils.hpp" + +#include "UniversalSwitchLog.hpp" + +#include + +namespace utils +{ + ecore::Timer EventGenerator::timer = {}; + + void EventGenerator::generateKeyPress(const std::string &keyId, unsigned multiplicity, PressType type) + { + auto delay = 0.0; + switch (type) { + case PressType::SHORT: + delay = SHORT_DELAY; + break; + + case PressType::LONG: + delay = LONG_DELAY; + break; + + default: + ASSERT(0, "Invalid press type"); + } + + GeneratorData genData{keyId, multiplicity, delay}; + + timer.reset(NO_DELAY, [genData]() { + return generateKeyDown(genData); + }); + } + + ecore::TimerRepetitionPolicy EventGenerator::generateKeyDown(GeneratorData genData) + { + auto inputgen = efl_util_input_initialize_generator(EFL_UTIL_INPUT_DEVTYPE_KEYBOARD); + efl_util_input_generate_key(inputgen, genData.key.c_str(), KEY_DOWN); + + timer.reset(genData.releaseDelay, [genData]() { + return generateKeyUp(genData); + }); + + return ecore::TimerRepetitionPolicy::cancel; + } + + ecore::TimerRepetitionPolicy EventGenerator::generateKeyUp(GeneratorData genData) + { + auto inputgen = efl_util_input_initialize_generator(EFL_UTIL_INPUT_DEVTYPE_KEYBOARD); + efl_util_input_generate_key(inputgen, genData.key.c_str(), KEY_UP); + + --(genData.multiplicity); + if (genData.multiplicity > 0) + timer.reset(SHORT_DELAY, [genData]() { + return generateKeyDown(genData); + }); + + return ecore::TimerRepetitionPolicy::cancel; + } +} diff --git a/src/utils.hpp b/src/utils.hpp index 98aed69..1fd52e8 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,6 +1,9 @@ #ifndef UTILS_HPP #define UTILS_HPP +#include "ecore.hpp" + +#include namespace utils { @@ -14,6 +17,36 @@ namespace utils v = hi; return v; } + + class EventGenerator + { + public: + enum class PressType { + SHORT, + LONG + }; + + struct GeneratorData { + std::string key; + unsigned multiplicity; + double releaseDelay; + }; + + static void generateKeyPress(const std::string &keyId, unsigned multiplicity = SINGLE_PRESS, PressType type = PressType::SHORT); + + private: + static ecore::TimerRepetitionPolicy generateKeyDown(GeneratorData genData); + static ecore::TimerRepetitionPolicy generateKeyUp(GeneratorData genData); + + static ecore::Timer timer; + + static constexpr unsigned SINGLE_PRESS = 1; + static constexpr int KEY_DOWN = 1; + static constexpr int KEY_UP = 0; + static constexpr double SHORT_DELAY = 0.1; + static constexpr double LONG_DELAY = 1.0; + static constexpr double NO_DELAY = 0.0; + }; } #endif diff --git a/tests/org.tizen.universal-switch-tests.xml b/tests/org.tizen.universal-switch-tests.xml index 55bd452..b3b5dd8 100644 --- a/tests/org.tizen.universal-switch-tests.xml +++ b/tests/org.tizen.universal-switch-tests.xml @@ -11,6 +11,7 @@ http://tizen.org/privilege/window.priority.set + http://tizen.org/privilege/inputgenerator diff --git a/tests/ui-scenarios/ConfigurationTests.hpp b/tests/ui-scenarios/ConfigurationTests.hpp index f9efa35..c1f022c 100644 --- a/tests/ui-scenarios/ConfigurationTests.hpp +++ b/tests/ui-scenarios/ConfigurationTests.hpp @@ -10,7 +10,7 @@ #include "SwitchConfigurationItem.hpp" #include "SwitchManager.hpp" #include "ActivityFactory.hpp" -#include "ecore.hpp" +#include "utils.hpp" #include @@ -73,13 +73,13 @@ public: void simulateKeyPress(const std::string &key) { - ecore::EventGenerator::generateKeyPress(key); + EXPECT_TRUE(ecore_timer_add(SMALL_DELAY_, addEventCb, key.c_str())); runMainLoop(); } void runMainLoop() { - EXPECT_TRUE(ecore_timer_add(estimatedTimeRequiredToSafelyPassingThroughTests_, mainLoopQuitCb, nullptr)); + EXPECT_TRUE(ecore_timer_add(ESTIMATED_TIME_REQUIRED_TO_SAFELY_PASSING_THROUGH_TESTS_, mainLoopQuitCb, nullptr)); ecore_main_loop_begin(); } @@ -89,6 +89,13 @@ public: return ECORE_CALLBACK_CANCEL; } + static Eina_Bool addEventCb(void *data) + { + auto key = static_cast(data); + utils::EventGenerator::generateKeyPress(key); + return ECORE_CALLBACK_CANCEL; + } + protected: std::shared_ptr configuration_; std::shared_ptr switchProvider_; @@ -98,8 +105,8 @@ protected: std::string key_ = "Down"; std::string switchId_ = "AccessoriesSwitchProvider_Down"; std::string activityType_ = "TWO_STEP_PROCESS_ACTIVITY"; - double estimatedTimeRequiredToSafelyPassingThroughTests_ = 1.0; - double smallDelayToSimulateKeyRelease = 0.1; + static constexpr double ESTIMATED_TIME_REQUIRED_TO_SAFELY_PASSING_THROUGH_TESTS_ = 1.0; + static constexpr double SMALL_DELAY_ = 0.1; }; class SwitchManager_ConfigurationTests : public SwitchManager -- 2.7.4