From 6a7446780b401c721a64fb28ce42094243448ff4 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 9 Apr 2023 22:59:55 -0700 Subject: [PATCH 1/6] hide terminal console when running on steam deck --- docs/changelog.txt | 1 + library/CMakeLists.txt | 2 + library/Core.cpp | 7 ++++ library/include/modules/DFSteam.h | 32 ++++++++++++++++ library/modules/DFSteam.cpp | 64 +++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 library/include/modules/DFSteam.h create mode 100644 library/modules/DFSteam.cpp diff --git a/docs/changelog.txt b/docs/changelog.txt index adc4ca786..e63b80bd7 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -52,6 +52,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - Mods: scripts from only the most recent version of an installed mod are added to the script path - Mods: give active mods a chance to reattach their load hooks when a world is reloaded - `gui/control-panel`: bugfix services are now enabled by default +- Core: hide DFHack terminal console by default when running on Steam Deck ## Documentation diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 92db43563..a3fcb8b6f 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -125,6 +125,7 @@ set(MODULE_HEADERS include/modules/Burrows.h include/modules/Constructions.h include/modules/DFSDL.h + include/modules/DFSteam.h include/modules/Designations.h include/modules/EventManager.h include/modules/Filesystem.h @@ -154,6 +155,7 @@ set(MODULE_SOURCES modules/Burrows.cpp modules/Constructions.cpp modules/DFSDL.cpp + modules/DFSteam.cpp modules/Designations.cpp modules/EventManager.cpp modules/Filesystem.cpp diff --git a/library/Core.cpp b/library/Core.cpp index 97c69946e..d03e8c632 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -49,6 +49,7 @@ distribution. #include "PluginManager.h" #include "ModuleFactory.h" #include "modules/DFSDL.h" +#include "modules/DFSteam.h" #include "modules/EventManager.h" #include "modules/Filesystem.h" #include "modules/Gui.h" @@ -1303,6 +1304,10 @@ static void run_dfhack_init(color_ostream &out, Core *core) return; } + // if we're running on Steam Deck, hide the terminal by default + if (DFSteam::DFIsSteamRunningOnSteamDeck()) + core->getConsole().hide(); + // load baseline defaults core->loadScriptFile(out, CONFIG_PATH + "init/default.dfhack.init", false); @@ -1668,6 +1673,8 @@ bool Core::Init() fatal("cannot bind SDL libraries"); return false; } + if (DFSteam::init(con)) + std::cerr << "Found Steam.\n"; std::cerr << "Initializing textures.\n"; Textures::init(con); // create mutex for syncing with interactive tasks diff --git a/library/include/modules/DFSteam.h b/library/include/modules/DFSteam.h new file mode 100644 index 000000000..3144830da --- /dev/null +++ b/library/include/modules/DFSteam.h @@ -0,0 +1,32 @@ +#pragma once + +#include "ColorText.h" +#include "Export.h" + +namespace DFHack +{ + +/** + * The DFSteam module - provides access to Steam functions without actually + * requiring build-time linkage to Steam + * \ingroup grp_modules + * \ingroup grp_dfsdl + */ +namespace DFSteam +{ + +/** + * Call this on DFHack init so we can load the function pointers. Returns false on + * failure. + */ +bool init(DFHack::color_ostream& out); + +/** + * Call this when DFHack is being unloaded. + */ +void cleanup(); + +DFHACK_EXPORT bool DFIsSteamRunningOnSteamDeck(); + +} +} diff --git a/library/modules/DFSteam.cpp b/library/modules/DFSteam.cpp new file mode 100644 index 000000000..d32eec961 --- /dev/null +++ b/library/modules/DFSteam.cpp @@ -0,0 +1,64 @@ +#include "Internal.h" + +#include "modules/DFSteam.h" + +#include "Debug.h" +#include "PluginManager.h" + +namespace DFHack +{ +DBG_DECLARE(core, dfsteam, DebugCategory::LINFO); +} + +using namespace DFHack; + +static DFLibrary* g_steam_handle = nullptr; +static const std::vector STEAM_LIBS { + "steam_api64.dll", + "steam_api", // TODO: validate this on OSX + "libsteam_api.so" // TODO: validate this on Linux +}; + +bool (*g_SteamAPI_Init)() = nullptr; +bool (*g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck)() = nullptr; + +bool DFSteam::init(color_ostream& out) { + for (auto& lib_str : STEAM_LIBS) { + if ((g_steam_handle = OpenPlugin(lib_str.c_str()))) + break; + } + if (!g_steam_handle) { + DEBUG(dfsteam, out).print("steam library not found; stubbing calls\n"); + return false; + } + +#define bind(handle, name) \ + g_##name = (decltype(g_##name))LookupPlugin(handle, #name); \ + if (!g_##name) { \ + WARN(dfsteam, out).print("steam library function not found: " #name "\n"); \ + } + + bind(g_steam_handle, SteamAPI_Init); + if (!g_SteamAPI_Init || !g_SteamAPI_Init()) + return false; + + bind(g_steam_handle, SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck); +#undef bind + + DEBUG(dfsteam, out).print("steam library linked\n"); + return true; +} + +void DFSteam::cleanup() { + if (!g_steam_handle) + return; + + ClosePlugin(g_steam_handle); + g_steam_handle = nullptr; +} + +bool DFSteam::DFIsSteamRunningOnSteamDeck() { + if (!g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck) + return false; + return g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck(); +} From f0d19c93631b32ea96d078985502a3244897b104 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 9 Apr 2023 23:30:23 -0700 Subject: [PATCH 2/6] add note about dfhooks --- library/modules/DFSteam.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/modules/DFSteam.cpp b/library/modules/DFSteam.cpp index d32eec961..8cc921608 100644 --- a/library/modules/DFSteam.cpp +++ b/library/modules/DFSteam.cpp @@ -39,6 +39,8 @@ bool DFSteam::init(color_ostream& out) { } bind(g_steam_handle, SteamAPI_Init); + + // TODO: can we remove this initialization of the Steam API once we move to dfhooks? if (!g_SteamAPI_Init || !g_SteamAPI_Init()) return false; From e4777d268836d34f1a11ff376dd26cb4a9130c0a Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 10 Apr 2023 00:00:47 -0700 Subject: [PATCH 3/6] add shutdown and cleanup logic --- library/Core.cpp | 1 + library/modules/DFSteam.cpp | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/library/Core.cpp b/library/Core.cpp index d03e8c632..b1fe2d389 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -2281,6 +2281,7 @@ int Core::Shutdown ( void ) allModules.clear(); Textures::cleanup(); DFSDL::cleanup(); + DFSteam::cleanup(); memset(&(s_mods), 0, sizeof(s_mods)); d.reset(); return -1; diff --git a/library/modules/DFSteam.cpp b/library/modules/DFSteam.cpp index 8cc921608..f58bc7cb4 100644 --- a/library/modules/DFSteam.cpp +++ b/library/modules/DFSteam.cpp @@ -20,6 +20,7 @@ static const std::vector STEAM_LIBS { }; bool (*g_SteamAPI_Init)() = nullptr; +void (*g_SteamAPI_Shutdown)() = nullptr; bool (*g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck)() = nullptr; bool DFSteam::init(color_ostream& out) { @@ -39,9 +40,10 @@ bool DFSteam::init(color_ostream& out) { } bind(g_steam_handle, SteamAPI_Init); + bind(g_steam_handle, SteamAPI_Shutdown); // TODO: can we remove this initialization of the Steam API once we move to dfhooks? - if (!g_SteamAPI_Init || !g_SteamAPI_Init()) + if (!g_SteamAPI_Init || !g_SteamAPI_Shutdown || !g_SteamAPI_Init()) return false; bind(g_steam_handle, SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck); @@ -55,6 +57,9 @@ void DFSteam::cleanup() { if (!g_steam_handle) return; + if (g_SteamAPI_Shutdown) + g_SteamAPI_Shutdown(); + ClosePlugin(g_steam_handle); g_steam_handle = nullptr; } From ce017ee4a88648809d789c57ff3504f00e91870e Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Mon, 10 Apr 2023 03:01:36 -0500 Subject: [PATCH 4/6] properly callIs SteamRunningOnSteamDeck --- library/modules/DFSteam.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/library/modules/DFSteam.cpp b/library/modules/DFSteam.cpp index f58bc7cb4..1226f2a27 100644 --- a/library/modules/DFSteam.cpp +++ b/library/modules/DFSteam.cpp @@ -21,7 +21,8 @@ static const std::vector STEAM_LIBS { bool (*g_SteamAPI_Init)() = nullptr; void (*g_SteamAPI_Shutdown)() = nullptr; -bool (*g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck)() = nullptr; +void* (*g_SteamInternal_FindOrCreateUserInterface)(int, char *) = nullptr; +bool (*g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck)(void*) = nullptr; bool DFSteam::init(color_ostream& out) { for (auto& lib_str : STEAM_LIBS) { @@ -46,6 +47,7 @@ bool DFSteam::init(color_ostream& out) { if (!g_SteamAPI_Init || !g_SteamAPI_Shutdown || !g_SteamAPI_Init()) return false; + bind(g_steam_handle, SteamInternal_FindOrCreateUserInterface); bind(g_steam_handle, SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck); #undef bind @@ -67,5 +69,11 @@ void DFSteam::cleanup() { bool DFSteam::DFIsSteamRunningOnSteamDeck() { if (!g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck) return false; - return g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck(); + + if (!g_SteamInternal_FindOrCreateUserInterface) + return false; + + void* SteamUtils = g_SteamInternal_FindOrCreateUserInterface(0, "SteamUtils010"); + + return g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck(SteamUtils); } From 836a3edcb958959ba9a4157b7419dacfb431e617 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 10 Apr 2023 01:12:43 -0700 Subject: [PATCH 5/6] add some more logging --- library/modules/DFSteam.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/modules/DFSteam.cpp b/library/modules/DFSteam.cpp index 1226f2a27..4c45f1114 100644 --- a/library/modules/DFSteam.cpp +++ b/library/modules/DFSteam.cpp @@ -21,7 +21,7 @@ static const std::vector STEAM_LIBS { bool (*g_SteamAPI_Init)() = nullptr; void (*g_SteamAPI_Shutdown)() = nullptr; -void* (*g_SteamInternal_FindOrCreateUserInterface)(int, char *) = nullptr; +void* (*g_SteamInternal_FindOrCreateUserInterface)(int, char*) = nullptr; bool (*g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck)(void*) = nullptr; bool DFSteam::init(color_ostream& out) { @@ -44,8 +44,10 @@ bool DFSteam::init(color_ostream& out) { bind(g_steam_handle, SteamAPI_Shutdown); // TODO: can we remove this initialization of the Steam API once we move to dfhooks? - if (!g_SteamAPI_Init || !g_SteamAPI_Shutdown || !g_SteamAPI_Init()) + if (!g_SteamAPI_Init || !g_SteamAPI_Shutdown || !g_SteamAPI_Init()) { + DEBUG(dfsteam, out).print("steam detected but cannot be initialized\n"); return false; + } bind(g_steam_handle, SteamInternal_FindOrCreateUserInterface); bind(g_steam_handle, SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck); From 17373dcffd6ec40cc1c8bdb105e1e92adff7f1ec Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 10 Apr 2023 01:16:22 -0700 Subject: [PATCH 6/6] constify! --- library/modules/DFSteam.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/modules/DFSteam.cpp b/library/modules/DFSteam.cpp index 4c45f1114..b27cc1744 100644 --- a/library/modules/DFSteam.cpp +++ b/library/modules/DFSteam.cpp @@ -21,7 +21,7 @@ static const std::vector STEAM_LIBS { bool (*g_SteamAPI_Init)() = nullptr; void (*g_SteamAPI_Shutdown)() = nullptr; -void* (*g_SteamInternal_FindOrCreateUserInterface)(int, char*) = nullptr; +void* (*g_SteamInternal_FindOrCreateUserInterface)(int, const char*) = nullptr; bool (*g_SteamAPI_ISteamUtils_IsSteamRunningOnSteamDeck)(void*) = nullptr; bool DFSteam::init(color_ostream& out) {