AyuGramDesktop/Telegram/SourceFiles/platform/linux/integration_linux.cpp
2023-05-15 09:51:13 +04:00

107 lines
2.5 KiB
C++

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
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 <xdpinhibit/xdpinhibit.hpp>
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<XdpInhibit::InhibitProxy> _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<uint>());
const auto sessionHandleToken = "tdesktop"
+ std::to_string(base::RandomValue<uint>());
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<Integration> CreateIntegration() {
return std::make_unique<LinuxIntegration>();
}
} // namespace Platform