Only allow one instance of gsr-ui to run

This commit is contained in:
dec05eba
2024-12-29 15:21:38 +01:00
parent c5c79bec64
commit b96b877a1a
4 changed files with 54 additions and 8 deletions

4
TODO
View File

@@ -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".
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?

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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();