dbus-device-reservation.c 15.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* WirePlumber
 *
 * Copyright © 2020 Collabora Ltd.
 *    @author Julian Bouzas <julian.bouzas@collabora.com>
 *
 * SPDX-License-Identifier: MIT
 */

/* Generated with gdbus-codegen */
#include "reserve-device-interface.h"

#include <pipewire/pipewire.h>

#include "dbus-device-reservation.h"

#define DEVICE_RESERVATION_SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
#define DEVICE_RESERVATION_OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"

19
struct _WpDbusDeviceReservation
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
  GObject parent;

  /* Props */
  gint card_id;
  char *application_name;
  gint priority;
  char *app_dev_name;

  char *service_name;
  char *object_path;
  GDBusConnection *connection;
  guint owner_id;
  guint registered_id;
  GDBusMethodInvocation *pending_release;

  GTask *pending_task;
  char *pending_property_name;
};

enum {
  PROP_0,
  PROP_CARD_ID,
  PROP_APPLICATION_NAME,
  PROP_PRIORITY,
  PROP_APP_DEV_NAME,
};

enum
{
  SIGNAL_RELEASE,
  SIGNAL_LAST,
};

static guint device_reservation_signals[SIGNAL_LAST] = { 0 };

56
57
G_DEFINE_TYPE (WpDbusDeviceReservation, wp_dbus_device_reservation,
    G_TYPE_OBJECT)
58
59
60
61
62
63
64

static void
handle_method_call (GDBusConnection *connection, const char *sender,
    const char *object_path, const char *interface_name,
    const char *method_name, GVariant *parameters,
    GDBusMethodInvocation *invocation, gpointer data)
{
65
  WpDbusDeviceReservation *self = data;
66
67
68
69
70
71
72

  if (g_strcmp0 (method_name, "RequestRelease") == 0) {
    gint priority;
    g_variant_get (parameters, "(i)", &priority);

    if (priority > self->priority) {
      if (self->pending_release)
73
        wp_dbus_device_reservation_complete_release (self, FALSE);
74
      self->pending_release = g_object_ref (invocation);
75
      g_signal_emit (self, device_reservation_signals[SIGNAL_RELEASE], 0, FALSE);
76
    } else {
77
      wp_dbus_device_reservation_complete_release (self, FALSE);
78
79
80
81
82
83
84
85
86
    }
  }
}

static GVariant *
handle_get_property (GDBusConnection *connection, const char *sender,
    const char *object_path, const char *interface_name,
    const char *property, GError **error, gpointer data)
{
87
  WpDbusDeviceReservation *self = data;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  GVariant *ret = NULL;

  if (g_strcmp0 (property, "ApplicationName") == 0)
    ret = g_variant_new_string (self->application_name ? self->application_name : "");
  else if (g_strcmp0 (property, "ApplicationDeviceName") == 0)
    ret = g_variant_new_string (self->app_dev_name ? self->app_dev_name : "");
  else if (g_strcmp0 (property, "Priority") == 0)
    ret = g_variant_new_int32 (self->priority);

  return ret;
}

static void
on_bus_acquired (GDBusConnection *connection, const gchar *name,
    gpointer user_data)
{
104
  WpDbusDeviceReservation *self = user_data;
105
106
107
108
109
110
111
  g_autoptr (GError) error = NULL;
  static const GDBusInterfaceVTable interface_vtable = {
    handle_method_call,
    handle_get_property,
    NULL,  /* Don't allow setting a property */
  };

112
  wp_debug_object (self, "bus acquired");
113
114
115

  self->registered_id = g_dbus_connection_register_object (connection,
      self->object_path,
116
      wp_org_freedesktop_reserve_device1_interface_info (),
117
118
119
120
121
122
123
124
125
126
127
128
      &interface_vtable,
      g_object_ref (self),
      g_object_unref,
      &error);
  g_return_if_fail (!error);
  g_return_if_fail (self->registered_id > 0);
}

static void
on_name_acquired (GDBusConnection *connection, const gchar *name,
    gpointer user_data)
{
129
130
  WpDbusDeviceReservation *self = user_data;
  wp_debug_object (self, "name acquired");
131
132
133
134
135
136
137
138
139
140
141

  self->connection = connection;

  /* Trigger the acquired task */
  if (self->pending_task) {
    g_task_return_pointer (self->pending_task, GUINT_TO_POINTER (TRUE), NULL);
    g_clear_object (&self->pending_task);
  }
}

static void
142
wp_dbus_device_reservation_unregister_object (WpDbusDeviceReservation *self)
143
144
145
146
147
148
149
150
151
152
153
154
{
  if (self->connection && self->registered_id > 0) {
    g_dbus_connection_unregister_object (self->connection, self->registered_id);
    self->registered_id = 0;
    self->connection = NULL;
  }
}

