Skip to content

Commit 5aa8903

Browse files
authored
feat(system): Prevent the system from sleeping or display from turning off while the game is busy in Windows OS (#1572)
1 parent f789ed4 commit 5aa8903

File tree

8 files changed

+66
-0
lines changed

8 files changed

+66
-0
lines changed

Generals/Code/GameEngine/Include/Common/OSDisplay.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ enum OSDisplayOtherFlags CPP_11(: UnsignedInt)
6363
// This function will return the button pressed to close the dialog.
6464
OSDisplayButtonType OSDisplayWarningBox(AsciiString p, AsciiString m, UnsignedInt buttonFlags, UnsignedInt otherFlags);
6565

66+
// TheSuperHackers @feature Tell the Operating System that the game is considered busy
67+
// and we would not like the display screen and/or system to shut off.
68+
void OSDisplaySetBusyState(Bool busyDisplay, Bool busySystem);
69+
6670
#endif /* __OSDISPLAY_H__ */

Generals/Code/GameEngine/Include/GameLogic/GameLogic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class GameLogic : public SubsystemInterface, public Snapshot
173173
Bool isInInternetGame( void );
174174
Bool isInShellGame( void );
175175
Bool isInMultiplayerGame( void );
176+
Bool isInInteractiveGame() const;
176177

177178
static Bool isInInteractiveGame(GameMode mode) { return mode != GAME_NONE && mode != GAME_SHELL; }
178179

@@ -254,6 +255,8 @@ class GameLogic : public SubsystemInterface, public Snapshot
254255

255256
private:
256257

258+
void updateDisplayBusyState();
259+
257260
void pauseGameLogic(Bool paused);
258261
void pauseGameSound(Bool paused);
259262
void pauseGameMusic(Bool paused);
@@ -397,6 +400,7 @@ inline GameMode GameLogic::getGameMode( void ) { return m_gameMode; }
397400
inline Bool GameLogic::isInLanGame( void ) { return (m_gameMode == GAME_LAN); }
398401
inline Bool GameLogic::isInSkirmishGame( void ) { return (m_gameMode == GAME_SKIRMISH); }
399402
inline Bool GameLogic::isInMultiplayerGame( void ) { return (m_gameMode == GAME_LAN) || (m_gameMode == GAME_INTERNET) ; }
403+
inline Bool GameLogic::isInInteractiveGame() const { return isInInteractiveGame(m_gameMode); }
400404
inline Bool GameLogic::isInReplayGame( void ) { return (m_gameMode == GAME_REPLAY); }
401405
inline Bool GameLogic::isInInternetGame( void ) { return (m_gameMode == GAME_INTERNET); }
402406
inline Bool GameLogic::isInShellGame( void ) { return (m_gameMode == GAME_SHELL); }

Generals/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "Common/LatchRestore.h"
4141
#include "Common/MapObject.h"
4242
#include "Common/MultiplayerSettings.h"
43+
#include "Common/OSDisplay.h"
4344
#include "Common/PerfTimer.h"
4445
#include "Common/Player.h"
4546
#include "Common/PlayerList.h"
@@ -981,6 +982,16 @@ void GameLogic::setGameLoading( Bool loading )
981982
m_loadingScene = loading;
982983
}
983984

985+
// ------------------------------------------------------------------------------------------------
986+
// ------------------------------------------------------------------------------------------------
987+
void GameLogic::updateDisplayBusyState()
988+
{
989+
const Bool busySystem = isInInteractiveGame() && !isGamePaused();
990+
const Bool busyDisplay = busySystem && !TheGlobalData->m_headless;
991+
992+
OSDisplaySetBusyState(busyDisplay, busySystem);
993+
}
994+
984995
// ------------------------------------------------------------------------------------------------
985996
// ------------------------------------------------------------------------------------------------
986997
void GameLogic::setGameMode( GameMode mode )
@@ -989,6 +1000,8 @@ void GameLogic::setGameMode( GameMode mode )
9891000
m_gameMode = mode;
9901001

9911002
TheMouse->onGameModeChanged(prev, mode);
1003+
1004+
updateDisplayBusyState();
9921005
}
9931006

9941007
// ------------------------------------------------------------------------------------------------
@@ -3736,6 +3749,8 @@ void GameLogic::setGamePaused( Bool paused, Bool pauseMusic, Bool pauseInput )
37363749
pauseGameSound(paused);
37373750
pauseGameMusic(paused && pauseMusic);
37383751
pauseGameInput(paused && pauseInput);
3752+
3753+
updateDisplayBusyState();
37393754
}
37403755

37413756
// ------------------------------------------------------------------------------------------------

Generals/Code/GameEngineDevice/Source/Win32Device/Common/Win32OSDisplay.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,13 @@ OSDisplayButtonType OSDisplayWarningBox(AsciiString p, AsciiString m, UnsignedIn
123123

124124
return OSDBT_CANCEL;
125125
}
126+
127+
//-------------------------------------------------------------------------------------------------
128+
void OSDisplaySetBusyState(Bool busyDisplay, Bool busySystem)
129+
{
130+
EXECUTION_STATE state = ES_CONTINUOUS;
131+
state |= busyDisplay ? ES_DISPLAY_REQUIRED : 0;
132+
state |= busySystem ? ES_SYSTEM_REQUIRED : 0;
133+
134+
::SetThreadExecutionState(state);
135+
}

