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