]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
ignore folder contents
[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 reordering */
841 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
842             vlc_value_t oldval, vlc_value_t newval, void *p_data )
843 {
844     VLC_UNUSED(oldval);
845     intf_thread_t *p_intf = p_data;
846
847     if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
848     {
849         /* don't signal when items are added/removed in p_category */
850         playlist_t *p_playlist = (playlist_t*)p_this;
851         playlist_add_t *p_add = newval.p_address;
852         playlist_item_t *p_item;
853         p_item = playlist_ItemGetById( p_playlist, p_add->i_node, VLC_TRUE );
854         assert( p_item );
855         while( p_item->p_parent )
856             p_item = p_item->p_parent;
857         if( p_item == p_playlist->p_root_category )
858             return VLC_SUCCESS;
859     }
860
861     if( p_intf->b_dead )
862         return VLC_SUCCESS;
863
864     TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
865     return VLC_SUCCESS;
866 }
867 /*****************************************************************************
868  * TrackChange: Playlist item change callback
869  *****************************************************************************/
870
871 DBUS_SIGNAL( TrackChangeSignal )
872 { /* emit the metadata of the new item */
873     SIGNAL_INIT( "TrackChange" );
874     OUT_ARGUMENTS;
875
876     input_item_t *p_item = (input_item_t*) p_data;
877     GetInputMeta ( p_item, &args );
878
879     SIGNAL_SEND;
880 }
881
882 /*****************************************************************************
883  * StatusChange: Player status change signal
884  *****************************************************************************/
885
886 DBUS_SIGNAL( StatusChangeSignal )
887 { /* send the updated status info on the bus */
888     SIGNAL_INIT( "StatusChange" );
889     OUT_ARGUMENTS;
890
891     /* we're called from a callback of input_thread_t, so it can not be
892      * destroyed before we return */
893     MarshalStatus( (intf_thread_t*) p_data, &args, VLC_FALSE );
894
895     SIGNAL_SEND;
896 }
897
898 /*****************************************************************************
899  * StateChange: callback on input "state"
900  *****************************************************************************/
901 static int StateChange( vlc_object_t *p_this, const char* psz_var,
902             vlc_value_t oldval, vlc_value_t newval, void *p_data )
903 {
904     VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
905     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
906     intf_sys_t          *p_sys      = p_intf->p_sys;
907
908     if( p_intf->b_dead )
909         return VLC_SUCCESS;
910
911     if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
912     {
913         input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
914         if( p_item )
915         {
916             p_sys->b_meta_read = VLC_TRUE;
917             TrackChangeSignal( p_sys->p_conn, p_item );
918         }
919     }
920
921     if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
922         newval.i_int == END_S )
923     {
924         StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
925     }
926
927     return VLC_SUCCESS;
928 }
929
930 /*****************************************************************************
931  * StatusChangeEmit: Emits the StatusChange signal
932  *****************************************************************************/
933 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
934             vlc_value_t oldval, vlc_value_t newval, void *p_data )
935 {
936     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
937     VLC_UNUSED(oldval); VLC_UNUSED(newval);
938     intf_thread_t *p_intf = p_data;
939
940     if( p_intf->b_dead )
941         return VLC_SUCCESS;
942
943     StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
944     return VLC_SUCCESS;
945 }
946
947 /*****************************************************************************
948  * TrackChange: callback on playlist "playlist-current"
949  *****************************************************************************/
950 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
951             vlc_value_t oldval, vlc_value_t newval, void *p_data )
952 {
953     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
954     intf_sys_t          *p_sys      = p_intf->p_sys;
955     playlist_t          *p_playlist;
956     input_thread_t      *p_input    = NULL;
957     input_item_t        *p_item     = NULL;
958     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
959     VLC_UNUSED( oldval ); VLC_UNUSED( newval );
960
961     if( p_intf->b_dead )
962         return VLC_SUCCESS;
963
964     p_sys->b_meta_read = VLC_FALSE;
965
966     p_playlist = pl_Yield( p_intf );
967     PL_LOCK;
968     p_input = p_playlist->p_input;
969
970     if( !p_input )
971     {
972         PL_UNLOCK;
973         pl_Release( p_playlist );
974         return VLC_SUCCESS;
975     }
976
977     vlc_object_yield( p_input );
978     PL_UNLOCK;
979     pl_Release( p_playlist );
980
981     p_item = input_GetItem( p_input );
982     if( !p_item )
983     {
984         vlc_object_release( p_input );
985         return VLC_EGENERIC;
986     }
987
988     if( input_item_IsPreparsed( p_item ) )
989     {
990         p_sys->b_meta_read = VLC_TRUE;
991         TrackChangeSignal( p_sys->p_conn, p_item );
992     }
993
994     dbus_int32_t i_caps = CAPS_NONE;
995
996     if( p_playlist->items.i_size > 0 )
997         i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
998
999     if( p_playlist->p_input )
1000     {
1001         access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input,
1002             VLC_OBJECT_ACCESS, FIND_CHILD );
1003         if( p_access )
1004         {
1005             vlc_bool_t b_can_pause;
1006             if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
1007                     b_can_pause )
1008                 i_caps |= CAPS_CAN_PAUSE;
1009             vlc_object_release( p_access );
1010         }
1011         demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
1012             VLC_OBJECT_DEMUX, FIND_CHILD );
1013         if( p_demux )
1014         {   /* XXX: is: demux can seek and access can not a possibility ? */
1015             vlc_bool_t b_can_seek;
1016             if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
1017                     b_can_seek )
1018                 i_caps |= CAPS_CAN_SEEK;
1019             vlc_object_release( p_demux );
1020         }
1021     }
1022
1023     if( ((intf_thread_t*)p_this)->p_sys->b_meta_read )
1024         i_caps |= CAPS_CAN_PROVIDE_METADATA;
1025
1026
1027     var_AddCallback( p_input, "state", StateChange, p_intf );
1028
1029     vlc_object_release( p_input );
1030     return VLC_SUCCESS;
1031 }
1032
1033 /*****************************************************************************
1034  * GetInputMeta: Fill a DBusMessage with the given input item metadata
1035  *****************************************************************************/
1036
1037 #define ADD_META( entry, type, data ) \
1038     if( data ) { \
1039         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1040                 NULL, &dict_entry ); \
1041         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1042                 &ppsz_meta_items[entry] ); \
1043         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1044                 type##_AS_STRING, &variant ); \
1045         dbus_message_iter_append_basic( &variant, \
1046                 type, \
1047                 & data ); \
1048         dbus_message_iter_close_container( &dict_entry, &variant ); \
1049         dbus_message_iter_close_container( &dict, &dict_entry ); }
1050
1051 #define ADD_VLC_META_STRING( entry, item ) \
1052     { \
1053         char * psz = input_item_Get##item( p_input );\
1054         ADD_META( entry, DBUS_TYPE_STRING, \
1055                   psz ); \
1056         free( psz ); \
1057     }
1058
1059 static int GetInputMeta( input_item_t* p_input,
1060                         DBusMessageIter *args )
1061 {
1062     DBusMessageIter dict, dict_entry, variant;
1063     /* We need the track length to be expressed in milli-seconds
1064      * instead of µ-seconds */
1065     dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1066
1067     const char* ppsz_meta_items[] =
1068     {
1069     "title", "artist", "genre", "copyright", "album", "tracknum",
1070     "description", "rating", "date", "setting", "url", "language",
1071     "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1072     "status", "URI", "length", "video-codec", "audio-codec",
1073     "video-bitrate", "audio-bitrate", "audio-samplerate"
1074     };
1075
1076     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1077
1078     ADD_VLC_META_STRING( 0,  Title );
1079     ADD_VLC_META_STRING( 1,  Artist );
1080     ADD_VLC_META_STRING( 2,  Genre );
1081     ADD_VLC_META_STRING( 3,  Copyright );
1082     ADD_VLC_META_STRING( 4,  Album );
1083     ADD_VLC_META_STRING( 5,  TrackNum );
1084     ADD_VLC_META_STRING( 6,  Description );
1085     ADD_VLC_META_STRING( 7,  Rating );
1086     ADD_VLC_META_STRING( 8,  Date );
1087     ADD_VLC_META_STRING( 9,  Setting );
1088     ADD_VLC_META_STRING( 10, URL );
1089     ADD_VLC_META_STRING( 11, Language );
1090     ADD_VLC_META_STRING( 12, NowPlaying );
1091     ADD_VLC_META_STRING( 13, Publisher );
1092     ADD_VLC_META_STRING( 14, EncodedBy );
1093     ADD_VLC_META_STRING( 15, ArtURL );
1094     ADD_VLC_META_STRING( 16, TrackID );
1095
1096     vlc_mutex_lock( &p_input->lock );
1097     if( p_input->p_meta )
1098         ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1099     vlc_mutex_unlock( &p_input->lock );
1100
1101     ADD_VLC_META_STRING( 18, URI );
1102     ADD_META( 19, DBUS_TYPE_INT64, i_length );
1103
1104     dbus_message_iter_close_container( args, &dict );
1105     return VLC_SUCCESS;
1106 }
1107
1108 #undef ADD_META
1109 #undef ADD_VLC_META_STRING
1110
1111 /*****************************************************************************
1112  * MarshalStatus: Fill a DBusMessage with the current player status
1113  *****************************************************************************/
1114
1115 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1116                           vlc_bool_t lock )
1117 { /* This is NOT the right way to do that, it would be better to sore
1118      the status information in p_sys and update it on change, thus
1119      avoiding a long lock */
1120
1121     DBusMessageIter status;
1122     dbus_int32_t i_state, i_random, i_repeat, i_loop;
1123     vlc_value_t val;
1124     playlist_t* p_playlist = NULL;
1125     input_thread_t* p_input = NULL;
1126
1127     p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1128     if( lock )
1129         PL_LOCK;
1130
1131     i_state = 2;
1132
1133     p_input = p_playlist->p_input;
1134     if( p_input )
1135     {
1136         var_Get( p_input, "state", &val );
1137         if( val.i_int >= END_S )
1138             i_state = 2;
1139         else if( val.i_int == PAUSE_S )
1140             i_state = 1;
1141         else if( val.i_int <= PLAYING_S )
1142             i_state = 0;
1143     }
1144
1145     var_Get( p_playlist, "random", &val );
1146     i_random = val.i_int;
1147
1148     var_Get( p_playlist, "repeat", &val );
1149     i_repeat = val.i_int;
1150
1151     var_Get( p_playlist, "loop", &val );
1152     i_loop = val.i_int;
1153
1154     if( lock )
1155         PL_UNLOCK;
1156     pl_Release( p_playlist );
1157
1158     dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1159     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1160     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1161     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1162     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1163     dbus_message_iter_close_container( args, &status );
1164
1165     return VLC_SUCCESS;
1166 }