GeneralsMD/Code/GameEngine/Include/Common/OSDisplay.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ enum OSDisplayOtherFlags CPP_11(: UnsignedInt)
6363
// This function will return the button pressed to close the dialog.
6464
OSDisplayButtonType OSDisplayWarningBox(AsciiString p, AsciiString m, UnsignedInt buttonFlags, UnsignedInt otherFlags);
6565

66+
// TheSuperHackers @feature Tell the Operating System that the game is considered busy
67+
// and we would not like the display screen and/or system to shut off.
68+
void OSDisplaySetBusyState(Bool busyDisplay, Bool busySystem);
69+
6670
#endif /* __OSDISPLAY_H__ */

GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class GameLogic : public SubsystemInterface, public Snapshot
183183
Bool isInInternetGame( void );
184184
Bool isInShellGame( void );
185185
Bool isInMultiplayerGame( void );
186+
Bool isInInteractiveGame() const;
186187

187188
static Bool isInInteractiveGame(GameMode mode) { return mode != GAME_NONE && mode != GAME_SHELL; }
188189

@@ -272,6 +273,8 @@ class GameLogic : public SubsystemInterface, public Snapshot
272273

273274
private:
274275

276+
void updateDisplayBusyState();
277+
275278
void pauseGameLogic(Bool paused);
276279
void pauseGameSound(Bool paused);
277280
void pauseGameMusic(Bool paused);
@@ -420,6 +423,7 @@ inline GameMode GameLogic::getGameMode( void ) { return m_gameMode; }
420423
inline Bool GameLogic::isInLanGame( void ) { return (m_gameMode == GAME_LAN); }
421424
inline Bool GameLogic::isInSkirmishGame( void ) { return (m_gameMode == GAME_SKIRMISH); }
422425
inline Bool GameLogic::isInMultiplayerGame( void ) { return (m_gameMode == GAME_LAN) || (m_gameMode == GAME_INTERNET) ; }
426+
inline Bool GameLogic::isInInteractiveGame() const { return isInInteractiveGame(m_gameMode); }
423427
inline Bool GameLogic::isInReplayGame( void ) { return (m_gameMode == GAME_REPLAY); }
424428
inline Bool GameLogic::isInInternetGame( void ) { return (m_gameMode == GAME_INTERNET); }
425429
inline Bool GameLogic::isInShellGame( void ) { return (m_gameMode == GAME_SHELL); }

GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "Common/LatchRestore.h"
4242
#include "Common/MapObject.h"
4343
#include "Common/MultiplayerSettings.h"
44+
#include "Common/OSDisplay.h"
4445
#include "Common/PerfTimer.h"
4546
#include "Common/Player.h"
4647
#include "Common/PlayerList.h"
@@ -1118,6 +1119,16 @@ void GameLogic::deleteLoadScreen( void )
11181119

11191120
}
11201121

1122+
// ------------------------------------------------------------------------------------------------
1123+
// ------------------------------------------------------------------------------------------------
1124+
void GameLogic::updateDisplayBusyState()
1125+
{
1126+
const Bool busySystem = isInInteractiveGame() && !isGamePaused();
1127+
const Bool busyDisplay = busySystem && !TheGlobalData->m_headless;
1128+
1129+
OSDisplaySetBusyState(busyDisplay, busySystem);
1130+
}
1131+
11211132
// ------------------------------------------------------------------------------------------------
11221133
// ------------------------------------------------------------------------------------------------
11231134
void GameLogic::setGameMode( GameMode mode )
@@ -1126,6 +1137,8 @@ void GameLogic::setGameMode( GameMode mode )
11261137
m_gameMode = mode;
11271138

11281139
TheMouse->onGameModeChanged(prev, mode);
1140+
1141+
updateDisplayBusyState();
11291142
}
11301143

11311144
// ------------------------------------------------------------------------------------------------
@@ -4288,6 +4301,8 @@ void GameLogic::setGamePaused( Bool paused, Bool pauseMusic, Bool pauseInput )
42884301
pauseGameSound(paused);
42894302
pauseGameMusic(paused && pauseMusic);
42904303
pauseGameInput(paused && pauseInput);
4304+
4305+
updateDisplayBusyState();
42914306
}
42924307

42934308
// ------------------------------------------------------------------------------------------------

GeneralsMD/Code/GameEngineDevice/Source/Win32Device/Common/Win32OSDisplay.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,13 @@ OSDisplayButtonType OSDisplayWarningBox(AsciiString p, AsciiString m, UnsignedIn
123123

124124
return OSDBT_CANCEL;
125125
}
126+
127+
//-------------------------------------------------------------------------------------------------
128+
void OSDisplaySetBusyState(Bool busyDisplay, Bool busySystem)
129+
{
130+
EXECUTION_STATE state = ES_CONTINUOUS;
131+
state |= busyDisplay ? ES_DISPLAY_REQUIRED : 0;
132+
state |= busySystem ? ES_SYSTEM_REQUIRED : 0;
133+
134+
::SetThreadExecutionState(state);
135+
}

0 commit comments

Comments
 (0)