diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-12-29 15:21:38 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-12-29 15:21:38 +0100 |
commit | b96b877a1a5c96d3d8982e43637d4223fdf99c14 (patch) | |
tree | 195bb3163e288c6e9d450ecfabb785819c6cfe65 | |
parent | c5c79bec64a991f350632f02f59d25c3d61d01fc (diff) |
Only allow one instance of gsr-ui to run
-rw-r--r-- | TODO | 4 | ||||
-rw-r--r-- | include/Process.hpp | 3 | ||||
-rw-r--r-- | src/Process.cpp | 47 | ||||
-rw-r--r-- | src/main.cpp | 8 |
4 files changed, 54 insertions, 8 deletions
@@ -117,4 +117,6 @@ All steam game names by ID are available at https://api.steampowered.com/ISteamA Dont put widget position to int position when scrolling. This makes the UI jitter when it's coming to a halt. -Show a popup asking if the user wants to add the program to system startup when launching the program, with a dismiss option and "Do not show again".
\ No newline at end of file +Show a popup asking if the user wants to add the program to system startup when launching the program, with a dismiss option and "Do not show again". + +Show warning if another instance of gpu screen recorder is already running when starting recording?
\ No newline at end of file diff --git a/include/Process.hpp b/include/Process.hpp index 40373b5..6fd8c31 100644 --- a/include/Process.hpp +++ b/include/Process.hpp @@ -17,6 +17,5 @@ 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); - // |output_buffer| should be at least PATH_MAX in size - bool read_cmdline_arg0(const char *filepath, char *output_buffer); + pid_t pidof(const char *process_name); }
\ No newline at end of file diff --git a/src/Process.cpp b/src/Process.cpp index a9c5103..2a704de 100644 --- a/src/Process.cpp +++ b/src/Process.cpp @@ -22,6 +22,15 @@ namespace gsr { fprintf(stderr, "\n"); } + static bool is_number(const char *str) { + for(int i = 0; str[i]; ++i) { + char c = str[i]; + if(c < '0' || c > '9') + return false; + } + return true; + } + bool exec_program_daemonized(const char **args) { /* 1 argument */ if(args[0] == nullptr) @@ -134,7 +143,8 @@ namespace gsr { return exit_status; } - bool read_cmdline_arg0(const char *filepath, char *output_buffer) { + // |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'; const char *arg0_end = NULL; @@ -151,13 +161,40 @@ namespace gsr { if(!arg0_end) goto err; - memcpy(output_buffer, buffer, arg0_end - buffer); - output_buffer[arg0_end - buffer] = '\0'; - close(fd); - return true; + if((arg0_end - buffer) + 1 <= output_buffer_size) { + memcpy(output_buffer, buffer, arg0_end - buffer); + output_buffer[arg0_end - buffer] = '\0'; + close(fd); + return true; + } err: close(fd); return false; } + + pid_t pidof(const char *process_name) { + pid_t result = -1; + DIR *dir = opendir("/proc"); + if(!dir) + return -1; + + char cmdline_filepath[PATH_MAX]; + char arg0[PATH_MAX]; + + struct dirent *entry; + while((entry = readdir(dir)) != NULL) { + if(!is_number(entry->d_name)) + continue; + + snprintf(cmdline_filepath, sizeof(cmdline_filepath), "/proc/%s/cmdline", entry->d_name); + if(read_cmdline_arg0(cmdline_filepath, arg0, sizeof(arg0)) && strcmp(process_name, arg0) == 0) { + result = atoi(entry->d_name); + break; + } + } + + closedir(dir); + return result; + } }
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1e74ddc..3abccd0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "../include/GlobalHotkeysX11.hpp" #include "../include/GlobalHotkeysLinux.hpp" #include "../include/gui/Utils.hpp" +#include "../include/Process.hpp" #include <unistd.h> #include <signal.h> @@ -170,6 +171,13 @@ int main(int argc, char **argv) { usage(); } + const pid_t gsr_ui_pid = gsr::pidof("gsr-ui"); + if(gsr_ui_pid != -1) { + const char *args[] = { "gsr-notify", "--text", "Another instance of GPU Screen Recorder UI is already running", "--timeout", "5.0", "--icon-color", "ff0000", "--bg-color", "ff0000", nullptr }; + gsr::exec_program_daemonized(args); + return 1; + } + // Cant get window texture when prime-run is used disable_prime_run(); |