Skip to content

Commit

Permalink
idle-inhibit: move to new impl
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Apr 21, 2024
1 parent 2343238 commit 7b58834
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 67 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ protocolNew("staging/tearing-control/tearing-control-v1.xml" "tearing-control-v1
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)
protocolNew("staging/cursor-shape/cursor-shape-v1.xml" "cursor-shape-v1" false)
protocolNew("unstable/idle-inhibit/idle-inhibit-unstable-v1.xml" "idle-inhibit-unstable-v1" false)

# tools
add_subdirectory(hyprctl)
Expand Down
1 change: 1 addition & 0 deletions protocols/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ new_protocols = [
[wl_protocol_dir, 'staging/fractional-scale/fractional-scale-v1.xml'],
[wl_protocol_dir, 'unstable/xdg-output/xdg-output-unstable-v1.xml'],
[wl_protocol_dir, 'staging/cursor-shape/cursor-shape-v1.xml'],
[wl_protocol_dir, 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'],
]

wl_protos_src = []
Expand Down
4 changes: 0 additions & 4 deletions src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,6 @@ void CCompositor::initServer() {

m_sWLRForeignRegistry = wlr_xdg_foreign_registry_create(m_sWLDisplay);

m_sWLRIdleInhibitMgr = wlr_idle_inhibit_v1_create(m_sWLDisplay);

wlr_xdg_foreign_v1_create(m_sWLDisplay, m_sWLRForeignRegistry);
wlr_xdg_foreign_v2_create(m_sWLDisplay, m_sWLRForeignRegistry);

Expand Down Expand Up @@ -300,7 +298,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_sWLRIdleInhibitMgr->events.new_inhibitor, &Events::listen_newIdleInhibitor, m_sWLRIdleInhibitMgr, "WLRIdleInhibitMgr");
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_sWLRTextInputMgr->events.text_input, &Events::listen_newTextInput, m_sWLRTextInputMgr, "TextInputMgr");
Expand Down Expand Up @@ -352,7 +349,6 @@ void CCompositor::removeAllSignals() {
removeWLSignal(&Events::listen_newVirtPtr);
removeWLSignal(&Events::listen_newVirtualKeyboard);
removeWLSignal(&Events::listen_RendererDestroy);
removeWLSignal(&Events::listen_newIdleInhibitor);
removeWLSignal(&Events::listen_powerMgrSetMode);
removeWLSignal(&Events::listen_newIME);
removeWLSignal(&Events::listen_newTextInput);
Expand Down
1 change: 0 additions & 1 deletion src/Compositor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class CCompositor {
wlr_foreign_toplevel_manager_v1* m_sWLRToplevelMgr;
wlr_tablet_manager_v2* m_sWLRTabletManager;
wlr_xdg_foreign_registry* m_sWLRForeignRegistry;
wlr_idle_inhibit_manager_v1* m_sWLRIdleInhibitMgr;
wlr_pointer_gestures_v1* m_sWLRPointerGestures;
wlr_output_power_manager_v1* m_sWLROutputPowerMgr;
wlr_input_method_manager_v2* m_sWLRIMEMgr;
Expand Down
2 changes: 0 additions & 2 deletions src/events/Events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ namespace Events {
// Renderer destroy
LISTENER(RendererDestroy);

LISTENER(newIdleInhibitor);

// session
LISTENER(sessionActive);

Expand Down
12 changes: 0 additions & 12 deletions src/helpers/WLClasses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,6 @@ struct STabletPad {
}
};

struct SIdleInhibitor {
wlr_idle_inhibitor_v1* pWlrInhibitor = nullptr;
CWindow* pWindow = nullptr;
std::shared_ptr<HOOK_CALLBACK_FN> onWindowDestroy;

DYNLISTENER(Destroy);

bool operator==(const SIdleInhibitor& b) const {
return pWlrInhibitor == b.pWlrInhibitor;
}
};

struct SSwipeGesture {
PHLWORKSPACE pWorkspaceBegin = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/signal/Signal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class CSignal {
public:
void emit(std::any data);
void emit(std::any data = {});

//
[[nodiscard("Listener is unregistered when the ptr is lost")]] CHyprSignalListener registerListener(std::function<void(std::any)> handler);
Expand Down
1 change: 0 additions & 1 deletion src/includes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ extern "C" {
#include <wlr/types/wlr_keyboard_shortcuts_inhibit_v1.h>
#include <wlr/types/wlr_virtual_pointer_v1.h>
#include <wlr/types/wlr_foreign_toplevel_management_v1.h>
#include <wlr/types/wlr_idle_inhibit_v1.h>
#include <wlr/util/log.h>
#include <wlr/util/region.h>
#include <wlr/util/edges.h>
Expand Down
3 changes: 3 additions & 0 deletions src/managers/ProtocolManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
#include "../protocols/FractionalScale.hpp"
#include "../protocols/XDGOutput.hpp"
#include "../protocols/CursorShape.hpp"
#include "../protocols/IdleInhibit.hpp"

#include "tearing-control-v1.hpp"
#include "fractional-scale-v1.hpp"
#include "xdg-output-unstable-v1.hpp"
#include "cursor-shape-v1.hpp"
#include "idle-inhibit-unstable-v1.hpp"

CProtocolManager::CProtocolManager() {

PROTO::tearing = std::make_unique<CTearingControlProtocol>(&wp_tearing_control_manager_v1_interface, 1, "TearingControl");
PROTO::fractional = std::make_unique<CFractionalScaleProtocol>(&wp_fractional_scale_manager_v1_interface, 1, "FractionalScale");
PROTO::xdgOutput = std::make_unique<CXDGOutputProtocol>(&zxdg_output_manager_v1_interface, 3, "XDGOutput");
PROTO::cursorShape = std::make_unique<CCursorShapeProtocol>(&wp_cursor_shape_manager_v1_interface, 1, "CursorShape");
PROTO::idleInhibit = std::make_unique<CIdleInhibitProtocol>(&zwp_idle_inhibit_manager_v1_interface, 1, "IdleInhibit");

// Old protocol implementations.
// TODO: rewrite them to use hyprwayland-scanner.
Expand Down
60 changes: 19 additions & 41 deletions src/managers/input/IdleInhibitor.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,39 @@
#include "InputManager.hpp"
#include "../../Compositor.hpp"
#include "../../protocols/IdleInhibit.hpp"

void Events::listener_newIdleInhibitor(wl_listener* listener, void* data) {
const auto WLRIDLEINHIBITOR = (wlr_idle_inhibitor_v1*)data;
void CInputManager::newIdleInhibitor(std::any inhibitor) {
const auto PINHIBIT = m_vIdleInhibitors.emplace_back(std::make_unique<SIdleInhibitor>()).get();
PINHIBIT->inhibitor = std::any_cast<std::shared_ptr<CIdleInhibitor>>(inhibitor);

if (!WLRIDLEINHIBITOR)
return;

g_pInputManager->newIdleInhibitor(WLRIDLEINHIBITOR);
}

static void destroyInhibitor(SIdleInhibitor* inhibitor) {
g_pHookSystem->unhook(inhibitor->onWindowDestroy);

g_pInputManager->m_lIdleInhibitors.remove(*inhibitor);

Debug::log(LOG, "Destroyed an idleinhibitor");

g_pInputManager->recheckIdleInhibitorStatus();
}
Debug::log(LOG, "New idle inhibitor registered for surface {:x}", (uintptr_t)PINHIBIT->inhibitor->surface);

void CInputManager::newIdleInhibitor(wlr_idle_inhibitor_v1* pInhibitor) {
const auto PINHIBIT = &m_lIdleInhibitors.emplace_back();
PINHIBIT->inhibitor->listeners.destroy = PINHIBIT->inhibitor->resource.lock()->events.destroy.registerListener(
[this, PINHIBIT](std::any data) { std::erase_if(m_vIdleInhibitors, [PINHIBIT](const auto& other) { return other.get() == PINHIBIT; }); });

Debug::log(LOG, "New idle inhibitor registered");
const auto PWINDOW = g_pCompositor->getWindowFromSurface(PINHIBIT->inhibitor->surface);

PINHIBIT->pWlrInhibitor = pInhibitor;
if (!PWINDOW) {
Debug::log(WARN, "Inhibitor is for no window?");
return;
}

PINHIBIT->onWindowDestroy = g_pHookSystem->hookDynamic("closeWindow", [PINHIBIT](void* self, SCallbackInfo& info, std::any data) {
if (PINHIBIT->pWindow == std::any_cast<CWindow*>(data))
destroyInhibitor(PINHIBIT);
PINHIBIT->pWindow = PWINDOW;
PINHIBIT->windowDestroyListener = PWINDOW->events.destroy.registerListener([PINHIBIT](std::any data) {
Debug::log(WARN, "Inhibitor got its window destroyed before its inhibitor resource.");
PINHIBIT->pWindow = nullptr;
});

PINHIBIT->hyprListener_Destroy.initCallback(
&pInhibitor->events.destroy,
[](void* owner, void* data) {
const auto PINH = (SIdleInhibitor*)owner;

destroyInhibitor(PINH);
},
PINHIBIT, "IdleInhibitor");

PINHIBIT->pWindow = g_pCompositor->getWindowFromSurface(pInhibitor->surface);

if (PINHIBIT->pWindow)
Debug::log(LOG, "IdleInhibitor got window {}", PINHIBIT->pWindow);

recheckIdleInhibitorStatus();
}

void CInputManager::recheckIdleInhibitorStatus() {

for (auto& ii : m_lIdleInhibitors) {
if (!ii.pWindow) {
for (auto& ii : m_vIdleInhibitors) {
if (!ii->pWindow) {
g_pCompositor->setIdleActivityInhibit(false);
return;
} else if (g_pHyprRenderer->shouldRenderWindow(ii.pWindow)) {
} else if (g_pHyprRenderer->shouldRenderWindow(ii->pWindow)) {
g_pCompositor->setIdleActivityInhibit(false);
return;
}
Expand Down
5 changes: 4 additions & 1 deletion src/managers/input/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../../config/ConfigValue.hpp"
#include "../../desktop/Window.hpp"
#include "../../protocols/CursorShape.hpp"
#include "../../protocols/IdleInhibit.hpp"

CInputManager::CInputManager() {
m_sListeners.setCursorShape = PROTO::cursorShape->events.setShape.registerListener([this](std::any data) {
Expand All @@ -29,6 +30,8 @@ CInputManager::CInputManager() {
m_sCursorSurfaceInfo.inUse = true;
g_pHyprRenderer->setCursorFromName(m_sCursorSurfaceInfo.name);
});

m_sListeners.newIdleInhibitor = PROTO::idleInhibit->events.newIdleInhibitor.registerListener([this](std::any data) { this->newIdleInhibitor(data); });
}

CInputManager::~CInputManager() {
Expand All @@ -38,7 +41,7 @@ CInputManager::~CInputManager() {
m_lTablets.clear();
m_lTabletTools.clear();
m_lTabletPads.clear();
m_lIdleInhibitors.clear();
m_vIdleInhibitors.clear();
m_lTouchDevices.clear();
m_lSwitches.clear();
}
Expand Down
16 changes: 12 additions & 4 deletions src/managers/input/InputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include "../../defines.hpp"
#include <list>
#include <any>
#include "../../helpers/WLClasses.hpp"
#include "../../helpers/Timer.hpp"
#include "InputMethodRelay.hpp"
#include "../../helpers/signal/Listener.hpp"

class CConstraint;
class CWindow;
class CIdleInhibitor;

enum eClickBehaviorMode {
CLICKMODE_DEFAULT = 0,
Expand Down Expand Up @@ -132,9 +134,6 @@ class CInputManager {
std::list<STabletTool> m_lTabletTools;
std::list<STabletPad> m_lTabletPads;

// idle inhibitors
std::list<SIdleInhibitor> m_lIdleInhibitors;

// Touch devices
std::list<STouchDevice> m_lTouchDevices;

Expand All @@ -150,7 +149,7 @@ class CInputManager {
void newTabletTool(wlr_input_device*);
void newTabletPad(wlr_input_device*);
void focusTablet(STablet*, wlr_tablet_tool*, bool motion = false);
void newIdleInhibitor(wlr_idle_inhibitor_v1*);
void newIdleInhibitor(std::any);
void recheckIdleInhibitorStatus();

void onSwipeBegin(wlr_pointer_swipe_begin_event*);
Expand Down Expand Up @@ -201,6 +200,7 @@ class CInputManager {
// Listeners
struct {
CHyprSignalListener setCursorShape;
CHyprSignalListener newIdleInhibitor;
} m_sListeners;

bool m_bCursorImageOverridden = false;
Expand Down Expand Up @@ -237,6 +237,14 @@ class CInputManager {
// for releasing mouse buttons
std::list<uint32_t> m_lCurrentlyHeldButtons;

// idle inhibitors
struct SIdleInhibitor {
std::shared_ptr<CIdleInhibitor> inhibitor;
CWindow* pWindow = nullptr;
CHyprSignalListener windowDestroyListener;
};
std::vector<std::unique_ptr<SIdleInhibitor>> m_vIdleInhibitors;

// swipe
void beginWorkspaceSwipe();
void updateWorkspaceSwipe(double);
Expand Down
48 changes: 48 additions & 0 deletions src/protocols/IdleInhibit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "IdleInhibit.hpp"

CIdleInhibitor::CIdleInhibitor(SP<CIdleInhibitorResource> resource_, wlr_surface* surf_) : resource(resource_), surface(surf_) {
;
}

CIdleInhibitorResource::CIdleInhibitorResource(SP<CZwpIdleInhibitorV1> resource_, wlr_surface* surface_) : resource(resource_), surface(surface_) {
hyprListener_surfaceDestroy.initCallback(
&surface->events.destroy, [this](void* owner, void* data) { PROTO::idleInhibit->removeInhibitor(this); }, this, "CIdleInhibitorResource");

resource->setOnDestroy([this](CZwpIdleInhibitorV1* p) { PROTO::idleInhibit->removeInhibitor(this); });
resource->setDestroy([this](CZwpIdleInhibitorV1* p) { PROTO::idleInhibit->removeInhibitor(this); });
}

CIdleInhibitorResource::~CIdleInhibitorResource() {
hyprListener_surfaceDestroy.removeCallback();
events.destroy.emit();
}

CIdleInhibitProtocol::CIdleInhibitProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
;
}

void CIdleInhibitProtocol::onManagerResourceDestroy(wl_resource* res) {
std::erase_if(m_vManagers, [res](const auto& other) { return other->resource() == res; });
}

void CIdleInhibitProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CZwpIdleInhibitManagerV1>(client, ver, id)).get();
RESOURCE->setOnDestroy([this](CZwpIdleInhibitManagerV1* p) { this->onManagerResourceDestroy(p->resource()); });

RESOURCE->setDestroy([this](CZwpIdleInhibitManagerV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
RESOURCE->setCreateInhibitor(
[this](CZwpIdleInhibitManagerV1* pMgr, uint32_t id, wl_resource* surface) { this->onCreateInhibitor(pMgr, id, wlr_surface_from_resource(surface)); });
}

void CIdleInhibitProtocol::removeInhibitor(CIdleInhibitorResource* resource) {
std::erase_if(m_vInhibitors, [resource](const auto& el) { return el.get() == resource; });
}

void CIdleInhibitProtocol::onCreateInhibitor(CZwpIdleInhibitManagerV1* pMgr, uint32_t id, wlr_surface* surface) {
const auto CLIENT = wl_resource_get_client(pMgr->resource());
const auto RESOURCE =
m_vInhibitors.emplace_back(std::make_shared<CIdleInhibitorResource>(std::make_shared<CZwpIdleInhibitorV1>(CLIENT, wl_resource_get_version(pMgr->resource()), id), surface));

RESOURCE->inhibitor = std::make_shared<CIdleInhibitor>(RESOURCE, surface);
events.newIdleInhibitor.emit(RESOURCE->inhibitor);
}
63 changes: 63 additions & 0 deletions src/protocols/IdleInhibit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <memory>
#include <unordered_map>
#include "WaylandProtocol.hpp"
#include "idle-inhibit-unstable-v1.hpp"
#include "../helpers/signal/Signal.hpp"

class CIdleInhibitorResource;

class CIdleInhibitor {
public:
CIdleInhibitor(SP<CIdleInhibitorResource> resource_, wlr_surface* surf_);

struct {
CHyprSignalListener destroy;
} listeners;

WP<CIdleInhibitorResource> resource;
wlr_surface* surface = nullptr;
};

class CIdleInhibitorResource {
public:
CIdleInhibitorResource(SP<CZwpIdleInhibitorV1> resource_, wlr_surface* surface_);
~CIdleInhibitorResource();

SP<CIdleInhibitor> inhibitor;

struct {
CSignal destroy;
} events;

private:
SP<CZwpIdleInhibitorV1> resource;
wlr_surface* surface = nullptr;

DYNLISTENER(surfaceDestroy);
};

class CIdleInhibitProtocol : public IWaylandProtocol {
public:
CIdleInhibitProtocol(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);

void onManagerResourceDestroy(wl_resource* res);
void onCreateInhibitor(CZwpIdleInhibitManagerV1* pMgr, uint32_t id, wlr_surface* surface);

void removeInhibitor(CIdleInhibitorResource*);

struct {
CSignal newIdleInhibitor; // data: SP<CIdleInhibitor>
} events;

private:
std::vector<UP<CZwpIdleInhibitManagerV1>> m_vManagers;
std::vector<SP<CIdleInhibitorResource>> m_vInhibitors;
};

namespace PROTO {
inline UP<CIdleInhibitProtocol> idleInhibit;
}
Loading

0 comments on commit 7b58834

Please sign in to comment.