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