Add Gesture support
Only supports OnRelease right now. Also, some bugs were spotted while writing this: - Sometimes deadlocks on startup (cause unknown) - Sometimes valid CIDs will be unknown (bug may have been fixed?)
This commit is contained in:
109
src/logid/actions/gesture/Gesture.cpp
Normal file
109
src/logid/actions/gesture/Gesture.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include "Gesture.h"
|
||||
#include "../../util/log.h"
|
||||
#include "ReleaseGesture.h"
|
||||
#include "../../backend/hidpp20/features/ReprogControls.h"
|
||||
|
||||
using namespace logid::actions;
|
||||
|
||||
Gesture::Gesture(Device *device) : _device (device)
|
||||
{
|
||||
}
|
||||
|
||||
Gesture::Config::Config(Device* device, libconfig::Setting& root,
|
||||
bool action_required) : _device (device)
|
||||
{
|
||||
if(action_required) {
|
||||
try {
|
||||
_action = Action::makeAction(_device,
|
||||
root.lookup("action"));
|
||||
} catch (libconfig::SettingNotFoundException &e) {
|
||||
throw InvalidGesture("action is missing");
|
||||
}
|
||||
|
||||
if(_action->reprogFlags() & backend::hidpp20::ReprogControls::RawXYDiverted)
|
||||
throw InvalidGesture("gesture cannot require RawXY");
|
||||
}
|
||||
|
||||
_threshold = LOGID_GESTURE_DEFAULT_THRESHOLD;
|
||||
try {
|
||||
auto& threshold = root.lookup("threshold");
|
||||
if(threshold.getType() == libconfig::Setting::TypeInt) {
|
||||
_threshold = (int)threshold;
|
||||
if(_threshold <= 0) {
|
||||
_threshold = LOGID_GESTURE_DEFAULT_THRESHOLD;
|
||||
logPrintf(WARN, "Line %d: threshold must be positive, setting"
|
||||
"to default (%d)", threshold.getSourceLine(),
|
||||
_threshold);
|
||||
}
|
||||
} else
|
||||
logPrintf(WARN, "Line %d: threshold must be an integer, setting "
|
||||
"to default (%d).", threshold.getSourceLine());
|
||||
} catch(libconfig::SettingNotFoundException& e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Gesture> Gesture::makeGesture(Device *device,
|
||||
libconfig::Setting &setting)
|
||||
{
|
||||
if(!setting.isGroup()) {
|
||||
logPrintf(WARN, "Line %d: Gesture is not a group, ignoring.",
|
||||
setting.getSourceLine());
|
||||
throw InvalidGesture();
|
||||
}
|
||||
|
||||
try {
|
||||
auto& gesture_mode = setting.lookup("mode");
|
||||
|
||||
if(gesture_mode.getType() != libconfig::Setting::TypeString) {
|
||||
logPrintf(WARN, "Line %d: Gesture mode must be a string,"
|
||||
"defaulting to OnRelease.",
|
||||
gesture_mode.getSourceLine());
|
||||
return std::make_shared<ReleaseGesture>(device, setting);
|
||||
}
|
||||
|
||||
std::string type = gesture_mode;
|
||||
std::transform(type.begin(), type.end(), type.begin(), ::tolower);
|
||||
|
||||
if(type == "onrelease")
|
||||
return std::make_shared<ReleaseGesture>(device, setting);
|
||||
else {
|
||||
logPrintf(WARN, "Line %d: Unknown gesture mode %s, defaulting to "
|
||||
"OnRelease.", gesture_mode.getSourceLine(),
|
||||
(const char*)gesture_mode);
|
||||
return std::make_shared<ReleaseGesture>(device, setting);
|
||||
}
|
||||
|
||||
} catch(libconfig::SettingNotFoundException& e) {
|
||||
return std::make_shared<ReleaseGesture>(device, setting);
|
||||
}
|
||||
}
|
||||
|
||||
int16_t Gesture::Config::threshold() const
|
||||
{
|
||||
return _threshold;
|
||||
}
|
||||
|
||||
std::shared_ptr<Action> Gesture::Config::action()
|
||||
{
|
||||
return _action;
|
||||
}
|
73
src/logid/actions/gesture/Gesture.h
Normal file
73
src/logid/actions/gesture/Gesture.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#ifndef LOGID_ACTION_GESTURE_H
|
||||
#define LOGID_ACTION_GESTURE_H
|
||||
|
||||
#include "../Action.h"
|
||||
|
||||
#define LOGID_GESTURE_DEFAULT_THRESHOLD 50
|
||||
|
||||
namespace logid {
|
||||
namespace actions
|
||||
{
|
||||
class InvalidGesture : public std::exception
|
||||
{
|
||||
public:
|
||||
explicit InvalidGesture(std::string what="") : _what (what)
|
||||
{
|
||||
}
|
||||
virtual const char* what()
|
||||
{
|
||||
return _what.c_str();
|
||||
}
|
||||
private:
|
||||
std::string _what;
|
||||
};
|
||||
|
||||
class Gesture
|
||||
{
|
||||
public:
|
||||
virtual void press() = 0;
|
||||
virtual void release(bool primary=false) = 0;
|
||||
virtual void move(int16_t axis) = 0;
|
||||
|
||||
virtual bool metThreshold() const = 0;
|
||||
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
Config(Device* device, libconfig::Setting& root,
|
||||
bool action_required=true);
|
||||
virtual int16_t threshold() const;
|
||||
virtual std::shared_ptr<Action> action();
|
||||
protected:
|
||||
Device* _device;
|
||||
std::shared_ptr<Action> _action;
|
||||
int16_t _threshold;
|
||||
};
|
||||
|
||||
static std::shared_ptr<Gesture> makeGesture(Device* device,
|
||||
libconfig::Setting& setting);
|
||||
|
||||
protected:
|
||||
explicit Gesture(Device* device);
|
||||
Device* _device;
|
||||
};
|
||||
}}
|
||||
|
||||
#endif //LOGID_ACTION_GESTURE_H
|
49
src/logid/actions/gesture/ReleaseGesture.cpp
Normal file
49
src/logid/actions/gesture/ReleaseGesture.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#include "ReleaseGesture.h"
|
||||
#include "../../util/log.h"
|
||||
|
||||
using namespace logid::actions;
|
||||
|
||||
ReleaseGesture::ReleaseGesture(Device *device, libconfig::Setting &root) :
|
||||
Gesture (device), _config (device, root)
|
||||
{
|
||||
}
|
||||
|
||||
void ReleaseGesture::press()
|
||||
{
|
||||
_axis = 0;
|
||||
}
|
||||
|
||||
void ReleaseGesture::release(bool primary)
|
||||
{
|
||||
if(metThreshold() && primary) {
|
||||
_config.action()->press();
|
||||
_config.action()->release();
|
||||
}
|
||||
}
|
||||
|
||||
void ReleaseGesture::move(int16_t axis)
|
||||
{
|
||||
_axis += axis;
|
||||
}
|
||||
|
||||
bool ReleaseGesture::metThreshold() const
|
||||
{
|
||||
return _axis >= _config.threshold();
|
||||
}
|
43
src/logid/actions/gesture/ReleaseGesture.h
Normal file
43
src/logid/actions/gesture/ReleaseGesture.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#ifndef LOGID_ACTION_RELEASEGESTURE_H
|
||||
#define LOGID_ACTION_RELEASEGESTURE_H
|
||||
|
||||
#include "Gesture.h"
|
||||
|
||||
namespace logid {
|
||||
namespace actions
|
||||
{
|
||||
class ReleaseGesture : public Gesture
|
||||
{
|
||||
public:
|
||||
ReleaseGesture(Device* device, libconfig::Setting& root);
|
||||
|
||||
virtual void press();
|
||||
virtual void release(bool primary=false);
|
||||
virtual void move(int16_t axis);
|
||||
|
||||
virtual bool metThreshold() const;
|
||||
|
||||
protected:
|
||||
int16_t _axis;
|
||||
Gesture::Config _config;
|
||||
};
|
||||
}}
|
||||
|
||||
#endif //LOGID_ACTION_RELEASEGESTURE_H
|
Reference in New Issue
Block a user