dbus-name.c 2.46 KB
Newer Older
1
/* PipeWire
Wim Taymans's avatar
Wim Taymans committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * Copyright © 2019 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

25
26
#include <errno.h>

Wim Taymans's avatar
Wim Taymans committed
27
#include <dbus/dbus.h>
28

Wim Taymans's avatar
Wim Taymans committed
29
#include <spa/support/dbus.h>
30
31
32
#include <spa/support/plugin.h>
#include <pipewire/context.h>

33
#include "log.h"
34
#include "dbus-name.h"
Wim Taymans's avatar
Wim Taymans committed
35

36
void *dbus_request_name(struct pw_context *context, const char *name)
Wim Taymans's avatar
Wim Taymans committed
37
38
39
40
41
42
43
44
45
46
47
{
	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);
48
49
50
51
	if (dbus == NULL) {
		errno = ENOTSUP;
		return NULL;
	}
Wim Taymans's avatar
Wim Taymans committed
52

53
54
	conn = spa_dbus_get_connection(dbus, SPA_DBUS_TYPE_SESSION);
	if (conn == NULL)
55
		return NULL;
Wim Taymans's avatar
Wim Taymans committed
56
57

	bus = spa_dbus_connection_get(conn);
58
59
	if (bus == NULL) {
		spa_dbus_connection_destroy(conn);
60
		return NULL;
61
	}
Wim Taymans's avatar
Wim Taymans committed
62
63
64
65
66
67

	dbus_error_init(&error);

	if (dbus_bus_request_name(bus, name,
			DBUS_NAME_FLAG_DO_NOT_QUEUE,
			&error) == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
68
		return conn;
Wim Taymans's avatar
Wim Taymans committed
69
70
71
72
73
74
75
76

	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);

77
78
	spa_dbus_connection_destroy(conn);

79
80
81
82
	errno = EEXIST;
	return NULL;
}

83
void dbus_release_name(void *data)
84
85
86
{
	struct spa_dbus_connection *conn = data;
	spa_dbus_connection_destroy(conn);
Wim Taymans's avatar
Wim Taymans committed
87
}