Fcitx
event.cpp
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 "event.h"
10 #include <cstdint>
11 #include <cstring>
12 #include <ctime>
13 #include <memory>
14 #include <stdexcept>
15 #include <utility>
16 #include "event_p.h"
17 #include "eventloopinterface.h"
18 #include "macros.h"
19 
20 namespace fcitx {
21 
23 public:
24  EventLoopPrivate(std::unique_ptr<EventLoopInterface> impl)
25  : impl_(std::move(impl)) {
26  if (!impl_) {
27  throw std::runtime_error("No available event loop implementation.");
28  }
29  }
30 
31  std::unique_ptr<EventLoopInterface> impl_;
32 
33  static EventLoopFactory factory_;
34 };
35 
36 EventLoopFactory EventLoopPrivate::factory_ = createDefaultEventLoop;
37 
38 void EventLoop::setEventLoopFactory(EventLoopFactory factory) {
39  if (factory) {
40  EventLoopPrivate::factory_ = std::move(factory);
41  } else {
42  EventLoopPrivate::factory_ = createDefaultEventLoop;
43  }
44 }
45 
46 EventLoop::EventLoop() : EventLoop(EventLoopPrivate::factory_()) {}
47 
48 EventLoop::EventLoop(std::unique_ptr<EventLoopInterface> impl)
49  : d_ptr(std::make_unique<EventLoopPrivate>(std::move(impl))) {}
50 
51 EventLoop::~EventLoop() = default;
52 
53 const char *EventLoop::impl() { return defaultEventLoopImplementation(); }
54 
55 const char *EventLoop::implementation() const {
56  FCITX_D();
57  return d->impl_->implementation();
58 }
59 
60 void *EventLoop::nativeHandle() {
61  FCITX_D();
62  return d->impl_->nativeHandle();
63 }
64 
65 bool EventLoop::exec() {
66  FCITX_D();
67  return d->impl_->exec();
68 }
69 
70 void EventLoop::exit() {
71  FCITX_D();
72  return d->impl_->exit();
73 }
74 
75 std::unique_ptr<EventSourceIO> EventLoop::addIOEvent(int fd, IOEventFlags flags,
76  IOCallback callback) {
77  FCITX_D();
78  return d->impl_->addIOEvent(fd, flags, std::move(callback));
79 }
80 
81 std::unique_ptr<EventSourceTime>
82 EventLoop::addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
83  TimeCallback callback) {
84  FCITX_D();
85  return d->impl_->addTimeEvent(clock, usec, accuracy, std::move(callback));
86 }
87 
88 std::unique_ptr<EventSource> EventLoop::addExitEvent(EventCallback callback) {
89  FCITX_D();
90  return d->impl_->addExitEvent(std::move(callback));
91 }
92 
93 std::unique_ptr<EventSource> EventLoop::addDeferEvent(EventCallback callback) {
94  FCITX_D();
95  return d->impl_->addDeferEvent(std::move(callback));
96 }
97 
98 std::unique_ptr<EventSource> EventLoop::addPostEvent(EventCallback callback) {
99  FCITX_D();
100  return d->impl_->addPostEvent(std::move(callback));
101 }
102 
103 std::unique_ptr<EventSourceAsync>
104 EventLoop::addAsyncEvent(EventCallback callback) {
105  FCITX_D();
106  if (auto *v2 = dynamic_cast<EventLoopInterfaceV2 *>(d->impl_.get())) {
107  return v2->addAsyncEvent(std::move(callback));
108  }
109  return nullptr;
110 }
111 
112 } // namespace fcitx
Definition: action.cpp:17
FCITX_NODISCARD std::unique_ptr< EventSourceAsync > addAsyncEvent(EventCallback callback)
Add an async event that is safe to be triggered from another thread.
Definition: event.cpp:104
const char * implementation() const
Return the name of implementation of event loop.
Definition: event.cpp:55
static FCITXUTILS_DEPRECATED const char * impl()
Return the default implementation name.
Definition: event.cpp:53
static void setEventLoopFactory(EventLoopFactory factory)
Set an external event loop implementation.
Definition: event.cpp:38