2023-02-08 18:12:00 +01:00
|
|
|
/* PipeWire */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2019 Wim Taymans */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
2020-11-20 11:52:57 +01:00
|
|
|
|
2021-06-18 22:14:05 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
2020-11-20 11:52:57 +01:00
|
|
|
#include <dbus/dbus.h>
|
2021-06-18 22:14:05 +02:00
|
|
|
|
2020-11-20 11:52:57 +01:00
|
|
|
#include <spa/support/dbus.h>
|
2021-06-18 22:14:05 +02:00
|
|
|
#include <spa/support/plugin.h>
|
|
|
|
|
#include <pipewire/context.h>
|
|
|
|
|
|
2021-09-22 09:28:54 +10:00
|
|
|
#include "log.h"
|
2021-06-18 22:14:05 +02:00
|
|
|
#include "dbus-name.h"
|
2020-11-20 11:52:57 +01:00
|
|
|
|
2021-06-18 22:14:05 +02:00
|
|
|
void *dbus_request_name(struct pw_context *context, const char *name)
|
2020-11-20 11:52:57 +01:00
|
|
|
{
|
|
|
|
|
struct spa_dbus *dbus;
|
|
|
|
|
struct spa_dbus_connection *conn;
|
|
|
|
|
const struct spa_support *support;
|
|
|
|
|
uint32_t n_support;
|
|
|
|
|
DBusConnection *bus;
|
|
|
|
|
DBusError error;
|
|
|
|
|
|
|
|
|
|
support = pw_context_get_support(context, &n_support);
|
|
|
|
|
|
|
|
|
|
dbus = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DBus);
|
2021-03-26 15:14:53 +01:00
|
|
|
if (dbus == NULL) {
|
|
|
|
|
errno = ENOTSUP;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2020-11-20 11:52:57 +01:00
|
|
|
|
2021-06-18 22:14:05 +02:00
|
|
|
conn = spa_dbus_get_connection(dbus, SPA_DBUS_TYPE_SESSION);
|
|
|
|
|
if (conn == NULL)
|
2021-03-26 15:14:53 +01:00
|
|
|
return NULL;
|
2020-11-20 11:52:57 +01:00
|
|
|
|
|
|
|
|
bus = spa_dbus_connection_get(conn);
|
2021-05-17 18:19:44 +03:00
|
|
|
if (bus == NULL) {
|
|
|
|
|
spa_dbus_connection_destroy(conn);
|
2021-05-26 10:23:17 +02:00
|
|
|
return NULL;
|
2021-05-17 18:19:44 +03:00
|
|
|
}
|
2020-11-20 11:52:57 +01:00
|
|
|
|
|
|
|
|
dbus_error_init(&error);
|
|
|
|
|
|
|
|
|
|
if (dbus_bus_request_name(bus, name,
|
|
|
|
|
DBUS_NAME_FLAG_DO_NOT_QUEUE,
|
|
|
|
|
&error) == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
|
2021-03-26 15:14:53 +01:00
|
|
|
return conn;
|
2020-11-20 11:52:57 +01:00
|
|
|
|
|
|
|
|
if (dbus_error_is_set(&error))
|
|
|
|
|
pw_log_error("Failed to acquire %s: %s: %s", name, error.name, error.message);
|
|
|
|
|
else
|
|
|
|
|
pw_log_error("D-Bus name %s already taken.", name);
|
|
|
|
|
|
|
|
|
|
dbus_error_free(&error);
|
|
|
|
|
|
2021-05-17 18:19:44 +03:00
|
|
|
spa_dbus_connection_destroy(conn);
|
|
|
|
|
|
2021-03-26 15:14:53 +01:00
|
|
|
errno = EEXIST;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 22:14:05 +02:00
|
|
|
void dbus_release_name(void *data)
|
2021-03-26 15:14:53 +01:00
|
|
|
{
|
|
|
|
|
struct spa_dbus_connection *conn = data;
|
|
|
|
|
spa_dbus_connection_destroy(conn);
|
2020-11-20 11:52:57 +01:00
|
|
|
}
|