[gcr/wip/nielsdg/lose-x11-dependency: 157/158] ui: Update frob-*prompt for Wayland systems




commit 021be790c4d06b8edb9e4d5f80e6ff1010043b04
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Mon Dec 9 08:30:22 2019 +0100

    ui: Update frob-*prompt for Wayland systems
    
    `frob-prompt` and `frob-system-prompt` aren't used in practice, but they
    give a good idea on how to export a handle when dealing with Wayland
    windows.

 ui/frob-prompt.c        | 57 ++++++++++++++++++++++++++++++++++++++-----
 ui/frob-system-prompt.c | 65 +++++++++++++++++++++++++++++++++++++------------
 2 files changed, 101 insertions(+), 21 deletions(-)
---
diff --git a/ui/frob-prompt.c b/ui/frob-prompt.c
index f5a84030..20f54824 100644
--- a/ui/frob-prompt.c
+++ b/ui/frob-prompt.c
@@ -28,8 +28,9 @@
 #ifdef GDK_WINDOWING_X11
 #include <gdk/gdkx.h>
 #endif
-
-#include <err.h>
+#ifdef GDK_WINDOWING_WAYLAND
+#include <gdk/gdkwayland.h>
+#endif
 
 static const gchar *file_name = NULL;
 static gchar *prompt_type = NULL;
@@ -44,6 +45,45 @@ on_delay_timeout (gpointer data)
        return FALSE;
 }
 
+#ifdef GDK_WINDOWING_X11
+static void
+set_caller_window_x11 (GcrPrompt *prompt, GdkWindow *window)
+{
+       gchar *caller_id = NULL;
+
+       if (!GDK_IS_X11_WINDOW (window))
+               return;
+
+       caller_id = g_strdup_printf ("%lu", (gulong)GDK_WINDOW_XID (window));
+       gcr_prompt_set_caller_window (prompt, caller_id);
+       g_free (caller_id);
+}
+#endif
+
+#ifdef GDK_WINDOWING_WAYLAND
+static void
+on_window_wl_export_handle (GdkWindow  *window,
+                            const char *handle,
+                            gpointer    user_data)
+{
+       GcrPrompt *prompt = GCR_PROMPT (user_data);
+
+       g_return_if_fail (handle);
+
+       gcr_prompt_set_caller_window (prompt, handle);
+}
+
+static void
+set_caller_window_wl (GcrPrompt *prompt, GdkWindow *window)
+{
+       if (!GDK_IS_WAYLAND_WINDOW (window))
+               return;
+
+       if (!gdk_wayland_window_export_handle (window, on_window_wl_export_handle, prompt, NULL))
+               g_warning ("Couldn't export Wayland window handle");
+}
+#endif
+
 static void
 prompt_perform (GtkWidget *parent)
 {
@@ -56,7 +96,6 @@ prompt_perform (GtkWidget *parent)
        const gchar *key;
        const gchar *password;
        GcrPromptReply reply;
-       gchar *caller_id = NULL;
        gboolean cont = TRUE;
        GMainLoop *loop;
        gchar *type;
@@ -80,9 +119,15 @@ prompt_perform (GtkWidget *parent)
                g_error ("couldn't create prompt: %s", error->message);
 
        if (parent) {
-               caller_id = g_strdup_printf ("%lu", (gulong)GDK_WINDOW_XID (gtk_widget_get_window (parent)));
-               gcr_prompt_set_caller_window (GCR_PROMPT (prompt), caller_id);
-               g_free (caller_id);
+               GdkWindow *window;
+
+               window = gtk_widget_get_window (parent);
+#ifdef GDK_WINDOWING_X11
+               set_caller_window_x11 (prompt, window);
+#endif
+#ifdef GDK_WINDOWING_WAYLAND
+               set_caller_window_wl (prompt, window);
+#endif
        }
 
        loop = g_main_loop_new (NULL, FALSE);
diff --git a/ui/frob-system-prompt.c b/ui/frob-system-prompt.c
index 02bdc285..330f8bd6 100644
--- a/ui/frob-system-prompt.c
+++ b/ui/frob-system-prompt.c
@@ -27,19 +27,53 @@
 #ifdef GDK_WINDOWING_X11
 #include <gdk/gdkx.h>
 #endif
+#ifdef GDK_WINDOWING_WAYLAND
+#include <gdk/gdkwayland.h>
+#endif
 
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 
+static void
+run_prompt (GcrPrompt *prompt)
+{
+       const char *password;
+       GError *error = NULL;
+
+       password = gcr_prompt_password_run (GCR_PROMPT (prompt), NULL, &error);
+       if (error != NULL) {
+               g_warning ("couldn't prompt for password: %s", error->message);
+               g_error_free (error);
+               g_object_unref (prompt);
+               return;
+       }
+
+       g_print ("password: %s\n", password);
+       g_object_unref (prompt);
+}
+
+static void
+on_gdk_wl_window_exported (GdkWindow *window,
+                           const char *handle,
+                           gpointer user_data)
+{
+       GcrPrompt *prompt = GCR_PROMPT (user_data);
+
+       g_return_if_fail (handle);
+
+       gcr_prompt_set_caller_window (prompt, handle);
+       run_prompt (prompt);
+}
+
 static void
 on_prompt_clicked (GtkToolButton *button,
                    gpointer user_data)
 {
        GcrPrompt *prompt;
        GError *error = NULL;
-       const gchar *password;
        GtkWidget *parent = user_data;
+       GdkWindow *window;
        gchar *caller_id;
 
        prompt = gcr_system_prompt_open (-1, NULL, &error);
@@ -48,29 +82,30 @@ on_prompt_clicked (GtkToolButton *button,
                g_error_free (error);
                return;
        }
-       g_object_add_weak_pointer (G_OBJECT (prompt), (gpointer *)&prompt);
 
        gcr_prompt_set_title (GCR_PROMPT (prompt), "This is the title");
        gcr_prompt_set_message (GCR_PROMPT (prompt), "This is the message");
        gcr_prompt_set_description (GCR_PROMPT (prompt), "This is the description");
 
+       window = gtk_widget_get_window (parent);
 #ifdef GDK_WINDOWING_X11
-       caller_id = g_strdup_printf ("%lu", (gulong)GDK_WINDOW_XID (gtk_widget_get_window (parent)));
-       gcr_prompt_set_caller_window (GCR_PROMPT (prompt), caller_id);
-       g_free (caller_id);
+       if (GDK_IS_X11_WINDOW (window)) {
+               caller_id = g_strdup_printf ("%lu", (gulong)GDK_WINDOW_XID (window));
+               gcr_prompt_set_caller_window (prompt, caller_id);
+               g_free (caller_id);
+       }
 #endif
-
-       password = gcr_prompt_password_run (GCR_PROMPT (prompt), NULL, &error);
-       if (error != NULL) {
-               g_warning ("couldn't prompt for password: %s", error->message);
-               g_error_free (error);
-               g_object_unref (prompt);
-               return;
+#ifdef GDK_WINDOWING_WAYLAND
+       if (GDK_IS_WAYLAND_WINDOW (window)) {
+               if (!gdk_wayland_window_export_handle (window, on_gdk_wl_window_exported, prompt, NULL)) {
+                       g_warning ("Couldn't export Wayland window handle");
+               } else {
+                       return; /* Don't run the prompt before the async method finished */
+               }
        }
+#endif
 
-       g_print ("password: %s\n", password);
-       g_object_unref (prompt);
-       g_assert (prompt == NULL);
+       run_prompt (prompt);
 }
 
 static gboolean


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]