1 
2 /*
3  * SPDX-FileCopyrightText: 2015-2015 CSSlayer <wengxt@gmail.com>
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  */
8 
9 #include <cstring>
10 #include "event.h"
11 
12 #define USEC_INFINITY ((uint64_t)-1)
13 #define USEC_PER_SEC ((uint64_t)1000000ULL)
14 #define NSEC_PER_USEC ((uint64_t)1000ULL)
15 
16 namespace fcitx {
17 
18 // From systemd :)
timespec_load(const struct timespec * ts)19 uint64_t timespec_load(const struct timespec *ts) {
20     if (ts->tv_sec == (time_t)-1 && ts->tv_nsec == (long)-1) {
21         return USEC_INFINITY;
22     }
23 
24     if ((uint64_t)ts->tv_sec >
25         (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) {
26         return USEC_INFINITY;
27     }
28 
29     return (uint64_t)ts->tv_sec * USEC_PER_SEC +
30            (uint64_t)ts->tv_nsec / NSEC_PER_USEC;
31 }
32 
now(clockid_t clock_id)33 uint64_t now(clockid_t clock_id) {
34     struct timespec ts;
35     clock_gettime(clock_id, &ts);
36 
37     return timespec_load(&ts);
38 }
39 
EventLoopException(int error)40 EventLoopException::EventLoopException(int error)
41     : std::runtime_error(std::strerror(error)), errno_(error) {}
42 
setNextInterval(uint64_t time)43 void EventSourceTime::setNextInterval(uint64_t time) {
44     setTime(now(clock()) + time);
45 }
46 
~EventSource()47 EventSource::~EventSource() {}
48 } // namespace fcitx
49