1 /*
2  * Copyright © 2014-2015 Red Hat, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "config.h"
25 
26 #include <assert.h>
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <string.h>
30 #include <sys/timerfd.h>
31 #include <unistd.h>
32 
33 #include "libinput-private.h"
34 #include "timer.h"
35 
36 void
libinput_timer_init(struct libinput_timer * timer,struct libinput * libinput,const char * timer_name,void (* timer_func)(uint64_t now,void * timer_func_data),void * timer_func_data)37 libinput_timer_init(struct libinput_timer *timer,
38 		    struct libinput *libinput,
39 		    const char *timer_name,
40 		    void (*timer_func)(uint64_t now, void *timer_func_data),
41 		    void *timer_func_data)
42 {
43 	timer->libinput = libinput;
44 	timer->timer_name = safe_strdup(timer_name);
45 	timer->timer_func = timer_func;
46 	timer->timer_func_data = timer_func_data;
47 }
48 
49 void
libinput_timer_destroy(struct libinput_timer * timer)50 libinput_timer_destroy(struct libinput_timer *timer)
51 {
52 	free(timer->timer_name);
53 }
54 
55 static void
libinput_timer_arm_timer_fd(struct libinput * libinput)56 libinput_timer_arm_timer_fd(struct libinput *libinput)
57 {
58 	int r;
59 	struct libinput_timer *timer;
60 	struct itimerspec its = { { 0, 0 }, { 0, 0 } };
61 	uint64_t earliest_expire = UINT64_MAX;
62 
63 	list_for_each(timer, &libinput->timer.list, link) {
64 		if (timer->expire < earliest_expire)
65 			earliest_expire = timer->expire;
66 	}
67 
68 	if (earliest_expire != UINT64_MAX) {
69 		its.it_value.tv_sec = earliest_expire / ms2us(1000);
70 		its.it_value.tv_nsec = (earliest_expire % ms2us(1000)) * 1000;
71 	}
72 
73 	r = timerfd_settime(libinput->timer.fd, TFD_TIMER_ABSTIME, &its, NULL);
74 	if (r)
75 		log_error(libinput, "timer: timerfd_settime error: %s\n", strerror(errno));
76 
77 	libinput->timer.next_expiry = earliest_expire;
78 }
79 
80 void
libinput_timer_set_flags(struct libinput_timer * timer,uint64_t expire,uint32_t flags)81 libinput_timer_set_flags(struct libinput_timer *timer,
82 			 uint64_t expire,
83 			 uint32_t flags)
84 {
85 #ifndef NDEBUG
86 	uint64_t now = libinput_now(timer->libinput);
87 	if (expire < now) {
88 		if ((flags & TIMER_FLAG_ALLOW_NEGATIVE) == 0)
89 			log_bug_client(timer->libinput,
90 				       "timer %s: offset negative (-%dms)\n",
91 				       timer->timer_name,
92 				       us2ms(now - expire));
93 	} else if ((expire - now) > ms2us(5000)) {
94 		log_bug_libinput(timer->libinput,
95 			 "timer %s: offset more than 5s, now %d expire %d\n",
96 			 timer->timer_name,
97 			 us2ms(now), us2ms(expire));
98 	}
99 #endif
100 
101 	assert(expire);
102 
103 	if (!timer->expire)
104 		list_insert(&timer->libinput->timer.list, &timer->link);
105 
106 	timer->expire = expire;
107 	libinput_timer_arm_timer_fd(timer->libinput);
108 }
109 
110 void
libinput_timer_set(struct libinput_timer * timer,uint64_t expire)111 libinput_timer_set(struct libinput_timer *timer, uint64_t expire)
112 {
113 	libinput_timer_set_flags(timer, expire, TIMER_FLAG_NONE);
114 }
115 
116 void
libinput_timer_cancel(struct libinput_timer * timer)117 libinput_timer_cancel(struct libinput_timer *timer)
118 {
119 	if (!timer->expire)
120 		return;
121 
122 	timer->expire = 0;
123 	list_remove(&timer->link);
124 	libinput_timer_arm_timer_fd(timer->libinput);
125 }
126 
127 static void
libinput_timer_handler(struct libinput * libinput,uint64_t now)128 libinput_timer_handler(struct libinput *libinput , uint64_t now)
129 {
130 	struct libinput_timer *timer;
131 
132 restart:
133 	list_for_each(timer, &libinput->timer.list, link) {
134 		if (timer->expire == 0)
135 			continue;
136 
137 		if (timer->expire <= now) {
138 			/* Clear the timer before calling timer_func,
139 			   as timer_func may re-arm it */
140 			libinput_timer_cancel(timer);
141 			timer->timer_func(now, timer->timer_func_data);
142 
143 			/*
144 			 * Restart the loop. We can't use
145 			 * list_for_each_safe() here because that only
146 			 * allows removing one (our) timer per timer_func.
147 			 * But the timer func may trigger another unrelated
148 			 * timer to be cancelled and removed, causing a
149 			 * segfault.
150 			 */
151 			goto restart;
152 		}
153 	}
154 }
155 
156 static void
libinput_timer_dispatch(void * data)157 libinput_timer_dispatch(void *data)
158 {
159 	struct libinput *libinput = data;
160 	uint64_t now;
161 	uint64_t discard;
162 	int r;
163 
164 	r = read(libinput->timer.fd, &discard, sizeof(discard));
165 	if (r == -1 && errno != EAGAIN)
166 		log_bug_libinput(libinput,
167 				 "timer: error %d reading from timerfd (%s)",
168 				 errno,
169 				 strerror(errno));
170 
171 	now = libinput_now(libinput);
172 	if (now == 0)
173 		return;
174 
175 	libinput_timer_handler(libinput, now);
176 }
177 
178 int
libinput_timer_subsys_init(struct libinput * libinput)179 libinput_timer_subsys_init(struct libinput *libinput)
180 {
181 	libinput->timer.fd = timerfd_create(CLOCK_MONOTONIC,
182 					    TFD_CLOEXEC | TFD_NONBLOCK);
183 	if (libinput->timer.fd < 0)
184 		return -1;
185 
186 	list_init(&libinput->timer.list);
187 
188 	libinput->timer.source = libinput_add_fd(libinput,
189 						 libinput->timer.fd,
190 						 libinput_timer_dispatch,
191 						 libinput);
192 	if (!libinput->timer.source) {
193 		close(libinput->timer.fd);
194 		return -1;
195 	}
196 
197 	return 0;
198 }
199 
200 void
libinput_timer_subsys_destroy(struct libinput * libinput)201 libinput_timer_subsys_destroy(struct libinput *libinput)
202 {
203 	/* All timer users should have destroyed their timers now */
204 	assert(list_empty(&libinput->timer.list));
205 
206 	libinput_remove_source(libinput, libinput->timer.source);
207 	close(libinput->timer.fd);
208 }
209 
210 /**
211  * For a caller calling libinput_dispatch() only infrequently, we may have a
212  * timer expiry *and* a later input event waiting in the pipe. We cannot
213  * guarantee that we read the timer expiry first, so this hook exists to
214  * flush any timers.
215  *
216  * Assume 'now' is the current time check if there is a current timer expiry
217  * before this time. If so, trigger the timer func.
218  */
219 void
libinput_timer_flush(struct libinput * libinput,uint64_t now)220 libinput_timer_flush(struct libinput *libinput, uint64_t now)
221 {
222 	if (libinput->timer.next_expiry == 0 ||
223 	    libinput->timer.next_expiry > now)
224 		return;
225 
226 	libinput_timer_handler(libinput, now);
227 }
228