blob: a48d7800403dd12433d4aa7c882fb8d2d4ab2511 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#pragma once
#include <functional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <optional>
#include <cstdint>
#include "../../helpers/memory/Memory.hpp"
#include "../../helpers/signal/Signal.hpp"
#include "../../desktop/DesktopTypes.hpp"
extern "C" {
#include <lua.h>
}
namespace Config::Lua {
// Manages hl.on() event subscriptions for a single Lua state lifetime.
// Destroyed (and recreated) on every config reload so callbacks never double-up.
//
// Hyprland objects (window, workspace, monitor, layer surface) are exposed to Lua
// as typed userdata holding a weak pointer. Field accesses read live C++ state via
// __index; accessing a field on a destroyed object raises a Lua error.
class CLuaEventHandler {
public:
explicit CLuaEventHandler(lua_State* L);
~CLuaEventHandler();
// Store a Lua function (as a registry ref) to be called when `name` fires.
// Returns a subscription handle, or std::nullopt if the event name is unknown.
std::optional<uint64_t> registerEvent(const std::string& name, int luaRef);
bool unregisterEvent(uint64_t handle);
void clearEvents();
static const std::unordered_set<std::string>& knownEvents();
private:
struct SSubscription {
std::string eventName;
int luaRef = -1;
};
lua_State* m_lua = nullptr;
std::unordered_map<std::string, std::vector<uint64_t>> m_callbacks;
std::unordered_map<uint64_t, SSubscription> m_subscriptions;
std::unordered_set<uint64_t> m_activeHandles;
std::unordered_set<uint64_t> m_reentrancyWarnedHandles;
uint64_t m_nextHandle = 1;
size_t m_dispatchDepth = 0;
std::vector<CHyprSignalListener> m_listeners;
static constexpr size_t MAX_DISPATCH_DEPTH = 32;
void dispatch(const std::string& name, int nargs, const std::function<void()>& pushArgs);
};
}
|