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 <format>
13 
14 namespace fcitx {
15 
16 namespace {
17 
18 constexpr uint64_t USEC_INFINITY = static_cast<uint64_t>(-1);
19 constexpr uint64_t USEC_PER_SEC = 1000000ULL;
20 constexpr uint64_t NSEC_PER_USEC = 1000ULL;
21 
22 std::string getErrorMessage(int error) {
23  if (error < 0) {
24  error = -error;
25  }
26  return std::format("EventLoopException: {0}", std::strerror(error));
27 }
28 
29 } // namespace
30 
31 // From systemd :)
32 uint64_t timespec_load(const struct timespec *ts) {
33  if (ts->tv_sec == (time_t)-1 && ts->tv_nsec == (long)-1) {
34  return USEC_INFINITY;
35  }
36 
37  if ((uint64_t)ts->tv_sec >
38  (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) {
39  return USEC_INFINITY;
40  }
41 
42  return (uint64_t)ts->tv_sec * USEC_PER_SEC +
43  (uint64_t)ts->tv_nsec / NSEC_PER_USEC;
44 }
45 
46 uint64_t now(clockid_t clock_id) {
47  struct timespec ts;
48  clock_gettime(clock_id, &ts);
49 
50  return timespec_load(&ts);
51 }
52 
53 EventLoopException::EventLoopException(int error)
54  : std::runtime_error(getErrorMessage(error)), errno_(error) {}
55 
56 void EventSourceTime::setNextInterval(uint64_t time) {
57  setTime(now(clock()) + time);
58 }
59 
60 EventSource::~EventSource() = default;
61 
62 EventLoopInterface::~EventLoopInterface() = default;
63 
64 } // namespace fcitx
Definition: action.cpp:17
Definition: matchrule.h:78