-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpris2.cpp
298 lines (265 loc) · 10 KB
/
mpris2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <ctype.h>
#include <dbus/dbus.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "eventcallbacks.h"
#include "mpris2.h"
const char *const INTERFACE_NAME = "org.mpris.MediaPlayer2";
const char *const SERVER_BUS_NAME = "org.mpris.MediaPlayer2.test";
const char *const OBJECT_PATH_NAME = "/org/mpris/MediaPlayer2/test";
const char *const METHOD_NAME = "add_numbers";
DBusError dbus_error;
void print_dbus_error(const char *str);
const char *intros = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" \
\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\"> \
<!-- GDBus 2.68.1 --> \
<node name=\"/org/mpris/MediaPlayer2/Player\"> \
<interface name=\"org.freedesktop.DBus.Properties\"> \
<method name=\"Get\"> \
<arg type=\"s\" name=\"interface_name\" direction=\"in\"/> \
<arg type=\"s\" name=\"property_name\" direction=\"in\"/> \
<arg type=\"v\" name=\"value\" direction=\"out\"/> \
</method> \
<method name=\"GetAll\"> \
<arg type=\"s\" name=\"interface_name\" direction=\"in\"/> \
<arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/> \
</method> \
<method name=\"Set\"> \
<arg type=\"s\" name=\"interface_name\" direction=\"in\"/> \
<arg type=\"s\" name=\"property_name\" direction=\"in\"/> \
<arg type=\"v\" name=\"value\" direction=\"in\"/>\
</method>\
<signal name=\"PropertiesChanged\"> \
<arg type=\"s\" name=\"interface_name\"/> \
<arg type=\"a{sv}\" name=\"changed_properties\"/>\
<arg type=\"as\" name=\"invalidated_properties\"/>\
</signal>\
</interface>\
<interface name=\"org.freedesktop.DBus.Introspectable\">\
<method name=\"Introspect\">\
<arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\
</method>\
</interface>\
<interface name=\"org.freedesktop.DBus.Peer\">\
<method name=\"Ping\"/>\
<method name=\"GetMachineId\">\
<arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\
</method>\
</interface>\
<interface name=\"org.mpris.MediaPlayer2\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
<method name=\"Raise\">\
</method>\
<method name=\"Quit\">\
</method>\
<property type=\"b\" name=\"CanQuit\" access=\"read\">\
</property>\
<property type=\"b\" name=\"CanRaise\" access=\"read\">\
</property>\
<property type=\"b\" name=\"HasTrackList\" access=\"read\">\
</property>\
<property type=\"s\" name=\"Identity\" access=\"read\">\
</property>\
<property type=\"s\" name=\"DesktopEntry\" access=\"read\">\
</property>\
<property type=\"as\" name=\"SupportedUriSchemes\" access=\"read\">\
</property>\
<property type=\"as\" name=\"SupportedMimeTypes\" access=\"read\">\
</property>\
</interface>\
<interface name=\"org.mpris.MediaPlayer2.Player\">\
<method name=\"Next\">\
</method>\
<method name=\"Previous\">\
</method>\
<method name=\"Pause\">\
</method>\
<method name=\"PlayPause\">\
</method>\
<method name=\"Stop\">\
</method>\
<method name=\"Play\">\
</method>\
<method name=\"Seek\">\
<arg type=\"x\" name=\"Offset\" direction=\"in\">\
</arg>\
</method>\
<method name=\"SetPosition\">\
<arg type=\"o\" name=\"TrackId\" direction=\"in\">\
</arg>\
<arg type=\"x\" name=\"Position\" direction=\"in\">\
</arg>\
</method>\
<method name=\"OpenUri\">\
<arg type=\"s\" name=\"Uri\" direction=\"in\">\
</arg>\
</method>\
<signal name=\"Seeked\">\
<arg type=\"x\" name=\"Position\">\
</arg>\
</signal>\
<property type=\"s\" name=\"PlaybackStatus\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"d\" name=\"Rate\" access=\"readwrite\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"a{sv}\" name=\"Metadata\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"d\" name=\"Volume\" access=\"readwrite\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"x\" name=\"Position\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\">\
</annotation>\
</property>\
<property type=\"d\" name=\"MinimumRate\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"d\" name=\"MaximumRate\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"b\" name=\"CanGoNext\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"b\" name=\"CanGoPrevious\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"b\" name=\"CanPlay\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"b\" name=\"CanPause\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"b\" name=\"CanSeek\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"true\">\
</annotation>\
</property>\
<property type=\"b\" name=\"CanControl\" access=\"read\">\
<annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\">\
</annotation>\
</property>\
</interface>\
</node>";
DBusHandlerResult server_message_handler(DBusConnection *conn, DBusMessage *message, void *data) { }
const DBusObjectPathVTable server_vtable = { .message_function = server_message_handler };
event_callbacks *global_callbacks = nullptr;
void *global_userptr = nullptr;
DBusConnection *conn;
void mpris2_init(event_callbacks *callbacks, void *user) {
global_callbacks = callbacks;
global_userptr = user;
int ret;
dbus_error_init(&dbus_error);
conn = dbus_bus_get(DBUS_BUS_SESSION, &dbus_error);
if (dbus_error_is_set(&dbus_error))
print_dbus_error("dbus_bus_get");
if (!conn)
exit(1);
// Get a well known name
ret = dbus_bus_request_name(conn, SERVER_BUS_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &dbus_error);
if (dbus_error_is_set(&dbus_error))
print_dbus_error("dbus_bus_get");
if (!dbus_connection_register_object_path(conn, OBJECT_PATH_NAME, &server_vtable, NULL)) {
printf("Failed to register a object path\n");
exit(1);
}
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
fprintf(stderr, "Dbus: not primary owner, ret = %d\n", ret);
exit(1);
}
}
void mpris2_process_events() {
// Block for msg from client
if (!dbus_connection_read_write_dispatch(conn, -1)) {
fprintf(stderr, "Not connected now.\n");
exit(1);
}
DBusMessage *message;
if ((message = dbus_connection_pop_message(conn)) == NULL) {
fprintf(stderr, "Did not get message\n");
return;
} else {
printf("nnfdfnd %s %s\n", dbus_message_get_member(message), dbus_message_get_path(message));
const char *member = dbus_message_get_member(message);
const char *path = dbus_message_get_path(message);
if (strcmp(member, "Introspect") == 0 && strcmp(path, "/") == 0) {
DBusMessage *reply;
if ((reply = dbus_message_new_method_return(message)) == NULL) {
fprintf(stderr, "Error in dbus_message_new_method_return\n");
exit(1);
}
DBusMessageIter iter;
dbus_message_iter_init_append(reply, &iter);
if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &intros)) {
fprintf(stderr, "Error in dbus_message_iter_append_basic\n");
exit(1);
}
if (!dbus_connection_send(conn, reply, NULL)) {
fprintf(stderr, "Error in dbus_connection_send\n");
exit(1);
}
dbus_connection_flush(conn);
dbus_message_unref(reply);
} else if (strcmp(member, "PlayPause") == 0) {
DBusMessage *reply;
if ((reply = dbus_message_new_method_return(message)) == NULL) {
fprintf(stderr, "Error in dbus_message_new_method_return\n");
exit(1);
}
global_callbacks->on_playpause(global_userptr);
if (!dbus_connection_send(conn, reply, NULL)) {
fprintf(stderr, "Error in dbus_connection_send\n");
exit(1);
}
dbus_connection_flush(conn);
dbus_message_unref(reply);
} else if (strcmp(member, "Next") == 0) {
DBusMessage *reply;
if ((reply = dbus_message_new_method_return(message)) == NULL) {
fprintf(stderr, "Error in dbus_message_new_method_return\n");
exit(1);
}
global_callbacks->on_next(global_userptr);
if (!dbus_connection_send(conn, reply, NULL)) {
fprintf(stderr, "Error in dbus_connection_send\n");
exit(1);
}
dbus_connection_flush(conn);
dbus_message_unref(reply);
} else {
DBusMessage *dbus_error_msg;
char error_msg[] = "Error in input";
if ((dbus_error_msg = dbus_message_new_error(message, DBUS_ERROR_FAILED, error_msg)) == NULL) {
fprintf(stderr, "Error in dbus_mesage_new_error\n");
exit(1);
}
if (!dbus_connection_send(conn, dbus_error_msg, NULL)) {
fprintf(stderr, "Error in dbus_connection_send\n");
exit(1);
}
dbus_connection_flush(conn);
dbus_message_unref(dbus_error_msg);
}
}
}
void mpris2_finit() { }
void print_dbus_error(const char *str) {
fprintf(stderr, "%s: %s\n", str, dbus_error.message);
dbus_error_free(&dbus_error);
}