mirror of
https://github.com/kotatogram/kotatogram-desktop
synced 2025-08-31 22:55:11 +00:00
Implement screen locker monitor on Linux
This commit is contained in:
@@ -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 <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() {
|
||||
|
Reference in New Issue
Block a user