From 8afb754c62d2c77600ceea5c77bd599c0e426185 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Wed, 17 Mar 2021 16:08:22 +0000
Subject: [PATCH] pv-runtime: Move PulseAudio setup into PvRuntime

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/runtime.c | 27 +++++++++++++++++++++++++++
 pressure-vessel/runtime.h |  4 ++++
 pressure-vessel/wrap.c    | 18 ++----------------
 3 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/pressure-vessel/runtime.c b/pressure-vessel/runtime.c
index 65d376678..9f85a3fc4 100644
--- a/pressure-vessel/runtime.c
+++ b/pressure-vessel/runtime.c
@@ -90,6 +90,7 @@ struct _PvRuntime
   gboolean runtime_is_just_usr;
   gboolean is_steamrt;
   gboolean is_scout;
+  gboolean is_flatpak_env;
 };
 
 struct _PvRuntimeClass
@@ -392,6 +393,8 @@ pv_runtime_init (PvRuntime *self)
   self->all_libc_from_provider = FALSE;
   self->variable_dir_fd = -1;
   self->mutable_sysroot_fd = -1;
+  self->is_flatpak_env = g_file_test ("/.flatpak-info",
+                                      G_FILE_TEST_IS_REGULAR);
 }
 
 static void
@@ -4841,6 +4844,30 @@ pv_runtime_set_search_paths (PvRuntime *self,
   pv_environ_lock_env (container_env, "LD_LIBRARY_PATH", ld_library_path->str);
 }
 
+void
+pv_runtime_use_shared_sockets (PvRuntime *self,
+                               FlatpakBwrap *bwrap,
+                               PvEnviron *container_env)
+{
+  if (pv_environ_is_locked (container_env, "PULSE_SERVER")
+      || self->is_flatpak_env)
+    {
+      /* Make the PulseAudio driver the default.
+       * We do this unconditionally when we are under Flatpak for parity
+       * with the freedesktop.org Platform. */
+      const gchar *alsa_config = "pcm.!default pulse\n"
+                                 "ctl.!default pulse\n";
+
+      if (bwrap != NULL)
+        {
+          flatpak_bwrap_add_args_data (bwrap, "asound.conf",
+                                       alsa_config, -1,
+                                       "/etc/asound.conf",
+                                       NULL);
+        }
+    }
+}
+
 static void
 pv_runtime_initable_iface_init (GInitableIface *iface,
                                 gpointer unused G_GNUC_UNUSED)
diff --git a/pressure-vessel/runtime.h b/pressure-vessel/runtime.h
index 6b3b91da9..151e59e2e 100644
--- a/pressure-vessel/runtime.h
+++ b/pressure-vessel/runtime.h
@@ -109,4 +109,8 @@ gboolean pv_runtime_garbage_collect_legacy (const char *variable_dir,
                                             const char *runtime_base,
                                             GError **error);
 
+void pv_runtime_use_shared_sockets (PvRuntime *self,
+                                    FlatpakBwrap *bwrap,
+                                    PvEnviron *container_env);
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (PvRuntime, g_object_unref)
diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c
index ebed24d0a..ba37233a7 100644
--- a/pressure-vessel/wrap.c
+++ b/pressure-vessel/wrap.c
@@ -2417,22 +2417,8 @@ main (int argc,
                            (runtime != NULL),
                            is_flatpak_env);
 
-  if (runtime != NULL
-      && (pv_environ_is_locked(container_env, "PULSE_SERVER")
-          || is_flatpak_env)
-      && bwrap != NULL)
-    {
-      /* Make the PulseAudio driver the default.
-       * We do this unconditionally when we are under Flatpak for parity
-       * with the freedesktop.org Platform. */
-      const gchar *alsa_config = "pcm.!default pulse\n"
-                                 "ctl.!default pulse\n";
-
-      flatpak_bwrap_add_args_data (bwrap, "asound.conf",
-                                   alsa_config, -1,
-                                   "/etc/asound.conf",
-                                   NULL);
-    }
+  if (runtime != NULL)
+    pv_runtime_use_shared_sockets (runtime, bwrap, container_env);
 
   if (is_flatpak_env)
     {
-- 
GitLab