1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-28 01:20:27 +00:00

vnc auto-reconnect

This commit is contained in:
kepler155c@gmail.com 2018-11-10 13:00:22 -05:00
parent 10a0c3a724
commit b3a061d39b
2 changed files with 141 additions and 108 deletions

View File

@ -141,61 +141,63 @@ local systemPage = UI.Page {
} }
if turtle then if turtle then
local Home = require('turtle.home') pcall(function()
local Home = require('turtle.home')
-- TODO: dont rely on turtle.home
local values = { }
Config.load('gps', values.home and { values.home } or { })
local values = { } systemPage.tabs:add({
Config.load('gps', values.home and { values.home } or { }) gpsTab = UI.Window {
tabTitle = 'GPS',
systemPage.tabs:add({ labelText = UI.Text {
gpsTab = UI.Window { x = 3, y = 2,
tabTitle = 'GPS', value = 'On restart, return to this location'
labelText = UI.Text { },
x = 3, y = 2, grid = UI.Grid {
value = 'On restart, return to this location' x = 3, ex = -3, y = 4,
}, height = 2,
grid = UI.Grid { values = values,
x = 3, ex = -3, y = 4, inactive = true,
height = 2, columns = {
values = values, { heading = 'x', key = 'x' },
inactive = true, { heading = 'y', key = 'y' },
columns = { { heading = 'z', key = 'z' },
{ heading = 'x', key = 'x' }, },
{ heading = 'y', key = 'y' }, },
{ heading = 'z', key = 'z' }, button1 = UI.Button {
x = 3, y = 7,
text = 'Set home',
event = 'gps_set',
},
button2 = UI.Button {
ex = -3, y = 7, width = 7,
text = 'Clear',
event = 'gps_clear',
}, },
}, },
button1 = UI.Button { })
x = 3, y = 7, function systemPage.tabs.gpsTab:eventHandler(event)
text = 'Set home', if event.type == 'gps_set' then
event = 'gps_set', systemPage.notification:info('Determining location', 10)
}, systemPage:sync()
button2 = UI.Button { if Home.set() then
ex = -3, y = 7, width = 7, Config.load('gps', values)
text = 'Clear', self.grid:setValues(values.home and { values.home } or { })
event = 'gps_clear', self.grid:draw()
}, systemPage.notification:success('Location set')
}, else
}) systemPage.notification:error('Unable to determine location')
function systemPage.tabs.gpsTab:eventHandler(event) end
if event.type == 'gps_set' then return true
systemPage.notification:info('Determining location', 10) elseif event.type == 'gps_clear' then
systemPage:sync() fs.delete('usr/config/gps')
if Home.set() then self.grid:setValues({ })
Config.load('gps', values)
self.grid:setValues(values.home and { values.home } or { })
self.grid:draw() self.grid:draw()
systemPage.notification:success('Location set') return true
else
systemPage.notification:error('Unable to determine location')
end end
return true
elseif event.type == 'gps_clear' then
fs.delete('usr/config/gps')
self.grid:setValues({ })
self.grid:draw()
return true
end end
end end)
end end
if settings then if settings then

View File

@ -1,12 +1,13 @@
_G.requireInjector(_ENV) _G.requireInjector(_ENV)
local Event = require('event') local Event = require('event')
local Socket = require('socket') local Socket = require('socket')
local Terminal = require('terminal') local Terminal = require('terminal')
local Util = require('util') local Util = require('util')
local colors = _G.colors local colors = _G.colors
local multishell = _ENV.multishell local multishell = _ENV.multishell
local os = _G.os
local term = _G.term local term = _G.term
local remoteId local remoteId
@ -26,75 +27,105 @@ if multishell then
multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId) multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId)
end end
print('connecting...') local function connect()
local socket, msg = Socket.connect(remoteId, 5900) local socket, msg = Socket.connect(remoteId, 5900)
if not socket then if not socket then
error(msg) return false, msg
end end
local function writeTermInfo() local function writeTermInfo()
local w, h = term.getSize() local w, h = term.getSize()
socket:write({ socket:write({
type = 'termInfo', type = 'termInfo',
width = w, width = w,
height = h, height = h,
isColor = term.isColor(), isColor = term.isColor(),
})
end
writeTermInfo()
local ct = Util.shallowCopy(term.current())
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
_debug('exiting routine')
break
end
for _,v in ipairs(data) do
ct[v.f](unpack(v.args))
end
end
end)
local filter = Util.transpose({
'char', 'paste', 'key', 'key_up',
'mouse_scroll', 'mouse_click', 'mouse_drag', 'mouse_up',
}) })
end
writeTermInfo()
local ct = Util.shallowCopy(term.current())
if not ct.isColor() then
Terminal.toGrayscale(ct)
end
Event.addRoutine(function()
while true do while true do
local data = socket:read() local e = Event.pullEvent()
if not data then local event = e[1]
if not socket.connected then
break break
end end
for _,v in ipairs(data) do
ct[v.f](unpack(v.args)) 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
end end
end end
end) return false, "Connection Lost"
end
ct.clear()
ct.setCursorPos(1, 1)
local filter = Util.transpose({
'char', 'paste', 'key', 'key_up',
'mouse_scroll', 'mouse_click', 'mouse_drag', 'mouse_up',
})
while true do while true do
local e = Event.pullEvent() term.clear()
local event = e[1] term.setCursorPos(1, 1)
if not socket.connected then print('connecting...')
print() local s, m = connect()
print('Connection lost') if s then
print('Press enter to exit')
_G.read()
break break
end end
if filter[event] then term.setBackgroundColor(colors.black)
socket:write({ term.setTextColor(colors.white)
type = 'shellRemote', term.clear()
event = e, term.setCursorPos(1, 1)
}) print(m)
elseif event == 'term_resize' then print('\nPress any key to exit')
writeTermInfo() print('\nRetrying in ... ')
elseif event == 'terminate' then local x, y = term.getCursorPos()
socket:close() for i = 5, 1, -1 do
ct.setBackgroundColor(colors.black) local timerId = os.startTimer(1)
ct.clear() term.setCursorPos(x, y)
ct.setCursorPos(1, 1) term.write(i)
break repeat
local e, id = os.pullEvent()
if e == 'char' or e == 'key' then
return
end
until e == 'timer' and id == timerId
end end
end end