diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cfa250f..43d8622 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,10 +47,10 @@ set(libinputactions_SRCS libinputactions/handlers/TouchscreenTriggerHandler.cpp libinputactions/handlers/TriggerHandler.cpp libinputactions/helpers/Math.cpp + libinputactions/helpers/QDBusConnection.cpp libinputactions/helpers/QString.cpp libinputactions/helpers/QThread.cpp libinputactions/helpers/QVariant.cpp - libinputactions/helpers/Session.cpp libinputactions/input/backends/InputBackend.cpp libinputactions/input/backends/LibevdevComplementaryInputBackend.cpp libinputactions/input/backends/LibinputInputBackend.cpp diff --git a/src/libinputactions/dbus/IntegratedDBusInterface.cpp b/src/libinputactions/dbus/IntegratedDBusInterface.cpp index 0571b0e..8277ddd 100644 --- a/src/libinputactions/dbus/IntegratedDBusInterface.cpp +++ b/src/libinputactions/dbus/IntegratedDBusInterface.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -31,7 +32,7 @@ namespace InputActions { IntegratedDBusInterface::IntegratedDBusInterface() - : m_bus(QDBusConnection::sessionBus()) + : m_bus(QDBusConnectionHelpers::sessionBus()) { m_bus.registerService(INPUTACTIONS_DBUS_SERVICE); m_bus.registerObject(INPUTACTIONS_DBUS_PATH, this, QDBusConnection::ExportAllSlots); diff --git a/src/libinputactions/helpers/Session.cpp b/src/libinputactions/helpers/QDBusConnection.cpp similarity index 53% rename from src/libinputactions/helpers/Session.cpp rename to src/libinputactions/helpers/QDBusConnection.cpp index 088a556..3017970 100644 --- a/src/libinputactions/helpers/Session.cpp +++ b/src/libinputactions/helpers/QDBusConnection.cpp @@ -16,19 +16,32 @@ along with this program. If not, see . */ -#include "Session.h" -#include +#include "QDBusConnection.h" +#include +#include -namespace InputActions::SessionHelpers +namespace InputActions::QDBusConnectionHelpers { -QString currentTty() +const QDBusConnection &sessionBus() { - QFile f("/sys/class/tty/tty0/active"); - if (f.open(QIODeviceBase::ReadOnly)) { - return QString::fromUtf8(f.readAll()).trimmed(); + static std::optional cached; + if (cached) { + return cached.value(); } - return "unknown"; + + gid_t rgid{}; + gid_t egid{}; + gid_t sgid{}; + if (!getresgid(&rgid, &egid, &sgid) && (rgid != egid || egid != sgid)) { + if (const auto address = qEnvironmentVariable("DBUS_SESSION_BUS_ADDRESS"); !address.isEmpty()) { + cached = QDBusConnection::connectToBus(address, "sessionBus"); + return cached.value(); + } + } + + cached = QDBusConnection::sessionBus(); + return cached.value(); } } \ No newline at end of file diff --git a/src/libinputactions/helpers/Session.h b/src/libinputactions/helpers/QDBusConnection.h similarity index 70% rename from src/libinputactions/helpers/Session.h rename to src/libinputactions/helpers/QDBusConnection.h index fad4a64..f8b4974 100644 --- a/src/libinputactions/helpers/Session.h +++ b/src/libinputactions/helpers/QDBusConnection.h @@ -18,11 +18,17 @@ #pragma once -#include +#include -namespace InputActions::SessionHelpers +namespace InputActions::QDBusConnectionHelpers { -QString currentTty(); +/** + * Same as QDBusConnection::sessionBus, but uses QDBusConnection::connectToBus with the address specified in the DBUS_SESSION_BUS_ADDRESS environment variable + * for setgid binaries. + * + * Connection is cached. + */ +const QDBusConnection &sessionBus(); } \ No newline at end of file diff --git a/src/libinputactions/interfaces/implementations/DBusNotificationManager.cpp b/src/libinputactions/interfaces/implementations/DBusNotificationManager.cpp index 543e148..7c41c5b 100644 --- a/src/libinputactions/interfaces/implementations/DBusNotificationManager.cpp +++ b/src/libinputactions/interfaces/implementations/DBusNotificationManager.cpp @@ -19,6 +19,7 @@ #include "DBusNotificationManager.h" #include #include +#include namespace InputActions { @@ -31,7 +32,7 @@ void DBusNotificationManager::sendNotification(const QString &title, const QStri QDBusInterface notificationsInterface("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", - QDBusConnection::sessionBus()); + QDBusConnectionHelpers::sessionBus()); if (notificationsInterface.isValid()) { notificationsInterface.asyncCall("Notify", "InputActions", 0U, "", title, content, QStringList(), QVariantMap(), 5000); }