#387 handle resizing, improved reconnect handling, fixed disconnect detection bug

This commit is contained in:
Mikayla Fischler 2024-02-21 20:33:07 -05:00
parent 372fd426d8
commit f152c37ea9
4 changed files with 221 additions and 81 deletions

View File

@ -242,43 +242,43 @@ function renderer.ui_ready() return engine.ui_ready end
function renderer.handle_disconnect(device) function renderer.handle_disconnect(device)
local is_used = false local is_used = false
if engine.monitors ~= nil then if not engine.monitors then return false end
if engine.monitors.primary == device then
if engine.ui.main_display ~= nil then
-- delete element tree and clear root UI elements
engine.ui.main_display.delete()
end
is_used = true if engine.monitors.primary == device then
engine.monitors.primary = nil if engine.ui.main_display ~= nil then
engine.ui.main_display = nil -- delete element tree and clear root UI elements
engine.ui.main_display.delete()
end
iocontrol.fp_monitor_state("main", false) is_used = true
elseif engine.monitors.flow == device then engine.monitors.primary = nil
if engine.ui.flow_display ~= nil then engine.ui.main_display = nil
-- delete element tree and clear root UI elements
engine.ui.flow_display.delete()
end
is_used = true iocontrol.fp_monitor_state("main", false)
engine.monitors.flow = nil elseif engine.monitors.flow == device then
engine.ui.flow_display = nil if engine.ui.flow_display ~= nil then
-- delete element tree and clear root UI elements
engine.ui.flow_display.delete()
end
iocontrol.fp_monitor_state("flow", false) is_used = true
else engine.monitors.flow = nil
for idx, monitor in pairs(engine.monitors.unit_displays) do engine.ui.flow_display = nil
if monitor == device then
if engine.ui.unit_displays[idx] ~= nil then
engine.ui.unit_displays[idx].delete()
end
is_used = true iocontrol.fp_monitor_state("flow", false)
engine.monitors.unit_displays[idx] = nil else
engine.ui.unit_displays[idx] = nil for idx, monitor in pairs(engine.monitors.unit_displays) do
if monitor == device then
iocontrol.fp_monitor_state(idx, false) if engine.ui.unit_displays[idx] ~= nil then
break engine.ui.unit_displays[idx].delete()
end end
is_used = true
engine.monitors.unit_displays[idx] = nil
engine.ui.unit_displays[idx] = nil
iocontrol.fp_monitor_state(idx, false)
break
end end
end end
end end
@ -293,52 +293,32 @@ end
function renderer.handle_reconnect(name, device) function renderer.handle_reconnect(name, device)
local is_used = false local is_used = false
if engine.monitors ~= nil then if not engine.monitors then return false end
if engine.monitors.primary_name == name then
is_used = true
_init_display(device)
engine.monitors.primary = device
local disp_x, disp_y = engine.monitors.primary.getSize() -- note: handle_resize is a more adaptive way of re-initializing a connected monitor
engine.dmesg_window.reposition(1, 1, disp_x, disp_y, engine.monitors.primary) -- since it can handle a monitor being reconnected that isn't the right size
if engine.ui_ready and (engine.ui.main_display == nil) then if engine.monitors.primary_name == name then
engine.dmesg_window.setVisible(false) is_used = true
engine.monitors.primary = device
engine.ui.main_display = DisplayBox{window=device,fg_bg=style.root} local disp_x, disp_y = engine.monitors.primary.getSize()
main_view(engine.ui.main_display) engine.dmesg_window.reposition(1, 1, disp_x, disp_y, engine.monitors.primary)
else
engine.dmesg_window.setVisible(true)
engine.dmesg_window.redraw()
end
iocontrol.fp_monitor_state("main", true) renderer.handle_resize(name)
elseif engine.monitors.flow_name == name then elseif engine.monitors.flow_name == name then
is_used = true is_used = true
_init_display(device) engine.monitors.flow = device
engine.monitors.flow = device
if engine.ui_ready and (engine.ui.flow_display == nil) then renderer.handle_resize(name)
engine.ui.flow_display = DisplayBox{window=device,fg_bg=style.root} else
flow_view(engine.ui.flow_display) for idx, monitor in ipairs(engine.monitors.unit_name_map) do
end if monitor == name then
is_used = true
engine.monitors.unit_displays[idx] = device
iocontrol.fp_monitor_state("flow", true) renderer.handle_resize(name)
else break
for idx, monitor in ipairs(engine.monitors.unit_name_map) do
if monitor == name then
is_used = true
_init_display(device)
engine.monitors.unit_displays[idx] = device
if engine.ui_ready and (engine.ui.unit_displays[idx] == nil) then
engine.ui.unit_displays[idx] = DisplayBox{window=device,fg_bg=style.root}
unit_view(engine.ui.unit_displays[idx], idx)
end
iocontrol.fp_monitor_state(idx, true)
break
end
end end
end end
end end
@ -346,6 +326,151 @@ function renderer.handle_reconnect(name, device)
return is_used return is_used
end end
-- handle a monitor being resized<br>
-- returns if this monitor is assigned + if the assigned screen still fits
---@param name string monitor name
---@return boolean is_used, boolean is_ok
function renderer.handle_resize(name)
local is_used = false
local is_ok = true
local ui = engine.ui
if not engine.monitors then return false, false end
if engine.monitors.primary_name == name and engine.monitors.primary then
local device = engine.monitors.primary ---@type table
-- this is necessary if the bottom left block was broken and on reconnect
_init_display(device)
is_used = true
-- resize dmesg window if needed, but don't make it thinner
local disp_w, disp_h = engine.monitors.primary.getSize()
local dmsg_w, dmsg_h = engine.dmesg_window.getSize()
if disp_h ~= dmsg_h then
engine.dmesg_window = window.reposition(1, 1, math.max(disp_w, dmsg_w), disp_h, engine.monitors.primary)
end
if ui.main_display then
ui.main_display.delete()
ui.main_display = nil
end
iocontrol.fp_monitor_state("main", true)
engine.dmesg_window.setVisible(not engine.ui_ready)
if engine.ui_ready then
local ok = pcall(function ()
ui.main_display = DisplayBox{window=device,fg_bg=style.root}
main_view(ui.main_display)
end)
if not ok then
if ui.main_display then
ui.main_display.delete()
ui.main_display = nil
end
device.setCursorPos(1, 1)
device.setBackgroundColor(colors.black)
device.setTextColor(colors.red)
device.clear()
device.write("monitor too small")
iocontrol.fp_monitor_state("main", false)
is_ok = false
end
else engine.dmesg_window.redraw() end
elseif engine.monitors.flow_name == name and engine.monitors.flow then
local device = engine.monitors.flow ---@type table
-- this is necessary if the bottom left block was broken and on reconnect
_init_display(device)
is_used = true
if ui.flow_display then
ui.flow_display.delete()
ui.flow_display = nil
end
iocontrol.fp_monitor_state("flow", true)
if engine.ui_ready then
engine.dmesg_window.setVisible(false)
local ok = pcall(function ()
ui.flow_display = DisplayBox{window=device,fg_bg=style.root}
flow_view(ui.flow_display)
end)
if not ok then
if ui.flow_display then
ui.flow_display.delete()
ui.flow_display = nil
end
device.setCursorPos(1, 1)
device.setBackgroundColor(colors.black)
device.setTextColor(colors.red)
device.clear()
device.write("monitor too small")
iocontrol.fp_monitor_state("flow", false)
is_ok = false
end
end
else
for idx, monitor in ipairs(engine.monitors.unit_name_map) do
local device = engine.monitors.unit_displays[idx]
if monitor == name and device then
-- this is necessary if the bottom left block was broken and on reconnect
_init_display(device)
is_used = true
if ui.unit_displays[idx] then
ui.unit_displays[idx].delete()
ui.unit_displays[idx] = nil
end
iocontrol.fp_monitor_state(idx, true)
if engine.ui_ready then
engine.dmesg_window.setVisible(false)
local ok = pcall(function ()
ui.unit_displays[idx] = DisplayBox{window=device,fg_bg=style.root}
unit_view(ui.unit_displays[idx], idx)
end)
if not ok then
if ui.unit_displays[idx] then
ui.unit_displays[idx].delete()
ui.unit_displays[idx] = nil
end
device.setCursorPos(1, 1)
device.setBackgroundColor(colors.black)
device.setTextColor(colors.red)
device.clear()
device.write("monitor too small")
iocontrol.fp_monitor_state(idx, false)
is_ok = false
end
end
break
end
end
end
return is_used, is_ok
end
-- handle a touch event -- handle a touch event
---@param event mouse_interaction|nil ---@param event mouse_interaction|nil
@ -355,15 +480,14 @@ function renderer.handle_mouse(event)
engine.ui.front_panel.handle_mouse(event) engine.ui.front_panel.handle_mouse(event)
elseif engine.ui_ready then elseif engine.ui_ready then
if event.monitor == engine.monitors.primary_name then if event.monitor == engine.monitors.primary_name then
engine.ui.main_display.handle_mouse(event) if engine.ui.main_display then engine.ui.main_display.handle_mouse(event) end
elseif event.monitor == engine.monitors.flow_name then elseif event.monitor == engine.monitors.flow_name then
engine.ui.flow_display.handle_mouse(event) if engine.ui.flow_display then engine.ui.flow_display.handle_mouse(event) end
else else
for id, monitor in ipairs(engine.monitors.unit_name_map) do for id, monitor in ipairs(engine.monitors.unit_name_map) do
if event.monitor == monitor then local display = engine.ui.unit_displays[id]
local layout = engine.ui.unit_displays[id] ---@type graphics_element if event.monitor == monitor and display then
layout.handle_mouse(event) if display then display.handle_mouse(event) end
break
end end
end end
end end

