From 11a090441efcbb4475840ab77eddaea9395d647e Mon Sep 17 00:00:00 2001 From: Tueem Date: Mon, 6 Jul 2026 16:43:49 +0200 Subject: [PATCH] fix(display): ensure display is connected and retry if not --- kiosk/server.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/kiosk/server.js b/kiosk/server.js index ea9b787..92a0c5a 100644 --- a/kiosk/server.js +++ b/kiosk/server.js @@ -28,7 +28,7 @@ function getMonitorGeometry(monitorName) { const stdout = execSync('xrandr').toString(); const lines = stdout.split('\n'); for (const line of lines) { - if (line.includes(monitorName) && line.includes('connected')) { + if (line.startsWith(monitorName + ' ') && line.includes(' connected')) { // Match geometry string like 1920x1080+1920+0 const match = line.match(/(\d+)x(\d+)\+(\d+)\+(\d+)/); if (match) { @@ -42,14 +42,33 @@ function getMonitorGeometry(monitorName) { } } } catch (e) { - console.error('Failed to parse xrandr:', e); + console.error('Failed to execute xrandr:', e.message); } - // Fallback geometry if xrandr fails - return { width: 1920, height: 1080, x: 0, y: 0 }; + return null; } -function buildFlags() { - const geometry = getMonitorGeometry(MONITOR); +function ensureMonitorConnected() { + return new Promise(resolve => { + const check = () => { + // Run xrandr --auto to ensure any newly connected displays are configured + try { + execSync('xrandr --auto'); + } catch(e) {} + + const geom = getMonitorGeometry(MONITOR); + if (geom) { + console.log(`Monitor ${MONITOR} is connected and ready.`); + resolve(geom); + } else { + console.log(`Waiting for monitor ${MONITOR} to be connected...`); + setTimeout(check, 5000); + } + }; + check(); + }); +} + +function buildFlags(geometry) { console.log(`Monitor ${MONITOR} resolved to position ${geometry.x},${geometry.y} with size ${geometry.width}x${geometry.height}`); const flags = [ @@ -79,13 +98,16 @@ function buildFlags() { } async function startChromium() { + console.log(`Checking connection status for ${MONITOR}...`); + const geometry = await ensureMonitorConnected(); + if (browserProcess) { console.log('Stopping existing Chromium...'); browserProcess.kill('SIGTERM'); await new Promise(resolve => setTimeout(resolve, 2000)); } - const flags = buildFlags(); + const flags = buildFlags(geometry); console.log('Starting Chromium with flags:', flags.join(' '));