| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2019-2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <wp/wp.h> |
| 10 |
#include <glib-unix.h> |
| 11 |
#include <pipewire/pipewire.h> |
| 12 |
#include <locale.h> |
| 13 |
|
| 14 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wireplumber") |
| 15 |
|
| 16 |
enum WpExitCode |
| 17 |
{ |
| 18 |
/* based on sysexits.h */ |
| 19 |
WP_EXIT_OK = 0, |
| 20 |
WP_EXIT_USAGE = 64, /* command line usage error */ |
| 21 |
WP_EXIT_UNAVAILABLE = 69, /* service unavailable */ |
| 22 |
WP_EXIT_SOFTWARE = 70, /* internal software error */ |
| 23 |
WP_EXIT_CONFIG = 78, /* configuration error */ |
| 24 |
}; |
| 25 |
|
| 26 |
static gboolean show_version = FALSE; |
| 27 |
static gchar * config_file = NULL; |
| 28 |
static gchar * profile = NULL; |
| 29 |
|
| 30 |
static GOptionEntry entries[] = |
| 31 |
{ |
| 32 |
{ "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &show_version, |
| 33 |
"Show version", NULL }, |
| 34 |
{ "config-file", 'c', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &config_file, |
| 35 |
"The configuration file to use", NULL }, |
| 36 |
{ "profile", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &profile, |
| 37 |
"The profile to load", NULL }, |
| 38 |
{ NULL } |
| 39 |
}; |
| 40 |
|
| 41 |
/*** WpDaemon ***/ |
| 42 |
|
| 43 |
typedef struct |
| 44 |
{ |
| 45 |
WpCore *core; |
| 46 |
GMainLoop *loop; |
| 47 |
gint exit_code; |
| 48 |
} WpDaemon; |
| 49 |
|
| 50 |
static void |
| 51 |
daemon_clear (WpDaemon * self) |
| 52 |
{ |
| 53 |
g_clear_pointer (&self->loop, g_main_loop_unref); |
| 54 |
g_clear_object (&self->core); |
| 55 |
} |
| 56 |
|
| 57 |
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (WpDaemon, daemon_clear) |
| 58 |
|
| 59 |
static void |
| 60 |
daemon_exit (WpDaemon * d, gint code) |
| 61 |
{ |
| 62 |
/* replace OK with an error, but do not replace error with OK */ |
| 63 |
if (d->exit_code == WP_EXIT_OK) |
| 64 |
d->exit_code = code; |
| 65 |
g_main_loop_quit (d->loop); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
on_disconnected (WpCore *core, WpDaemon * d) |
| 70 |
{ |
| 71 |
wp_info ("disconnected from pipewire"); |
| 72 |
daemon_exit (d, WP_EXIT_OK); |
| 73 |
} |
| 74 |
|
| 75 |
static gboolean |
| 76 |
signal_handler (int signal, gpointer data) |
| 77 |
{ |
| 78 |
WpDaemon *d = data; |
| 79 |
if (signal == SIGTERM) |
| 80 |
wp_info ("stopped by signal: %s", strsignal (signal)); |
| 81 |
else |
| 82 |
wp_notice ("stopped by signal: %s", strsignal (signal)); |
| 83 |
daemon_exit (d, WP_EXIT_OK); |
| 84 |
return G_SOURCE_CONTINUE; |
| 85 |
} |
| 86 |
|
| 87 |
static gboolean |
| 88 |
signal_handler_int (gpointer data) |
| 89 |
{ |
| 90 |
return signal_handler (SIGINT, data); |
| 91 |
} |
| 92 |
|
| 93 |
static gboolean |
| 94 |
signal_handler_hup (gpointer data) |
| 95 |
{ |
| 96 |
return signal_handler (SIGHUP, data); |
| 97 |
} |
| 98 |
|
| 99 |
static gboolean |
| 100 |
signal_handler_term (gpointer data) |
| 101 |
{ |
| 102 |
return signal_handler (SIGTERM, data); |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
on_core_activated (WpObject * core, GAsyncResult * res, WpDaemon * d) |
| 107 |
{ |
| 108 |
g_autoptr (GError) error = NULL; |
| 109 |
|
| 110 |
if (!wp_object_activate_finish (core, res, &error)) { |
| 111 |
fprintf (stderr, "%s\n", error->message); |
| 112 |
|
| 113 |
switch (error->code) { |
| 114 |
case WP_LIBRARY_ERROR_SERVICE_UNAVAILABLE: |
| 115 |
daemon_exit (d, WP_EXIT_UNAVAILABLE); |
| 116 |
break; |
| 117 |
|
| 118 |
case WP_LIBRARY_ERROR_INVALID_ARGUMENT: |
| 119 |
daemon_exit (d, WP_EXIT_CONFIG); |
| 120 |
break; |
| 121 |
|
| 122 |
default: |
| 123 |
daemon_exit (d, WP_EXIT_SOFTWARE); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static void |
| 129 |
warn_about_deprecated_config (void) |
| 130 |
{ |
| 131 |
g_autoptr (WpIterator) it = NULL; |
| 132 |
g_auto (GValue) val = G_VALUE_INIT; |
| 133 |
gboolean detected = FALSE; |
| 134 |
|
| 135 |
/* test only in directories that were used in 0.4 */ |
| 136 |
const WpBaseDirsFlags flags = WP_BASE_DIRS_ENV_CONFIG | |
| 137 |
WP_BASE_DIRS_XDG_CONFIG_HOME | |
| 138 |
WP_BASE_DIRS_BUILD_SYSCONFDIR | |
| 139 |
WP_BASE_DIRS_BUILD_DATADIR | |
| 140 |
WP_BASE_DIRS_FLAG_SUBDIR_WIREPLUMBER; |
| 141 |
|
| 142 |
it = wp_base_dirs_new_files_iterator ( |
| 143 |
flags, "main.lua.d", ".lua"); |
| 144 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 145 |
const gchar *file = g_value_get_string (&val); |
| 146 |
wp_notice ("Old configuration file detected: %s", file); |
| 147 |
detected = TRUE; |
| 148 |
} |
| 149 |
g_clear_pointer (&it, wp_iterator_unref); |
| 150 |
|
| 151 |
it = wp_base_dirs_new_files_iterator ( |
| 152 |
flags, "policy.lua.d", ".lua"); |
| 153 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 154 |
const gchar *file = g_value_get_string (&val); |
| 155 |
wp_notice ("Old configuration file detected: %s", file); |
| 156 |
detected = TRUE; |
| 157 |
} |
| 158 |
g_clear_pointer (&it, wp_iterator_unref); |
| 159 |
|
| 160 |
it = wp_base_dirs_new_files_iterator ( |
| 161 |
flags, "bluetooth.lua.d", ".lua"); |
| 162 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 163 |
const gchar *file = g_value_get_string (&val); |
| 164 |
wp_notice ("Old configuration file detected: %s", file); |
| 165 |
detected = TRUE; |
| 166 |
} |
| 167 |
g_clear_pointer (&it, wp_iterator_unref); |
| 168 |
|
| 169 |
if (detected) { |
| 170 |
wp_warning ( |
| 171 |
"Lua configuration files are NOT supported in WirePlumber 0.5. " |
| 172 |
"You need to port them to the new format if you want to use them.\n" |
| 173 |
"-> See https://pipewire.pages.freedesktop.org/wireplumber/daemon/configuration/migration.html"); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
gint |
| 178 |
main (gint argc, gchar **argv) |
| 179 |
{ |
| 180 |
g_auto (WpDaemon) d = {0}; |
| 181 |
g_autoptr (GOptionContext) context = NULL; |
| 182 |
g_autoptr (GError) error = NULL; |
| 183 |
g_autoptr (WpProperties) properties = NULL; |
| 184 |
g_autoptr (WpConf) conf = NULL; |
| 185 |
|
| 186 |
setlocale (LC_ALL, ""); |
| 187 |
setlocale (LC_NUMERIC, "C"); |
| 188 |
wp_init (WP_INIT_ALL); |
| 189 |
|
| 190 |
context = g_option_context_new ("- PipeWire Session/Policy Manager"); |
| 191 |
g_option_context_add_main_entries (context, entries, NULL); |
| 192 |
if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 193 |
fprintf (stderr, "%s\n", error->message); |
| 194 |
return WP_EXIT_USAGE; |
| 195 |
} |
| 196 |
|
| 197 |
if (show_version) { |
| 198 |
g_print ("%s\n" |
| 199 |
"Compiled with libwireplumber %s\n" |
| 200 |
"Linked with libwireplumber %s\n", |
| 201 |
argv[0], |
| 202 |
WIREPLUMBER_VERSION, |
| 203 |
wp_get_library_version()); |
| 204 |
return WP_EXIT_OK; |
| 205 |
} |
| 206 |
|
| 207 |
if (!config_file) |
| 208 |
config_file = "wireplumber.conf"; |
| 209 |
if (!profile) |
| 210 |
profile = "main"; |
| 211 |
|
| 212 |
/* load configuration */ |
| 213 |
conf = wp_conf_new_open (config_file, NULL, &error); |
| 214 |
if (!conf) { |
| 215 |
fprintf (stderr, "Failed to load configuration: %s\n", error->message); |
| 216 |
return WP_EXIT_CONFIG; |
| 217 |
} |
| 218 |
|
| 219 |
warn_about_deprecated_config (); |
| 220 |
|
| 221 |
/* prepare core properties */ |
| 222 |
properties = wp_properties_new ( |
| 223 |
PW_KEY_APP_VERSION, WIREPLUMBER_VERSION, |
| 224 |
"wireplumber.daemon", "true", |
| 225 |
"wireplumber.profile", profile, |
| 226 |
NULL); |
| 227 |
|
| 228 |
if (g_str_equal (profile, "main")) { |
| 229 |
wp_properties_set (properties, PW_KEY_APP_NAME, "WirePlumber"); |
| 230 |
} else { |
| 231 |
g_autofree gchar *app_name = g_strdup_printf ("WirePlumber (%s)", profile); |
| 232 |
wp_properties_set (properties, PW_KEY_APP_NAME, app_name); |
| 233 |
} |
| 234 |
|
| 235 |
/* prefer manager socket */ |
| 236 |
if (pw_check_library_version(0, 3, 84)) |
| 237 |
wp_properties_set (properties, PW_KEY_REMOTE_NAME, |
| 238 |
("[" PW_DEFAULT_REMOTE "-manager," PW_DEFAULT_REMOTE "]")); |
| 239 |
|
| 240 |
/* init wireplumber daemon */ |
| 241 |
d.loop = g_main_loop_new (NULL, FALSE); |
| 242 |
d.core = wp_core_new (NULL, g_steal_pointer (&conf), |
| 243 |
g_steal_pointer (&properties)); |
| 244 |
g_signal_connect (d.core, "disconnected", G_CALLBACK (on_disconnected), &d); |
| 245 |
|
| 246 |
/* watch for exit signals */ |
| 247 |
g_unix_signal_add (SIGINT, signal_handler_int, &d); |
| 248 |
g_unix_signal_add (SIGTERM, signal_handler_term, &d); |
| 249 |
g_unix_signal_add (SIGHUP, signal_handler_hup, &d); |
| 250 |
|
| 251 |
wp_object_activate (WP_OBJECT (d.core), WP_OBJECT_FEATURES_ALL, NULL, |
| 252 |
(GAsyncReadyCallback) on_core_activated, &d); |
| 253 |
|
| 254 |
/* run */ |
| 255 |
g_main_loop_run (d.loop); |
| 256 |
wp_core_disconnect (d.core); |
| 257 |
return d.exit_code; |
| 258 |
} |