1
0
mirror of https://github.com/kepler155c/opus synced 2025-02-14 07:50:01 +00:00
opus/sys/apps/vnc.lua

101 lines
1.7 KiB
Lua
Raw Normal View History

2018-01-24 17:39:38 -05:00
_G.requireInjector(_ENV)
local Event = require('event')
local Socket = require('socket')
2016-12-11 14:24:52 -05:00
local Terminal = require('terminal')
local Util = require('util')
2016-12-11 14:24:52 -05:00
2017-10-08 17:45:01 -04:00
local colors = _G.colors
local multishell = _ENV.multishell
local term = _G.term
2016-12-11 14:24:52 -05:00
local remoteId
local args = { ... }
if #args == 1 then
2018-01-24 17:39:38 -05:00
remoteId = tonumber(args[1])
2016-12-11 14:24:52 -05:00
else
2018-01-24 17:39:38 -05:00
print('Enter host ID')
remoteId = tonumber(_G.read())
2016-12-11 14:24:52 -05:00
end
if not remoteId then
2018-01-24 17:39:38 -05:00
error('Syntax: vnc <host ID>')
2016-12-11 14:24:52 -05:00
end
2018-01-20 07:18:13 -05:00
if multishell then
2018-01-24 17:39:38 -05:00
multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId)
2018-01-20 07:18:13 -05:00
end
2016-12-11 14:24:52 -05:00
print('connecting...')
2017-10-09 13:08:38 -04:00
local socket, msg = Socket.connect(remoteId, 5900)
2016-12-11 14:24:52 -05:00
if not socket then
2018-01-24 17:39:38 -05:00
error(msg)
2016-12-11 14:24:52 -05:00
end
2017-10-05 13:07:48 -04:00
local function writeTermInfo()
2018-01-24 17:39:38 -05:00
local w, h = term.getSize()
socket:write({
type = 'termInfo',
width = w,
height = h,
isColor = term.isColor(),
})
2017-10-05 13:07:48 -04:00
end
writeTermInfo()
2016-12-11 14:24:52 -05:00
local ct = Util.shallowCopy(term.current())
if not ct.isColor() then
2018-01-24 17:39:38 -05:00
Terminal.toGrayscale(ct)
2016-12-11 14:24:52 -05:00
end
2017-07-23 22:37:07 -04:00
Event.addRoutine(function()
2018-01-24 17:39:38 -05:00
while true do
local data = socket:read()
if not data then
break
end
for _,v in ipairs(data) do
ct[v.f](unpack(v.args))
end
end
2016-12-11 14:24:52 -05:00
end)
ct.clear()
ct.setCursorPos(1, 1)
2017-05-25 21:06:17 -04:00
local filter = Util.transpose({
2018-01-24 17:39:38 -05:00
'char', 'paste', 'key', 'key_up',
'mouse_scroll', 'mouse_click', 'mouse_drag', 'mouse_up',
2017-05-25 21:06:17 -04:00
})
2016-12-11 14:24:52 -05:00
while true do
2018-01-24 17:39:38 -05:00
local e = Event.pullEvent()
local event = e[1]
if not socket.connected then
print()
print('Connection lost')
print('Press enter to exit')
_G.read()
break
end
if filter[event] then
socket:write({
type = 'shellRemote',
event = e,
})
elseif event == 'term_resize' then
writeTermInfo()
elseif event == 'terminate' then
socket:close()
ct.setBackgroundColor(colors.black)
ct.clear()
ct.setCursorPos(1, 1)
break
end
2017-05-25 21:06:17 -04:00
end