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 *****************************************************************************/
46 #include <dbus/dbus.h>
49 #include <vlc_common.h>
50 #include <vlc_plugin.h>
52 #include <vlc_interface.h>
54 #include <vlc_input.h>
55 #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( vlc_object_t *, const char *, vlc_value_t,
70 vlc_value_t, void * );
72 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
73 vlc_value_t, void * );
75 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
76 vlc_value_t, void * );
78 static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
79 vlc_value_t, void * );
81 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
82 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, bool );
83 static int UpdateCaps( intf_thread_t*, bool );
85 /* GetCaps() capabilities */
89 CAPS_CAN_GO_NEXT = 1 << 0,
90 CAPS_CAN_GO_PREV = 1 << 1,
91 CAPS_CAN_PAUSE = 1 << 2,
92 CAPS_CAN_PLAY = 1 << 3,
93 CAPS_CAN_SEEK = 1 << 4,
94 CAPS_CAN_PROVIDE_METADATA = 1 << 5,
95 CAPS_CAN_HAS_TRACKLIST = 1 << 6
100 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 libvlc_Quit(((vlc_object_t*)p_this)->p_libvlc);
132 DBUS_METHOD( MprisVersion )
133 { /*implemented version of the mpris spec */
136 VLC_UNUSED( p_this );
137 dbus_uint16_t i_major = VLC_MPRIS_VERSION_MAJOR;
138 dbus_uint16_t i_minor = VLC_MPRIS_VERSION_MINOR;
139 DBusMessageIter version;
141 if( !dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL,
143 return DBUS_HANDLER_RESULT_NEED_MEMORY;
145 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
147 return DBUS_HANDLER_RESULT_NEED_MEMORY;
149 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
151 return DBUS_HANDLER_RESULT_NEED_MEMORY;
153 if( !dbus_message_iter_close_container( &args, &version ) )
154 return DBUS_HANDLER_RESULT_NEED_MEMORY;
158 DBUS_METHOD( PositionGet )
159 { /* returns position in milliseconds */
162 vlc_value_t position;
165 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
166 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
172 var_Get( p_input, "time", &position );
173 i_pos = position.i_time / 1000;
174 vlc_object_release( p_input );
176 pl_Release( ((vlc_object_t*) p_this) );
181 DBUS_METHOD( PositionSet )
182 { /* set position in milliseconds */
185 vlc_value_t position;
186 playlist_t* p_playlist = NULL;
190 dbus_error_init( &error );
192 dbus_message_get_args( p_from, &error,
193 DBUS_TYPE_INT32, &i_pos,
196 if( dbus_error_is_set( &error ) )
198 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
200 dbus_error_free( &error );
201 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
203 p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
204 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
208 position.i_time = i_pos * 1000;
209 var_Set( p_input, "time", position );
210 vlc_object_release( p_input );
212 pl_Release( ((vlc_object_t*) p_this) );
216 DBUS_METHOD( VolumeGet )
217 { /* returns volume in percentage */
220 dbus_int32_t i_dbus_vol;
221 audio_volume_t i_vol;
222 /* 2nd argument of aout_VolumeGet is int32 */
223 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
224 double f_vol = 100. * i_vol / AOUT_VOLUME_MAX;
225 i_dbus_vol = round( f_vol );
226 ADD_INT32( &i_dbus_vol );
230 DBUS_METHOD( VolumeSet )
231 { /* set volume in percentage */
235 dbus_error_init( &error );
237 dbus_int32_t i_dbus_vol;
238 audio_volume_t i_vol;
240 dbus_message_get_args( p_from, &error,
241 DBUS_TYPE_INT32, &i_dbus_vol,
244 if( dbus_error_is_set( &error ) )
246 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
248 dbus_error_free( &error );
249 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
252 double f_vol = AOUT_VOLUME_MAX * i_dbus_vol / 100.;
253 i_vol = round( f_vol );
254 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
260 { /* next playlist item */
262 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
263 playlist_Next( p_playlist );
264 pl_Release( ((vlc_object_t*) p_this) );
269 { /* previous playlist item */
271 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
272 playlist_Prev( p_playlist );
273 pl_Release( ((vlc_object_t*) p_this) );
280 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
281 playlist_Stop( p_playlist );
282 pl_Release( ((vlc_object_t*) p_this) );
286 DBUS_METHOD( GetStatus )
287 { /* returns the current status as a struct of 4 ints */
289 First 0 = Playing, 1 = Paused, 2 = Stopped.
290 Second 0 = Playing linearly , 1 = Playing randomly.
291 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
292 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
297 MarshalStatus( p_this, &args, true );
305 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
306 playlist_Pause( p_playlist );
307 pl_Release( (vlc_object_t*) p_this );
314 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
316 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
321 input_Control( p_input, INPUT_SET_POSITION, i_pos );
322 vlc_object_release( p_input );
325 playlist_Play( p_playlist );
327 pl_Release( (vlc_object_t*) p_this );
331 DBUS_METHOD( GetCurrentMetadata )
335 playlist_t* p_playlist = pl_Hold( (vlc_object_t*) p_this );
337 playlist_item_t* p_item = playlist_CurrentPlayingItem( p_playlist );
339 GetInputMeta( p_item->p_input, &args );
341 pl_Release( (vlc_object_t*) p_this );
345 DBUS_METHOD( GetCaps )
350 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
355 /* Media Player information */
357 DBUS_METHOD( Identity )
363 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
365 ADD_STRING( &psz_identity );
366 free( psz_identity );
369 return DBUS_HANDLER_RESULT_NEED_MEMORY;
376 DBUS_METHOD( AddTrack )
377 { /* add the string to the playlist, and play it if the boolean is true */
382 dbus_error_init( &error );
383 playlist_t* p_playlist = NULL;
388 dbus_message_get_args( p_from, &error,
389 DBUS_TYPE_STRING, &psz_mrl,
390 DBUS_TYPE_BOOLEAN, &b_play,
393 if( dbus_error_is_set( &error ) )
395 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
397 dbus_error_free( &error );
398 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
401 p_playlist = pl_Hold( (vlc_object_t*) p_this );
402 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
403 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
404 PLAYLIST_END, true, false );
405 pl_Release( (vlc_object_t*) p_this );
407 dbus_int32_t i_success = 0;
408 ADD_INT32( &i_success );
413 DBUS_METHOD( GetCurrentTrack )
418 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
419 dbus_int32_t i_position = p_playlist->i_current_index;
420 pl_Release( (vlc_object_t*) p_this );
422 ADD_INT32( &i_position );
426 DBUS_METHOD( GetMetadata )
431 dbus_error_init( &error );
433 dbus_int32_t i_position;
435 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
438 dbus_message_get_args( p_from, &error,
439 DBUS_TYPE_INT32, &i_position,
442 if( dbus_error_is_set( &error ) )
445 pl_Release( (vlc_object_t*) p_this );
446 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
448 dbus_error_free( &error );
449 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
452 if( i_position < p_playlist->current.i_size )
454 GetInputMeta( p_playlist->current.p_elems[i_position]->p_input, &args );
458 pl_Release( (vlc_object_t*) p_this );
462 DBUS_METHOD( GetLength )
467 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
468 dbus_int32_t i_elements = p_playlist->current.i_size;
469 pl_Release( (vlc_object_t*) p_this );
471 ADD_INT32( &i_elements );
475 DBUS_METHOD( DelTrack )
480 dbus_error_init( &error );
482 dbus_int32_t i_position;
483 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
485 dbus_message_get_args( p_from, &error,
486 DBUS_TYPE_INT32, &i_position,
489 if( dbus_error_is_set( &error ) )
491 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
493 dbus_error_free( &error );
494 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
498 if( i_position < p_playlist->current.i_size )
500 playlist_DeleteFromInput( p_playlist,
501 p_playlist->current.p_elems[i_position]->p_input->i_id,
506 pl_Release( (vlc_object_t*) p_this );
511 DBUS_METHOD( SetLoop )
519 playlist_t* p_playlist = NULL;
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 val.b_bool = ( b_loop == TRUE ) ? true : false ;
535 p_playlist = pl_Hold( (vlc_object_t*) p_this );
536 var_Set ( p_playlist, "loop", val );
537 pl_Release( ((vlc_object_t*) p_this) );
542 DBUS_METHOD( Repeat )
548 dbus_bool_t b_repeat;
550 playlist_t* p_playlist = NULL;
552 dbus_error_init( &error );
553 dbus_message_get_args( p_from, &error,
554 DBUS_TYPE_BOOLEAN, &b_repeat,
557 if( dbus_error_is_set( &error ) )
559 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
561 dbus_error_free( &error );
562 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
565 val.b_bool = ( b_repeat == TRUE ) ? true : false ;
567 p_playlist = pl_Hold( (vlc_object_t*) p_this );
568 var_Set ( p_playlist, "repeat", val );
569 pl_Release( ((vlc_object_t*) p_this) );
574 DBUS_METHOD( SetRandom )
580 dbus_bool_t b_random;
582 playlist_t* p_playlist = NULL;
584 dbus_error_init( &error );
585 dbus_message_get_args( p_from, &error,
586 DBUS_TYPE_BOOLEAN, &b_random,
589 if( dbus_error_is_set( &error ) )
591 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
593 dbus_error_free( &error );
594 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
597 val.b_bool = ( b_random == TRUE ) ? true : false ;
599 p_playlist = pl_Hold( (vlc_object_t*) p_this );
600 var_Set ( p_playlist, "random", val );
601 pl_Release( ((vlc_object_t*) p_this) );
605 /*****************************************************************************
606 * Introspection method
607 *****************************************************************************/
609 DBUS_METHOD( handle_introspect_root )
610 { /* handles introspection of root object */
614 ADD_STRING( &psz_introspection_xml_data_root );
618 DBUS_METHOD( handle_introspect_player )
623 ADD_STRING( &psz_introspection_xml_data_player );
627 DBUS_METHOD( handle_introspect_tracklist )
632 ADD_STRING( &psz_introspection_xml_data_tracklist );
636 /*****************************************************************************
637 * handle_*: answer to incoming messages
638 *****************************************************************************/
640 #define METHOD_FUNC( method, function ) \
641 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
642 return function( p_conn, p_from, p_this )
644 DBUS_METHOD( handle_root )
647 if( dbus_message_is_method_call( p_from,
648 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
649 return handle_introspect_root( p_conn, p_from, p_this );
651 /* here D-Bus method's names are associated to an handler */
653 METHOD_FUNC( "Identity", Identity );
654 METHOD_FUNC( "MprisVersion", MprisVersion );
655 METHOD_FUNC( "Quit", Quit );
657 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
661 DBUS_METHOD( handle_player )
663 if( dbus_message_is_method_call( p_from,
664 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
665 return handle_introspect_player( p_conn, p_from, p_this );
667 /* here D-Bus method's names are associated to an handler */
669 METHOD_FUNC( "Prev", Prev );
670 METHOD_FUNC( "Next", Next );
671 METHOD_FUNC( "Stop", Stop );
672 METHOD_FUNC( "Play", Play );
673 METHOD_FUNC( "Pause", Pause );
674 METHOD_FUNC( "Repeat", Repeat );
675 METHOD_FUNC( "VolumeSet", VolumeSet );
676 METHOD_FUNC( "VolumeGet", VolumeGet );
677 METHOD_FUNC( "PositionSet", PositionSet );
678 METHOD_FUNC( "PositionGet", PositionGet );
679 METHOD_FUNC( "GetStatus", GetStatus );
680 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
681 METHOD_FUNC( "GetCaps", GetCaps );
683 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
686 DBUS_METHOD( handle_tracklist )
688 if( dbus_message_is_method_call( p_from,
689 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
690 return handle_introspect_tracklist( p_conn, p_from, p_this );
692 /* here D-Bus method's names are associated to an handler */
694 METHOD_FUNC( "GetMetadata", GetMetadata );
695 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
696 METHOD_FUNC( "GetLength", GetLength );
697 METHOD_FUNC( "AddTrack", AddTrack );
698 METHOD_FUNC( "DelTrack", DelTrack );
699 METHOD_FUNC( "SetLoop", SetLoop );
700 METHOD_FUNC( "SetRandom", SetRandom );
702 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
705 /*****************************************************************************
706 * Open: initialize interface
707 *****************************************************************************/
709 static int Open( vlc_object_t *p_this )
710 { /* initialisation of the connection */
711 intf_thread_t *p_intf = (intf_thread_t*)p_this;
712 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
713 playlist_t *p_playlist;
714 DBusConnection *p_conn;
720 p_sys->b_meta_read = false;
721 p_sys->i_caps = CAPS_NONE;
722 p_sys->b_dead = false;
724 dbus_error_init( &error );
726 /* connect to the session bus */
727 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
730 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
732 dbus_error_free( &error );
737 /* register a well-known name on the bus */
738 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
739 if( dbus_error_is_set( &error ) )
741 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
742 ": %s", error.message );
743 dbus_error_free( &error );
748 /* we register the objects */
749 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
750 &vlc_dbus_root_vtable, p_this );
751 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
752 &vlc_dbus_player_vtable, p_this );
753 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
754 &vlc_dbus_tracklist_vtable, p_this );
756 dbus_connection_flush( p_conn );
758 p_playlist = pl_Hold( p_intf );
760 var_AddCallback( p_playlist, "item-current", TrackChange, p_intf );
761 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
762 var_AddCallback( p_playlist, "playlist-item-append", TrackListChangeEmit, p_intf );
763 var_AddCallback( p_playlist, "playlist-item-deleted", TrackListChangeEmit, p_intf );
764 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
765 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
766 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
768 pl_Release( p_intf );
770 p_intf->pf_run = Run;
771 p_intf->p_sys = p_sys;
772 p_sys->p_conn = p_conn;
774 UpdateCaps( p_intf, false );
779 /*****************************************************************************
780 * Close: destroy interface
781 *****************************************************************************/
783 static void Close ( vlc_object_t *p_this )
785 intf_thread_t *p_intf = (intf_thread_t*) p_this;
786 playlist_t *p_playlist = pl_Hold( p_intf );;
787 input_thread_t *p_input;
789 var_DelCallback( p_playlist, "item-current", TrackChange, p_intf );
790 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
791 var_DelCallback( p_playlist, "playlist-item-append", TrackListChangeEmit, p_intf );
792 var_DelCallback( p_playlist, "playlist-item-deleted", TrackListChangeEmit, p_intf );
793 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
794 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
795 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
797 p_input = playlist_CurrentInput( p_playlist );
800 var_DelCallback( p_input, "state", StateChange, p_intf );
801 vlc_object_release( p_input );
804 pl_Release( p_intf );
806 dbus_connection_unref( p_intf->p_sys->p_conn );
808 free( p_intf->p_sys );
811 /*****************************************************************************
813 *****************************************************************************/
815 static void Run ( intf_thread_t *p_intf )
819 msleep( INTF_IDLE_SLEEP );
820 int canc = vlc_savecancel();
821 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
822 vlc_restorecancel( canc );
826 /******************************************************************************
827 * CapsChange: player capabilities change signal
828 *****************************************************************************/
829 DBUS_SIGNAL( CapsChangeSignal )
831 SIGNAL_INIT( "CapsChange" );
834 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
838 /******************************************************************************
839 * TrackListChange: tracklist order / length change signal
840 *****************************************************************************/
841 DBUS_SIGNAL( TrackListChangeSignal )
842 { /* emit the new tracklist lengh */
843 SIGNAL_INIT("TrackListChange");
846 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_data );
847 dbus_int32_t i_elements = p_playlist->current.i_size;
848 pl_Release( (vlc_object_t*) p_data );
850 ADD_INT32( &i_elements );
854 /*****************************************************************************
855 * TrackListChangeEmit: Emits the TrackListChange signal
856 *****************************************************************************/
857 /* FIXME: It is not called on tracklist reordering */
858 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
859 vlc_value_t oldval, vlc_value_t newval, void *p_data )
862 intf_thread_t *p_intf = p_data;
864 if( !strcmp( psz_var, "playlist-item-append" ) )
866 /* don't signal when items are added/removed in p_category */
867 playlist_t *p_playlist = (playlist_t*)p_this;
868 playlist_add_t *p_add = newval.p_address;
869 playlist_item_t *p_item;
870 p_item = playlist_ItemGetById( p_playlist, p_add->i_node );
872 while( p_item->p_parent )
873 p_item = p_item->p_parent;
874 if( p_item == p_playlist->p_root_category )
878 if( p_intf->p_sys->b_dead )
881 UpdateCaps( p_intf, true );
882 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
885 /*****************************************************************************
886 * TrackChange: Playlist item change callback
887 *****************************************************************************/
889 DBUS_SIGNAL( TrackChangeSignal )
890 { /* emit the metadata of the new item */
891 SIGNAL_INIT( "TrackChange" );
894 input_item_t *p_item = (input_item_t*) p_data;
895 GetInputMeta ( p_item, &args );
900 /*****************************************************************************
901 * StatusChange: Player status change signal
902 *****************************************************************************/
904 DBUS_SIGNAL( StatusChangeSignal )
905 { /* send the updated status info on the bus */
906 SIGNAL_INIT( "StatusChange" );
909 /* we're called from a callback of input_thread_t, so it can not be
910 * destroyed before we return */
911 MarshalStatus( (intf_thread_t*) p_data, &args, false );
916 /*****************************************************************************
917 * StateChange: callback on input "state"
918 *****************************************************************************/
919 static int StateChange( vlc_object_t *p_this, const char* psz_var,
920 vlc_value_t oldval, vlc_value_t newval, void *p_data )
922 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
923 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
924 intf_sys_t *p_sys = p_intf->p_sys;
926 if( p_intf->p_sys->b_dead )
929 UpdateCaps( p_intf, true );
931 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
933 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
936 p_sys->b_meta_read = true;
937 TrackChangeSignal( p_sys->p_conn, p_item );
941 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
942 newval.i_int == END_S )
944 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
950 /*****************************************************************************
951 * StatusChangeEmit: Emits the StatusChange signal
952 *****************************************************************************/
953 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
954 vlc_value_t oldval, vlc_value_t newval, void *p_data )
956 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
957 VLC_UNUSED(oldval); VLC_UNUSED(newval);
958 intf_thread_t *p_intf = p_data;
960 if( p_intf->p_sys->b_dead )
963 UpdateCaps( p_intf, false );
964 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
968 /*****************************************************************************
969 * TrackChange: callback on playlist "item-current"
970 *****************************************************************************/
971 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
972 vlc_value_t oldval, vlc_value_t newval, void *p_data )
974 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
975 intf_sys_t *p_sys = p_intf->p_sys;
976 playlist_t *p_playlist;
977 input_thread_t *p_input = NULL;
978 input_item_t *p_item = NULL;
979 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
980 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
982 if( p_intf->p_sys->b_dead )
985 p_sys->b_meta_read = false;
987 p_playlist = pl_Hold( p_intf );
988 p_input = playlist_CurrentInput( p_playlist );
991 pl_Release( p_intf );
995 pl_Release( p_intf );
997 p_item = input_GetItem( p_input );
1000 vlc_object_release( p_input );
1001 return VLC_EGENERIC;
1004 if( input_item_IsPreparsed( p_item ) )
1006 p_sys->b_meta_read = true;
1007 TrackChangeSignal( p_sys->p_conn, p_item );
1010 var_AddCallback( p_input, "state", StateChange, p_intf );
1012 vlc_object_release( p_input );
1016 /*****************************************************************************
1017 * UpdateCaps: update p_sys->i_caps
1018 ****************************************************************************/
1019 static int UpdateCaps( intf_thread_t* p_intf, bool b_playlist_locked )
1021 intf_sys_t* p_sys = p_intf->p_sys;
1022 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1023 playlist_t* p_playlist = pl_Hold( p_intf );
1024 if( !b_playlist_locked ) PL_LOCK;
1026 if( p_playlist->current.i_size > 0 )
1027 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1028 if( !b_playlist_locked ) PL_UNLOCK;
1030 input_thread_t* p_input = playlist_CurrentInput( p_playlist );
1033 /* XXX: if UpdateCaps() is called too early, these are
1034 * unconditionnaly true */
1035 if( var_GetBool( p_input, "can-pause" ) )
1036 i_caps |= CAPS_CAN_PAUSE;
1037 if( var_GetBool( p_input, "can-seek" ) )
1038 i_caps |= CAPS_CAN_SEEK;
1039 vlc_object_release( p_input );
1042 pl_Release( p_intf );
1044 if( p_sys->b_meta_read )
1045 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1047 if( i_caps != p_intf->p_sys->i_caps )
1049 p_sys->i_caps = i_caps;
1050 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1056 /*****************************************************************************
1057 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1058 *****************************************************************************/
1060 #define ADD_META( entry, type, data ) \
1062 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1063 NULL, &dict_entry ); \
1064 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1065 &ppsz_meta_items[entry] ); \
1066 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1067 type##_AS_STRING, &variant ); \
1068 dbus_message_iter_append_basic( &variant, \
1071 dbus_message_iter_close_container( &dict_entry, &variant ); \
1072 dbus_message_iter_close_container( &dict, &dict_entry ); }
1074 #define ADD_VLC_META_STRING( entry, item ) \
1076 char * psz = input_item_Get##item( p_input );\
1077 ADD_META( entry, DBUS_TYPE_STRING, \
1082 static int GetInputMeta( input_item_t* p_input,
1083 DBusMessageIter *args )
1085 DBusMessageIter dict, dict_entry, variant;
1086 /* We need the track length to be expressed in milli-seconds
1087 * instead of µ-seconds */
1088 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1090 const char* ppsz_meta_items[] =
1092 "title", "artist", "genre", "copyright", "album", "tracknum",
1093 "description", "rating", "date", "setting", "url", "language",
1094 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1095 "status", "location", "length", "video-codec", "audio-codec",
1096 "video-bitrate", "audio-bitrate", "audio-samplerate"
1099 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1101 ADD_VLC_META_STRING( 0, Title );
1102 ADD_VLC_META_STRING( 1, Artist );
1103 ADD_VLC_META_STRING( 2, Genre );
1104 ADD_VLC_META_STRING( 3, Copyright );
1105 ADD_VLC_META_STRING( 4, Album );
1106 ADD_VLC_META_STRING( 5, TrackNum );
1107 ADD_VLC_META_STRING( 6, Description );
1108 ADD_VLC_META_STRING( 7, Rating );
1109 ADD_VLC_META_STRING( 8, Date );
1110 ADD_VLC_META_STRING( 9, Setting );
1111 ADD_VLC_META_STRING( 10, URL );
1112 ADD_VLC_META_STRING( 11, Language );
1113 ADD_VLC_META_STRING( 12, NowPlaying );
1114 ADD_VLC_META_STRING( 13, Publisher );
1115 ADD_VLC_META_STRING( 14, EncodedBy );
1116 ADD_VLC_META_STRING( 15, ArtURL );
1117 ADD_VLC_META_STRING( 16, TrackID );
1119 vlc_mutex_lock( &p_input->lock );
1120 if( p_input->p_meta )
1121 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1122 vlc_mutex_unlock( &p_input->lock );
1124 ADD_VLC_META_STRING( 18, URI );
1125 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1127 dbus_message_iter_close_container( args, &dict );
1132 #undef ADD_VLC_META_STRING
1134 /*****************************************************************************
1135 * MarshalStatus: Fill a DBusMessage with the current player status
1136 *****************************************************************************/
1138 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1140 { /* This is NOT the right way to do that, it would be better to sore
1141 the status information in p_sys and update it on change, thus
1142 avoiding a long lock */
1144 DBusMessageIter status;
1145 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1147 playlist_t* p_playlist = NULL;
1148 input_thread_t* p_input = NULL;
1150 p_playlist = pl_Hold( p_intf );
1154 p_input = playlist_CurrentInput( p_playlist );
1157 var_Get( p_input, "state", &val );
1158 if( val.i_int >= END_S )
1160 else if( val.i_int == PAUSE_S )
1162 else if( val.i_int <= PLAYING_S )
1164 vlc_object_release( p_input );
1167 i_random = var_CreateGetBool( p_playlist, "random" );
1169 i_repeat = var_CreateGetBool( p_playlist, "repeat" );
1171 i_loop = var_CreateGetBool( p_playlist, "loop" );
1173 pl_Release( p_intf );
1175 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1176 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1177 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1178 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1179 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1180 dbus_message_iter_close_container( args, &status );