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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
local BUTTONS = { left = 1, right = 2, middle = 3, side1 = 4, side2 = 5 }
local KEYS = {
enter = 40,
esc = 41,
back = 42,
tab = 43,
space = 44,
["-"] = 45,
["."] = 55,
caps = 57,
print = 70,
scroll = 71,
pause = 72,
insert = 73,
home = 74,
pgup = 75,
del = 76,
["end"] = 77,
pgdown = 78,
right = 79,
left = 80,
down = 81,
up = 82,
aplcat = 101,
["ctr-l"] = 224,
["shift-l"] = 225,
["alt-l"] = 226,
["gui-l"] = 227,
["ctr-r"] = 228,
["shift-r"] = 229,
["alt-r"] = 230,
["gui-r"] = 231,
}
for i = 0, 25 do
KEYS[string.char(97 + i)] = 4 + i
end
for i = 1, 12 do
KEYS[`f{i}`] = 57 + i
end
for i = 13, 20 do
KEYS[`f{i}`] = 91 + i
end
local DHZ_NAMES = {}
for _, name in
{
"move",
"left",
"right",
"middle",
"side1",
"side2",
"wheel",
"mouse",
"keydown",
"keyup",
"isdown_left",
"isdown_right",
"isdown_middle",
"isdown_side1",
"isdown_side2",
"monitor",
"mask_left",
"mask_right",
"mask_middle",
"mask_wheel",
"mask_side1",
"mask_side2",
"mask_x",
"mask_y",
"mask_all",
"mask_keyboard",
"dismask_keyboard",
"dismask_keyboard_all",
}
do
DHZ_NAMES[name] = true
end
local function usage_of(arg)
if arg == nil then
return nil
end
if arg:match("^%d+$") then
return tonumber(arg)
end
return KEYS[arg:lower()]
end
local function key_is_down(usage)
for _, name in Input.GetActiveKeys() do
if HID.Usage(name) == usage then
return true
end
end
return false
end
local function bit(down)
return if down then "1" else "0"
end
local function balanced(s)
local depth = 0
for c in s:gmatch("[%(%)%[%]]") do
depth += if c == "(" or c == "[" then 1 else -1
if depth < 0 then
return false
end
end
return depth == 0
end
local function split_args(s)
local args = {}
for a in s:gmatch("[^,]+") do
table.insert(args, a)
end
return args
end
local function wheel(w)
if math.abs(w) >= 120 then
w = math.sign(w) * (math.abs(w) // 120)
end
if w ~= 0 then
HID.Scroll(-w)
end
end
local function new(api)
local echo = true
local shift = nil
local monitor = nil
local last_report = nil
local function tap(down, up)
down()
Run(function()
Sleep(Math.Random(75, 125))
up()
end)
end
local function tap_key(usage)
if usage then
tap(function()
api.key(usage, true)
end, function()
api.key(usage, false)
end)
end
end
local function ferrum(name, args)
local button = BUTTONS[name:match("^km%.(%w+)$") or ""]
if button then
if args[1] == nil then
return bit(Input.IsDown(`Mouse{button}`))
end
api.button(button, tonumber(args[1]) == 1)
elseif name == "km.move" or name == "m" then
local x, y = tonumber(args[1]), tonumber(args[2])
if x and y and (x ~= 0 or y ~= 0) then
HID.Move(x, y)
end
elseif name == "km.click" then
local n = tonumber(args[1])
if n and n >= 0 and n <= 4 then
tap(function()
api.button(n + 1, true)
end, function()
api.button(n + 1, false)
end)
end
elseif name == "km.wheel" then
local n = tonumber(args[1])
if n and n ~= 0 then
HID.Scroll(math.clamp(n, -128, 127))
end
elseif
name == "km.down"
or name == "km.up"
or name == "km.multidown"
or name == "km.multiup"
then
local down = name:sub(-4) == "down"
for _, a in args do
local usage = usage_of(a)
if usage then
api.key(usage, down)
end
end
elseif name == "km.press" or name == "km.multipress" then
for _, a in args do
tap_key(usage_of(a))
end
elseif name == "km.isdown" then
local usage = usage_of(args[1])
return bit(usage ~= nil and key_is_down(usage))
elseif name == "km.version" then
return "kmbox: Ferrum"
elseif name == "km.echo" then
if args[1] == nil then
return bit(echo)
end
echo = tonumber(args[1]) ~= 0
elseif name == "km.has_mouse" or name == "km.has_keyboard" then
return "1"
elseif name == "km.has_controller" then
return "0"
elseif name == "device.VID" and args[1] == nil then
return "VID=046d"
elseif name == "device.PID" and args[1] == nil then
return "PID=c53f"
end
return nil
end
local function ferrum_line(raw)
local line = raw:gsub("[^%w%+%-_%.,%(%)%[%];=]", "")
local calls = {}
for part in line:gmatch("[^;]+") do
local name, inner = part:match("^([%w_%.%+%-]+)%((.*)%)$")
if not name or not balanced(inner) then
return nil
end
table.insert(calls, { name, split_args(inner) })
end
if #calls == 0 then
return nil
end
local result = nil
for _, call in calls do
result = ferrum(call[1], call[2]) or result
end
local out = if result then result .. "\r\n" else ""
if echo then
out = raw:match("^%s*(.-)%s*$") .. "\r\n" .. out .. ">>> "
end
return out
end
local function unshift(s, k)
return (
s:gsub("%a", function(c)
local base = if c:match("%l") then 97 else 65
return string.char((c:byte() - base - k) % 26 + base)
end)
)
end
local function dhz_parse(data, k)
local plain = unshift(data, k)
local name, inner = plain:match("^%s*([%w_]+)%((.-)%)%s*$")
if name and DHZ_NAMES[name] then
return name, split_args(inner)
end
return nil
end
local function dhz(data, ip)
local name, args = nil, nil
if shift then
name, args = dhz_parse(data, shift)
end
if not name then
for k = 0, 25 do
name, args = dhz_parse(data, k)
if name then
shift = k
break
end
end
end
if not name then
return data
end
local n = tonumber(args[1])
local button = BUTTONS[name]
if button then
api.button(button, n == 1)
elseif name == "move" then
local y = tonumber(args[2])
if n and y then
HID.Move(n, y)
end
elseif name == "wheel" then
if n then
wheel(n)
end
elseif name == "mouse" then
local x, y, w = tonumber(args[2]), tonumber(args[3]), tonumber(args[4])
if n and x and y and w then
for b = 1, 5 do
api.button(b, bit32.btest(n, bit32.lshift(1, b - 1)))
end
if x ~= 0 or y ~= 0 then
HID.Move(x, y)
end
wheel(w)
end
elseif name == "keydown" or name == "keyup" then
local usage = usage_of(args[1])
if usage then
api.key(usage, name == "keydown")
end
elseif name:match("^isdown_") then
return bit(Input.IsDown(`Mouse{BUTTONS[name:sub(8)]}`))
elseif name == "monitor" then
monitor = if n and n > 0 then { ip = ip, port = n } else nil
last_report = nil
end
return data
end
local t = {}
function t.handle(data, ip)
if
not (
data:match("^%s*km%.")
or data:match("^%s*device%.")
or data:match("^%s*m%(")
)
then
return dhz(data, ip)
end
local out = {}
for line in data:gmatch("[^\r\n]+") do
local reply = ferrum_line(line)
if reply and reply ~= "" then
table.insert(out, reply)
end
end
return if #out > 0 then table.concat(out) else nil
end
function t.tick()
if not monitor then
return
end
local report = table.concat({
bit(Input.IsDown("Mouse1")),
bit(Input.IsDown("Mouse3")),
bit(Input.IsDown("Mouse2")),
bit(Input.IsDown("Mouse4")),
bit(Input.IsDown("Mouse5")),
}, "|")
if report ~= last_report then
last_report = report
api.send(monitor.ip, monitor.port, report)
end
end
function t.reset()
echo, shift, monitor, last_report = true, nil, nil, nil
end
return t
end
return { new = new }