35 #include "model/logger.h"
44 static const GDBusInterfaceVTable interface_vtable = {HandleMethodCall};
52 static gpointer static_user_data = 0;
54 static void OnNameAcquired(GDBusConnection*,
const gchar* ,
56 DEBUG_LOG <<
"OnNameAcquired";
57 auto ctx =
static_cast<DbusCtx*
>(user_data);
58 ctx->handler->SetMainInstance(
true);
61 static void OnNameReleased(GDBusConnection*,
const gchar* ,
63 auto ctx =
static_cast<DbusCtx*
>(user_data);
64 ctx->handler->SetMainInstance(
false);
68 static void OnBusAcquired(GDBusConnection* connection,
const gchar* ,
70 DEBUG_LOG <<
"OnBusAcquired";
71 auto ctx =
static_cast<DbusCtx*
>(user_data);
72 g_dbus_connection_register_object(
73 connection, kDbusObject, ctx->handler->introspection_data->interfaces[0],
74 &interface_vtable, user_data, 0 , 0);
77 static void HandleMethodCall(GDBusConnection*,
const gchar* ,
80 const gchar* method_name, GVariant* parameters,
81 GDBusMethodInvocation* invocation,
84 auto ctx =
static_cast<DbusCtx*
>(static_user_data);
85 if (0 == g_strcmp0(method_name,
"Ping")) {
87 args[0] = g_variant_new_uint32(21614);
88 args[1] = g_variant_new_boolean(
true);
89 g_dbus_method_invocation_return_value(invocation,
90 g_variant_new_tuple(args, 2));
91 }
else if (0 == g_strcmp0(method_name,
"Raise")) {
92 if (ctx) ctx->handler->on_raise.Notify();
93 g_dbus_method_invocation_return_value(invocation, 0);
94 }
else if (0 == g_strcmp0(method_name,
"Quit")) {
95 if (ctx) ctx->handler->on_quit.Notify();
96 g_dbus_method_invocation_return_value(invocation, 0);
97 }
else if (ctx && 0 == g_strcmp0(method_name,
"Open")) {
99 g_variant_get(parameters,
"(&s)", &path);
100 bool ok = ctx->handler->open_file_cb(std::string(path));
102 params[0] = g_variant_new_boolean(ok);
103 g_dbus_method_invocation_return_value(invocation,
104 g_variant_new_tuple(params, 1));
106 }
else if (ctx && g_strcmp0(method_name,
"GetRestEndpoint") == 0) {
109 g_variant_new_string(ctx->handler->get_rest_api_endpoint_cb().c_str());
110 g_dbus_method_invocation_return_value(invocation,
111 g_variant_new_tuple(params, 1));
118 if (!s_instance) s_instance =
new DbusServer();
124 s_instance =
nullptr;
127 DbusServer::DbusServer()
129 introspection_data(0),
130 m_is_main_instance(false),
135 auto flags =
static_cast<GBusNameOwnerFlags
>(
136 G_BUS_NAME_OWNER_FLAGS_NONE | G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE);
138 auto ctx =
new DbusCtx(0,
this);
139 auto deleter = [](gpointer p) {
delete static_cast<DbusCtx*
>(p); };
140 static_user_data = ctx;
141 m_owner_id = g_bus_own_name(G_BUS_TYPE_SESSION, kDbusName, flags,
142 OnBusAcquired, OnNameAcquired, OnNameReleased,
144 DEBUG_LOG <<
"DbusServer::DbusServer, m_owner_id: " << m_owner_id;
147 DbusServer::~DbusServer() {
148 if (m_owner_id) g_bus_unown_name(m_owner_id);
155 using namespace std::literals::chrono_literals;
159 std::this_thread::sleep_for(5ms);
160 }
while (!m_is_main_instance && i++ < 200);
Basic interface to low-level DBus library.
GDBusNodeInfo * introspection_data
Callback context.
static void Disconnect()
Clear current singleton instance and disconnect from session bus.
void WaitUntilValid() override
Wait until ready.
Base interface for local server command handling.
DBus interface header file.