2 * Copyright © 2014 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/timerfd.h>
30 #include "libinput-private.h"
34 libinput_timer_init(struct libinput_timer *timer, struct libinput *libinput,
35 void (*timer_func)(uint64_t now, void *timer_func_data),
36 void *timer_func_data)
38 timer->libinput = libinput;
39 timer->timer_func = timer_func;
40 timer->timer_func_data = timer_func_data;
44 libinput_timer_arm_timer_fd(struct libinput *libinput)
47 struct libinput_timer *timer;
48 struct itimerspec its = { { 0, 0 }, { 0, 0 } };
49 uint64_t earliest_expire = UINT64_MAX;
51 list_for_each(timer, &libinput->timer.list, link) {
52 if (timer->expire < earliest_expire)
53 earliest_expire = timer->expire;
56 if (earliest_expire != UINT64_MAX) {
57 its.it_value.tv_sec = earliest_expire / 1000;
58 its.it_value.tv_nsec = (earliest_expire % 1000) * 1000 * 1000;
61 r = timerfd_settime(libinput->timer.fd, TFD_TIMER_ABSTIME, &its, NULL);
63 log_error(libinput, "timerfd_settime error: %s\n", strerror(errno));
67 libinput_timer_set(struct libinput_timer *timer, uint64_t expire)
72 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
73 uint64_t now = ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000;
74 if (abs(expire - now) > 5000)
75 log_bug_libinput(timer->libinput,
76 "timer offset more than 5s, now %"
77 PRIu64 " expire %" PRIu64 "\n",
80 log_error(timer->libinput,
81 "clock_gettime error: %s\n", strerror(errno));
88 list_insert(&timer->libinput->timer.list, &timer->link);
90 timer->expire = expire;
91 libinput_timer_arm_timer_fd(timer->libinput);
95 libinput_timer_cancel(struct libinput_timer *timer)
101 list_remove(&timer->link);
102 libinput_timer_arm_timer_fd(timer->libinput);
106 libinput_timer_handler(void *data)
108 struct libinput *libinput = data;
109 struct libinput_timer *timer, *tmp;
114 r = clock_gettime(CLOCK_MONOTONIC, &ts);
116 log_error(libinput, "clock_gettime error: %s\n", strerror(errno));
120 now = ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000;
122 list_for_each_safe(timer, tmp, &libinput->timer.list, link) {
123 if (timer->expire <= now) {
124 /* Clear the timer before calling timer_func,
125 as timer_func may re-arm it */
126 libinput_timer_cancel(timer);
127 timer->timer_func(now, timer->timer_func_data);
133 libinput_timer_subsys_init(struct libinput *libinput)
135 libinput->timer.fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
136 if (libinput->timer.fd < 0)
139 list_init(&libinput->timer.list);
141 libinput->timer.source = libinput_add_fd(libinput,
143 libinput_timer_handler,
145 if (!libinput->timer.source) {
146 close(libinput->timer.fd);
154 libinput_timer_subsys_destroy(struct libinput *libinput)
156 /* All timer users should have destroyed their timers now */
157 assert(list_empty(&libinput->timer.list));
159 libinput_remove_source(libinput, libinput->timer.source);
160 close(libinput->timer.fd);