| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845 |
- #include "SessionMonitor.h"
- #include <algorithm>
- #include <array>
- #include <chrono>
- #include <cstdint>
- #include <cstdlib>
- #include <cstring>
- #include <memory>
- #include <optional>
- #include <poll.h>
- #include <string>
- #include <string_view>
- #include <sys/eventfd.h>
- #include <sys/socket.h>
- #include <tuple>
- #include <unistd.h>
- #include "WallpaperEngine/Desktop/DlopenX11.h"
- #include "WallpaperEngine/Desktop/HyprlandIPC.h"
- #include "WallpaperEngine/Desktop/SessionBus.h"
- #include "WallpaperEngine/Logging/Log.h"
- #include "WallpaperEngine/Logging/ThreadGuard.h"
- #include "nlohmann/json.hpp"
- #if defined(ENABLE_WAYLAND) && defined(ENABLE_KDE_FEATURES)
- #define LWE_KWIN_DPMS 1
- #include <vector>
- #include <wayland-client.h>
- #include "WallpaperEngine/Desktop/HostWayland.h"
- #include "WallpaperEngine/Desktop/KDESession.h"
- #include "kwin-dpms-protocol.h"
- #endif
- using namespace WallpaperEngine::Desktop;
- namespace {
- // signals can be missed when a screen locker restarts, so the state is asked again now and then
- constexpr auto RECHECK_INTERVAL = std::chrono::seconds (30);
- constexpr int DPMS_POLL_MS = 1000;
- struct ScreenSaverName {
- const char* service;
- const char* path;
- const char* interface;
- };
- constexpr std::array SCREEN_SAVERS = {
- ScreenSaverName { "org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver" },
- ScreenSaverName { "org.freedesktop.ScreenSaver", "/ScreenSaver", "org.freedesktop.ScreenSaver" },
- ScreenSaverName { "org.gnome.ScreenSaver", "/org/gnome/ScreenSaver", "org.gnome.ScreenSaver" },
- ScreenSaverName { "org.cinnamon.ScreenSaver", "/org/cinnamon/ScreenSaver", "org.cinnamon.ScreenSaver" },
- ScreenSaverName { "org.mate.ScreenSaver", "/org/mate/ScreenSaver", "org.mate.ScreenSaver" },
- };
- // DPMS through dlopen, libXext isn't a build dependency
- class X11Dpms {
- public:
- bool open () {
- if (!this->m_x.load ("libXext.so.6")) {
- return false;
- }
- auto queryExtension = this->m_x.extensionSymbol<int (*) (void*, int*, int*)> ("DPMSQueryExtension");
- auto capable = this->m_x.extensionSymbol<int (*) (void*)> ("DPMSCapable");
- this->m_info = this->m_x.extensionSymbol<int (*) (void*, unsigned short*, unsigned char*)> ("DPMSInfo");
- if (queryExtension == nullptr || capable == nullptr || this->m_info == nullptr) {
- return false;
- }
- if (!this->m_x.connect ()) {
- return false;
- }
- int eventBase = 0;
- int errorBase = 0;
- return queryExtension (this->m_x.display (), &eventBase, &errorBase) && capable (this->m_x.display ());
- }
- /** DPMS off, standby and suspend all mean a blank screen. Nothing when DPMS is not enabled on the server */
- std::optional<bool> blanked () {
- unsigned short level = 0;
- unsigned char enabled = 0;
- if (!this->m_info (this->m_x.display (), &level, &enabled)) {
- return std::nullopt;
- }
- return enabled != 0 && level != 0;
- }
- private:
- DlopenX11 m_x;
- int (*m_info) (void*, unsigned short*, unsigned char*) = nullptr;
- };
- #ifdef LWE_KWIN_DPMS
- // KWin's org_kde_kwin_dpms: the compositor tells the state of each output, nothing is ever requested (the protocol
- // has a set request). Other compositors don't offer the manager and the connection is dropped again
- class KWinDpms {
- public:
- explicit KWinDpms (wl_display* display) : m_display (display) { }
- ~KWinDpms () {
- this->m_outputs.clear ();
- if (this->m_manager != nullptr) {
- org_kde_kwin_dpms_manager_destroy (this->m_manager);
- }
- if (this->m_registry != nullptr) {
- wl_registry_destroy (this->m_registry);
- }
- wl_display_disconnect (this->m_display);
- }
- KWinDpms (const KWinDpms&) = delete;
- KWinDpms& operator= (const KWinDpms&) = delete;
- bool open () {
- this->m_registry = wl_display_get_registry (this->m_display);
- wl_registry_add_listener (this->m_registry, ®istryListener, this);
- if (!this->roundtrip () || this->m_manager == nullptr) {
- return false;
- }
- for (const auto& output : this->m_outputs) {
- this->attach (*output);
- }
- return this->roundtrip ();
- }
- [[nodiscard]] int fd () const { return wl_display_get_fd (this->m_display); }
- [[nodiscard]] bool failed () const { return wl_display_get_error (this->m_display) != 0; }
- void beforePoll () {
- while (wl_display_prepare_read (this->m_display) != 0) {
- wl_display_dispatch_pending (this->m_display);
- }
- wl_display_flush (this->m_display);
- }
- void afterPoll (const bool readable) {
- if (readable) {
- wl_display_read_events (this->m_display);
- } else {
- wl_display_cancel_read (this->m_display);
- }
- wl_display_dispatch_pending (this->m_display);
- }
- [[nodiscard]] bool allOff () const {
- return !this->m_outputs.empty () && std::ranges::all_of (this->m_outputs, [] (const auto& output) {
- return output->mode != ORG_KDE_KWIN_DPMS_MODE_ON;
- });
- }
- private:
- static constexpr int ROUNDTRIP_MS = 2000;
- bool roundtrip () {
- bool done = false;
- wl_callback* callback = wl_display_sync (this->m_display);
- wl_callback_add_listener (callback, &syncListener, &done);
- const auto deadline = std::chrono::steady_clock::now () + std::chrono::milliseconds (ROUNDTRIP_MS);
- while (!done && !this->failed ()) {
- const auto left
- = std::chrono::duration_cast<std::chrono::milliseconds> (deadline - std::chrono::steady_clock::now ());
- if (left.count () <= 0) {
- break;
- }
- this->beforePoll ();
- pollfd pending = { .fd = this->fd (), .events = POLLIN, .revents = 0 };
- const int ready = poll (&pending, 1, static_cast<int> (left.count ()));
- this->afterPoll (ready > 0);
- }
- if (!done) {
- wl_callback_destroy (callback);
- }
- return done;
- }
- static void syncDone (void* data, wl_callback*, uint32_t) { *static_cast<bool*> (data) = true; }
- static constexpr wl_callback_listener syncListener = { .done = syncDone };
- struct Output {
- uint32_t name = 0;
- wl_output* output = nullptr;
- org_kde_kwin_dpms* dpms = nullptr;
- uint32_t mode = ORG_KDE_KWIN_DPMS_MODE_ON;
- ~Output () {
- if (this->dpms != nullptr) {
- org_kde_kwin_dpms_release (this->dpms);
- }
- if (this->output != nullptr) {
- wl_output_destroy (this->output);
- }
- }
- };
- void attach (Output& output) const {
- if (output.dpms == nullptr && this->m_manager != nullptr) {
- output.dpms = org_kde_kwin_dpms_manager_get (this->m_manager, output.output);
- org_kde_kwin_dpms_add_listener (output.dpms, &dpmsListener, &output);
- }
- }
- static void registryGlobal (void* data, wl_registry* registry, uint32_t name, const char* interface, uint32_t) {
- auto* self = static_cast<KWinDpms*> (data);
- if (std::strcmp (interface, org_kde_kwin_dpms_manager_interface.name) == 0) {
- self->m_manager = static_cast<org_kde_kwin_dpms_manager*> (
- wl_registry_bind (registry, name, &org_kde_kwin_dpms_manager_interface, 1)
- );
- } else if (std::strcmp (interface, wl_output_interface.name) == 0) {
- auto& output = self->m_outputs.emplace_back (std::make_unique<Output> ());
- output->name = name;
- output->output = static_cast<wl_output*> (wl_registry_bind (registry, name, &wl_output_interface, 1));
- self->attach (*output);
- }
- }
- static void registryGlobalRemove (void* data, wl_registry*, uint32_t name) {
- std::erase_if (static_cast<KWinDpms*> (data)->m_outputs, [name] (const auto& output) {
- return output->name == name;
- });
- }
- static void dpmsSupported (void*, org_kde_kwin_dpms*, uint32_t) { }
- static void dpmsMode (void* data, org_kde_kwin_dpms*, uint32_t mode) { static_cast<Output*> (data)->mode = mode; }
- static void dpmsDone (void*, org_kde_kwin_dpms*) { }
- static constexpr wl_registry_listener registryListener = {
- .global = registryGlobal,
- .global_remove = registryGlobalRemove,
- };
- static constexpr org_kde_kwin_dpms_listener dpmsListener = {
- .supported = dpmsSupported,
- .mode = dpmsMode,
- .done = dpmsDone,
- };
- wl_display* m_display;
- wl_registry* m_registry = nullptr;
- org_kde_kwin_dpms_manager* m_manager = nullptr;
- std::vector<std::unique_ptr<Output>> m_outputs;
- };
- #endif
- // sway and Hyprland blank an output without telling its clients (wlr-output-management still says enabled, and sway
- // keeps sending frame callbacks), but both list the power state of their outputs on the IPC socket. Only read, asked
- // once a second like an X server's DPMS
- class OutputPower {
- public:
- static std::unique_ptr<OutputPower> detect () {
- const char* runtime = std::getenv ("XDG_RUNTIME_DIR");
- if (const char* sway = std::getenv ("SWAYSOCK"); sway != nullptr && *sway != '\0') {
- return std::unique_ptr<OutputPower> (new OutputPower (Kind::Sway, sway));
- }
- if (auto hypr = findHyprlandSocket (
- runtime != nullptr ? runtime : "", std::getenv ("HYPRLAND_INSTANCE_SIGNATURE"),
- std::getenv ("WAYLAND_DISPLAY"), std::getenv ("XDG_CURRENT_DESKTOP")
- );
- !hypr.empty ()) {
- return std::unique_ptr<OutputPower> (new OutputPower (Kind::Hyprland, hypr));
- }
- return nullptr;
- }
- [[nodiscard]] std::optional<bool> blanked () const {
- const auto reply
- = this->m_kind == Kind::Sway ? this->swayOutputs () : hyprlandRequest (this->m_path, "j/monitors");
- if (!reply.has_value ()) {
- return std::nullopt;
- }
- const auto outputs = nlohmann::json::parse (*reply, nullptr, false);
- if (!outputs.is_array ()) {
- return std::nullopt;
- }
- const char* key = this->m_kind == Kind::Sway ? "power" : "dpmsStatus";
- bool any = false;
- for (const auto& output : outputs) {
- if (!output.is_object ()) {
- return std::nullopt;
- }
- if (this->m_kind == Kind::Sway) {
- const auto active = output.find ("active");
- if (active != output.end () && !active->is_boolean ()) {
- return std::nullopt;
- }
- if (active == output.end () || !active->get<bool> ()) {
- continue;
- }
- }
- const auto state = output.find (key);
- if (state == output.end () || !state->is_boolean ()) {
- return std::nullopt;
- }
- if (state->get<bool> ()) {
- return false;
- }
- any = true;
- }
- return any;
- }
- private:
- enum class Kind { Sway, Hyprland };
- OutputPower (const Kind kind, std::string path) : m_kind (kind), m_path (std::move (path)) { }
- [[nodiscard]] std::optional<std::string> swayOutputs () const {
- const int fd = connectUnixSocket (this->m_path, 100000);
- if (fd < 0) {
- return std::nullopt;
- }
- std::optional<std::string> result;
- constexpr std::string_view MAGIC = "i3-ipc";
- char request[MAGIC.size () + 8] = {};
- const uint32_t type = 3;
- std::copy (MAGIC.begin (), MAGIC.end (), request);
- std::memcpy (request + MAGIC.size () + 4, &type, sizeof (type));
- char header[sizeof (request)];
- if (send (fd, request, sizeof (request), MSG_NOSIGNAL) == static_cast<ssize_t> (sizeof (request))
- && recvAll (fd, header, sizeof (header))) {
- uint32_t length = 0;
- std::memcpy (&length, header + MAGIC.size (), sizeof (length));
- // a monitor list is a few KB, anything near a megabyte is not one
- if (length < (1u << 20)) {
- std::string payload (length, '\0');
- if (recvAll (fd, payload.data (), length)) {
- result = std::move (payload);
- }
- }
- }
- close (fd);
- return result;
- }
- static bool recvAll (const int fd, char* data, const size_t size) {
- size_t done = 0;
- while (done < size) {
- const ssize_t got = recv (fd, data + done, size - done, 0);
- if (got <= 0) {
- return false;
- }
- done += static_cast<size_t> (got);
- }
- return true;
- }
- Kind m_kind;
- std::string m_path;
- };
- DBusMessage* methodCall (const char* service, const char* path, const char* interface, const char* method) {
- DBusMessage* call = dbus_message_new_method_call (service, path, interface, method);
- // asking must never start a screen saver the session doesn't run
- dbus_message_set_auto_start (call, FALSE);
- return call;
- }
- std::optional<bool> readBoolean (DBusMessage* reply) {
- DBusMessageIter iter;
- dbus_bool_t value = FALSE;
- if (!dbus_message_iter_init (reply, &iter)) {
- return std::nullopt;
- }
- if (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_VARIANT) {
- DBusMessageIter variant;
- dbus_message_iter_recurse (&iter, &variant);
- if (dbus_message_iter_get_arg_type (&variant) != DBUS_TYPE_BOOLEAN) {
- return std::nullopt;
- }
- dbus_message_iter_get_basic (&variant, &value);
- return value != FALSE;
- }
- if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_BOOLEAN) {
- return std::nullopt;
- }
- dbus_message_iter_get_basic (&iter, &value);
- return value != FALSE;
- }
- std::optional<bool> screenSaverActive (DBusConnection* bus, const ScreenSaverName& name) {
- std::string error;
- DBusMessage* reply = callBlocking (bus, methodCall (name.service, name.path, name.interface, "GetActive"), error);
- if (reply == nullptr) {
- return std::nullopt;
- }
- const auto active = readBoolean (reply);
- dbus_message_unref (reply);
- return active;
- }
- std::string logindSessionPath (DBusConnection* bus) {
- const auto objectPath = [] (DBusMessage* reply) -> std::string {
- const char* path = nullptr;
- std::string result;
- if (reply != nullptr) {
- if (dbus_message_get_args (reply, nullptr, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
- result = path;
- }
- dbus_message_unref (reply);
- }
- return result;
- };
- std::string error;
- DBusMessage* byPid = methodCall (
- "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "GetSessionByPID"
- );
- const auto pid = static_cast<dbus_uint32_t> (getpid ());
- dbus_message_append_args (byPid, DBUS_TYPE_UINT32, &pid, DBUS_TYPE_INVALID);
- if (auto path = objectPath (callBlocking (bus, byPid, error)); !path.empty ()) {
- return path;
- }
- // a Flatpak's pid is not the one logind knows
- if (const char* id = std::getenv ("XDG_SESSION_ID"); id != nullptr && *id != '\0') {
- DBusMessage* byId = methodCall (
- "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "GetSession"
- );
- dbus_message_append_args (byId, DBUS_TYPE_STRING, &id, DBUS_TYPE_INVALID);
- return objectPath (callBlocking (bus, byId, error));
- }
- return {};
- }
- std::optional<bool> logindLockedHint (DBusConnection* bus, const std::string& session) {
- DBusMessage* reply = getProperty (
- bus, "org.freedesktop.login1", session.c_str (), "org.freedesktop.login1.Session", "LockedHint", false
- );
- if (reply == nullptr) {
- return std::nullopt;
- }
- const auto hint = readBoolean (reply);
- dbus_message_unref (reply);
- return hint;
- }
- // 0 is on, 1 standby, 2 suspend, 3 off
- std::optional<bool> mutterDisplayOff (DBusConnection* bus) {
- DBusMessage* reply = getProperty (
- bus, "org.gnome.Mutter.DisplayConfig", "/org/gnome/Mutter/DisplayConfig", "org.gnome.Mutter.DisplayConfig",
- "PowerSaveMode", false
- );
- if (reply == nullptr) {
- return std::nullopt;
- }
- std::optional<bool> off;
- DBusMessageIter variant;
- if (propertyValue (reply, variant) && dbus_message_iter_get_arg_type (&variant) == DBUS_TYPE_INT32) {
- dbus_int32_t mode = 0;
- dbus_message_iter_get_basic (&variant, &mode);
- off = mode > 0;
- }
- dbus_message_unref (reply);
- return off;
- }
- void addMatch (DBusConnection* bus, const std::string& rule) {
- DBusError error;
- dbus_error_init (&error);
- dbus_bus_add_match (bus, rule.c_str (), &error);
- if (dbus_error_is_set (&error)) {
- dbus_error_free (&error);
- }
- }
- }
- struct SessionMonitor::Sources {
- DBusConnection* systemBus = nullptr;
- DBusConnection* sessionBus = nullptr;
- std::string session;
- std::atomic<bool> changed = true;
- // the argument of the last ActiveChanged, -1 before the first one; a locker that doesn't answer GetActive still
- // sends it
- std::atomic<int> screenSaverSignal = -1;
- bool hint = false;
- bool screenSaver = false;
- std::unique_ptr<X11Dpms> dpms;
- std::unique_ptr<OutputPower> outputPower;
- bool outputsOff = false;
- std::chrono::steady_clock::time_point outputsChecked {};
- bool mutterOff = false;
- #ifdef LWE_KWIN_DPMS
- wl_display* kwinDisplay = nullptr;
- std::unique_ptr<KWinDpms> kwin;
- #endif
- };
- namespace {
- DBusHandlerResult noteSignal (DBusConnection*, DBusMessage* message, void* data) {
- auto& sources = *static_cast<SessionMonitor::Sources*> (data);
- if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL) {
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
- dbus_bool_t active = FALSE;
- if (dbus_message_is_signal (message, "org.freedesktop.ScreenSaver", "ActiveChanged")
- || dbus_message_is_signal (message, "org.gnome.ScreenSaver", "ActiveChanged")
- || dbus_message_is_signal (message, "org.cinnamon.ScreenSaver", "ActiveChanged")
- || dbus_message_is_signal (message, "org.mate.ScreenSaver", "ActiveChanged")) {
- if (dbus_message_get_args (message, nullptr, DBUS_TYPE_BOOLEAN, &active, DBUS_TYPE_INVALID)) {
- sources.screenSaverSignal = active != FALSE;
- }
- }
- sources.changed = true;
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
- }
- SessionMonitor::SessionMonitor () :
- m_wakeFd (eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK)), m_sources (std::make_unique<Sources> ()) {
- #ifdef LWE_KWIN_DPMS
- // main thread: connectWayland picks the route to the compositor once (Flatpak helper)
- if (const char* type = std::getenv ("XDG_SESSION_TYPE"); type != nullptr && std::string_view (type) == "wayland"
- && std::getenv ("WAYLAND_DISPLAY") != nullptr && isKDESession ()) {
- this->m_sources->kwinDisplay = connectWayland ();
- }
- #endif
- try {
- this->m_thread = std::thread ([this] {
- WallpaperEngine::Logging::guardThread ("Session monitor", [this] { this->run (); });
- });
- } catch (...) {
- if (this->m_wakeFd >= 0) {
- close (this->m_wakeFd);
- }
- #ifdef LWE_KWIN_DPMS
- if (this->m_sources->kwinDisplay != nullptr) {
- wl_display_disconnect (this->m_sources->kwinDisplay);
- }
- #endif
- throw;
- }
- }
- SessionMonitor::~SessionMonitor () {
- this->m_stop = true;
- this->wake ();
- if (this->m_thread.joinable ()) {
- this->m_thread.join ();
- }
- closeBus (this->m_sources->systemBus);
- closeBus (this->m_sources->sessionBus);
- if (this->m_wakeFd >= 0) {
- close (this->m_wakeFd);
- }
- }
- void SessionMonitor::wake () const {
- if (this->m_wakeFd >= 0) {
- const uint64_t one = 1;
- std::ignore = write (this->m_wakeFd, &one, sizeof (one));
- }
- }
- void SessionMonitor::run () {
- auto& sources = *this->m_sources;
- sources.systemBus = privateBus (DBUS_BUS_SYSTEM);
- sources.sessionBus = privateBus (DBUS_BUS_SESSION);
- if (sources.systemBus != nullptr) {
- sources.session = logindSessionPath (sources.systemBus);
- if (!sources.session.empty ()) {
- addMatch (
- sources.systemBus,
- "type='signal',sender='org.freedesktop.login1',path='" + sources.session
- + "',interface='org.freedesktop.DBus.Properties'"
- );
- addMatch (
- sources.systemBus,
- "type='signal',sender='org.freedesktop.login1',path='" + sources.session
- + "',interface='org.freedesktop.login1.Session'"
- );
- dbus_connection_add_filter (sources.systemBus, noteSignal, &sources, nullptr);
- } else {
- sLog.debug ("Lock: no logind session, relying on the screen saver");
- }
- }
- if (sources.sessionBus != nullptr) {
- for (const auto& name : SCREEN_SAVERS) {
- addMatch (
- sources.sessionBus,
- std::string ("type='signal',interface='") + name.interface + "',member='ActiveChanged'"
- );
- }
- addMatch (
- sources.sessionBus,
- "type='signal',sender='org.gnome.Mutter.DisplayConfig',interface='org.freedesktop.DBus.Properties'"
- );
- dbus_connection_add_filter (sources.sessionBus, noteSignal, &sources, nullptr);
- }
- // an X server tells nothing by itself, it is asked once a second
- if (const char* type = std::getenv ("XDG_SESSION_TYPE"); type == nullptr || std::string_view (type) == "x11") {
- sources.dpms = std::make_unique<X11Dpms> ();
- if (!sources.dpms->open ()) {
- sources.dpms.reset ();
- }
- }
- if (const char* type = std::getenv ("XDG_SESSION_TYPE"); type != nullptr && std::string_view (type) == "wayland") {
- sources.outputPower = OutputPower::detect ();
- }
- #ifdef LWE_KWIN_DPMS
- if (sources.kwinDisplay != nullptr) {
- sources.kwin = std::make_unique<KWinDpms> (sources.kwinDisplay);
- sources.kwinDisplay = nullptr;
- if (!sources.kwin->open ()) {
- sLog.debug ("Display: no org_kde_kwin_dpms, relying on frame callbacks");
- sources.kwin.reset ();
- }
- }
- #endif
- auto nextRecheck = std::chrono::steady_clock::now ();
- while (!this->m_stop) {
- if (sources.changed.exchange (false) || std::chrono::steady_clock::now () >= nextRecheck) {
- nextRecheck = std::chrono::steady_clock::now () + RECHECK_INTERVAL;
- // a call that timed out says nothing, the state from before stays
- if (!sources.session.empty ()) {
- if (const auto hint = logindLockedHint (sources.systemBus, sources.session); hint.has_value ()) {
- sources.hint = *hint;
- }
- }
- std::optional<bool> active;
- bool saverOnBus = false;
- if (sources.sessionBus != nullptr) {
- for (const auto& name : SCREEN_SAVERS) {
- if (this->m_stop) {
- break;
- }
- saverOnBus = saverOnBus || dbus_bus_name_has_owner (sources.sessionBus, name.service, nullptr);
- if (const auto answer = screenSaverActive (sources.sessionBus, name); answer.has_value ()) {
- active = active.value_or (false) || *answer;
- }
- }
- }
- if (this->m_stop) {
- break;
- }
- // a locker that went away while locked never sends ActiveChanged(false)
- sources.screenSaver = active.value_or (saverOnBus && sources.screenSaverSignal == 1);
- if (sources.sessionBus != nullptr) {
- if (const auto off = mutterDisplayOff (sources.sessionBus); off.has_value ()) {
- sources.mutterOff = *off;
- }
- }
- const bool locked = sources.hint || sources.screenSaver;
- if (locked != this->m_locked) {
- this->m_locked = locked;
- sLog.out (
- "Lock: session ", locked ? "locked" : "unlocked", " (logind hint ", sources.hint ? "set" : "clear",
- ", screen saver ", sources.screenSaver ? "active" : "inactive", ")"
- );
- }
- }
- pollfd fds[4] = {
- { .fd = this->m_wakeFd, .events = POLLIN, .revents = 0 },
- { .fd = -1, .events = POLLIN, .revents = 0 },
- { .fd = -1, .events = POLLIN, .revents = 0 },
- { .fd = -1, .events = POLLIN, .revents = 0 },
- };
- // the blocking calls above read the socket and queue whatever else arrived, poll never sees that again
- bool queued = false;
- for (int i = 0; i < 2; i++) {
- DBusConnection* bus = i == 0 ? sources.systemBus : sources.sessionBus;
- if (bus != nullptr && dbus_connection_get_is_connected (bus)) {
- int fd = -1;
- dbus_connection_get_unix_fd (bus, &fd);
- fds[1 + i].fd = fd;
- queued = queued || dbus_connection_get_dispatch_status (bus) == DBUS_DISPATCH_DATA_REMAINS;
- }
- }
- bool displayOff = sources.mutterOff;
- if (sources.dpms != nullptr) {
- displayOff = displayOff || sources.dpms->blanked ().value_or (false);
- }
- if (sources.outputPower != nullptr) {
- const auto now = std::chrono::steady_clock::now ();
- if (now - sources.outputsChecked >= std::chrono::milliseconds (DPMS_POLL_MS)) {
- sources.outputsOff = sources.outputPower->blanked ().value_or (false);
- sources.outputsChecked = now;
- }
- displayOff = displayOff || sources.outputsOff;
- }
- #ifdef LWE_KWIN_DPMS
- if (sources.kwin != nullptr) {
- if (sources.kwin->failed ()) {
- sLog.debug ("Display: lost the Wayland connection used for KWin's DPMS state");
- sources.kwin.reset ();
- } else {
- displayOff = displayOff || sources.kwin->allOff ();
- sources.kwin->beforePoll ();
- fds[3].fd = sources.kwin->fd ();
- }
- }
- #endif
- if (displayOff != this->m_displayOff) {
- this->m_displayOff = displayOff;
- sLog.out ("Display: ", displayOff ? "blanked (DPMS)" : "on again");
- }
- auto untilRecheck
- = std::chrono::duration_cast<std::chrono::milliseconds> (nextRecheck - std::chrono::steady_clock::now ());
- if (sources.dpms != nullptr || sources.outputPower != nullptr) {
- untilRecheck = std::min (untilRecheck, std::chrono::milliseconds (DPMS_POLL_MS));
- }
- poll (fds, 4, queued ? 0 : static_cast<int> (std::max<int64_t> (1, untilRecheck.count ())));
- #ifdef LWE_KWIN_DPMS
- if (sources.kwin != nullptr) {
- sources.kwin->afterPoll ((fds[3].revents & POLLIN) != 0);
- }
- #endif
- if (fds[0].revents & POLLIN) {
- uint64_t value;
- std::ignore = read (this->m_wakeFd, &value, sizeof (value));
- }
- // unread messages count against the session bus quota, read them even when only the other bus spoke
- for (DBusConnection* bus : { sources.systemBus, sources.sessionBus }) {
- if (bus != nullptr) {
- drainBus (bus);
- }
- }
- }
- }
|