From b3a061d39b707cf40b61ca6609a8b9ff0ee66301 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Sat, 10 Nov 2018 13:00:22 -0500 Subject: [PATCH] vnc auto-reconnect --- sys/apps/System.lua | 100 ++++++++++++++--------------- sys/apps/vnc.lua | 149 ++++++++++++++++++++++++++------------------ 2 files changed, 141 insertions(+), 108 deletions(-) diff --git a/sys/apps/System.lua b/sys/apps/System.lua index 10e4816..ce2a2b1 100644 --- a/sys/apps/System.lua +++ b/sys/apps/System.lua @@ -141,61 +141,63 @@ local systemPage = UI.Page { } 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 = { } - Config.load('gps', values.home and { values.home } or { }) - - systemPage.tabs:add({ - gpsTab = UI.Window { - tabTitle = 'GPS', - labelText = UI.Text { - x = 3, y = 2, - value = 'On restart, return to this location' - }, - grid = UI.Grid { - x = 3, ex = -3, y = 4, - height = 2, - values = values, - inactive = true, - columns = { - { heading = 'x', key = 'x' }, - { heading = 'y', key = 'y' }, - { heading = 'z', key = 'z' }, + systemPage.tabs:add({ + gpsTab = UI.Window { + tabTitle = 'GPS', + labelText = UI.Text { + x = 3, y = 2, + value = 'On restart, return to this location' + }, + grid = UI.Grid { + x = 3, ex = -3, y = 4, + height = 2, + values = values, + inactive = true, + columns = { + { 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, - text = 'Set home', - event = 'gps_set', - }, - button2 = UI.Button { - ex = -3, y = 7, width = 7, - text = 'Clear', - event = 'gps_clear', - }, - }, - }) - function systemPage.tabs.gpsTab:eventHandler(event) - if event.type == 'gps_set' then - systemPage.notification:info('Determining location', 10) - systemPage:sync() - if Home.set() then - Config.load('gps', values) - self.grid:setValues(values.home and { values.home } or { }) + }) + function systemPage.tabs.gpsTab:eventHandler(event) + if event.type == 'gps_set' then + systemPage.notification:info('Determining location', 10) + systemPage:sync() + if Home.set() then + Config.load('gps', values) + self.grid:setValues(values.home and { values.home } or { }) + self.grid:draw() + systemPage.notification:success('Location set') + else + systemPage.notification:error('Unable to determine location') + end + return true + elseif event.type == 'gps_clear' then + fs.delete('usr/config/gps') + self.grid:setValues({ }) self.grid:draw() - systemPage.notification:success('Location set') - else - systemPage.notification:error('Unable to determine location') + return true 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 if settings then diff --git a/sys/apps/vnc.lua b/sys/apps/vnc.lua index 6ea3852..ca8b670 100644 --- a/sys/apps/vnc.lua +++ b/sys/apps/vnc.lua @@ -1,12 +1,13 @@ _G.requireInjector(_ENV) -local Event = require('event') -local Socket = require('socket') -local Terminal = require('terminal') -local Util = require('util') +local Event = require('event') +local Socket = require('socket') +local Terminal = require('terminal') +local Util = require('util') local colors = _G.colors local multishell = _ENV.multishell +local os = _G.os local term = _G.term local remoteId @@ -26,75 +27,105 @@ if multishell then multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId) end -print('connecting...') -local socket, msg = Socket.connect(remoteId, 5900) +local function connect() + local socket, msg = Socket.connect(remoteId, 5900) -if not socket then - error(msg) -end + if not socket then + return false, msg + end -local function writeTermInfo() - local w, h = term.getSize() - socket:write({ - type = 'termInfo', - width = w, - height = h, - isColor = term.isColor(), + local function writeTermInfo() + local w, h = term.getSize() + socket:write({ + type = 'termInfo', + width = w, + height = h, + 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 - local data = socket:read() - if not data then + local e = Event.pullEvent() + local event = e[1] + + if not socket.connected then break 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) - -ct.clear() -ct.setCursorPos(1, 1) - -local filter = Util.transpose({ - 'char', 'paste', 'key', 'key_up', - 'mouse_scroll', 'mouse_click', 'mouse_drag', 'mouse_up', -}) + return false, "Connection Lost" +end while true do - local e = Event.pullEvent() - local event = e[1] + term.clear() + term.setCursorPos(1, 1) - if not socket.connected then - print() - print('Connection lost') - print('Press enter to exit') - _G.read() + print('connecting...') + local s, m = connect() + if s then 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 + 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 end end