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