};
struct owner_id {
- owner_id(std::string_view name, GBusAcquiredCallback on_connect, GBusNameLostCallback on_error,
- gpointer user_data)
+ owner_id() : inner(0) {}
+
+ void own_name_on_connection(GDBusConnection *connection, std::string_view name,
+ GBusNameAcquiredCallback on_name_acquired, GBusNameLostCallback on_name_lost, gpointer user_data)
{
- inner = g_bus_own_name(G_BUS_TYPE_SYSTEM, name.data(), G_BUS_NAME_OWNER_FLAGS_NONE, on_connect,
- nullptr, on_error, user_data, nullptr);
+ inner = g_bus_own_name_on_connection(connection, name.data(), G_BUS_NAME_OWNER_FLAGS_NONE,
+ on_name_acquired, on_name_lost, user_data, nullptr);
+ std::cout << "Acquiring " << name << std::endl;
}
~owner_id()
* before any sub session is switched to. */
static constexpr int INITIAL_SUB_SESSION_ID = 0;
- sessiond_context() : data(xml), id(bus_name, glib_bus_connected, glib_name_lost, this), loop()
+ sessiond_context() : data(xml), id(), loop()
{
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
+ if (!connection)
+ throw std::runtime_error("Cannot connect to the system bus");
+
+ guint registration_id = g_dbus_connection_register_object(
+ connection, bus_object.data(), data.inner->interfaces[0], &table, this, nullptr, nullptr);
+ if (registration_id <= 0)
+ throw std::runtime_error("Couldn't register bus ID");
+
+ id.own_name_on_connection(connection, bus_name, glib_name_acquired, glib_name_lost, this);
}
void run()
g_main_loop_run(loop.inner);
}
- void on_bus_connected(GDBusConnection *conn)
+ void on_name_acquired()
{
- connection = conn;
- guint registration_id = g_dbus_connection_register_object(
- connection, bus_object.data(), data.inner->interfaces[0], &table, this, nullptr, nullptr);
- if (registration_id <= 0)
- throw std::runtime_error("Couldn't register bus ID");
+ std::cout << "Bus name acquired" << std::endl;
}
void on_name_lost()
g_dbus_method_invocation_return_value(invocation, ret);
}
- static void glib_bus_connected(GDBusConnection *conn, const gchar *name, gpointer user_data)
+ static void glib_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
{
auto self = static_cast<sessiond_context *>(user_data);
- self->on_bus_connected(conn);
+ self->on_name_acquired();
}
+
static void glib_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data)
{
auto self = static_cast<sessiond_context *>(user_data);