This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmpris_v1.c
623 lines (562 loc) · 21.3 KB
/
mpris_v1.c
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
MPRIS plugin for Deadbeef player
Copyright (C) 2011 HuangCongyu <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "mpris_v1.h"
#include "mpris_common.h"
#include "introspection_xml.h"
#include <gio/gio.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <stdlib.h>
//caps
enum{
NONE = 0,
CAN_GO_NEXT = 1 << 0,
CAN_GO_PREV = 1 << 1,
CAN_PAUSE = 1 << 2,
CAN_PLAY = 1 << 3,
CAN_SEEK = 1 << 4,
CAN_PROVIDE_METADATA = 1 << 5,
CAN_HAS_TRACKLIST = 1 << 6
};
struct _DB_mpris_server_v1
{
GDBusConnection *con;
GDBusNodeInfo *introspection_data_root;
GDBusNodeInfo *introspection_data_player;
GDBusNodeInfo *introspection_data_tracklist;
guint owner_id;
guint root_reg_id;
guint player_reg_id;
guint tracklist_reg_id;
};
/*
* Information.
*/
static DB_mpris_server_v1 *server = NULL;
/*
* Handle the / object method call
*/
static void handle_root_method_call(GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
debug("V1 / method: %s", method_name);
gchar identify[100];
//Identity
if(g_strcmp0(method_name, MPRIS_METHOD_IDENTITY) == 0){
g_sprintf(identify, "DeadBeeF %d.%d", deadbeef -> vmajor, deadbeef -> vminor);
g_dbus_method_invocation_return_value(invocation
, g_variant_new("(s)", identify));
return;
}
//Quit
if(g_strcmp0(method_name, MPRIS_METHOD_QUIT) == 0){
g_dbus_method_invocation_return_value(invocation, NULL);
debug("Quit...");
/*
* Quit the media player
*/
deadbeef -> sendmessage(DB_EV_TERMINATE, 0, 0, 0);
return;
}
//MprisVersion
if(g_strcmp0(method_name, MPRIS_METHOD_VERSION) == 0){
g_dbus_method_invocation_return_value(invocation
, g_variant_new("((qq))", 1, 0));
return;
}
debug("Error! Unsupported method. %s.%s", interface_name, method_name);
g_dbus_method_invocation_return_error(invocation
, G_DBUS_ERROR
, G_DBUS_ERROR_NOT_SUPPORTED
, "Method %s.%s not supported"
, interface_name
, method_name);
return;
}
/*
* Handle /Player method call
*/
static void handle_player_method_call(GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
//Next
if(g_strcmp0(method_name, MPRIS_METHOD_NEXT) == 0){
g_dbus_method_invocation_return_value(invocation, NULL);
deadbeef -> sendmessage(DB_EV_NEXT, 0, 0, 0);
goto go_return;
}
//Prev
if(g_strcmp0(method_name, MPRIS_METHOD_PREV) == 0){
g_dbus_method_invocation_return_value(invocation, NULL);
deadbeef -> sendmessage(DB_EV_PREV, 0, 0, 0);
goto go_return;
}
//Play
if(g_strcmp0(method_name, MPRIS_METHOD_PLAY) == 0){
g_dbus_method_invocation_return_value(invocation, NULL);
deadbeef -> sendmessage(DB_EV_PLAY_CURRENT, 0, 0, 0);
goto go_return;
}
//Stop
if(g_strcmp0(method_name, MPRIS_METHOD_STOP) == 0){
g_dbus_method_invocation_return_value(invocation, NULL);
deadbeef -> sendmessage(DB_EV_STOP, 0, 0, 0);
goto go_return;
}
//Pause
if(g_strcmp0(method_name, MPRIS_METHOD_PAUSE) == 0){
g_dbus_method_invocation_return_value(invocation, NULL);
DB_output_t *op = deadbeef -> get_output();
if(op == NULL){
return;
}
int state = op -> state();
if (state == OUTPUT_STATE_PLAYING){
deadbeef->sendmessage(DB_EV_PAUSE, 0, 0, 0);
}else{
deadbeef->sendmessage(DB_EV_PLAY_CURRENT, 0, 0, 0);
}
goto go_return;
}
//Repeat
if(g_strcmp0(method_name, MPRIS_METHOD_REPEAT) == 0){
gboolean loop;
g_variant_get(parameters, "(b)", &loop);
if(loop == TRUE){
set_loop_status(g_variant_new_string("Track"));
}else{
set_loop_status(g_variant_new_string("None"));
}
g_dbus_method_invocation_return_value(invocation, NULL);
goto go_return;
}
GVariant *ret_val = NULL;
//GetStatus
if(g_strcmp0(method_name, MPRIS_METHOD_GETSTATUS) == 0){
g_dbus_method_invocation_return_value(invocation
, get_status());
goto go_return;
}
//GetMetadata
if(g_strcmp0(method_name, MPRIS_METHOD_GETMETA) == 0){
ret_val = get_metadata(-1);
g_dbus_method_invocation_return_value(invocation, ret_val);
goto go_return;
}
//GetCaps
if(g_strcmp0(method_name, MPRIS_METHOD_GETCAPS) == 0){
ret_val = g_variant_new("((i))" , CAN_GO_NEXT | CAN_GO_PREV
| CAN_PAUSE | CAN_PLAY
| CAN_SEEK | CAN_PROVIDE_METADATA
| CAN_HAS_TRACKLIST);
g_dbus_method_invocation_return_value(invocation, ret_val);
goto go_return;
}
//PositionGet
if(g_strcmp0(method_name, MPRIS_METHOD_POSITIONGET) == 0){
DB_playItem_t *track = NULL;
track = deadbeef -> streamer_get_playing_track();
if(track == NULL){
ret_val = g_variant_new("(i)", 0);
}else{
float duration = deadbeef -> pl_get_item_duration(track);
float pos = deadbeef -> playback_get_pos();
//we need ms
ret_val = g_variant_new("(i)", (int)(pos * duration * 10));
deadbeef -> pl_item_unref(track);
}
g_dbus_method_invocation_return_value(invocation, ret_val);
goto go_return;
}
//PositionSet
if(g_strcmp0(method_name, MPRIS_METHOD_POSITIONSET) == 0){
int pos = 0;
g_variant_get(parameters, "(i)", &pos);
debug("Set position %d.", pos);
deadbeef -> sendmessage(DB_EV_SEEK, 0, pos, 0);
g_dbus_method_invocation_return_value(invocation, NULL);
goto go_return;
}
//VolumeGet
if(g_strcmp0(method_name, MPRIS_METHOD_VOLUMEGET) == 0){
float min_vol = deadbeef -> volume_get_min_db();
float volume = deadbeef -> volume_get_db() - min_vol;
debug("Get Volume: %f", volume);
ret_val = g_variant_new("(i)" , (int)(volume / ( - min_vol) * 100));
g_dbus_method_invocation_return_value(invocation, ret_val);
goto go_return;
}
//VolumeSet
if(g_strcmp0(method_name, MPRIS_METHOD_VOLUMESET) == 0){
int volume = 0;
g_variant_get(parameters, "(i)", &volume);
volume = volume < 0 ? 0 : volume;
volume = volume > 100 ? 1000 : volume;
float vol_f = 50 - ((float)volume / (float)100 * (float)50);
debug("Set Volume: %d %f", volume, vol_f);
deadbeef -> volume_set_db(-vol_f);
g_dbus_method_invocation_return_value(invocation, NULL);
goto go_return;
}
debug("Error! Unsupported method. %s.%s", interface_name, method_name);
g_dbus_method_invocation_return_error(invocation
, G_DBUS_ERROR
, G_DBUS_ERROR_NOT_SUPPORTED
, "Method %s.%s not supported"
, interface_name
, method_name);
go_return:
return;
}
/*
* The callback of plt_add_file
*/
static int add_file_cb(DB_playItem_t *it, void *data) {
gboolean *play_imm = (gboolean*)data;
if (it != NULL) {
debug("add_file_cb %d\n", play_imm);
if (*play_imm == TRUE) {
ddb_playlist_t *pl = deadbeef -> plt_get_curr();
int idx = deadbeef -> plt_get_item_idx(pl, it, PL_MAIN);
deadbeef -> sendmessage(DB_EV_PLAY_NUM, 0, idx, 0);
deadbeef -> plt_unref(pl);
}
} else {
debug("add_file_cb NULL\n");
}
g_free(play_imm);
return 0;
}
/*
* Handle /TrackList object method call
*/
static void handle_tracklist_method_call(GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
//GetMetadata
if(g_strcmp0(method_name, MPRIS_METHOD_GETMETA) == 0){
int track_id = 0;
g_variant_get(parameters, "(i)", & track_id);
GVariant *ret_val = get_metadata(track_id);
g_dbus_method_invocation_return_value(invocation, ret_val);
return;
}
//GetCurrentTrack
if(g_strcmp0(method_name, MPRIS_METHOD_GETCURRENTTRACK) == 0){
int track_id = 0;
GVariant *ret_val = NULL;
DB_playItem_t *track = deadbeef -> streamer_get_playing_track();
if(track == NULL){
ret_val = g_variant_new("(i)", -1);
}else{
ddb_playlist_t *pl = deadbeef -> plt_get_curr();
track_id = deadbeef -> plt_get_item_idx(pl ,track, PL_MAIN);
deadbeef -> plt_unref(pl);
ret_val = g_variant_new("(i)", track_id);
}
g_dbus_method_invocation_return_value(invocation, ret_val);
return;
}
//GetLength
if(g_strcmp0(method_name, MPRIS_METHOD_GETLENGTH) == 0){
int track_id = 0;
GVariant *ret_val = NULL;
/*
* We use the index of the last track to calculate the length
* of the play list.
*/
ddb_playlist_t *pl = deadbeef -> plt_get_curr();
DB_playItem_t *track = deadbeef -> plt_get_last(pl, PL_MAIN);
if(track == NULL){
ret_val = g_variant_new("(i)", 0);
}else{
track_id = deadbeef -> plt_get_item_idx(pl, track, PL_MAIN);
deadbeef -> pl_item_unref(track);
ret_val = g_variant_new("(i)", track_id + 1);
}
deadbeef -> plt_unref(pl);
g_dbus_method_invocation_return_value(invocation, ret_val);
return;
}
//AddTrack
if(g_strcmp0(method_name, MPRIS_METHOD_ADDTRACK) == 0){
const char *uri;
gboolean *play, playnow;
play = g_malloc(sizeof(gboolean));
g_variant_get (parameters, "(sb)", &uri, play);
playnow = *play;
debug("Add Track: %s %d", uri, *play);
ddb_playlist_t *pl = deadbeef -> plt_get_curr();
/*
* !!add_file_cb can NOT be called...
*/
int ret = deadbeef -> plt_add_file(pl, uri, add_file_cb, (void*)play);
deadbeef -> plt_unref(pl);
if (ret == 0) {
deadbeef -> sendmessage(DB_EV_PLAYLISTCHANGED, 0, 0, 0);
}
if(playnow){
/*
* Play the new added track.
* I don't know why plt_add_file do not call the add_file_cb.
* The track is added at the last of the playlist, so we can play it
* using as following.
*/
ddb_playlist_t *pl = deadbeef -> plt_get_curr();
DB_playItem_t *track = deadbeef -> plt_get_last(pl, PL_MAIN);
int track_id = deadbeef -> plt_get_item_idx(pl, track, PL_MAIN);
deadbeef -> plt_unref(pl);
deadbeef -> pl_item_unref(track);
deadbeef -> sendmessage(DB_EV_PLAY_NUM, 0, track_id, 0);
}
g_dbus_method_invocation_return_value(invocation
, g_variant_new("(i)", ret));
return;
}
//DelTrack
if(g_strcmp0(method_name, MPRIS_METHOD_DELTRACK) == 0){
/*
* No Supported!
*/
debug("DelTrack is not supported...");
g_dbus_method_invocation_return_value(invocation, NULL);
return;
}
//SetLoop
if(g_strcmp0(method_name, MPRIS_METHOD_SETLOOP) == 0){
gboolean loop;
g_variant_get(parameters, "(b)", &loop);
if(loop == TRUE){
set_loop_status(g_variant_new_string("Playlist"));
}else{
set_loop_status(g_variant_new_string("None"));
}
g_dbus_method_invocation_return_value(invocation, NULL);
return;
}
//SetRandom
if(g_strcmp0(method_name, MPRIS_METHOD_SETRANDOM) == 0){
gboolean random;
g_variant_get(parameters, "(b)", &random);
if(random == FALSE){
deadbeef -> conf_set_int("playback.order", PLAYBACK_ORDER_LINEAR);
}else{
deadbeef -> conf_set_int("playback.order", PLAYBACK_ORDER_RANDOM);
}
deadbeef -> sendmessage(DB_EV_CONFIGCHANGED, 0, 0, 0);
g_dbus_method_invocation_return_value(invocation, NULL);
return;
}
debug("Error! Unsupported method. %s.%s", interface_name, method_name);
g_dbus_method_invocation_return_error(invocation
, G_DBUS_ERROR
, G_DBUS_ERROR_NOT_SUPPORTED
, "Method %s.%s not supported"
, interface_name
, method_name);
return;
}
void DB_mpris_emit_trackchange_v1()
{
GVariant *metadata = get_metadata(-1);
debug("V1: emit track change signl.");
emit_signal(server -> con, MPRIS_V1_INTERFACE, MPRIS_V1_PLAYER_PATH
, MPRIS_SIGNAL_TRACKCHANGE, metadata);
}
static GVariant *old_status = NULL;
void DB_mpris_emit_statuschange_v1()
{
GVariant *new_status = get_status();
if(old_status == NULL || !g_variant_equal(old_status, new_status)){
if(old_status != NULL){
g_variant_unref(old_status);
}
old_status = g_variant_ref(new_status);
debug("V1: emit status change signl.");
emit_signal(server -> con, MPRIS_V1_INTERFACE, MPRIS_V1_PLAYER_PATH
, MPRIS_SIGNAL_STATUSCHANGE, new_status);
}else{
g_variant_unref(new_status);
}
}
void DB_mpris_emit_capschange_v1()
{
/*
* Will NOT change any more!!
*/
}
void DB_mpris_emit_tracklistchange_v1()
{
int track_id = 0;
/*
* We use the index of the last track to calculate the length
* of the play list.
*/
ddb_playlist_t *pl = deadbeef -> plt_get_curr();
DB_playItem_t *track = deadbeef -> plt_get_last(pl, PL_MAIN);
if(track == NULL){
/*
* If the playlist is empty, plt_get_last will return NULL.
* pl_item_unref will NOT check the parmeter, so we must check it here.
*/
deadbeef -> plt_unref(pl);
return;
}
track_id = deadbeef -> plt_get_item_idx(pl, track, PL_MAIN);
deadbeef -> plt_unref(pl);
deadbeef -> pl_item_unref(track);
GVariant *ret_val = g_variant_new("(i)", track_id + 1);
debug("V1: emit tracklist change signl.");
emit_signal(server -> con, MPRIS_V1_INTERFACE, MPRIS_V1_TRACKLIST_PATH
, MPRIS_SIGNAL_TRACKLISTCHAGE, ret_val);
}
/* root vtable */
static const GDBusInterfaceVTable root_vtable =
{
handle_root_method_call,
NULL,
NULL
};
/* player vtable */
static const GDBusInterfaceVTable player_vtable =
{
handle_player_method_call,
NULL,
NULL
};
/* tracklist vtable */
static const GDBusInterfaceVTable tracklist_vtable =
{
handle_tracklist_method_call,
NULL,
NULL
};
static void on_bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
//The /Player object implemets org.freedesktop.MediaPalyer interface
server -> player_reg_id = g_dbus_connection_register_object(
connection
, MPRIS_V1_PLAYER_PATH
, server -> introspection_data_player -> interfaces[0]
, &player_vtable
, NULL /* user_data */
, NULL /* user_data_free_func */
, NULL); /* GError** */
g_assert(server -> player_reg_id > 0);
//The / object implemets org.freedesktop.MediaPalyer interface
server -> root_reg_id = g_dbus_connection_register_object(
connection
, MPRIS_V1_ROOT_PATH
, server -> introspection_data_root -> interfaces[0]
, &root_vtable
, NULL /* user_data */
, NULL /* user_data_free_func */
, NULL); /* GError** */
g_assert(server -> root_reg_id > 0);
//The /Tracklist object implemets org.freedesktop.MediaPalyer interface
server -> tracklist_reg_id = g_dbus_connection_register_object(
connection
, MPRIS_V1_TRACKLIST_PATH
, server -> introspection_data_tracklist -> interfaces[0]
, &tracklist_vtable
, NULL /* user_data */
, NULL /* user_data_free_func */
, NULL); /* GError** */
g_assert(server -> tracklist_reg_id > 0);
server -> con = connection;
}
static void on_name_acquired(GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
debug("name acquired: %s", name);
}
static void on_name_lost (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
debug("name lost: %s", name);
}
gint DB_mpris_server_start_v1(DB_mpris_server_v1 **srv)
{
#if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
g_type_init();
#endif
server = g_new(DB_mpris_server_v1, 1);
if(server == NULL){
debug("Create DB_mpris_server error!!\n");
return DB_MPRIS_ERROR;
}
server -> introspection_data_root
= g_dbus_node_info_new_for_xml(xml_v1_root, NULL);
if(server -> introspection_data_root == NULL){
debug("Create root dbus node info error!! %s %d\n", __FILE__, __LINE__);
return DB_MPRIS_ERROR;
}
server -> introspection_data_player
= g_dbus_node_info_new_for_xml(xml_v1_player, NULL);
if(server -> introspection_data_player == NULL){
debug("Create player dbus node info error!! %s %d\n", __FILE__, __LINE__);
return DB_MPRIS_ERROR;
}
server -> introspection_data_tracklist
= g_dbus_node_info_new_for_xml(xml_v1_tracklist, NULL);
if(server -> introspection_data_tracklist == NULL){
debug("Create tracklist dbus node info error!! %s %d\n", __FILE__, __LINE__);
return DB_MPRIS_ERROR;
}
server -> owner_id = g_bus_own_name(G_BUS_TYPE_SESSION
, MPRIS_V1_SERVICE
, G_BUS_NAME_OWNER_FLAGS_NONE
, on_bus_acquired
, on_name_acquired
, on_name_lost
, NULL, NULL);
*srv = server;
return DB_MPRIS_OK;
}
gint DB_mpris_server_stop_v1(DB_mpris_server_v1 *srv)
{
g_dbus_connection_unregister_object(srv -> con, srv -> root_reg_id);
g_dbus_connection_unregister_object(srv -> con, srv -> player_reg_id);
g_dbus_connection_unregister_object(srv -> con, srv -> tracklist_reg_id);
g_bus_unown_name(srv -> owner_id);
g_dbus_node_info_unref(srv -> introspection_data_root);
g_dbus_node_info_unref(srv -> introspection_data_tracklist);
g_dbus_node_info_unref(srv -> introspection_data_player);
g_free(srv);
return DB_MPRIS_OK;
}