render: add cross-platform desktop companion display

This commit is contained in:
AverageConsumer
2026-08-16 15:54:04 +02:00
parent b72d1d34b5
commit 99d9908017
10 changed files with 490 additions and 8 deletions
+56
View File
@@ -0,0 +1,56 @@
package.path = "./?.lua;./?/init.lua;" .. package.path
local clock, quit = 1, false
local sent, queue, draws = {}, {}, 0
local peer = {
send = function(_, data) sent[#sent + 1] = data end,
disconnect_now = function() end,
}
local host = {
connect = function() return peer end,
service = function()
if #queue == 0 then return nil end
return table.remove(queue, 1)
end,
}
local rendered = {
setFilter = function() end, replacePixels = function() end,
release = function() end,
}
love = {
timer = { getTime = function() return clock end },
data = { decompress = function(_, _, value) return value end },
image = { newImageData = function(w, h, format, raw)
assert(w == 1 and h == 1 and format == "rgba8" and raw == "rgba")
return {}
end },
graphics = {
newImage = function() return rendered end,
getDimensions = function() return 100, 100 end,
clear = function() end, setColor = function() end,
draw = function() draws = draws + 1 end,
},
event = { quit = function() quit = true end },
}
package.preload.enet = function()
return { host_create = function() return host end }
end
require("src.render.DesktopCompanion").install({ port = 50000, token = "token" })
queue[#queue + 1] = { type = "connect" }
love.update()
assert(sent[#sent] == "Htoken", "companion authenticates after connecting")
queue[#queue + 1] = {
type = "receive", data = "Ftoken\n1,1,0,auto\nrgba",
}
love.update()
love.draw()
assert(draws == 1, "companion draws a received frame")
love.mousepressed(50, 50, 1)
love.mousereleased(50, 50, 1)
assert(sent[#sent - 1] == "Itoken\ndown,0,0"
and sent[#sent] == "Itoken\nup,0,0", "mouse input maps back to source pixels")
queue[#queue + 1] = { type = "receive", data = "Qtoken" }
love.update()
assert(quit, "parent can close the companion")
print("desktop companion: ok")
+57
View File
@@ -0,0 +1,57 @@
package.path = "./?.lua;./?/init.lua;" .. package.path
local clock = 1
love = {
timer = { getTime = function() return clock end },
data = {
hash = function() return "digest" end,
encode = function() return "0123456789abcdef0123456789abcdef" end,
compress = function(_, _, value) return value end,
},
}
local sent, spawned, queue = {}, nil, {}
local peer = {
send = function(_, data, channel, flag)
sent[#sent + 1] = { data = data, channel = channel, flag = flag }
end,
disconnect_now = function() end,
}
local host = {
service = function()
if #queue == 0 then return nil end
return table.remove(queue, 1)
end,
destroy = function() end,
}
package.loaded["src.core.Platform"] = { canSpawnProcess = function() return true end }
package.loaded["src.core.HostShell"] = {
spawnSelfDetached = function(args) spawned = args return true end,
}
package.preload.enet = function()
return { host_create = function() return host end }
end
local Screen = require("src.render.SecondScreen")
assert(Screen.usable(), "the shared facade selects the desktop backend")
Screen.setEnabled(true)
assert(spawned and spawned[1]:match("^%-%-display%-companion=%d+,[%w]+$"),
"enabling launches one companion of this app")
local token = spawned[1]:match(",([%w]+)$")
queue[#queue + 1] = { type = "receive", peer = peer, data = "H" .. token }
assert(Screen.detected(), "a token-authenticated companion becomes detected")
local pixels = { getString = function() return "rgba" end }
assert(Screen.push(pixels, 1, 1, 0x102030, "auto"),
"a connected companion accepts a frame")
assert(sent[#sent].data:find("^F" .. token .. "\n1,1,1056816,auto\nrgba"),
"frame metadata and pixels stay in one loopback packet")
queue[#queue + 1] = {
type = "receive", peer = peer, data = "I" .. token .. "\ndown,3,4",
}
assert(Screen.pollTouch() == "down,3,4", "companion input returns to the mod")
Screen.setEnabled(false)
assert(sent[#sent].data == "Q" .. token, "disabling closes the companion")
print("desktop second screen: ok")
+32
View File
@@ -0,0 +1,32 @@
package.path = "./?.lua;./?/init.lua;" .. package.path
local osName, fused, command = "Windows", true, nil
love = {
system = { getOS = function() return osName end },
filesystem = {
getExecutablePath = function() return "C:\\Game\\gen1recomp.exe" end,
getSource = function() return "C:\\Source\\gen1recomp" end,
isFused = function() return fused end,
},
}
package.loaded["src.core.Platform"] = { canSpawnProcess = function() return true end }
local execute = os.execute
os.execute = function(value) command = value return 0 end
local HostShell = require("src.core.HostShell")
assert(HostShell.spawnSelfDetached({ "--display-companion=50000,token" }))
assert(command:find('start "" /b ', 1, true)
and command:find('"C:\\Game\\gen1recomp.exe"', 1, true),
"Windows launches the fused app detached")
osName, fused = "Linux", false
assert(HostShell.spawnSelfDetached({ "--display-companion=50000,token" }))
assert(command:find("'C:\\Source\\gen1recomp'", 1, true)
and command:sub(-1) == "&", "Linux source runs include the game folder")
osName, fused = "OS X", true
assert(HostShell.spawnSelfDetached({ "--display-companion=50000,token" }))
assert(not command:find("start", 1, true) and command:sub(-1) == "&",
"macOS uses the same detached POSIX path")
os.execute = execute
print("spawn self detached: ok")