-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyprctl.lua
119 lines (107 loc) · 2.75 KB
/
hyprctl.lua
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
local unix = require("socket.unix")
local Hyprctl = {}
Hyprctl.__index = Hyprctl
--- @param bind_path string|nil
function Hyprctl.new(bind_path)
if not bind_path then
bind_path = os.getenv("XDG_RUNTIME_DIR") ..
"/hypr/" .. os.getenv("HYPRLAND_INSTANCE_SIGNATURE")
end
local self = setmetatable({}, Hyprctl)
self.bind_path = bind_path
return self
end
--- @class Event
--- @field name string
--- @field data string|nil
--- @param event_name string
--- @param callback fun(event: Event)
function Hyprctl:register(event_name, callback)
if not self.event_callback then
self.event_callback = {}
end
if not self.event_callback[event_name] then
self.event_callback[event_name] = {}
end
self.event_callback[event_name][#self.event_callback[event_name] + 1] = callback
end
function Hyprctl:listen()
local client = assert(unix.stream())
local socket_path = self.bind_path .. "/.socket2.sock"
local success, err = client:connect(socket_path)
if not success then
return nil, "connect failed: " .. err
end
while true do
::continue::
local chunk = ""
chunk, err = client:receive()
if chunk then
-- format: EVENT>>DATA\n
-- remove \n if exists
if chunk:sub(-1) == "\n" then
chunk = chunk:sub(1, -2)
end
-- split by `>>`
local parts = {}
for part in chunk:gmatch("([^>>]+)") do
table.insert(parts, part)
end
-- IPC events list: https://wiki.hyprland.org/hyprland-wiki/pages/IPC/
local event = {
name = parts[1],
}
if #parts > 1 then
event.data = parts[2]
end
-- support wildcard callback
local events = { event.name, "*" }
for _, evt in ipairs(events) do
if self.event_callback and self.event_callback[evt] then
for _, callback in ipairs(self.event_callback[evt]) do
callback(event)
end
end
end
end
if err == "closed" then
break
end
if err then
print("receive failed: " .. err)
goto continue
end
end
end
--- @param src string
function Hyprctl:write(src)
local client = assert(unix.stream())
local socket_path = self.bind_path .. "/.socket.sock"
local success, err = client:connect(socket_path)
if not success then
return nil, "connect failed: " .. err
end
-- Write data
assert(client:send(src))
-- Read response
local chunks = {}
local data = ""
local chunk = ""
while true do
chunk, err = client:receive("*a")
if chunk then
table.insert(chunks, chunk)
end
if err == "closed" then
break
end
if err then
return nil, "receive failed: " .. err
end
end
data = table.concat(chunks)
-- Close socket
client:close()
return data
end
return Hyprctl