]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
Read the correct type from the vlc value - fixes #1569
[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->items.i_size / 2 )
439     {
440         GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->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->items.i_size / 2;
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     if( i_position <= p_playlist->items.i_size / 2 )
484     {
485         playlist_DeleteFromInput( p_playlist,
486             p_playlist->items.p_elems[i_position*2-1]->i_id,
487             false );
488     }
489
490     pl_Release( p_playlist );
491
492     REPLY_SEND;
493 }
494
495 DBUS_METHOD( SetLoop )
496 {
497     REPLY_INIT;
498     OUT_ARGUMENTS;
499
500     DBusError error;
501     dbus_bool_t b_loop;
502     vlc_value_t val;
503     playlist_t* p_playlist = NULL;
504
505     dbus_error_init( &error );
506     dbus_message_get_args( p_from, &error,
507             DBUS_TYPE_BOOLEAN, &b_loop,
508             DBUS_TYPE_INVALID );
509
510     if( dbus_error_is_set( &error ) )
511     {
512         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
513                 error.message );
514         dbus_error_free( &error );
515         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
516     }
517
518     val.b_bool = ( b_loop == TRUE ) ? true : false ;
519     p_playlist = pl_Yield( (vlc_object_t*) p_this );
520     var_Set ( p_playlist, "loop", val );
521     pl_Release( ((vlc_object_t*) p_this) );
522
523     REPLY_SEND;
524 }
525
526 DBUS_METHOD( Repeat )
527 {
528     REPLY_INIT;
529     OUT_ARGUMENTS;
530
531     DBusError error;
532     dbus_bool_t b_repeat;
533     vlc_value_t val;
534     playlist_t* p_playlist = NULL;
535
536     dbus_error_init( &error );
537     dbus_message_get_args( p_from, &error,
538             DBUS_TYPE_BOOLEAN, &b_repeat,
539             DBUS_TYPE_INVALID );
540
541     if( dbus_error_is_set( &error ) )
542     {
543         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
544                 error.message );
545         dbus_error_free( &error );
546         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
547     }
548
549     val.b_bool = ( b_repeat == TRUE ) ? true : false ;
550
551     p_playlist = pl_Yield( (vlc_object_t*) p_this );
552     var_Set ( p_playlist, "repeat", val );
553     pl_Release( ((vlc_object_t*) p_this) );
554
555     REPLY_SEND;
556 }
557
558 DBUS_METHOD( SetRandom )
559 {
560     REPLY_INIT;
561     OUT_ARGUMENTS;
562
563     DBusError error;
564     dbus_bool_t b_random;
565     vlc_value_t val;
566     playlist_t* p_playlist = NULL;
567
568     dbus_error_init( &error );
569     dbus_message_get_args( p_from, &error,
570             DBUS_TYPE_BOOLEAN, &b_random,
571             DBUS_TYPE_INVALID );
572
573     if( dbus_error_is_set( &error ) )
574     {
575         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
576                 error.message );
577         dbus_error_free( &error );
578         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
579     }
580
581     val.b_bool = ( b_random == TRUE ) ? true : false ;
582
583     p_playlist = pl_Yield( (vlc_object_t*) p_this );
584     var_Set ( p_playlist, "random", val );
585     pl_Release( ((vlc_object_t*) p_this) );
586
587     REPLY_SEND;
588 }
589 /*****************************************************************************
590  * Introspection method
591  *****************************************************************************/
592
593 DBUS_METHOD( handle_introspect_root )
594 { /* handles introspection of root object */
595     VLC_UNUSED(p_this);
596     REPLY_INIT;
597     OUT_ARGUMENTS;
598     ADD_STRING( &psz_introspection_xml_data_root );
599     REPLY_SEND;
600 }
601
602 DBUS_METHOD( handle_introspect_player )
603 {
604     VLC_UNUSED(p_this);
605     REPLY_INIT;
606     OUT_ARGUMENTS;
607     ADD_STRING( &psz_introspection_xml_data_player );
608     REPLY_SEND;
609 }
610
611 DBUS_METHOD( handle_introspect_tracklist )
612 {
613     VLC_UNUSED(p_this);
614     REPLY_INIT;
615     OUT_ARGUMENTS;
616     ADD_STRING( &psz_introspection_xml_data_tracklist );
617     REPLY_SEND;
618 }
619
620 /*****************************************************************************
621  * handle_*: answer to incoming messages
622  *****************************************************************************/
623
624 #define METHOD_FUNC( method, function ) \
625     else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
626         return function( p_conn, p_from, p_this )
627
628 DBUS_METHOD( handle_root )
629 {
630
631     if( dbus_message_is_method_call( p_from,
632                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
633         return handle_introspect_root( p_conn, p_from, p_this );
634
635     /* here D-Bus method's names are associated to an handler */
636
637     METHOD_FUNC( "Identity",                Identity );
638     METHOD_FUNC( "MprisVersion",            MprisVersion );
639     METHOD_FUNC( "Quit",                    Quit );
640
641     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
642 }
643
644
645 DBUS_METHOD( handle_player )
646 {
647     if( dbus_message_is_method_call( p_from,
648                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
649         return handle_introspect_player( p_conn, p_from, p_this );
650
651     /* here D-Bus method's names are associated to an handler */
652
653     METHOD_FUNC( "Prev",                    Prev );
654     METHOD_FUNC( "Next",                    Next );
655     METHOD_FUNC( "Stop",                    Stop );
656     METHOD_FUNC( "Play",                    Play );
657     METHOD_FUNC( "Pause",                   Pause );
658     METHOD_FUNC( "Repeat",                  Repeat );
659     METHOD_FUNC( "VolumeSet",               VolumeSet );
660     METHOD_FUNC( "VolumeGet",               VolumeGet );
661     METHOD_FUNC( "PositionSet",             PositionSet );
662     METHOD_FUNC( "PositionGet",             PositionGet );
663     METHOD_FUNC( "GetStatus",               GetStatus );
664     METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
665     METHOD_FUNC( "GetCaps",                 GetCaps );
666
667     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
668 }
669
670 DBUS_METHOD( handle_tracklist )
671 {
672     if( dbus_message_is_method_call( p_from,
673                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
674     return handle_introspect_tracklist( p_conn, p_from, p_this );
675
676     /* here D-Bus method's names are associated to an handler */
677
678     METHOD_FUNC( "GetMetadata",             GetMetadata );
679     METHOD_FUNC( "GetCurrentTrack",         GetCurrentTrack );
680     METHOD_FUNC( "GetLength",               GetLength );
681     METHOD_FUNC( "AddTrack",                AddTrack );
682     METHOD_FUNC( "DelTrack",                DelTrack );
683     METHOD_FUNC( "SetLoop",                 SetLoop );
684     METHOD_FUNC( "SetRandom",               SetRandom );
685
686     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
687 }
688
689 /*****************************************************************************
690  * Open: initialize interface
691  *****************************************************************************/
692
693 static int Open( vlc_object_t *p_this )
694 { /* initialisation of the connection */
695     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
696     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
697     playlist_t      *p_playlist;
698     DBusConnection  *p_conn;
699     DBusError       error;
700
701     if( !p_sys )
702         return VLC_ENOMEM;
703
704     p_sys->b_meta_read = false;
705     p_sys->i_caps = CAPS_NONE;
706
707     dbus_error_init( &error );
708
709     /* connect to the session bus */
710     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
711     if( !p_conn )
712     {
713         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
714                 error.message );
715         dbus_error_free( &error );
716         free( p_sys );
717         return VLC_EGENERIC;
718     }
719
720     /* register a well-known name on the bus */
721     dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
722     if( dbus_error_is_set( &error ) )
723     {
724         msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
725                  ": %s", error.message );
726         dbus_error_free( &error );
727         free( p_sys );
728         return VLC_EGENERIC;
729     }
730
731     /* we register the objects */
732     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
733             &vlc_dbus_root_vtable, p_this );
734     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
735             &vlc_dbus_player_vtable, p_this );
736     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
737             &vlc_dbus_tracklist_vtable, p_this );
738
739     dbus_connection_flush( p_conn );
740
741     p_playlist = pl_Yield( p_intf );
742     PL_LOCK;
743     var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
744     var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
745     var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
746     var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
747     var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
748     var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
749     var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
750     PL_UNLOCK;
751     pl_Release( p_playlist );
752
753     p_intf->pf_run = Run;
754     p_intf->p_sys = p_sys;
755     p_sys->p_conn = p_conn;
756
757     UpdateCaps( p_intf, false );
758
759     return VLC_SUCCESS;
760 }
761
762 /*****************************************************************************
763  * Close: destroy interface
764  *****************************************************************************/
765
766 static void Close   ( vlc_object_t *p_this )
767 {
768     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
769     playlist_t      *p_playlist = pl_Yield( p_intf );;
770     input_thread_t  *p_input;
771
772     p_this->b_dead = true;
773
774     PL_LOCK;
775     var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
776     var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
777     var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
778     var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
779     var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
780     var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
781     var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
782
783     p_input = p_playlist->p_input;
784     if ( p_input )
785     {
786         vlc_object_yield( p_input );
787         var_DelCallback( p_input, "state", StateChange, p_intf );
788         vlc_object_release( p_input );
789     }
790
791     PL_UNLOCK;
792     pl_Release( p_playlist );
793
794     dbus_connection_unref( p_intf->p_sys->p_conn );
795
796     free( p_intf->p_sys );
797 }
798
799 /*****************************************************************************
800  * Run: main loop
801  *****************************************************************************/
802
803 static void Run          ( intf_thread_t *p_intf )
804 {
805     while( !intf_ShouldDie( p_intf ) )
806     {
807         msleep( INTF_IDLE_SLEEP );
808         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
809     }
810 }
811
812 /******************************************************************************
813  * CapsChange: player capabilities change signal
814  *****************************************************************************/
815 DBUS_SIGNAL( CapsChangeSignal )
816 {
817     SIGNAL_INIT( "CapsChange" );
818     OUT_ARGUMENTS;
819
820     ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
821     SIGNAL_SEND;
822 }
823
824 /******************************************************************************
825  * TrackListChange: tracklist order / length change signal 
826  *****************************************************************************/
827 DBUS_SIGNAL( TrackListChangeSignal )
828 { /* emit the new tracklist lengh */
829     SIGNAL_INIT("TrackListChange");
830     OUT_ARGUMENTS;
831
832     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
833     dbus_int32_t i_elements = p_playlist->items.i_size / 2;
834     pl_Release( p_playlist );
835
836     ADD_INT32( &i_elements );
837     SIGNAL_SEND;
838 }
839
840 /*****************************************************************************
841  * TrackListChangeEmit: Emits the TrackListChange signal
842  *****************************************************************************/
843 /* FIXME: It is not called on tracklist reordering */
844 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
845             vlc_value_t oldval, vlc_value_t newval, void *p_data )
846 {
847     VLC_UNUSED(oldval);
848     intf_thread_t *p_intf = p_data;
849
850     if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
851     {
852         /* don't signal when items are added/removed in p_category */
853         playlist_t *p_playlist = (playlist_t*)p_this;
854         playlist_add_t *p_add = newval.p_address;
855         playlist_item_t *p_item;
856         p_item = playlist_ItemGetById( p_playlist, p_add->i_node, true );
857         assert( p_item );
858         while( p_item->p_parent )
859             p_item = p_item->p_parent;
860         if( p_item == p_playlist->p_root_category )
861             return VLC_SUCCESS;
862     }
863
864     if( p_intf->b_dead )
865         return VLC_SUCCESS;
866
867     UpdateCaps( p_intf, true );
868     TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
869     return VLC_SUCCESS;
870 }
871 /*****************************************************************************
872  * TrackChange: Playlist item change callback
873  *****************************************************************************/
874
875 DBUS_SIGNAL( TrackChangeSignal )
876 { /* emit the metadata of the new item */
877     SIGNAL_INIT( "TrackChange" );
878     OUT_ARGUMENTS;
879
880     input_item_t *p_item = (input_item_t*) p_data;
881     GetInputMeta ( p_item, &args );
882
883     SIGNAL_SEND;
884 }
885
886 /*****************************************************************************
887  * StatusChange: Player status change signal
888  *****************************************************************************/
889
890 DBUS_SIGNAL( StatusChangeSignal )
891 { /* send the updated status info on the bus */
892     SIGNAL_INIT( "StatusChange" );
893     OUT_ARGUMENTS;
894
895     /* we're called from a callback of input_thread_t, so it can not be
896      * destroyed before we return */
897     MarshalStatus( (intf_thread_t*) p_data, &args, false );
898
899     SIGNAL_SEND;
900 }
901
902 /*****************************************************************************
903  * StateChange: callback on input "state"
904  *****************************************************************************/
905 static int StateChange( vlc_object_t *p_this, const char* psz_var,
906             vlc_value_t oldval, vlc_value_t newval, void *p_data )
907 {
908     VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
909     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
910     intf_sys_t          *p_sys      = p_intf->p_sys;
911
912     if( p_intf->b_dead )
913         return VLC_SUCCESS;
914
915     UpdateCaps( p_intf, true );
916
917     if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
918     {
919         input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
920         if( p_item )
921         {
922             p_sys->b_meta_read = true;
923             TrackChangeSignal( p_sys->p_conn, p_item );
924         }
925     }
926
927     if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
928         newval.i_int == END_S )
929     {
930         StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
931     }
932
933     return VLC_SUCCESS;
934 }
935
936 /*****************************************************************************
937  * StatusChangeEmit: Emits the StatusChange signal
938  *****************************************************************************/
939 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
940             vlc_value_t oldval, vlc_value_t newval, void *p_data )
941 {
942     VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
943     VLC_UNUSED(oldval); VLC_UNUSED(newval);
944     intf_thread_t *p_intf = p_data;
945
946     if( p_intf->b_dead )
947         return VLC_SUCCESS;
948
949     UpdateCaps( p_intf, false );
950     StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
951     return VLC_SUCCESS;
952 }
953
954 /*****************************************************************************
955  * TrackChange: callback on playlist "playlist-current"
956  *****************************************************************************/
957 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
958             vlc_value_t oldval, vlc_value_t newval, void *p_data )
959 {
960     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
961     intf_sys_t          *p_sys      = p_intf->p_sys;
962     playlist_t          *p_playlist;
963     input_thread_t      *p_input    = NULL;
964     input_item_t        *p_item     = NULL;
965     VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
966     VLC_UNUSED( oldval ); VLC_UNUSED( newval );
967
968     if( p_intf->b_dead )
969         return VLC_SUCCESS;
970
971     p_sys->b_meta_read = false;
972
973     p_playlist = pl_Yield( p_intf );
974     p_input = p_playlist->p_input;
975
976     if( !p_input )
977     {
978         PL_UNLOCK;
979         pl_Release( p_playlist );
980         return VLC_SUCCESS;
981     }
982
983     vlc_object_yield( p_input );
984     pl_Release( p_playlist );
985
986     p_item = input_GetItem( p_input );
987     if( !p_item )
988     {
989         vlc_object_release( p_input );
990         return VLC_EGENERIC;
991     }
992
993     if( input_item_IsPreparsed( p_item ) )
994     {
995         p_sys->b_meta_read = true;
996         TrackChangeSignal( p_sys->p_conn, p_item );
997     }
998
999     var_AddCallback( p_input, "state", StateChange, p_intf );
1000
1001     vlc_object_release( p_input );
1002     return VLC_SUCCESS;
1003 }
1004
1005 /*****************************************************************************
1006  * UpdateCaps: update p_sys->i_caps
1007  ****************************************************************************/
1008 static int UpdateCaps( intf_thread_t* p_intf, bool b_playlist_locked )
1009 {
1010     intf_sys_t* p_sys = p_intf->p_sys;
1011     dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1012     playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
1013     if( !b_playlist_locked ) PL_LOCK;
1014     
1015     if( p_playlist->items.i_size > 0 )
1016         i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1017
1018     if( p_playlist->p_input )
1019     {
1020         /* XXX: if UpdateCaps() is called too early, these are
1021          * unconditionnaly true */
1022         if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1023             i_caps |= CAPS_CAN_PAUSE;
1024         if( var_GetBool( p_playlist->p_input, "seekable" ) )
1025             i_caps |= CAPS_CAN_SEEK;
1026     }
1027
1028     if( !b_playlist_locked ) PL_UNLOCK;
1029     pl_Release( p_playlist );
1030
1031     if( p_sys->b_meta_read )
1032         i_caps |= CAPS_CAN_PROVIDE_METADATA;
1033
1034     if( i_caps != p_intf->p_sys->i_caps )
1035     {
1036         p_sys->i_caps = i_caps;
1037         CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1038     }
1039
1040     return VLC_SUCCESS;
1041 }
1042
1043 /*****************************************************************************
1044  * GetInputMeta: Fill a DBusMessage with the given input item metadata
1045  *****************************************************************************/
1046
1047 #define ADD_META( entry, type, data ) \
1048     if( data ) { \
1049         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1050                 NULL, &dict_entry ); \
1051         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1052                 &ppsz_meta_items[entry] ); \
1053         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1054                 type##_AS_STRING, &variant ); \
1055         dbus_message_iter_append_basic( &variant, \
1056                 type, \
1057                 & data ); \
1058         dbus_message_iter_close_container( &dict_entry, &variant ); \
1059         dbus_message_iter_close_container( &dict, &dict_entry ); }
1060
1061 #define ADD_VLC_META_STRING( entry, item ) \
1062     { \
1063         char * psz = input_item_Get##item( p_input );\
1064         ADD_META( entry, DBUS_TYPE_STRING, \
1065                   psz ); \
1066         free( psz ); \
1067     }
1068
1069 static int GetInputMeta( input_item_t* p_input,
1070                         DBusMessageIter *args )
1071 {
1072     DBusMessageIter dict, dict_entry, variant;
1073     /* We need the track length to be expressed in milli-seconds
1074      * instead of µ-seconds */
1075     dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1076
1077     const char* ppsz_meta_items[] =
1078     {
1079     "title", "artist", "genre", "copyright", "album", "tracknum",
1080     "description", "rating", "date", "setting", "url", "language",
1081     "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1082     "status", "URI", "length", "video-codec", "audio-codec",
1083     "video-bitrate", "audio-bitrate", "audio-samplerate"
1084     };
1085
1086     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1087
1088     ADD_VLC_META_STRING( 0,  Title );
1089     ADD_VLC_META_STRING( 1,  Artist );
1090     ADD_VLC_META_STRING( 2,  Genre );
1091     ADD_VLC_META_STRING( 3,  Copyright );
1092     ADD_VLC_META_STRING( 4,  Album );
1093     ADD_VLC_META_STRING( 5,  TrackNum );
1094     ADD_VLC_META_STRING( 6,  Description );
1095     ADD_VLC_META_STRING( 7,  Rating );
1096     ADD_VLC_META_STRING( 8,  Date );
1097     ADD_VLC_META_STRING( 9,  Setting );
1098     ADD_VLC_META_STRING( 10, URL );
1099     ADD_VLC_META_STRING( 11, Language );
1100     ADD_VLC_META_STRING( 12, NowPlaying );
1101     ADD_VLC_META_STRING( 13, Publisher );
1102     ADD_VLC_META_STRING( 14, EncodedBy );
1103     ADD_VLC_META_STRING( 15, ArtURL );
1104     ADD_VLC_META_STRING( 16, TrackID );
1105
1106     vlc_mutex_lock( &p_input->lock );
1107     if( p_input->p_meta )
1108         ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1109     vlc_mutex_unlock( &p_input->lock );
1110
1111     ADD_VLC_META_STRING( 18, URI );
1112     ADD_META( 19, DBUS_TYPE_INT64, i_length );
1113
1114     dbus_message_iter_close_container( args, &dict );
1115     return VLC_SUCCESS;
1116 }
1117
1118 #undef ADD_META
1119 #undef ADD_VLC_META_STRING
1120
1121 /*****************************************************************************
1122  * MarshalStatus: Fill a DBusMessage with the current player status
1123  *****************************************************************************/
1124
1125 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1126                           bool lock )
1127 { /* This is NOT the right way to do that, it would be better to sore
1128      the status information in p_sys and update it on change, thus
1129      avoiding a long lock */
1130
1131     DBusMessageIter status;
1132     dbus_int32_t i_state, i_random, i_repeat, i_loop;
1133     vlc_value_t val;
1134     playlist_t* p_playlist = NULL;
1135     input_thread_t* p_input = NULL;
1136
1137     p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1138     if( lock )
1139         PL_LOCK;
1140
1141     i_state = 2;
1142
1143     p_input = p_playlist->p_input;
1144     if( p_input )
1145     {
1146         var_Get( p_input, "state", &val );
1147         if( val.i_int >= END_S )
1148             i_state = 2;
1149         else if( val.i_int == PAUSE_S )
1150             i_state = 1;
1151         else if( val.i_int <= PLAYING_S )
1152             i_state = 0;
1153     }
1154
1155     i_random = var_CreateGetBool( p_playlist, "random" );
1156
1157     i_repeat = var_CreateGetBool( p_playlist, "repeat" );
1158
1159     i_loop = var_CreateGetBool( p_playlist, "loop" );
1160
1161     if( lock )
1162         PL_UNLOCK;
1163     pl_Release( p_playlist );
1164
1165     dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1166     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1167     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1168     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1169     dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1170     dbus_message_iter_close_container( args, &status );
1171
1172     return VLC_SUCCESS;
1173 }