Skip to content

Commit

Permalink
config: Add option to set the prediction target
Browse files Browse the repository at this point in the history
  • Loading branch information
StollD committed Apr 24, 2023
1 parent 474e8bc commit 83cdd66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 7 additions & 0 deletions etc/iptsd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@
##
# TipDistance = 0

##
## Reduces the latency of the stylus by using input prediction. This option is the amount
## of milliseconds that the predictor will look into the future. Larger values will be more
## unstable. A good starting point is 50 milliseconds.
##
# PredictionTarget = 0

[Cone]
##
## The wideness of the cone in degrees.
Expand Down
23 changes: 15 additions & 8 deletions src/apps/daemon/stylus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "uinput-device.hpp"

#include <common/chrono.hpp>
#include <common/types.hpp>
#include <core/generic/config.hpp>
#include <core/generic/device.hpp>
Expand Down Expand Up @@ -43,8 +44,12 @@ class StylusDevice {
// Kalman-based input predictor to reduce latency.
prediction::SinglePointerPredictor m_predictor {};

// How many milliseconds the predictor will look ahead.
milliseconds<usize> m_prediction_target;

public:
StylusDevice(const core::Config &config, const core::DeviceInfo &info)
: m_prediction_target {config.stylus_prediction_target}
{
m_uinput->set_name("IPTS Stylus");
m_uinput->set_vendor(info.vendor);
Expand Down Expand Up @@ -86,7 +91,7 @@ class StylusDevice {
void update(const ipts::StylusData &data)
{
// If the stylus moves to proximity, reset the predictor
if (!m_active && data.proximity)
if (!m_last.contact && data.contact)
m_predictor.reset();

m_active = data.proximity;
Expand All @@ -100,15 +105,17 @@ class StylusDevice {
f64 cy = data.y;
f64 cp = data.pressure;

m_predictor.update(Vector2<f64> {cx, cy}, cp);
// Run prediction if the user enabled it.
if (m_prediction_target.count() > 0 && data.contact) {
m_predictor.update(Vector2<f64> {cx, cy}, cp);

// If the predictor was able to predict something, update our values.
if (m_predictor.predict(100ms)) {
const Vector2<f64> pos = m_predictor.position();
if (m_predictor.predict(m_prediction_target)) {
const Vector2<f64> pos = m_predictor.position();

cx = pos.x();
cy = pos.y();
cp = m_predictor.pressure();
cx = pos.x();
cy = pos.y();
cp = m_predictor.pressure();
}
}

const Vector2<i32> tilt = calculate_tilt(data.altitude, data.azimuth);
Expand Down
1 change: 1 addition & 0 deletions src/core/generic/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Config {
// [Stylus]
bool stylus_disable = false;
f64 stylus_tip_distance = 0;
usize stylus_prediction_target = 0;

// [Cone]
f64 cone_angle = 30;
Expand Down
1 change: 1 addition & 0 deletions src/core/linux/config-loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class ConfigLoader {

this->get(ini, "Stylus", "Disable", m_config.stylus_disable);
this->get(ini, "Stylus", "TipDistance", m_config.stylus_tip_distance);
this->get(ini, "Stylus", "PredictionTarget", m_config.stylus_prediction_target);

this->get(ini, "Cone", "Angle", m_config.cone_angle);
this->get(ini, "Cone", "Distance", m_config.cone_distance);
Expand Down

0 comments on commit 83cdd66

Please sign in to comment.