]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
Fix MPRIS method names
[vlc] / modules / control / dbus.c
1 /*****************************************************************************
2  * dbus.c : D-Bus control interface
3  *****************************************************************************
4  * Copyright © 2006-2008 Rafaël Carré
5  * Copyright © 2007-2008 Mirsal Ennaime
6  * $Id$
7  *
8  * Authors:    Rafaël Carré <funman at videolanorg>
9  *             Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*
27  * D-Bus Specification:
28  *      http://dbus.freedesktop.org/doc/dbus-specification.html
29  * D-Bus low-level C API (libdbus)
30  *      http://dbus.freedesktop.org/doc/dbus/api/html/index.html
31  *  extract:
32  *   "If you use this low-level API directly, you're signing up for some pain."
33  *
34  * MPRIS Specification (still drafting on Jan, 23 of 2008):
35  *      http://wiki.xmms2.xmms.se/index.php/MPRIS
36  */
37
38 /*****************************************************************************
39  * Preamble
40  *****************************************************************************/
41
42 #include <dbus/dbus.h>
43
44 #include "dbus.h"
45
46 #ifdef HAVE_CONFIG_H
47 # include "config.h"
48 #endif
49
50 #include <vlc/vlc.h>
51 #include <vlc_aout.h>
52 #include <vlc_interface.h>
53 #include <vlc_meta.h>
54 #include <vlc_input.h>
55 #include <vlc_playlist.h>
56
57 /*****************************************************************************
58  * Local prototypes.
59  *****************************************************************************/
60
61 static int  Open    ( vlc_object_t * );
62 static void Close   ( vlc_object_t * );
63 static void Run     ( intf_thread_t * );
64
65 static int StateChange( vlc_object_t *, const char *, vlc_value_t,
66                         vlc_value_t, void * );
67
68 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
69                         vlc_value_t, void * );
70
71 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
72                         vlc_value_t, void * );
73
74 static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
75                         vlc_value_t, void * );
76
77 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
78 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, bool );
79 static int UpdateCaps( intf_thread_t*, bool );
80
81 /* GetCaps() capabilities */
82 enum
83 {
84      CAPS_NONE                  = 0,
85      CAPS_CAN_GO_NEXT           = 1 << 0,
86      CAPS_CAN_GO_PREV           = 1 << 1,
87      CAPS_CAN_PAUSE             = 1 << 2,
88      CAPS_CAN_PLAY              = 1 << 3,
89      CAPS_CAN_SEEK              = 1 << 4,
90      CAPS_CAN_PROVIDE_METADATA  = 1 << 5,
91      CAPS_CAN_HAS_TRACKLIST     = 1 << 6
92 };
93
94 struct intf_sys_t
95 {
96     DBusConnection *p_conn;
97     bool      b_meta_read;
98     dbus_int32_t    i_caps;
99 };
100
101 /*****************************************************************************
102  * Module descriptor
103  *****************************************************************************/
104
105 vlc_module_begin();
106     set_shortname( _("dbus"));
107     set_category( CAT_INTERFACE );
108     set_subcategory( SUBCAT_INTERFACE_CONTROL );
109     set_description( _("D-Bus control interface") );
110     set_capability( "interface", 0 );
111     set_callbacks( Open, Close );
112 vlc_module_end();
113
114 /*****************************************************************************
115  * Methods
116  *****************************************************************************/
117
118 /* Player */
119
120 DBUS_METHOD( Quit )
121 { /* exits vlc */
122     REPLY_INIT;
123     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
124     playlist_Stop( p_playlist );
125     pl_Release( ((vlc_object_t*) p_this) );
126     vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
127     REPLY_SEND;
128 }
129
130 DBUS_METHOD( MprisVersion )
131 { /*implemented version of the mpris spec */
132     REPLY_INIT;
133     OUT_ARGUMENTS;
134     VLC_UNUSED( p_this );
135     dbus_uint16_t i_major = VLC_MPRIS_VERSION_MAJOR;
136     dbus_uint16_t i_minor = VLC_MPRIS_VERSION_MINOR;
137     DBusMessageIter version;
138
139     if( !dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL,
140             &version ) )
141         return DBUS_HANDLER_RESULT_NEED_MEMORY;
142
143     if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
144             &i_major ) )
145         return DBUS_HANDLER_RESULT_NEED_MEMORY;
146
147     if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
148             &i_minor ) )
149         return DBUS_HANDLER_RESULT_NEED_MEMORY;
150
151     if( !dbus_message_iter_close_container( &args, &version ) )
152         return DBUS_HANDLER_RESULT_NEED_MEMORY;
153     REPLY_SEND;
154 }
155
156 DBUS_METHOD( PositionGet )
157 { /* returns position in milliseconds */
158     REPLY_INIT;
159     OUT_ARGUMENTS;
160     vlc_value_t position;
161     dbus_int32_t i_pos;
162
163     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
164     PL_LOCK;
165     input_thread_t *p_input = p_playlist->p_input;
166
167     if( !p_input )
168         i_pos = 0;
169     else
170     {
171         var_Get( p_input, "time", &position );
172         i_pos = position.i_time / 1000;
173     }
174     PL_UNLOCK;
175     pl_Release( ((vlc_object_t*) p_this) );
176     ADD_INT32( &i_pos );
177     REPLY_SEND;
178 }
179
180 DBUS_METHOD( PositionSet )
181 { /* set position in milliseconds */
182
183     REPLY_INIT;
184     vlc_value_t position;
185     playlist_t* p_playlist = NULL;
186     dbus_int32_t i_pos;
187
188     DBusError error;
189     dbus_error_init( &error );
190
191     dbus_message_get_args( p_from, &error,
192             DBUS_TYPE_INT32, &i_pos,
193             DBUS_TYPE_INVALID );
194
195     if( dbus_error_is_set( &error ) )
196     {
197         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
198                 error.message );
199         dbus_error_free( &error );
200         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
201     }
202     p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
203     PL_LOCK;
204     input_thread_t *p_input = p_playlist->p_input;
205
206     if( p_input )
207     {
208         position.i_time = i_pos * 1000;
209         var_Set( p_input, "time", position );
210     }
211     PL_UNLOCK;
212     pl_Release( ((vlc_object_t*) p_this) );
213     REPLY_SEND;
214 }
215
216 DBUS_METHOD( VolumeGet )
217 { /* returns volume in percentage */
218     REPLY_INIT;
219     OUT_ARGUMENTS;
220     dbus_int32_t i_dbus_vol;
221     audio_volume_t i_vol;
222     /* 2nd argument of aout_VolumeGet is int32 */
223     aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
224     i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
225     ADD_INT32( &i_dbus_vol );
226     REPLY_SEND;
227 }
228
229 DBUS_METHOD( VolumeSet )
230 { /* set volume in percentage */
231     REPLY_INIT;
232
233     DBusError error;
234     dbus_error_init( &error );
235
236     dbus_int32_t i_dbus_vol;
237     audio_volume_t i_vol;
238
239     dbus_message_get_args( p_from, &error,
240             DBUS_TYPE_INT32, &i_dbus_vol,
241             DBUS_TYPE_INVALID );
242
243     if( dbus_error_is_set( &error ) )
244     {
245         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
246                 error.message );
247         dbus_error_free( &error );
248         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
249     }
250
251     i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
252     aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
253
254     REPLY_SEND;
255 }
256
257 DBUS_METHOD( Next )
258 { /* next playlist item */
259     REPLY_INIT;
260     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
261     playlist_Next( p_playlist );
262     pl_Release( ((vlc_object_t*) p_this) );
263     REPLY_SEND;
264 }
265
266 DBUS_METHOD( Prev )
267 { /* previous playlist item */
268     REPLY_INIT;
269     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
270     playlist_Prev( p_playlist );
271     pl_Release( ((vlc_object_t*) p_this) );
272     REPLY_SEND;
273 }
274
275 DBUS_METHOD( Stop )
276 { /* stop playing */
277     REPLY_INIT;
278     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
279     playlist_Stop( p_playlist );
280     pl_Release( ((vlc_object_t*) p_this) );
281     REPLY_SEND;
282 }
283
284 DBUS_METHOD( GetStatus )
285 { /* returns the current status as a struct of 4 ints */
286 /*
287     First   0 = Playing, 1 = Paused, 2 = Stopped.
288     Second  0 = Playing linearly , 1 = Playing randomly.
289     Third   0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
290     Fourth  0 = Stop playing once the last element has been played, 1 = Never give up playing *
291  */
292     REPLY_INIT;
293     OUT_ARGUMENTS;
294
295     MarshalStatus( p_this, &args, true );
296
297     REPLY_SEND;
298 }
299
300 DBUS_METHOD( Pause )
301 {
302     REPLY_INIT;
303     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
304     playlist_Pause( p_playlist );
305     pl_Release( p_playlist );
306     REPLY_SEND;
307 }
308
309 DBUS_METHOD( Play )
310 {
311     REPLY_INIT;
312     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
313     playlist_Play( p_playlist );
314     pl_Release( p_playlist );
315     REPLY_SEND;
316 }
317
318 DBUS_METHOD( GetCurrentMetadata )
319 {
320     REPLY_INIT;
321     OUT_ARGUMENTS;
322     playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
323     PL_LOCK;
324     if( p_playlist->status.p_item )
325         GetInputMeta( p_playlist->status.p_item->p_input, &args );
326     PL_UNLOCK;
327     pl_Release( p_playlist );
328     REPLY_SEND;
329 }
330
331 DBUS_METHOD( GetCaps )
332 {
333     REPLY_INIT;
334     OUT_ARGUMENTS;
335
336     ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
337
338     REPLY_SEND;
339 }
340
341 /* Media Player information */
342
343 DBUS_METHOD( Identity )
344 {
345     VLC_UNUSED(p_this);
346     REPLY_INIT;
347     OUT_ARGUMENTS;
348     char *psz_identity;
349     if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
350     {
351         ADD_STRING( &psz_identity );
352         free( psz_identity );
353     }
354     else
355         return DBUS_HANDLER_RESULT_NEED_MEMORY;
356
357     REPLY_SEND;
358 }
359
360 /* TrackList */
361
362 DBUS_METHOD( AddTrack )
363 { /* add the string to the playlist, and play it if the boolean is true */
364     REPLY_INIT;
365
366     DBusError error;
367     dbus_error_init( &error );
368     playlist_t* p_playlist = NULL;
369
370     char *psz_mrl;
371     dbus_bool_t b_play;
372
373     dbus_message_get_args( p_from, &error,
374             DBUS_TYPE_STRING, &psz_mrl,
375             DBUS_TYPE_BOOLEAN, &b_play,
376             DBUS_TYPE_INVALID );
377
378     if( dbus_error_is_set( &error ) )
379     {
380         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
381                 error.message );
382         dbus_error_free( &error );
383         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
384     }
385
386     p_playlist = pl_Yield( (vlc_object_t*) p_this );
387     playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
388             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
389             PLAYLIST_END, true, false );
390     pl_Release( p_playlist );
391
392     REPLY_SEND;
393 }
394
395 DBUS_METHOD( GetCurrentTrack )
396 {
397     REPLY_INIT;
398     OUT_ARGUMENTS;
399
400     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
401     dbus_int32_t i_position = p_playlist->i_current_index;
402     pl_Release( p_playlist );
403
404     ADD_INT32( &i_position );
405     REPLY_SEND;
406 }
407
408 DBUS_METHOD( GetMetadata )
409 {
410     REPLY_INIT;
411     OUT_ARGUMENTS;
412     DBusError error;
413     dbus_error_init( &error );
414
415     dbus_int32_t i_position;
416
417     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
418     PL_LOCK;
419
420     dbus_message_get_args( p_from, &error,
421            DBUS_TYPE_INT32, &i_position,
422            DBUS_TYPE_INVALID );
423
424     if( dbus_error_is_set( &error ) )
425     {
426         PL_UNLOCK;
427         pl_Release( p_playlist );
428         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
429                 error.message );
430         dbus_error_free( &error );
431         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
432     }
433
434     if( i_position <= p_playlist->items.i_size / 2 )
435     {
436         GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
437     }
438
439     PL_UNLOCK;
440     pl_Release( p_playlist );
441     REPLY_SEND;
442 }
443
444 DBUS_METHOD( GetLength )
445 {
446     REPLY_INIT;
447     OUT_ARGUMENTS;
448
449     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
450     dbus_int32_t i_elements = p_playlist->items.i_size / 2;
451     pl_Release( p_playlist );
452
453     ADD_INT32( &i_elements );
454     REPLY_SEND;
455 }
456
457 DBUS_METHOD( DelTrack )
458 {
459     REPLY_INIT;
460
461     DBusError error;
462     dbus_error_init( &error );
463
464     dbus_int32_t i_position;
465     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
466
467     dbus_message_get_args( p_from, &error,
468             DBUS_TYPE_INT32, &i_position,
469             DBUS_TYPE_INVALID );
470
471     if( dbus_error_is_set( &error ) )
472     {
473         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
474                 error.message );
475         dbus_error_free( &error );
476         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
477     }
478
479     if( i_position <= p_playlist->items.i_size / 2 )
480     {
481         playlist_DeleteFromInput( p_playlist,
482             p_playlist->items.p_elems[i_position*2-1]->i_id,
483             false );
484     }
485
486     pl_Release( p_playlist );
487
488     REPLY_SEND;
489 }
490
491 DBUS_METHOD( SetLoop )
492 {
493     REPLY_INIT;
494     OUT_ARGUMENTS;
495
496     DBusError error;
497     dbus_bool_t b_loop;
498     vlc_value_t val;
499     playlist_t* p_playlist = NULL;
500
501     dbus_error_init( &error );
502     dbus_message_get_args( p_from, &error,
503             DBUS_TYPE_BOOLEAN, &b_loop,
504             DBUS_TYPE_INVALID );
505
506     if( dbus_error_is_set( &error ) )
507     {
508         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
509                 error.message );
510         dbus_error_free( &error );
511         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
512     }
513
514     val.b_bool = ( b_loop == TRUE ) ? true : false ;
515     p_playlist = pl_Yield( (vlc_object_t*) p_this );
516     var_Set ( p_playlist, "loop", val );
517     pl_Release( ((vlc_object_t*) p_this) );
518
519     REPLY_SEND;
520 }
521
522 DBUS_METHOD( Repeat )
523 {
524     REPLY_INIT;
525     OUT_ARGUMENTS;
526
527     DBusError error;
528     dbus_bool_t b_repeat;
529     vlc_value_t val;
530     playlist_t* p_playlist = NULL;
531
532     dbus_error_init( &error );
533     dbus_message_get_args( p_from, &error,
534             DBUS_TYPE_BOOLEAN, &b_repeat,
535             DBUS_TYPE_INVALID );
536
537     if( dbus_error_is_set( &error ) )
538     {
539         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
540                 error.message );
541         dbus_error_free( &error );
542         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
543     }
544
545     val.b_bool = ( b_repeat == TRUE ) ? true : false ;
546
547     p_playlist = pl_Yield( (vlc_object_t*) p_this );
548     var_Set ( p_playlist, "repeat", val );
549     pl_Release( ((vlc_object_t*) p_this) );
550
551     REPLY_SEND;
552 }
553
554 DBUS_METHOD( SetRandom )
555 {
556     REPLY_INIT;
557     OUT_ARGUMENTS;
558
559     DBusError error;
560     dbus_bool_t b_random;
561     vlc_value_t val;
562     playlist_t* p_playlist = NULL;
563
564     dbus_error_init( &error );
565     dbus_message_get_args( p_from, &error,
566             DBUS_TYPE_BOOLEAN, &b_random,
567             DBUS_TYPE_INVALID );
568
569     if( dbus_error_is_set( &error ) )
570     {
571         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
572                 error.message );
573         dbus_error_free( &error );
574         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
575     }
576
577     val.b_bool = ( b_random == TRUE ) ? true : false ;
578
579     p_playlist = pl_Yield( (vlc_object_t*) p_this );
580     var_Set ( p_playlist, "random", val );
581     pl_Release( ((vlc_object_t*) p_this) );
582
583     REPLY_SEND;
584 }
585 /*****************************************************************************
586  * Introspection method
587  *****************************************************************************/
588
589 DBUS_METHOD( handle_introspect_root )
590 { /* handles introspection of root object */
591     VLC_UNUSED(p_this);
592     REPLY_INIT;
593     OUT_ARGUMENTS;
594     ADD_STRING( &psz_introspection_xml_data_root );
595     REPLY_SEND;
596 }
597
598 DBUS_METHOD( handle_introspect_player )
599 {
600     VLC_UNUSED(p_this);
601     REPLY_INIT;
602     OUT_ARGUMENTS;
603     ADD_STRING( &psz_introspection_xml_data_player );
604     REPLY_SEND;
605 }
606
607 DBUS_METHOD( handle_introspect_tracklist )
608 {
609     VLC_UNUSED(p_this);
610     REPLY_INIT;
611     OUT_ARGUMENTS;
612     ADD_STRING( &psz_introspection_xml_data_tracklist );
613     REPLY_SEND;
614 }
615
616 /*****************************************************************************
617  * handle_*: answer to incoming messages
618  *****************************************************************************/
619
620 #define METHOD_FUNC( method, function ) \
621     else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
622         return function( p_conn, p_from, p_this )
623
624 DBUS_METHOD( handle_root )
625 {
626
627     if( dbus_message_is_method_call( p_from,
628                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
629         return handle_introspect_root( p_conn, p_from, p_this );
630
631     /* here D-Bus method's names are associated to an handler */
632
633     METHOD_FUNC( "Identity",                Identity );
634     METHOD_FUNC( "MprisVersion",            MprisVersion );
635
636     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
637 }
638
639
640 DBUS_METHOD( handle_player )
641 {
642     if( dbus_message_is_method_call( p_from,
643                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
644         return handle_introspect_player( p_conn, p_from, p_this );
645
646     /* here D-Bus method's names are associated to an handler */
647
648     METHOD_FUNC( "Prev",                    Prev );
649     METHOD_FUNC( "Next",                    Next );
650     METHOD_FUNC( "Quit",                    Quit );
651     METHOD_FUNC( "Stop",                    Stop );
652     METHOD_FUNC( "Play",                    Play );
653     METHOD_FUNC( "Pause",                   Pause );
654     METHOD_FUNC( "Repeat",                  Repeat );
655     METHOD_FUNC( "VolumeSet",               VolumeSet );
656     METHOD_FUNC( "VolumeGet",               VolumeGet );
657     METHOD_FUNC( "PositionSet",             PositionSet );
658     METHOD_FUNC( "PositionGet",             PositionGet );
659     METHOD_FUNC( "GetStatus",               GetStatus );
660     METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
661     METHOD_FUNC( "GetCaps",                 GetCaps );
662
663     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
664 }
665
666 DBUS_METHOD( handle_tracklist )
667 {
668     if( dbus_message_is_method_call( p_from,
669                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
670     return handle_introspect_tracklist( p_conn, p_from, p_this );
671
672     /* here D-Bus method's names are associated to an handler */
673
674     METHOD_FUNC( "GetMetadata",             GetMetadata );
675     METHOD_FUNC( "GetCurrentTrack",         GetCurrentTrack );
676     METHOD_FUNC( "GetLength",               GetLength );
677     METHOD_FUNC( "AddTrack",                AddTrack );
678     METHOD_FUNC( "DelTrack",                DelTrack );
679     METHOD_FUNC( "SetLoop",                 SetLoop );
680     METHOD_FUNC( "SetRandom",               SetRandom );
681
682     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
683 }
684
685 /*****************************************************************************
686  * Open: initialize interface
687  *****************************************************************************/
688
689 static int Open( vlc_object_t *p_this )
690 { /* initialisation of the connection */
691     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
692     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
693     playlist_t      *p_playlist;
694     DBusConnection  *p_conn;
695     DBusError       error;
696
697     if( !p_sys )
698         return VLC_ENOMEM;
699
700     p_sys->b_meta_read = false;
701     p_sys->i_caps = CAPS_NONE;
702
703     dbus_error_init( &error );
704
705     /* connect to the session bus */
706     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
707     if( !p_conn )
708     {
709         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
710                 error.message );
711         dbus_error_free( &error );
712         free( p_sys );
713         return VLC_EGENERIC;
714     }
715
716     /* register a well-known name on the bus */
717     dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
718     if( dbus_error_is_set( &error ) )
719     {
720         msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
721                  ": %s", error.message );
722         dbus_error_free( &error );
723         free( p_sys );
724         return VLC_EGENERIC;
725     }
726
727     /* we register the objects */
728     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
729             &vlc_dbus_root_vtable, p_this );
730     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
731             &vlc_dbus_player_vtable, p_this );
732     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
733             &vlc_dbus_tracklist_vtable, p_this );
734
735     dbus_connection_flush( p_conn );
736
737     p_playlist = pl_Yield( p_intf );
738     PL_LOCK;
739     var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
740     var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
741     var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
742     var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
743     var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
744     var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
745     var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
746     PL_UNLOCK;
747     pl_Release( p_playlist );
748
749     p_intf->pf_run = Run;
750     p_intf->p_sys = p_sys;
751     p_sys->p_conn = p_conn;
752
753     UpdateCaps( p_intf, false );
754
755     return VLC_SUCCESS;
756 }
757
758 /*****************************************************************************
759  * Close: destroy interface
760  *****************************************************************************/
761
762 static void Close   ( vlc_object_t *p_this )
763 {
764     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
765     playlist_t      *p_playlist = pl_Yield( p_intf );;
766     input_thread_t  *p_input;
767
768     p_this->b_dead = true;
769
770     PL_LOCK;
771     var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
772     var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
773     var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
774     var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
775     var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
776     var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
777     var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
778
779     p_input = p_playlist->p_input;
780     if ( p_input )
781     {
782         vlc_object_yield( p_input );
783         var_DelCallback( p_input, "state", StateChange, p_intf );
784         vlc_object_release( p_input );
785     }
786
787     PL_UNLOCK;
788     pl_Release( p_playlist );
789
790     dbus_connection_unref( p_intf->p_sys->p_conn );
791
792     free( p_intf->p_sys );
793 }
794
795 /*****************************************************************************
796  * Run: main loop
797  *****************************************************************************/
798
799 static void Run          ( intf_thread_t *p_intf )
800 {
801     while( !intf_ShouldDie( p_intf ) )
802     {
803         msleep( INTF_IDLE_SLEEP );
804         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
805     }
806 }
807
808 /******************************************************************************
809  * CapsChange: player capabilities change signal
810  *****************************************************************************/
811 DBUS_SIGNAL( CapsChangeSignal )
812 {
813     SIGNAL_INIT( "CapsChange" );
814     OUT_ARGUMENTS;
815
816     ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
817     SIGNAL_SEND;
818 }
819
820 /******************************************************************************
821  * TrackListChange: tracklist order / length change signal 
822  *****************************************************************************/
823 DBUS_SIGNAL( TrackListChangeSignal )
824 { /* emit the new tracklist lengh */
825     SIGNAL_INIT("TrackListChange");
826     OUT_ARGUMENTS;
827
828     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
829     dbus_int32_t i_elements = p_playlist->items.i_size / 2;
830     pl_Release( p_playlist );
831
832     ADD_INT32( &i_elements );
833     SIGNAL_SEND;
834 }
835
836 /*****************************************************************************
837  * TrackListChangeEmit: Emits the TrackListChange signal
838  *****************************************************************************/
839 /* FIXME: It is not called on tracklist reordering */
840 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
841             vlc_value_t oldval, vlc_value_t newval, void *p_data )
842 {
843     VLC_UNUSED(oldval);
844     intf_thread_t *p_intf = p_data;
845
846     if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
847     {
848         /* don't signal when items are added/removed in p_category */
849         playlist_t *p_playlist = (playlist_t*)p_this;
850         playlist_add_t *p_add = newval.p_address;
851         playlist_item_t *p_item;
852         p_item = playlist_ItemGetById( p_playlist, p_add->i_node, true );
853         assert( p_item );
854         while( p_item->p_parent )
855             p_item = p_item->p_parent;
856         if( p_item == p_playlist->p_root_category )
857             return VLC_SUCCESS;
858     }
859
860     if( p_intf->b_dead )
861         return VLC_SUCCESS;
862
863     UpdateCaps( p_intf, true );
864     TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
865     return VLC_SUCCESS;
866 }
867 /*****************************************************************************
868  * TrackChange: Playlist item change callback
869  *****************************************************************************/
870
871 DBUS_SIGNAL( TrackChangeSignal )
872 { /* emit the metadata of the new item */
873     SIGNAL_INIT( "TrackChange" );
874     OUT_ARGUMENTS;
875
876     input_item_t *p_item = (input_item_t*) p_data;
877     GetInputMeta ( p_item, &args );
878
879     SIGNAL_SEND;
880 }
881
882 /*****************************************************************************
883  * StatusChange: Player status change signal
884  *****************************************************************************/
885
886 DBUS_SIGNAL( StatusChangeSignal )
887 { /* send the updated status info on the bus */
888     SIGNAL_INIT( "StatusChange" );
889     OUT_ARGUMENTS;
890
891     /* we're called from a callback of input_thread_t, so it can not be
892      * destroyed before we return */
893     MarshalStatus( (intf_thread_t*) p_data, &args, false );
894
895     SIGNAL_SEND;
896 }
897
898 /*****************************************************************************
899  * StateChange: callback on input "state"
900  *****************************************************************************/
901 static int StateChange( vlc_object_t *p_this, const char* psz_var,
902             vlc_value_t oldval, vlc_value_t newval, void *p_data )
903 {
904     VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
905     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
906     intf_sys_t          *p_sys      = p_intf->p_sys;
907
908     if( p_intf->b_dead )
909         return VLC_SUCCESS;
910
911     UpdateCaps( p_intf, false );
912
913     if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
914     {
915         input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
916         if( p_item )
917         {
918             p_sys->b_meta_read = true;
919             TrackChangeSignal( p_sys->p_conn, p_item );
920         }
921     }
922
923     if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
924         newval.i_int == END_S )
925     {
926         StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
927     }
928
929     return VLC_SUCCESS;
930 }
931
932 /*****************************************************************************
933  * StatusChangeEmit: Emits the StatusChange signal
934  *****************************************************************************/
935 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
936             vlc_value_t oldval, vlc_value_t newval, void *p_data )
937 {
938     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
939     VLC_UNUSED(oldval); VLC_UNUSED(newval);
940     intf_thread_t *p_intf = p_data;
941
942     if( p_intf->b_dead )
943         return VLC_SUCCESS;
944
945     UpdateCaps( p_intf, false );
946     StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
947     return VLC_SUCCESS;
948 }
949
950 /*****************************************************************************
951  * TrackChange: callback on playlist "playlist-current"
952  *****************************************************************************/
953 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
954             vlc_value_t oldval, vlc_value_t newval, void *p_data )
955 {
956     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
957     intf_sys_t          *p_sys      = p_intf->p_sys;
958     playlist_t          *p_playlist;
959     input_thread_t      *p_input    = NULL;
960     input_item_t        *p_item     = NULL;
961     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
962     VLC_UNUSED( oldval ); VLC_UNUSED( newval );
963
964     if( p_intf->b_dead )
965         return VLC_SUCCESS;
966
967     p_sys->b_meta_read = false;
968
969     p_playlist = pl_Yield( p_intf );
970     p_input = p_playlist->p_input;
971
972     if( !p_input )
973     {
974         PL_UNLOCK;
975         pl_Release( p_playlist );
976         return VLC_SUCCESS;
977     }
978
979     vlc_object_yield( p_input );
980     pl_Release( p_playlist );
981
982     p_item = input_GetItem( p_input );
983     if( !p_item )
984     {
985         vlc_object_release( p_input );
986         return VLC_EGENERIC;
987     }
988
989     if( input_item_IsPreparsed( p_item ) )
990     {
991         p_sys->b_meta_read = true;
992         TrackChangeSignal( p_sys->p_conn, p_item );
993     }
994
995     var_AddCallback( p_input, "state", StateChange, p_intf );
996
997     vlc_object_release( p_input );
998     return VLC_SUCCESS;
999 }
1000
1001 /*****************************************************************************
1002  * UpdateCaps: update p_sys->i_caps
1003  ****************************************************************************/
1004 static int UpdateCaps( intf_thread_t* p_intf, bool b_playlist_locked )
1005 {
1006     intf_sys_t* p_sys = p_intf->p_sys;
1007     dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1008     playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
1009     if( !b_playlist_locked ) PL_LOCK;
1010     
1011     if( p_playlist->items.i_size > 0 )
1012         i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1013
1014     if( p_playlist->p_input )
1015     {
1016         /* XXX: if UpdateCaps() is called too early, these are
1017          * unconditionnaly true */
1018         if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1019             i_caps |= CAPS_CAN_PAUSE;
1020         if( var_GetBool( p_playlist->p_input, "seekable" ) )
1021             i_caps |= CAPS_CAN_SEEK;
1022     }
1023
1024     if( !b_playlist_locked ) PL_UNLOCK;
1025     pl_Release( p_playlist );
1026
1027     if( p_sys->b_meta_read )
1028         i_caps |= CAPS_CAN_PROVIDE_METADATA;
1029
1030     if( i_caps != p_intf->p_sys->i_caps )
1031     {
1032         p_sys->i_caps = i_caps;
1033         CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1034     }
1035
1036     return VLC_SUCCESS;
1037 }
1038
1039 /*****************************************************************************
1040  * GetInputMeta: Fill a DBusMessage with the given input item metadata
1041  *****************************************************************************/
1042
1043 #define ADD_META( entry, type, data ) \
1044     if( data ) { \
1045         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1046                 NULL, &dict_entry ); \
1047         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1048                 &ppsz_meta_items[entry] ); \
1049         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1050                 type##_AS_STRING, &variant ); \
1051         dbus_message_iter_append_basic( &variant, \
1052                 type, \
1053                 & data ); \
1054         dbus_message_iter_close_container( &dict_entry, &variant ); \
1055         dbus_message_iter_close_container( &dict, &dict_entry ); }
1056
1057 #define ADD_VLC_META_STRING( entry, item ) \
1058     { \
1059         char * psz = input_item_Get##item( p_input );\
1060         ADD_META( entry, DBUS_TYPE_STRING, \
1061                   psz ); \
1062         free( psz ); \
1063     }
1064
1065 static int GetInputMeta( input_item_t* p_input,
1066                         DBusMessageIter *args )
1067 {
1068     DBusMessageIter dict, dict_entry, variant;
1069     /* We need the track length to be expressed in milli-seconds
1070      * instead of µ-seconds */
1071     dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1072
1073     const char* ppsz_meta_items[] =
1074     {
1075     "title", "artist", "genre", "copyright", "album", "tracknum",
1076     "description", "rating", "date", "setting", "url", "language",
1077     "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1078     "status", "URI", "length", "video-codec", "audio-codec",
1079     "video-bitrate", "audio-bitrate", "audio-samplerate"
1080     };
1081
1082     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1083
1084     ADD_VLC_META_STRING( 0,  Title );
1085     ADD_VLC_META_STRING( 1,  Artist );
1086     ADD_VLC_META_STRING( 2,  Genre );
1087     ADD_VLC_META_STRING( 3,  Copyright );
1088     ADD_VLC_META_STRING( 4,  Album );
1089     ADD_VLC_META_STRING( 5,  TrackNum );
1090     ADD_VLC_META_STRING( 6,  Description );
1091     ADD_VLC_META_STRING( 7,  Rating );
1092     ADD_VLC_META_STRING( 8,  Date );
1093     ADD_VLC_META_STRING( 9,  Setting );
1094     ADD_VLC_META_STRING( 10, URL );
1095     ADD_VLC_META_STRING( 11, Language );
1096     ADD_VLC_META_STRING( 12, NowPlaying );
1097     ADD_VLC_META_STRING( 13, Publisher );
1098     ADD_VLC_META_STRING( 14, EncodedBy );
1099     ADD_VLC_META_STRING( 15, ArtURL );
1100     ADD_VLC_META_STRING( 16, TrackID );
1101
1102     vlc_mutex_lock( &p_input->lock );
1103     if( p_input->p_meta )
1104         ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1105     vlc_mutex_unlock( &p_input->lock );
1106
1107     ADD_VLC_META_STRING( 18, URI );
1108     ADD_META( 19, DBUS_TYPE_INT64, i_length );
1109
1110     dbus_message_iter_close_container( args, &dict );
1111     return VLC_SUCCESS;
1112 }
1113
1114 #undef ADD_META
1115 #undef ADD_VLC_META_STRING
1116
1117 /*****************************************************************************
1118  * MarshalStatus: Fill a DBusMessage with the current player status
1119  *****************************************************************************/
1120
1121 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1122                           bool lock )
1123 { /* This is NOT the right way to do that, it would be better to sore
1124      the status information in p_sys and update it on change, thus
1125      avoiding a long lock */
1126
1127     DBusMessageIter status;
1128     dbus_int32_t i_state, i_random, i_repeat, i_loop;
1129     vlc_value_t val;
1130     playlist_t* p_playlist = NULL;
1131     input_thread_t* p_input = NULL;
1132
1133     p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1134     if( lock )
1135         PL_LOCK;
1136
1137     i_state = 2;
1138
1139     p_input = p_playlist->p_input;
1140     if( p_input )
1141     {
1142         var_Get( p_input, "state", &val );
1143         if( val.i_int >= END_S )
1144             i_state = 2;
1145         else if( val.i_int == PAUSE_S )
1146             i_state = 1;
1147         else if( val.i_int <= PLAYING_S )
1148             i_state = 0;
1149     }
1150
1151     var_Get( p_playlist, "random", &val );
1152     i_random = val.i_int;
1153
1154     var_Get( p_playlist, "repeat", &val );
1155     i_repeat = val.i_int;
1156
1157     var_Get( p_playlist, "loop", &val );
1158     i_loop = val.i_int;
1159
1160     if( lock )
1161         PL_UNLOCK;
1162     pl_Release( p_playlist );
1163
1164     dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1165     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1166     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1167     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1168     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1169     dbus_message_iter_close_container( args, &status );
1170
1171     return VLC_SUCCESS;
1172 }