Skip to content

Commit

Permalink
Add option to keep Steam alive before sleep or hibernation (#61)
Browse files Browse the repository at this point in the history
* Add option to keep Steam alive before sleep or hibernation
* Bump version
  • Loading branch information
FrogTheFrog committed Oct 18, 2023
1 parent ae2c2c2 commit 81f6532
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 30 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

cmake_minimum_required(VERSION 3.21)
project(MoonDeckBuddy
VERSION 1.5.6
VERSION 1.5.7
DESCRIPTION "A server-side buddy app to help control the PC and Steam from SteamDeck via the MoonDeck plugin."
HOMEPAGE_URL "https://github.com/FrogTheFrog/moondeck-buddy"
LANGUAGES C CXX)
Expand Down
2 changes: 1 addition & 1 deletion src/buddy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main(int argc, char* argv[])

// HERE WE GO!!! (a.k.a. starting point)
setupRoutes(new_server, pairing_manager, pc_control, sunshine_apps, app_settings.getPreferHibernation(),
app_settings.getForceBigPicture());
app_settings.getForceBigPicture(), app_settings.getCloseSteamBeforeSleep());

client_ids.load();
if (!new_server.startServer(app_settings.getPort(), ":/ssl/moondeck_cert.pem", ":/ssl/moondeck_key.pem",
Expand Down
15 changes: 9 additions & 6 deletions src/buddy/routing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void setupPairing(server::HttpServer& server, server::PairingManager& pairing_ma

//---------------------------------------------------------------------------------------------------------------------

void setupPcState(server::HttpServer& server, os::PcControl& pc_control, bool prefer_hibernation)
void setupPcState(server::HttpServer& server, os::PcControl& pc_control, bool prefer_hibernation,
bool close_steam_before_sleep)
{
server.route(
"/pcState", QHttpServerRequest::Method::Get,
Expand All @@ -158,7 +159,7 @@ void setupPcState(server::HttpServer& server, os::PcControl& pc_control, bool pr
});

server.route("/changePcState", QHttpServerRequest::Method::Post,
[&server, &pc_control, prefer_hibernation](const QHttpServerRequest& request)
[&server, &pc_control, prefer_hibernation, close_steam_before_sleep](const QHttpServerRequest& request)
{
if (!server.isAuthorized(request))
{
Expand Down Expand Up @@ -188,8 +189,9 @@ void setupPcState(server::HttpServer& server, os::PcControl& pc_control, bool pr
result = pc_control.shutdownPC(*grace_period);
break;
case ChangePcState::Suspend:
result = (prefer_hibernation && pc_control.hibernatePC(*grace_period))
|| pc_control.suspendPC(*grace_period);
result =
(prefer_hibernation && pc_control.hibernatePC(*grace_period, close_steam_before_sleep))
|| pc_control.suspendPC(*grace_period, close_steam_before_sleep);
break;
}
return QHttpServerResponse{QJsonObject{{"result", result}}};
Expand Down Expand Up @@ -362,11 +364,12 @@ void setupRouteLogging(server::HttpServer& server)
//---------------------------------------------------------------------------------------------------------------------

void setupRoutes(server::HttpServer& server, server::PairingManager& pairing_manager, os::PcControl& pc_control,
os::SunshineApps& sunshine_apps, bool prefer_hibernation, bool force_big_picture)
os::SunshineApps& sunshine_apps, bool prefer_hibernation, bool force_big_picture,
bool close_steam_before_sleep)
{
routing_internal::setupApiVersion(server);
routing_internal::setupPairing(server, pairing_manager);
routing_internal::setupPcState(server, pc_control, prefer_hibernation);
routing_internal::setupPcState(server, pc_control, prefer_hibernation, close_steam_before_sleep);
routing_internal::setupHostInfo(server, pc_control);
routing_internal::setupSteamControl(server, pc_control, force_big_picture);
routing_internal::setupResolution(server, pc_control);
Expand Down
3 changes: 2 additions & 1 deletion src/buddy/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
//---------------------------------------------------------------------------------------------------------------------

void setupRoutes(server::HttpServer& server, server::PairingManager& pairing_manager, os::PcControl& pc_control,
os::SunshineApps& sunshine_apps, bool prefer_hibernation, bool force_big_picture);
os::SunshineApps& sunshine_apps, bool prefer_hibernation, bool force_big_picture,
bool close_steam_before_sleep);
28 changes: 22 additions & 6 deletions src/lib/os/pccontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,20 @@ bool PcControl::restartPC(uint grace_period_in_sec)

//---------------------------------------------------------------------------------------------------------------------

bool PcControl::suspendPC(uint grace_period_in_sec)
bool PcControl::suspendPC(uint grace_period_in_sec, bool close_steam)
{
if (m_pc_state_handler.suspendPC(grace_period_in_sec))
{
closeSteam(std::nullopt);
restoreChangedResolution(true);
if (close_steam)
{
closeSteam(std::nullopt);
restoreChangedResolution(true);
}
else
{
endStream();
}

emit signalShowTrayMessage(
"Suspend in progress", m_app_meta.getAppName() + " is about to suspend you real hard :P",
QSystemTrayIcon::MessageIcon::Information, static_cast<int>(grace_period_in_sec) * SEC_TO_MS);
Expand All @@ -139,12 +147,20 @@ bool PcControl::suspendPC(uint grace_period_in_sec)

//---------------------------------------------------------------------------------------------------------------------

bool PcControl::hibernatePC(uint grace_period_in_sec)
bool PcControl::hibernatePC(uint grace_period_in_sec, bool close_steam)
{
if (m_pc_state_handler.hibernatePC(grace_period_in_sec))
{
closeSteam(std::nullopt);
restoreChangedResolution(true);
if (close_steam)
{
closeSteam(std::nullopt);
restoreChangedResolution(true);
}
else
{
endStream();
}

emit signalShowTrayMessage(
"Hibernation in progress", m_app_meta.getAppName() + " is about to put you into hard sleep :O",
QSystemTrayIcon::MessageIcon::Information, static_cast<int>(grace_period_in_sec) * SEC_TO_MS);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/os/pccontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class PcControl : public QObject

bool shutdownPC(uint grace_period_in_sec);
bool restartPC(uint grace_period_in_sec);
bool suspendPC(uint grace_period_in_sec);
bool hibernatePC(uint grace_period_in_sec);
bool suspendPC(uint grace_period_in_sec, bool close_steam);
bool hibernatePC(uint grace_period_in_sec, bool close_steam);

bool endStream();

Expand Down
42 changes: 29 additions & 13 deletions src/lib/utils/appsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ AppSettings::AppSettings(const QString& filepath)
, m_prefer_hibernation{false}
, m_ssl_protocol{QSsl::SecureProtocols}
, m_force_big_picture{true}
, m_close_steam_before_sleep{true}
, m_nvidia_reset_mouse_acceleration_after_stream_end_hack{false}
{
if (!parseSettingsFile(filepath))
Expand Down Expand Up @@ -119,6 +120,13 @@ bool AppSettings::getForceBigPicture() const

//---------------------------------------------------------------------------------------------------------------------

bool AppSettings::getCloseSteamBeforeSleep() const
{
return m_close_steam_before_sleep;
}

//---------------------------------------------------------------------------------------------------------------------

// NOLINTNEXTLINE(*-function-cognitive-complexity)
bool AppSettings::parseSettingsFile(const QString& filepath)
{
Expand All @@ -141,21 +149,22 @@ bool AppSettings::parseSettingsFile(const QString& filepath)
}
else if (!json_data.isEmpty())
{
const auto obj_v = json_data.object();
const auto port_v = obj_v.value(QLatin1String("port"));
const auto logging_rules_v = obj_v.value(QLatin1String("logging_rules"));
const auto handled_displays_v = obj_v.value(QLatin1String("handled_displays"));
const auto sunshine_apps_filepath_v = obj_v.value(QLatin1String("sunshine_apps_filepath"));
const auto prefer_hibernation_v = obj_v.value(QLatin1String("prefer_hibernation"));
const auto ssl_protocol_v = obj_v.value(QLatin1String("ssl_protocol"));
const auto force_big_picture_v = obj_v.value(QLatin1String("force_big_picture"));
const auto obj_v = json_data.object();
const auto port_v = obj_v.value(QLatin1String("port"));
const auto logging_rules_v = obj_v.value(QLatin1String("logging_rules"));
const auto handled_displays_v = obj_v.value(QLatin1String("handled_displays"));
const auto sunshine_apps_filepath_v = obj_v.value(QLatin1String("sunshine_apps_filepath"));
const auto prefer_hibernation_v = obj_v.value(QLatin1String("prefer_hibernation"));
const auto ssl_protocol_v = obj_v.value(QLatin1String("ssl_protocol"));
const auto force_big_picture_v = obj_v.value(QLatin1String("force_big_picture"));
const auto close_steam_before_sleep_v = obj_v.value(QLatin1String("close_steam_before_sleep"));

// TODO: remove
const auto nvidia_reset_mouse_acceleration_after_stream_end_hack_v =
obj_v.value(QLatin1String("nvidia_reset_mouse_acceleration_after_stream_end_hack"));

// TODO: dec. once removed
constexpr int current_entries{8};
constexpr int current_entries{9};
int valid_entries{0};

if (port_v.isDouble())
Expand Down Expand Up @@ -230,6 +239,12 @@ bool AppSettings::parseSettingsFile(const QString& filepath)
valid_entries++;
}

if (close_steam_before_sleep_v.isBool())
{
m_close_steam_before_sleep = close_steam_before_sleep_v.toBool();
valid_entries++;
}

if (nvidia_reset_mouse_acceleration_after_stream_end_hack_v.isBool())
{
m_nvidia_reset_mouse_acceleration_after_stream_end_hack =
Expand All @@ -254,10 +269,11 @@ void AppSettings::saveDefaultFile(const QString& filepath) const
obj["logging_rules"] = m_logging_rules;
obj["handled_displays"] =
QJsonArray::fromStringList({std::cbegin(m_handled_displays), std::cend(m_handled_displays)});
obj["sunshine_apps_filepath"] = m_sunshine_apps_filepath;
obj["prefer_hibernation"] = m_prefer_hibernation;
obj["ssl_protocol"] = QStringLiteral("SecureProtocols");
obj["force_big_picture"] = m_force_big_picture;
obj["sunshine_apps_filepath"] = m_sunshine_apps_filepath;
obj["prefer_hibernation"] = m_prefer_hibernation;
obj["ssl_protocol"] = QStringLiteral("SecureProtocols");
obj["force_big_picture"] = m_force_big_picture;
obj["close_steam_before_sleep"] = m_close_steam_before_sleep;
obj["nvidia_reset_mouse_acceleration_after_stream_end_hack"] =
m_nvidia_reset_mouse_acceleration_after_stream_end_hack;

Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils/appsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AppSettings
bool getPreferHibernation() const;
QSsl::SslProtocol getSslProtocol() const;
bool getForceBigPicture() const;
bool getCloseSteamBeforeSleep() const;

private:
bool parseSettingsFile(const QString& filepath);
Expand All @@ -36,6 +37,7 @@ class AppSettings
bool m_prefer_hibernation;
QSsl::SslProtocol m_ssl_protocol;
bool m_force_big_picture;
bool m_close_steam_before_sleep;

public:
// TODO: remove
Expand Down

0 comments on commit 81f6532

Please sign in to comment.