Fcitx
bus.h
Go to the documentation of this file.
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_DBUS_BUS_H_
8 #define _FCITX_UTILS_DBUS_BUS_H_
9 
10 #include <cstdint>
11 #include <memory>
12 #include <string>
16 #include <fcitx-utils/event.h>
17 #include <fcitx-utils/fcitxutils_export.h>
18 #include <fcitx-utils/flags.h>
19 #include <fcitx-utils/macros.h>
20 
21 /// \addtogroup FcitxUtils
22 /// \{
23 /// \file
24 /// \brief API for DBus bus.
25 
26 namespace fcitx::dbus {
27 
28 /**
29  * Virtual base class represent some internal registration of the bus.
30  *
31  * Mainly used with C++ RAII idiom.
32  */
33 class FCITXUTILS_EXPORT Slot {
34 public:
35  virtual ~Slot();
36 };
37 
38 enum class BusType { Default, Session, System };
39 enum class RequestNameFlag {
40  None = 0,
41  ReplaceExisting = 1ULL << 0,
42  AllowReplacement = 1ULL << 1,
43  Queue = 1ULL << 2
44 };
45 
46 class BusPrivate;
47 
48 /**
49  * A class that represents a connection to the Bus.
50  */
51 class FCITXUTILS_EXPORT Bus {
52 public:
53  /// Connect to given address.
54  Bus(const std::string &address);
55 
56  /// Connect to given dbus type.
57  Bus(BusType type);
58 
59  virtual ~Bus();
60  Bus(const Bus &other) = delete;
61  Bus(Bus &&other) noexcept;
62 
63  /// Check if the connection is open.
64  FCITX_NODISCARD bool isOpen() const;
65 
66  /// Attach this bus to an event loop.
67  void attachEventLoop(EventLoop *loop);
68 
69  /// Remove this bus from an event loop.
70  void detachEventLoop();
71 
72  /**
73  * Return the attached event loop
74  *
75  * @return attached event loop, not necessary a valid pointer if event loop
76  * is destructed before bus.
77  *
78  * @since 5.0.22
79  */
80  FCITX_NODISCARD EventLoop *eventLoop() const;
81 
82  FCITX_NODISCARD std::unique_ptr<Slot> addMatch(const MatchRule &rule,
83  MessageCallback callback);
84  FCITX_NODISCARD std::unique_ptr<Slot> addFilter(MessageCallback callback);
85  FCITX_NODISCARD std::unique_ptr<Slot> addObject(const std::string &path,
86  MessageCallback callback);
87  /**
88  * Register a new object on the dbus.
89  *
90  * @param path object path
91  * @param interface object interface
92  * @param obj object
93  * @return registration succeeds or not.
94  */
95  bool addObjectVTable(const std::string &path, const std::string &interface,
96  ObjectVTableBase &vtable);
97 
98  /// Create a new signal message
99  Message createSignal(const char *path, const char *interface,
100  const char *member);
101 
102  /// Create a new method message.
103  Message createMethodCall(const char *destination, const char *path,
104  const char *interface, const char *member);
105 
106  /**
107  * Return the name of the compiled implentation of fcitx dbus
108  *
109  * @return "sdbus" or "libdbus"
110  */
111  static const char *impl();
112 
113  /**
114  * Return the internal pointer of the implemenation.
115  *
116  * @return internal pointer
117  */
118  FCITX_NODISCARD void *nativeHandle() const;
119 
120  /**
121  * Request the dbus name on the bus.
122  *
123  * @param name service name
124  * @param flags request name flag.
125  * @return requesting name is successful or not.
126  */
127  bool requestName(const std::string &name, Flags<RequestNameFlag> flags);
128 
129  /// Release the dbus name.
130  bool releaseName(const std::string &name);
131 
132  /**
133  * Helper function to query the service owner.
134  *
135  * @param name dbus name
136  * @param usec dbus timeout
137  * @return unique name of the owner.
138  */
139  std::string serviceOwner(const std::string &name, uint64_t usec);
140  std::unique_ptr<Slot> serviceOwnerAsync(const std::string &name,
141  uint64_t usec,
142  MessageCallback callback);
143 
144  /**
145  * Return the unique name of current connection. E.g. :1.34
146  *
147  * @return unique name
148  */
149  std::string uniqueName();
150 
151  /**
152  * Return the dbus address being connected to.
153  *
154  * @return dbus address
155  */
156  std::string address();
157 
158  /**
159  * Flush the bus immediately.
160  */
161  void flush();
162 
163 private:
164  std::unique_ptr<BusPrivate> d_ptr;
165  FCITX_DECLARE_PRIVATE(Bus);
166 };
167 } // namespace fcitx::dbus
168 
169 #endif // _FCITX_UTILS_DBUS_BUS_H_
Basic DBus type of a DBus message.
Definition: message.h:224
A dbus matching rule to be used with add match.
Definition: matchrule.h:36
Virtual base class represent some internal registration of the bus.
Definition: bus.h:33
A class that represents a connection to the Bus.
Definition: bus.h:51
API for DBus message.
High level API for dbus objects.
API for DBus matching rule.
Class provides bit flag support for Enum.
Definition: flags.h:33
Helper template class to make easier to use type safe enum flags.