Fcitx
event.h
1 /*
2  * SPDX-FileCopyrightText: 2015-2015 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_UTILS_EVENT_H_
8 #define _FCITX_UTILS_EVENT_H_
9 
10 #include <sys/types.h>
11 #include <cstdint>
12 #include <functional>
13 #include <memory>
14 #include <fcitx-utils/eventloopinterface.h>
15 #include <fcitx-utils/fcitxutils_export.h>
16 #include <fcitx-utils/flags.h>
17 #include <fcitx-utils/macros.h>
18 
19 #if defined(_WIN32)
20 #include <pthread.h>
21 #endif
22 
23 namespace fcitx {
24 
25 using EventLoopFactory = std::function<std::unique_ptr<EventLoopInterface>()>;
26 
27 class EventLoopPrivate;
28 class FCITXUTILS_EXPORT EventLoop {
29 public:
30  EventLoop();
31  EventLoop(std::unique_ptr<EventLoopInterface> impl);
32  virtual ~EventLoop();
33  bool exec();
34  void exit();
35 
36  /**
37  * Return the default implementation name.
38  *
39  * This will only return the default implementation name.
40  * Do not rely on this value.
41  *
42  * @see EventLoop::implementation
43  */
44  FCITXUTILS_DEPRECATED static const char *impl();
45 
46  /**
47  * Return the name of implementation of event loop.
48  * @since 5.1.12
49  */
50  const char *implementation() const;
51  void *nativeHandle();
52 
53  FCITX_NODISCARD std::unique_ptr<EventSourceIO>
54  addIOEvent(int fd, IOEventFlags flags, IOCallback callback);
55  FCITX_NODISCARD std::unique_ptr<EventSourceTime>
56  addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
57  TimeCallback callback);
58  FCITX_NODISCARD std::unique_ptr<EventSource>
59  addExitEvent(EventCallback callback);
60  FCITX_NODISCARD std::unique_ptr<EventSource>
61  addDeferEvent(EventCallback callback);
62  FCITX_NODISCARD std::unique_ptr<EventSource>
63  addPostEvent(EventCallback callback);
64 
65  /**
66  * Add an async event that is safe to be triggered from another thread.
67  *
68  * To ensure safe usage of this event:
69  * 1. Do not change event to disable, if there may be pending call to send()
70  * 2. Due to (1), if it is oneshot, ensure you only call send() once.
71  * 3. Join all the possible pending thread that may call send(), before
72  * destructing the event.
73  * 4. Like other event, the event should be only created/destructed on the
74  * event loop thread.
75  *
76  * EventDispatcher uses this event internally and provides an easier and
77  * safer interface to use.
78  *
79  * @see EventDispatcher
80  *
81  * @param callback callback function
82  * @return async event source
83  * @since 5.1.13
84  */
85  FCITX_NODISCARD std::unique_ptr<EventSourceAsync>
86  addAsyncEvent(EventCallback callback);
87 
88  /**
89  * Set an external event loop implementation.
90  *
91  * This is useful if you need to integrate fcitx with another event loop.
92  *
93  * @since 5.1.12
94  */
95  static void setEventLoopFactory(EventLoopFactory factory);
96 
97 private:
98  const std::unique_ptr<EventLoopPrivate> d_ptr;
99  FCITX_DECLARE_PRIVATE(EventLoop);
100 };
101 } // namespace fcitx
102 
103 #endif // _FCITX_UTILS_EVENT_H_
Definition: action.cpp:17
Helper template class to make easier to use type safe enum flags.