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