aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp264
1 files changed, 154 insertions, 110 deletions
diff --git a/src/main.cpp b/src/main.cpp
index ffee855..a68ff7d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,18 +1,15 @@
#include "../include/GsrInfo.hpp"
-#include "../include/Theme.hpp"
#include "../include/Overlay.hpp"
-#include "../include/GlobalHotkeysX11.hpp"
-#include "../include/GlobalHotkeysLinux.hpp"
#include "../include/gui/Utils.hpp"
#include "../include/Process.hpp"
#include "../include/Rpc.hpp"
#include <unistd.h>
#include <signal.h>
-#include <thread>
#include <string.h>
+#include <limits.h>
+#include <malloc.h>
-#include <X11/keysym.h>
#include <mglpp/mglpp.hpp>
#include <mglpp/system/Clock.hpp>
@@ -33,105 +30,73 @@ 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");
unsetenv("__GLX_VENDOR_LIBRARY_NAME");
unsetenv("__VK_LAYER_NV_optimus");
+ unsetenv("DRI_PRIME");
}
-static std::unique_ptr<gsr::GlobalHotkeysX11> register_x11_hotkeys(gsr::Overlay *overlay) {
- auto global_hotkeys = std::make_unique<gsr::GlobalHotkeysX11>();
- const bool show_hotkey_registered = global_hotkeys->bind_key_press({ XK_z, Mod1Mask }, "show_hide", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
+static void rpc_add_commands(gsr::Rpc *rpc, gsr::Overlay *overlay) {
+ rpc->add_handler("show_ui", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
+ overlay->show();
+ });
+
+ rpc->add_handler("toggle-show", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
overlay->toggle_show();
});
- const bool record_hotkey_registered = global_hotkeys->bind_key_press({ XK_F9, Mod1Mask }, "record", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
+ rpc->add_handler("toggle-record", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
overlay->toggle_record();
});
- const bool pause_hotkey_registered = global_hotkeys->bind_key_press({ XK_F7, Mod1Mask }, "pause", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
+ rpc->add_handler("toggle-pause", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
overlay->toggle_pause();
});
- const bool stream_hotkey_registered = global_hotkeys->bind_key_press({ XK_F8, Mod1Mask }, "stream", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
+ rpc->add_handler("toggle-stream", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
overlay->toggle_stream();
});
- const bool replay_hotkey_registered = global_hotkeys->bind_key_press({ XK_F10, ShiftMask | Mod1Mask }, "replay_start", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
+ rpc->add_handler("toggle-replay", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
overlay->toggle_replay();
});
- const bool replay_save_hotkey_registered = global_hotkeys->bind_key_press({ XK_F10, Mod1Mask }, "replay_save", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
+ rpc->add_handler("replay-save", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
overlay->save_replay();
});
- if(!show_hotkey_registered)
- fprintf(stderr, "error: failed to register hotkey alt+z for showing the overlay because the hotkey is registered by another program\n");
-
- if(!record_hotkey_registered)
- fprintf(stderr, "error: failed to register hotkey alt+f9 for recording because the hotkey is registered by another program\n");
-
- if(!pause_hotkey_registered)
- fprintf(stderr, "error: failed to register hotkey alt+f7 for pausing because the hotkey is registered by another program\n");
-
- if(!stream_hotkey_registered)
- fprintf(stderr, "error: failed to register hotkey alt+f8 for streaming because the hotkey is registered by another program\n");
-
- if(!replay_hotkey_registered)
- fprintf(stderr, "error: failed to register hotkey alt+shift+f10 for starting replay because the hotkey is registered by another program\n");
-
- if(!replay_save_hotkey_registered)
- fprintf(stderr, "error: failed to register hotkey alt+f10 for saving replay because the hotkey is registered by another program\n");
-
- if(!show_hotkey_registered || !record_hotkey_registered || !pause_hotkey_registered || !stream_hotkey_registered || !replay_hotkey_registered || !replay_save_hotkey_registered)
- return nullptr;
-
- return global_hotkeys;
-}
-
-static std::unique_ptr<gsr::GlobalHotkeysLinux> register_linux_hotkeys(gsr::Overlay *overlay) {
- auto global_hotkeys = std::make_unique<gsr::GlobalHotkeysLinux>();
- if(!global_hotkeys->start())
- fprintf(stderr, "error: failed to start global hotkeys\n");
-
- global_hotkeys->bind_action("show_hide", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
- overlay->toggle_show();
- });
-
- global_hotkeys->bind_action("record", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
- overlay->toggle_record();
+ 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();
});
- global_hotkeys->bind_action("pause", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
- overlay->toggle_pause();
+ 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();
});
- global_hotkeys->bind_action("stream", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
- overlay->toggle_stream();
+ rpc->add_handler("take-screenshot", [overlay](const std::string &name) {
+ fprintf(stderr, "rpc command executed: %s\n", name.c_str());
+ overlay->take_screenshot();
});
- global_hotkeys->bind_action("replay_start", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
- overlay->toggle_replay();
- });
-
- global_hotkeys->bind_action("replay_save", [overlay](const std::string &id) {
- fprintf(stderr, "pressed %s\n", id.c_str());
- overlay->save_replay();
+ 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();
});
-
- return global_hotkeys;
}
static bool is_gsr_ui_virtual_keyboard_running() {
@@ -152,22 +117,82 @@ 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 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");
@@ -183,19 +208,29 @@ 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();
}
+ 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.
- if(is_gsr_ui_virtual_keyboard_running()) {
+ // 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", 7)) {
+ 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");
} else {
fprintf(stderr, "Error: failed to send command to running gsr-ui instance, user will have to open the UI manually with Alt+Z\n");
@@ -204,15 +239,16 @@ int main(int argc, char **argv) {
}
return 1;
}
- // 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();
+ 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);
@@ -225,11 +261,16 @@ int main(int argc, char **argv) {
unsetenv("vblank_mode");
signal(SIGINT, sigint_handler);
-
- if(mgl_init() != 0) {
- fprintf(stderr, "Error: failed to initialize mgl. Failed to either connect to the X11 server or setup opengl\n");
- exit(1);
- }
+ 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
@@ -240,8 +281,17 @@ int main(int argc, char **argv) {
}
const gsr::DisplayServer display_server = gsr_info.system_info.display_server;
- if(display_server == gsr::DisplayServer::WAYLAND)
- fprintf(stderr, "Warning: Wayland support is experimental and requires XWayland. Things may not work as expected.\n");
+ if(display_server == gsr::DisplayServer::WAYLAND) {
+ fprintf(stderr, "Warning: Wayland doesn't support this program properly and XWayland is required. Things may not work as expected. Use X11 if you experience issues.\n");
+ } else {
+ // Cant get window texture when prime-run is used
+ disable_prime_run();
+ }
+
+ 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);
+ }
gsr::SupportedCaptureOptions capture_options = gsr::get_supported_capture_options(gsr_info);
@@ -271,20 +321,15 @@ int main(int argc, char **argv) {
fprintf(stderr, "Info: gsr ui is now ready, waiting for inputs. Press alt+z to show/hide the overlay\n");
- auto rpc = std::make_unique<gsr::Rpc>();
- if(!rpc->create("gsr-ui"))
- fprintf(stderr, "Error: Failed to create rpc, commands won't be received\n");
-
auto overlay = std::make_unique<gsr::Overlay>(resources_path, std::move(gsr_info), std::move(capture_options), egl_funcs);
-
- rpc->add_handler("show_ui", [&](const std::string&) {
+ if(launch_action == LaunchAction::LAUNCH_SHOW)
overlay->show();
- });
- std::unique_ptr<gsr::GlobalHotkeys> global_hotkeys = register_linux_hotkeys(overlay.get());
+ auto rpc = std::make_unique<gsr::Rpc>();
+ if(!rpc->create("gsr-ui"))
+ fprintf(stderr, "Error: Failed to create rpc, commands won't be received\n");
- if(launch_action == LaunchAction::LAUNCH_SHOW)
- overlay->show();
+ rpc_add_commands(rpc.get(), overlay.get());
// 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.
@@ -296,26 +341,25 @@ int main(int argc, char **argv) {
gsr::set_frame_delta_seconds(frame_delta_seconds);
rpc->poll();
- global_hotkeys->poll_events();
- overlay->handle_events(global_hotkeys.get());
+ overlay->handle_events();
if(!overlay->draw()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ usleep(100 * 1000); // 100ms
mgl_ping_display_server();
}
}
fprintf(stderr, "Info: shutting down!\n");
rpc.reset();
- global_hotkeys.reset();
overlay.reset();
- gsr::deinit_theme();
- gsr::deinit_color_theme();
mgl_deinit();
if(exit_reason == "back-to-old-ui") {
const char *args[] = { "gpu-screen-recorder-gtk", "use-old-ui", 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;
}