dbus.h 4.82 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
/* Simple Plugin API
Wim Taymans's avatar
Wim Taymans committed
2
 *
Wim Taymans's avatar
Wim Taymans committed
3
 * Copyright © 2018 Wim Taymans
Wim Taymans's avatar
Wim Taymans committed
4
 *
Wim Taymans's avatar
Wim Taymans committed
5
6
7
8
9
10
 * 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:
Wim Taymans's avatar
Wim Taymans committed
11
 *
Wim Taymans's avatar
Wim Taymans committed
12
13
14
15
16
17
18
19
20
21
22
 * 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.
Wim Taymans's avatar
Wim Taymans committed
23
24
 */

25
26
#ifndef SPA_DBUS_H
#define SPA_DBUS_H
Wim Taymans's avatar
Wim Taymans committed
27
28
29
30
31
32
33

#ifdef __cplusplus
extern "C" {
#endif

#include <spa/support/loop.h>

34
/** \defgroup spa_dbus DBus
35
 * DBus communication
36
37
 */

38
/**
39
 * \addtogroup spa_dbus
40
41
42
 * \{
 */

Wim Taymans's avatar
Wim Taymans committed
43
44
45
#define SPA_TYPE_INTERFACE_DBus		SPA_TYPE_INFO_INTERFACE_BASE "DBus"

#define SPA_VERSION_DBUS		0
46
47
struct spa_dbus { struct spa_interface iface; };

Wim Taymans's avatar
Wim Taymans committed
48
49
50
51
52
53
enum spa_dbus_type {
	SPA_DBUS_TYPE_SESSION,	/**< The login session bus */
	SPA_DBUS_TYPE_SYSTEM,	/**< The systemwide bus */
	SPA_DBUS_TYPE_STARTER	/**< The bus that started us, if any */
};

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define SPA_DBUS_CONNECTION_EVENT_DESTROY	0
#define SPA_DBUS_CONNECTION_EVENT_DISCONNECTED	1
#define SPA_DBUS_CONNECTION_EVENT_NUM		2

struct spa_dbus_connection_events {
#define SPA_VERSION_DBUS_CONNECTION_EVENTS	0
        uint32_t version;

	/* a connection is destroyed */
	void (*destroy) (void *data);

	/* a connection disconnected */
	void (*disconnected) (void *data);
};

Wim Taymans's avatar
Wim Taymans committed
69
struct spa_dbus_connection {
70
#define SPA_VERSION_DBUS_CONNECTION	1
Wim Taymans's avatar
Wim Taymans committed
71
72
        uint32_t version;
	/**
73
	 * Get the DBusConnection from a wrapper
Wim Taymans's avatar
Wim Taymans committed
74
	 *
75
76
77
78
79
	 * Note that the returned handle is closed and unref'd by spa_dbus
	 * immediately before emitting the asynchronous "disconnected" event.
	 * The caller must either deal with the invalidation, or keep an extra
	 * ref on the handle returned.
	 *
Wim Taymans's avatar
Wim Taymans committed
80
	 * \param conn the spa_dbus_connection wrapper
Wim Taymans's avatar
Wim Taymans committed
81
	 * \return a pointer of type DBusConnection
Wim Taymans's avatar
Wim Taymans committed
82
	 */
Wim Taymans's avatar
Wim Taymans committed
83
	void *(*get) (struct spa_dbus_connection *conn);
Wim Taymans's avatar
Wim Taymans committed
84
85
86
87
88
89
	/**
	 * Destroy a dbus connection wrapper
	 *
	 * \param conn the wrapper to destroy
	 */
	void (*destroy) (struct spa_dbus_connection *conn);
90
91
92
93
94
95
96
97
98
99

	/**
	 * Add a listener for events
	 *
	 * Since version 1
	 */
	void (*add_listener) (struct spa_dbus_connection *conn,
			struct spa_hook *listener,
			const struct spa_dbus_connection_events *events,
			void *data);
Wim Taymans's avatar
Wim Taymans committed
100
101
};

102
103
104
105
106
107
108
109
110
111
112
113
114
115
#define spa_dbus_connection_call(c,method,vers,...)			\
({									\
	if (SPA_LIKELY(SPA_CALLBACK_CHECK(c,method,vers)))		\
		c->method((c), ## __VA_ARGS__);				\
})

#define spa_dbus_connection_call_vp(c,method,vers,...)			\
({									\
	void *_res = NULL;						\
	if (SPA_LIKELY(SPA_CALLBACK_CHECK(c,method,vers)))		\
		_res = c->method((c), ## __VA_ARGS__);			\
	_res;								\
})

116
117
/** \copydoc spa_dbus_connection.get
 * \sa spa_dbus_connection.get */
118
#define spa_dbus_connection_get(c)		spa_dbus_connection_call_vp(c,get,0)
119
120
/** \copydoc spa_dbus_connection.destroy
 * \sa spa_dbus_connection.destroy */
121
#define spa_dbus_connection_destroy(c)		spa_dbus_connection_call(c,destroy,0)
122
123
/** \copydoc spa_dbus_connection.add_listener
 * \sa spa_dbus_connection.add_listener */
124
#define spa_dbus_connection_add_listener(c,...)	spa_dbus_connection_call(c,add_listener,1,__VA_ARGS__)
Wim Taymans's avatar
Wim Taymans committed
125

126
127
struct spa_dbus_methods {
#define SPA_VERSION_DBUS_METHODS	0
Wim Taymans's avatar
Wim Taymans committed
128
129
130
131
132
133
134
135
136
137
138
        uint32_t version;

	/**
	 * Get a new connection wrapper for the given bus type.
	 *
	 * The connection wrapper is completely configured to operate
	 * in the main context of the handle that manages the spa_dbus
	 * interface.
	 *
	 * \param dbus the dbus manager
	 * \param type the bus type to wrap
Wim Taymans's avatar
Wim Taymans committed
139
140
	 * \param error location for the DBusError
	 * \return a new dbus connection wrapper or NULL on error
Wim Taymans's avatar
Wim Taymans committed
141
	 */
142
	struct spa_dbus_connection * (*get_connection) (void *object,
Wim Taymans's avatar
Wim Taymans committed
143
							enum spa_dbus_type type);
Wim Taymans's avatar
Wim Taymans committed
144
145
};

146
147
148
/** \copydoc spa_dbus_methods.get_connection
 * \sa spa_dbus_methods.get_connection
 */
149
150
151
152
153
154
155
156
157
static inline struct spa_dbus_connection *
spa_dbus_get_connection(struct spa_dbus *dbus, enum spa_dbus_type type)
{
	struct spa_dbus_connection *res = NULL;
	spa_interface_call_res(&dbus->iface,
                        struct spa_dbus_methods, res,
			get_connection, 0, type);
	return res;
}
Wim Taymans's avatar
Wim Taymans committed
158

159
160
161
162
/**
 * \}
 */

Wim Taymans's avatar
Wim Taymans committed
163
164
165
166
#ifdef __cplusplus
}  /* extern "C" */
#endif

167
#endif /* SPA_DBUS_H */