1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright © 2006-2008 Rafaël Carré
5 * Copyright © 2007-2008 Mirsal Ennaime
6 * Copyright © 2009 The VideoLAN team
9 * Authors: Rafaël Carré <funman at videolanorg>
10 * Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * D-Bus Specification:
29 * http://dbus.freedesktop.org/doc/dbus-specification.html
30 * D-Bus low-level C API (libdbus)
31 * http://dbus.freedesktop.org/doc/dbus/api/html/index.html
33 * "If you use this low-level API directly, you're signing up for some pain."
35 * MPRIS Specification version 1.0
36 * http://wiki.xmms2.xmms.se/index.php/MPRIS
39 /*****************************************************************************
41 *****************************************************************************/
47 #include <dbus/dbus.h>
50 #include <vlc_common.h>
51 #include <vlc_plugin.h>
53 #include <vlc_interface.h>
54 #include <vlc_playlist.h>
61 /*****************************************************************************
63 *****************************************************************************/
65 static int Open ( vlc_object_t * );
66 static void Close ( vlc_object_t * );
67 static void Run ( intf_thread_t * );
69 static int StateChange( intf_thread_t *, int );
70 static int TrackChange( intf_thread_t * );
71 static int StatusChangeEmit( intf_thread_t *);
72 static int TrackListChangeEmit( intf_thread_t *, int, int );
74 static int AllCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
76 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
77 static int MarshalStatus ( intf_thread_t *, DBusMessageIter * );
78 static int UpdateCaps( intf_thread_t* );
80 /* GetCaps() capabilities */
84 CAPS_CAN_GO_NEXT = 1 << 0,
85 CAPS_CAN_GO_PREV = 1 << 1,
86 CAPS_CAN_PAUSE = 1 << 2,
87 CAPS_CAN_PLAY = 1 << 3,
88 CAPS_CAN_SEEK = 1 << 4,
89 CAPS_CAN_PROVIDE_METADATA = 1 << 5,
90 CAPS_CAN_HAS_TRACKLIST = 1 << 6
93 // The signal that can be get from the callbacks
98 SIGNAL_PLAYLIST_ITEM_APPEND,
99 SIGNAL_PLAYLIST_ITEM_DELETED,
108 DBusConnection *p_conn;
109 playlist_t *p_playlist;
113 vlc_array_t *p_events;
115 input_thread_t *p_input;
125 #define INTF ((intf_thread_t *)p_this)
126 #define PL (INTF->p_sys->p_playlist)
129 /*****************************************************************************
131 *****************************************************************************/
134 set_shortname( N_("dbus"))
135 set_category( CAT_INTERFACE )
136 set_subcategory( SUBCAT_INTERFACE_CONTROL )
137 set_description( N_("D-Bus control interface") )
138 set_capability( "interface", 0 )
139 set_callbacks( Open, Close )
142 /*****************************************************************************
144 *****************************************************************************/
151 libvlc_Quit(INTF->p_libvlc);
155 DBUS_METHOD( MprisVersion )
156 { /*implemented version of the mpris spec */
159 VLC_UNUSED( p_this );
160 dbus_uint16_t i_major = VLC_MPRIS_VERSION_MAJOR;
161 dbus_uint16_t i_minor = VLC_MPRIS_VERSION_MINOR;
162 DBusMessageIter version;
164 if( !dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL,
166 return DBUS_HANDLER_RESULT_NEED_MEMORY;
168 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
170 return DBUS_HANDLER_RESULT_NEED_MEMORY;
172 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
174 return DBUS_HANDLER_RESULT_NEED_MEMORY;
176 if( !dbus_message_iter_close_container( &args, &version ) )
177 return DBUS_HANDLER_RESULT_NEED_MEMORY;
181 DBUS_METHOD( PositionGet )
182 { /* returns position in milliseconds */
187 input_thread_t *p_input = playlist_CurrentInput( PL );
193 i_pos = var_GetTime( p_input, "time" ) / 1000;
194 vlc_object_release( p_input );
200 DBUS_METHOD( PositionSet )
201 { /* set position in milliseconds */
204 vlc_value_t position;
208 dbus_error_init( &error );
210 dbus_message_get_args( p_from, &error,
211 DBUS_TYPE_INT32, &i_pos,
214 if( dbus_error_is_set( &error ) )
216 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
218 dbus_error_free( &error );
219 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
221 input_thread_t *p_input = playlist_CurrentInput( PL );
225 position.i_time = ((mtime_t)i_pos) * 1000;
226 var_Set( p_input, "time", position );
227 vlc_object_release( p_input );
232 DBUS_METHOD( VolumeGet )
233 { /* returns volume in percentage */
236 dbus_int32_t i_dbus_vol;
237 audio_volume_t i_vol;
239 /* 2nd argument of aout_VolumeGet is int32 */
240 aout_VolumeGet( PL, &i_vol );
242 double f_vol = 100. * i_vol / AOUT_VOLUME_MAX;
243 i_dbus_vol = round( f_vol );
244 ADD_INT32( &i_dbus_vol );
248 DBUS_METHOD( VolumeSet )
249 { /* set volume in percentage */
253 dbus_error_init( &error );
255 dbus_int32_t i_dbus_vol;
256 audio_volume_t i_vol;
258 dbus_message_get_args( p_from, &error,
259 DBUS_TYPE_INT32, &i_dbus_vol,
262 if( dbus_error_is_set( &error ) )
264 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
266 dbus_error_free( &error );
267 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
270 double f_vol = AOUT_VOLUME_MAX * i_dbus_vol / 100.;
271 i_vol = round( f_vol );
272 aout_VolumeSet( PL, i_vol );
277 { /* next playlist item */
284 { /* previous playlist item */
297 DBUS_METHOD( GetStatus )
298 { /* returns the current status as a struct of 4 ints */
300 First 0 = Playing, 1 = Paused, 2 = Stopped.
301 Second 0 = Playing linearly , 1 = Playing randomly.
302 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
303 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
308 MarshalStatus( p_this, &args );
316 playlist_Pause( PL );
324 input_thread_t *p_input = playlist_CurrentInput( PL );
329 input_Control( p_input, INPUT_SET_POSITION, i_pos );
330 vlc_object_release( p_input );
338 DBUS_METHOD( GetCurrentMetadata )
342 playlist_t *p_playlist = PL;
345 playlist_item_t* p_item = playlist_CurrentPlayingItem( p_playlist );
347 GetInputMeta( p_item->p_input, &args );
352 DBUS_METHOD( GetCaps )
357 ADD_INT32( &INTF->p_sys->i_caps );
362 /* Media Player information */
364 DBUS_METHOD( Identity )
371 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
373 ADD_STRING( &psz_identity );
374 free( psz_identity );
377 return DBUS_HANDLER_RESULT_NEED_MEMORY;
384 DBUS_METHOD( AddTrack )
385 { /* add the string to the playlist, and play it if the boolean is true */
390 dbus_error_init( &error );
395 dbus_message_get_args( p_from, &error,
396 DBUS_TYPE_STRING, &psz_mrl,
397 DBUS_TYPE_BOOLEAN, &b_play,
400 if( dbus_error_is_set( &error ) )
402 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
404 dbus_error_free( &error );
405 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
408 playlist_Add( PL, psz_mrl, NULL, PLAYLIST_APPEND |
409 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
410 PLAYLIST_END, true, false );
412 dbus_int32_t i_success = 0;
413 ADD_INT32( &i_success );
418 DBUS_METHOD( GetCurrentTrack )
423 playlist_t *p_playlist = PL;
426 dbus_int32_t i_position = PL->i_current_index;
429 ADD_INT32( &i_position );
433 DBUS_METHOD( GetMetadata )
438 dbus_error_init( &error );
440 dbus_int32_t i_position;
441 playlist_t *p_playlist = PL;
443 dbus_message_get_args( p_from, &error,
444 DBUS_TYPE_INT32, &i_position,
447 if( dbus_error_is_set( &error ) )
449 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
451 dbus_error_free( &error );
452 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
456 if( i_position < p_playlist->current.i_size )
458 GetInputMeta( p_playlist->current.p_elems[i_position]->p_input, &args );
465 DBUS_METHOD( GetLength )
469 playlist_t *p_playlist = PL;
472 dbus_int32_t i_elements = PL->current.i_size;
475 ADD_INT32( &i_elements );
479 DBUS_METHOD( DelTrack )
484 dbus_error_init( &error );
486 dbus_int32_t i_position;
487 playlist_t *p_playlist = PL;
489 dbus_message_get_args( p_from, &error,
490 DBUS_TYPE_INT32, &i_position,
493 if( dbus_error_is_set( &error ) )
495 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
497 dbus_error_free( &error );
498 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
502 if( i_position < p_playlist->current.i_size )
504 playlist_DeleteFromInput( p_playlist,
505 p_playlist->current.p_elems[i_position]->p_input,
513 DBUS_METHOD( SetLoop )
521 dbus_error_init( &error );
522 dbus_message_get_args( p_from, &error,
523 DBUS_TYPE_BOOLEAN, &b_loop,
526 if( dbus_error_is_set( &error ) )
528 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
530 dbus_error_free( &error );
531 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
534 var_SetBool( PL, "loop", ( b_loop == TRUE ) );
539 DBUS_METHOD( Repeat )
545 dbus_bool_t b_repeat;
547 dbus_error_init( &error );
548 dbus_message_get_args( p_from, &error,
549 DBUS_TYPE_BOOLEAN, &b_repeat,
552 if( dbus_error_is_set( &error ) )
554 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
556 dbus_error_free( &error );
557 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
560 var_SetBool( PL, "repeat", ( b_repeat == TRUE ) );
565 DBUS_METHOD( SetRandom )
571 dbus_bool_t b_random;
573 dbus_error_init( &error );
574 dbus_message_get_args( p_from, &error,
575 DBUS_TYPE_BOOLEAN, &b_random,
578 if( dbus_error_is_set( &error ) )
580 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
582 dbus_error_free( &error );
583 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
586 var_SetBool( PL, "random", ( b_random == TRUE ) );
590 /*****************************************************************************
591 * Introspection method
592 *****************************************************************************/
594 DBUS_METHOD( handle_introspect_root )
595 { /* handles introspection of root object */
599 ADD_STRING( &psz_introspection_xml_data_root );
603 DBUS_METHOD( handle_introspect_player )
608 ADD_STRING( &psz_introspection_xml_data_player );
612 DBUS_METHOD( handle_introspect_tracklist )
617 ADD_STRING( &psz_introspection_xml_data_tracklist );
621 /*****************************************************************************
622 * handle_*: answer to incoming messages
623 *****************************************************************************/
625 #define METHOD_FUNC( method, function ) \
626 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
627 return function( p_conn, p_from, p_this )
629 DBUS_METHOD( handle_root )
632 if( dbus_message_is_method_call( p_from,
633 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
634 return handle_introspect_root( p_conn, p_from, p_this );
636 /* here D-Bus method's names are associated to an handler */
638 METHOD_FUNC( "Identity", Identity );
639 METHOD_FUNC( "MprisVersion", MprisVersion );
640 METHOD_FUNC( "Quit", Quit );
642 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
646 DBUS_METHOD( handle_player )
648 if( dbus_message_is_method_call( p_from,
649 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
650 return handle_introspect_player( p_conn, p_from, p_this );
652 /* here D-Bus method's names are associated to an handler */
654 METHOD_FUNC( "Prev", Prev );
655 METHOD_FUNC( "Next", Next );
656 METHOD_FUNC( "Stop", Stop );
657 METHOD_FUNC( "Play", Play );
658 METHOD_FUNC( "Pause", Pause );
659 METHOD_FUNC( "Repeat", Repeat );
660 METHOD_FUNC( "VolumeSet", VolumeSet );
661 METHOD_FUNC( "VolumeGet", VolumeGet );
662 METHOD_FUNC( "PositionSet", PositionSet );
663 METHOD_FUNC( "PositionGet", PositionGet );
664 METHOD_FUNC( "GetStatus", GetStatus );
665 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
666 METHOD_FUNC( "GetCaps", GetCaps );
668 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
671 DBUS_METHOD( handle_tracklist )
673 if( dbus_message_is_method_call( p_from,
674 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
675 return handle_introspect_tracklist( p_conn, p_from, p_this );
677 /* here D-Bus method's names are associated to an handler */
679 METHOD_FUNC( "GetMetadata", GetMetadata );
680 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
681 METHOD_FUNC( "GetLength", GetLength );
682 METHOD_FUNC( "AddTrack", AddTrack );
683 METHOD_FUNC( "DelTrack", DelTrack );
684 METHOD_FUNC( "SetLoop", SetLoop );
685 METHOD_FUNC( "SetRandom", SetRandom );
687 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
690 /*****************************************************************************
691 * Open: initialize interface
692 *****************************************************************************/
694 static int Open( vlc_object_t *p_this )
695 { /* initialisation of the connection */
696 intf_thread_t *p_intf = (intf_thread_t*)p_this;
697 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
698 playlist_t *p_playlist;
699 DBusConnection *p_conn;
705 p_sys->b_meta_read = false;
706 p_sys->i_caps = CAPS_NONE;
707 p_sys->b_dead = false;
708 p_sys->p_input = NULL;
710 dbus_error_init( &error );
712 /* connect to the session bus */
713 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
716 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
718 dbus_error_free( &error );
723 /* register a well-known name on the bus */
724 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
725 if( dbus_error_is_set( &error ) )
727 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
728 ": %s", error.message );
729 dbus_error_free( &error );
734 /* we register the objects */
735 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
736 &vlc_dbus_root_vtable, p_this );
737 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
738 &vlc_dbus_player_vtable, p_this );
739 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
740 &vlc_dbus_tracklist_vtable, p_this );
742 dbus_connection_flush( p_conn );
744 p_intf->pf_run = Run;
745 p_intf->p_sys = p_sys;
746 p_sys->p_conn = p_conn;
747 p_sys->p_events = vlc_array_new();
748 vlc_mutex_init( &p_sys->lock );
750 p_playlist = pl_Get( p_intf );
751 p_sys->p_playlist = p_playlist;
753 var_AddCallback( p_playlist, "item-current", AllCallback, p_intf );
754 var_AddCallback( p_playlist, "intf-change", AllCallback, p_intf );
755 var_AddCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
756 var_AddCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
757 var_AddCallback( p_playlist, "random", AllCallback, p_intf );
758 var_AddCallback( p_playlist, "repeat", AllCallback, p_intf );
759 var_AddCallback( p_playlist, "loop", AllCallback, p_intf );
761 UpdateCaps( p_intf );
766 /*****************************************************************************
767 * Close: destroy interface
768 *****************************************************************************/
770 static void Close ( vlc_object_t *p_this )
772 intf_thread_t *p_intf = (intf_thread_t*) p_this;
773 intf_sys_t *p_sys = p_intf->p_sys;
774 playlist_t *p_playlist = p_sys->p_playlist;
776 var_DelCallback( p_playlist, "item-current", AllCallback, p_intf );
777 var_DelCallback( p_playlist, "intf-change", AllCallback, p_intf );
778 var_DelCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
779 var_DelCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
780 var_DelCallback( p_playlist, "random", AllCallback, p_intf );
781 var_DelCallback( p_playlist, "repeat", AllCallback, p_intf );
782 var_DelCallback( p_playlist, "loop", AllCallback, p_intf );
786 var_DelCallback( p_sys->p_input, "state", AllCallback, p_intf );
787 vlc_object_release( p_sys->p_input );
790 dbus_connection_unref( p_sys->p_conn );
792 // Free the events array
793 for( int i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
795 callback_info_t* info = vlc_array_item_at_index( p_sys->p_events, i );
798 vlc_mutex_destroy( &p_sys->lock );
799 vlc_array_destroy( p_sys->p_events );
803 /*****************************************************************************
805 *****************************************************************************/
807 static void Run ( intf_thread_t *p_intf )
811 if( dbus_connection_get_dispatch_status(p_intf->p_sys->p_conn)
812 == DBUS_DISPATCH_COMPLETE )
813 msleep( INTF_IDLE_SLEEP );
814 int canc = vlc_savecancel();
815 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
817 /* Get the list of events to process
819 * We can't keep the lock on p_intf->p_sys->p_events, else we risk a
821 * The signal functions could lock mutex X while p_events is locked;
822 * While some other function in vlc (playlist) might lock mutex X
823 * and then set a variable which would call AllCallback(), which itself
824 * needs to lock p_events to add a new event.
826 vlc_mutex_lock( &p_intf->p_sys->lock );
827 int i_events = vlc_array_count( p_intf->p_sys->p_events );
828 callback_info_t* info[i_events];
829 for( int i = i_events - 1; i >= 0; i-- )
831 info[i] = vlc_array_item_at_index( p_intf->p_sys->p_events, i );
832 vlc_array_remove( p_intf->p_sys->p_events, i );
834 vlc_mutex_unlock( &p_intf->p_sys->lock );
836 for( int i = 0; i < i_events; i++ )
838 switch( info[i]->signal )
840 case SIGNAL_ITEM_CURRENT:
841 TrackChange( p_intf );
843 case SIGNAL_INTF_CHANGE:
844 case SIGNAL_PLAYLIST_ITEM_APPEND:
845 case SIGNAL_PLAYLIST_ITEM_DELETED:
846 TrackListChangeEmit( p_intf, info[i]->signal, info[i]->i_node );
851 StatusChangeEmit( p_intf );
854 StateChange( p_intf, info[i]->i_input_state );
861 vlc_restorecancel( canc );
866 // Get all the callbacks
867 static int AllCallback( vlc_object_t *p_this, const char *psz_var,
868 vlc_value_t oldval, vlc_value_t newval, void *p_data )
872 intf_thread_t *p_intf = (intf_thread_t*)p_data;
874 callback_info_t *info = malloc( sizeof( callback_info_t ) );
878 // Wich event is it ?
879 if( !strcmp( "item-current", psz_var ) )
880 info->signal = SIGNAL_ITEM_CURRENT;
881 else if( !strcmp( "intf-change", psz_var ) )
882 info->signal = SIGNAL_INTF_CHANGE;
883 else if( !strcmp( "playlist-item-append", psz_var ) )
885 info->signal = SIGNAL_PLAYLIST_ITEM_APPEND;
886 info->i_node = ((playlist_add_t*)newval.p_address)->i_node;
888 else if( !strcmp( "playlist-item-deleted", psz_var ) )
889 info->signal = SIGNAL_PLAYLIST_ITEM_DELETED;
890 else if( !strcmp( "random", psz_var ) )
891 info->signal = SIGNAL_RANDOM;
892 else if( !strcmp( "repeat", psz_var ) )
893 info->signal = SIGNAL_REPEAT;
894 else if( !strcmp( "loop", psz_var ) )
895 info->signal = SIGNAL_LOOP;
896 else if( !strcmp( "state", psz_var ) )
898 info->signal = SIGNAL_STATE;
899 info->i_input_state = newval.i_int;
905 vlc_mutex_lock( &p_intf->p_sys->lock );
906 vlc_array_append( p_intf->p_sys->p_events, info );
907 vlc_mutex_unlock( &p_intf->p_sys->lock );
911 /******************************************************************************
912 * CapsChange: player capabilities change signal
913 *****************************************************************************/
914 DBUS_SIGNAL( CapsChangeSignal )
916 SIGNAL_INIT( MPRIS_DBUS_PLAYER_PATH, "CapsChange" );
919 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
923 /******************************************************************************
924 * TrackListChange: tracklist order / length change signal
925 *****************************************************************************/
926 DBUS_SIGNAL( TrackListChangeSignal )
927 { /* emit the new tracklist lengh */
928 SIGNAL_INIT( MPRIS_DBUS_TRACKLIST_PATH, "TrackListChange");
931 playlist_t *p_playlist = ((intf_thread_t*)p_data)->p_sys->p_playlist;
933 dbus_int32_t i_elements = p_playlist->current.i_size;
936 ADD_INT32( &i_elements );
940 /*****************************************************************************
941 * TrackListChangeEmit: Emits the TrackListChange signal
942 *****************************************************************************/
943 /* FIXME: It is not called on tracklist reordering */
944 static int TrackListChangeEmit( intf_thread_t *p_intf, int signal, int i_node )
946 // "playlist-item-append"
947 if( signal == SIGNAL_PLAYLIST_ITEM_APPEND )
949 /* don't signal when items are added/removed in p_category */
950 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
952 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_node );
954 while( p_item->p_parent )
955 p_item = p_item->p_parent;
956 if( p_item == p_playlist->p_root_category )
964 if( p_intf->p_sys->b_dead )
967 UpdateCaps( p_intf );
968 TrackListChangeSignal( p_intf->p_sys->p_conn, p_intf );
971 /*****************************************************************************
972 * TrackChange: Playlist item change callback
973 *****************************************************************************/
975 DBUS_SIGNAL( TrackChangeSignal )
976 { /* emit the metadata of the new item */
977 SIGNAL_INIT( MPRIS_DBUS_PLAYER_PATH, "TrackChange" );
980 input_item_t *p_item = (input_item_t*) p_data;
981 GetInputMeta ( p_item, &args );
986 /*****************************************************************************
987 * StatusChange: Player status change signal
988 *****************************************************************************/
990 DBUS_SIGNAL( StatusChangeSignal )
991 { /* send the updated status info on the bus */
992 SIGNAL_INIT( MPRIS_DBUS_PLAYER_PATH, "StatusChange" );
995 /* we're called from a callback of input_thread_t, so it can not be
996 * destroyed before we return */
997 MarshalStatus( (intf_thread_t*) p_data, &args );
1002 /*****************************************************************************
1003 * StateChange: callback on input "state"
1004 *****************************************************************************/
1005 //static int StateChange( vlc_object_t *p_this, const char* psz_var,
1006 // vlc_value_t oldval, vlc_value_t newval, void *p_data )
1007 static int StateChange( intf_thread_t *p_intf, int i_input_state )
1009 intf_sys_t *p_sys = p_intf->p_sys;
1010 playlist_t *p_playlist = p_sys->p_playlist;
1011 input_thread_t *p_input;
1012 input_item_t *p_item;
1014 if( p_intf->p_sys->b_dead )
1017 UpdateCaps( p_intf );
1019 if( !p_sys->b_meta_read && i_input_state == PLAYING_S )
1021 p_input = playlist_CurrentInput( p_playlist );
1024 p_item = input_GetItem( p_input );
1027 p_sys->b_meta_read = true;
1028 TrackChangeSignal( p_sys->p_conn, p_item );
1030 vlc_object_release( p_input );
1034 if( i_input_state == PLAYING_S || i_input_state == PAUSE_S ||
1035 i_input_state == END_S )
1037 StatusChangeSignal( p_sys->p_conn, p_intf );
1043 /*****************************************************************************
1044 * StatusChangeEmit: Emits the StatusChange signal
1045 *****************************************************************************/
1046 static int StatusChangeEmit( intf_thread_t * p_intf )
1048 if( p_intf->p_sys->b_dead )
1051 UpdateCaps( p_intf );
1052 StatusChangeSignal( p_intf->p_sys->p_conn, p_intf );
1056 /*****************************************************************************
1057 * TrackChange: callback on playlist "item-current"
1058 *****************************************************************************/
1059 static int TrackChange( intf_thread_t *p_intf )
1061 intf_sys_t *p_sys = p_intf->p_sys;
1062 playlist_t *p_playlist = p_sys->p_playlist;
1063 input_thread_t *p_input = NULL;
1064 input_item_t *p_item = NULL;
1066 if( p_intf->p_sys->b_dead )
1069 if( p_sys->p_input )
1071 var_DelCallback( p_sys->p_input, "state", AllCallback, p_intf );
1072 vlc_object_release( p_sys->p_input );
1073 p_sys->p_input = NULL;
1076 p_sys->b_meta_read = false;
1078 p_input = playlist_CurrentInput( p_playlist );
1084 p_item = input_GetItem( p_input );
1087 vlc_object_release( p_input );
1088 return VLC_EGENERIC;
1091 if( input_item_IsPreparsed( p_item ) )
1093 p_sys->b_meta_read = true;
1094 TrackChangeSignal( p_sys->p_conn, p_item );
1097 p_sys->p_input = p_input;
1098 var_AddCallback( p_input, "state", AllCallback, p_intf );
1103 /*****************************************************************************
1104 * UpdateCaps: update p_sys->i_caps
1105 * This function have to be called with the playlist unlocked
1106 ****************************************************************************/
1107 static int UpdateCaps( intf_thread_t* p_intf )
1109 intf_sys_t* p_sys = p_intf->p_sys;
1110 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1111 playlist_t* p_playlist = p_sys->p_playlist;
1114 if( p_playlist->current.i_size > 0 )
1115 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1118 input_thread_t* p_input = playlist_CurrentInput( p_playlist );
1121 /* XXX: if UpdateCaps() is called too early, these are
1122 * unconditionnaly true */
1123 if( var_GetBool( p_input, "can-pause" ) )
1124 i_caps |= CAPS_CAN_PAUSE;
1125 if( var_GetBool( p_input, "can-seek" ) )
1126 i_caps |= CAPS_CAN_SEEK;
1127 vlc_object_release( p_input );
1130 if( p_sys->b_meta_read )
1131 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1133 if( i_caps != p_intf->p_sys->i_caps )
1135 p_sys->i_caps = i_caps;
1136 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1142 /*****************************************************************************
1143 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1144 *****************************************************************************/
1146 #define ADD_META( entry, type, data ) \
1148 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1149 NULL, &dict_entry ); \
1150 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1151 &ppsz_meta_items[entry] ); \
1152 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1153 type##_AS_STRING, &variant ); \
1154 dbus_message_iter_append_basic( &variant, \
1157 dbus_message_iter_close_container( &dict_entry, &variant ); \
1158 dbus_message_iter_close_container( &dict, &dict_entry ); }
1160 #define ADD_VLC_META_STRING( entry, item ) \
1162 char * psz = input_item_Get##item( p_input );\
1163 ADD_META( entry, DBUS_TYPE_STRING, \
1168 static int GetInputMeta( input_item_t* p_input,
1169 DBusMessageIter *args )
1171 DBusMessageIter dict, dict_entry, variant;
1172 /** The duration of the track can be expressed in second, milli-seconds and
1174 dbus_int64_t i_mtime = input_item_GetDuration( p_input );
1175 dbus_uint32_t i_time = i_mtime / 1000000;
1176 dbus_int64_t i_length = i_mtime / 1000;
1178 const char* ppsz_meta_items[] =
1180 /* Official MPRIS metas */
1181 "location", "title", "artist", "album", "tracknumber", "time", "mtime",
1182 "genre", "rating", "date", "arturl",
1183 "audio-bitrate", "audio-samplerate", "video-bitrate",
1184 /* VLC specifics metas */
1185 "audio-codec", "copyright", "description", "encodedby", "language", "length",
1186 "nowplaying", "publisher", "setting", "status", "trackid", "url",
1190 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1192 ADD_VLC_META_STRING( 0, URI );
1193 ADD_VLC_META_STRING( 1, Title );
1194 ADD_VLC_META_STRING( 2, Artist );
1195 ADD_VLC_META_STRING( 3, Album );
1196 ADD_VLC_META_STRING( 4, TrackNum );
1197 ADD_META( 5, DBUS_TYPE_UINT32, i_time );
1198 ADD_META( 6, DBUS_TYPE_UINT32, i_mtime );
1199 ADD_VLC_META_STRING( 7, Genre );
1200 ADD_VLC_META_STRING( 8, Rating );
1201 ADD_VLC_META_STRING( 9, Date );
1202 ADD_VLC_META_STRING( 10, ArtURL );
1204 ADD_VLC_META_STRING( 15, Copyright );
1205 ADD_VLC_META_STRING( 16, Description );
1206 ADD_VLC_META_STRING( 17, EncodedBy );
1207 ADD_VLC_META_STRING( 18, Language );
1208 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1209 ADD_VLC_META_STRING( 20, NowPlaying );
1210 ADD_VLC_META_STRING( 21, Publisher );
1211 ADD_VLC_META_STRING( 22, Setting );
1212 ADD_VLC_META_STRING( 24, TrackID );
1213 ADD_VLC_META_STRING( 25, URL );
1215 vlc_mutex_lock( &p_input->lock );
1216 if( p_input->p_meta )
1218 int i_status = vlc_meta_GetStatus( p_input->p_meta );
1219 ADD_META( 23, DBUS_TYPE_INT32, i_status );
1221 vlc_mutex_unlock( &p_input->lock );
1223 dbus_message_iter_close_container( args, &dict );
1228 #undef ADD_VLC_META_STRING
1230 /*****************************************************************************
1231 * MarshalStatus: Fill a DBusMessage with the current player status
1232 *****************************************************************************/
1234 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args )
1235 { /* This is NOT the right way to do that, it would be better to sore
1236 the status information in p_sys and update it on change, thus
1237 avoiding a long lock */
1239 DBusMessageIter status;
1240 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1242 playlist_t* p_playlist = p_intf->p_sys->p_playlist;
1243 input_thread_t* p_input = NULL;
1247 p_input = playlist_CurrentInput( p_playlist );
1250 i_val = var_GetInteger( p_input, "state" );
1251 if( i_val >= END_S )
1253 else if( i_val == PAUSE_S )
1255 else if( i_val <= PLAYING_S )
1257 vlc_object_release( p_input );
1260 i_random = var_CreateGetBool( p_playlist, "random" );
1262 i_repeat = var_CreateGetBool( p_playlist, "repeat" );
1264 i_loop = var_CreateGetBool( p_playlist, "loop" );
1266 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1267 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1268 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1269 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1270 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1271 dbus_message_iter_close_container( args, &status );