From eb4ce76f013d4df74897466702769086329911c3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 16 Jan 2025 19:34:47 +0100 Subject: Install flatpak version of the systemd service if missing or remove it if not using flatpak version --- src/main.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index c81bc8c..dd507fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -189,6 +190,48 @@ static bool is_gsr_ui_virtual_keyboard_running() { return virtual_keyboard_running; } +static void install_flatpak_systemd_service() { + const bool systemd_service_exists = system( + "data_home=$(flatpak-spawn --host -- /bin/sh -c 'echo \"${XDG_DATA_HOME:-$HOME/.local/share}\"') && " + "flatpak-spawn --host -- ls \"$data_home/systemd/user/gpu-screen-recorder-ui.service\"") == 0; + if(systemd_service_exists) + return; + + bool service_install_successful = (system( + "data_home=$(flatpak-spawn --host -- /bin/sh -c 'echo \"${XDG_DATA_HOME:-$HOME/.local/share}\"') && " + "flatpak-spawn --host -- install -Dm644 /var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/share/gpu-screen-recorder/gpu-screen-recorder-ui.service \"$data_home/systemd/user/gpu-screen-recorder-ui.service\"") == 0); + service_install_successful &= (system("flatpak-spawn --host -- systemctl --user daemon-reload") == 0); + if(service_install_successful) + fprintf(stderr, "Info: the systemd service file was missing. It has now been installed\n"); + else + fprintf(stderr, "Error: the systemd service file is missing and failed to install it again\n"); +} + +static void remove_flatpak_systemd_service() { + char systemd_service_path[PATH_MAX]; + const char *xdg_data_home = getenv("XDG_DATA_HOME"); + const char *home = getenv("HOME"); + if(xdg_data_home) { + snprintf(systemd_service_path, sizeof(systemd_service_path), "%s/systemd/user/gpu-screen-recorder-ui.service", xdg_data_home); + } else if(home) { + snprintf(systemd_service_path, sizeof(systemd_service_path), "%s/.local/share/systemd/user/gpu-screen-recorder-ui.service", home); + } else { + fprintf(stderr, "Error: failed to get user home directory\n"); + return; + } + + if(access(systemd_service_path, F_OK) != 0) + return; + + remove(systemd_service_path); + system("systemctl --user daemon-reload"); + fprintf(stderr, "Info: conflicting flatpak version of the systemd service for gsr-ui was found at \"%s\", it has now been removed\n", systemd_service_path); +} + +static bool is_flatpak() { + return getenv("FLATPAK_ID") != nullptr; +} + static void usage() { printf("usage: gsr-ui [action]\n"); printf("OPTIONS:\n"); @@ -228,6 +271,11 @@ int main(int argc, char **argv) { usage(); } + if(is_flatpak()) + install_flatpak_systemd_service(); + else + remove_flatpak_systemd_service(); + // TODO: This is a shitty method to detect if multiple instances of gsr-ui is running but this will work properly even in flatpak // that uses pid sandboxing. Replace this with a better method once we no longer rely on linux global hotkeys on some platform. // TODO: This method doesn't work when disabling hotkeys and the method below with pidof gsr-ui doesn't work in flatpak. -- cgit v1.2.3