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