From fa7653b6732c0dac52f72648d31f144370c7ca11 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Fri, 7 Jul 2023 21:36:34 +0200
Subject: Fix crash on startup when using wayland, so we can show proper error
 message

---
 README.md    |  2 +-
 src/main.cpp | 23 ++++++++++++-----------
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md
index 0971e2f..a035d35 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@ More info at [gpu-screen-recorder](https://git.dec05eba.com/gpu-screen-recorder/
 This software works only on X11 (Wayland with Xwayland is NOT supported).
 ### TEMPORARY ISSUES
 1) Recording the monitor on steam deck might fail sometimes. This happens even when using ffmpeg directly. This might be a steam deck driver bug. Recording a single window doesn't have this issue.
-2) Videos created on AMD/Intel are in variable framerate format. Use MPV to play such videos, otherwise you might experience stuttering in the video if you are using a buggy video player.
+2) Videos created on AMD/Intel are in variable framerate format. Use MPV to play such videos, otherwise you might experience stuttering in the video if you are using a buggy video player. Try saving the video into a .mkv file instead when using AMD/Intel, as some software may have better support for .mkv files (such as kdenlive).
 # Performance
 On a system with a i5 4690k CPU and a GTX 1080 GPU:\
 When recording Legend of Zelda Breath of the Wild at 4k, fps drops from 30 to 7 when using OBS Studio + nvenc, however when using this screen recorder the fps remains at 30.\
diff --git a/src/main.cpp b/src/main.cpp
index a60749b..a1d1671 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -125,7 +125,7 @@ struct Container {
 static const Container supported_containers[] = {
     { "mp4", "mp4" },
     { "flv", "flv" },
-    { "matroska", "mkv" },
+    { "matroska", "mkv" }, // TODO: Default to this on amd/intel, add (Recommended on AMD/Intel)
     { "mov", "mov" }
 };
 
@@ -2120,7 +2120,7 @@ static GtkWidget* create_replay_page(GtkApplication *app, GtkStack *stack) {
     }
     gtk_widget_set_hexpand(GTK_WIDGET(replay_container), true);
     gtk_grid_attach(container_grid, GTK_WIDGET(replay_container), 1, 0, 1, 1);
-    gtk_combo_box_set_active(GTK_COMBO_BOX(replay_container), 0);
+    gtk_combo_box_set_active(GTK_COMBO_BOX(replay_container), 0); // TODO:
 
     GtkGrid *replay_time_grid = GTK_GRID(gtk_grid_new());
     gtk_grid_attach(grid, GTK_WIDGET(replay_time_grid), 0, row++, 5, 1);
@@ -2213,7 +2213,7 @@ static GtkWidget* create_recording_page(GtkApplication *app, GtkStack *stack) {
     }
     gtk_widget_set_hexpand(GTK_WIDGET(record_container), true);
     gtk_grid_attach(container_grid, GTK_WIDGET(record_container), 1, 0, 1, 1);
-    gtk_combo_box_set_active(GTK_COMBO_BOX(record_container), 0);
+    gtk_combo_box_set_active(GTK_COMBO_BOX(record_container), 0); // TODO:
 
     GtkGrid *start_button_grid = GTK_GRID(gtk_grid_new());
     gtk_grid_attach(grid, GTK_WIDGET(start_button_grid), 0, row++, 3, 1);
@@ -2514,17 +2514,13 @@ static bool gl_get_gpu_info(Display *dpy, gpu_info *info) {
     return supported;
 }
 
-static bool is_wayland() {
-    return !gdk_x11_get_default_xdisplay();
-}
-
-static bool is_xwayland() {
+static bool is_xwayland(Display *dpy) {
     int opcode, event, error;
-    if(XQueryExtension(gdk_x11_get_default_xdisplay(), "XWAYLAND", &opcode, &event, &error))
+    if(XQueryExtension(dpy, "XWAYLAND", &opcode, &event, &error))
         return true;
 
     bool xwayland_found = false;
-    for_each_active_monitor_output(gdk_x11_get_default_xdisplay(), [&xwayland_found](const XRROutputInfo *output_info, const XRRCrtcInfo*, const XRRModeInfo*) {
+    for_each_active_monitor_output(dpy, [&xwayland_found](const XRROutputInfo *output_info, const XRRCrtcInfo*, const XRRModeInfo*) {
         if(output_info->nameLen >= 8 && strncmp(output_info->name, "XWAYLAND", 8) == 0)
             xwayland_found = true;
     });
@@ -2543,7 +2539,11 @@ static const char* gpu_vendor_to_name(gpu_vendor vendor) {
 static void activate(GtkApplication *app, gpointer) {
     nvfbc_installed = is_nv_fbc_installed();
 
-    if(is_wayland() || is_xwayland()) {
+    Display *dpy = XOpenDisplay(NULL);
+    const bool is_wayland = !dpy;
+
+    if(is_wayland || is_xwayland(dpy)) {
+        XCloseDisplay(dpy);
         GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
             "GPU Screen Recorder only works in a pure X11 session. Xwayland is not supported.");
         gtk_dialog_run(GTK_DIALOG(dialog));
@@ -2551,6 +2551,7 @@ static void activate(GtkApplication *app, gpointer) {
         g_application_quit(G_APPLICATION(app));
         return;
     }
+    XCloseDisplay(dpy);
 
     if(!gl_get_gpu_info(gdk_x11_get_default_xdisplay(), &gpu_inf)) {
         GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
-- 
cgit v1.2.3-70-g09d2