Add Axis gesture
This commit is contained in:
parent
ecfd03fd21
commit
10bb10e5c5
|
@ -28,6 +28,7 @@ add_executable(logid
|
||||||
actions/gesture/Gesture.cpp
|
actions/gesture/Gesture.cpp
|
||||||
actions/gesture/ReleaseGesture.cpp
|
actions/gesture/ReleaseGesture.cpp
|
||||||
actions/gesture/IntervalGesture.cpp
|
actions/gesture/IntervalGesture.cpp
|
||||||
|
actions/gesture/AxisGesture.cpp
|
||||||
backend/Error.cpp
|
backend/Error.cpp
|
||||||
backend/raw/DeviceMonitor.cpp
|
backend/raw/DeviceMonitor.cpp
|
||||||
backend/raw/RawDevice.cpp
|
backend/raw/RawDevice.cpp
|
||||||
|
|
|
@ -87,7 +87,7 @@ uint InputDevice::toKeyCode(std::string name)
|
||||||
|
|
||||||
uint InputDevice::toAxisCode(std::string name)
|
uint InputDevice::toAxisCode(std::string name)
|
||||||
{
|
{
|
||||||
return _toEventCode(EV_KEY, std::move(name));
|
return _toEventCode(EV_REL, std::move(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint InputDevice::_toEventCode(uint type, const std::string& name)
|
uint InputDevice::_toEventCode(uint type, const std::string& name)
|
||||||
|
|
126
src/logid/actions/gesture/AxisGesture.cpp
Normal file
126
src/logid/actions/gesture/AxisGesture.cpp
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
/*
|
||||||
|
* 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 <cmath>
|
||||||
|
#include "AxisGesture.h"
|
||||||
|
#include "../../InputDevice.h"
|
||||||
|
#include "../../util/log.h"
|
||||||
|
|
||||||
|
using namespace logid::actions;
|
||||||
|
|
||||||
|
AxisGesture::AxisGesture(Device *device, libconfig::Setting &root) :
|
||||||
|
Gesture (device), _config (device, root)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void AxisGesture::press()
|
||||||
|
{
|
||||||
|
_axis = 0;
|
||||||
|
_axis_remainder = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AxisGesture::release(bool primary)
|
||||||
|
{
|
||||||
|
// Do nothing
|
||||||
|
(void)primary; // Suppress unused warning
|
||||||
|
}
|
||||||
|
|
||||||
|
void AxisGesture::move(int16_t axis)
|
||||||
|
{
|
||||||
|
int16_t new_axis = _axis + axis;
|
||||||
|
if(new_axis > _config.threshold()) {
|
||||||
|
double move = axis;
|
||||||
|
if(_axis < _config.threshold())
|
||||||
|
move = new_axis - _config.threshold();
|
||||||
|
bool negative_multiplier = _config.multiplier() < 0;
|
||||||
|
if(negative_multiplier)
|
||||||
|
move *= -_config.multiplier();
|
||||||
|
else
|
||||||
|
move *= _config.multiplier();
|
||||||
|
|
||||||
|
double move_floor = floor(move);
|
||||||
|
_axis_remainder = move - move_floor;
|
||||||
|
if(_axis_remainder >= 1) {
|
||||||
|
double int_remainder = floor(_axis_remainder);
|
||||||
|
move_floor += int_remainder;
|
||||||
|
_axis_remainder -= int_remainder;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(negative_multiplier)
|
||||||
|
move_floor = -move_floor;
|
||||||
|
|
||||||
|
virtual_input->moveAxis(_config.axis(), move_floor);
|
||||||
|
}
|
||||||
|
_axis = new_axis;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AxisGesture::metThreshold() const
|
||||||
|
{
|
||||||
|
return _axis >= _config.threshold();
|
||||||
|
}
|
||||||
|
|
||||||
|
AxisGesture::Config::Config(Device *device, libconfig::Setting &setting) :
|
||||||
|
Gesture::Config(device, setting, false)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
auto& axis = setting.lookup("axis");
|
||||||
|
if(axis.isNumber()) {
|
||||||
|
_axis = axis;
|
||||||
|
} else if(axis.getType() == libconfig::Setting::TypeString) {
|
||||||
|
try {
|
||||||
|
_axis = virtual_input->toAxisCode(axis);
|
||||||
|
} catch(InputDevice::InvalidEventCode& e) {
|
||||||
|
logPrintf(WARN, "Line %d: Invalid axis %s, skipping."
|
||||||
|
, axis.getSourceLine(), axis.c_str());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logPrintf(WARN, "Line %d: axis must be string or int, skipping.",
|
||||||
|
axis.getSourceLine(), axis.c_str());
|
||||||
|
throw InvalidGesture();
|
||||||
|
}
|
||||||
|
} catch(libconfig::SettingNotFoundException& e) {
|
||||||
|
logPrintf(WARN, "Line %d: axis is a required field, skippimg.",
|
||||||
|
setting.getSourceLine());
|
||||||
|
throw InvalidGesture();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
auto& multiplier = setting.lookup("axis_multiplier");
|
||||||
|
if(multiplier.isNumber()) {
|
||||||
|
if(multiplier.getType() == libconfig::Setting::TypeFloat)
|
||||||
|
_multiplier = multiplier;
|
||||||
|
else
|
||||||
|
_multiplier = (int)multiplier;
|
||||||
|
} else {
|
||||||
|
logPrintf(WARN, "Line %d: axis_multiplier must be a number, "
|
||||||
|
"setting to default (1).",
|
||||||
|
multiplier.getSourceLine());
|
||||||
|
}
|
||||||
|
} catch(libconfig::SettingNotFoundException& e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int AxisGesture::Config::axis() const
|
||||||
|
{
|
||||||
|
return _axis;
|
||||||
|
}
|
||||||
|
|
||||||
|
double AxisGesture::Config::multiplier() const
|
||||||
|
{
|
||||||
|
return _multiplier;
|
||||||
|
}
|
55
src/logid/actions/gesture/AxisGesture.h
Normal file
55
src/logid/actions/gesture/AxisGesture.h
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
/*
|
||||||
|
* 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_AXISGESTURE_H
|
||||||
|
#define LOGID_ACTION_AXISGESTURE_H
|
||||||
|
|
||||||
|
#include "Gesture.h"
|
||||||
|
|
||||||
|
namespace logid {
|
||||||
|
namespace actions
|
||||||
|
{
|
||||||
|
class AxisGesture : public Gesture
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AxisGesture(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);
|
||||||
|
unsigned int axis() const;
|
||||||
|
double multiplier() const;
|
||||||
|
private:
|
||||||
|
unsigned int _axis;
|
||||||
|
double _multiplier = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int16_t _axis;
|
||||||
|
double _axis_remainder;
|
||||||
|
Config _config;
|
||||||
|
};
|
||||||
|
}}
|
||||||
|
|
||||||
|
#endif //LOGID_ACTION_AXISGESTURE_H
|
|
@ -22,6 +22,7 @@
|
||||||
#include "ReleaseGesture.h"
|
#include "ReleaseGesture.h"
|
||||||
#include "../../backend/hidpp20/features/ReprogControls.h"
|
#include "../../backend/hidpp20/features/ReprogControls.h"
|
||||||
#include "IntervalGesture.h"
|
#include "IntervalGesture.h"
|
||||||
|
#include "AxisGesture.h"
|
||||||
|
|
||||||
using namespace logid::actions;
|
using namespace logid::actions;
|
||||||
|
|
||||||
|
@ -51,7 +52,7 @@ Gesture::Config::Config(Device* device, libconfig::Setting& root,
|
||||||
_threshold = (int)threshold;
|
_threshold = (int)threshold;
|
||||||
if(_threshold <= 0) {
|
if(_threshold <= 0) {
|
||||||
_threshold = LOGID_GESTURE_DEFAULT_THRESHOLD;
|
_threshold = LOGID_GESTURE_DEFAULT_THRESHOLD;
|
||||||
logPrintf(WARN, "Line %d: threshold must be positive, setting"
|
logPrintf(WARN, "Line %d: threshold must be positive, setting "
|
||||||
"to default (%d)", threshold.getSourceLine(),
|
"to default (%d)", threshold.getSourceLine(),
|
||||||
_threshold);
|
_threshold);
|
||||||
}
|
}
|
||||||
|
@ -87,8 +88,10 @@ std::shared_ptr<Gesture> Gesture::makeGesture(Device *device,
|
||||||
|
|
||||||
if(type == "onrelease")
|
if(type == "onrelease")
|
||||||
return std::make_shared<ReleaseGesture>(device, setting);
|
return std::make_shared<ReleaseGesture>(device, setting);
|
||||||
if(type == "oninterval" || type == "onfewpixels")
|
else if(type == "oninterval" || type == "onfewpixels")
|
||||||
return std::make_shared<IntervalGesture>(device, setting);
|
return std::make_shared<IntervalGesture>(device, setting);
|
||||||
|
else if(type == "axis")
|
||||||
|
return std::make_shared<AxisGesture>(device, setting);
|
||||||
else {
|
else {
|
||||||
logPrintf(WARN, "Line %d: Unknown gesture mode %s, defaulting to "
|
logPrintf(WARN, "Line %d: Unknown gesture mode %s, defaulting to "
|
||||||
"OnRelease.", gesture_mode.getSourceLine(),
|
"OnRelease.", gesture_mode.getSourceLine(),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user