static void
on_name_lost (GDBusConnection *connection, const gchar *name,
    gpointer user_data)
{
155
156
  WpDbusDeviceReservation *self = user_data;
  wp_debug_object (self, "name lost");
157
158
159
160

  self->connection = connection;

  /* Unregister object */
161
  wp_dbus_device_reservation_unregister_object (self);
162
163
164
165
166
167
168
169
170
171
172
173

  /* Trigger the acquired task */
  if (self->pending_task) {
    GError *error = g_error_new (WP_DOMAIN_LIBRARY,
        WP_LIBRARY_ERROR_OPERATION_FAILED,
        "dbus name lost before acquiring (connection=%p)", connection);
    g_task_return_error (self->pending_task, error);
    g_clear_object (&self->pending_task);
  }
}

static void
174
wp_dbus_device_reservation_constructed (GObject * object)
175
{
176
  WpDbusDeviceReservation *self = WP_DBUS_DEVICE_RESERVATION (object);
177
178
179
180
181
182
183

  /* Set service name and object path */
  self->service_name = g_strdup_printf (DEVICE_RESERVATION_SERVICE_PREFIX
      "Audio%d", self->card_id);
  self->object_path = g_strdup_printf (DEVICE_RESERVATION_OBJECT_PREFIX
      "Audio%d", self->card_id);

184
  G_OBJECT_CLASS (wp_dbus_device_reservation_parent_class)->constructed (object);
185
186
187
}

