1 module ddbus.bus;
2 
3 import ddbus.router;
4 import ddbus.thin;
5 import ddbus.c_lib;
6 import std..string;
7 
8 enum BusService = "org.freedesktop.DBus";
9 enum BusPath = "/org/freedesktop/DBus";
10 enum BusInterface = "org.freedesktop.DBus";
11 
12 enum NameFlags {
13   AllowReplace = 1,
14   ReplaceExisting = 2,
15   NoQueue = 4
16 }
17 
18 /// Requests a DBus well-known name.
19 /// returns if the name is owned after the call.
20 /// Involves blocking call on a DBus method, may throw an exception on failure.
21 bool requestName(Connection conn, string name,
22     NameFlags flags = NameFlags.NoQueue | NameFlags.AllowReplace) {
23   auto msg = Message(BusService, BusPath, BusInterface, "RequestName");
24   msg.build(name, cast(uint)(flags));
25   auto res = conn.sendWithReplyBlocking(msg).to!uint;
26   return (res == 1) || (res == 4);
27 }
28 
29 /// A simple main loop that isn't necessarily efficient
30 /// and isn't guaranteed to work with other tasks and threads.
31 /// Use only for apps that only do DBus triggered things.
32 void simpleMainLoop(Connection conn) {
33   while (dbus_connection_read_write_dispatch(conn.conn, -1)) {
34   } // empty loop body
35 }
36 
37 /// Single tick in the DBus connection which can be used for
38 /// concurrent updates.
39 bool tick(Connection conn) {
40   return cast(bool) dbus_connection_read_write_dispatch(conn.conn, 0);
41 }
42 
43 unittest {
44   import dunit.toolkit;
45 
46   Connection conn = connectToBus();
47   conn.requestName("ca.thume.ddbus.testing").assertTrue();
48 }