2019-09-04 15:11:45 +10:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2013-2019 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2020-03-22 10:31:44 +10:00
|
|
|
#include <assert.h>
|
2024-10-21 11:07:14 +10:00
|
|
|
#include <errno.h>
|
2025-07-01 16:30:11 +10:00
|
|
|
#include <linux/input.h>
|
2025-12-01 09:17:37 +10:00
|
|
|
#include <stdbool.h>
|
2019-09-04 15:11:45 +10:00
|
|
|
#include <stdint.h>
|
2025-07-01 16:30:11 +10:00
|
|
|
#include <time.h>
|
2019-09-04 15:11:45 +10:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2020-03-22 10:31:44 +10:00
|
|
|
#include "util-macros.h"
|
2025-12-01 09:17:37 +10:00
|
|
|
#include "util-newtype.h"
|
2020-03-22 10:31:44 +10:00
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
DECLARE_NEWTYPE(usec, uint64_t);
|
|
|
|
|
|
|
|
|
|
static inline usec_t
|
|
|
|
|
usec_from_millis(uint32_t millis)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_from_uint64_t(millis * 1000);
|
2019-09-04 15:11:45 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline usec_t
|
|
|
|
|
usec_from_seconds(uint32_t secs)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_from_millis(secs * 1000);
|
2019-09-04 15:11:45 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline usec_t
|
|
|
|
|
usec_from_hours(uint32_t hours)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_from_seconds(hours * 3600);
|
2019-09-04 15:11:45 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline uint32_t
|
|
|
|
|
usec_to_millis(usec_t us)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_as_uint64_t(us) / 1000;
|
2019-09-04 15:11:45 +10:00
|
|
|
}
|
|
|
|
|
|
2024-10-21 11:07:34 +10:00
|
|
|
static inline uint32_t
|
2025-12-01 09:17:37 +10:00
|
|
|
usec_to_seconds(usec_t us)
|
2024-10-21 11:07:34 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_as_uint64_t(us) / 1000000;
|
2024-10-21 11:07:34 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline uint32_t
|
|
|
|
|
usec_to_minutes(usec_t us)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_to_seconds(us) / 60;
|
2019-09-04 15:11:45 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline uint32_t
|
|
|
|
|
usec_to_hours(usec_t us)
|
2021-09-17 09:00:38 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_to_minutes(us) / 60;
|
2021-09-17 09:00:38 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline usec_t
|
|
|
|
|
usec_add_millis(usec_t us, uint32_t millis)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_from_uint64_t(usec_as_uint64_t(us) + millis * 1000);
|
2019-09-04 15:11:45 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline usec_t
|
|
|
|
|
usec_delta(usec_t later, usec_t earlier)
|
2024-10-21 11:07:34 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_from_uint64_t(later.v - earlier.v);
|
2024-10-21 11:07:34 +10:00
|
|
|
}
|
|
|
|
|
|
Introduce custom acceleration profile
The custom acceleration profile allow the user to define custom
acceleration functions for each movement type per device, giving
full control over accelerations behavior at different speeds.
This commit introduces 2 movement types which corresponds to the
2 profiles currently in use by libinput.
regular filter is Motion type.
constant filter is Fallback type.
This allows possible expansion of new movement types for the
different devices.
The custom pointer acceleration profile gives the user full control over the
acceleration behavior at different speeds.
The user needs to provide a custom acceleration function f(x) where
the x-axis is the device speed and the y-axis is the pointer speed.
The user should take into account the native device dpi and screen dpi in
order to achieve the desired behavior/feel of the acceleration.
The custom acceleration function is defined using n points which are spaced
uniformly along the x-axis, starting from 0 and continuing in constant steps.
There by the points defining the custom function are:
(0 * step, f[0]), (1 * step, f[1]), ..., ((n-1) * step, f[n-1])
where f is a list of n unitless values defining the acceleration
factor for each velocity.
When a velocity value does not lie exactly on those points, a linear
interpolation of the two closest points will be calculated.
When a velocity value is greater than the max point defined, a linear
extrapolation of the two biggest points will be calculated.
Signed-off-by: Yinon Burgansky <51504-Yinon@users.noreply.gitlab.freedesktop.org>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2022-12-13 00:23:59 +02:00
|
|
|
static inline double
|
2025-12-01 09:17:37 +10:00
|
|
|
us2ms_f(usec_t us)
|
Introduce custom acceleration profile
The custom acceleration profile allow the user to define custom
acceleration functions for each movement type per device, giving
full control over accelerations behavior at different speeds.
This commit introduces 2 movement types which corresponds to the
2 profiles currently in use by libinput.
regular filter is Motion type.
constant filter is Fallback type.
This allows possible expansion of new movement types for the
different devices.
The custom pointer acceleration profile gives the user full control over the
acceleration behavior at different speeds.
The user needs to provide a custom acceleration function f(x) where
the x-axis is the device speed and the y-axis is the pointer speed.
The user should take into account the native device dpi and screen dpi in
order to achieve the desired behavior/feel of the acceleration.
The custom acceleration function is defined using n points which are spaced
uniformly along the x-axis, starting from 0 and continuing in constant steps.
There by the points defining the custom function are:
(0 * step, f[0]), (1 * step, f[1]), ..., ((n-1) * step, f[n-1])
where f is a list of n unitless values defining the acceleration
factor for each velocity.
When a velocity value does not lie exactly on those points, a linear
interpolation of the two closest points will be calculated.
When a velocity value is greater than the max point defined, a linear
extrapolation of the two biggest points will be calculated.
Signed-off-by: Yinon Burgansky <51504-Yinon@users.noreply.gitlab.freedesktop.org>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2022-12-13 00:23:59 +02:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return (double)usec_as_uint64_t(us) / 1000.0;
|
Introduce custom acceleration profile
The custom acceleration profile allow the user to define custom
acceleration functions for each movement type per device, giving
full control over accelerations behavior at different speeds.
This commit introduces 2 movement types which corresponds to the
2 profiles currently in use by libinput.
regular filter is Motion type.
constant filter is Fallback type.
This allows possible expansion of new movement types for the
different devices.
The custom pointer acceleration profile gives the user full control over the
acceleration behavior at different speeds.
The user needs to provide a custom acceleration function f(x) where
the x-axis is the device speed and the y-axis is the pointer speed.
The user should take into account the native device dpi and screen dpi in
order to achieve the desired behavior/feel of the acceleration.
The custom acceleration function is defined using n points which are spaced
uniformly along the x-axis, starting from 0 and continuing in constant steps.
There by the points defining the custom function are:
(0 * step, f[0]), (1 * step, f[1]), ..., ((n-1) * step, f[n-1])
where f is a list of n unitless values defining the acceleration
factor for each velocity.
When a velocity value does not lie exactly on those points, a linear
interpolation of the two closest points will be calculated.
When a velocity value is greater than the max point defined, a linear
extrapolation of the two biggest points will be calculated.
Signed-off-by: Yinon Burgansky <51504-Yinon@users.noreply.gitlab.freedesktop.org>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2022-12-13 00:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline usec_t
|
|
|
|
|
usec_from_timeval(const struct timeval *tv)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_from_uint64_t(tv->tv_sec * 1000000 + tv->tv_usec);
|
2019-09-04 15:11:45 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline usec_t
|
|
|
|
|
usec_from_timespec(const struct timespec *tp)
|
2024-10-21 11:07:34 +10:00
|
|
|
{
|
2025-12-01 09:17:37 +10:00
|
|
|
return usec_from_uint64_t(tp->tv_sec * 1000000 + tp->tv_nsec / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline usec_t
|
|
|
|
|
usec_from_now(void)
|
|
|
|
|
{
|
|
|
|
|
struct timespec ts = { 0, 0 };
|
|
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
|
|
|
|
|
|
return usec_from_timespec(&ts);
|
2024-10-21 11:07:34 +10:00
|
|
|
}
|
|
|
|
|
|
2019-09-04 15:11:45 +10:00
|
|
|
static inline struct timeval
|
2025-12-01 09:17:37 +10:00
|
|
|
usec_to_timeval(usec_t time)
|
2019-09-04 15:11:45 +10:00
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
2025-12-01 09:17:37 +10:00
|
|
|
uint64_t time_us = usec_as_uint64_t(time);
|
|
|
|
|
uint64_t one_sec_us = usec_as_uint64_t(usec_from_millis(1000));
|
2019-09-04 15:11:45 +10:00
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
tv.tv_sec = time_us / one_sec_us;
|
|
|
|
|
tv.tv_usec = time_us % one_sec_us;
|
2019-09-04 15:11:45 +10:00
|
|
|
|
|
|
|
|
return tv;
|
|
|
|
|
}
|
2020-03-22 10:31:44 +10:00
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
static inline struct timespec
|
|
|
|
|
usec_to_timespec(usec_t time)
|
|
|
|
|
{
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
uint64_t time_us = usec_as_uint64_t(time);
|
|
|
|
|
uint64_t one_sec_us = usec_as_uint64_t(usec_from_millis(1000));
|
|
|
|
|
|
|
|
|
|
ts.tv_sec = time_us / one_sec_us;
|
|
|
|
|
ts.tv_nsec = (time_us % one_sec_us) * 1000;
|
|
|
|
|
|
|
|
|
|
return ts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline usec_t
|
|
|
|
|
usec_add(usec_t a, usec_t b)
|
|
|
|
|
{
|
|
|
|
|
return usec_from_uint64_t(usec_as_uint64_t(a) + usec_as_uint64_t(b));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline usec_t
|
|
|
|
|
usec_sub(usec_t a, usec_t b)
|
|
|
|
|
{
|
|
|
|
|
return usec_from_uint64_t(usec_as_uint64_t(a) - usec_as_uint64_t(b));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline usec_t
|
|
|
|
|
usec_div(usec_t a, uint64_t b)
|
|
|
|
|
{
|
|
|
|
|
return usec_from_uint64_t(usec_as_uint64_t(a) / b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline usec_t
|
|
|
|
|
usec_mul(usec_t a, double b)
|
|
|
|
|
{
|
|
|
|
|
return usec_from_uint64_t(usec_as_uint64_t(a) * b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
|
usec_is_zero(usec_t a)
|
|
|
|
|
{
|
|
|
|
|
return usec_as_uint64_t(a) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
msleep(unsigned int ms)
|
|
|
|
|
{
|
|
|
|
|
usleep(ms * 1000);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 11:07:14 +10:00
|
|
|
static inline int
|
2025-12-01 09:17:37 +10:00
|
|
|
now_in_us(usec_t *us)
|
2024-10-21 11:07:14 +10:00
|
|
|
{
|
|
|
|
|
struct timespec ts = { 0, 0 };
|
|
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
|
2025-12-01 09:17:37 +10:00
|
|
|
*us = usec_from_uint64_t(0);
|
2024-10-21 11:07:14 +10:00
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 09:17:37 +10:00
|
|
|
*us = usec_from_timespec(&ts);
|
|
|
|
|
|
2024-10-21 11:07:14 +10:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 10:31:44 +10:00
|
|
|
struct human_time {
|
|
|
|
|
unsigned int value;
|
|
|
|
|
const char *unit;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts a time delta in µs to a human-readable time like "2h" or "4d"
|
|
|
|
|
*/
|
|
|
|
|
static inline struct human_time
|
2025-12-01 09:17:37 +10:00
|
|
|
to_human_time(usec_t us)
|
2020-03-22 10:31:44 +10:00
|
|
|
{
|
|
|
|
|
struct human_time t;
|
|
|
|
|
struct c {
|
|
|
|
|
const char *unit;
|
|
|
|
|
unsigned int change_from_previous;
|
|
|
|
|
uint64_t limit;
|
|
|
|
|
} conversion[] = {
|
2025-07-01 16:30:11 +10:00
|
|
|
{ "us", 1, 5000 }, { "ms", 1000, 5000 }, { "s", 1000, 120 },
|
|
|
|
|
{ "min", 60, 120 }, { "h", 60, 48 }, { "d", 24, ~0 },
|
2020-03-22 10:31:44 +10:00
|
|
|
};
|
2025-12-01 09:17:37 +10:00
|
|
|
uint64_t value = usec_as_uint64_t(us);
|
2020-03-22 10:31:44 +10:00
|
|
|
|
|
|
|
|
ARRAY_FOR_EACH(conversion, c) {
|
2025-07-01 16:30:11 +10:00
|
|
|
value = value / c->change_from_previous;
|
2020-03-22 10:31:44 +10:00
|
|
|
if (value < c->limit) {
|
|
|
|
|
t.unit = c->unit;
|
|
|
|
|
t.value = value;
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(!"We should never get here");
|
|
|
|
|
}
|