Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Power saver blocker disabling after library updates #3519

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -28,9 +28,7 @@ import { cpus } from 'os'
import {
existsSync,
rmSync,
unlinkSync,
watch,
writeFileSync,
readdirSync,
readFileSync
} from 'graceful-fs'
Expand Down Expand Up @@ -545,24 +543,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 @@ -920,12 +921,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
Loading