From db60bee7dcdd3ec42219023e656899a813023743 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Mon, 8 May 2023 10:31:01 +0400 Subject: [PATCH] Implement screen locker monitor on Linux --- Telegram/CMakeLists.txt | 3 + .../platform/linux/integration_linux.cpp | 83 ++++++++ .../linux/org.freedesktop.portal.Inhibit.xml | 186 ++++++++++++++++++ 3 files changed, 272 insertions(+) create mode 100644 Telegram/SourceFiles/platform/linux/org.freedesktop.portal.Inhibit.xml diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 02b9b8cdb..2d39276ac 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1514,6 +1514,9 @@ else() desktop-app::external_glibmm ) + include(${cmake_helpers_loc}/external/glib/generate_dbus.cmake) + generate_dbus(Telegram org.freedesktop.portal. XdpInhibit ${src_loc}/platform/linux/org.freedesktop.portal.Inhibit.xml) + if (NOT DESKTOP_APP_DISABLE_X11_INTEGRATION) target_link_libraries(Telegram PRIVATE diff --git a/Telegram/SourceFiles/platform/linux/integration_linux.cpp b/Telegram/SourceFiles/platform/linux/integration_linux.cpp index 0af71d7c1..54176c3e0 100644 --- a/Telegram/SourceFiles/platform/linux/integration_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/integration_linux.cpp @@ -8,13 +8,96 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/linux/integration_linux.h" #include "platform/platform_integration.h" +#include "base/platform/linux/base_linux_xdp_utilities.h" +#include "core/application.h" +#include "base/random.h" + +#include + +using namespace gi::repository; namespace Platform { namespace { class LinuxIntegration final : public Integration { +public: + LinuxIntegration(); + + void init() override; + +private: + [[nodiscard]] XdpInhibit::Inhibit inhibit() { + return *_inhibitProxy; + } + + gi::result _inhibitProxy; }; +LinuxIntegration::LinuxIntegration() +: _inhibitProxy( + XdpInhibit::InhibitProxy::new_for_bus_sync( + Gio::BusType::SESSION_, + Gio::DBusProxyFlags::DO_NOT_AUTO_START_AT_CONSTRUCTION_, + std::string(base::Platform::XDP::kService), + std::string(base::Platform::XDP::kObjectPath))) { +} + +void LinuxIntegration::init() { + if (!_inhibitProxy) { + return; + } + + auto uniqueName = _inhibitProxy->get_connection().get_unique_name(); + uniqueName.erase(0, 1); + uniqueName.replace(uniqueName.find('.'), 1, 1, '_'); + + const auto handleToken = "tdesktop" + + std::to_string(base::RandomValue()); + + const auto sessionHandleToken = "tdesktop" + + std::to_string(base::RandomValue()); + + const auto sessionHandle = "/org/freedesktop/portal/desktop/session/" + + uniqueName + + '/' + + sessionHandleToken; + + inhibit().signal_state_changed().connect([ + mySessionHandle = sessionHandle + ]( + XdpInhibit::Inhibit, + const std::string &sessionHandle, + GLib::Variant state) { + if (sessionHandle != mySessionHandle) { + return; + } + + Core::App().setScreenIsLocked( + GLib::VariantDict::new_( + state + ).lookup_value( + "screensaver-active" + ).get_boolean() + ); + }); + + auto options = std::array{ + GLib::Variant::new_dict_entry( + GLib::Variant::new_string("handle_token"), + GLib::Variant::new_variant( + GLib::Variant::new_string(handleToken))), + GLib::Variant::new_dict_entry( + GLib::Variant::new_string("session_handle_token"), + GLib::Variant::new_variant( + GLib::Variant::new_string(sessionHandleToken))), + }; + + inhibit().call_create_monitor( + {}, + GLib::Variant::new_array(options.data(), options.size()), + nullptr); +} + } // namespace std::unique_ptr CreateIntegration() { diff --git a/Telegram/SourceFiles/platform/linux/org.freedesktop.portal.Inhibit.xml b/Telegram/SourceFiles/platform/linux/org.freedesktop.portal.Inhibit.xml new file mode 100644 index 000000000..e91bd22d3 --- /dev/null +++ b/Telegram/SourceFiles/platform/linux/org.freedesktop.portal.Inhibit.xml @@ -0,0 +1,186 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +