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