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