diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-12-29 16:00:52 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-12-29 16:00:52 +0100 |
commit | 4b506e865a6997fd24b39335a96cfeea7d14c209 (patch) | |
tree | 1f0735e62e678e913ec40d47a69938957cd3ce61 | |
parent | 78f44a9486bbc682ea723bb76a24091421c41d78 (diff) |
Make systemctl work in flatpak
-rw-r--r-- | include/Process.hpp | 4 | ||||
-rw-r--r-- | src/Process.cpp | 33 | ||||
-rw-r--r-- | src/gui/GlobalSettingsPage.cpp | 4 |
3 files changed, 39 insertions, 2 deletions
diff --git a/include/Process.hpp b/include/Process.hpp index 6fd8c31..9662126 100644 --- a/include/Process.hpp +++ b/include/Process.hpp @@ -17,5 +17,9 @@ namespace gsr { pid_t exec_program(const char **args, int *read_fd); // Arguments ending with NULL. Returns the exit status of the program or -1 on error int exec_program_get_stdout(const char **args, std::string &result); + // Arguments ending with NULL. Returns the exit status of the program or -1 on error. + // This works the same as |exec_program_get_stdout|, except on flatpak where this runs the program on the + // host machine with flatpak-spawn --host + int exec_program_on_host_get_stdout(const char **args, std::string &result); pid_t pidof(const char *process_name); }
\ No newline at end of file diff --git a/src/Process.cpp b/src/Process.cpp index 2a704de..347c485 100644 --- a/src/Process.cpp +++ b/src/Process.cpp @@ -31,6 +31,15 @@ namespace gsr { return true; } + static int count_num_args(const char **args) { + int num_args = 0; + while(*args) { + ++num_args; + ++args; + } + return num_args; + } + bool exec_program_daemonized(const char **args) { /* 1 argument */ if(args[0] == nullptr) @@ -143,6 +152,30 @@ namespace gsr { return exit_status; } + int exec_program_on_host_get_stdout(const char **args, std::string &result) { + if(count_num_args(args) > 64 - 3) { + fprintf(stderr, "Error: too many arguments when trying to launch \"%s\"\n", args[0]); + return -1; + } + + const bool inside_flatpak = getenv("FLATPAK_ID") != NULL; + if(inside_flatpak) { + // Assumes programs wont need more than 64 - 3 args + const char *modified_args[64] = { "flatpak-spawn", "--host", "--" }; + for(int i = 3; i < 64; ++i) { + const char *arg = args[i - 3]; + if(!arg) { + modified_args[i] = nullptr; + break; + } + modified_args[i] = arg; + } + return exec_program_get_stdout(modified_args, result); + } else { + return exec_program_get_stdout(args, result); + } + } + // |output_buffer| should be at least PATH_MAX in size bool read_cmdline_arg0(const char *filepath, char *output_buffer, int output_buffer_size) { output_buffer[0] = '\0'; diff --git a/src/gui/GlobalSettingsPage.cpp b/src/gui/GlobalSettingsPage.cpp index 9b927f0..7e79c16 100644 --- a/src/gui/GlobalSettingsPage.cpp +++ b/src/gui/GlobalSettingsPage.cpp @@ -77,7 +77,7 @@ namespace gsr { const char *args[] = { "systemctl", enable ? "enable" : "disable", "--user", "gpu-screen-recorder-ui", nullptr }; std::string stdout_str; - const int exit_status = exec_program_get_stdout(args, stdout_str); + const int exit_status = exec_program_on_host_get_stdout(args, stdout_str); if(on_startup_changed) on_startup_changed(enable, exit_status); }; @@ -109,7 +109,7 @@ namespace gsr { const char *args[] = { "systemctl", "is-enabled", "--quiet", "--user", "gpu-screen-recorder-ui", nullptr }; std::string stdout_str; - const int exit_status = exec_program_get_stdout(args, stdout_str); + const int exit_status = exec_program_on_host_get_stdout(args, stdout_str); startup_radio_button_ptr->set_selected_item(exit_status == 0 ? "start_on_system_startup" : "dont_start_on_system_startup", false, false); } |