aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp92
1 files changed, 79 insertions, 13 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 17a73a5..a68ff7d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,6 +8,7 @@
#include <signal.h>
#include <string.h>
#include <limits.h>
+#include <malloc.h>
#include <mglpp/mglpp.hpp>
#include <mglpp/system/Clock.hpp>
@@ -29,6 +30,10 @@ static void sigint_handler(int signal) {
running = 0;
}
+static void signal_ignore(int) {
+
+}
+
static void disable_prime_run() {
unsetenv("__NV_PRIME_RENDER_OFFLOAD");
unsetenv("__NV_PRIME_RENDER_OFFLOAD_PROVIDER");
@@ -72,6 +77,26 @@ static void rpc_add_commands(gsr::Rpc *rpc, gsr::Overlay *overlay) {
fprintf(stderr, "rpc command executed: %s\n", name.c_str());
overlay->save_replay();
});
+
+ rpc->add_handler("replay-save-1-min", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
+ overlay->save_replay_1_min();
+ });
+
+ rpc->add_handler("replay-save-10-min", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
+ overlay->save_replay_10_min();
+ });
+
+ rpc->add_handler("take-screenshot", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
+ overlay->take_screenshot();
+ });
+
+ rpc->add_handler("take-screenshot-region", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
+ overlay->take_screenshot_region();
+ });
}
static bool is_gsr_ui_virtual_keyboard_running() {
@@ -134,22 +159,40 @@ static bool is_flatpak() {
return getenv("FLATPAK_ID") != nullptr;
}
+static void set_display_server_environment_variables() {
+ // Some users dont have properly setup environments (no display manager that does systemctl --user import-environment DISPLAY WAYLAND_DISPLAY)
+ const char *display = getenv("DISPLAY");
+ if(!display) {
+ display = ":0";
+ setenv("DISPLAY", display, true);
+ }
+
+ const char *wayland_display = getenv("WAYLAND_DISPLAY");
+ if(!wayland_display) {
+ wayland_display = "wayland-1";
+ setenv("WAYLAND_DISPLAY", wayland_display, true);
+ }
+}
+
static void usage() {
printf("usage: gsr-ui [action]\n");
printf("OPTIONS:\n");
- printf(" action The launch action. Should be either \"launch-show\" or \"launch-hide\". Optional, defaults to \"launch-hide\".\n");
+ printf(" action The launch action. Should be either \"launch-show\", \"launch-hide\" or \"launch-daemon\". Optional, defaults to \"launch-hide\".\n");
printf(" If \"launch-show\" is used then the program starts and the UI is immediately opened and can be shown/hidden with Alt+Z.\n");
- printf(" If \"launch-hide\" is used then the program starts but the UI is not opened until Alt+Z is pressed.\n");
+ printf(" If \"launch-hide\" is used then the program starts but the UI is not opened until Alt+Z is pressed. The UI will be opened if the program is already running in another process.\n");
+ printf(" If \"launch-daemon\" is used then the program starts but the UI is not opened until Alt+Z is pressed. The UI will not be opened if the program is already running in another process.\n");
exit(1);
}
enum class LaunchAction {
LAUNCH_SHOW,
- LAUNCH_HIDE
+ LAUNCH_HIDE,
+ LAUNCH_DAEMON
};
int main(int argc, char **argv) {
setlocale(LC_ALL, "C"); // Sigh... stupid C
+ mallopt(M_MMAP_THRESHOLD, 65536);
if(geteuid() == 0) {
fprintf(stderr, "Error: don't run gsr-ui as the root user\n");
@@ -165,24 +208,27 @@ int main(int argc, char **argv) {
launch_action = LaunchAction::LAUNCH_SHOW;
} else if(strcmp(launch_action_opt, "launch-hide") == 0) {
launch_action = LaunchAction::LAUNCH_HIDE;
+ } else if(strcmp(launch_action_opt, "launch-daemon") == 0) {
+ launch_action = LaunchAction::LAUNCH_DAEMON;
} else {
- printf("error: invalid action \"%s\", expected \"launch-show\" or \"launch-hide\".\n", launch_action_opt);
+ printf("error: invalid action \"%s\", expected \"launch-show\", \"launch-hide\" or \"launch-daemon\".\n", launch_action_opt);
usage();
}
} else {
usage();
}
- if(is_flatpak())
- install_flatpak_systemd_service();
- else
- remove_flatpak_systemd_service();
+ set_display_server_environment_variables();
// 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.
// What do? creating a pid file doesn't work in flatpak either.
+ // TODO: This doesn't work in flatpak when disabling hotkeys.
if(is_gsr_ui_virtual_keyboard_running() || gsr::pidof("gsr-ui", getpid()) != -1) {
+ if(launch_action == LaunchAction::LAUNCH_DAEMON)
+ return 1;
+
gsr::Rpc rpc;
if(rpc.open("gsr-ui") && rpc.write("show_ui\n", 8)) {
fprintf(stderr, "Error: another instance of gsr-ui is already running, opening that one instead\n");
@@ -194,6 +240,16 @@ int main(int argc, char **argv) {
return 1;
}
+ if(gsr::pidof("gpu-screen-recorder", getpid()) != -1) {
+ const char *args[] = { "gsr-notify", "--text", "GPU Screen Recorder is already running in another process.\nPlease close it before using GPU Screen Recorder UI.", "--timeout", "5.0", "--icon-color", "ff0000", "--bg-color", "ff0000", nullptr };
+ gsr::exec_program_daemonized(args);
+ }
+
+ if(is_flatpak())
+ install_flatpak_systemd_service();
+ else
+ remove_flatpak_systemd_service();
+
// Stop nvidia driver from buffering frames
setenv("__GL_MaxFramesAllowed", "1", true);
// If this is set to 1 then cuGraphicsGLRegisterImage will fail for egl context with error: invalid OpenGL or DirectX context,
@@ -205,6 +261,16 @@ int main(int argc, char **argv) {
unsetenv("vblank_mode");
signal(SIGINT, sigint_handler);
+ signal(SIGTERM, sigint_handler);
+ signal(SIGUSR1, signal_ignore);
+ signal(SIGUSR2, signal_ignore);
+ signal(SIGRTMIN, signal_ignore);
+ signal(SIGRTMIN+1, signal_ignore);
+ signal(SIGRTMIN+2, signal_ignore);
+ signal(SIGRTMIN+3, signal_ignore);
+ signal(SIGRTMIN+4, signal_ignore);
+ signal(SIGRTMIN+5, signal_ignore);
+ signal(SIGRTMIN+6, signal_ignore);
gsr::GsrInfo gsr_info;
// TODO: Show the error in ui
@@ -222,7 +288,7 @@ int main(int argc, char **argv) {
disable_prime_run();
}
- if(mgl_init() != 0) {
+ if(mgl_init(MGL_WINDOW_SYSTEM_X11) != 0) {
fprintf(stderr, "Error: failed to initialize mgl. Failed to either connect to the X11 server or setup opengl\n");
exit(1);
}
@@ -290,10 +356,10 @@ int main(int argc, char **argv) {
if(exit_reason == "back-to-old-ui") {
const char *args[] = { "gpu-screen-recorder-gtk", "use-old-ui", nullptr };
execvp(args[0], (char* const*)args);
- } else if(exit_reason == "restart") {
- const char *args[] = { "gsr-ui", "launch-show", nullptr };
- execvp(args[0], (char* const*)args);
+ return 0;
+ } else if(exit_reason == "exit") {
+ return 0;
}
- return 0;
+ return mgl_is_connected_to_display_server() ? 0 : 1;
}