1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright (C) 2006 Rafaël Carré
7 * Authors: Rafaël Carré <funman at videolanorg>
8 * Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 * D-Bus Specification:
27 * http://dbus.freedesktop.org/doc/dbus-specification.html
28 * D-Bus low-level C API (libdbus)
29 * http://dbus.freedesktop.org/doc/dbus/api/html/index.html
36 * macros to read incoming arguments
38 * explore different possible types (arrays..)
40 * what must we do if org.videolan.vlc already exist on the bus ?
41 * ( there is more than one vlc instance )
44 /*****************************************************************************
46 *****************************************************************************/
48 #include <dbus/dbus.h>
57 #include <vlc_interface.h>
59 #include <vlc_input.h>
60 #include <vlc_playlist.h>
63 /*****************************************************************************
65 *****************************************************************************/
67 static int Open ( vlc_object_t * );
68 static void Close ( vlc_object_t * );
69 static void Run ( intf_thread_t * );
72 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
73 vlc_value_t oldval, vlc_value_t newval, void *p_data );
75 static int GetInputMeta ( input_item_t *p_input,
76 DBusMessageIter *args);
80 DBusConnection *p_conn;
83 /*****************************************************************************
85 *****************************************************************************/
88 set_shortname( _("dbus"));
89 set_category( CAT_INTERFACE );
90 set_subcategory( SUBCAT_INTERFACE_CONTROL );
91 set_description( _("D-Bus control interface") );
92 set_capability( "interface", 0 );
93 set_callbacks( Open, Close );
96 /*****************************************************************************
98 *****************************************************************************/
100 DBUS_METHOD( PlaylistExport_XSPF )
101 { /*export playlist to an xspf file */
103 /* reads the filename to export to */
104 /* returns the status as int32:
113 dbus_error_init( &error );
118 dbus_message_get_args( p_from, &error,
119 DBUS_TYPE_STRING, &psz_file,
122 if( dbus_error_is_set( &error ) )
124 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
126 dbus_error_free( &error );
127 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
130 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
132 if( ( !playlist_IsEmpty( p_playlist ) ) &&
133 ( p_playlist->p_root_category->i_children > 0 ) )
135 if( playlist_Export( p_playlist, psz_file,
136 p_playlist->p_root_category->pp_children[0],
137 "export-xspf" ) == VLC_SUCCESS )
145 pl_Release( ((vlc_object_t*) p_this ) );
157 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
158 playlist_Stop( p_playlist );
159 pl_Release( ((vlc_object_t*) p_this) );
160 vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
164 DBUS_METHOD( PositionGet )
165 { /* returns position as an int in the range [0;1000] */
168 vlc_value_t position;
171 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
172 input_thread_t *p_input = p_playlist->p_input;
178 var_Get( p_input, "position", &position );
179 i_pos = position.f_float * 1000 ;
181 pl_Release( ((vlc_object_t*) p_this) );
186 DBUS_METHOD( PositionSet )
187 { /* set position from an int in the range [0;1000] */
190 vlc_value_t position;
191 playlist_t* p_playlist = NULL;
195 dbus_error_init( &error );
197 dbus_message_get_args( p_from, &error,
198 DBUS_TYPE_INT32, &i_pos,
201 if( dbus_error_is_set( &error ) )
203 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
205 dbus_error_free( &error );
206 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
208 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
209 input_thread_t *p_input = p_playlist->p_input;
213 position.f_float = ((float)i_pos) / 1000;
214 var_Set( p_input, "position", position );
216 pl_Release( ((vlc_object_t*) p_this) );
220 DBUS_METHOD( VolumeGet )
221 { /* returns volume in percentage */
224 dbus_int32_t i_dbus_vol;
225 audio_volume_t i_vol;
226 /* 2nd argument of aout_VolumeGet is int32 */
227 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
228 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
229 ADD_INT32( &i_dbus_vol );
233 DBUS_METHOD( VolumeSet )
234 { /* set volume in percentage */
238 dbus_error_init( &error );
240 dbus_int32_t i_dbus_vol;
241 audio_volume_t i_vol;
243 dbus_message_get_args( p_from, &error,
244 DBUS_TYPE_INT32, &i_dbus_vol,
247 if( dbus_error_is_set( &error ) )
249 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
251 dbus_error_free( &error );
252 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
255 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
256 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
262 { /* next playlist item */
264 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
265 playlist_Next( p_playlist );
266 pl_Release( ((vlc_object_t*) p_this) );
271 { /* previous playlist item */
273 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
274 playlist_Prev( p_playlist );
275 pl_Release( ((vlc_object_t*) p_this) );
282 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
283 playlist_Stop( p_playlist );
284 pl_Release( ((vlc_object_t*) p_this) );
288 DBUS_METHOD( GetStatus )
289 { /* returns an int: 0=playing 1=paused 2=stopped */
293 dbus_int32_t i_status;
296 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
297 input_thread_t *p_input = p_playlist->p_input;
302 var_Get( p_input, "state", &val );
303 if( val.i_int == PAUSE_S )
305 else if( val.i_int == PLAYING_S )
309 pl_Release( p_playlist );
311 ADD_INT32( &i_status );
318 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
319 playlist_Pause( p_playlist );
320 pl_Release( p_playlist );
327 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
328 playlist_Play( p_playlist );
329 pl_Release( p_playlist );
333 DBUS_METHOD( GetCurrentMetadata )
337 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
339 GetInputMeta( p_playlist->status.p_item->p_input, &args );
341 pl_Release( p_playlist );
345 /* Media Player information */
347 DBUS_METHOD( Identity )
351 char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 1 );
352 sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
353 ADD_STRING( &psz_identity );
354 free( psz_identity );
360 DBUS_METHOD( AddTrack )
361 { /* add the string to the playlist, and play it if the boolean is true */
365 dbus_error_init( &error );
366 playlist_t* p_playlist = NULL;
371 dbus_message_get_args( p_from, &error,
372 DBUS_TYPE_STRING, &psz_mrl,
373 DBUS_TYPE_BOOLEAN, &b_play,
376 if( dbus_error_is_set( &error ) )
378 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
380 dbus_error_free( &error );
381 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
384 p_playlist = pl_Yield( (vlc_object_t*) p_this );
385 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
386 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
387 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
388 pl_Release( p_playlist );
393 DBUS_METHOD( GetCurrentTrack )
397 dbus_int32_t i_position = 0;
398 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
399 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
401 while ( p_tested_item->p_input->i_id !=
402 p_playlist->status.p_item->p_input->i_id )
408 pl_Release( p_playlist );
410 ADD_INT32( &i_position );
414 DBUS_METHOD( GetMetadata )
419 dbus_error_init( &error );
421 dbus_int32_t i_position, i_count = 0;
423 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
424 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
426 dbus_message_get_args( p_from, &error,
427 DBUS_TYPE_INT32, &i_position,
430 if( dbus_error_is_set( &error ) )
432 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
434 dbus_error_free( &error );
435 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
438 while ( i_count < i_position )
444 GetInputMeta ( p_tested_item->p_input, &args );
446 pl_Release( p_playlist );
450 DBUS_METHOD( GetLength )
455 dbus_int32_t i_elements = 0;
456 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
457 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
458 playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist,
459 p_playlist->p_root_onelevel );
461 while ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id )
467 pl_Release( p_playlist );
469 ADD_INT32( &i_elements );
473 DBUS_METHOD( DelTrack )
478 dbus_error_init( &error );
480 dbus_int32_t i_position, i_count = 0;
481 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
482 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
484 dbus_message_get_args( p_from, &error,
485 DBUS_TYPE_INT32, &i_position,
488 if( dbus_error_is_set( &error ) )
490 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
492 dbus_error_free( &error );
493 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
496 while ( i_count < i_position )
503 playlist_DeleteFromInput( p_playlist,
504 p_tested_item->p_input->i_id,
508 pl_Release( p_playlist );
521 playlist_t* p_playlist = NULL;
523 dbus_error_init( &error );
524 dbus_message_get_args( p_from, &error,
525 DBUS_TYPE_BOOLEAN, &b_loop,
528 if( dbus_error_is_set( &error ) )
530 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
532 dbus_error_free( &error );
533 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
536 val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
537 p_playlist = pl_Yield( (vlc_object_t*) p_this );
538 var_Set ( p_playlist, "loop", val );
539 pl_Release( ((vlc_object_t*) p_this) );
544 DBUS_METHOD( Repeat )
550 dbus_bool_t b_repeat;
552 playlist_t* p_playlist = NULL;
554 dbus_error_init( &error );
555 dbus_message_get_args( p_from, &error,
556 DBUS_TYPE_BOOLEAN, &b_repeat,
559 if( dbus_error_is_set( &error ) )
561 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
563 dbus_error_free( &error );
564 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
567 val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
569 p_playlist = pl_Yield( (vlc_object_t*) p_this );
570 var_Set ( p_playlist, "repeat", val );
571 pl_Release( ((vlc_object_t*) p_this) );
576 DBUS_METHOD( Random )
582 dbus_bool_t b_random;
584 playlist_t* p_playlist = NULL;
586 dbus_error_init( &error );
587 dbus_message_get_args( p_from, &error,
588 DBUS_TYPE_BOOLEAN, &b_random,
591 if( dbus_error_is_set( &error ) )
593 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
595 dbus_error_free( &error );
596 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
599 val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
601 p_playlist = pl_Yield( (vlc_object_t*) p_this );
602 var_Set ( p_playlist, "random", val );
603 pl_Release( ((vlc_object_t*) p_this) );
607 /*****************************************************************************
608 * Introspection method
609 *****************************************************************************/
611 DBUS_METHOD( handle_introspect_root )
612 { /* handles introspection of /org/videolan/vlc */
615 ADD_STRING( &psz_introspection_xml_data_root );
619 DBUS_METHOD( handle_introspect_player )
623 ADD_STRING( &psz_introspection_xml_data_player );
627 DBUS_METHOD( handle_introspect_tracklist )
631 ADD_STRING( &psz_introspection_xml_data_tracklist );
635 /*****************************************************************************
636 * handle_*: answer to incoming messages
637 *****************************************************************************/
639 #define METHOD_FUNC( method, function ) \
640 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
641 return function( p_conn, p_from, p_this )
643 DBUS_METHOD( handle_root )
646 if( dbus_message_is_method_call( p_from,
647 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
648 return handle_introspect_root( p_conn, p_from, p_this );
650 /* here D-Bus method's names are associated to an handler */
652 METHOD_FUNC( "Identity", Identity );
654 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
658 DBUS_METHOD( handle_player )
660 if( dbus_message_is_method_call( p_from,
661 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
662 return handle_introspect_player( p_conn, p_from, p_this );
664 /* here D-Bus method's names are associated to an handler */
666 METHOD_FUNC( "Prev", Prev );
667 METHOD_FUNC( "Next", Next );
668 METHOD_FUNC( "Quit", Quit );
669 METHOD_FUNC( "Stop", Stop );
670 METHOD_FUNC( "Play", Play );
671 METHOD_FUNC( "Pause", Pause );
672 METHOD_FUNC( "VolumeSet", VolumeSet );
673 METHOD_FUNC( "VolumeGet", VolumeGet );
674 METHOD_FUNC( "PositionSet", PositionSet );
675 METHOD_FUNC( "PositionGet", PositionGet );
676 METHOD_FUNC( "GetStatus", GetStatus );
677 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
679 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
682 DBUS_METHOD( handle_tracklist )
684 if( dbus_message_is_method_call( p_from,
685 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
686 return handle_introspect_tracklist( p_conn, p_from, p_this );
688 /* here D-Bus method's names are associated to an handler */
690 METHOD_FUNC( "GetMetadata", GetMetadata );
691 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
692 METHOD_FUNC( "GetLength", GetLength );
693 METHOD_FUNC( "AddTrack", AddTrack );
694 METHOD_FUNC( "DelTrack", DelTrack );
695 METHOD_FUNC( "Loop", Loop );
696 METHOD_FUNC( "Repeat", Repeat );
697 METHOD_FUNC( "Random", Random );
699 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
702 /*****************************************************************************
703 * Open: initialize interface
704 *****************************************************************************/
706 static int Open( vlc_object_t *p_this )
707 { /* initialisation of the connection */
708 intf_thread_t *p_intf = (intf_thread_t*)p_this;
709 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
710 playlist_t *p_playlist;
711 DBusConnection *p_conn;
717 dbus_threads_init_default();
719 dbus_error_init( &error );
721 /* connect to the session bus */
722 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
725 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
727 dbus_error_free( &error );
732 /* register a well-known name on the bus */
733 dbus_bus_request_name( p_conn, "org.freedesktop.MediaPlayer", 0, &error );
734 if( dbus_error_is_set( &error ) )
736 msg_Err( p_this, "Error requesting org.freedesktop.MediaPlayer service:" " %s\n", error.message );
737 dbus_error_free( &error );
742 /* we register the objects */
743 dbus_connection_register_object_path( p_conn, VLC_DBUS_ROOT_PATH,
744 &vlc_dbus_root_vtable, p_this );
745 dbus_connection_register_object_path( p_conn, VLC_DBUS_PLAYER_PATH,
746 &vlc_dbus_player_vtable, p_this );
747 dbus_connection_register_object_path( p_conn, VLC_DBUS_TRACKLIST_PATH,
748 &vlc_dbus_tracklist_vtable, p_this );
750 dbus_connection_flush( p_conn );
752 p_playlist = pl_Yield( p_intf );
754 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
756 pl_Release( p_playlist );
758 p_intf->pf_run = Run;
759 p_intf->p_sys = p_sys;
760 p_sys->p_conn = p_conn;
765 /*****************************************************************************
766 * Close: destroy interface
767 *****************************************************************************/
769 static void Close ( vlc_object_t *p_this )
771 intf_thread_t *p_intf = (intf_thread_t*) p_this;
772 playlist_t *p_playlist = pl_Yield( p_intf );;
775 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
777 pl_Release( p_playlist );
779 dbus_connection_unref( p_intf->p_sys->p_conn );
781 free( p_intf->p_sys );
784 /*****************************************************************************
786 *****************************************************************************/
788 static void Run ( intf_thread_t *p_intf )
790 while( !p_intf->b_die )
792 msleep( INTF_IDLE_SLEEP );
793 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
797 /*****************************************************************************
798 * TrackChange: Playlist item change callback
799 *****************************************************************************/
801 DBUS_SIGNAL( TrackChangeSignal )
802 { /* emit the metadata of the new item */
803 SIGNAL_INIT( "TrackChange" );
806 input_thread_t *p_input = (input_thread_t*) p_data;
807 GetInputMeta ( input_GetItem(p_input), &args );
812 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
813 vlc_value_t oldval, vlc_value_t newval, void *p_data )
815 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
816 intf_sys_t *p_sys = p_intf->p_sys;
817 playlist_t *p_playlist;
818 input_thread_t *p_input = NULL;
819 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
821 p_playlist = pl_Yield( p_intf );
823 p_input = p_playlist->p_input;
828 pl_Release( p_playlist );
832 vlc_object_yield( p_input );
834 pl_Release( p_playlist );
836 TrackChangeSignal( p_sys->p_conn, p_input );
838 vlc_object_release( p_input );
842 /*****************************************************************************
843 * GetInputMeta: Fill a DBusMessage with the given input item metadata
844 *****************************************************************************/
846 #define ADD_META( entry, type, data ) \
848 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
849 NULL, &dict_entry ); \
850 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
851 &ppsz_meta_items[entry] ); \
852 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
853 type##_AS_STRING, &variant ); \
854 dbus_message_iter_append_basic( &variant, \
857 dbus_message_iter_close_container( &dict_entry, &variant ); \
858 dbus_message_iter_close_container( &dict, &dict_entry ); }\
860 #define ADD_VLC_META_STRING( entry, item ) \
861 ADD_META( entry, DBUS_TYPE_STRING, \
862 p_input->p_meta->psz_##item );
864 static int GetInputMeta( input_item_t* p_input,
865 DBusMessageIter *args )
866 { /*FIXME: Works only for already read metadata. */
868 DBusMessageIter dict, dict_entry, variant;
869 /* We need the track length to be expressed in seconds
870 * instead of milliseconds */
871 dbus_int64_t i_length = (p_input->i_duration / 1000);
874 const char* ppsz_meta_items[] =
876 "title", "artist", "genre", "copyright", "album", "tracknum",
877 "description", "rating", "date", "setting", "url", "language",
878 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
879 "status", "URI", "length", "video-codec", "audio-codec",
880 "video-bitrate", "audio-bitrate", "audio-samplerate"
883 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
885 ADD_VLC_META_STRING( 0, title );
886 ADD_VLC_META_STRING( 1, artist );
887 ADD_VLC_META_STRING( 2, genre );
888 ADD_VLC_META_STRING( 3, copyright );
889 ADD_VLC_META_STRING( 4, album );
890 ADD_VLC_META_STRING( 5, tracknum );
891 ADD_VLC_META_STRING( 6, description );
892 ADD_VLC_META_STRING( 7, rating );
893 ADD_VLC_META_STRING( 8, date );
894 ADD_VLC_META_STRING( 9, setting );
895 ADD_VLC_META_STRING( 10, url );
896 ADD_VLC_META_STRING( 11, language );
897 ADD_VLC_META_STRING( 12, nowplaying );
898 ADD_VLC_META_STRING( 13, publisher );
899 ADD_VLC_META_STRING( 14, encodedby );
900 ADD_VLC_META_STRING( 15, arturl );
901 ADD_VLC_META_STRING( 16, trackid );
903 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
904 ADD_META( 18, DBUS_TYPE_STRING, p_input->psz_uri );
905 ADD_META( 19, DBUS_TYPE_INT64, i_length );
907 dbus_message_iter_close_container( args, &dict );
912 #undef ADD_VLC_META_STRING