Debian Codegraph @

modules/module-reserve-device/reserve-device.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2021 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "reserve-device.h"
10 #include "plugin.h"
11 #include "transitions.h"
12 #include "reserve-device-interface.h"
13 #include "reserve-device-enums.h"
14
15 /*
16 * WpReserveDevice:
17 */
18 G_DEFINE_TYPE (WpReserveDevice, wp_reserve_device, G_TYPE_OBJECT)
19
20 enum
21 {
22 ACTION_ACQUIRE,
23 ACTION_RELEASE,
24 ACTION_DENY_RELEASE,
25 SIGNAL_RELEASE_REQUESTED,
26 LAST_SIGNAL
27 };
28
29 enum
30 {
31 PROP_0,
32 PROP_PLUGIN,
33 PROP_NAME,
34 PROP_APP_NAME,
35 PROP_APP_DEV_NAME,
36 PROP_PRIORITY,
37 PROP_STATE,
38 PROP_OWNER_APP_NAME,
39 };
40
41 static guint signals[LAST_SIGNAL] = { 0 };
42
43 static void
44 wp_reserve_device_init (WpReserveDevice * self)
45 {
46 g_weak_ref_init (&self->plugin, NULL);
47 g_weak_ref_init (&self->transition, NULL);
48 }
49
50 static void
51 on_got_proxy (GObject * src, GAsyncResult * res, WpReserveDevice *self)
52 {
53 g_autoptr (GError) error = NULL;
54 g_autoptr (WpOrgFreedesktopReserveDevice1) proxy =
55 wp_org_freedesktop_reserve_device1_proxy_new_finish (res, &error);
56
57 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
58 /* Operation canceled: self may be invalid by now. */
59 return;
60 }
61
62 g_clear_object (&self->get_owner_call);
63
64 if (!proxy) {
65 wp_info_object (self, "%s: Could not get proxy of remote reservation: %s",
66 self->name, error->message);
67 return;
68 }
69
70 wp_debug_object (self, "%s owned by: %s", self->name,
71 wp_org_freedesktop_reserve_device1_get_application_name (proxy));
72
73 /* ensure that we are still busy and there is no owner_app_name */
74 if (self->state == WP_RESERVE_DEVICE_STATE_BUSY && !self->owner_app_name) {
75 self->owner_app_name =
76 wp_org_freedesktop_reserve_device1_dup_application_name (proxy);
77 g_object_notify (G_OBJECT (self), "owner-application-name");
78 }
79 }
80
81 static void
82 update_owner_app_name (WpReserveDevice *self)
83 {
84 g_cancellable_cancel (self->get_owner_call);
85 g_clear_object (&self->get_owner_call);
86
87 if (self->state == WP_RESERVE_DEVICE_STATE_BUSY && !self->owner_app_name) {
88 /* create proxy */
89 g_autoptr (WpReserveDevicePlugin) plugin = g_weak_ref_get (&self->plugin);
90 g_autoptr (GDBusConnection) conn = NULL;
91 g_object_get (plugin->dbus, "connection", &conn, NULL);
92
93 self->get_owner_call = g_cancellable_new ();
94 wp_org_freedesktop_reserve_device1_proxy_new (conn,
95 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
96 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
97 self->service_name, self->object_path, self->get_owner_call,
98 (GAsyncReadyCallback) on_got_proxy, self);
99 }
100 else if (self->state != WP_RESERVE_DEVICE_STATE_BUSY && self->owner_app_name) {
101 g_clear_pointer (&self->owner_app_name, g_free);
102 g_object_notify (G_OBJECT (self), "owner-application-name");
103 }
104 }
105
106 static void
107 on_name_appeared (GDBusConnection *connection, const gchar *name,
108 const gchar *owner, gpointer user_data)
109 {
110 WpReserveDevice *self = WP_RESERVE_DEVICE (user_data);
111 g_autoptr (WpTransition) t = g_weak_ref_get (&self->transition);
112
113 if (!t || wp_transition_get_completed (t)) {
114 self->state = WP_RESERVE_DEVICE_STATE_BUSY;
115 wp_info_object (self, "%s busy (by %s)", name, owner);
116 g_object_notify (G_OBJECT (self), "state");
117 update_owner_app_name (self);
118 }
119 }
120
121 static void
122 on_name_vanished (GDBusConnection *connection, const gchar *name,
123 gpointer user_data)
124 {
125 WpReserveDevice *self = WP_RESERVE_DEVICE (user_data);
126 g_autoptr (WpTransition) t = g_weak_ref_get (&self->transition);
127
128 if (!t || wp_transition_get_completed (t)) {
129 self->state = WP_RESERVE_DEVICE_STATE_AVAILABLE;
130 wp_info_object (self, "%s released", name);
131 g_object_notify (G_OBJECT (self), "state");
132 update_owner_app_name (self);
133 }
134 }
135
136 static void
137 wp_reserve_device_constructed (GObject * object)
138 {
139 WpReserveDevice *self = WP_RESERVE_DEVICE (object);
140 g_autoptr (WpReserveDevicePlugin) plugin = g_weak_ref_get (&self->plugin);
141 g_autoptr (GDBusConnection) conn = NULL;
142
143 g_object_get (plugin->dbus, "connection", &conn, NULL);
144
145 self->service_name =
146 g_strdup_printf (FDO_RESERVE_DEVICE1_SERVICE ".%s", self->name);
147 self->object_path =
148 g_strdup_printf (FDO_RESERVE_DEVICE1_PATH "/%s", self->name);
149
150 /* Watch for the name */
151 self->watcher_id = g_bus_watch_name_on_connection (conn,
152 self->service_name, G_BUS_NAME_WATCHER_FLAGS_NONE,
153 on_name_appeared, on_name_vanished, self, NULL);
154
155 G_OBJECT_CLASS (wp_reserve_device_parent_class)->constructed (object);
156 }
157
158 static void
159 wp_reserve_device_finalize (GObject * object)
160 {
161 WpReserveDevice *self = WP_RESERVE_DEVICE (object);
162
163 if (self->watcher_id > 0)
164 g_bus_unwatch_name (self->watcher_id);
165 if (self->owner_id > 0)
166 g_bus_unown_name (self->owner_id);
167
168 g_cancellable_cancel (self->get_owner_call);
169 g_clear_object (&self->get_owner_call);
170
171 g_weak_ref_clear (&self->plugin);
172 g_weak_ref_clear (&self->transition);
173 g_clear_pointer (&self->name, g_free);
174 g_clear_pointer (&self->app_name, g_free);
175 g_clear_pointer (&self->app_dev_name, g_free);
176 g_clear_pointer (&self->service_name, g_free);
177 g_clear_pointer (&self->object_path, g_free);
178
179 G_OBJECT_CLASS (wp_reserve_device_parent_class)->finalize (object);
180 }
181
182 static void
183 on_acquire_transition_done (GObject *rd, GAsyncResult *res, gpointer data)
184 {
185 WpReserveDevice *self = WP_RESERVE_DEVICE (data);
186 g_autoptr (GError) error = NULL;
187
188 gboolean acquired = wp_reserve_device_acquire_transition_finish (res, &error);
189 if (error) {
190 wp_notice_object (self, "%s: Acquire error: %s", self->name,
191 error->message);
192 }
193
194 self->state = acquired ?
195 WP_RESERVE_DEVICE_STATE_ACQUIRED : WP_RESERVE_DEVICE_STATE_BUSY;
196 g_object_notify (G_OBJECT (self), "state");
197 update_owner_app_name (self);
198 }
199
200 static void
201 wp_reserve_device_acquire (WpReserveDevice * self)
202 {
203 g_autoptr (WpTransition) t = g_weak_ref_get (&self->transition);
204 if (self->state == WP_RESERVE_DEVICE_STATE_ACQUIRED ||
205 (t && !wp_transition_get_completed (t))) {
206 wp_debug_object (self, "%s: already acquired or operation in progress",
207 self->name);
208 return;
209 }
210
211 g_autoptr (WpReserveDevicePlugin) plugin = g_weak_ref_get (&self->plugin);
212 WpTransition *transition = wp_reserve_device_acquire_transition_new (self,
213 NULL, on_acquire_transition_done, self);
214 g_weak_ref_set (&self->transition, transition);
215 wp_transition_advance (transition);
216 }
217
218 static void
219 wp_reserve_device_release (WpReserveDevice * self)
220 {
221 if (self->state != WP_RESERVE_DEVICE_STATE_ACQUIRED) {
222 wp_debug_object (self, "%s: not acquired", self->name);
223 return;
224 }
225
226 /* set state to AVAILABLE to ensure that on_name_lost()
227 does not emit SIGNAL_REQUEST_RELEASE */
228 /* on_name_vanished() will emit the state change */
229 self->state = WP_RESERVE_DEVICE_STATE_AVAILABLE;
230 wp_reserve_device_unown_name (self);
231
232 if (self->req_rel_invocation) {
233 wp_org_freedesktop_reserve_device1_complete_request_release (NULL,
234 self->req_rel_invocation, TRUE);
235 self->req_rel_invocation = NULL;
236 }
237 }
238
239 static void
240 wp_reserve_device_deny_release (WpReserveDevice * self, gboolean success)
241 {
242 if (self->req_rel_invocation) {
243 wp_org_freedesktop_reserve_device1_complete_request_release (NULL,
244 self->req_rel_invocation, FALSE);
245 self->req_rel_invocation = NULL;
246 }
247 }
248
249 static gboolean
250 wp_reserve_device_handle_request_release (WpOrgFreedesktopReserveDevice1 *iface,
251 GDBusMethodInvocation *invocation, gint priority, gpointer user_data)
252 {
253 WpReserveDevice *self = WP_RESERVE_DEVICE (user_data);
254
255 /* deny release if the priority is lower than ours */
256 if (priority < self->priority) {
257 wp_org_freedesktop_reserve_device1_complete_request_release (iface,
258 g_object_ref (invocation), FALSE);
259 return TRUE;
260 }
261
262 /* else, request the release of the device from the implementation;
263 if signal handlers are connected, assume the functionality is implemented,
264 otherwise return FALSE to let the iface return UnknownMethod */
265 if (g_signal_has_handler_pending (self,
266 signals[SIGNAL_RELEASE_REQUESTED], 0, FALSE)) {
267 self->req_rel_invocation = g_object_ref (invocation);
268 g_signal_emit (self, signals[SIGNAL_RELEASE_REQUESTED], 0, FALSE);
269 return TRUE;
270 }
271 return FALSE;
272 }
273
274 static void
275 wp_reserve_device_get_property (GObject * object, guint property_id,
276 GValue * value, GParamSpec * pspec)
277 {
278 WpReserveDevice *self = WP_RESERVE_DEVICE (object);
279
280 switch (property_id) {
281 case PROP_NAME:
282 g_value_set_string (value, self->name);
283 break;
284 case PROP_APP_NAME:
285 g_value_set_string (value, self->app_name);
286 break;
287 case PROP_APP_DEV_NAME:
288 g_value_set_string (value, self->app_dev_name);
289 break;
290 case PROP_PRIORITY:
291 g_value_set_int (value, self->priority);
292 break;
293 case PROP_STATE:
294 g_value_set_enum (value, self->state);
295 break;
296 case PROP_OWNER_APP_NAME:
297 switch (self->state) {
298 case WP_RESERVE_DEVICE_STATE_ACQUIRED:
299 g_value_set_string (value, self->app_name);
300 break;
301 default:
302 g_value_set_string (value, self->owner_app_name);
303 break;
304 }
305 break;
306 default:
307 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
308 break;
309 }
310 }
311
312 static void
313 wp_reserve_device_set_property (GObject * object,
314 guint property_id, const GValue * value, GParamSpec * pspec)
315 {
316 WpReserveDevice *self = WP_RESERVE_DEVICE (object);
317
318 switch (property_id) {
319 case PROP_PLUGIN:
320 g_weak_ref_set (&self->plugin, g_value_get_object (value));
321 break;
322 case PROP_NAME:
323 g_clear_pointer (&self->name, g_free);
324 self->name = g_value_dup_string (value);
325 break;
326 case PROP_APP_NAME:
327 g_clear_pointer (&self->app_name, g_free);
328 self->app_name = g_value_dup_string (value);
329 break;
330 case PROP_APP_DEV_NAME:
331 g_clear_pointer (&self->app_dev_name, g_free);
332 self->app_dev_name = g_value_dup_string (value);
333 break;
334 case PROP_PRIORITY:
335 self->priority = g_value_get_int(value);
336 break;
337 default:
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
339 break;
340 }
341 }
342
343 /**
344 * wp_reserve_device_class_init
345 *
346 * @param klass: the reserve device class
347 */
348 static void
349 wp_reserve_device_class_init (WpReserveDeviceClass * klass)
350 {
351 GObjectClass *object_class = (GObjectClass *) klass;
352
353 object_class->constructed = wp_reserve_device_constructed;
354 object_class->finalize = wp_reserve_device_finalize;
355 object_class->get_property = wp_reserve_device_get_property;
356 object_class->set_property = wp_reserve_device_set_property;
357
358 g_object_class_install_property (object_class, PROP_PLUGIN,
359 g_param_spec_object ("plugin", "plugin",
360 "The parent plugin instance", wp_reserve_device_plugin_get_type (),
361 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
362
363 g_object_class_install_property (object_class, PROP_NAME,
364 g_param_spec_string ("name", "name",
365 "The reservation name", NULL,
366 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
367
368 g_object_class_install_property (object_class, PROP_APP_NAME,
369 g_param_spec_string ("application-name", "application-name",
370 "The application name", NULL,
371 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
372
373 g_object_class_install_property (object_class, PROP_APP_DEV_NAME,
374 g_param_spec_string ("application-device-name", "application-device-name",
375 "The application device name", NULL,
376 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
377
378 g_object_class_install_property (object_class, PROP_PRIORITY,
379 g_param_spec_int ("priority", "priority",
380 "The priority", G_MININT, G_MAXINT, 0,
381 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
382
383 g_object_class_install_property (object_class, PROP_STATE,
384 g_param_spec_enum ("state", "state", "The state",
385 WP_TYPE_RESERVE_DEVICE_STATE, WP_RESERVE_DEVICE_STATE_UNKNOWN,
386 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
387
388 g_object_class_install_property (object_class, PROP_OWNER_APP_NAME,
389 g_param_spec_string ("owner-application-name", "owner-application-name",
390 "The owner application name", NULL,
391 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
392
393 /**
394 * WpReserveDevice acquire:
395 *
396 * @section signal_acquire_section acquire
397 */
398 signals[ACTION_ACQUIRE] = g_signal_new_class_handler (
399 "acquire", G_TYPE_FROM_CLASS (klass),
400 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
401 (GCallback) wp_reserve_device_acquire,
402 NULL, NULL, NULL, G_TYPE_NONE, 0);
403
404 /**
405 * WpReserveDevice release:
406 *
407 * @section signal_release_section release
408 */
409 signals[ACTION_RELEASE] = g_signal_new_class_handler (
410 "release", G_TYPE_FROM_CLASS (klass),
411 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
412 (GCallback) wp_reserve_device_release,
413 NULL, NULL, NULL, G_TYPE_NONE, 0);
414
415 /**
416 * WpReserveDevice deny-release:
417 *
418 * @section signal_deny_release_section deny-release
419 */
420 signals[ACTION_DENY_RELEASE] = g_signal_new_class_handler (
421 "deny-release", G_TYPE_FROM_CLASS (klass),
422 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
423 (GCallback) wp_reserve_device_deny_release,
424 NULL, NULL, NULL, G_TYPE_NONE, 0);
425
426 /**
427 * WpReserveDevice release-requested:
428 *
429 * @section signal_release_requested_section release-requested
430 *
431 * @em forced: %TRUE if the name was forcibly taken from us,
432 * %FALSE if the `RequestRelease()` d-bus method was called
433 *
434 * @brief Signaled when the device needs to be released. If @em forced is %FALSE,
435 * call [release](@ref signal_release_section) to release or
436 * [deny-release](@ref signal_deny_release_section)
437 * to refuse and return %FALSE from the `RequestRelease()` d-bus method.
438 */
439 signals[SIGNAL_RELEASE_REQUESTED] = g_signal_new (
440 "release-requested", G_TYPE_FROM_CLASS (klass),
441 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
442 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
443 }
444
445 void
446 wp_reserve_device_export_object (WpReserveDevice *self)
447 {
448 g_autoptr (WpReserveDevicePlugin) plugin = g_weak_ref_get (&self->plugin);
449 if (plugin) {
450 g_autoptr (GDBusObjectSkeleton) skeleton =
451 g_dbus_object_skeleton_new (self->object_path);
452 g_autoptr (WpOrgFreedesktopReserveDevice1) iface =
453 wp_org_freedesktop_reserve_device1_skeleton_new ();
454 g_object_set (iface,
455 "priority", self->priority,
456 "application-name", self->app_name,
457 "application-device-name", self->app_dev_name,
458 NULL);
459 g_signal_connect_object (iface, "handle-request-release",
460 (GCallback) wp_reserve_device_handle_request_release, self, 0);
461 g_dbus_object_skeleton_add_interface (skeleton,
462 G_DBUS_INTERFACE_SKELETON (iface));
463 g_dbus_object_manager_server_export (plugin->manager, skeleton);
464 }
465 }
466
467 void
468 wp_reserve_device_unexport_object (WpReserveDevice *self)
469 {
470 g_autoptr (WpReserveDevicePlugin) plugin = g_weak_ref_get (&self->plugin);
471 if (plugin) {
472 wp_debug_object (self, "unexport %s", self->object_path);
473 g_dbus_object_manager_server_unexport (plugin->manager, self->object_path);
474 }
475 }
476
477 static void
478 on_name_acquired (GDBusConnection *connection, const gchar *name,
479 gpointer user_data)
480 {
481 WpReserveDevice *self = WP_RESERVE_DEVICE (user_data);
482 g_autoptr (WpTransition) t = g_weak_ref_get (&self->transition);
483
484 wp_debug_object (self, "%s acquired", name);
485
486 if (t)
487 wp_reserve_device_acquire_transition_name_acquired (t);
488 }
489
490 static void
491 on_name_lost (GDBusConnection *connection, const gchar *name,
492 gpointer user_data)
493 {
494 WpReserveDevice *self = WP_RESERVE_DEVICE (user_data);
495 g_autoptr (WpTransition) t = g_weak_ref_get (&self->transition);
496
497 wp_debug_object (self, "%s lost", name);
498
499 if (t) {
500 wp_reserve_device_acquire_transition_name_lost (t);
501 return;
502 }
503
504 if (self->state == WP_RESERVE_DEVICE_STATE_ACQUIRED) {
505 /* Emit release signal with forced set to TRUE */
506 g_signal_emit (self, signals[SIGNAL_RELEASE_REQUESTED], 0, TRUE);
507 wp_reserve_device_unown_name (self);
508 }
509
510 wp_reserve_device_unexport_object (self);
511 }
512
513 void
514 wp_reserve_device_own_name (WpReserveDevice * self, gboolean force)
515 {
516 g_return_if_fail (self->owner_id == 0);
517
518 g_autoptr (WpReserveDevicePlugin) plugin = g_weak_ref_get (&self->plugin);
519 if (plugin) {
520 g_autoptr (GDBusConnection) conn = NULL;
521 g_object_get (plugin->dbus, "connection", &conn, NULL);
522
523 GBusNameOwnerFlags flags = G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE;
524 if (self->priority != G_MAXINT32)
525 flags |= G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
526 if (force)
527 flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
528
529 wp_debug_object (self, "request ownership of %s", self->service_name);
530
531 self->owner_id = g_bus_own_name_on_connection (conn,
532 self->service_name, flags, on_name_acquired, on_name_lost, self, NULL);
533 }
534 }
535
536 void
537 wp_reserve_device_unown_name (WpReserveDevice * self)
538 {
539 if (self->owner_id) {
540 wp_debug_object (self, "drop ownership of %s", self->service_name);
541 g_bus_unown_name (self->owner_id);
542 self->owner_id = 0;
543 }
544 }