From ecfd03fd216a61338d2b52546225631c3599485e Mon Sep 17 00:00:00 2001 From: pixl Date: Sat, 11 Jul 2020 20:28:00 -0400 Subject: [PATCH] Add OnInterval/OnFewPixels gesture --- src/logid/CMakeLists.txt | 1 + src/logid/actions/gesture/Gesture.cpp | 3 + src/logid/actions/gesture/IntervalGesture.cpp | 91 +++++++++++++++++++ src/logid/actions/gesture/IntervalGesture.h | 53 +++++++++++ src/logid/actions/gesture/ReleaseGesture.cpp | 1 - 5 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 src/logid/actions/gesture/IntervalGesture.cpp create mode 100644 src/logid/actions/gesture/IntervalGesture.h diff --git a/src/logid/CMakeLists.txt b/src/logid/CMakeLists.txt index c279ff8..c50a83f 100644 --- a/src/logid/CMakeLists.txt +++ b/src/logid/CMakeLists.txt @@ -27,6 +27,7 @@ add_executable(logid actions/GestureAction.cpp actions/gesture/Gesture.cpp actions/gesture/ReleaseGesture.cpp + actions/gesture/IntervalGesture.cpp backend/Error.cpp backend/raw/DeviceMonitor.cpp backend/raw/RawDevice.cpp diff --git a/src/logid/actions/gesture/Gesture.cpp b/src/logid/actions/gesture/Gesture.cpp index 4691d4e..bebba9b 100644 --- a/src/logid/actions/gesture/Gesture.cpp +++ b/src/logid/actions/gesture/Gesture.cpp @@ -21,6 +21,7 @@ #include "../../util/log.h" #include "ReleaseGesture.h" #include "../../backend/hidpp20/features/ReprogControls.h" +#include "IntervalGesture.h" using namespace logid::actions; @@ -86,6 +87,8 @@ std::shared_ptr Gesture::makeGesture(Device *device, if(type == "onrelease") return std::make_shared(device, setting); + if(type == "oninterval" || type == "onfewpixels") + return std::make_shared(device, setting); else { logPrintf(WARN, "Line %d: Unknown gesture mode %s, defaulting to " "OnRelease.", gesture_mode.getSourceLine(), diff --git a/src/logid/actions/gesture/IntervalGesture.cpp b/src/logid/actions/gesture/IntervalGesture.cpp new file mode 100644 index 0000000..355c230 --- /dev/null +++ b/src/logid/actions/gesture/IntervalGesture.cpp @@ -0,0 +1,91 @@ +/* + * Copyright 2019-2020 PixlOne + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#include "IntervalGesture.h" +#include "../../util/log.h" + +using namespace logid::actions; + +IntervalGesture::IntervalGesture(Device *device, libconfig::Setting &root) : + Gesture (device), _config (device, root) +{ +} + +void IntervalGesture::press() +{ + _axis = 0; + _interval_pass_count = 0; +} + +void IntervalGesture::release(bool primary) +{ + // Do nothing + (void)primary; // Suppress unused warning +} + +void IntervalGesture::move(int16_t axis) +{ + _axis += axis; + if(_axis < _config.threshold()) + return; + + int16_t new_interval_count = (_axis - _config.threshold())/ + _config.interval(); + if(new_interval_count > _interval_pass_count) { + _config.action()->press(); + _config.action()->release(); + } + _interval_pass_count = new_interval_count; +} + +bool IntervalGesture::metThreshold() const +{ + return _axis >= _config.threshold(); +} + +IntervalGesture::Config::Config(Device *device, libconfig::Setting &setting) : + Gesture::Config(device, setting) +{ + try { + auto& interval = setting.lookup("interval"); + if(interval.getType() != libconfig::Setting::TypeInt) { + logPrintf(WARN, "Line %d: interval must be an integer, skipping.", + interval.getSourceLine()); + throw InvalidGesture(); + } + _interval = (int)interval; + } catch(libconfig::SettingNotFoundException& e) { + try { + // pixels is an alias for interval + auto& interval = setting.lookup("pixels"); + if(interval.getType() != libconfig::Setting::TypeInt) { + logPrintf(WARN, "Line %d: pixels must be an integer, skipping.", + interval.getSourceLine()); + throw InvalidGesture(); + } + _interval = (int)interval; + } catch(libconfig::SettingNotFoundException& e) { + logPrintf(WARN, "Line %d: interval is a required field, skipping.", + setting.getSourceLine()); + } + } +} + +int16_t IntervalGesture::Config::interval() const +{ + return _interval; +} \ No newline at end of file diff --git a/src/logid/actions/gesture/IntervalGesture.h b/src/logid/actions/gesture/IntervalGesture.h new file mode 100644 index 0000000..29dcb47 --- /dev/null +++ b/src/logid/actions/gesture/IntervalGesture.h @@ -0,0 +1,53 @@ +/* + * Copyright 2019-2020 PixlOne + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#ifndef LOGID_ACTION_INTERVALGESTURE_H +#define LOGID_ACTION_INTERVALGESTURE_H + +#include "Gesture.h" + +namespace logid { +namespace actions +{ + class IntervalGesture : public Gesture + { + public: + IntervalGesture(Device* device, libconfig::Setting& root); + + virtual void press(); + virtual void release(bool primary=false); + virtual void move(int16_t axis); + + virtual bool metThreshold() const; + + class Config : public Gesture::Config + { + public: + Config(Device* device, libconfig::Setting& setting); + int16_t interval() const; + private: + int16_t _interval; + }; + + protected: + int16_t _axis; + int16_t _interval_pass_count; + Config _config; + }; +}} + +#endif //LOGID_ACTION_INTERVALGESTURE_H diff --git a/src/logid/actions/gesture/ReleaseGesture.cpp b/src/logid/actions/gesture/ReleaseGesture.cpp index 1e14891..52b3ea1 100644 --- a/src/logid/actions/gesture/ReleaseGesture.cpp +++ b/src/logid/actions/gesture/ReleaseGesture.cpp @@ -16,7 +16,6 @@ * */ #include "ReleaseGesture.h" -#include "../../util/log.h" using namespace logid::actions;