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>
50 #include <vlc_common.h>
51 #include <vlc_plugin.h>
53 #include <vlc_interface.h>
55 #include <vlc_input.h>
56 #include <vlc_playlist.h>
62 /*****************************************************************************
64 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
68 static void Run ( intf_thread_t * );
70 static int StateChange( vlc_object_t *, const char *, vlc_value_t,
71 vlc_value_t, void * );
73 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
74 vlc_value_t, void * );
76 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
77 vlc_value_t, void * );
79 static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
80 vlc_value_t, void * );
82 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
83 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, bool );
84 static int UpdateCaps( intf_thread_t*, bool );
86 /* GetCaps() capabilities */
90 CAPS_CAN_GO_NEXT = 1 << 0,
91 CAPS_CAN_GO_PREV = 1 << 1,
92 CAPS_CAN_PAUSE = 1 << 2,
93 CAPS_CAN_PLAY = 1 << 3,
94 CAPS_CAN_SEEK = 1 << 4,
95 CAPS_CAN_PROVIDE_METADATA = 1 << 5,
96 CAPS_CAN_HAS_TRACKLIST = 1 << 6
101 DBusConnection *p_conn;
106 /*****************************************************************************
108 *****************************************************************************/
111 set_shortname( N_("dbus"));
112 set_category( CAT_INTERFACE );
113 set_subcategory( SUBCAT_INTERFACE_CONTROL );
114 set_description( N_("D-Bus control interface") );
115 set_capability( "interface", 0 );
116 set_callbacks( Open, Close );
119 /*****************************************************************************
121 *****************************************************************************/
128 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
129 playlist_Stop( p_playlist );
130 pl_Release( ((vlc_object_t*) p_this) );
131 vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
135 DBUS_METHOD( MprisVersion )
136 { /*implemented version of the mpris spec */
139 VLC_UNUSED( p_this );
140 dbus_uint16_t i_major = VLC_MPRIS_VERSION_MAJOR;
141 dbus_uint16_t i_minor = VLC_MPRIS_VERSION_MINOR;
142 DBusMessageIter version;
144 if( !dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL,
146 return DBUS_HANDLER_RESULT_NEED_MEMORY;
148 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
150 return DBUS_HANDLER_RESULT_NEED_MEMORY;
152 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
154 return DBUS_HANDLER_RESULT_NEED_MEMORY;
156 if( !dbus_message_iter_close_container( &args, &version ) )
157 return DBUS_HANDLER_RESULT_NEED_MEMORY;
161 DBUS_METHOD( PositionGet )
162 { /* returns position in milliseconds */
165 vlc_value_t position;
168 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
170 input_thread_t *p_input = p_playlist->p_input;
176 var_Get( p_input, "time", &position );
177 i_pos = position.i_time / 1000;
180 pl_Release( ((vlc_object_t*) p_this) );
185 DBUS_METHOD( PositionSet )
186 { /* set position in milliseconds */
189 vlc_value_t position;
190 playlist_t* p_playlist = NULL;
194 dbus_error_init( &error );
196 dbus_message_get_args( p_from, &error,
197 DBUS_TYPE_INT32, &i_pos,
200 if( dbus_error_is_set( &error ) )
202 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
204 dbus_error_free( &error );
205 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
207 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
209 input_thread_t *p_input = p_playlist->p_input;
213 position.i_time = i_pos * 1000;
214 var_Set( p_input, "time", position );
217 pl_Release( ((vlc_object_t*) p_this) );
221 DBUS_METHOD( VolumeGet )
222 { /* returns volume in percentage */
225 dbus_int32_t i_dbus_vol;
226 audio_volume_t i_vol;
227 /* 2nd argument of aout_VolumeGet is int32 */
228 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
229 double f_vol = 100. * i_vol / AOUT_VOLUME_MAX;
230 i_dbus_vol = round( f_vol );
231 ADD_INT32( &i_dbus_vol );
235 DBUS_METHOD( VolumeSet )
236 { /* set volume in percentage */
240 dbus_error_init( &error );
242 dbus_int32_t i_dbus_vol;
243 audio_volume_t i_vol;
245 dbus_message_get_args( p_from, &error,
246 DBUS_TYPE_INT32, &i_dbus_vol,
249 if( dbus_error_is_set( &error ) )
251 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
253 dbus_error_free( &error );
254 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
257 double f_vol = AOUT_VOLUME_MAX * i_dbus_vol / 100.;
258 i_vol = round( f_vol );
259 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
265 { /* next playlist item */
267 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
268 playlist_Next( p_playlist );
269 pl_Release( ((vlc_object_t*) p_this) );
274 { /* previous playlist item */
276 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
277 playlist_Prev( p_playlist );
278 pl_Release( ((vlc_object_t*) p_this) );
285 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
286 playlist_Stop( p_playlist );
287 pl_Release( ((vlc_object_t*) p_this) );
291 DBUS_METHOD( GetStatus )
292 { /* returns the current status as a struct of 4 ints */
294 First 0 = Playing, 1 = Paused, 2 = Stopped.
295 Second 0 = Playing linearly , 1 = Playing randomly.
296 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
297 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
302 MarshalStatus( p_this, &args, true );
310 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
311 playlist_Pause( p_playlist );
312 pl_Release( (vlc_object_t*) p_this );
319 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
322 input_thread_t *p_input = p_playlist->p_input;
324 vlc_object_yield( p_input );
330 input_Control( p_input, INPUT_SET_POSITION, i_pos );
331 vlc_object_release( p_input );
334 playlist_Play( p_playlist );
336 pl_Release( (vlc_object_t*) p_this );
340 DBUS_METHOD( GetCurrentMetadata )
344 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
346 if( p_playlist->status.p_item )
347 GetInputMeta( p_playlist->status.p_item->p_input, &args );
349 pl_Release( (vlc_object_t*) p_this );
353 DBUS_METHOD( GetCaps )
358 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
363 /* Media Player information */
365 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 );
391 playlist_t* p_playlist = NULL;
396 dbus_message_get_args( p_from, &error,
397 DBUS_TYPE_STRING, &psz_mrl,
398 DBUS_TYPE_BOOLEAN, &b_play,
401 if( dbus_error_is_set( &error ) )
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 p_playlist = pl_Yield( (vlc_object_t*) p_this );
410 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
411 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
412 PLAYLIST_END, true, false );
413 pl_Release( (vlc_object_t*) p_this );
415 dbus_int32_t i_success = 0;
416 ADD_INT32( &i_success );
421 DBUS_METHOD( GetCurrentTrack )
426 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
427 dbus_int32_t i_position = p_playlist->i_current_index;
428 pl_Release( (vlc_object_t*) p_this );
430 ADD_INT32( &i_position );
434 DBUS_METHOD( GetMetadata )
439 dbus_error_init( &error );
441 dbus_int32_t i_position;
443 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
446 dbus_message_get_args( p_from, &error,
447 DBUS_TYPE_INT32, &i_position,
450 if( dbus_error_is_set( &error ) )
453 pl_Release( (vlc_object_t*) p_this );
454 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
456 dbus_error_free( &error );
457 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
460 if( i_position < p_playlist->current.i_size )
462 GetInputMeta( p_playlist->current.p_elems[i_position]->p_input, &args );
466 pl_Release( (vlc_object_t*) p_this );
470 DBUS_METHOD( GetLength )
475 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
476 dbus_int32_t i_elements = p_playlist->current.i_size;
477 pl_Release( (vlc_object_t*) p_this );
479 ADD_INT32( &i_elements );
483 DBUS_METHOD( DelTrack )
488 dbus_error_init( &error );
490 dbus_int32_t i_position;
491 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
493 dbus_message_get_args( p_from, &error,
494 DBUS_TYPE_INT32, &i_position,
497 if( dbus_error_is_set( &error ) )
499 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
501 dbus_error_free( &error );
502 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
506 if( i_position < p_playlist->current.i_size )
508 playlist_DeleteFromInput( p_playlist,
509 p_playlist->current.p_elems[i_position]->p_input->i_id,
514 pl_Release( (vlc_object_t*) p_this );
519 DBUS_METHOD( SetLoop )
527 playlist_t* p_playlist = NULL;
529 dbus_error_init( &error );
530 dbus_message_get_args( p_from, &error,
531 DBUS_TYPE_BOOLEAN, &b_loop,
534 if( dbus_error_is_set( &error ) )
536 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
538 dbus_error_free( &error );
539 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
542 val.b_bool = ( b_loop == TRUE ) ? true : false ;
543 p_playlist = pl_Yield( (vlc_object_t*) p_this );
544 var_Set ( p_playlist, "loop", val );
545 pl_Release( ((vlc_object_t*) p_this) );
550 DBUS_METHOD( Repeat )
556 dbus_bool_t b_repeat;
558 playlist_t* p_playlist = NULL;
560 dbus_error_init( &error );
561 dbus_message_get_args( p_from, &error,
562 DBUS_TYPE_BOOLEAN, &b_repeat,
565 if( dbus_error_is_set( &error ) )
567 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
569 dbus_error_free( &error );
570 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
573 val.b_bool = ( b_repeat == TRUE ) ? true : false ;
575 p_playlist = pl_Yield( (vlc_object_t*) p_this );
576 var_Set ( p_playlist, "repeat", val );
577 pl_Release( ((vlc_object_t*) p_this) );
582 DBUS_METHOD( SetRandom )
588 dbus_bool_t b_random;
590 playlist_t* p_playlist = NULL;
592 dbus_error_init( &error );
593 dbus_message_get_args( p_from, &error,
594 DBUS_TYPE_BOOLEAN, &b_random,
597 if( dbus_error_is_set( &error ) )
599 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
601 dbus_error_free( &error );
602 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
605 val.b_bool = ( b_random == TRUE ) ? true : false ;
607 p_playlist = pl_Yield( (vlc_object_t*) p_this );
608 var_Set ( p_playlist, "random", val );
609 pl_Release( ((vlc_object_t*) p_this) );
613 /*****************************************************************************
614 * Introspection method
615 *****************************************************************************/
617 DBUS_METHOD( handle_introspect_root )
618 { /* handles introspection of root object */
622 ADD_STRING( &psz_introspection_xml_data_root );
626 DBUS_METHOD( handle_introspect_player )
631 ADD_STRING( &psz_introspection_xml_data_player );
635 DBUS_METHOD( handle_introspect_tracklist )
640 ADD_STRING( &psz_introspection_xml_data_tracklist );
644 /*****************************************************************************
645 * handle_*: answer to incoming messages
646 *****************************************************************************/
648 #define METHOD_FUNC( method, function ) \
649 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
650 return function( p_conn, p_from, p_this )
652 DBUS_METHOD( handle_root )
655 if( dbus_message_is_method_call( p_from,
656 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
657 return handle_introspect_root( p_conn, p_from, p_this );
659 /* here D-Bus method's names are associated to an handler */
661 METHOD_FUNC( "Identity", Identity );
662 METHOD_FUNC( "MprisVersion", MprisVersion );
663 METHOD_FUNC( "Quit", Quit );
665 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
669 DBUS_METHOD( handle_player )
671 if( dbus_message_is_method_call( p_from,
672 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
673 return handle_introspect_player( p_conn, p_from, p_this );
675 /* here D-Bus method's names are associated to an handler */
677 METHOD_FUNC( "Prev", Prev );
678 METHOD_FUNC( "Next", Next );
679 METHOD_FUNC( "Stop", Stop );
680 METHOD_FUNC( "Play", Play );
681 METHOD_FUNC( "Pause", Pause );
682 METHOD_FUNC( "Repeat", Repeat );
683 METHOD_FUNC( "VolumeSet", VolumeSet );
684 METHOD_FUNC( "VolumeGet", VolumeGet );
685 METHOD_FUNC( "PositionSet", PositionSet );
686 METHOD_FUNC( "PositionGet", PositionGet );
687 METHOD_FUNC( "GetStatus", GetStatus );
688 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
689 METHOD_FUNC( "GetCaps", GetCaps );
691 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
694 DBUS_METHOD( handle_tracklist )
696 if( dbus_message_is_method_call( p_from,
697 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
698 return handle_introspect_tracklist( p_conn, p_from, p_this );
700 /* here D-Bus method's names are associated to an handler */
702 METHOD_FUNC( "GetMetadata", GetMetadata );
703 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
704 METHOD_FUNC( "GetLength", GetLength );
705 METHOD_FUNC( "AddTrack", AddTrack );
706 METHOD_FUNC( "DelTrack", DelTrack );
707 METHOD_FUNC( "SetLoop", SetLoop );
708 METHOD_FUNC( "SetRandom", SetRandom );
710 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
713 /*****************************************************************************
714 * Open: initialize interface
715 *****************************************************************************/
717 static int Open( vlc_object_t *p_this )
718 { /* initialisation of the connection */
719 intf_thread_t *p_intf = (intf_thread_t*)p_this;
720 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
721 playlist_t *p_playlist;
722 DBusConnection *p_conn;
728 p_sys->b_meta_read = false;
729 p_sys->i_caps = CAPS_NONE;
731 dbus_error_init( &error );
733 /* connect to the session bus */
734 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
737 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
739 dbus_error_free( &error );
744 /* register a well-known name on the bus */
745 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
746 if( dbus_error_is_set( &error ) )
748 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
749 ": %s", error.message );
750 dbus_error_free( &error );
755 /* we register the objects */
756 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
757 &vlc_dbus_root_vtable, p_this );
758 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
759 &vlc_dbus_player_vtable, p_this );
760 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
761 &vlc_dbus_tracklist_vtable, p_this );
763 dbus_connection_flush( p_conn );
765 p_playlist = pl_Yield( p_intf );
767 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
768 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
769 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
770 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
771 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
772 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
773 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
775 pl_Release( p_intf );
777 p_intf->pf_run = Run;
778 p_intf->p_sys = p_sys;
779 p_sys->p_conn = p_conn;
781 UpdateCaps( p_intf, false );
786 /*****************************************************************************
787 * Close: destroy interface
788 *****************************************************************************/
790 static void Close ( vlc_object_t *p_this )
792 intf_thread_t *p_intf = (intf_thread_t*) p_this;
793 playlist_t *p_playlist = pl_Yield( p_intf );;
794 input_thread_t *p_input;
796 p_this->b_dead = true;
799 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
800 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
801 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
802 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
803 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
804 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
805 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
807 p_input = p_playlist->p_input;
810 vlc_object_yield( p_input );
811 var_DelCallback( p_input, "state", StateChange, p_intf );
812 vlc_object_release( p_input );
816 pl_Release( p_intf );
818 dbus_connection_unref( p_intf->p_sys->p_conn );
820 free( p_intf->p_sys );
823 /*****************************************************************************
825 *****************************************************************************/
827 static void Run ( intf_thread_t *p_intf )
829 while( !intf_ShouldDie( p_intf ) )
831 msleep( INTF_IDLE_SLEEP );
832 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
836 /******************************************************************************
837 * CapsChange: player capabilities change signal
838 *****************************************************************************/
839 DBUS_SIGNAL( CapsChangeSignal )
841 SIGNAL_INIT( "CapsChange" );
844 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
848 /******************************************************************************
849 * TrackListChange: tracklist order / length change signal
850 *****************************************************************************/
851 DBUS_SIGNAL( TrackListChangeSignal )
852 { /* emit the new tracklist lengh */
853 SIGNAL_INIT("TrackListChange");
856 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
857 dbus_int32_t i_elements = p_playlist->current.i_size;
858 pl_Release( (vlc_object_t*) p_data );
860 ADD_INT32( &i_elements );
864 /*****************************************************************************
865 * TrackListChangeEmit: Emits the TrackListChange signal
866 *****************************************************************************/
867 /* FIXME: It is not called on tracklist reordering */
868 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
869 vlc_value_t oldval, vlc_value_t newval, void *p_data )
872 intf_thread_t *p_intf = p_data;
874 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
876 /* don't signal when items are added/removed in p_category */
877 playlist_t *p_playlist = (playlist_t*)p_this;
878 playlist_add_t *p_add = newval.p_address;
879 playlist_item_t *p_item;
880 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, pl_Locked );
882 while( p_item->p_parent )
883 p_item = p_item->p_parent;
884 if( p_item == p_playlist->p_root_category )
891 UpdateCaps( p_intf, true );
892 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
895 /*****************************************************************************
896 * TrackChange: Playlist item change callback
897 *****************************************************************************/
899 DBUS_SIGNAL( TrackChangeSignal )
900 { /* emit the metadata of the new item */
901 SIGNAL_INIT( "TrackChange" );
904 input_item_t *p_item = (input_item_t*) p_data;
905 GetInputMeta ( p_item, &args );
910 /*****************************************************************************
911 * StatusChange: Player status change signal
912 *****************************************************************************/
914 DBUS_SIGNAL( StatusChangeSignal )
915 { /* send the updated status info on the bus */
916 SIGNAL_INIT( "StatusChange" );
919 /* we're called from a callback of input_thread_t, so it can not be
920 * destroyed before we return */
921 MarshalStatus( (intf_thread_t*) p_data, &args, false );
926 /*****************************************************************************
927 * StateChange: callback on input "state"
928 *****************************************************************************/
929 static int StateChange( vlc_object_t *p_this, const char* psz_var,
930 vlc_value_t oldval, vlc_value_t newval, void *p_data )
932 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
933 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
934 intf_sys_t *p_sys = p_intf->p_sys;
939 UpdateCaps( p_intf, true );
941 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
943 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
946 p_sys->b_meta_read = true;
947 TrackChangeSignal( p_sys->p_conn, p_item );
951 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
952 newval.i_int == STOP_S || newval.i_int == END_S )
954 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
960 /*****************************************************************************
961 * StatusChangeEmit: Emits the StatusChange signal
962 *****************************************************************************/
963 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
964 vlc_value_t oldval, vlc_value_t newval, void *p_data )
966 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
967 VLC_UNUSED(oldval); VLC_UNUSED(newval);
968 intf_thread_t *p_intf = p_data;
973 UpdateCaps( p_intf, false );
974 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
978 /*****************************************************************************
979 * TrackChange: callback on playlist "playlist-current"
980 *****************************************************************************/
981 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
982 vlc_value_t oldval, vlc_value_t newval, void *p_data )
984 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
985 intf_sys_t *p_sys = p_intf->p_sys;
986 playlist_t *p_playlist;
987 input_thread_t *p_input = NULL;
988 input_item_t *p_item = NULL;
989 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
990 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
995 p_sys->b_meta_read = false;
997 p_playlist = pl_Yield( p_intf );
998 p_input = p_playlist->p_input;
1003 pl_Release( p_intf );
1007 vlc_object_yield( p_input );
1008 pl_Release( p_intf );
1010 p_item = input_GetItem( p_input );
1013 vlc_object_release( p_input );
1014 return VLC_EGENERIC;
1017 if( input_item_IsPreparsed( p_item ) )
1019 p_sys->b_meta_read = true;
1020 TrackChangeSignal( p_sys->p_conn, p_item );
1023 var_AddCallback( p_input, "state", StateChange, p_intf );
1025 vlc_object_release( p_input );
1029 /*****************************************************************************
1030 * UpdateCaps: update p_sys->i_caps
1031 ****************************************************************************/
1032 static int UpdateCaps( intf_thread_t* p_intf, bool b_playlist_locked )
1034 intf_sys_t* p_sys = p_intf->p_sys;
1035 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1036 playlist_t* p_playlist = pl_Yield( p_intf );
1037 if( !b_playlist_locked ) PL_LOCK;
1039 if( p_playlist->current.i_size > 0 )
1040 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1042 if( p_playlist->p_input )
1044 /* XXX: if UpdateCaps() is called too early, these are
1045 * unconditionnaly true */
1046 if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1047 i_caps |= CAPS_CAN_PAUSE;
1048 if( var_GetBool( p_playlist->p_input, "seekable" ) )
1049 i_caps |= CAPS_CAN_SEEK;
1052 if( !b_playlist_locked ) PL_UNLOCK;
1053 pl_Release( p_intf );
1055 if( p_sys->b_meta_read )
1056 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1058 if( i_caps != p_intf->p_sys->i_caps )
1060 p_sys->i_caps = i_caps;
1061 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1067 /*****************************************************************************
1068 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1069 *****************************************************************************/
1071 #define ADD_META( entry, type, data ) \
1073 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1074 NULL, &dict_entry ); \
1075 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1076 &ppsz_meta_items[entry] ); \
1077 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1078 type##_AS_STRING, &variant ); \
1079 dbus_message_iter_append_basic( &variant, \
1082 dbus_message_iter_close_container( &dict_entry, &variant ); \
1083 dbus_message_iter_close_container( &dict, &dict_entry ); }
1085 #define ADD_VLC_META_STRING( entry, item ) \
1087 char * psz = input_item_Get##item( p_input );\
1088 ADD_META( entry, DBUS_TYPE_STRING, \
1093 static int GetInputMeta( input_item_t* p_input,
1094 DBusMessageIter *args )
1096 DBusMessageIter dict, dict_entry, variant;
1097 /* We need the track length to be expressed in milli-seconds
1098 * instead of µ-seconds */
1099 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1101 const char* ppsz_meta_items[] =
1103 "title", "artist", "genre", "copyright", "album", "tracknum",
1104 "description", "rating", "date", "setting", "url", "language",
1105 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1106 "status", "location", "length", "video-codec", "audio-codec",
1107 "video-bitrate", "audio-bitrate", "audio-samplerate"
1110 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1112 ADD_VLC_META_STRING( 0, Title );
1113 ADD_VLC_META_STRING( 1, Artist );
1114 ADD_VLC_META_STRING( 2, Genre );
1115 ADD_VLC_META_STRING( 3, Copyright );
1116 ADD_VLC_META_STRING( 4, Album );
1117 ADD_VLC_META_STRING( 5, TrackNum );
1118 ADD_VLC_META_STRING( 6, Description );
1119 ADD_VLC_META_STRING( 7, Rating );
1120 ADD_VLC_META_STRING( 8, Date );
1121 ADD_VLC_META_STRING( 9, Setting );
1122 ADD_VLC_META_STRING( 10, URL );
1123 ADD_VLC_META_STRING( 11, Language );
1124 ADD_VLC_META_STRING( 12, NowPlaying );
1125 ADD_VLC_META_STRING( 13, Publisher );
1126 ADD_VLC_META_STRING( 14, EncodedBy );
1127 ADD_VLC_META_STRING( 15, ArtURL );
1128 ADD_VLC_META_STRING( 16, TrackID );
1130 vlc_mutex_lock( &p_input->lock );
1131 if( p_input->p_meta )
1132 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1133 vlc_mutex_unlock( &p_input->lock );
1135 ADD_VLC_META_STRING( 18, URI );
1136 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1138 dbus_message_iter_close_container( args, &dict );
1143 #undef ADD_VLC_META_STRING
1145 /*****************************************************************************
1146 * MarshalStatus: Fill a DBusMessage with the current player status
1147 *****************************************************************************/
1149 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1151 { /* This is NOT the right way to do that, it would be better to sore
1152 the status information in p_sys and update it on change, thus
1153 avoiding a long lock */
1155 DBusMessageIter status;
1156 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1158 playlist_t* p_playlist = NULL;
1159 input_thread_t* p_input = NULL;
1161 p_playlist = pl_Yield( p_intf );
1167 p_input = p_playlist->p_input;
1170 var_Get( p_input, "state", &val );
1171 if( val.i_int >= END_S )
1173 else if( val.i_int == PAUSE_S )
1175 else if( val.i_int <= PLAYING_S )
1179 i_random = var_CreateGetBool( p_playlist, "random" );
1181 i_repeat = var_CreateGetBool( p_playlist, "repeat" );
1183 i_loop = var_CreateGetBool( p_playlist, "loop" );
1187 pl_Release( p_intf );
1189 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1190 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1191 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1192 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1193 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1194 dbus_message_iter_close_container( args, &status );