Skip to content

Commit

Permalink
Merge pull request #199 from hex007/move-pstimeout
Browse files Browse the repository at this point in the history
PowerSaver Updates
  • Loading branch information
joolswills committed Aug 4, 2017
2 parents d953d6b + e924b5d commit 244bf47
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 56 deletions.
5 changes: 4 additions & 1 deletion es-app/src/SystemScreenSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#endif
#include "components/VideoVlcComponent.h"
#include "platform.h"
#include "PowerSaver.h"
#include "Renderer.h"
#include "Settings.h"
#include "SystemData.h"
Expand Down Expand Up @@ -100,6 +101,7 @@ void SystemScreenSaver::startScreenSaver()
mVideoScreensaver->setVideo(path);
mVideoScreensaver->setScreensaverMode(true);
mVideoScreensaver->onShow();
PowerSaver::runningScreenSaver(true);
mTimer = 0;
return;
}
Expand All @@ -114,6 +116,7 @@ void SystemScreenSaver::stopScreenSaver()
delete mVideoScreensaver;
mVideoScreensaver = NULL;
mState = STATE_INACTIVE;
PowerSaver::runningScreenSaver(false);
}

void SystemScreenSaver::renderScreenSaver()
Expand Down Expand Up @@ -318,4 +321,4 @@ void SystemScreenSaver::launchGame()
{
view->launch(mCurrentGame);
}
}
}
2 changes: 1 addition & 1 deletion es-app/src/SystemScreenSaver.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ class SystemScreenSaver : public Window::ScreenSaver
int mTimer;
FileData* mCurrentGame;
std::string mGameName;
std::string mSystemName;
std::string mSystemName;
};
32 changes: 14 additions & 18 deletions es-app/src/guis/GuiMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ GuiMenu::GuiMenu(Window* window) : GuiComponent(window), mMenu(window, "MAIN MEN
for(auto it = transitions.begin(); it != transitions.end(); it++)
vol_dev->add(*it, *it, Settings::getInstance()->getString("AudioDevice") == *it);
s->addWithLabel("AUDIO DEVICE", vol_dev);
s->addSaveFunc([vol_dev] {
s->addSaveFunc([vol_dev] {
Settings::getInstance()->setString("AudioDevice", vol_dev->getSelected());
VolumeControl::getInstance()->deinit();
VolumeControl::getInstance()->init();
Expand Down Expand Up @@ -136,7 +136,10 @@ GuiMenu::GuiMenu(Window* window) : GuiComponent(window), mMenu(window, "MAIN MEN
auto screensaver_time = std::make_shared<SliderComponent>(mWindow, 0.f, 30.f, 1.f, "m");
screensaver_time->setValue((float)(Settings::getInstance()->getInt("ScreenSaverTime") / (1000 * 60)));
s->addWithLabel("SCREENSAVER AFTER", screensaver_time);
s->addSaveFunc([screensaver_time] { Settings::getInstance()->setInt("ScreenSaverTime", (int)round(screensaver_time->getValue()) * (1000 * 60)); });
s->addSaveFunc([screensaver_time] {
Settings::getInstance()->setInt("ScreenSaverTime", (int)round(screensaver_time->getValue()) * (1000 * 60));
PowerSaver::updateTimeouts();
});

// screensaver behavior
auto screensaver_behavior = std::make_shared< OptionListComponent<std::string> >(mWindow, "SCREENSAVER BEHAVIOR", false);
Expand Down Expand Up @@ -179,7 +182,7 @@ GuiMenu::GuiMenu(Window* window) : GuiComponent(window), mMenu(window, "MAIN MEN
s->addSaveFunc([move_carousel] {
if (move_carousel->getState()
&& !Settings::getInstance()->getBool("MoveCarousel")
&& PowerSaver::ps_instant == PowerSaver::getTimeout())
&& PowerSaver::getMode() == PowerSaver::INSTANT)
{
Settings::getInstance()->setString("PowerSaverMode", "default");
PowerSaver::init();
Expand All @@ -199,7 +202,7 @@ GuiMenu::GuiMenu(Window* window) : GuiComponent(window), mMenu(window, "MAIN MEN
s->addSaveFunc([transition_style] {
if (Settings::getInstance()->getString("TransitionStyle") == "instant"
&& transition_style->getSelected() != "instant"
&& PowerSaver::ps_instant == PowerSaver::getTimeout())
&& PowerSaver::getMode() == PowerSaver::INSTANT)
{
Settings::getInstance()->setString("PowerSaverMode", "default");
PowerSaver::init();
Expand Down Expand Up @@ -266,7 +269,7 @@ GuiMenu::GuiMenu(Window* window) : GuiComponent(window), mMenu(window, "MAIN MEN
addEntry("GAME COLLECTION SETTINGS", 0x777777FF, true,
[this] { openCollectionSystemSettings();
});

addEntry("OTHER SETTINGS", 0x777777FF, true,
[this] {
auto s = new GuiSettings(mWindow, "OTHER SETTINGS");
Expand All @@ -288,21 +291,14 @@ GuiMenu::GuiMenu(Window* window) : GuiComponent(window), mMenu(window, "MAIN MEN
power_saver->add(*it, *it, Settings::getInstance()->getString("PowerSaverMode") == *it);
s->addWithLabel("POWER SAVER MODES", power_saver);
s->addSaveFunc([this, power_saver] {
if (Settings::getInstance()->getString("PowerSaverMode") != "instant" && power_saver->getSelected() == "instant"){
mWindow->pushGui(new GuiMsgBox(mWindow, "Setting Power Saver to Instant Mode disables Carousel transition and sets Transition Style to Instant. Would you like to continue?"
, "YES", [] {
Settings::getInstance()->setString("TransitionStyle", "instant");
Settings::getInstance()->setString("PowerSaverMode", "instant");
Settings::getInstance()->setBool("MoveCarousel", false);
PowerSaver::init();
}, "NO", nullptr)
);
} else {
Settings::getInstance()->setString("PowerSaverMode", power_saver->getSelected());
PowerSaver::init();
if (Settings::getInstance()->getString("PowerSaverMode") != "instant" && power_saver->getSelected() == "instant") {
Settings::getInstance()->setString("TransitionStyle", "instant");
Settings::getInstance()->setBool("MoveCarousel", false);
}
Settings::getInstance()->setString("PowerSaverMode", power_saver->getSelected());
PowerSaver::init();
});

// gamelists
auto save_gamelists = std::make_shared<SwitchComponent>(mWindow);
save_gamelists->setState(Settings::getInstance()->getBool("SaveGamelistsOnExit"));
Expand Down
31 changes: 14 additions & 17 deletions es-app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool parseArgs(int argc, char* argv[], unsigned int* width, unsigned int* height
#ifndef WIN32
}else if(strcmp(argv[i], "--show-hidden-files") == 0)
{
Settings::getInstance()->setBool("ShowHiddenFiles", true);
Settings::getInstance()->setBool("ShowHiddenFiles", true);
#endif
}else if(strcmp(argv[i], "--draw-framerate") == 0)
{
Expand Down Expand Up @@ -95,7 +95,7 @@ bool parseArgs(int argc, char* argv[], unsigned int* width, unsigned int* height
AttachConsole(ATTACH_PARENT_PROCESS);
freopen("CONOUT$", "wb", stdout);
#endif
std::cout <<
std::cout <<
"EmulationStation, a graphical front-end for ROM browsing.\n"
"Written by Alec \"Aloshi\" Lofquist.\n"
"Version " << PROGRAM_VERSION_STRING << ", built " << PROGRAM_BUILT_STRING << "\n\n"
Expand Down Expand Up @@ -139,7 +139,7 @@ bool verifyHomeFolderExists()
return true;
}

// Returns true if everything is OK,
// Returns true if everything is OK,
bool loadSystemConfigFile(const char** errorString)
{
*errorString = NULL;
Expand Down Expand Up @@ -187,8 +187,8 @@ int main(int argc, char* argv[])
// only show the console on Windows if HideConsole is false
#ifdef WIN32
// MSVC has a "SubSystem" option, with two primary options: "WINDOWS" and "CONSOLE".
// In "WINDOWS" mode, no console is automatically created for us. This is good,
// because we can choose to only create the console window if the user explicitly
// In "WINDOWS" mode, no console is automatically created for us. This is good,
// because we can choose to only create the console window if the user explicitly
// asks for it, preventing it from flashing open and then closing.
// In "CONSOLE" mode, a console is always automatically created for us before we
// enter main. In this case, we can only hide the console after the fact, which
Expand All @@ -210,7 +210,7 @@ int main(int argc, char* argv[])
}else{
// we want to hide the console
// if we're compiled with the "WINDOWS" subsystem, this is already done.
// if we're compiled with the "CONSOLE" subsystem, a console is already created;
// if we're compiled with the "CONSOLE" subsystem, a console is already created;
// it'll flash open, but we hide it nearly immediately
if(GetConsoleWindow()) // should only pass in "CONSOLE" mode
ShowWindow(GetConsoleWindow(), SW_HIDE);
Expand Down Expand Up @@ -266,7 +266,7 @@ int main(int argc, char* argv[])
// we can't handle es_systems.cfg file problems inside ES itself, so display the error message then quit
window.pushGui(new GuiMsgBox(&window,
errorMsg,
"QUIT", [] {
"QUIT", [] {
SDL_Event* quit = new SDL_Event();
quit->type = SDL_QUIT;
SDL_PushEvent(quit);
Expand Down Expand Up @@ -302,19 +302,16 @@ int main(int argc, char* argv[])

int lastTime = SDL_GetTicks();
int ps_time = SDL_GetTicks();

bool running = true;
bool ps_standby = false;

// assuming screensaver timeout is not updated regularly.
int timeout = (unsigned int) Settings::getInstance()->getInt("ScreenSaverTime");

while(running)
{
SDL_Event event;
bool ps_standby = PowerSaver::getState() && SDL_GetTicks() - ps_time > PowerSaver::getTimeout();
if(ps_standby ? SDL_WaitEventTimeout(&event, timeout - 100) : SDL_PollEvent(&event))
bool ps_standby = PowerSaver::getState() && SDL_GetTicks() - ps_time > PowerSaver::getMode();

if(ps_standby ? SDL_WaitEventTimeout(&event, PowerSaver::getTimeout()) : SDL_PollEvent(&event))
{
do
{
Expand All @@ -337,12 +334,12 @@ int main(int argc, char* argv[])
break;
}
} while(SDL_PollEvent(&event));

// triggered if exiting from SDL_WaitEvent
if (ps_standby)
// show as if continuing from last event
lastTime = SDL_GetTicks();

// reset counter
ps_time = SDL_GetTicks();
}
Expand All @@ -359,7 +356,7 @@ int main(int argc, char* argv[])
lastTime = curTime;

// cap deltaTime
if(deltaTime > timeout || deltaTime < 0)
if((deltaTime > PowerSaver::getTimeout() && PowerSaver::getTimeout() > 0) || deltaTime < 0)
deltaTime = 1000;

window.update(deltaTime);
Expand Down
45 changes: 33 additions & 12 deletions es-core/src/PowerSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@
#include "Settings.h"
#include <string.h>

bool PowerSaver::mState = true;
int PowerSaver::mTimeout = PowerSaver::ps_default;
bool PowerSaver::mState = false;
bool PowerSaver::mRunningScreenSaver = false;

void PowerSaver::init(bool state)
int PowerSaver::mPlayNextTimeout = -1;
int PowerSaver::mScreenSaverTimeout = -1;
PowerSaver::mode PowerSaver::mMode = PowerSaver::DISABLED;

void PowerSaver::init()
{
setState(true);
updateTimeout();
updateTimeouts();
updateMode();
}

int PowerSaver::getTimeout()
{
return mTimeout;
return mRunningScreenSaver ? mPlayNextTimeout : mScreenSaverTimeout;
}

void PowerSaver::updateTimeouts()
{
mScreenSaverTimeout = (unsigned int) Settings::getInstance()->getInt("ScreenSaverTime");
mScreenSaverTimeout = mScreenSaverTimeout > 0 ? mScreenSaverTimeout - 100 : -1;
mPlayNextTimeout = 30000;
}

PowerSaver::mode PowerSaver::getMode()
{
return mMode;
}

void PowerSaver::updateTimeout()
void PowerSaver::updateMode()
{
std::string mode = Settings::getInstance()->getString("PowerSaverMode");

if (mode == "disabled") {
mTimeout = ps_disabled;
mMode = DISABLED;
} else if (mode == "instant") {
mTimeout = ps_instant;
mMode = INSTANT;
} else if (mode == "enhanced") {
mTimeout = ps_enhanced;
} else { // default
mTimeout = ps_default;
mMode = ENHANCED;
} else {
mMode = DEFAULT;
}
}

Expand All @@ -42,3 +59,7 @@ void PowerSaver::setState(bool state)
mState = ps_enabled && state;
}

void PowerSaver::runningScreenSaver(bool state)
{
mRunningScreenSaver = state;
}
23 changes: 16 additions & 7 deletions es-core/src/PowerSaver.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
class PowerSaver
{
public:
enum ps_state : int { ps_disabled = -1, ps_instant = 200, ps_enhanced = 3000, ps_default = 10000 };
static void init(bool state = true);
enum mode : int { DISABLED = -1, INSTANT = 200, ENHANCED = 3000, DEFAULT = 10000 };

static void init();

static int getTimeout();
static void updateTimeout();
static void updateTimeouts();

static mode getMode();
static void updateMode();

static bool getState();
static void setState(bool state);


static void runningScreenSaver(bool state);

private:
static bool mState;
static int mTimeout;
static bool mRunningScreenSaver;

static mode mMode;
static int mPlayNextTimeout;
static int mScreenSaverTimeout;
};

0 comments on commit 244bf47

Please sign in to comment.