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