diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 86 |
1 files changed, 62 insertions, 24 deletions
diff --git a/src/main.cpp b/src/main.cpp index ffee855..9c20a81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ #include "../include/GsrInfo.hpp" -#include "../include/Theme.hpp" #include "../include/Overlay.hpp" #include "../include/GlobalHotkeysX11.hpp" #include "../include/GlobalHotkeysLinux.hpp" @@ -96,8 +95,8 @@ static std::unique_ptr<gsr::GlobalHotkeysX11> register_x11_hotkeys(gsr::Overlay return global_hotkeys; } -static std::unique_ptr<gsr::GlobalHotkeysLinux> register_linux_hotkeys(gsr::Overlay *overlay) { - auto global_hotkeys = std::make_unique<gsr::GlobalHotkeysLinux>(); +static std::unique_ptr<gsr::GlobalHotkeysLinux> register_linux_hotkeys(gsr::Overlay *overlay, gsr::GlobalHotkeysLinux::GrabType grab_type) { + auto global_hotkeys = std::make_unique<gsr::GlobalHotkeysLinux>(grab_type); if(!global_hotkeys->start()) fprintf(stderr, "error: failed to start global hotkeys\n"); @@ -134,6 +133,43 @@ static std::unique_ptr<gsr::GlobalHotkeysLinux> register_linux_hotkeys(gsr::Over return global_hotkeys; } +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(); + }); + + rpc->add_handler("toggle-record", [overlay](const std::string &name) { + fprintf(stderr, "rpc command executed: %s\n", name.c_str()); + overlay->toggle_record(); + }); + + rpc->add_handler("toggle-pause", [overlay](const std::string &name) { + fprintf(stderr, "rpc command executed: %s\n", name.c_str()); + overlay->toggle_pause(); + }); + + rpc->add_handler("toggle-stream", [overlay](const std::string &name) { + fprintf(stderr, "rpc command executed: %s\n", name.c_str()); + overlay->toggle_stream(); + }); + + rpc->add_handler("toggle-replay", [overlay](const std::string &name) { + fprintf(stderr, "rpc command executed: %s\n", name.c_str()); + overlay->toggle_replay(); + }); + + rpc->add_handler("replay-save", [overlay](const std::string &name) { + fprintf(stderr, "rpc command executed: %s\n", name.c_str()); + overlay->save_replay(); + }); +} + static bool is_gsr_ui_virtual_keyboard_running() { FILE *f = fopen("/proc/bus/input/devices", "rb"); if(!f) @@ -193,9 +229,11 @@ int main(int argc, char **argv) { // 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. + if(is_gsr_ui_virtual_keyboard_running() || gsr::pidof("gsr-ui", getpid()) != -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,12 +242,6 @@ 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(); @@ -271,20 +303,21 @@ 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 overlay = std::make_unique<gsr::Overlay>(resources_path, std::move(gsr_info), std::move(capture_options), egl_funcs); + if(launch_action == LaunchAction::LAUNCH_SHOW) + overlay->show(); + 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&) { - overlay->show(); - }); + rpc_add_commands(rpc.get(), overlay.get()); - std::unique_ptr<gsr::GlobalHotkeys> global_hotkeys = register_linux_hotkeys(overlay.get()); - - if(launch_action == LaunchAction::LAUNCH_SHOW) - overlay->show(); + std::unique_ptr<gsr::GlobalHotkeys> global_hotkeys = nullptr; + if(overlay->get_config().main_config.hotkeys_enable_option == "enable_hotkeys") + global_hotkeys = register_linux_hotkeys(overlay.get(), gsr::GlobalHotkeysLinux::GrabType::ALL); + else if(overlay->get_config().main_config.hotkeys_enable_option == "enable_hotkeys_virtual_devices") + global_hotkeys = register_linux_hotkeys(overlay.get(), gsr::GlobalHotkeysLinux::GrabType::VIRTUAL); // 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,7 +329,10 @@ int main(int argc, char **argv) { gsr::set_frame_delta_seconds(frame_delta_seconds); rpc->poll(); - global_hotkeys->poll_events(); + + if(global_hotkeys) + global_hotkeys->poll_events(); + overlay->handle_events(global_hotkeys.get()); if(!overlay->draw()) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); @@ -306,15 +342,17 @@ int main(int argc, char **argv) { fprintf(stderr, "Info: shutting down!\n"); rpc.reset(); - global_hotkeys.reset(); + if(global_hotkeys) + 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); + } else if(exit_reason == "restart") { + const char *args[] = { "gsr-ui", "launch-show", nullptr }; + execvp(args[0], (char* const*)args); } return 0; |