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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
local M = {}
local function trim(value)
return (value or ""):gsub("^%s+", ""):gsub("%s+$", "")
end
local dsp = hl.dsp or hl
local window_api = (dsp and dsp.window) or hl.window or {}
local workspace_api = (dsp and dsp.workspace) or {}
local group_api = (dsp and dsp.group) or {}
local function exec_cmd(cmd)
if dsp and dsp.exec_cmd then
return dsp.exec_cmd(cmd)
end
return function()
hl.exec_cmd(cmd)
end
end
local function shell_quote(value)
return "'" .. tostring(value):gsub("'", "'\\''") .. "'"
end
local function raw_dispatch_cmd(command)
if dsp and dsp.exec_raw then
return dsp.exec_raw(tostring(command))
end
local expression = "hl.dsp.exec_raw(" .. string.format("%q", tostring(command)) .. ")"
return exec_cmd("hyprctl dispatch " .. shell_quote(expression))
end
local function workspace_dispatch(value)
if dsp and dsp.focus then
return function()
hl.dispatch(dsp.focus({ workspace = value }))
end
end
return raw_dispatch_cmd("workspace " .. tostring(value))
end
local known_dispatchers = {
bringactivetotop = true,
changegroupactive = true,
cyclenext = true,
fullscreen = true,
killactive = true,
layoutmsg = true,
movefocus = true,
moveintogroup = true,
moveoutofgroup = true,
movecurrentworkspacetomonitor = true,
movetoworkspace = true,
movetoworkspacesilent = true,
movewindow = true,
pseudo = true,
resizeactive = true,
setprop = true,
swapwindow = true,
togglegroup = true,
togglefloating = true,
togglespecialworkspace = true,
workspace = true,
}
local function direction(value)
local directions = {
l = "left",
r = "right",
u = "up",
d = "down",
left = "left",
right = "right",
up = "up",
down = "down",
}
return directions[trim(value)] or trim(value)
end
local function workspace_value(value)
value = trim(value)
return tonumber(value) or value
end
local function dispatch_factory_safely(factory)
pcall(function()
local dispatcher = factory()
if dispatcher then
hl.dispatch(dispatcher)
end
end)
end
local function dispatch(name, args)
name = trim(name)
args = trim(args)
if args:match("^exec%s*,") then
return exec_cmd(trim(args:gsub("^exec%s*,", "", 1)))
end
if name == "exec" then
return exec_cmd(args)
end
if known_dispatchers[args] and not known_dispatchers[name] then
if args == "movewindow" and window_api.drag then
return window_api.drag()
end
if args == "resizewindow" and window_api.resize then
return window_api.resize()
end
return raw_dispatch_cmd(args)
end
if name == "killactive" then
if window_api.close then
return window_api.close()
end
if window_api.kill then
return window_api.kill()
end
return raw_dispatch_cmd("killactive")
end
if name == "togglefloating" and window_api.float then
return window_api.float({ action = "toggle" })
end
if name == "fullscreen" then
if window_api.fullscreen then
if args == "1" then
return window_api.fullscreen({ mode = "maximized" })
end
return window_api.fullscreen({ mode = "fullscreen" })
end
if args == "1" then
return exec_cmd("hyprctl dispatch 'hl.dsp.window.fullscreen({ mode = \"maximized\" })'")
end
return exec_cmd("hyprctl dispatch 'hl.dsp.window.fullscreen({ mode = \"fullscreen\" })'")
end
if name == "pseudo" and window_api.pseudo then
return window_api.pseudo()
end
if name == "workspace" then
return workspace_dispatch(workspace_value(args))
end
if name == "movetoworkspace" then
if window_api.move then
return function()
hl.dispatch(window_api.move({ workspace = workspace_value(args) }))
end
end
return raw_dispatch_cmd("movetoworkspace " .. args)
end
if name == "movetoworkspacesilent" then
if window_api.move then
return function()
hl.dispatch(window_api.move({ workspace = workspace_value(args), follow = false }))
end
end
return raw_dispatch_cmd("movetoworkspacesilent " .. args)
end
if name == "resizeactive" then
return raw_dispatch_cmd("resizeactive " .. args)
end
if name == "movecurrentworkspacetomonitor" then
return raw_dispatch_cmd("movecurrentworkspacetomonitor " .. args)
end
if name == "movefocus" then
if dsp and dsp.focus then
return function()
dispatch_factory_safely(function()
return dsp.focus({ direction = direction(args) })
end)
end
end
return raw_dispatch_cmd("movefocus " .. args)
end
if name == "movewindow" then
if window_api.move then
return function()
dispatch_factory_safely(function()
return window_api.move({ direction = direction(args) })
end)
end
end
return raw_dispatch_cmd("movewindow " .. args)
end
if name == "swapwindow" then
local swap_direction = trim(args)
if swap_direction == "" then
return nil
end
return exec_cmd("$HOME/.config/hypr/scripts/LuaSwapWindow.sh " .. swap_direction)
end
if name == "togglegroup" and group_api.toggle then
return group_api.toggle()
end
if name == "changegroupactive" and group_api.next and group_api.prev then
if args == "b" or args == "prev" or args == "-1" then
return group_api.prev()
end
return group_api.next()
end
if name == "moveintogroup" and window_api.move then
return window_api.move({ into_group = direction(args) })
end
if name == "moveoutofgroup" and window_api.move then
return window_api.move({ out_of_group = true })
end
if name == "layoutmsg" and dsp and dsp.layout then
return dsp.layout(args)
end
if name == "bringactivetotop" and window_api.bring_to_top then
return window_api.bring_to_top()
end
if name == "setprop" and window_api.set_prop then
local prop, value = args:match("^(%S+)%s+(.+)$")
if prop and value then
return window_api.set_prop({ prop = prop, value = value })
end
end
if args ~= "" then
return raw_dispatch_cmd(name .. " " .. args)
end
return raw_dispatch_cmd(name)
end
local function chord(mods, key)
mods = trim(mods):gsub("%s+", " + ")
key = trim(key)
key = key:gsub("^xf86", "XF86")
local key_aliases = {
XF86AudioPlayPause = "XF86AudioPlay",
XF86audiolowervolume = "XF86AudioLowerVolume",
XF86audiomute = "XF86AudioMute",
XF86audioraisevolume = "XF86AudioRaiseVolume",
XF86audiostop = "XF86AudioStop",
}
key = key_aliases[key] or key
local shifted_number_keys = {
["code:10"] = "exclam",
["code:11"] = "at",
["code:12"] = "numbersign",
["code:13"] = "dollar",
["code:14"] = "percent",
["code:15"] = "asciicircum",
["code:16"] = "ampersand",
["code:17"] = "asterisk",
["code:18"] = "parenleft",
["code:19"] = "parenright",
}
local number_keys = {
["code:10"] = "1",
["code:11"] = "2",
["code:12"] = "3",
["code:13"] = "4",
["code:14"] = "5",
["code:15"] = "6",
["code:16"] = "7",
["code:17"] = "8",
["code:18"] = "9",
["code:19"] = "0",
}
if mods:match("SHIFT") and shifted_number_keys[key] then
key = shifted_number_keys[key]
else
key = number_keys[key] or key
end
if mods == "" then
return key
end
return mods .. " + " .. key
end
local function bind(mods, key, fn, opts)
if opts then
hl.bind(chord(mods, key), fn, opts)
else
hl.bind(chord(mods, key), fn)
end
if mods:match("SHIFT") then
local number_key = ({
["code:10"] = "1",
["code:11"] = "2",
["code:12"] = "3",
["code:13"] = "4",
["code:14"] = "5",
["code:15"] = "6",
["code:16"] = "7",
["code:17"] = "8",
["code:18"] = "9",
["code:19"] = "0",
})[key]
if number_key then
if opts then
hl.bind(chord(mods, number_key), fn, opts)
else
hl.bind(chord(mods, number_key), fn)
end
end
end
end
local function unbind_chord(key_chord)
if hl.unbind then
pcall(hl.unbind, key_chord)
end
end
local function bindm(mods, key, dispatcher, description)
local action = nil
if dispatcher == "movewindow" and window_api.drag then
action = window_api.drag()
elseif dispatcher == "resizewindow" then
if window_api.resize then
action = window_api.resize()
else
action = raw_dispatch_cmd("resizewindow")
end
else
action = raw_dispatch_cmd(dispatcher)
end
bind(mods, key, action, { description = description, mouse = true })
end
local keys_to_unbind = {
"SUPER + V",
"SUPER + W",
"SUPER + P",
"SUPER + R",
"SUPER + N",
"SUPER + T",
"SUPER + X",
"SUPER + CTRL + S",
"SUPER + G",
"SUPER + ALT + S",
"SUPER + F",
"SUPER + ALT + F",
"SUPER + CTRL + F",
"SUPER + CTRL + A",
"SUPER + CTRL + B",
"SUPER + CTRL + W",
"SUPER + CTRL + T",
"ALT + TAB",
"SUPER + mouse_down",
"SUPER + mouse_up",
"SUPER + SLASH",
"SUPER + code:61",
"SUPER + ALT + code:61",
}
local function unbind_default_keys()
for _, key in ipairs(keys_to_unbind) do
unbind_chord(key)
end
end
M.window_api = window_api
M.workspace_api = workspace_api
M.group_api = group_api
M.exec_cmd = exec_cmd
M.raw_dispatch_cmd = raw_dispatch_cmd
M.dispatch = dispatch
M.bind = bind
M.bindm = bindm
M.unbind_default_keys = unbind_default_keys
return M
|