static void
188
189
wp_dbus_device_reservation_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
190
{
191
  WpDbusDeviceReservation *self = WP_DBUS_DEVICE_RESERVATION (object);
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

  switch (property_id) {
  case PROP_CARD_ID:
    g_value_set_int (value, self->card_id);
    break;
  case PROP_APPLICATION_NAME:
    g_value_set_string (value, self->application_name);
    break;
  case PROP_PRIORITY:
    g_value_set_int (value, self->priority);
    break;
  case PROP_APP_DEV_NAME:
    g_value_set_string (value, self->app_dev_name);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}

static void
213
wp_dbus_device_reservation_set_property (GObject * object,
214
215
    guint property_id, const GValue * value, GParamSpec * pspec)
{
216
  WpDbusDeviceReservation *self = WP_DBUS_DEVICE_RESERVATION (object);
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

  switch (property_id) {
  case PROP_CARD_ID:
    self->card_id = g_value_get_int (value);
    break;
  case PROP_APPLICATION_NAME:
    g_clear_pointer (&self->application_name, g_free);
    self->application_name = g_value_dup_string (value);
    break;
  case PROP_PRIORITY:
    self->priority = g_value_get_int(value);
    break;
  case PROP_APP_DEV_NAME:
    g_clear_pointer (&self->app_dev_name, g_free);
    self->app_dev_name = g_value_dup_string (value);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}

static void
240
wp_dbus_device_reservation_finalize (GObject * object)
241
{
242
  WpDbusDeviceReservation *self = WP_DBUS_DEVICE_RESERVATION (object);
243
244
245
246
247
248
249
250
251
252
253

  /* Finish pending task */
  if (self->pending_task) {
    GError *error = g_error_new (WP_DOMAIN_LIBRARY,
        WP_LIBRARY_ERROR_OPERATION_FAILED, "finishing before task is done");
    g_task_return_error (self->pending_task, error);
  }
  g_clear_object (&self->pending_task);
  g_clear_pointer (&self->pending_property_name, g_free);

  /* Unregister and release */
254
255
  wp_dbus_device_reservation_unregister_object (self);
  wp_dbus_device_reservation_release (self);
256
257
258
259
260
261
262
263
264

  g_clear_object (&self->pending_release);
  g_clear_pointer (&self->service_name, g_free);
  g_clear_pointer (&self->object_path, g_free);

  /* Props */
  g_clear_pointer (&self->application_name, g_free);
  g_clear_pointer (&self->app_dev_name, g_free);

265
  G_OBJECT_CLASS ( wp_dbus_device_reservation_parent_class)->finalize (object);
266
267
268
}

static void
269
wp_dbus_device_reservation_init (WpDbusDeviceReservation * self)
270
271
272
273
{
}

static void
274
wp_dbus_device_reservation_class_init (WpDbusDeviceReservationClass * klass)
275
276
277
{
  GObjectClass *object_class = (GObjectClass *) klass;

278
279
280
281
  object_class->constructed = wp_dbus_device_reservation_constructed;
  object_class->get_property = wp_dbus_device_reservation_get_property;
  object_class->set_property = wp_dbus_device_reservation_set_property;
  object_class->finalize = wp_dbus_device_reservation_finalize;
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

  /* Properties */
  g_object_class_install_property (object_class, PROP_CARD_ID,
      g_param_spec_int ("card-id", "card-id",
          "The card Id", G_MININT, G_MAXINT, -1,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_APPLICATION_NAME,
      g_param_spec_string ("application-name", "application-name",
          "The application name", NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_PRIORITY,
      g_param_spec_int ("priority", "priority",
          "The priority", G_MININT, G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_APP_DEV_NAME,
      g_param_spec_string ("app-dev-name", "app-dev-name",
          "The application device name", NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));

  /* Signals */
  device_reservation_signals[SIGNAL_RELEASE] = g_signal_new (
      "release", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
304
      0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
305
306
}

307
308
309
WpDbusDeviceReservation *
wp_dbus_device_reservation_new (gint card_id, const char *application_name,
    gint priority, const char *app_dev_name)
310
{
311
  return g_object_new (WP_TYPE_DBUS_DEVICE_RESERVATION,
312
313
314
315
316
317
318
319
      "card-id", card_id,
      "application-name", application_name,
      "priority", priority,
      "app-dev-name", app_dev_name,
      NULL);
}

void
320
wp_dbus_device_reservation_release (WpDbusDeviceReservation *self)
321
{
322
  g_return_if_fail (WP_IS_DBUS_DEVICE_RESERVATION (self));
323
324
325
326
327
328
329
330
331
  if (self->owner_id == 0)
    return;

  /* Release */
  g_bus_unown_name (self->owner_id);
  self->owner_id = 0;
}

void
332
333
wp_dbus_device_reservation_complete_release (WpDbusDeviceReservation *self,
    gboolean res)
334
{
335
  g_return_if_fail (WP_IS_DBUS_DEVICE_RESERVATION (self));
336
337
338
339
340

  if (!self->pending_release)
    return;

  g_dbus_method_invocation_return_value (self->pending_release,
341
      g_variant_new ("(b)", res));
342
343
344
345
346
347
  g_clear_object (&self->pending_release);
}

static void
on_unowned (gpointer user_data)
{
348
349
  WpDbusDeviceReservation *self = user_data;
  wp_dbus_device_reservation_unregister_object (self);
350
351
352
353
  g_object_unref (self);
}

gboolean
354
355
wp_dbus_device_reservation_acquire (WpDbusDeviceReservation *self,
    GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
356
{
357
  g_return_val_if_fail (WP_IS_DBUS_DEVICE_RESERVATION (self), FALSE);
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  g_return_val_if_fail (!self->pending_task, FALSE);
  if (self->owner_id > 0)
    return FALSE;

  /* Set the new task */
  self->pending_task = g_task_new (self, cancellable, callback, user_data);

  /* Aquire */
  self->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, self->service_name,
      self->priority < INT32_MAX ?
      G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT : G_BUS_NAME_OWNER_FLAGS_NONE,
      on_bus_acquired, on_name_acquired, on_name_lost, g_object_ref (self),
      on_unowned);
  g_return_val_if_fail (self->owner_id > 0, FALSE);
  return TRUE;
}

static void
on_request_release_done (GObject *proxy, GAsyncResult *res, gpointer user_data)
{
378
  WpDbusDeviceReservation *self = user_data;
379
380
381
382
  g_autoptr (GError) error = NULL;
  gboolean ret;

  /* Finish */
383
384
  wp_org_freedesktop_reserve_device1_call_request_release_finish (
      WP_ORG_FREEDESKTOP_RESERVE_DEVICE1 (proxy),
385
386
387
388
389
390
391
392
393
394
395
396
397
398
      &ret, res, &error);

  /* Return */
  g_return_if_fail (self->pending_task);
  if (error)
    g_task_return_error (self->pending_task, g_steal_pointer (&error));
  else
    g_task_return_pointer (self->pending_task, GUINT_TO_POINTER (ret), NULL);
  g_clear_object (&self->pending_task);
}

static void
on_proxy_done_request_release (GObject *obj, GAsyncResult *res, gpointer data)
{
399
400
  WpDbusDeviceReservation *self = data;
  g_autoptr (WpOrgFreedesktopReserveDevice1) proxy = NULL;
401
402
403
  g_autoptr (GError) error = NULL;

  /* Finish */
404
  proxy = wp_org_freedesktop_reserve_device1_proxy_new_for_bus_finish (
405
406
407
408
409
410
411
412
413
414
415
416
      res, &error);

  /* Check for errors */
  g_return_if_fail (self->pending_task);
  if (error) {
    g_task_return_error (self->pending_task, g_steal_pointer (&error));
    g_clear_object (&self->pending_task);
    return;
  }

  /* Request release */
  g_return_if_fail (proxy);
417
  wp_org_freedesktop_reserve_device1_call_request_release (proxy,
418
419
420
421
     self->priority, NULL, on_request_release_done, self);
}

gboolean
422
423
wp_dbus_device_reservation_request_release (WpDbusDeviceReservation *self,
    GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
424
{
425
  g_return_val_if_fail (WP_IS_DBUS_DEVICE_RESERVATION (self), FALSE);
426
427
428
429
430
431
  g_return_val_if_fail (!self->pending_task, FALSE);

  /* Set the new task */
  self->pending_task = g_task_new (self, cancellable, callback, user_data);

  /* Get the proxy */
432
  wp_org_freedesktop_reserve_device1_proxy_new_for_bus (
433
434
435
436
437
438
439
440
      G_BUS_TYPE_SESSION, G_BUS_NAME_OWNER_FLAGS_NONE, self->service_name,
      self->object_path, NULL, on_proxy_done_request_release, self);
  return TRUE;
}

static void
on_proxy_done_request_property (GObject *obj, GAsyncResult *res, gpointer data)
{
441
442
  WpDbusDeviceReservation *self = data;
  g_autoptr (WpOrgFreedesktopReserveDevice1) proxy = NULL;
443
444
445
  g_autoptr (GError) error = NULL;

  /* Finish */
446
447
  proxy = wp_org_freedesktop_reserve_device1_proxy_new_for_bus_finish (res,
      &error);
448
449
450
451
452
453
454
455
456
457
458
459
460
461

  /* Check for errors */
  g_return_if_fail (self->pending_task);
  if (error) {
    g_task_return_error (self->pending_task, g_steal_pointer (&error));
    g_clear_object (&self->pending_task);
    return;
  }

  /* Request the property */
  g_return_if_fail (proxy);
  g_return_if_fail (self->pending_property_name);

  if (g_strcmp0 (self->pending_property_name, "ApplicationName") == 0) {
462
    char *v = wp_org_freedesktop_reserve_device1_dup_application_name (proxy);
463
464
465
    g_task_return_pointer (self->pending_task, v, g_free);
  }
  else if (g_strcmp0 (self->pending_property_name, "ApplicationDeviceName") == 0) {
466
    char *v = wp_org_freedesktop_reserve_device1_dup_application_device_name (proxy);
467
468
469
    g_task_return_pointer (self->pending_task, v, g_free);
  }
  else if (g_strcmp0 (self->pending_property_name, "Priority") == 0) {
470
    gint v = wp_org_freedesktop_reserve_device1_get_priority (proxy);
471
472
473
474
475
476
477
478
479
480
481
482
    g_task_return_pointer (self->pending_task, GINT_TO_POINTER (v), NULL);
  }
  else {
    GError *error = g_error_new (WP_DOMAIN_LIBRARY,
        WP_LIBRARY_ERROR_OPERATION_FAILED, "invalid property '%s' on proxy",
        self->pending_property_name);
    g_task_return_error (self->pending_task, error);
  }
  g_clear_object (&self->pending_task);
}

gboolean
483
484
485
wp_dbus_device_reservation_request_property (WpDbusDeviceReservation *self,
    const char *name, GCancellable *cancellable, GAsyncReadyCallback callback,
    gpointer user_data)
486
{
487
  g_return_val_if_fail (WP_IS_DBUS_DEVICE_RESERVATION (self), FALSE);
488
489
490
491
492
493
494
495
  g_return_val_if_fail (!self->pending_task, FALSE);

  /* Set the new task and property name */
  self->pending_task = g_task_new (self, cancellable, callback, user_data);
  g_clear_pointer (&self->pending_property_name, g_free);
  self->pending_property_name = g_strdup (name);

  /* Get the proxy */
496
  wp_org_freedesktop_reserve_device1_proxy_new_for_bus (
497
498
499
500
501
502
      G_BUS_TYPE_SESSION, G_BUS_NAME_OWNER_FLAGS_NONE, self->service_name,
      self->object_path, NULL, on_proxy_done_request_property, self);
  return TRUE;
}

gpointer
503
504
wp_dbus_device_reservation_async_finish (WpDbusDeviceReservation *self,
    GAsyncResult * res, GError ** error)
505
{
506
  g_return_val_if_fail (WP_IS_DBUS_DEVICE_RESERVATION (self), FALSE);
507
508
509
510
  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);

  return g_task_propagate_pointer (G_TASK (res), error);
}