From 7eba31b012ed6b68d914414cad2726a28ecc1004 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Thu, 27 Mar 2025 19:30:28 +0000
Subject: [PATCH] run-dbus: Don't call GetAddress() if AT_SPI_BUS_ADDRESS is
 set

In the real at-spi2-core client library, AT_SPI_BUS_ADDRESS is treated
as higher-precedence than the AT_SPI_BUS X11 atom or the GetAddress()
D-Bus method. Mimic this.

We don't currently implement querying the X11 atom because that would
require a libX11 dependency, so leave a comment indicating where it
would appear in precedence order.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Forwarded: https://github.com/flatpak/flatpak/pull/6173
---
 pressure-vessel/flatpak-run-dbus.c | 56 ++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 18 deletions(-)

diff --git a/pressure-vessel/flatpak-run-dbus.c b/pressure-vessel/flatpak-run-dbus.c
index 5fd802dd6..4eed6dbed 100644
--- a/pressure-vessel/flatpak-run-dbus.c
+++ b/pressure-vessel/flatpak-run-dbus.c
@@ -419,6 +419,7 @@ flatpak_run_add_a11y_dbus_args (FlatpakBwrap   *app_bwrap,
   g_autoptr(GDBusMessage) reply = NULL;
   g_autoptr(GDBusMessage) msg = NULL;
   g_autofree char *proxy_socket = NULL;
+  const char *value;
 
   if ((flags & FLATPAK_RUN_FLAG_NO_A11Y_BUS_PROXY) != 0)
     return FALSE;
@@ -427,26 +428,45 @@ flatpak_run_add_a11y_dbus_args (FlatpakBwrap   *app_bwrap,
   if (session_bus == NULL)
     return FALSE;
 
-  msg = g_dbus_message_new_method_call ("org.a11y.Bus", "/org/a11y/bus", "org.a11y.Bus", "GetAddress");
-  g_dbus_message_set_body (msg, g_variant_new ("()"));
-  reply =
-    g_dbus_connection_send_message_with_reply_sync (session_bus, msg,
-                                                    G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-                                                    30000,
-                                                    NULL,
-                                                    NULL,
-                                                    NULL);
-  if (reply)
+  value = g_getenv ("AT_SPI_BUS_ADDRESS");
+
+  if (value != NULL && value[0] != '\0')
     {
-      if (g_dbus_message_to_gerror (reply, &local_error))
-        {
-          if (!g_error_matches (local_error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN))
-            g_message ("Can't find a11y bus: %s", local_error->message);
-        }
-      else
+      a11y_address = g_strdup (value);
+      g_debug ("Retrieved AT-SPI bus address \"%s\" from environment",
+               a11y_address);
+    }
+
+  /* To have an accurate emulation of AT-SPI's behaviour, ideally we would
+   * also query the AT_SPI_BUS atom on the root window if a11y_address is
+   * still NULL here, but that would require a libX11 dependency, and
+   * isn't done when running under native Wayland anyway. */
+
+  if (a11y_address == NULL)
+    {
+      msg = g_dbus_message_new_method_call ("org.a11y.Bus", "/org/a11y/bus", "org.a11y.Bus", "GetAddress");
+      g_dbus_message_set_body (msg, g_variant_new ("()"));
+      reply =
+        g_dbus_connection_send_message_with_reply_sync (session_bus, msg,
+                                                        G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+                                                        30000,
+                                                        NULL,
+                                                        NULL,
+                                                        NULL);
+      if (reply)
         {
-          g_variant_get (g_dbus_message_get_body (reply),
-                         "(s)", &a11y_address);
+          if (g_dbus_message_to_gerror (reply, &local_error))
+            {
+              if (!g_error_matches (local_error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN))
+                g_message ("Can't find a11y bus: %s", local_error->message);
+            }
+          else
+            {
+              g_variant_get (g_dbus_message_get_body (reply),
+                             "(s)", &a11y_address);
+              g_debug ("Retrieved AT-SPI bus address \"%s\" from session bus",
+                       a11y_address);
+            }
         }
     }
 
-- 
GitLab