From 0376b90bcedf2e251d793a138fdfad753c36d65f Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Sun, 27 Aug 2023 14:59:43 +0530 Subject: [PATCH] Add support for inhibition locks. --- .../implementation/linux/device_info.cc | 44 ++++++++++++++----- .../implementation/linux/device_info.h | 38 +++++++++++++++- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/internal/platform/implementation/linux/device_info.cc b/internal/platform/implementation/linux/device_info.cc index d3d1c950..83af0413 100644 --- a/internal/platform/implementation/linux/device_info.cc +++ b/internal/platform/implementation/linux/device_info.cc @@ -8,24 +8,23 @@ #include #include +#include "absl/synchronization/mutex.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/linux/dbus.h" #include "internal/platform/implementation/linux/device_info.h" #include "internal/platform/logging.h" -#include "absl/synchronization/mutex.h" namespace nearby { namespace linux { void CurrentUserSession::RegisterScreenLockedListener( - absl::string_view listener_name, - std::function callback) { + absl::string_view listener_name, + std::function callback) { absl::MutexLock l(&screen_lock_listeners_mutex_); screen_lock_listeners_[listener_name] = callback; } -void - CurrentUserSession::UnregisterScreenLockedListener(absl::string_view listener_name) -{ +void CurrentUserSession::UnregisterScreenLockedListener( + absl::string_view listener_name) { absl::MutexLock l(&screen_lock_listeners_mutex_); screen_lock_listeners_.erase(listener_name); } @@ -46,8 +45,8 @@ void CurrentUserSession::onUnlock() { DeviceInfo::DeviceInfo(sdbus::IConnection &system_bus) : system_bus_(system_bus), - current_user_session_(std::make_unique(system_bus_)) { -} + current_user_session_(std::make_unique(system_bus_)), + login_manager_(std::make_unique(system_bus_)) {} std::optional DeviceInfo::GetOsDeviceName() const { Hostnamed hostnamed(system_bus_); @@ -128,8 +127,7 @@ std::optional DeviceInfo::GetLogPath() const { if (dir == NULL) { return std::filesystem::path("/tmp"); } - return std::filesystem::path(std::string(dir)) / "Google Nearby" / - "logs"; + return std::filesystem::path(std::string(dir)) / "Google Nearby" / "logs"; } std::optional DeviceInfo::GetCrashDumpPath() const { @@ -137,8 +135,7 @@ std::optional DeviceInfo::GetCrashDumpPath() const { if (dir == NULL) { return std::filesystem::path("/tmp"); } - return std::filesystem::path(std::string(dir)) / "Google Nearby" / - "crashes"; + return std::filesystem::path(std::string(dir)) / "Google Nearby" / "crashes"; } bool DeviceInfo::IsScreenLocked() const { @@ -149,5 +146,28 @@ bool DeviceInfo::IsScreenLocked() const { return false; } } + +bool DeviceInfo::PreventSleep() { + try { + inhibit_fd_ = login_manager_->Inhibit("sleep", "Google Nearby", + "Google Nearby", "block"); + return true; + } catch (const sdbus::Error& e) { + DBUS_LOG_METHOD_CALL_ERROR(login_manager_, "Inhibit", e); + return false; + } +} + +bool DeviceInfo::AllowSleep() { + if (!inhibit_fd_.has_value()) { + NEARBY_LOGS(ERROR) << __func__ + << "No inhibit lock is acquired at the moment"; + return false; + } + + inhibit_fd_.reset(); + return true; +} + } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/device_info.h b/internal/platform/implementation/linux/device_info.h index b1c4c7de..c2ca6287 100644 --- a/internal/platform/implementation/linux/device_info.h +++ b/internal/platform/implementation/linux/device_info.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -13,6 +14,7 @@ #include "absl/synchronization/mutex.h" #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/linux/hostname_client_glue.h" +#include "internal/platform/implementation/linux/login_manager_client_glue.h" #include "internal/platform/implementation/linux/login_session_client_glue.h" namespace nearby { @@ -62,6 +64,33 @@ public: ~Hostnamed() { unregisterProxy(); } }; +class LoginManager + : public sdbus::ProxyInterfaces { +public: + LoginManager(sdbus::IConnection &system_bus) + : ProxyInterfaces("org.freedesktop.login1", + "/org/freedesktop/hostname1") { + registerProxy(); + } + ~LoginManager() { unregisterProxy(); } + +protected: + void onSessionNew(const std::string &session_id, + const sdbus::ObjectPath &object_path) override {} + void onSessionRemoved(const std::string &session_id, + const sdbus::ObjectPath &object_path) override {} + void onUserNew(const uint32_t &uid, + const sdbus::ObjectPath &object_path) override {} + void onUserRemoved(const uint32_t &uid, + const sdbus::ObjectPath &object_path) override {} + void onSeatNew(const std::string &seat_id, + const sdbus::ObjectPath &object_path) override {} + void onSeatRemoved(const std::string &seat_id, + const sdbus::ObjectPath &object_path) override {} + void onPrepareForShutdown(const bool &start) override {} + void onPrepareForSleep(const bool &start) override {} +}; + class DeviceInfo : public api::DeviceInfo { public: DeviceInfo(sdbus::IConnection &system_bus); @@ -94,13 +123,18 @@ public: // TODO: Implement listening to logind for changes to LockedState. void RegisterScreenLockedListener( absl::string_view listener_name, - std::function callback) override{}; + std::function callback) override {} void - UnregisterScreenLockedListener(absl::string_view listener_name) override{}; + UnregisterScreenLockedListener(absl::string_view listener_name) override {} + + bool PreventSleep() override; + bool AllowSleep() override; private: sdbus::IConnection &system_bus_; std::unique_ptr current_user_session_; + std::unique_ptr login_manager_; + std::optional inhibit_fd_; }; } // namespace linux } // namespace nearby