Skip to content

Commit

Permalink
Automatically treat launcher-type apps as detached
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Oct 4, 2023
1 parent cfd78b5 commit ed95b50
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/about/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ namespace proc {
}
}

_app_launch_time = std::chrono::steady_clock::now();

fg.disable();

return 0;
Expand All @@ -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();
}

Expand Down Expand Up @@ -548,6 +558,7 @@ namespace proc {
auto image_path = app_node.get_optional<std::string>("image-path"s);
auto working_dir = app_node.get_optional<std::string>("working-dir"s);
auto elevated = app_node.get_optional<bool>("elevated"s);
auto auto_detach = app_node.get_optional<bool>("auto-detach"s);

std::vector<proc::cmd_t> prep_cmds;
if (!exclude_global_prep.value_or(false)) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace proc {
std::string image_path;
std::string id;
bool elevated;
bool auto_detach;
};

class proc_t {
Expand Down Expand Up @@ -93,6 +94,7 @@ namespace proc {
boost::process::environment _env;
std::vector<ctx_t> _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 {};
Expand Down
23 changes: 23 additions & 0 deletions src_assets/common/assets/web/apps.html
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ <h1>Applications</h1>
permissions to run properly.
</div>
</div>
<!-- auto-detach -->
<div class="mb-3 form-check">
<label for="autoDetach" class="form-check-label"
>Continue streaming if the application exits quickly</label
>
<input
type="checkbox"
class="form-check-input"
id="autoDetach"
v-model="editForm['auto-detach']"
true-value="true"
false-value="false"
/>
<div class="form-text">
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.
</div>
</div>
<!-- Image path -->
<div class="mb-3">
<label for="appImagePath" class="form-label">Image</label>
Expand Down Expand Up @@ -379,6 +398,7 @@ <h4 class="modal-title">Covers Found</h4>
index: -1,
"exclude-global-prep-cmd": false,
elevated: false,
"auto-detach": true,
"prep-cmd": [],
detached: [],
"image-path": ""
Expand All @@ -398,6 +418,9 @@ <h4 class="modal-title">Covers Found</h4>
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) {
Expand Down

0 comments on commit ed95b50

Please sign in to comment.