2014-06-06 17:01:05 +02:00
|
|
|
/*
|
2015-05-28 08:23:59 +10:00
|
|
|
* Copyright © 2014-2015 Red Hat, Inc.
|
2014-06-06 17:01:05 +02:00
|
|
|
*
|
2015-06-11 12:09:18 +10:00
|
|
|
* 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:
|
2014-06-06 17:01:05 +02:00
|
|
|
*
|
2015-06-11 12:09:18 +10:00
|
|
|
* 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.
|
2014-06-06 17:01:05 +02:00
|
|
|
*/
|
|
|
|
|
|
2015-06-01 08:09:26 +10:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2014-06-06 17:01:05 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
2014-06-24 16:23:13 +02:00
|
|
|
#include <inttypes.h>
|
2014-06-06 17:01:05 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "libinput-private.h"
|
|
|
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
|
|
void
|
2017-07-03 11:28:18 +10:00
|
|
|
libinput_timer_init(struct libinput_timer *timer,
|
|
|
|
|
struct libinput *libinput,
|
|
|
|
|
const char *timer_name,
|
2014-06-06 17:01:05 +02:00
|
|
|
void (*timer_func)(uint64_t now, void *timer_func_data),
|
|
|
|
|
void *timer_func_data)
|
|
|
|
|
{
|
|
|
|
|
timer->libinput = libinput;
|
2017-11-09 10:40:25 +10:00
|
|
|
timer->timer_name = safe_strdup(timer_name);
|
2014-06-06 17:01:05 +02:00
|
|
|
timer->timer_func = timer_func;
|
|
|
|
|
timer->timer_func_data = timer_func_data;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 11:28:18 +10:00
|
|
|
void
|
|
|
|
|
libinput_timer_destroy(struct libinput_timer *timer)
|
|
|
|
|
{
|
|
|
|
|
free(timer->timer_name);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 17:01:05 +02:00
|
|
|
static void
|
|
|
|
|
libinput_timer_arm_timer_fd(struct libinput *libinput)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
struct libinput_timer *timer;
|
2014-07-03 09:32:02 +10:00
|
|
|
struct itimerspec its = { { 0, 0 }, { 0, 0 } };
|
2014-06-06 17:01:05 +02:00
|
|
|
uint64_t earliest_expire = UINT64_MAX;
|
|
|
|
|
|
|
|
|
|
list_for_each(timer, &libinput->timer.list, link) {
|
|
|
|
|
if (timer->expire < earliest_expire)
|
|
|
|
|
earliest_expire = timer->expire;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (earliest_expire != UINT64_MAX) {
|
2015-07-27 17:51:52 +08:00
|
|
|
its.it_value.tv_sec = earliest_expire / ms2us(1000);
|
|
|
|
|
its.it_value.tv_nsec = (earliest_expire % ms2us(1000)) * 1000;
|
2014-06-06 17:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = timerfd_settime(libinput->timer.fd, TFD_TIMER_ABSTIME, &its, NULL);
|
|
|
|
|
if (r)
|
2017-02-24 16:03:44 +10:00
|
|
|
log_error(libinput, "timer: timerfd_settime error: %s\n", strerror(errno));
|
2017-09-19 15:51:09 +10:00
|
|
|
|
|
|
|
|
libinput->timer.next_expiry = earliest_expire;
|
2014-06-06 17:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2017-02-20 09:14:42 +10:00
|
|
|
libinput_timer_set_flags(struct libinput_timer *timer,
|
|
|
|
|
uint64_t expire,
|
|
|
|
|
uint32_t flags)
|
2014-06-06 17:01:05 +02:00
|
|
|
{
|
2014-06-24 16:23:13 +02:00
|
|
|
#ifndef NDEBUG
|
2014-09-01 16:47:28 +10:00
|
|
|
uint64_t now = libinput_now(timer->libinput);
|
2017-02-20 09:14:42 +10:00
|
|
|
if (expire < now) {
|
|
|
|
|
if ((flags & TIMER_FLAG_ALLOW_NEGATIVE) == 0)
|
2018-02-06 14:10:40 +10:00
|
|
|
log_bug_client(timer->libinput,
|
|
|
|
|
"timer %s: offset negative (-%dms)\n",
|
|
|
|
|
timer->timer_name,
|
|
|
|
|
us2ms(now - expire));
|
2017-02-20 09:14:42 +10:00
|
|
|
} else if ((expire - now) > ms2us(5000)) {
|
2014-09-01 16:47:28 +10:00
|
|
|
log_bug_libinput(timer->libinput,
|
2017-11-09 10:39:13 +10:00
|
|
|
"timer %s: offset more than 5s, now %d expire %d\n",
|
2017-11-09 10:40:25 +10:00
|
|
|
timer->timer_name,
|
2017-11-09 10:39:13 +10:00
|
|
|
us2ms(now), us2ms(expire));
|
2017-02-20 09:14:42 +10:00
|
|
|
}
|
2014-06-24 16:23:13 +02:00
|
|
|
#endif
|
|
|
|
|
|
2014-06-06 17:01:05 +02:00
|
|
|
assert(expire);
|
|
|
|
|
|
|
|
|
|
if (!timer->expire)
|
|
|
|
|
list_insert(&timer->libinput->timer.list, &timer->link);
|
|
|
|
|
|
|
|
|
|
timer->expire = expire;
|
|
|
|
|
libinput_timer_arm_timer_fd(timer->libinput);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-20 09:14:42 +10:00
|
|
|
void
|
|
|
|
|
libinput_timer_set(struct libinput_timer *timer, uint64_t expire)
|
|
|
|
|
{
|
|
|
|
|
libinput_timer_set_flags(timer, expire, TIMER_FLAG_NONE);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 17:01:05 +02:00
|
|
|
void
|
|
|
|
|
libinput_timer_cancel(struct libinput_timer *timer)
|
|
|
|
|
{
|
|
|
|
|
if (!timer->expire)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
timer->expire = 0;
|
|
|
|
|
list_remove(&timer->link);
|
|
|
|
|
libinput_timer_arm_timer_fd(timer->libinput);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-09-19 15:51:09 +10:00
|
|
|
libinput_timer_handler(struct libinput *libinput , uint64_t now)
|
2014-06-06 17:01:05 +02:00
|
|
|
{
|
2017-07-17 12:30:25 +10:00
|
|
|
struct libinput_timer *timer;
|
2014-06-06 17:01:05 +02:00
|
|
|
|
2017-07-17 12:30:25 +10:00
|
|
|
restart:
|
|
|
|
|
list_for_each(timer, &libinput->timer.list, link) {
|
2017-07-17 11:58:11 +10:00
|
|
|
if (timer->expire == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2014-06-06 17:01:05 +02:00
|
|
|
if (timer->expire <= now) {
|
|
|
|
|
/* Clear the timer before calling timer_func,
|
|
|
|
|
as timer_func may re-arm it */
|
|
|
|
|
libinput_timer_cancel(timer);
|
|
|
|
|
timer->timer_func(now, timer->timer_func_data);
|
2017-07-17 12:30:25 +10:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Restart the loop. We can't use
|
|
|
|
|
* list_for_each_safe() here because that only
|
|
|
|
|
* allows removing one (our) timer per timer_func.
|
|
|
|
|
* But the timer func may trigger another unrelated
|
|
|
|
|
* timer to be cancelled and removed, causing a
|
|
|
|
|
* segfault.
|
|
|
|
|
*/
|
|
|
|
|
goto restart;
|
2014-06-06 17:01:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 15:51:09 +10:00
|
|
|
static void
|
|
|
|
|
libinput_timer_dispatch(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct libinput *libinput = data;
|
|
|
|
|
uint64_t now;
|
|
|
|
|
uint64_t discard;
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
r = read(libinput->timer.fd, &discard, sizeof(discard));
|
|
|
|
|
if (r == -1 && errno != EAGAIN)
|
|
|
|
|
log_bug_libinput(libinput,
|
|
|
|
|
"timer: error %d reading from timerfd (%s)",
|
|
|
|
|
errno,
|
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
|
|
now = libinput_now(libinput);
|
|
|
|
|
if (now == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
libinput_timer_handler(libinput, now);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 17:01:05 +02:00
|
|
|
int
|
|
|
|
|
libinput_timer_subsys_init(struct libinput *libinput)
|
|
|
|
|
{
|
2015-05-01 09:09:38 +10:00
|
|
|
libinput->timer.fd = timerfd_create(CLOCK_MONOTONIC,
|
|
|
|
|
TFD_CLOEXEC | TFD_NONBLOCK);
|
2014-06-06 17:01:05 +02:00
|
|
|
if (libinput->timer.fd < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
list_init(&libinput->timer.list);
|
|
|
|
|
|
|
|
|
|
libinput->timer.source = libinput_add_fd(libinput,
|
|
|
|
|
libinput->timer.fd,
|
2017-09-19 15:51:09 +10:00
|
|
|
libinput_timer_dispatch,
|
2014-06-06 17:01:05 +02:00
|
|
|
libinput);
|
|
|
|
|
if (!libinput->timer.source) {
|
|
|
|
|
close(libinput->timer.fd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
libinput_timer_subsys_destroy(struct libinput *libinput)
|
|
|
|
|
{
|
|
|
|
|
/* All timer users should have destroyed their timers now */
|
|
|
|
|
assert(list_empty(&libinput->timer.list));
|
|
|
|
|
|
|
|
|
|
libinput_remove_source(libinput, libinput->timer.source);
|
|
|
|
|
close(libinput->timer.fd);
|
|
|
|
|
}
|
2017-09-19 15:51:09 +10:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* For a caller calling libinput_dispatch() only infrequently, we may have a
|
|
|
|
|
* timer expiry *and* a later input event waiting in the pipe. We cannot
|
|
|
|
|
* guarantee that we read the timer expiry first, so this hook exists to
|
|
|
|
|
* flush any timers.
|
|
|
|
|
*
|
|
|
|
|
* Assume 'now' is the current time check if there is a current timer expiry
|
|
|
|
|
* before this time. If so, trigger the timer func.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
libinput_timer_flush(struct libinput *libinput, uint64_t now)
|
|
|
|
|
{
|
|
|
|
|
if (libinput->timer.next_expiry == 0 ||
|
|
|
|
|
libinput->timer.next_expiry > now)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
libinput_timer_handler(libinput, now);
|
|
|
|
|
}
|