diff --git a/src/backend/api/misc.ts b/src/backend/api/misc.ts index e608f1f137..49990bae73 100644 --- a/src/backend/api/misc.ts +++ b/src/backend/api/misc.ts @@ -67,7 +67,7 @@ export const openWebviewPage = (url: string) => export const setZoomFactor = (zoom: string) => ipcRenderer.send('setZoomFactor', zoom) export const frontendReady = () => ipcRenderer.send('frontendReady') -export const lock = () => ipcRenderer.send('lock') +export const lock = (playing: boolean) => ipcRenderer.send('lock', playing) export const unlock = () => ipcRenderer.send('unlock') export const login = async (sid: string) => ipcRenderer.invoke('login', sid) export const logoutLegendary = async () => ipcRenderer.invoke('logoutLegendary') diff --git a/src/backend/main.ts b/src/backend/main.ts index f46a6818ef..349641e916 100644 --- a/src/backend/main.ts +++ b/src/backend/main.ts @@ -27,9 +27,7 @@ import { cpus } from 'os' import { existsSync, rmSync, - unlinkSync, watch, - writeFileSync, readdirSync, readFileSync } from 'graceful-fs' @@ -527,24 +525,31 @@ process.on('uncaughtException', async (err) => { }) let powerId: number | null +let displaySleepId: number | null -ipcMain.on('lock', () => { - if (!existsSync(join(gamesConfigPath, 'lock'))) { - writeFileSync(join(gamesConfigPath, 'lock'), '') - if (!powerId) { - logInfo('Preventing machine to sleep', LogPrefix.Backend) - powerId = powerSaveBlocker.start('prevent-app-suspension') - } +ipcMain.on('lock', (e, playing: boolean) => { + if (!playing && (!powerId || !powerSaveBlocker.isStarted(powerId))) { + logInfo('Preventing machine to sleep', LogPrefix.Backend) + powerId = powerSaveBlocker.start('prevent-app-suspension') + } + + if ( + playing && + (!displaySleepId || !powerSaveBlocker.isStarted(displaySleepId)) + ) { + logInfo('Preventing display to sleep', LogPrefix.Backend) + displaySleepId = powerSaveBlocker.start('prevent-display-sleep') } }) ipcMain.on('unlock', () => { - if (existsSync(join(gamesConfigPath, 'lock'))) { - unlinkSync(join(gamesConfigPath, 'lock')) - if (powerId) { - logInfo('Stopping Power Saver Blocker', LogPrefix.Backend) - powerSaveBlocker.stop(powerId) - } + if (powerId && powerSaveBlocker.isStarted(powerId)) { + logInfo('Stopping Power Saver Blocker', LogPrefix.Backend) + powerSaveBlocker.stop(powerId) + } + if (displaySleepId && powerSaveBlocker.isStarted(displaySleepId)) { + logInfo('Stopping Display Sleep Blocker', LogPrefix.Backend) + powerSaveBlocker.stop(displaySleepId) } }) diff --git a/src/common/typedefs/ipcBridge.d.ts b/src/common/typedefs/ipcBridge.d.ts index 004c7ddff6..d4387c02d7 100644 --- a/src/common/typedefs/ipcBridge.d.ts +++ b/src/common/typedefs/ipcBridge.d.ts @@ -56,7 +56,7 @@ interface SyncIPCFunctions { changeLanguage: (language: string) => void notify: (args: { title: string; body: string }) => void frontendReady: () => void - lock: () => void + lock: (playing: boolean) => void unlock: () => void quit: () => void openExternalUrl: (url: string) => void diff --git a/src/frontend/state/GlobalState.tsx b/src/frontend/state/GlobalState.tsx index 3b5facdcb6..a637824219 100644 --- a/src/frontend/state/GlobalState.tsx +++ b/src/frontend/state/GlobalState.tsx @@ -10,7 +10,8 @@ import { Runner, WineVersionInfo, LibraryTopSectionOptions, - ExperimentalFeatures + ExperimentalFeatures, + Status } from 'common/types' import { DialogModalOptions, @@ -930,12 +931,28 @@ class GlobalState extends PureComponent { storage.setItem('hide_changelogs', JSON.stringify(hideChangelogsOnStartup)) storage.setItem('last_changelog', JSON.stringify(lastChangelogShown)) - const pendingOps = libraryStatus.filter( - (game) => game.status !== 'playing' && game.status !== 'done' + const allowedPendingOps: Status[] = [ + 'installing', + 'updating', + 'launching', + 'playing', + 'redist', + 'winetricks', + 'extracting', + 'repairing', + 'moving', + 'syncing-saves', + 'uninstalling' + ] + + const pendingOps = libraryStatus.filter((game) => + allowedPendingOps.includes(game.status) ).length + const playing = + libraryStatus.filter((game) => game.status === 'playing').length > 0 if (pendingOps) { - window.api.lock() + window.api.lock(playing) } else { window.api.unlock() }