diff --git a/CMakeLists.txt b/CMakeLists.txt index 16419981..aa34efb9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -252,7 +252,6 @@ target_link_libraries(Hyprland protocol("protocols/idle.xml" "idle" true) protocol("protocols/tablet-unstable-v2.xml" "tablet-unstable-v2" true) protocol("protocols/wlr-layer-shell-unstable-v1.xml" "wlr-layer-shell-unstable-v1" true) -protocol("protocols/wlr-output-power-management-unstable-v1.xml" "wlr-output-power-management-unstable-v1" true) protocol("protocols/wlr-screencopy-unstable-v1.xml" "wlr-screencopy-unstable-v1" true) protocol("subprojects/hyprland-protocols/protocols/hyprland-global-shortcuts-v1.xml" "hyprland-global-shortcuts-v1" true) protocol("subprojects/hyprland-protocols/protocols/hyprland-toplevel-export-v1.xml" "hyprland-toplevel-export-v1" true) @@ -262,6 +261,7 @@ protocol("unstable/text-input/text-input-unstable-v1.xml" "text-input-unstable-v protocolNew("protocols/wlr-gamma-control-unstable-v1.xml" "wlr-gamma-control-unstable-v1" true) protocolNew("protocols/wlr-foreign-toplevel-management-unstable-v1.xml" "wlr-foreign-toplevel-management-unstable-v1" true) +protocolNew("protocols/wlr-output-power-management-unstable-v1.xml" "wlr-output-power-management-unstable-v1" true) protocolNew("staging/tearing-control/tearing-control-v1.xml" "tearing-control-v1" false) protocolNew("staging/fractional-scale/fractional-scale-v1.xml" "fractional-scale-v1" false) protocolNew("unstable/xdg-output/xdg-output-unstable-v1.xml" "xdg-output-unstable-v1" false) diff --git a/protocols/meson.build b/protocols/meson.build index d5f2af64..8f7e897f 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -28,7 +28,6 @@ protocols = [ [wl_protocol_dir, 'unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml'], [wl_protocol_dir, 'unstable/text-input/text-input-unstable-v1.xml'], ['wlr-layer-shell-unstable-v1.xml'], - ['wlr-output-power-management-unstable-v1.xml'], ['wlr-screencopy-unstable-v1.xml'], ['tablet-unstable-v2.xml'], ['idle.xml'], @@ -39,6 +38,7 @@ protocols = [ new_protocols = [ ['wlr-gamma-control-unstable-v1.xml'], ['wlr-foreign-toplevel-management-unstable-v1.xml'], + ['wlr-output-power-management-unstable-v1.xml'], [wl_protocol_dir, 'staging/tearing-control/tearing-control-v1.xml'], [wl_protocol_dir, 'staging/fractional-scale/fractional-scale-v1.xml'], [wl_protocol_dir, 'unstable/xdg-output/xdg-output-unstable-v1.xml'], diff --git a/src/Compositor.cpp b/src/Compositor.cpp index d178215c..d33c73d6 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -228,8 +228,6 @@ void CCompositor::initServer() { m_sWLROutputLayout = wlr_output_layout_create(m_sWLDisplay); - m_sWLROutputPowerMgr = wlr_output_power_manager_v1_create(m_sWLDisplay); - m_sWLRXDGShell = wlr_xdg_shell_create(m_sWLDisplay, 6); m_sWLRCursor = wlr_cursor_create(); @@ -319,7 +317,6 @@ void CCompositor::initAllSignals() { addWLSignal(&m_sWLRVirtPtrMgr->events.new_virtual_pointer, &Events::listen_newVirtPtr, m_sWLRVirtPtrMgr, "VirtPtrMgr"); addWLSignal(&m_sWLRVKeyboardMgr->events.new_virtual_keyboard, &Events::listen_newVirtualKeyboard, m_sWLRVKeyboardMgr, "VKeyboardMgr"); addWLSignal(&m_sWLRRenderer->events.destroy, &Events::listen_RendererDestroy, m_sWLRRenderer, "WLRRenderer"); - addWLSignal(&m_sWLROutputPowerMgr->events.set_mode, &Events::listen_powerMgrSetMode, m_sWLROutputPowerMgr, "PowerMgr"); addWLSignal(&m_sWLRIMEMgr->events.input_method, &Events::listen_newIME, m_sWLRIMEMgr, "IMEMgr"); addWLSignal(&m_sWLRActivation->events.request_activate, &Events::listen_activateXDG, m_sWLRActivation, "ActivationV1"); addWLSignal(&m_sWLRSessionLockMgr->events.new_lock, &Events::listen_newSessionLock, m_sWLRSessionLockMgr, "SessionLockMgr"); @@ -365,7 +362,6 @@ void CCompositor::removeAllSignals() { removeWLSignal(&Events::listen_newVirtPtr); removeWLSignal(&Events::listen_newVirtualKeyboard); removeWLSignal(&Events::listen_RendererDestroy); - removeWLSignal(&Events::listen_powerMgrSetMode); removeWLSignal(&Events::listen_newIME); removeWLSignal(&Events::listen_activateXDG); removeWLSignal(&Events::listen_newSessionLock); diff --git a/src/Compositor.hpp b/src/Compositor.hpp index 47ffc7cf..931be9f3 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -66,7 +66,6 @@ class CCompositor { wlr_virtual_pointer_manager_v1* m_sWLRVirtPtrMgr; wlr_tablet_manager_v2* m_sWLRTabletManager; wlr_xdg_foreign_registry* m_sWLRForeignRegistry; - wlr_output_power_manager_v1* m_sWLROutputPowerMgr; wlr_input_method_manager_v2* m_sWLRIMEMgr; wlr_xdg_activation_v1* m_sWLRActivation; wlr_linux_dmabuf_v1* m_sWLRLinuxDMABuf; diff --git a/src/events/Events.hpp b/src/events/Events.hpp index 2f52f11d..b5109284 100644 --- a/src/events/Events.hpp +++ b/src/events/Events.hpp @@ -111,9 +111,6 @@ namespace Events { LISTENER(pinchUpdate); LISTENER(pinchEnd); - // Power - LISTENER(powerMgrSetMode); - // IME LISTENER(newIME); LISTENER(newVirtualKeyboard); diff --git a/src/events/Misc.cpp b/src/events/Misc.cpp index 996c5128..c6e42881 100644 --- a/src/events/Misc.cpp +++ b/src/events/Misc.cpp @@ -188,23 +188,6 @@ void Events::listener_sessionActive(wl_listener* listener, void* data) { g_pConfigManager->m_bWantsMonitorReload = true; } -void Events::listener_powerMgrSetMode(wl_listener* listener, void* data) { - Debug::log(LOG, "PowerMgr set mode!"); - - const auto EVENT = (wlr_output_power_v1_set_mode_event*)data; - const auto PMONITOR = g_pCompositor->getMonitorFromOutput(EVENT->output); - - if (!PMONITOR) { - Debug::log(ERR, "Invalid powerMgrSetMode output"); - return; - } - - wlr_output_state_set_enabled(PMONITOR->state.wlr(), EVENT->mode == 1); - - if (!PMONITOR->state.commit()) - Debug::log(ERR, "Couldn't set power mode"); -} - void Events::listener_newIME(wl_listener* listener, void* data) { Debug::log(LOG, "New IME added!"); diff --git a/src/helpers/Monitor.hpp b/src/helpers/Monitor.hpp index 4d1e1c70..9fd9ee2f 100644 --- a/src/helpers/Monitor.hpp +++ b/src/helpers/Monitor.hpp @@ -133,6 +133,8 @@ class CMonitor { CSignal destroy; CSignal connect; CSignal disconnect; + CSignal dpmsChanged; + CSignal modeChanged; } events; std::array>, 4> m_aLayerSurfaceLayers; diff --git a/src/includes.hpp b/src/includes.hpp index 7f679f97..1428351b 100644 --- a/src/includes.hpp +++ b/src/includes.hpp @@ -84,7 +84,6 @@ extern "C" { #include #include #include -#include #include #include #include diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 996424cc..556a7950 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -2006,6 +2006,8 @@ void CKeybindManager::dpms(std::string arg) { if (enable) g_pHyprRenderer->damageMonitor(m.get()); + + m->events.dpmsChanged.emit(); } g_pCompositor->m_bDPMSStateON = enable; diff --git a/src/managers/ProtocolManager.cpp b/src/managers/ProtocolManager.cpp index cffc6c83..9531e6d0 100644 --- a/src/managers/ProtocolManager.cpp +++ b/src/managers/ProtocolManager.cpp @@ -15,6 +15,7 @@ #include "../protocols/ShortcutsInhibit.hpp" #include "../protocols/TextInputV3.hpp" #include "../protocols/PointerConstraints.hpp" +#include "../protocols/OutputPower.hpp" #include "tearing-control-v1.hpp" #include "fractional-scale-v1.hpp" @@ -31,6 +32,7 @@ #include "keyboard-shortcuts-inhibit-unstable-v1.hpp" #include "text-input-unstable-v3.hpp" #include "pointer-constraints-unstable-v1.hpp" +#include "wlr-output-power-management-unstable-v1.hpp" CProtocolManager::CProtocolManager() { @@ -49,6 +51,7 @@ CProtocolManager::CProtocolManager() { PROTO::shortcutsInhibit = std::make_unique(&zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1, "ShortcutsInhibit"); PROTO::textInputV3 = std::make_unique(&zwp_text_input_manager_v3_interface, 1, "TextInputV3"); PROTO::constraints = std::make_unique(&zwp_pointer_constraints_v1_interface, 1, "PointerConstraints"); + PROTO::outputPower = std::make_unique(&zwlr_output_power_manager_v1_interface, 1, "OutputPower"); // Old protocol implementations. // TODO: rewrite them to use hyprwayland-scanner. diff --git a/src/protocols/OutputPower.cpp b/src/protocols/OutputPower.cpp new file mode 100644 index 00000000..48e713f5 --- /dev/null +++ b/src/protocols/OutputPower.cpp @@ -0,0 +1,80 @@ +#include "OutputPower.hpp" +#include "../Compositor.hpp" + +#define LOGM PROTO::outputPower->protoLog + +COutputPower::COutputPower(SP resource_, CMonitor* pMonitor_) : resource(resource_), pMonitor(pMonitor_) { + if (!resource->resource()) + return; + + resource->setDestroy([this](CZwlrOutputPowerV1* r) { PROTO::outputPower->destroyOutputPower(this); }); + resource->setOnDestroy([this](CZwlrOutputPowerV1* r) { PROTO::outputPower->destroyOutputPower(this); }); + + resource->setSetMode([this](CZwlrOutputPowerV1* r, zwlrOutputPowerV1Mode mode) { + if (!pMonitor) + return; + + pMonitor->dpmsStatus = mode == ZWLR_OUTPUT_POWER_V1_MODE_ON; + + wlr_output_state_set_enabled(pMonitor->state.wlr(), pMonitor->dpmsStatus); + + if (!pMonitor->state.commit()) + LOGM(ERR, "Couldn't set dpms to {} for {}", pMonitor->dpmsStatus, pMonitor->szName); + }); + + resource->sendMode(pMonitor->dpmsStatus ? ZWLR_OUTPUT_POWER_V1_MODE_ON : ZWLR_OUTPUT_POWER_V1_MODE_OFF); + + listeners.monitorDestroy = pMonitor->events.destroy.registerListener([this](std::any v) { + pMonitor = nullptr; + resource->sendFailed(); + }); + + listeners.monitorDpms = pMonitor->events.dpmsChanged.registerListener( + [this](std::any v) { resource->sendMode(pMonitor->dpmsStatus ? ZWLR_OUTPUT_POWER_V1_MODE_ON : ZWLR_OUTPUT_POWER_V1_MODE_OFF); }); + listeners.monitorState = pMonitor->events.modeChanged.registerListener( + [this](std::any v) { resource->sendMode(pMonitor->dpmsStatus ? ZWLR_OUTPUT_POWER_V1_MODE_ON : ZWLR_OUTPUT_POWER_V1_MODE_OFF); }); +} + +bool COutputPower::good() { + return resource->resource(); +} + +COutputPowerProtocol::COutputPowerProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { + ; +} + +void COutputPowerProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { + const auto RESOURCE = m_vManagers.emplace_back(std::make_unique(client, ver, id)).get(); + RESOURCE->setOnDestroy([this](CZwlrOutputPowerManagerV1* p) { this->onManagerResourceDestroy(p->resource()); }); + + RESOURCE->setDestroy([this](CZwlrOutputPowerManagerV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); }); + RESOURCE->setGetOutputPower([this](CZwlrOutputPowerManagerV1* hiThereFriend, uint32_t id, wl_resource* output) { this->onGetOutputPower(hiThereFriend, id, output); }); +} + +void COutputPowerProtocol::onManagerResourceDestroy(wl_resource* res) { + std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; }); +} + +void COutputPowerProtocol::destroyOutputPower(COutputPower* power) { + std::erase_if(m_vOutputPowers, [&](const auto& other) { return other.get() == power; }); +} + +void COutputPowerProtocol::onGetOutputPower(CZwlrOutputPowerManagerV1* pMgr, uint32_t id, wl_resource* output) { + + const auto PMONITOR = g_pCompositor->getMonitorFromOutput(wlr_output_from_resource(output)); + + if (!PMONITOR) { + wl_resource_post_error(pMgr->resource(), 0, "Invalid output resource"); + return; + } + + const auto CLIENT = wl_resource_get_client(pMgr->resource()); + const auto RESOURCE = + m_vOutputPowers.emplace_back(std::make_unique(std::make_shared(CLIENT, wl_resource_get_version(pMgr->resource()), id), PMONITOR)).get(); + + if (!RESOURCE->good()) { + wl_resource_post_no_memory(pMgr->resource()); + m_vOutputPowers.pop_back(); + return; + } +} diff --git a/src/protocols/OutputPower.hpp b/src/protocols/OutputPower.hpp new file mode 100644 index 00000000..44788a8f --- /dev/null +++ b/src/protocols/OutputPower.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include +#include "WaylandProtocol.hpp" +#include "wlr-output-power-management-unstable-v1.hpp" +#include "../helpers/signal/Listener.hpp" + +class CMonitor; + +class COutputPower { + public: + COutputPower(SP resource_, CMonitor* pMonitor); + + bool good(); + + private: + SP resource; + + CMonitor* pMonitor = nullptr; + + struct { + CHyprSignalListener monitorDestroy; + CHyprSignalListener monitorState; + CHyprSignalListener monitorDpms; + } listeners; +}; + +class COutputPowerProtocol : public IWaylandProtocol { + public: + COutputPowerProtocol(const wl_interface* iface, const int& ver, const std::string& name); + + virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id); + + private: + void onManagerResourceDestroy(wl_resource* res); + void destroyOutputPower(COutputPower* pointer); + void onGetOutputPower(CZwlrOutputPowerManagerV1* pMgr, uint32_t id, wl_resource* output); + + // + std::vector> m_vManagers; + std::vector> m_vOutputPowers; + + friend class COutputPower; +}; + +namespace PROTO { + inline UP outputPower; +}; \ No newline at end of file diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index 4d1ee97d..e34e6a72 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -1904,6 +1904,8 @@ bool CHyprRenderer::applyMonitorRule(CMonitor* pMonitor, SMonitorRule* pMonitorR if (pMonitor->m_bEnabled) pMonitor->onDisconnect(); + pMonitor->events.modeChanged.emit(); + return true; } @@ -2301,6 +2303,8 @@ bool CHyprRenderer::applyMonitorRule(CMonitor* pMonitor, SMonitorRule* pMonitorR EMIT_HOOK_EVENT("monitorLayoutChanged", nullptr); + pMonitor->events.modeChanged.emit(); + Events::listener_change(nullptr, nullptr); return true;