Debian Codegraph @

src/tools/wpexec.c

raw
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/keys.h>
12 #include <stdio.h>
13 #include <locale.h>
14
15 WP_DEFINE_LOCAL_LOG_TOPIC ("wpexec")
16
17 #define WP_DOMAIN_DAEMON (wp_domain_daemon_quark ())
18 static G_DEFINE_QUARK (wireplumber-daemon, wp_domain_daemon);
19
20 enum WpExitCode
21 {
22 /* based on sysexits.h */
23 WP_EXIT_OK = 0,
24 WP_EXIT_USAGE = 64, /* command line usage error */
25 WP_EXIT_UNAVAILABLE = 69, /* service unavailable */
26 WP_EXIT_SOFTWARE = 70, /* internal software error */
27 };
28
29 static gchar * exec_script = NULL;
30 static gchar *exec_args_s = NULL;
31 static WpSpaJson *exec_args = NULL;
32
33 static gboolean
34 parse_exec_script_arg (const gchar *option_name, const gchar *value,
35 gpointer data, GError **error)
36 {
37 /* the first argument is the script */
38 if (!exec_script) {
39 exec_script = g_strdup (value);
40 return TRUE;
41 }
42
43 /* the second argument is a json object with script arguments */
44 if (!exec_args) {
45 exec_args_s = g_strdup (value);
46 exec_args = wp_spa_json_new_wrap_string (exec_args_s);
47 if (!exec_args || !wp_spa_json_is_object (exec_args)) {
48 g_set_error (error, WP_DOMAIN_DAEMON, WP_EXIT_USAGE,
49 "script argument must be a JSON object");
50 return FALSE;
51 }
52 }
53
54 return TRUE;
55 }
56
57 static GOptionEntry entries[] =
58 {
59 { G_OPTION_REMAINING, 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_CALLBACK,
60 parse_exec_script_arg, NULL, NULL },
61 { NULL }
62 };
63
64 /*** WpInitTransition ***/
65
66 struct _WpInitTransition
67 {
68 WpTransition parent;
69 guint plugins_loaded;
70 };
71
72 enum {
73 STEP_CONNECT = WP_TRANSITION_STEP_CUSTOM_START,
74 STEP_ACTIVATE_PLUGINS,
75 STEP_ACTIVATE_SCRIPT,
76 };
77
78 G_DECLARE_FINAL_TYPE (WpInitTransition, wp_init_transition,
79 WP, INIT_TRANSITION, WpTransition)
80 G_DEFINE_TYPE (WpInitTransition, wp_init_transition, WP_TYPE_TRANSITION)
81
82 static void
83 wp_init_transition_init (WpInitTransition * self)
84 {
85 }
86
87 static guint
88 wp_init_transition_get_next_step (WpTransition * transition, guint step)
89 {
90 switch (step) {
91 case WP_TRANSITION_STEP_NONE: return STEP_CONNECT;
92 case STEP_CONNECT: return STEP_ACTIVATE_PLUGINS;
93 case STEP_ACTIVATE_SCRIPT: return WP_TRANSITION_STEP_NONE;
94
95 case STEP_ACTIVATE_PLUGINS: {
96 WpInitTransition *self = WP_INIT_TRANSITION (transition);
97 if (self->plugins_loaded == 2)
98 return STEP_ACTIVATE_SCRIPT;
99 else
100 return STEP_ACTIVATE_PLUGINS;
101 }
102
103 default:
104 g_return_val_if_reached (WP_TRANSITION_STEP_ERROR);
105 }
106 }
107
108 static void
109 on_plugin_loaded (WpCore * core, GAsyncResult * res, WpInitTransition *self)
110 {
111 GError *error = NULL;
112
113 if (!wp_core_load_component_finish (core, res, &error)) {
114 wp_transition_return_error (WP_TRANSITION (self), error);
115 return;
116 }
117 ++self->plugins_loaded;
118 wp_transition_advance (WP_TRANSITION (self));
119 }
120
121 static void
122 wp_init_transition_execute_step (WpTransition * transition, guint step)
123 {
124 WpInitTransition *self = WP_INIT_TRANSITION (transition);
125 WpCore *core = wp_transition_get_source_object (transition);
126
127 switch (step) {
128 case STEP_CONNECT:
129 g_signal_connect_object (core, "connected",
130 G_CALLBACK (wp_transition_advance), transition, G_CONNECT_SWAPPED);
131
132 if (!wp_core_connect (core)) {
133 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_DAEMON,
134 WP_EXIT_UNAVAILABLE, "Failed to connect to PipeWire"));
135 return;
136 }
137 break;
138
139 case STEP_ACTIVATE_PLUGINS: {
140 wp_core_load_component (core, "libwireplumber-module-lua-scripting",
141 "module", NULL, NULL, NULL, (GAsyncReadyCallback) on_plugin_loaded, self);
142 wp_core_load_component (core, "libwireplumber-module-standard-event-source",
143 "module", NULL, NULL, NULL, (GAsyncReadyCallback) on_plugin_loaded, self);
144 break;
145 }
146
147 case STEP_ACTIVATE_SCRIPT: {
148 wp_core_load_component (core, exec_script, "script/lua", exec_args, NULL, NULL,
149 (GAsyncReadyCallback) on_plugin_loaded, self);
150 break;
151 }
152
153 case WP_TRANSITION_STEP_ERROR:
154 break;
155
156 default:
157 g_assert_not_reached ();
158 }
159 }
160
161 static void
162 wp_init_transition_class_init (WpInitTransitionClass * klass)
163 {
164 WpTransitionClass * transition_class = (WpTransitionClass *) klass;
165
166 transition_class->get_next_step = wp_init_transition_get_next_step;
167 transition_class->execute_step = wp_init_transition_execute_step;
168 }
169
170 /*** WpExec ***/
171
172 typedef struct
173 {
174 WpCore *core;
175 GMainLoop *loop;
176 gint exit_code;
177 } WpExec;
178
179 static void
180 wpexec_clear (WpExec * self)
181 {
182 g_clear_pointer (&self->loop, g_main_loop_unref);
183 g_clear_object (&self->core);
184 }
185
186 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (WpExec, wpexec_clear)
187
188 static gboolean
189 signal_handler (gpointer data)
190 {
191 WpExec *d = data;
192 g_main_loop_quit (d->loop);
193 return G_SOURCE_REMOVE;
194 }
195
196 static gboolean
197 init_start (WpTransition * transition)
198 {
199 wp_transition_advance (transition);
200 return G_SOURCE_REMOVE;
201 }
202
203 static void
204 init_done (WpCore * core, GAsyncResult * res, WpExec * d)
205 {
206 g_autoptr (GError) error = NULL;
207 if (!wp_transition_finish (res, &error)) {
208 fprintf (stderr, "%s\n", error->message);
209 d->exit_code = (error->domain == WP_DOMAIN_DAEMON) ?
210 error->code : WP_EXIT_SOFTWARE;
211 g_main_loop_quit (d->loop);
212 }
213 }
214
215 gint
216 main (gint argc, gchar **argv)
217 {
218 g_auto (WpExec) d = {0};
219 g_autoptr (GOptionContext) context = NULL;
220 g_autoptr (GError) error = NULL;
221
222 setlocale (LC_ALL, "");
223 setlocale (LC_NUMERIC, "C");
224 wp_init (WP_INIT_ALL);
225
226 context = g_option_context_new ("- WirePlumber script interpreter");
227 g_option_context_add_main_entries (context, entries, NULL);
228 if (!g_option_context_parse (context, &argc, &argv, &error)) {
229 fprintf (stderr, "%s\n", error->message);
230 return WP_EXIT_USAGE;
231 }
232
233 /* init wireplumber core */
234 d.loop = g_main_loop_new (NULL, FALSE);
235 d.core = wp_core_new (NULL, NULL, wp_properties_new (
236 PW_KEY_APP_NAME, "wpexec",
237 NULL));
238 g_signal_connect_swapped (d.core, "disconnected",
239 G_CALLBACK (g_main_loop_quit), d.loop);
240
241 /* watch for exit signals */
242 g_unix_signal_add (SIGINT, signal_handler, &d);
243 g_unix_signal_add (SIGTERM, signal_handler, &d);
244 g_unix_signal_add (SIGHUP, signal_handler, &d);
245
246 /* initialization transition */
247 g_idle_add ((GSourceFunc) init_start,
248 wp_transition_new (wp_init_transition_get_type (), d.core,
249 NULL, (GAsyncReadyCallback) init_done, &d));
250
251 /* run */
252 g_main_loop_run (d.loop);
253 wp_core_disconnect (d.core);
254 return d.exit_code;
255 }