From 13984f8636f174944eb99c7acbfda1c53d5faa76 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 27 Mar 2026 02:18:06 +0100 Subject: [PATCH] Replace flatpak/native autostart with the current gsr-ui type when launching gsr-ui, for users that switch between them --- include/Utils.hpp | 1 + src/Overlay.cpp | 2 -- src/Utils.cpp | 42 +++++++++++++++++++++++++++++++----------- src/main.cpp | 3 +++ 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/include/Utils.hpp b/include/Utils.hpp index 8f6c8c3..365c25b 100644 --- a/include/Utils.hpp +++ b/include/Utils.hpp @@ -41,6 +41,7 @@ namespace gsr { bool is_xdg_autostart_enabled(); // Returns 0 on success int set_xdg_autostart(bool enable); + void replace_xdg_autostart_with_current_gsr_type(); // Systemd user service helpers bool is_systemd_service_enabled(const char *service_name); diff --git a/src/Overlay.cpp b/src/Overlay.cpp index 882f718..2e9052a 100644 --- a/src/Overlay.cpp +++ b/src/Overlay.cpp @@ -1864,8 +1864,6 @@ namespace gsr { exit_reason = "back-to-old-ui"; else exit_reason = "exit"; - - set_xdg_autostart(false); exit(); } diff --git a/src/Utils.cpp b/src/Utils.cpp index 59720ae..86ca6d4 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -1,14 +1,27 @@ #include "../include/Utils.hpp" +#include "../include/Process.hpp" #include #include +#include #include #include #include #include #include -#include "../include/Process.hpp" namespace gsr { + static std::optional get_xdg_autostart_content() { + const char *args[] = { + "/bin/sh", "-c", + "cat \"${XDG_CONFIG_HOME:-$HOME/.config}/autostart/gpu-screen-recorder-ui.desktop\"", + nullptr + }; + std::string output; + if(exec_program_on_host_get_stdout(args, output, true) != 0) + return std::nullopt; + return output; + } + void string_split_char(std::string_view str, char delimiter, StringSplitCallback callback_func) { size_t index = 0; while(index < str.size()) { @@ -241,15 +254,8 @@ namespace gsr { } bool is_xdg_autostart_enabled() { - const char *args[] = { - "/bin/sh", "-c", - "cat \"${XDG_CONFIG_HOME:-$HOME/.config}/autostart/gpu-screen-recorder-ui.desktop\"", - nullptr - }; - std::string output; - if(exec_program_on_host_get_stdout(args, output, true) != 0) - return false; - return output.find("Hidden=true") == std::string::npos; + const std::optional output = get_xdg_autostart_content(); + return output.has_value() && output.value().find("Hidden=true") == std::string::npos; } int set_xdg_autostart(bool enable) { @@ -261,7 +267,8 @@ namespace gsr { return 67; } - const char *exec_line = (getenv("FLATPAK_ID") != nullptr) + const bool is_flatpak = getenv("FLATPAK_ID") != nullptr; + const char *exec_line = is_flatpak ? "Exec=flatpak run com.dec05eba.gpu_screen_recorder gsr-ui launch-daemon" : "Exec=gsr-ui launch-daemon"; @@ -286,6 +293,19 @@ namespace gsr { return exec_program_on_host_get_stdout(args, dummy, true); } + void replace_xdg_autostart_with_current_gsr_type() { + const std::optional output = get_xdg_autostart_content(); + if(!output.has_value()) + return; + + const bool is_flatpak = getenv("FLATPAK_ID") != nullptr; + const bool is_exec_flatpak = output.value().find("flatpak run") != std::string::npos; + if(is_flatpak != is_exec_flatpak) { + const bool is_autostart_enabled = output.value().find("Hidden=true") == std::string::npos; + set_xdg_autostart(is_autostart_enabled); + } + } + bool is_systemd_service_enabled(const char *service_name) { const char *args[] = { "systemctl", "--user", "is-enabled", service_name, nullptr }; std::string output; diff --git a/src/main.cpp b/src/main.cpp index b5245d4..99bde68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -319,6 +319,8 @@ int main(int argc, char **argv) { } } + gsr::replace_xdg_autostart_with_current_gsr_type(); + // TODO: Add hotkeys in Overlay when using x11 global hotkeys. The hotkeys in Overlay should duplicate each key that is used for x11 global hotkeys. std::string exit_reason; @@ -344,6 +346,7 @@ int main(int argc, char **argv) { mgl_deinit(); if(exit_reason == "back-to-old-ui") { + gsr::set_xdg_autostart(false); const char *args[] = { "gpu-screen-recorder-gtk", "use-old-ui", nullptr }; execvp(args[0], (char* const*)args); return 0;