From 5348239ccbf4e8d0f1dc2c34f9eedad4b18efa10 Mon Sep 17 00:00:00 2001
From: Julian Bouzas <julian.bouzas@collabora.com>
Date: Fri, 13 Dec 2019 10:45:44 -0500
Subject: [PATCH] policy: add _get_session API for the policy manager

---
 lib/wp/policy.c | 34 ++++++++++++++++++++++++++++++++++
 lib/wp/policy.h |  3 +++
 2 files changed, 37 insertions(+)

diff --git a/lib/wp/policy.c b/lib/wp/policy.c
index 175c9a9a..7e7f647c 100644
--- a/lib/wp/policy.c
+++ b/lib/wp/policy.c
@@ -6,6 +6,8 @@
  * SPDX-License-Identifier: MIT
  */
 
+#include <pipewire/pipewire.h>
+
 #include "policy.h"
 #include "private.h"
 
@@ -16,6 +18,8 @@ struct _WpPolicyManager
   GObject parent;
   GList *policies;
   WpObjectManager *endpoints_om;
+  WpObjectManager *sessions_om;
+  GWeakRef curr_session;
 };
 
 enum {
@@ -31,6 +35,8 @@ static void
 wp_policy_manager_init (WpPolicyManager *self)
 {
   self->endpoints_om = wp_object_manager_new ();
+  self->sessions_om = wp_object_manager_new ();
+  g_weak_ref_init (&self->curr_session, NULL);
 }
 
 static void
@@ -40,6 +46,8 @@ wp_policy_manager_finalize (GObject *object)
 
   g_debug ("WpPolicyManager destroyed");
 
+  g_weak_ref_clear (&self->curr_session);
+  g_clear_object (&self->sessions_om);
   g_clear_object (&self->endpoints_om);
   g_list_free_full (self->policies, g_object_unref);
 
@@ -88,6 +96,16 @@ policy_mgr_endpoint_removed (WpObjectManager *om, WpBaseEndpoint *ep,
   }
 }
 
+static void
+policy_mgr_session_changed (WpObjectManager *om, WpPolicyManager *self)
+{
+  g_autoptr (GPtrArray) arr = NULL;
+
+  arr = wp_object_manager_get_objects (om, WP_TYPE_PROXY_SESSION);
+  if (arr->len > 0)
+    g_weak_ref_set (&self->curr_session, g_ptr_array_index (arr, 0));
+}
+
 /**
  * wp_policy_manager_get_instance:
  * @core: the #WpCore
@@ -116,12 +134,28 @@ wp_policy_manager_get_instance (WpCore *core)
         (GCallback) policy_mgr_endpoint_removed, mgr, 0);
     wp_core_install_object_manager (core, mgr->endpoints_om);
 
+    /* install the object manager to listen to changed sessions */
+    wp_object_manager_add_proxy_interest (mgr->sessions_om,
+        PW_TYPE_INTERFACE_Session, NULL,
+        WP_PROXY_FEATURE_INFO | WP_PROXY_SESSION_FEATURE_DEFAULT_ENDPOINT);
+    g_signal_connect_object (mgr->sessions_om, "objects-changed",
+        (GCallback) policy_mgr_session_changed, mgr, 0);
+    wp_core_install_object_manager (core, mgr->sessions_om);
+
     wp_core_register_object (core, g_object_ref (mgr));
   }
 
   return mgr;
 }
 
+WpSession *
+wp_policy_manager_get_session (WpPolicyManager *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return g_weak_ref_get (&self->curr_session);
+}
+
 static inline gboolean
 media_class_matches (const gchar * media_class, const gchar * lookup)
 {
diff --git a/lib/wp/policy.h b/lib/wp/policy.h
index ef335585..694690c4 100644
--- a/lib/wp/policy.h
+++ b/lib/wp/policy.h
@@ -10,6 +10,7 @@
 #define __WIREPLUMBER_POLICY_H__
 
 #include "base-endpoint.h"
+#include "session.h"
 
 G_BEGIN_DECLS
 
@@ -51,6 +52,8 @@ struct _WpPolicyClass
 };
 
 WpPolicyManager * wp_policy_manager_get_instance (WpCore *core);
+WpSession * wp_policy_manager_get_session (WpPolicyManager *self);
+
 GPtrArray * wp_policy_manager_list_endpoints (WpPolicyManager * self,
     const gchar * media_class);
 
-- 
GitLab