View File

@ -22,7 +22,7 @@ local sounder = require("coordinator.sounder")
local apisessions = require("coordinator.session.apisessions") local apisessions = require("coordinator.session.apisessions")
local COORDINATOR_VERSION = "v1.2.2" local COORDINATOR_VERSION = "v1.2.3"
local println = util.println local println = util.println
local println_ts = util.println_ts local println_ts = util.println_ts
@ -79,8 +79,8 @@ local function main()
-- system startup -- system startup
---------------------------------------- ----------------------------------------
-- re-mount devices now that logging is ready -- log mounts now since mounting was done before logging was ready
ppm.mount_all() ppm.log_mounts()
-- report versions/init fp PSIL -- report versions/init fp PSIL
iocontrol.init_fp(COORDINATOR_VERSION, comms.version) iocontrol.init_fp(COORDINATOR_VERSION, comms.version)
@ -269,6 +269,11 @@ local function main()
iocontrol.fp_has_speaker(true) iocontrol.fp_has_speaker(true)
end end
end end
elseif event == "monitor_resize" then
local is_used, is_ok = renderer.handle_resize(param1)
if is_used then
log_sys(util.c("configured monitor ", param1, " resized, ", util.trinary(is_ok, "display still fits", "display no longer fits")))
end
elseif event == "timer" then elseif event == "timer" then
if loop_clock.is_clock(param1) then if loop_clock.is_clock(param1) then
-- main loop tick -- main loop tick

View File

@ -300,6 +300,17 @@ function ppm.handle_unmount(iface)
return pm_type, pm_dev return pm_type, pm_dev
end end
-- log all mounts, to be used if `ppm.mount_all` is called before logging is ready
function ppm.log_mounts()
for iface, mount in pairs(ppm_sys.mounts) do
log.info(util.c("PPM: had found a ", mount.type, " (", iface, ")"))
end
if #ppm_sys.mounts == 0 then
log.warning("PPM: no devices had been found")
end
end
-- GENERAL ACCESSORS -- -- GENERAL ACCESSORS --
-- list all available peripherals -- list all available peripherals

View File

@ -22,7 +22,7 @@ local t_pack = table.pack
local util = {} local util = {}
-- scada-common version -- scada-common version
util.version = "1.1.14" util.version = "1.1.15"
util.TICK_TIME_S = 0.05 util.TICK_TIME_S = 0.05
util.TICK_TIME_MS = 50 util.TICK_TIME_MS = 50