1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apps/network/vnc.lua

74 lines
1.6 KiB
Lua
Raw Normal View History

2017-08-03 05:46:39 +00:00
local Event = require('event')
local Socket = require('socket')
local Util = require('util')
2016-12-11 19:24:52 +00:00
2018-01-13 20:17:26 +00:00
local os = _G.os
2018-01-14 04:40:53 +00:00
local terminal = _G.device.terminal
2018-01-13 20:17:26 +00:00
2017-10-05 17:07:48 +00:00
local function vncHost(socket)
2018-01-24 22:39:38 +00:00
local methods = { 'blit', 'clear', 'clearLine', 'setCursorPos', 'write',
'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', }
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local oldTerm = Util.shallowCopy(terminal)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
for _,k in pairs(methods) do
terminal[k] = function(...)
if not socket.queue then
socket.queue = { }
Event.onTimeout(0, function()
socket:write(socket.queue)
socket.queue = nil
end)
end
table.insert(socket.queue, {
f = k,
args = { ... },
})
oldTerm[k](...)
end
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
while true do
local data = socket:read()
if not data then
print('vnc: closing connection to ' .. socket.dhost)
break
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if data.type == 'shellRemote' then
os.queueEvent(table.unpack(data.event))
elseif data.type == 'termInfo' then
terminal.getSize = function()
return data.width, data.height
end
os.queueEvent('term_resize')
end
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
for k,v in pairs(oldTerm) do
terminal[k] = v
end
os.queueEvent('term_resize')
2016-12-11 19:24:52 +00:00
end
2017-08-03 05:46:39 +00:00
Event.addRoutine(function()
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
print('vnc: listening on port 5900')
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
while true do
local socket = Socket.server(5900)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
print('vnc: connection from ' .. socket.dhost)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
-- no new process - only 1 connection allowed
-- due to term size issues
2019-04-20 17:48:13 +00:00
local s, m = pcall(vncHost, socket)
2018-01-24 22:39:38 +00:00
socket:close()
2019-04-20 17:48:13 +00:00
if not s and m then
print('vnc error')
_G.printError(m)
end
2018-01-24 22:39:38 +00:00
end
2017-08-03 05:46:39 +00:00
end)