Add support for preventing automatic screensaver or sleep.
It was suggested I make an issue about this, so here it is :) Related Forum Post
When players play our game with a USB Gamepad without touching the keyboard or mouse, the computer sleeps automatically while they're playing. It'd be nice for some way to prevent this by disabling the automatic sleep while the window is open. This feature would also be useful for playing a longer video uninterrupted (say, in the case of a game cutscene).
The proper way to do this in Windows is by using SetThreadExecutionState. This is what we recently put in to fix the issue.
On Mac, it looks like it's suggested to use UpdateSystemActivity.
On Linux, XResetScreenSaver.
This isn't a high priority issue, but I think it'd be nice to have in SFML. Every game that uses gamepads will run into this issue, as most computers have an automatic screensaver or screen blank.
It's worth noting that this does not seem to occur when using Windows-specific XInput gamepads, such as XBox 360 controllers.
Sounds actually reasonable and already finished a basic implementation for Windows, although I'm not sure that's the ideal way to do it on Linux? Sounds a bit like you're turning the screensaver off rather than just preventing it from kicking in? And how about other power states like sleep/standby?
The "modern" implementation for OS X is described in this document: Q&A 1340. UpdateSystemActivity is deprecated.
The Windows and Android implementations are in the new branch and ready for testing (it's kinda trivial):
For Windows:
- Default behavior: Power saving enabled.
- Open an elevated command prompt and run
powercfg -requests. - The output should list all programs currently preventing different powersaving from kicking in.
For Android:
- Default behavior: Power saving disabled.
- Just disable it and wait. The display should turn off after a set amount of time.
Next will be X11. If anyone feels like doing the MacOS version... :)
Looking for feedback:
Is it sufficient to be able to just disable screensaver/sleep? Or would it better to have some enum to controll whether the system should avoid sleep but is allowed to turn off the screen (e.g. for server programs)?
I don't have any other systems to dev or test on, but it works fine on Windows 7.
That's a good point about differentiating between screensaver/sleep. The only reason I could see against having some sort of enum/bitfield is if if the different platforms don't support such a distinction.
If this feature is eventually marked as accepted I'll look into OS X impl.
I currently don't see any use case for a screensaver-but-no-sleep mode. Who would use it?
However, if we support things like that, why not add sleep / awake events (especially useful on mobile OSes)?
BTW, @MarioLiebisch you have tabs in your impl. :stuck_out_tongue_winking_eye:
I currently don't see any use case for a screensaver-but-no-sleep mode. Who would use it?
Building a server/daemon utilizing SFML? Like a game server?
you have tabs in your impl.
Huh? What? Bah!
Building a server/daemon utilizing SFML? Like a game server?
Makes sense. :)
So going back to this suggestion/issue: Could I get some feedback on the following suggestion?
// what you'd set for most games or multimedia apps
sf::setPowerPolicy(sf::Power::PreventScreensaver);
// prevent sleeping only (for daemons)
sf::setPowerPolicy(sf::Power::PreventSleep);
// the default behavior (i.e. allow anything)
sf::setPowerPolicy(sf::Power::None);
Alternatively, I guess the following enum members might be better to understand esp. for mobile platforms:
sf::Power::KeepScreenActive
sf::Power::KeepSystemActive
sf::Power::None
What about sf::Power::setPolicy instead of sf::setPowerPolicy? (It would be closer to others APIs such as keyboard, mouse, joystick, ...)
Regarding the mobile platforms, I guess a good doc/tutorial would be enough -- no need to duplicate names I think.
Hm right. This namespace could also include a member to determine whether the system is running on batteries. Also I wasn't talking about aliases or so, just name alternatives.
Was this ever added to 2.5.1? I'm on ubuntu with latest SFML and noticed my laptop was going to sleep when I used a joystick to play my game.