-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
98 lines (88 loc) · 2.61 KB
/
main.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
local Hyprctl = require("hyprctl")
local cjson = require("cjson")
local hyprctl = Hyprctl.new()
local execute = function(command)
return pcall(os.execute, command)
end
--- @param monitor table
local handle_external_monitor = function(monitor)
local name = monitor.name
local resolution = ("%dx%d"):format(monitor.width, monitor.height)
if resolution == "1920x1080" then
hyprctl:write(("/keyword monitor %s,%s,%s,%d"):format(name, "preferred", "0x-1080", 1))
elseif resolution == "3840x2160" then
hyprctl:write(("/keyword monitor %s,%s,%s,%d"):format(name, "preferred", "0x-1080", 2))
end
end
--- @return table
local get_monitors = function()
local data, err = hyprctl:write("j/monitors")
if err then
print("get monitors failed: " .. err)
return {}
end
local monitors = cjson.decode(data)
return monitors
end
hyprctl:register("monitoradded", function(event)
local monitor_name = event.data
print("monitor added: " .. monitor_name)
local monitors = get_monitors()
for _, monitor in ipairs(monitors) do
if monitor.name == monitor_name then
print("monitor found: " .. monitor_name)
handle_external_monitor(monitor)
execute("eww close-all && killall eww")
execute("eww open-many bar builtin")
return
end
end
print("monitor not found: " .. monitor_name)
end)
hyprctl:register("activewindow", function(event)
local window = event.data
print("active window: " .. window)
if not window then
return
end
local windows = {}
for w in string.gmatch(window, "[^,]+") do
windows[#windows + 1] = w
end
window = windows[#windows]
if not window then
return
end
execute("eww update active_window='" .. window .. "'")
end)
hyprctl:register("workspace", function(event)
local workspace = event.data
print("active workspace: " .. workspace)
execute("eww update active_workspace='" .. workspace .. "'")
end)
hyprctl:register("*", function(_)
local data, err = hyprctl:write("j/workspaces")
if err then
print("get monitors failed: " .. err)
return
end
local workspaces = cjson.decode(data)
table.sort(workspaces, function(a, b) return a.id < b.id end)
data = cjson.encode(workspaces)
execute("eww update workspaces='" .. data .. "'")
end)
-- Set monitors on startup
for _, monitor in ipairs(get_monitors()) do
local name = monitor.name
if name == "HDMI-A-1" then
handle_external_monitor(monitor)
goto continue
end
if name == "eDP-1" then
hyprctl:write(("/keyword monitor %s,%s,%s,%d"):format(name, "preferred", "0x0", 2))
goto continue
end
print("unknown monitor: " .. name)
::continue::
end
hyprctl:listen()