1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright © 2006-2008 Rafaël Carré
5 * Copyright © 2007-2008 Mirsal Ennaime
8 * Authors: Rafaël Carré <funman at videolanorg>
9 * Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
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.
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.
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 *****************************************************************************/
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
32 * "If you use this low-level API directly, you're signing up for some pain."
34 * MPRIS Specification (still drafting on Jan, 23 of 2008):
35 * http://wiki.xmms2.xmms.se/index.php/MPRIS
38 /*****************************************************************************
40 *****************************************************************************/
42 #include <dbus/dbus.h>
52 #include <vlc_interface.h>
54 #include <vlc_input.h>
55 #include <vlc_playlist.h>
56 #include <vlc_demux.h>
58 /*****************************************************************************
60 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close ( vlc_object_t * );
64 static void Run ( intf_thread_t * );
66 static int StateChange( vlc_object_t *, const char *, vlc_value_t,
67 vlc_value_t, void * );
69 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
70 vlc_value_t, void * );
72 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
73 vlc_value_t, void * );
75 static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
76 vlc_value_t, void * );
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* );
82 /* GetCaps() capabilities */
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
97 DBusConnection *p_conn;
98 vlc_bool_t b_meta_read;
102 /*****************************************************************************
104 *****************************************************************************/
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 );
115 /*****************************************************************************
117 *****************************************************************************/
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);
131 DBUS_METHOD( PositionGet )
132 { /* returns position in milliseconds */
135 vlc_value_t position;
138 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
140 input_thread_t *p_input = p_playlist->p_input;
146 var_Get( p_input, "time", &position );
147 i_pos = position.i_time / 1000;
150 pl_Release( ((vlc_object_t*) p_this) );
155 DBUS_METHOD( PositionSet )
156 { /* set position in milliseconds */
159 vlc_value_t position;
160 playlist_t* p_playlist = NULL;
164 dbus_error_init( &error );
166 dbus_message_get_args( p_from, &error,
167 DBUS_TYPE_INT32, &i_pos,
170 if( dbus_error_is_set( &error ) )
172 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
174 dbus_error_free( &error );
175 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
177 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
179 input_thread_t *p_input = p_playlist->p_input;
183 position.i_time = i_pos * 1000;
184 var_Set( p_input, "time", position );
187 pl_Release( ((vlc_object_t*) p_this) );
191 DBUS_METHOD( VolumeGet )
192 { /* returns volume in percentage */
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 );
204 DBUS_METHOD( VolumeSet )
205 { /* set volume in percentage */
209 dbus_error_init( &error );
211 dbus_int32_t i_dbus_vol;
212 audio_volume_t i_vol;
214 dbus_message_get_args( p_from, &error,
215 DBUS_TYPE_INT32, &i_dbus_vol,
218 if( dbus_error_is_set( &error ) )
220 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
222 dbus_error_free( &error );
223 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
226 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
227 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
233 { /* next playlist item */
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) );
242 { /* previous playlist item */
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) );
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) );
259 DBUS_METHOD( GetStatus )
260 { /* returns the current status as a struct of 4 ints */
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 *
270 MarshalStatus( p_this, &args, VLC_TRUE );
278 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
279 playlist_Pause( p_playlist );
280 pl_Release( p_playlist );
287 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
288 playlist_Play( p_playlist );
289 pl_Release( p_playlist );
293 DBUS_METHOD( GetCurrentMetadata )
297 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
299 if( p_playlist->status.p_item )
300 GetInputMeta( p_playlist->status.p_item->p_input, &args );
302 pl_Release( p_playlist );
306 DBUS_METHOD( GetCaps )
311 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
316 /* Media Player information */
318 DBUS_METHOD( Identity )
324 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
326 ADD_STRING( &psz_identity );
327 free( psz_identity );
330 return DBUS_HANDLER_RESULT_NEED_MEMORY;
337 DBUS_METHOD( AddTrack )
338 { /* add the string to the playlist, and play it if the boolean is true */
342 dbus_error_init( &error );
343 playlist_t* p_playlist = NULL;
348 dbus_message_get_args( p_from, &error,
349 DBUS_TYPE_STRING, &psz_mrl,
350 DBUS_TYPE_BOOLEAN, &b_play,
353 if( dbus_error_is_set( &error ) )
355 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
357 dbus_error_free( &error );
358 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
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 );
370 DBUS_METHOD( GetCurrentTrack )
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 );
379 ADD_INT32( &i_position );
383 DBUS_METHOD( GetMetadata )
388 dbus_error_init( &error );
390 dbus_int32_t i_position;
392 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
395 dbus_message_get_args( p_from, &error,
396 DBUS_TYPE_INT32, &i_position,
399 if( dbus_error_is_set( &error ) )
402 pl_Release( p_playlist );
403 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
405 dbus_error_free( &error );
406 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
409 if( i_position <= p_playlist->items.i_size / 2 )
411 GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
415 pl_Release( p_playlist );
419 DBUS_METHOD( GetLength )
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 );
428 ADD_INT32( &i_elements );
432 DBUS_METHOD( DelTrack )
437 dbus_error_init( &error );
439 dbus_int32_t i_position;
440 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
442 dbus_message_get_args( p_from, &error,
443 DBUS_TYPE_INT32, &i_position,
446 if( dbus_error_is_set( &error ) )
448 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
450 dbus_error_free( &error );
451 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
454 if( i_position <= p_playlist->items.i_size / 2 )
456 playlist_DeleteFromInput( p_playlist,
457 p_playlist->items.p_elems[i_position*2-1]->i_id,
461 pl_Release( p_playlist );
474 playlist_t* p_playlist = NULL;
476 dbus_error_init( &error );
477 dbus_message_get_args( p_from, &error,
478 DBUS_TYPE_BOOLEAN, &b_loop,
481 if( dbus_error_is_set( &error ) )
483 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
485 dbus_error_free( &error );
486 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
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) );
497 DBUS_METHOD( Repeat )
503 dbus_bool_t b_repeat;
505 playlist_t* p_playlist = NULL;
507 dbus_error_init( &error );
508 dbus_message_get_args( p_from, &error,
509 DBUS_TYPE_BOOLEAN, &b_repeat,
512 if( dbus_error_is_set( &error ) )
514 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
516 dbus_error_free( &error );
517 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
520 val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
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) );
529 DBUS_METHOD( Random )
535 dbus_bool_t b_random;
537 playlist_t* p_playlist = NULL;
539 dbus_error_init( &error );
540 dbus_message_get_args( p_from, &error,
541 DBUS_TYPE_BOOLEAN, &b_random,
544 if( dbus_error_is_set( &error ) )
546 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
548 dbus_error_free( &error );
549 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
552 val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
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) );
560 /*****************************************************************************
561 * Introspection method
562 *****************************************************************************/
564 DBUS_METHOD( handle_introspect_root )
565 { /* handles introspection of root object */
569 ADD_STRING( &psz_introspection_xml_data_root );
573 DBUS_METHOD( handle_introspect_player )
578 ADD_STRING( &psz_introspection_xml_data_player );
582 DBUS_METHOD( handle_introspect_tracklist )
587 ADD_STRING( &psz_introspection_xml_data_tracklist );
591 /*****************************************************************************
592 * handle_*: answer to incoming messages
593 *****************************************************************************/
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 )
599 DBUS_METHOD( handle_root )
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 );
606 /* here D-Bus method's names are associated to an handler */
608 METHOD_FUNC( "Identity", Identity );
610 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
614 DBUS_METHOD( handle_player )
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 );
620 /* here D-Bus method's names are associated to an handler */
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 );
637 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
640 DBUS_METHOD( handle_tracklist )
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 );
646 /* here D-Bus method's names are associated to an handler */
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 );
656 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
659 /*****************************************************************************
660 * Open: initialize interface
661 *****************************************************************************/
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;
674 p_sys->b_meta_read = VLC_FALSE;
675 p_sys->i_caps = CAPS_NONE;
677 dbus_error_init( &error );
679 /* connect to the session bus */
680 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
683 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
685 dbus_error_free( &error );
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 ) )
694 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
695 ": %s", error.message );
696 dbus_error_free( &error );
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 );
709 dbus_connection_flush( p_conn );
711 p_playlist = pl_Yield( p_intf );
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 );
721 pl_Release( p_playlist );
723 p_intf->pf_run = Run;
724 p_intf->p_sys = p_sys;
725 p_sys->p_conn = p_conn;
727 UpdateCaps( p_intf );
732 /*****************************************************************************
733 * Close: destroy interface
734 *****************************************************************************/
736 static void Close ( vlc_object_t *p_this )
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;
742 p_this->b_dead = VLC_TRUE;
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 );
753 p_input = p_playlist->p_input;
756 vlc_object_yield( p_input );
757 var_DelCallback( p_input, "state", StateChange, p_intf );
758 vlc_object_release( p_input );
762 pl_Release( p_playlist );
764 dbus_connection_unref( p_intf->p_sys->p_conn );
766 free( p_intf->p_sys );
769 /*****************************************************************************
771 *****************************************************************************/
773 static void Run ( intf_thread_t *p_intf )
775 while( !intf_ShouldDie( p_intf ) )
777 msleep( INTF_IDLE_SLEEP );
778 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
782 /******************************************************************************
783 * CapsChange: player capabilities change signal
784 *****************************************************************************/
785 DBUS_SIGNAL( CapsChangeSignal )
787 SIGNAL_INIT( "CapsChange" );
790 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
794 /******************************************************************************
795 * TrackListChange: tracklist order / length change signal
796 *****************************************************************************/
797 DBUS_SIGNAL( TrackListChangeSignal )
798 { /* emit the new tracklist lengh */
799 SIGNAL_INIT("TrackListChange");
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 );
806 ADD_INT32( &i_elements );
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 )
818 intf_thread_t *p_intf = p_data;
820 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
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 );
828 while( p_item->p_parent )
829 p_item = p_item->p_parent;
830 if( p_item == p_playlist->p_root_category )
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 );
842 /*****************************************************************************
843 * TrackChange: Playlist item change callback
844 *****************************************************************************/
846 DBUS_SIGNAL( TrackChangeSignal )
847 { /* emit the metadata of the new item */
848 SIGNAL_INIT( "TrackChange" );
851 input_item_t *p_item = (input_item_t*) p_data;
852 GetInputMeta ( p_item, &args );
857 /*****************************************************************************
858 * StatusChange: Player status change signal
859 *****************************************************************************/
861 DBUS_SIGNAL( StatusChangeSignal )
862 { /* send the updated status info on the bus */
863 SIGNAL_INIT( "StatusChange" );
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 );
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 )
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;
886 UpdateCaps( p_intf );
888 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
890 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
893 p_sys->b_meta_read = VLC_TRUE;
894 TrackChangeSignal( p_sys->p_conn, p_item );
898 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
899 newval.i_int == END_S )
901 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
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 )
913 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
914 VLC_UNUSED(oldval); VLC_UNUSED(newval);
915 intf_thread_t *p_intf = p_data;
920 UpdateCaps( p_intf );
921 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
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 )
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 );
942 p_sys->b_meta_read = VLC_FALSE;
944 p_playlist = pl_Yield( p_intf );
946 p_input = p_playlist->p_input;
951 pl_Release( p_playlist );
955 vlc_object_yield( p_input );
957 pl_Release( p_playlist );
959 p_item = input_GetItem( p_input );
962 vlc_object_release( p_input );
966 if( input_item_IsPreparsed( p_item ) )
968 p_sys->b_meta_read = VLC_TRUE;
969 TrackChangeSignal( p_sys->p_conn, p_item );
972 var_AddCallback( p_input, "state", StateChange, p_intf );
974 vlc_object_release( p_input );
978 /*****************************************************************************
979 * UpdateCaps: update p_sys->i_caps
980 ****************************************************************************/
981 static int UpdateCaps( intf_thread_t* p_intf )
983 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
984 playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
987 if( p_playlist->items.i_size > 0 )
988 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
990 if( p_playlist->p_input )
992 if( var_GetBoolean( p_playlist->p_input, "can-pause" ) )
993 i_caps |= CAPS_CAN_PAUSE;
995 demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
996 VLC_OBJECT_DEMUX, FIND_CHILD );
999 vlc_bool_t b_can_seek;
1000 if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
1002 i_caps |= CAPS_CAN_SEEK;
1003 vlc_object_release( p_demux );
1008 pl_Release( p_playlist );
1010 if( p_intf->p_sys->b_meta_read )
1011 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1013 if( i_caps != p_intf->p_sys->i_caps )
1015 p_intf->p_sys->i_caps = i_caps;
1016 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1022 /*****************************************************************************
1023 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1024 *****************************************************************************/
1026 #define ADD_META( entry, type, 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, \
1037 dbus_message_iter_close_container( &dict_entry, &variant ); \
1038 dbus_message_iter_close_container( &dict, &dict_entry ); }
1040 #define ADD_VLC_META_STRING( entry, item ) \
1042 char * psz = input_item_Get##item( p_input );\
1043 ADD_META( entry, DBUS_TYPE_STRING, \
1048 static int GetInputMeta( input_item_t* p_input,
1049 DBusMessageIter *args )
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 );
1056 const char* ppsz_meta_items[] =
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"
1065 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
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 );
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 );
1090 ADD_VLC_META_STRING( 18, URI );
1091 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1093 dbus_message_iter_close_container( args, &dict );
1098 #undef ADD_VLC_META_STRING
1100 /*****************************************************************************
1101 * MarshalStatus: Fill a DBusMessage with the current player status
1102 *****************************************************************************/
1104 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
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 */
1110 DBusMessageIter status;
1111 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1113 playlist_t* p_playlist = NULL;
1114 input_thread_t* p_input = NULL;
1116 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1122 p_input = p_playlist->p_input;
1125 var_Get( p_input, "state", &val );
1126 if( val.i_int >= END_S )
1128 else if( val.i_int == PAUSE_S )
1130 else if( val.i_int <= PLAYING_S )
1134 var_Get( p_playlist, "random", &val );
1135 i_random = val.i_int;
1137 var_Get( p_playlist, "repeat", &val );
1138 i_repeat = val.i_int;
1140 var_Get( p_playlist, "loop", &val );
1145 pl_Release( p_playlist );
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 );