From ed95b50f7a0fa7d9b491eb9af13295baa2e3602b Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 3 Oct 2023 23:14:41 -0500 Subject: [PATCH] Automatically treat launcher-type apps as detached --- docs/source/about/usage.rst | 1 + src/process.cpp | 12 ++++++++++++ src/process.h | 2 ++ src_assets/common/assets/web/apps.html | 23 +++++++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/docs/source/about/usage.rst b/docs/source/about/usage.rst index 80d66a03..b59df128 100644 --- a/docs/source/about/usage.rst +++ b/docs/source/about/usage.rst @@ -254,6 +254,7 @@ Application List - ``image-path`` - The full path to the cover art image to use. - ``name`` - The name of the application/game - ``output`` - The file where the output of the command is stored + - ``auto-detach`` - Specifies whether the app should be treated as detached if it exits quickly - ``prep-cmd`` - A list of commands to be run before/after the application - If any of the prep-commands fail, starting the application is aborted diff --git a/src/process.cpp b/src/process.cpp index 8e8d2b35..227c6fda 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -227,6 +227,8 @@ namespace proc { } } + _app_launch_time = std::chrono::steady_clock::now(); + fg.disable(); return 0; @@ -237,9 +239,17 @@ namespace proc { if (placebo || _process.running()) { return _app_id; } + else if (_app.auto_detach && _process.native_exit_code() == 0 && + std::chrono::steady_clock::now() - _app_launch_time < 5s) { + BOOST_LOG(info) << "App exited gracefully within 5 seconds of launch. Treating the app as a detached command."sv; + BOOST_LOG(info) << "Adjust this behavior in the Applications tab or apps.json if this is not what you want."sv; + placebo = true; + return _app_id; + } // Perform cleanup actions now if needed if (_process) { + BOOST_LOG(info) << "App exited with code ["sv << _process.native_exit_code() << ']'; terminate(); } @@ -548,6 +558,7 @@ namespace proc { auto image_path = app_node.get_optional("image-path"s); auto working_dir = app_node.get_optional("working-dir"s); auto elevated = app_node.get_optional("elevated"s); + auto auto_detach = app_node.get_optional("auto-detach"s); std::vector prep_cmds; if (!exclude_global_prep.value_or(false)) { @@ -606,6 +617,7 @@ namespace proc { } ctx.elevated = elevated.value_or(false); + ctx.auto_detach = auto_detach.value_or(true); auto possible_ids = calculate_app_id(name, ctx.image_path, i++); if (ids.count(std::get<0>(possible_ids)) == 0) { diff --git a/src/process.h b/src/process.h index 5e658146..c1ff28af 100644 --- a/src/process.h +++ b/src/process.h @@ -52,6 +52,7 @@ namespace proc { std::string image_path; std::string id; bool elevated; + bool auto_detach; }; class proc_t { @@ -93,6 +94,7 @@ namespace proc { boost::process::environment _env; std::vector _apps; ctx_t _app; + std::chrono::steady_clock::time_point _app_launch_time; // If no command associated with _app_id, yet it's still running bool placebo {}; diff --git a/src_assets/common/assets/web/apps.html b/src_assets/common/assets/web/apps.html index f8257cca..bd09051d 100644 --- a/src_assets/common/assets/web/apps.html +++ b/src_assets/common/assets/web/apps.html @@ -229,6 +229,25 @@ permissions to run properly. + +
+ + +
+ This will attempt to automatically detect launcher-type apps that close + quickly after launching another program or instance of themselves. When + a launcher-type app is detected, it is treated as a detached app. +
+
@@ -379,6 +398,7 @@ index: -1, "exclude-global-prep-cmd": false, elevated: false, + "auto-detach": true, "prep-cmd": [], detached: [], "image-path": "" @@ -398,6 +418,9 @@ if(this.editForm["elevated"] === undefined && this.platform === 'windows'){ this.$set(this.editForm, "elevated", false); } + if(this.editForm["auto-detach"] === undefined){ + this.$set(this.editForm, "auto-detach", true); + } this.showEditForm = true; }, showDeleteForm(id) {