]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
D-Bus, The Repeat method should be part of /Player, not /TrackList
[vlc] / modules / control / dbus.c
1 /*****************************************************************************
2  * dbus.c : D-Bus control interface
3  *****************************************************************************
4  * Copyright (C) 2006 Rafaël Carré
5  * $Id$
6  *
7  * Authors:    Rafaël Carré <funman at videolanorg>
8  *             Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*
26  * D-Bus Specification:
27  *      http://dbus.freedesktop.org/doc/dbus-specification.html
28  * D-Bus low-level C API (libdbus)
29  *      http://dbus.freedesktop.org/doc/dbus/api/html/index.html
30  */
31
32 /*
33  * TODO:
34  *  properties ?
35  *
36  *  macros to read incoming arguments
37  *
38  *  explore different possible types (arrays..)
39  *
40  *  what must we do if org.videolan.vlc already exist on the bus ?
41  *  ( there is more than one vlc instance )
42  */
43
44 /*****************************************************************************
45  * Preamble
46  *****************************************************************************/
47
48 #include <dbus/dbus.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "dbus.h"
54
55 #include <vlc/vlc.h>
56 #include <vlc_aout.h>
57 #include <vlc_interface.h>
58 #include <vlc_meta.h>
59 #include <vlc_input.h>
60 #include <vlc_playlist.h>
61
62 /*****************************************************************************
63  * Local prototypes.
64  *****************************************************************************/
65
66 static int  Open    ( vlc_object_t * );
67 static void Close   ( vlc_object_t * );
68 static void Run     ( intf_thread_t * );
69
70 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
71                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
72
73 static int GetInputMeta ( input_item_t *p_input, 
74                     DBusMessageIter *args);
75
76 struct intf_sys_t
77 {
78     DBusConnection *p_conn;
79 };
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84
85 vlc_module_begin();
86     set_shortname( _("dbus"));
87     set_category( CAT_INTERFACE );
88     set_subcategory( SUBCAT_INTERFACE_CONTROL );
89     set_description( _("D-Bus control interface") );
90     set_capability( "interface", 0 );
91     set_callbacks( Open, Close );
92 vlc_module_end();
93
94 /*****************************************************************************
95  * Methods
96  *****************************************************************************/
97 #if 0
98 DBUS_METHOD( PlaylistExport_XSPF )
99 { /*export playlist to an xspf file */
100
101   /* reads the filename to export to */
102   /* returns the status as int32:
103    *    0 : success
104    *    1 : error
105    *    2 : playlist empty
106    */
107     REPLY_INIT;
108     OUT_ARGUMENTS;
109
110     DBusError error; 
111     dbus_error_init( &error );
112
113     char *psz_file;
114     dbus_int32_t i_ret;
115
116     dbus_message_get_args( p_from, &error,
117             DBUS_TYPE_STRING, &psz_file,
118             DBUS_TYPE_INVALID );
119
120     if( dbus_error_is_set( &error ) )
121     {
122         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
123                 error.message );
124         dbus_error_free( &error );
125         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
126     }
127
128     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
129
130     if( ( !playlist_IsEmpty( p_playlist ) ) &&
131             ( p_playlist->p_root_category->i_children > 0 ) )
132     {
133         if( playlist_Export( p_playlist, psz_file,
134                          p_playlist->p_root_category->pp_children[0],
135                          "export-xspf" ) == VLC_SUCCESS )
136             i_ret = 0;
137         else
138             i_ret = 1;
139     }
140     else
141         i_ret = 2;
142
143     pl_Release( ((vlc_object_t*) p_this ) );
144
145     ADD_INT32( &i_ret );
146     REPLY_SEND;
147 }
148 #endif
149
150 /* Player */
151
152 DBUS_METHOD( Quit )
153 { /* exits vlc */
154     REPLY_INIT;
155     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
156     playlist_Stop( p_playlist );
157     pl_Release( ((vlc_object_t*) p_this) );
158     vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
159     REPLY_SEND;
160 }
161
162 DBUS_METHOD( PositionGet )
163 { /* returns position as an int in the range [0;1000] */
164     REPLY_INIT;
165     OUT_ARGUMENTS;
166     vlc_value_t position;
167     dbus_int32_t i_pos;
168
169     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
170     input_thread_t *p_input = p_playlist->p_input;
171
172     if( !p_input )
173         i_pos = 0;
174     else
175     {
176         var_Get( p_input, "position", &position );
177         i_pos = position.f_float * 1000 ;
178     }
179     pl_Release( ((vlc_object_t*) p_this) );
180     ADD_INT32( &i_pos );
181     REPLY_SEND;
182 }
183
184 DBUS_METHOD( PositionSet )
185 { /* set position from an int in the range [0;1000] */
186
187     REPLY_INIT;
188     vlc_value_t position;
189     playlist_t* p_playlist = NULL;
190     dbus_int32_t i_pos;
191
192     DBusError error;
193     dbus_error_init( &error );
194
195     dbus_message_get_args( p_from, &error,
196             DBUS_TYPE_INT32, &i_pos,
197             DBUS_TYPE_INVALID );
198
199     if( dbus_error_is_set( &error ) )
200     {
201         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
202                 error.message );
203         dbus_error_free( &error );
204         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
205     }
206     p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
207     input_thread_t *p_input = p_playlist->p_input;
208
209     if( p_input )
210     {
211         position.f_float = ((float)i_pos) / 1000;
212         var_Set( p_input, "position", position );
213     }
214     pl_Release( ((vlc_object_t*) p_this) );
215     REPLY_SEND;
216 }
217
218 DBUS_METHOD( VolumeGet )
219 { /* returns volume in percentage */
220     REPLY_INIT;
221     OUT_ARGUMENTS;
222     dbus_int32_t i_dbus_vol;
223     audio_volume_t i_vol;
224     /* 2nd argument of aout_VolumeGet is int32 */
225     aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
226     i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
227     ADD_INT32( &i_dbus_vol );
228     REPLY_SEND;
229 }
230
231 DBUS_METHOD( VolumeSet )
232 { /* set volume in percentage */
233     REPLY_INIT;
234
235     DBusError error;
236     dbus_error_init( &error );
237
238     dbus_int32_t i_dbus_vol;
239     audio_volume_t i_vol;
240
241     dbus_message_get_args( p_from, &error,
242             DBUS_TYPE_INT32, &i_dbus_vol,
243             DBUS_TYPE_INVALID );
244
245     if( dbus_error_is_set( &error ) )
246     {
247         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
248                 error.message );
249         dbus_error_free( &error );
250         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
251     }
252
253     i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
254     aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
255
256     REPLY_SEND;
257 }
258
259 DBUS_METHOD( Next )
260 { /* next playlist item */
261     REPLY_INIT;
262     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
263     playlist_Next( p_playlist );
264     pl_Release( ((vlc_object_t*) p_this) );
265     REPLY_SEND;
266 }
267
268 DBUS_METHOD( Prev )
269 { /* previous playlist item */
270     REPLY_INIT;
271     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
272     playlist_Prev( p_playlist );
273     pl_Release( ((vlc_object_t*) p_this) );
274     REPLY_SEND;
275 }
276
277 DBUS_METHOD( Stop )
278 { /* stop playing */
279     REPLY_INIT;
280     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
281     playlist_Stop( p_playlist );
282     pl_Release( ((vlc_object_t*) p_this) );
283     REPLY_SEND;
284 }
285
286 DBUS_METHOD( GetStatus )
287 { /* returns an int: 0=playing 1=paused 2=stopped */
288     REPLY_INIT;
289     OUT_ARGUMENTS;
290
291     dbus_int32_t i_status;
292     vlc_value_t val;
293
294     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
295     input_thread_t *p_input = p_playlist->p_input;
296
297     i_status = 2;
298     if( p_input )
299     {
300         var_Get( p_input, "state", &val );
301         if( val.i_int == PAUSE_S )
302             i_status = 1;
303         else if( val.i_int == PLAYING_S )
304             i_status = 0;
305     }
306
307     pl_Release( p_playlist );
308
309     ADD_INT32( &i_status );
310     REPLY_SEND;
311 }
312
313 DBUS_METHOD( Pause )
314 {
315     REPLY_INIT;
316     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
317     playlist_Pause( p_playlist );
318     pl_Release( p_playlist );
319     REPLY_SEND;
320 }
321
322 DBUS_METHOD( Play )
323 {
324     REPLY_INIT;
325     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
326     playlist_Play( p_playlist );
327     pl_Release( p_playlist );
328     REPLY_SEND;
329 }
330
331 DBUS_METHOD( GetCurrentMetadata )
332 {
333     REPLY_INIT;
334     OUT_ARGUMENTS;
335     playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
336
337     GetInputMeta( p_playlist->status.p_item->p_input, &args );
338
339     pl_Release( p_playlist );
340     REPLY_SEND;
341 }
342
343 /* Media Player information */
344
345 DBUS_METHOD( Identity )
346 {
347     REPLY_INIT;
348     OUT_ARGUMENTS;
349     char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 1 );
350     sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
351     ADD_STRING( &psz_identity );
352     free( psz_identity );
353     REPLY_SEND;
354 }
355
356 /* TrackList */
357
358 DBUS_METHOD( AddTrack )
359 { /* add the string to the playlist, and play it if the boolean is true */
360     REPLY_INIT;
361
362     DBusError error;
363     dbus_error_init( &error );
364     playlist_t* p_playlist = NULL;
365
366     char *psz_mrl;
367     dbus_bool_t b_play;
368
369     dbus_message_get_args( p_from, &error,
370             DBUS_TYPE_STRING, &psz_mrl,
371             DBUS_TYPE_BOOLEAN, &b_play,
372             DBUS_TYPE_INVALID );
373
374     if( dbus_error_is_set( &error ) )
375     {
376         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
377                 error.message );
378         dbus_error_free( &error );
379         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
380     }
381
382     p_playlist = pl_Yield( (vlc_object_t*) p_this );
383     playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
384             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
385             PLAYLIST_END, VLC_TRUE, VLC_FALSE );
386     pl_Release( p_playlist );
387
388     REPLY_SEND;
389 }
390
391 DBUS_METHOD( GetCurrentTrack )
392 {
393     REPLY_INIT;
394     OUT_ARGUMENTS;
395     dbus_int32_t i_position = 0;
396     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
397     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
398
399     while ( p_tested_item->p_input->i_id !=
400                     p_playlist->status.p_item->p_input->i_id )
401     {
402         i_position++;
403         TEST_NEXT;
404     }
405
406     pl_Release( p_playlist );
407
408     ADD_INT32( &i_position );
409     REPLY_SEND;
410 }
411
412 DBUS_METHOD( GetMetadata )
413 {
414     REPLY_INIT;
415     OUT_ARGUMENTS;
416     DBusError error;
417     dbus_error_init( &error );
418
419     dbus_int32_t i_position, i_count = 0;
420
421     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
422     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
423
424     dbus_message_get_args( p_from, &error,
425             DBUS_TYPE_INT32, &i_position,
426             DBUS_TYPE_INVALID );
427
428     if( dbus_error_is_set( &error ) )
429     {
430         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
431                 error.message );
432         dbus_error_free( &error );
433         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
434     }
435
436     while ( i_count < i_position ) 
437     {
438         i_count++;
439         TEST_NEXT;
440     }
441
442     GetInputMeta ( p_tested_item->p_input, &args );
443
444     pl_Release( p_playlist );
445     REPLY_SEND;
446 }
447
448 DBUS_METHOD( GetLength )
449 {
450     REPLY_INIT;
451     OUT_ARGUMENTS;
452
453     dbus_int32_t i_elements = 0;
454     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
455     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
456     playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist,
457                     p_playlist->p_root_onelevel ); 
458
459     while ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id )
460     {
461         i_elements++;
462         TEST_NEXT;
463     }
464
465     pl_Release( p_playlist );
466
467     ADD_INT32( &i_elements );
468     REPLY_SEND;
469 }
470
471 DBUS_METHOD( DelTrack )
472 {
473     REPLY_INIT;
474
475     DBusError error;
476     dbus_error_init( &error );
477
478     dbus_int32_t i_position, i_count = 0;
479     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
480     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
481
482     dbus_message_get_args( p_from, &error,
483             DBUS_TYPE_INT32, &i_position,
484             DBUS_TYPE_INVALID );
485
486     if( dbus_error_is_set( &error ) )
487     {
488         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
489                 error.message );
490         dbus_error_free( &error );
491         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
492     }
493
494     while ( i_count < i_position ) 
495     {
496         i_count++;
497         TEST_NEXT;
498     }
499
500     PL_LOCK; 
501     playlist_DeleteFromInput( p_playlist,
502         p_tested_item->p_input->i_id,
503         VLC_TRUE );
504     PL_UNLOCK;
505
506     pl_Release( p_playlist );
507
508     REPLY_SEND;
509 }
510
511 DBUS_METHOD( Loop )
512 {
513     REPLY_INIT;
514     OUT_ARGUMENTS;
515
516     DBusError error;
517     dbus_bool_t b_loop;
518     vlc_value_t val;
519     playlist_t* p_playlist = NULL;
520
521     dbus_error_init( &error );
522     dbus_message_get_args( p_from, &error,
523             DBUS_TYPE_BOOLEAN, &b_loop,
524             DBUS_TYPE_INVALID );
525
526     if( dbus_error_is_set( &error ) )
527     {
528         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
529                 error.message );
530         dbus_error_free( &error );
531         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
532     }
533
534     val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
535     p_playlist = pl_Yield( (vlc_object_t*) p_this );
536     var_Set ( p_playlist, "loop", val );
537     pl_Release( ((vlc_object_t*) p_this) );
538
539     REPLY_SEND;
540 }
541
542 DBUS_METHOD( Repeat )
543 {
544     REPLY_INIT;
545     OUT_ARGUMENTS;
546
547     DBusError error;
548     dbus_bool_t b_repeat;
549     vlc_value_t val;
550     playlist_t* p_playlist = NULL;
551
552     dbus_error_init( &error );
553     dbus_message_get_args( p_from, &error,
554             DBUS_TYPE_BOOLEAN, &b_repeat,
555             DBUS_TYPE_INVALID );
556
557     if( dbus_error_is_set( &error ) )
558     {
559         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
560                 error.message );
561         dbus_error_free( &error );
562         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
563     }
564
565     val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
566
567     p_playlist = pl_Yield( (vlc_object_t*) p_this );
568     var_Set ( p_playlist, "repeat", val );
569     pl_Release( ((vlc_object_t*) p_this) );
570
571     REPLY_SEND;
572 }
573
574 DBUS_METHOD( Random )
575 {
576     REPLY_INIT;
577     OUT_ARGUMENTS;
578
579     DBusError error;
580     dbus_bool_t b_random;
581     vlc_value_t val;
582     playlist_t* p_playlist = NULL;
583     
584     dbus_error_init( &error );
585     dbus_message_get_args( p_from, &error,
586             DBUS_TYPE_BOOLEAN, &b_random,
587             DBUS_TYPE_INVALID );
588     
589     if( dbus_error_is_set( &error ) )
590     {
591         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
592                 error.message );
593         dbus_error_free( &error );
594         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
595     }
596
597     val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
598     
599     p_playlist = pl_Yield( (vlc_object_t*) p_this );
600     var_Set ( p_playlist, "random", val );
601     pl_Release( ((vlc_object_t*) p_this) );
602
603     REPLY_SEND;
604 }
605 /*****************************************************************************
606  * Introspection method
607  *****************************************************************************/
608
609 DBUS_METHOD( handle_introspect_root )
610 { /* handles introspection of /org/videolan/vlc */
611     REPLY_INIT;
612     OUT_ARGUMENTS;
613     ADD_STRING( &psz_introspection_xml_data_root );
614     REPLY_SEND;
615 }
616
617 DBUS_METHOD( handle_introspect_player )
618 {
619     REPLY_INIT;
620     OUT_ARGUMENTS;
621     ADD_STRING( &psz_introspection_xml_data_player );
622     REPLY_SEND;
623 }
624
625 DBUS_METHOD( handle_introspect_tracklist )
626 {
627     REPLY_INIT;
628     OUT_ARGUMENTS;
629     ADD_STRING( &psz_introspection_xml_data_tracklist );
630     REPLY_SEND;
631 }
632
633 /*****************************************************************************
634  * handle_*: answer to incoming messages
635  *****************************************************************************/
636
637 #define METHOD_FUNC( method, function ) \
638     else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
639         return function( p_conn, p_from, p_this )
640
641 DBUS_METHOD( handle_root )
642 {
643
644     if( dbus_message_is_method_call( p_from,
645                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
646         return handle_introspect_root( p_conn, p_from, p_this );
647
648     /* here D-Bus method's names are associated to an handler */
649
650     METHOD_FUNC( "Identity",                Identity );
651
652     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
653 }
654
655
656 DBUS_METHOD( handle_player )
657 {
658     if( dbus_message_is_method_call( p_from,
659                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
660     return handle_introspect_player( p_conn, p_from, p_this );
661
662     /* here D-Bus method's names are associated to an handler */
663
664     METHOD_FUNC( "Prev",                    Prev );
665     METHOD_FUNC( "Next",                    Next );
666     METHOD_FUNC( "Quit",                    Quit );
667     METHOD_FUNC( "Stop",                    Stop );
668     METHOD_FUNC( "Play",                    Play );
669     METHOD_FUNC( "Pause",                   Pause );
670     METHOD_FUNC( "Repeat",                  Repeat );
671     METHOD_FUNC( "VolumeSet",               VolumeSet );
672     METHOD_FUNC( "VolumeGet",               VolumeGet );
673     METHOD_FUNC( "PositionSet",             PositionSet );
674     METHOD_FUNC( "PositionGet",             PositionGet );
675     METHOD_FUNC( "GetStatus",               GetStatus );
676     METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
677
678     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
679 }
680
681 DBUS_METHOD( handle_tracklist )
682 {
683     if( dbus_message_is_method_call( p_from,
684                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
685     return handle_introspect_tracklist( p_conn, p_from, p_this );
686
687     /* here D-Bus method's names are associated to an handler */
688
689     METHOD_FUNC( "GetMetadata",             GetMetadata );
690     METHOD_FUNC( "GetCurrentTrack",         GetCurrentTrack );
691     METHOD_FUNC( "GetLength",               GetLength );
692     METHOD_FUNC( "AddTrack",                AddTrack );
693     METHOD_FUNC( "DelTrack",                DelTrack );
694     METHOD_FUNC( "Loop",                    Loop );
695     METHOD_FUNC( "Random",                  Random );
696
697     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
698 }
699
700 /*****************************************************************************
701  * Open: initialize interface
702  *****************************************************************************/
703
704 static int Open( vlc_object_t *p_this )
705 { /* initialisation of the connection */
706     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
707     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
708     playlist_t      *p_playlist;
709     DBusConnection  *p_conn;
710     DBusError       error;
711
712     if( !p_sys )
713         return VLC_ENOMEM;
714
715     dbus_threads_init_default();
716
717     dbus_error_init( &error );
718
719     /* connect to the session bus */
720     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
721     if( !p_conn )
722     {
723         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
724                 error.message );
725         dbus_error_free( &error );
726         free( p_sys );
727         return VLC_EGENERIC;
728     }
729
730     /* register a well-known name on the bus */
731     dbus_bus_request_name( p_conn, "org.freedesktop.MediaPlayer", 0, &error );
732     if( dbus_error_is_set( &error ) )
733     {
734         msg_Err( p_this, "Error requesting org.freedesktop.MediaPlayer service:"                " %s\n", error.message );
735         dbus_error_free( &error );
736         free( p_sys );
737         return VLC_EGENERIC;
738     }
739
740     /* we register the objects */
741     dbus_connection_register_object_path( p_conn, VLC_DBUS_ROOT_PATH,
742             &vlc_dbus_root_vtable, p_this );
743     dbus_connection_register_object_path( p_conn, VLC_DBUS_PLAYER_PATH,
744             &vlc_dbus_player_vtable, p_this );
745     dbus_connection_register_object_path( p_conn, VLC_DBUS_TRACKLIST_PATH,
746             &vlc_dbus_tracklist_vtable, p_this );
747
748     dbus_connection_flush( p_conn );
749
750     p_playlist = pl_Yield( p_intf );
751     PL_LOCK;
752     var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
753     PL_UNLOCK;
754     pl_Release( p_playlist );
755
756     p_intf->pf_run = Run;
757     p_intf->p_sys = p_sys;
758     p_sys->p_conn = p_conn;
759
760     return VLC_SUCCESS;
761 }
762
763 /*****************************************************************************
764  * Close: destroy interface
765  *****************************************************************************/
766
767 static void Close   ( vlc_object_t *p_this )
768 {
769     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
770     playlist_t      *p_playlist = pl_Yield( p_intf );;
771
772     PL_LOCK;
773     var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
774     PL_UNLOCK;
775     pl_Release( p_playlist );
776
777     dbus_connection_unref( p_intf->p_sys->p_conn );
778
779     free( p_intf->p_sys );
780 }
781
782 /*****************************************************************************
783  * Run: main loop
784  *****************************************************************************/
785
786 static void Run          ( intf_thread_t *p_intf )
787 {
788     while( !p_intf->b_die )
789     {
790         msleep( INTF_IDLE_SLEEP );
791         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
792     }
793 }
794
795 /*****************************************************************************
796  * TrackChange: Playlist item change callback
797  *****************************************************************************/
798
799 DBUS_SIGNAL( TrackChangeSignal )
800 { /* emit the metadata of the new item */
801     SIGNAL_INIT( "TrackChange" );
802     OUT_ARGUMENTS;
803
804     input_thread_t *p_input = (input_thread_t*) p_data;
805     GetInputMeta ( input_GetItem(p_input), &args );
806
807     SIGNAL_SEND;
808 }
809
810 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
811             vlc_value_t oldval, vlc_value_t newval, void *p_data )
812 {
813     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
814     intf_sys_t          *p_sys      = p_intf->p_sys;
815     playlist_t          *p_playlist;
816     input_thread_t      *p_input    = NULL;
817     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
818
819     p_playlist = pl_Yield( p_intf );
820     PL_LOCK;
821     p_input = p_playlist->p_input;
822
823     if( !p_input )
824     {
825         PL_UNLOCK;
826         pl_Release( p_playlist );
827         return VLC_SUCCESS;
828     }
829
830     vlc_object_yield( p_input );
831     PL_UNLOCK;
832     pl_Release( p_playlist );
833
834     TrackChangeSignal( p_sys->p_conn, p_input );
835
836     vlc_object_release( p_input );
837     return VLC_SUCCESS;
838 }
839
840 /*****************************************************************************
841  * GetInputMeta: Fill a DBusMessage with the given input item metadata
842  *****************************************************************************/
843
844 #define ADD_META( entry, type, data ) \
845     if( data ) { \
846         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
847                 NULL, &dict_entry ); \
848         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
849                 &ppsz_meta_items[entry] ); \
850         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
851                 type##_AS_STRING, &variant ); \
852         dbus_message_iter_append_basic( &variant, \
853                 type, \
854                 & data ); \
855         dbus_message_iter_close_container( &dict_entry, &variant ); \
856         dbus_message_iter_close_container( &dict, &dict_entry ); }\
857
858 #define ADD_VLC_META_STRING( entry, item ) \
859         ADD_META( entry, DBUS_TYPE_STRING, \
860                 p_input->p_meta->psz_##item );
861
862 static int GetInputMeta( input_item_t* p_input,
863                         DBusMessageIter *args )
864 { /*FIXME: Works only for already read metadata. */
865
866     DBusMessageIter dict, dict_entry, variant;
867     /* We need the track length to be expressed in seconds
868      * instead of milliseconds */
869     dbus_int64_t i_length = (p_input->i_duration / 1000);
870
871
872     const char* ppsz_meta_items[] = 
873     {
874     "title", "artist", "genre", "copyright", "album", "tracknum",
875     "description", "rating", "date", "setting", "url", "language",
876     "nowplaying", "publisher", "encodedby", "arturl", "trackid",
877     "status", "URI", "length", "video-codec", "audio-codec",
878     "video-bitrate", "audio-bitrate", "audio-samplerate"
879     };
880
881     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
882
883     ADD_VLC_META_STRING( 0, title );
884     ADD_VLC_META_STRING( 1, artist );
885     ADD_VLC_META_STRING( 2, genre );
886     ADD_VLC_META_STRING( 3, copyright );
887     ADD_VLC_META_STRING( 4, album );
888     ADD_VLC_META_STRING( 5, tracknum );
889     ADD_VLC_META_STRING( 6, description );
890     ADD_VLC_META_STRING( 7, rating );
891     ADD_VLC_META_STRING( 8, date );
892     ADD_VLC_META_STRING( 9, setting );
893     ADD_VLC_META_STRING( 10, url );
894     ADD_VLC_META_STRING( 11, language );
895     ADD_VLC_META_STRING( 12, nowplaying );
896     ADD_VLC_META_STRING( 13, publisher );
897     ADD_VLC_META_STRING( 14, encodedby );
898     ADD_VLC_META_STRING( 15, arturl );
899     ADD_VLC_META_STRING( 16, trackid ); 
900
901     ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
902     ADD_META( 18, DBUS_TYPE_STRING, p_input->psz_uri );
903     ADD_META( 19, DBUS_TYPE_INT64, i_length );
904
905     dbus_message_iter_close_container( args, &dict );
906     return VLC_SUCCESS;
907 }
908
909 #undef ADD_META
910 #undef ADD_VLC_META_STRING