Skip to content

Commit

Permalink
[FIX] Power saver blocker disabling after library updates (#3519)
Browse files Browse the repository at this point in the history
* [FIX] Power saver blocker disabling after library updates

* improve logic

* fix display sleeping

* lint

* remove unused imports
  • Loading branch information
Etaash-mathamsetty authored Aug 9, 2024
1 parent 7b81db9 commit b5c8adf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/backend/api/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
35 changes: 20 additions & 15 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import { cpus } from 'os'
import {
existsSync,
rmSync,
unlinkSync,
watch,
writeFileSync,
readdirSync,
readFileSync
} from 'graceful-fs'
Expand Down Expand Up @@ -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)
}
})

Expand Down
2 changes: 1 addition & 1 deletion src/common/typedefs/ipcBridge.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions src/frontend/state/GlobalState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
Runner,
WineVersionInfo,
LibraryTopSectionOptions,
ExperimentalFeatures
ExperimentalFeatures,
Status
} from 'common/types'
import {
DialogModalOptions,
Expand Down Expand Up @@ -930,12 +931,28 @@ class GlobalState extends PureComponent<Props> {
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()
}
Expand Down

0 comments on commit b5c8adf

Please sign in to comment.