Fcitx
eventloopinterface.cpp
1 /*
2  * SPDX-FileCopyrightText: 2015-2024 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #include "eventloopinterface.h"
8 #include <cstdint>
9 #include <cstring>
10 #include <ctime>
11 #include <stdexcept>
12 #include <string>
13 #include <format>
14 
15 namespace fcitx {
16 
17 namespace {
18 
19 constexpr uint64_t USEC_INFINITY = static_cast<uint64_t>(-1);
20 constexpr uint64_t USEC_PER_SEC = 1000000ULL;
21 constexpr uint64_t NSEC_PER_USEC = 1000ULL;
22 
23 std::string getErrorMessage(int error) {
24  if (error < 0) {
25  error = -error;
26  }
27  return std::format("EventLoopException: {0}", std::strerror(error));
28 }
29 
30 } // namespace
31 
32 // From systemd :)
33 uint64_t timespec_load(const struct timespec *ts) {
34  if (ts->tv_sec == (time_t)-1 && ts->tv_nsec == (long)-1) {
35  return USEC_INFINITY;
36  }
37 
38  if ((uint64_t)ts->tv_sec >
39  (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) {
40  return USEC_INFINITY;
41  }
42 
43  return ((uint64_t)ts->tv_sec * USEC_PER_SEC) +
44  ((uint64_t)ts->tv_nsec / NSEC_PER_USEC);
45 }
46 
47 uint64_t now(clockid_t clock_id) {
48  struct timespec ts;
49  clock_gettime(clock_id, &ts);
50 
51  return timespec_load(&ts);
52 }
53 
54 EventLoopException::EventLoopException(int error)
55  : std::runtime_error(getErrorMessage(error)), errno_(error) {}
56 
57 void EventSourceTime::setNextInterval(uint64_t time) {
58  setTime(now(clock()) + time);
59 }
60 
61 EventSource::~EventSource() = default;
62 
63 EventLoopInterface::~EventLoopInterface() = default;
64 
65 } // namespace fcitx
Definition: action.cpp:17
Definition: matchrule.h:78