|
@@ -8,18 +8,21 @@ import type {
|
|
|
EngineFlags,
|
|
EngineFlags,
|
|
|
ScreenAssignment,
|
|
ScreenAssignment,
|
|
|
ScreenTarget,
|
|
ScreenTarget,
|
|
|
|
|
+ SplitScreen,
|
|
|
WallpaperBackend,
|
|
WallpaperBackend,
|
|
|
WallpaperCompat,
|
|
WallpaperCompat,
|
|
|
WallpaperMeta
|
|
WallpaperMeta
|
|
|
} from '@shared/types'
|
|
} from '@shared/types'
|
|
|
import { getAllWallpapers, getWallpaper, updateWallpaper } from './library.service'
|
|
import { getAllWallpapers, getWallpaper, updateWallpaper } from './library.service'
|
|
|
-import { applyWallpaperMeta, isAnimatedWallpaper } from './wallpaper.service'
|
|
|
|
|
|
|
+import { applyWallpaperMeta, isAnimatedWallpaper, usesEngine } from './wallpaper.service'
|
|
|
import {
|
|
import {
|
|
|
ALL_SCREENS,
|
|
ALL_SCREENS,
|
|
|
getActiveScreens,
|
|
getActiveScreens,
|
|
|
getLweStatus,
|
|
getLweStatus,
|
|
|
getRunningInstances,
|
|
getRunningInstances,
|
|
|
|
|
+ getUnusedScreens,
|
|
|
hotswapLweSettings,
|
|
hotswapLweSettings,
|
|
|
|
|
+ isLweRunning,
|
|
|
launchLweAsync,
|
|
launchLweAsync,
|
|
|
liveEngineFlagOptions,
|
|
liveEngineFlagOptions,
|
|
|
onLweExit,
|
|
onLweExit,
|
|
@@ -29,23 +32,73 @@ import {
|
|
|
type LweSplitSlot
|
|
type LweSplitSlot
|
|
|
} from './lwe.service'
|
|
} from './lwe.service'
|
|
|
import { resolveEngineFlags } from '@shared/engineFlags'
|
|
import { resolveEngineFlags } from '@shared/engineFlags'
|
|
|
-import { clearAssignment, getAssignments, setAssignment } from './wallpaper-state.service'
|
|
|
|
|
-import { getDefaultFps, getDisplayMode, getGlobalEngineFlags, getSplitScreens, setSplitScreen } from './config.service'
|
|
|
|
|
-import { planRestore, restoreSuperseded } from './restore-plan'
|
|
|
|
|
|
|
+import {
|
|
|
|
|
+ clearAssignment,
|
|
|
|
|
+ getAssignments,
|
|
|
|
|
+ isSplitActive,
|
|
|
|
|
+ markSplitActive,
|
|
|
|
|
+ setAssignment,
|
|
|
|
|
+ splitActiveScreens
|
|
|
|
|
+} from './wallpaper-state.service'
|
|
|
|
|
+import { getDefaultFps, getDisplayMode, getGlobalEngineFlags, getSplitScreens } from './config.service'
|
|
|
|
|
+import { planRestore, restoreSuperseded, trySplit, type RestoreInput } from './restore-plan'
|
|
|
import { ensureDependencyInstalled } from './dependency.service'
|
|
import { ensureDependencyInstalled } from './dependency.service'
|
|
|
import { t } from '../i18n'
|
|
import { t } from '../i18n'
|
|
|
|
|
|
|
|
const LAUNCH_CHECK_MS = 20_000
|
|
const LAUNCH_CHECK_MS = 20_000
|
|
|
-// bumped when a screen is launched or stopped, so a later restore does not replace that
|
|
|
|
|
|
|
+// bumped by every launch and stop of a screen, so a restore can tell it was overtaken
|
|
|
const screenUse = new Map<string, number>()
|
|
const screenUse = new Map<string, number>()
|
|
|
|
|
|
|
|
-function markScreenUsed(screen: ScreenTarget): void {
|
|
|
|
|
- screenUse.set(screen, (screenUse.get(screen) ?? 0) + 1)
|
|
|
|
|
|
|
+function markScreenUsed(screen: ScreenTarget): number {
|
|
|
|
|
+ const count = (screenUse.get(screen) ?? 0) + 1
|
|
|
|
|
+ screenUse.set(screen, count)
|
|
|
|
|
+
|
|
|
|
|
+ return count
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function screenUseCount(screen: ScreenTarget): number {
|
|
function screenUseCount(screen: ScreenTarget): number {
|
|
|
return screenUse.get(screen) ?? 0
|
|
return screenUse.get(screen) ?? 0
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+const screenCommitted = new Map<string, number>()
|
|
|
|
|
+
|
|
|
|
|
+function newerLaunchLanded(screen: ScreenTarget, stamp: number): boolean {
|
|
|
|
|
+ return (screenCommitted.get(screen) ?? 0) > stamp
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// a stop never claims a screen
|
|
|
|
|
+const screenClaims = new Map<string, number>()
|
|
|
|
|
+
|
|
|
|
|
+function claimStamp(screen: ScreenTarget, stamp: number): void {
|
|
|
|
|
+ if ((screenClaims.get(screen) ?? 0) < stamp) screenClaims.set(screen, stamp)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function claimScreen(screen: ScreenTarget): number {
|
|
|
|
|
+ const stamp = markScreenUsed(screen)
|
|
|
|
|
+ claimStamp(screen, stamp)
|
|
|
|
|
+
|
|
|
|
|
+ return stamp
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function releaseScreen(screen: ScreenTarget, stamp: number): void {
|
|
|
|
|
+ if (screenClaims.get(screen) === stamp) screenClaims.delete(screen)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function newerLaunchTakes(screen: ScreenTarget, stamp: number): boolean {
|
|
|
|
|
+ return newerLaunchLanded(screen, stamp) || (screenClaims.get(screen) ?? 0) > stamp
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// a killed engine still resolves its launch
|
|
|
|
|
+function launchStood(engine: boolean, screen: ScreenTarget): boolean {
|
|
|
|
|
+ return !engine || isLweRunning(screen)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function commitLaunch(screen: ScreenTarget, stamp: number): boolean {
|
|
|
|
|
+ if (newerLaunchLanded(screen, stamp)) return false
|
|
|
|
|
+ screenCommitted.set(screen, stamp)
|
|
|
|
|
+
|
|
|
|
|
+ return true
|
|
|
|
|
+}
|
|
|
// automatic relaunches after a runtime crash, at most one per screen in this window
|
|
// automatic relaunches after a runtime crash, at most one per screen in this window
|
|
|
const RELAUNCH_COOLDOWN_MS = 60_000
|
|
const RELAUNCH_COOLDOWN_MS = 60_000
|
|
|
|
|
|
|
@@ -53,6 +106,7 @@ let win: BrowserWindow | null = null
|
|
|
const lastAutoRelaunch = new Map<ScreenTarget, number>()
|
|
const lastAutoRelaunch = new Map<ScreenTarget, number>()
|
|
|
const launchChecks = new Map<ScreenTarget, ReturnType<typeof setTimeout>>()
|
|
const launchChecks = new Map<ScreenTarget, ReturnType<typeof setTimeout>>()
|
|
|
let playlistRecovery: ((screen: ScreenTarget, wallpaperId: string) => boolean) | null = null
|
|
let playlistRecovery: ((screen: ScreenTarget, wallpaperId: string) => boolean) | null = null
|
|
|
|
|
+let volumeSource: VolumeSource | null = null
|
|
|
|
|
|
|
|
function send(channel: string, payload?: unknown): void {
|
|
function send(channel: string, payload?: unknown): void {
|
|
|
if (win && !win.isDestroyed()) win.webContents.send(channel, payload)
|
|
if (win && !win.isDestroyed()) win.webContents.send(channel, payload)
|
|
@@ -65,14 +119,25 @@ export function getDisplayTargets(): ScreenTarget[] {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/** '*' (or nothing) means every screen: one engine in shared mode, one per screen otherwise */
|
|
/** '*' (or nothing) means every screen: one engine in shared mode, one per screen otherwise */
|
|
|
-export function expandScreen(screen?: ScreenTarget): ScreenTarget[] {
|
|
|
|
|
- const targets = getDisplayTargets()
|
|
|
|
|
|
|
+export function expandScreen(screen?: ScreenTarget, targets: ScreenTarget[] = getDisplayTargets()): ScreenTarget[] {
|
|
|
if (screen === undefined || screen === ALL_SCREENS) return targets
|
|
if (screen === undefined || screen === ALL_SCREENS) return targets
|
|
|
return targets.includes(screen) ? [screen] : []
|
|
return targets.includes(screen) ? [screen] : []
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+export function isShownOn(wallpaperId: string, screen?: ScreenTarget): boolean {
|
|
|
|
|
+ const assigned = getAssignments()
|
|
|
|
|
+
|
|
|
|
|
+ return expandScreen(screen).some((target) => (assigned[target] ?? assigned[ALL_SCREENS]) === wallpaperId)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export function getScreenAssignments(): ScreenAssignment[] {
|
|
export function getScreenAssignments(): ScreenAssignment[] {
|
|
|
- return Object.entries(getAssignments()).map(([screen, wallpaperId]) => ({ screen, wallpaperId }))
|
|
|
|
|
|
|
+ const splits = new Set(splitActiveScreens())
|
|
|
|
|
+
|
|
|
|
|
+ return Object.entries(getAssignments()).map(([screen, wallpaperId]) => ({
|
|
|
|
|
+ screen,
|
|
|
|
|
+ wallpaperId,
|
|
|
|
|
+ split: splits.has(screen)
|
|
|
|
|
+ }))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const busySources = new Map<string, () => string[]>()
|
|
const busySources = new Map<string, () => string[]>()
|
|
@@ -108,6 +173,10 @@ export function setPlaylistRecovery(handler: (screen: ScreenTarget, wallpaperId:
|
|
|
playlistRecovery = handler
|
|
playlistRecovery = handler
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+export function setVolumeSource(source: VolumeSource): void {
|
|
|
|
|
+ volumeSource = source
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function patchCompat(wallpaper: WallpaperMeta, patch: Partial<WallpaperCompat>): void {
|
|
function patchCompat(wallpaper: WallpaperMeta, patch: Partial<WallpaperCompat>): void {
|
|
|
updateWallpaper(wallpaper.id, { compat: { ...wallpaper.compat, ...patch } })
|
|
updateWallpaper(wallpaper.id, { compat: { ...wallpaper.compat, ...patch } })
|
|
|
send(IpcChannels.EVENT_LIBRARY_CHANGED)
|
|
send(IpcChannels.EVENT_LIBRARY_CHANGED)
|
|
@@ -142,15 +211,21 @@ export async function showWallpaper(
|
|
|
if (targets.length === 0) throw new Error(`Screen ${screen} is not connected`)
|
|
if (targets.length === 0) throw new Error(`Screen ${screen} is not connected`)
|
|
|
|
|
|
|
|
let appliedPath = ''
|
|
let appliedPath = ''
|
|
|
- for (const target of targets) {
|
|
|
|
|
- markScreenUsed(target)
|
|
|
|
|
- appliedPath = await applyWallpaperMeta(wallpaper, { ...options, screen: target })
|
|
|
|
|
- setAssignment(target, wallpaper.id)
|
|
|
|
|
- // a single wallpaper replaces a split, or the next start would bring the split back
|
|
|
|
|
- // whenever this wallpaper is also the split's first slot
|
|
|
|
|
- setSplitScreen(target, null)
|
|
|
|
|
- // only a wallpaper that crashed on launch before needs its fresh launches watched
|
|
|
|
|
- if (wallpaper.compat?.freshLaunch === 'crash') watchLaunch(wallpaper.id, target)
|
|
|
|
|
|
|
+ const engine = usesEngine(wallpaper)
|
|
|
|
|
+ const stamps = new Map(targets.map((target) => [target, markScreenUsed(target)]))
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (const target of targets) {
|
|
|
|
|
+ const stamp = stamps.get(target) ?? 0
|
|
|
|
|
+ if (newerLaunchTakes(target, stamp)) continue
|
|
|
|
|
+ claimStamp(target, stamp)
|
|
|
|
|
+ appliedPath = await applyWallpaperMeta(wallpaper, { ...options, screen: target })
|
|
|
|
|
+ if (!launchStood(engine, target) || !commitLaunch(target, stamp)) continue
|
|
|
|
|
+ setAssignment(target, wallpaper.id)
|
|
|
|
|
+ // only a wallpaper that crashed on launch before needs its fresh launches watched
|
|
|
|
|
+ if (wallpaper.compat?.freshLaunch === 'crash') watchLaunch(wallpaper.id, target)
|
|
|
|
|
+ }
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ for (const [target, stamp] of stamps) releaseScreen(target, stamp)
|
|
|
}
|
|
}
|
|
|
send(IpcChannels.EVENT_DISPLAY_CHANGED)
|
|
send(IpcChannels.EVENT_DISPLAY_CHANGED)
|
|
|
return appliedPath
|
|
return appliedPath
|
|
@@ -160,21 +235,40 @@ export async function showWallpaper(
|
|
|
export async function showSplitScreen(
|
|
export async function showSplitScreen(
|
|
|
screen: ScreenTarget,
|
|
screen: ScreenTarget,
|
|
|
beforeLaunch?: () => void,
|
|
beforeLaunch?: () => void,
|
|
|
- dependencyPrompt: 'always' | 'once' | 'never' = 'always'
|
|
|
|
|
|
|
+ dependencyPrompt: 'always' | 'once' | 'never' = 'always',
|
|
|
|
|
+ skipMissing = false
|
|
|
): Promise<void> {
|
|
): Promise<void> {
|
|
|
if (getDisplayMode() !== 'per-screen' || screen === ALL_SCREENS) throw new Error(t('split.needsPerScreen'))
|
|
if (getDisplayMode() !== 'per-screen' || screen === ALL_SCREENS) throw new Error(t('split.needsPerScreen'))
|
|
|
if (!getDisplayTargets().includes(screen)) throw new Error(`Screen ${screen} is not connected`)
|
|
if (!getDisplayTargets().includes(screen)) throw new Error(`Screen ${screen} is not connected`)
|
|
|
if (!supportsSplitScreens()) throw new Error(t('split.engineTooOld'))
|
|
if (!supportsSplitScreens()) throw new Error(t('split.engineTooOld'))
|
|
|
|
|
+ const claim = { stamp: claimScreen(screen) }
|
|
|
|
|
+ try {
|
|
|
|
|
+ await launchSplit(screen, claim, beforeLaunch, dependencyPrompt, skipMissing)
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ releaseScreen(screen, claim.stamp)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+async function launchSplit(
|
|
|
|
|
+ screen: ScreenTarget,
|
|
|
|
|
+ claim: { stamp: number },
|
|
|
|
|
+ beforeLaunch: (() => void) | undefined,
|
|
|
|
|
+ dependencyPrompt: 'always' | 'once' | 'never',
|
|
|
|
|
+ skipMissing: boolean
|
|
|
|
|
+): Promise<void> {
|
|
|
|
|
|
|
|
const split = getSplitScreens()[screen]
|
|
const split = getSplitScreens()[screen]
|
|
|
const slots: LweSplitSlot[] = []
|
|
const slots: LweSplitSlot[] = []
|
|
|
const shown: WallpaperMeta[] = []
|
|
const shown: WallpaperMeta[] = []
|
|
|
- for (const slot of split?.slots ?? []) {
|
|
|
|
|
- const wallpaper = getWallpaper(slot.wallpaperId)
|
|
|
|
|
- if (!wallpaper?.localPath || !isAnimatedWallpaper(wallpaper) || !wallpaperIsOnDisk(wallpaper.localPath)) continue
|
|
|
|
|
- await ensureDependencyInstalled(wallpaper, dependencyPrompt)
|
|
|
|
|
|
|
+ for (const { slot, wallpaper, path: slotPath } of showableSplitSlots(screen, skipMissing)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await ensureDependencyInstalled(wallpaper, dependencyPrompt)
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ if (dependencyPrompt === 'never') continue
|
|
|
|
|
+ throw err
|
|
|
|
|
+ }
|
|
|
slots.push({
|
|
slots.push({
|
|
|
- path: wallpaper.localPath,
|
|
|
|
|
|
|
+ path: slotPath,
|
|
|
scalingMode: slot.scalingMode ?? wallpaper.scalingMode,
|
|
scalingMode: slot.scalingMode ?? wallpaper.scalingMode,
|
|
|
zoom: wallpaper.zoom,
|
|
zoom: wallpaper.zoom,
|
|
|
offsetX: wallpaper.offsetX,
|
|
offsetX: wallpaper.offsetX,
|
|
@@ -185,7 +279,12 @@ export async function showSplitScreen(
|
|
|
shown.push(wallpaper)
|
|
shown.push(wallpaper)
|
|
|
}
|
|
}
|
|
|
if (slots.length === 0 || !split) throw new Error(t('split.empty'))
|
|
if (slots.length === 0 || !split) throw new Error(t('split.empty'))
|
|
|
- markScreenUsed(screen)
|
|
|
|
|
|
|
+ if (newerLaunchTakes(screen, claim.stamp)) {
|
|
|
|
|
+ // restore leaves the screen to whoever took it, a split somebody asked for goes on
|
|
|
|
|
+ if (!beforeLaunch) return
|
|
|
|
|
+ releaseScreen(screen, claim.stamp)
|
|
|
|
|
+ claim.stamp = claimScreen(screen)
|
|
|
|
|
+ }
|
|
|
beforeLaunch?.()
|
|
beforeLaunch?.()
|
|
|
|
|
|
|
|
// overrides would hit every slot by id, so slots play as published
|
|
// overrides would hit every slot by id, so slots play as published
|
|
@@ -196,12 +295,16 @@ export async function showSplitScreen(
|
|
|
fps: getDefaultFps() ?? undefined,
|
|
fps: getDefaultFps() ?? undefined,
|
|
|
forceFresh: true
|
|
forceFresh: true
|
|
|
})
|
|
})
|
|
|
|
|
+ if (!isLweRunning(screen) || !commitLaunch(screen, claim.stamp)) return
|
|
|
setAssignment(screen, shown[0].id)
|
|
setAssignment(screen, shown[0].id)
|
|
|
|
|
+ markSplitActive(screen)
|
|
|
send(IpcChannels.EVENT_DISPLAY_CHANGED)
|
|
send(IpcChannels.EVENT_DISPLAY_CHANGED)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export async function stopDisplay(screen?: ScreenTarget): Promise<void> {
|
|
export async function stopDisplay(screen?: ScreenTarget): Promise<void> {
|
|
|
- for (const target of expandScreen(screen)) markScreenUsed(target)
|
|
|
|
|
|
|
+ // no expandScreen here, it shells out
|
|
|
|
|
+ const marked = screen === undefined || screen === ALL_SCREENS ? [...screenUse.keys()] : [screen]
|
|
|
|
|
+ for (const target of marked) markScreenUsed(target)
|
|
|
if (screen === undefined || screen === ALL_SCREENS) {
|
|
if (screen === undefined || screen === ALL_SCREENS) {
|
|
|
await stopLwe()
|
|
await stopLwe()
|
|
|
clearAssignment()
|
|
clearAssignment()
|
|
@@ -224,7 +327,12 @@ function notifyCrash(wallpaper: WallpaperMeta, screen: ScreenTarget, phase: Cras
|
|
|
|
|
|
|
|
async function relaunchFresh(wallpaper: WallpaperMeta, screen: ScreenTarget): Promise<boolean> {
|
|
async function relaunchFresh(wallpaper: WallpaperMeta, screen: ScreenTarget): Promise<boolean> {
|
|
|
try {
|
|
try {
|
|
|
- await applyWallpaperMeta(getWallpaper(wallpaper.id) ?? wallpaper, { screen, forceFresh: true, dependencyPrompt: 'once' })
|
|
|
|
|
|
|
+ await applyWallpaperMeta(getWallpaper(wallpaper.id) ?? wallpaper, {
|
|
|
|
|
+ screen,
|
|
|
|
|
+ forceFresh: true,
|
|
|
|
|
+ dependencyPrompt: 'once',
|
|
|
|
|
+ volume: volumeSource?.(screen, wallpaper.id)
|
|
|
|
|
+ })
|
|
|
return true
|
|
return true
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
console.error('[Display] Fresh relaunch failed:', (err as Error).message)
|
|
console.error('[Display] Fresh relaunch failed:', (err as Error).message)
|
|
@@ -312,13 +420,24 @@ export async function testFreshLaunch(wallpaperId: string, screen?: ScreenTarget
|
|
|
if (!wallpaper) throw new Error(`Wallpaper ${wallpaperId} not found in library`)
|
|
if (!wallpaper) throw new Error(`Wallpaper ${wallpaperId} not found in library`)
|
|
|
const target = expandScreen(screen)[0]
|
|
const target = expandScreen(screen)[0]
|
|
|
if (!target) throw new Error('No screen to test on')
|
|
if (!target) throw new Error('No screen to test on')
|
|
|
- await applyWallpaperMeta(wallpaper, { screen: target, forceFresh: true })
|
|
|
|
|
- setAssignment(target, wallpaper.id)
|
|
|
|
|
|
|
+ const stamp = claimScreen(target)
|
|
|
|
|
+ try {
|
|
|
|
|
+ await applyWallpaperMeta(wallpaper, { screen: target, forceFresh: true })
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ releaseScreen(target, stamp)
|
|
|
|
|
+ }
|
|
|
|
|
+ if (launchStood(usesEngine(wallpaper), target) && commitLaunch(target, stamp)) setAssignment(target, wallpaper.id)
|
|
|
send(IpcChannels.EVENT_DISPLAY_CHANGED)
|
|
send(IpcChannels.EVENT_DISPLAY_CHANGED)
|
|
|
watchLaunch(wallpaper.id, target, () => { notifyCrash(getWallpaper(wallpaper.id) ?? wallpaper, target, 'launch', t('crash.testPassed')) }
|
|
watchLaunch(wallpaper.id, target, () => { notifyCrash(getWallpaper(wallpaper.id) ?? wallpaper, target, 'launch', t('crash.testPassed')) }
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+export function forgetAssignments(): void {
|
|
|
|
|
+ for (const target of screenUse.keys()) markScreenUsed(target)
|
|
|
|
|
+ clearAssignment()
|
|
|
|
|
+ send(IpcChannels.EVENT_DISPLAY_CHANGED)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/** Stops everything and forgets assignments, the screens mean something else after a mode switch */
|
|
/** Stops everything and forgets assignments, the screens mean something else after a mode switch */
|
|
|
export async function resetDisplays(): Promise<void> {
|
|
export async function resetDisplays(): Promise<void> {
|
|
|
for (const timer of launchChecks.values()) clearTimeout(timer)
|
|
for (const timer of launchChecks.values()) clearTimeout(timer)
|
|
@@ -333,7 +452,7 @@ export function initDisplay(window: BrowserWindow): void {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/** A missing drive still has a library entry. Launching that path marks a healthy wallpaper as a launch crash. */
|
|
|
|
|
|
|
+// a missing drive keeps its library entry, and launching it would mark a healthy wallpaper as crashed
|
|
|
function wallpaperIsOnDisk(localPath: string): boolean {
|
|
function wallpaperIsOnDisk(localPath: string): boolean {
|
|
|
if (!fs.existsSync(localPath)) return false
|
|
if (!fs.existsSync(localPath)) return false
|
|
|
try {
|
|
try {
|
|
@@ -343,45 +462,109 @@ function wallpaperIsOnDisk(localPath: string): boolean {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/** The slot showSplitScreen would store as the assignment, when that folder is actually there */
|
|
|
|
|
-function splitSlotId(screen: ScreenTarget): string | null {
|
|
|
|
|
- const split = getSplitScreens()[screen]
|
|
|
|
|
- if (!split) return null
|
|
|
|
|
- for (const slot of split.slots) {
|
|
|
|
|
|
|
+// a manual show lets the engine report a missing folder
|
|
|
|
|
+function showableSplitSlots(screen: ScreenTarget, onDiskOnly = true): { slot: SplitScreen['slots'][number]; wallpaper: WallpaperMeta; path: string }[] {
|
|
|
|
|
+ return (getSplitScreens()[screen]?.slots ?? []).flatMap((slot) => {
|
|
|
const wallpaper = getWallpaper(slot.wallpaperId)
|
|
const wallpaper = getWallpaper(slot.wallpaperId)
|
|
|
- if (!wallpaper?.localPath || !isAnimatedWallpaper(wallpaper) || !wallpaperIsOnDisk(wallpaper.localPath)) continue
|
|
|
|
|
- return wallpaper.id
|
|
|
|
|
- }
|
|
|
|
|
- return null
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * Puts the last animated wallpaper back on each screen after a restart. A live engine already showing
|
|
|
|
|
- * it is left alone. A split comes back through showSplitScreen. Screens a playlist or schedule just
|
|
|
|
|
- * claimed are skipped so the two launches do not run over each other.
|
|
|
|
|
- */
|
|
|
|
|
-export async function restoreAssignedWallpapers(skipScreens: ReadonlySet<ScreenTarget> = new Set()): Promise<void> {
|
|
|
|
|
- if (!getLweStatus().installed) return
|
|
|
|
|
- const saved = getAssignments()
|
|
|
|
|
- const useAtStart = new Map(Object.keys(saved).map((screen) => [screen, screenUseCount(screen)]))
|
|
|
|
|
- const targets = new Set(getDisplayTargets())
|
|
|
|
|
|
|
+ const localPath = wallpaper?.localPath
|
|
|
|
|
+ return wallpaper && localPath && isAnimatedWallpaper(wallpaper) && (!onDiskOnly || wallpaperIsOnDisk(localPath)) ? [{ slot, wallpaper, path: localPath }] : []
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const SPLIT_RETRY_MS = 3000
|
|
|
|
|
+
|
|
|
|
|
+export type VolumeSource = (screen: ScreenTarget, wallpaperId: string) => number | undefined
|
|
|
|
|
+
|
|
|
|
|
+type Running = ReturnType<typeof getRunningInstances>[number]
|
|
|
|
|
+
|
|
|
|
|
+function engineOf(inst?: Running): RestoreInput['running'] {
|
|
|
|
|
+ return inst ? { split: !!inst.split, wallpaperPath: inst.wallpaperPath } : undefined
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// a restore waiting for a retry must not start an engine after the shutdown kill
|
|
|
|
|
+let restoreRun = 0
|
|
|
|
|
+let restoreCancelled = false
|
|
|
|
|
+
|
|
|
|
|
+export function cancelRestore(): void {
|
|
|
|
|
+ restoreCancelled = true
|
|
|
|
|
+ restoreRun++
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function splitTry(screen: ScreenTarget) {
|
|
|
|
|
+ return trySplit({
|
|
|
|
|
+ show: () => showSplitScreen(screen, undefined, 'never', true),
|
|
|
|
|
+ uses: () => screenUseCount(screen),
|
|
|
|
|
+ onError: (err) => { console.error(`[Display] Could not restore the split on ${screen}:`, (err as Error).message) }
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+async function retrySplit(
|
|
|
|
|
+ run: number,
|
|
|
|
|
+ screen: ScreenTarget,
|
|
|
|
|
+ wallpaper: WallpaperMeta | null | undefined,
|
|
|
|
|
+ input: RestoreInput,
|
|
|
|
|
+ volumeFor?: VolumeSource
|
|
|
|
|
+): Promise<void> {
|
|
|
|
|
+ const settled = screenUseCount(screen)
|
|
|
|
|
+ await new Promise((resolve) => setTimeout(resolve, SPLIT_RETRY_MS))
|
|
|
|
|
+ if (run !== restoreRun || screenUseCount(screen) !== settled) return
|
|
|
|
|
+ if ((await splitTry(screen)) !== 'failed' || !wallpaper || run !== restoreRun) return
|
|
|
|
|
+
|
|
|
|
|
+ // the failed tries changed what is running
|
|
|
|
|
+ const live = getRunningInstances().find((inst) => inst.screen === screen)
|
|
|
|
|
+ if (planRestore({ ...input, splitActive: false, running: engineOf(live) }) !== 'wallpaper') return
|
|
|
|
|
+
|
|
|
|
|
+ console.log(`[Display] Restoring ${wallpaper.title} on ${screen} instead of its split`)
|
|
|
|
|
+ await showWallpaper(wallpaper, screen, { dependencyPrompt: 'never', volume: volumeFor?.(screen, wallpaper.id) })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export async function restoreAssignedWallpapers(
|
|
|
|
|
+ skipScreens: ReadonlySet<ScreenTarget> = new Set(),
|
|
|
|
|
+ volumeFor?: VolumeSource
|
|
|
|
|
+): Promise<void> {
|
|
|
|
|
+ if (restoreCancelled || !getLweStatus().installed) return
|
|
|
|
|
+ await restoreScreens(++restoreRun, skipScreens, volumeFor, undefined, 0)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const RESTORE_RETRIES = 5
|
|
|
|
|
+
|
|
|
|
|
+async function restoreScreens(
|
|
|
|
|
+ run: number,
|
|
|
|
|
+ skipScreens: ReadonlySet<ScreenTarget>,
|
|
|
|
|
+ volumeFor: VolumeSource | undefined,
|
|
|
|
|
+ only: readonly ScreenTarget[] | undefined,
|
|
|
|
|
+ attempt: number,
|
|
|
|
|
+ carried?: { useAtStart: Map<string, number>; targets?: Set<string> }
|
|
|
|
|
+): Promise<void> {
|
|
|
|
|
+ const saved = Object.fromEntries(Object.entries(getAssignments()).filter(([screen]) => !only || only.includes(screen)))
|
|
|
|
|
+ // judged against the first pass, a launch started since has taken the screen
|
|
|
|
|
+ const useAtStart = new Map(Object.keys(saved).map((screen) => [screen, carried?.useAtStart.get(screen) ?? screenUseCount(screen)]))
|
|
|
|
|
+ const targets = carried?.targets ?? new Set(getDisplayTargets())
|
|
|
const running = new Map(getRunningInstances().map((inst) => [inst.screen, inst]))
|
|
const running = new Map(getRunningInstances().map((inst) => [inst.screen, inst]))
|
|
|
|
|
+ // '*' is an old single-wallpaper assignment, restorable in shared mode only
|
|
|
|
|
+ const missing = Object.keys(saved).filter((screen) => screen !== ALL_SCREENS && !targets.has(screen) && !skipScreens.has(screen))
|
|
|
|
|
+ const unused = missing.length > 0 ? new Set(getUnusedScreens()) : new Set<string>()
|
|
|
|
|
+ const waiting = new Set(missing.filter((screen) => !unused.has(screen)))
|
|
|
for (const [screen, wallpaperId] of Object.entries(saved)) {
|
|
for (const [screen, wallpaperId] of Object.entries(saved)) {
|
|
|
|
|
+ if (run !== restoreRun) return
|
|
|
const wallpaper = getWallpaper(wallpaperId)
|
|
const wallpaper = getWallpaper(wallpaperId)
|
|
|
const localPath = wallpaper?.localPath ?? ''
|
|
const localPath = wallpaper?.localPath ?? ''
|
|
|
const inst = running.get(screen)
|
|
const inst = running.get(screen)
|
|
|
- const plan = planRestore({
|
|
|
|
|
|
|
+ const splitActive = isSplitActive(screen)
|
|
|
|
|
+ const input: RestoreInput = {
|
|
|
skipped: skipScreens.has(screen),
|
|
skipped: skipScreens.has(screen),
|
|
|
connected: targets.has(screen),
|
|
connected: targets.has(screen),
|
|
|
animated: !!wallpaper && isAnimatedWallpaper(wallpaper),
|
|
animated: !!wallpaper && isAnimatedWallpaper(wallpaper),
|
|
|
onDisk: !!localPath && wallpaperIsOnDisk(localPath),
|
|
onDisk: !!localPath && wallpaperIsOnDisk(localPath),
|
|
|
localPath,
|
|
localPath,
|
|
|
- running: inst ? { split: !!inst.split, wallpaperPath: inst.wallpaperPath } : undefined,
|
|
|
|
|
- splitSlotId: splitSlotId(screen),
|
|
|
|
|
- wallpaperId
|
|
|
|
|
- })
|
|
|
|
|
- if (plan === 'skip' || !wallpaper) continue
|
|
|
|
|
- // an earlier screen takes at least 2s. Play, the tray or a playlist may have taken this one since.
|
|
|
|
|
|
|
+ running: engineOf(inst),
|
|
|
|
|
+ splitActive,
|
|
|
|
|
+ splitShowable: splitActive && showableSplitSlots(screen).length > 0
|
|
|
|
|
+ }
|
|
|
|
|
+ const plan = planRestore(input)
|
|
|
|
|
+ if (plan === 'wait') waiting.add(screen)
|
|
|
|
|
+ if (plan === 'skip' || plan === 'wait') continue
|
|
|
|
|
+ // an earlier screen takes 2s or more, Play, the tray or a playlist may have taken this one since
|
|
|
if (restoreSuperseded(wallpaperId, getAssignments()[screen], useAtStart.get(screen) ?? 0, screenUseCount(screen))) {
|
|
if (restoreSuperseded(wallpaperId, getAssignments()[screen], useAtStart.get(screen) ?? 0, screenUseCount(screen))) {
|
|
|
console.log(`[Display] Leaving ${screen}, it changed while restoring`)
|
|
console.log(`[Display] Leaving ${screen}, it changed while restoring`)
|
|
|
continue
|
|
continue
|
|
@@ -389,13 +572,34 @@ export async function restoreAssignedWallpapers(skipScreens: ReadonlySet<ScreenT
|
|
|
try {
|
|
try {
|
|
|
if (plan === 'split') {
|
|
if (plan === 'split') {
|
|
|
console.log(`[Display] Restoring split on ${screen}`)
|
|
console.log(`[Display] Restoring split on ${screen}`)
|
|
|
- await showSplitScreen(screen, undefined, 'never')
|
|
|
|
|
- } else {
|
|
|
|
|
- console.log(`[Display] Restoring ${wallpaper.title} on ${screen}`)
|
|
|
|
|
- await showWallpaper(wallpaper, screen, { dependencyPrompt: 'never' })
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if ((await splitTry(screen)) === 'failed') {
|
|
|
|
|
+ void retrySplit(run, screen, wallpaper, input, volumeFor).catch((err: unknown) => {
|
|
|
|
|
+ console.error(`[Display] Could not restore ${screen}:`, (err as Error).message)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ continue
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ if (!wallpaper) continue
|
|
|
|
|
+
|
|
|
|
|
+ console.log(`[Display] Restoring ${wallpaper.title} on ${screen}`)
|
|
|
|
|
+ await showWallpaper(wallpaper, screen, { dependencyPrompt: 'never', volume: volumeFor?.(screen, wallpaperId) })
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
console.error(`[Display] Could not restore ${wallpaper?.title ?? wallpaperId} on ${screen}:`, (err as Error).message)
|
|
console.error(`[Display] Could not restore ${wallpaper?.title ?? wallpaperId} on ${screen}:`, (err as Error).message)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ if (waiting.size === 0) return
|
|
|
|
|
+ if (attempt >= RESTORE_RETRIES) {
|
|
|
|
|
+ console.log(`[Display] Gave up restoring ${[...waiting].join(', ')}, still waiting after ${RESTORE_RETRIES} tries`)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ await new Promise((resolve) => setTimeout(resolve, SPLIT_RETRY_MS))
|
|
|
|
|
+ if (run !== restoreRun) return
|
|
|
|
|
+ const onlyFolders = [...waiting].every((screen) => targets.has(screen))
|
|
|
|
|
+ await restoreScreens(run, skipScreens, volumeFor, [...waiting], attempt + 1, {
|
|
|
|
|
+ useAtStart,
|
|
|
|
|
+ targets: onlyFolders ? targets : undefined
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|