From 3468554eb3ddeac3cbf079ad6ebf3c8a39efc2f2 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Sun, 24 Nov 2024 19:13:35 +0100
Subject: Allow running the ui on Wayland through XWayland

---
 src/gui/SettingsPage.cpp | 41 ++++++++++++++++++++++++-----------------
 src/main.cpp             | 13 ++++++++-----
 2 files changed, 32 insertions(+), 22 deletions(-)

(limited to 'src')

diff --git a/src/gui/SettingsPage.cpp b/src/gui/SettingsPage.cpp
index 949e31a..79f6c52 100644
--- a/src/gui/SettingsPage.cpp
+++ b/src/gui/SettingsPage.cpp
@@ -42,7 +42,7 @@ namespace gsr {
         add_widget(std::move(content_page));
 
         add_widgets(gsr_info);
-        add_page_specific_widgets();
+        add_page_specific_widgets(gsr_info);
         load(gsr_info);
     }
 
@@ -550,16 +550,16 @@ namespace gsr {
         }
     }
 
-    void SettingsPage::add_page_specific_widgets() {
+    void SettingsPage::add_page_specific_widgets(const GsrInfo &gsr_info) {
         switch(type) {
             case Type::REPLAY:
-                add_replay_widgets();
+                add_replay_widgets(gsr_info);
                 break;
             case Type::RECORD:
-                add_record_widgets();
+                add_record_widgets(gsr_info);
                 break;
             case Type::STREAM:
-                add_stream_widgets();
+                add_stream_widgets(gsr_info);
                 break;
         }
     }
@@ -622,18 +622,23 @@ namespace gsr {
         return replay_time_list;
     }
 
-    std::unique_ptr<RadioButton> SettingsPage::create_start_replay_automatically() {
+    std::unique_ptr<RadioButton> SettingsPage::create_start_replay_automatically(const GsrInfo &gsr_info) {
+        char fullscreen_text[256];
+        snprintf(fullscreen_text, sizeof(fullscreen_text), "Turn on replay when starting a fullscreen application%s", gsr_info.system_info.display_server == DisplayServer::X11 ? "" : " (X11 only)");
+
         auto radiobutton = std::make_unique<RadioButton>(&get_theme().body_font, RadioButton::Orientation::VERTICAL);
         radiobutton->add_item("Don't turn on replay automatically", "dont_turn_on_automatically");
         radiobutton->add_item("Turn on replay at system startup", "turn_on_at_system_startup");
-        radiobutton->add_item("Turn on replay when starting a fullscreen application", "turn_on_at_fullscreen");
+        radiobutton->add_item(fullscreen_text, "turn_on_at_fullscreen");
         radiobutton->add_item("Turn on replay when power supply is connected", "turn_on_at_power_supply_connected");
         turn_on_replay_automatically_mode_ptr = radiobutton.get();
         return radiobutton;
     }
 
-    std::unique_ptr<CheckBox> SettingsPage::create_save_replay_in_game_folder() {
-        auto checkbox = std::make_unique<CheckBox>(&get_theme().body_font, "Save video in a folder with the name of the game");
+    std::unique_ptr<CheckBox> SettingsPage::create_save_replay_in_game_folder(const GsrInfo &gsr_info) {
+        char text[256];
+        snprintf(text, sizeof(text), "Save video in a folder with the name of the game%s", gsr_info.system_info.display_server == DisplayServer::X11 ? "" : " (X11 only)");
+        auto checkbox = std::make_unique<CheckBox>(&get_theme().body_font, text);
         save_replay_in_game_folder_ptr = checkbox.get();
         return checkbox;
     }
@@ -654,7 +659,7 @@ namespace gsr {
         estimated_file_size_ptr->set_text(buffer);
     }
 
-    void SettingsPage::add_replay_widgets() {
+    void SettingsPage::add_replay_widgets(const GsrInfo &gsr_info) {
         auto file_info_list = std::make_unique<List>(List::Orientation::VERTICAL);
         auto file_info_data_list = std::make_unique<List>(List::Orientation::HORIZONTAL);
         file_info_data_list->add_widget(create_save_directory("Directory to save replays:"));
@@ -665,8 +670,8 @@ namespace gsr {
         settings_list_ptr->add_widget(std::make_unique<Subsection>("File info", std::move(file_info_list), mgl::vec2f(settings_scrollable_page_ptr->get_inner_size().x, 0.0f)));
 
         auto general_list = std::make_unique<List>(List::Orientation::VERTICAL);
-        general_list->add_widget(create_start_replay_automatically());
-        general_list->add_widget(create_save_replay_in_game_folder());
+        general_list->add_widget(create_start_replay_automatically(gsr_info));
+        general_list->add_widget(create_save_replay_in_game_folder(gsr_info));
         settings_list_ptr->add_widget(std::make_unique<Subsection>("General", std::move(general_list), mgl::vec2f(settings_scrollable_page_ptr->get_inner_size().x, 0.0f)));
 
         auto checkboxes_list = std::make_unique<List>(List::Orientation::VERTICAL);
@@ -711,20 +716,22 @@ namespace gsr {
         };
     }
 
-    std::unique_ptr<CheckBox> SettingsPage::create_save_recording_in_game_folder() {
-        auto checkbox = std::make_unique<CheckBox>(&get_theme().body_font, "Save video in a folder with the name of the game");
+    std::unique_ptr<CheckBox> SettingsPage::create_save_recording_in_game_folder(const GsrInfo &gsr_info) {
+        char text[256];
+        snprintf(text, sizeof(text), "Save video in a folder with the name of the game%s", gsr_info.system_info.display_server == DisplayServer::X11 ? "" : " (X11 only)");
+        auto checkbox = std::make_unique<CheckBox>(&get_theme().body_font, text);
         save_recording_in_game_folder_ptr = checkbox.get();
         return checkbox;
     }
 
-    void SettingsPage::add_record_widgets() {
+    void SettingsPage::add_record_widgets(const GsrInfo &gsr_info) {
         auto file_list = std::make_unique<List>(List::Orientation::HORIZONTAL);
         file_list->add_widget(create_save_directory("Directory to save the video:"));
         file_list->add_widget(create_container_section());
         settings_list_ptr->add_widget(std::make_unique<Subsection>("File info", std::move(file_list), mgl::vec2f(settings_scrollable_page_ptr->get_inner_size().x, 0.0f)));
 
         auto general_list = std::make_unique<List>(List::Orientation::VERTICAL);
-        general_list->add_widget(create_save_recording_in_game_folder());
+        general_list->add_widget(create_save_recording_in_game_folder(gsr_info));
         settings_list_ptr->add_widget(std::make_unique<Subsection>("General", std::move(general_list), mgl::vec2f(settings_scrollable_page_ptr->get_inner_size().x, 0.0f)));
 
         auto checkboxes_list = std::make_unique<List>(List::Orientation::VERTICAL);
@@ -818,7 +825,7 @@ namespace gsr {
         return container_list;
     }
 
-    void SettingsPage::add_stream_widgets() {
+    void SettingsPage::add_stream_widgets(const GsrInfo&) {
         auto streaming_info_list = std::make_unique<List>(List::Orientation::HORIZONTAL);
         streaming_info_list->add_widget(create_streaming_service_section());
         streaming_info_list->add_widget(create_stream_key_section());
diff --git a/src/main.cpp b/src/main.cpp
index be2e50d..cded01c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -56,6 +56,11 @@ int main(void) {
 
     signal(SIGINT, sigint_handler);
 
+    if(mgl_init() != 0) {
+        fprintf(stderr, "error: failed to initialize mgl. Either failed to connec to the X11 server or failed to setup opengl\n");
+        exit(1);
+    }
+
     gsr::GsrInfo gsr_info;
     // TODO: Show the error in ui
     gsr::GsrInfoExitStatus gsr_info_exit_status = gsr::get_gpu_screen_recorder_info(&gsr_info);
@@ -64,10 +69,8 @@ int main(void) {
         exit(1);
     }
 
-    if(gsr_info.system_info.display_server == gsr::DisplayServer::WAYLAND) {
-        fprintf(stderr, "error: Wayland is currently not supported\n");
-        exit(1);
-    }
+   if(gsr_info.system_info.display_server == gsr::DisplayServer::WAYLAND)
+       fprintf(stderr, "warning: Wayland support is experimental and requires XWayland. Things may not work as expected.\n");
 
     std::string resources_path;
     if(access("sibs-build", F_OK) == 0) {
@@ -80,7 +83,6 @@ int main(void) {
 #endif
     }
 
-    mgl::Init init;
     mgl_context *context = mgl_get_context();
 
     egl_functions egl_funcs;
@@ -198,6 +200,7 @@ int main(void) {
     overlay.reset();
     gsr::deinit_theme();
     gsr::deinit_color_theme();
+    mgl_deinit();
 
     return 0;
 }
-- 
cgit v1.2.3-70-g09d2