Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/modules/inhibitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Inhibitor : public ALabel {
private:
auto handleToggle(::GdkEventButton* const& e) -> bool override;

const std::unique_ptr<::GDBusConnection, void (*)(::GDBusConnection*)> dbus_;
std::unique_ptr<::GDBusConnection, void (*)(::GDBusConnection*)> dbus_;
const std::string inhibitors_;
int handle_ = -1;
};
Expand Down
49 changes: 36 additions & 13 deletions src/modules/inhibitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,46 @@ namespace {

using DBus = std::unique_ptr<GDBusConnection, void (*)(GDBusConnection*)>;

auto destroyDBus(GDBusConnection* connection) -> void {
if (!g_dbus_connection_is_closed(connection)) {
GError* error = nullptr;
g_dbus_connection_close_sync(connection, nullptr, &error);
if (error) {
spdlog::error("g_bus_connection_close_sync failed(): {}", error->message);
g_error_free(error);
}
}
g_object_unref(connection);
}

// A connection of our own rather than the shared one from g_bus_get_sync(): the
// destructor below closes it, and closing the shared connection would close it
// for the whole process, with g_bus_get_sync() handing the same closed
// connection back to every later caller.
auto dbus() -> DBus {
GError* error = nullptr;
GDBusConnection* connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
gchar* address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);

if (error) {
spdlog::error("g_bus_get_sync() failed: {}", error->message);
spdlog::error("g_dbus_address_get_for_bus_sync() failed: {}", error->message);
g_error_free(error);
connection = nullptr;
return DBus{nullptr, destroyDBus};
}

auto destructor = [](GDBusConnection* connection) {
GError* error = nullptr;
g_dbus_connection_close_sync(connection, nullptr, &error);
if (error) {
spdlog::error("g_bus_connection_close_sync failed(): {}", error->message);
g_error_free(error);
}
};
GDBusConnection* connection = g_dbus_connection_new_for_address_sync(
address,
static_cast<GDBusConnectionFlags>(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION),
nullptr, nullptr, &error);
g_free(address);

return DBus{connection, destructor};
if (error) {
spdlog::error("g_dbus_connection_new_for_address_sync() failed: {}", error->message);
g_error_free(error);
connection = nullptr;
}

return DBus{connection, destroyDBus};
}

auto getLocks(const DBus& bus, const std::string& inhibitors) -> int {
Expand Down Expand Up @@ -135,7 +155,10 @@ auto Inhibitor::handleToggle(GdkEventButton* const& e) -> bool {
::close(handle_);
handle_ = -1;
} else {
handle_ = ::getLocks(dbus_, inhibitors_);
if (dbus_ == nullptr || g_dbus_connection_is_closed(dbus_.get())) {
dbus_ = ::dbus();
}
handle_ = dbus_ == nullptr ? -1 : ::getLocks(dbus_, inhibitors_);
if (handle_ == -1) {
spdlog::error("cannot get inhibitor locks");
}
Expand Down