From 1962a8ca1c5453fa1682b45e0a2db1f4a79ef0c9 Mon Sep 17 00:00:00 2001
From: George Kiagiadakis <george.kiagiadakis@collabora.com>
Date: Wed, 5 Jun 2019 18:38:56 +0300
Subject: [PATCH] daemon: quit when the pipewire remote disconnects or signals
 an error

This is implemented in a slightly hacky way, we register the GMainLoop
as a global object and use it from the module to quit the daemon.
This is bad design because the module assumes it is loaded inside
our daemon.

In the future, this should change. It looks like we should have an
object that tracks the state of PipeWire and main() should track
state changes of that object and decide what to do.
---
 modules/module-pipewire.c | 22 ++++++++++++++++------
 src/main.c                |  2 ++
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/modules/module-pipewire.c b/modules/module-pipewire.c
index 986bd5dd..fbcf04fc 100644
--- a/modules/module-pipewire.c
+++ b/modules/module-pipewire.c
@@ -110,14 +110,24 @@ on_remote_state_changed (void *d, enum pw_remote_state old_state,
         &data->registry_listener, &registry_events, data);
     break;
 
-  case PW_REMOTE_STATE_UNCONNECTED:
-    // TODO quit wireplumber
+  case PW_REMOTE_STATE_UNCONNECTED: {
+    g_autoptr (WpCore) core = wp_module_get_core (data->module);
+    if (core) {
+      g_message ("disconnected from PipeWire");
+      g_main_loop_quit (wp_core_get_global (core,
+              g_quark_from_string ("main-loop")));
+    }
     break;
-
-  case PW_REMOTE_STATE_ERROR:
-    // TODO quit wireplumber
+  }
+  case PW_REMOTE_STATE_ERROR: {
+    g_autoptr (WpCore) core = wp_module_get_core (data->module);
+    if (core) {
+      g_message ("PipeWire remote error: %s", error);
+      g_main_loop_quit (wp_core_get_global (core,
+              g_quark_from_string ("main-loop")));
+    }
     break;
-
+  }
   default:
     break;
   }
diff --git a/src/main.c b/src/main.c
index 5c70c7ce..27740021 100644
--- a/src/main.c
+++ b/src/main.c
@@ -197,6 +197,8 @@ main (gint argc, gchar **argv)
 
   /* init main loop */
   data.loop = loop = g_main_loop_new (NULL, FALSE);
+  wp_core_register_global (core, g_quark_from_static_string ("main-loop"),
+      g_main_loop_ref (loop), (GDestroyNotify) g_main_loop_unref);
 
   /* watch for exit signals */
 
-- 
GitLab