opus/sys/apps/vnc.lua

139 lines
2.6 KiB
Lua
Raw Normal View History

local Event = require('opus.event')
local Socket = require('opus.socket')
local Terminal = require('opus.terminal')
local Util = require('opus.util')
2016-12-11 19:24:52 +00:00
2017-10-08 21:45:01 +00:00
local colors = _G.colors
local multishell = _ENV.multishell
2018-11-10 18:00:22 +00:00
local os = _G.os
local shell = _ENV.shell
2017-10-08 21:45:01 +00:00
local term = _G.term
2016-12-11 19:24:52 +00:00
local remoteId
2019-06-30 23:53:26 +00:00
local args, options = Util.parse(...)
2016-12-11 19:24:52 +00:00
if #args == 1 then
2018-01-24 22:39:38 +00:00
remoteId = tonumber(args[1])
2016-12-11 19:24:52 +00:00
else
2018-01-24 22:39:38 +00:00
print('Enter host ID')
remoteId = tonumber(_G.read())
2016-12-11 19:24:52 +00:00
end
if not remoteId then
2018-01-24 22:39:38 +00:00
error('Syntax: vnc <host ID>')
2016-12-11 19:24:52 +00:00
end
2018-01-20 12:18:13 +00:00
if multishell then
2019-06-30 23:53:26 +00:00
multishell.setTitle(multishell.getCurrent(),
(options.s and 'SVNC-' or 'VNC-') .. remoteId)
2018-01-20 12:18:13 +00:00
end
2016-12-11 19:24:52 +00:00
2018-11-10 18:00:22 +00:00
local function connect()
2019-06-30 23:53:26 +00:00
local socket, msg, reason = Socket.connect(remoteId, options.s and 5901 or 5900)
if reason == 'NOTRUST' then
local s, m = shell.run('trust ' .. remoteId)
if not s then
return s, m
end
socket, msg = Socket.connect(remoteId, 5900)
end
2016-12-11 19:24:52 +00:00
2018-11-10 18:00:22 +00:00
if not socket then
return false, msg
end
2016-12-11 19:24:52 +00:00
2018-11-10 18:00:22 +00:00
local function writeTermInfo()
local w, h = term.getSize()
socket:write({
type = 'termInfo',
width = w,
height = h,
isColor = term.isColor(),
})
end
2017-10-05 17:07:48 +00:00
2018-11-10 18:00:22 +00:00
writeTermInfo()
2016-12-11 19:24:52 +00:00
2018-11-10 18:00:22 +00:00
local ct = Util.shallowCopy(term.current())
2016-12-11 19:24:52 +00:00
2018-11-10 18:00:22 +00:00
if not ct.isColor() then
Terminal.toGrayscale(ct)
end
ct.clear()
ct.setCursorPos(1, 1)
Event.addRoutine(function()
while true do
local data = socket:read()
if not data then
break
end
for _,v in ipairs(data) do
ct[v.f](table.unpack(v.args))
2018-11-10 18:00:22 +00:00
end
end
end)
local filter = Util.transpose({
'char', 'paste', 'key', 'key_up',
'mouse_scroll', 'mouse_click', 'mouse_drag', 'mouse_up',
})
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
while true do
2018-11-10 18:00:22 +00:00
local e = Event.pullEvent()
local event = e[1]
if not socket.connected then
2018-01-24 22:39:38 +00:00
break
end
2018-11-10 18:00:22 +00:00
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)
return true
2018-01-24 22:39:38 +00:00
end
end
2018-11-10 18:00:22 +00:00
return false, "Connection Lost"
end
2017-05-26 01:06:17 +00:00
2016-12-11 19:24:52 +00:00
while true do
2018-11-10 18:00:22 +00:00
term.clear()
term.setCursorPos(1, 1)
print('connecting...')
local s, m = connect()
if s then
2018-01-24 22:39:38 +00:00
break
end
2018-11-10 18:00:22 +00:00
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1, 1)
print(m)
print('\nPress any key to exit')
print('\nRetrying in ... ')
local x, y = term.getCursorPos()
for i = 5, 1, -1 do
local timerId = os.startTimer(1)
term.setCursorPos(x, y)
term.write(i)
repeat
local e, id = os.pullEvent()
if e == 'char' or e == 'key' then
return
end
until e == 'timer' and id == timerId
2018-01-24 22:39:38 +00:00
end
2017-05-26 01:06:17 +00:00
end