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
31 "If you use this low-level API directly, you're signing up for some pain."
32 * MPRIS Specification (still drafting on June, 25 of 2007):
33 * http://wiki.xmms2.xmms.se/index.php/Media_Player_Interfaces
36 /*****************************************************************************
38 *****************************************************************************/
40 #include <dbus/dbus.h>
49 #include <vlc_interface.h>
51 #include <vlc_input.h>
52 #include <vlc_playlist.h>
54 /*****************************************************************************
56 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close ( vlc_object_t * );
60 static void Run ( intf_thread_t * );
62 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
63 vlc_value_t oldval, vlc_value_t newval, void *p_data );
65 static int GetInputMeta ( input_item_t *p_input,
66 DBusMessageIter *args);
70 DBusConnection *p_conn;
73 /*****************************************************************************
75 *****************************************************************************/
78 set_shortname( _("dbus"));
79 set_category( CAT_INTERFACE );
80 set_subcategory( SUBCAT_INTERFACE_CONTROL );
81 set_description( _("D-Bus control interface") );
82 set_capability( "interface", 0 );
83 set_callbacks( Open, Close );
86 /*****************************************************************************
88 *****************************************************************************/
90 DBUS_METHOD( PlaylistExport_XSPF )
91 { /*export playlist to an xspf file */
93 /* reads the filename to export to */
94 /* returns the status as int32:
103 dbus_error_init( &error );
108 dbus_message_get_args( p_from, &error,
109 DBUS_TYPE_STRING, &psz_file,
112 if( dbus_error_is_set( &error ) )
114 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
116 dbus_error_free( &error );
117 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
120 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
122 if( ( !playlist_IsEmpty( p_playlist ) ) &&
123 ( p_playlist->p_root_category->i_children > 0 ) )
125 if( playlist_Export( p_playlist, psz_file,
126 p_playlist->p_root_category->pp_children[0],
127 "export-xspf" ) == VLC_SUCCESS )
135 pl_Release( ((vlc_object_t*) p_this ) );
147 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
148 playlist_Stop( p_playlist );
149 pl_Release( ((vlc_object_t*) p_this) );
150 vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
154 DBUS_METHOD( PositionGet )
155 { /* returns position as an int in the range [0;1000] */
158 vlc_value_t position;
161 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
162 input_thread_t *p_input = p_playlist->p_input;
168 var_Get( p_input, "position", &position );
169 i_pos = position.f_float * 1000 ;
171 pl_Release( ((vlc_object_t*) p_this) );
176 DBUS_METHOD( PositionSet )
177 { /* set position from an int in the range [0;1000] */
180 vlc_value_t position;
181 playlist_t* p_playlist = NULL;
185 dbus_error_init( &error );
187 dbus_message_get_args( p_from, &error,
188 DBUS_TYPE_INT32, &i_pos,
191 if( dbus_error_is_set( &error ) )
193 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
195 dbus_error_free( &error );
196 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
198 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
199 input_thread_t *p_input = p_playlist->p_input;
203 position.f_float = ((float)i_pos) / 1000;
204 var_Set( p_input, "position", position );
206 pl_Release( ((vlc_object_t*) p_this) );
210 DBUS_METHOD( VolumeGet )
211 { /* returns volume in percentage */
214 dbus_int32_t i_dbus_vol;
215 audio_volume_t i_vol;
216 /* 2nd argument of aout_VolumeGet is int32 */
217 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
218 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
219 ADD_INT32( &i_dbus_vol );
223 DBUS_METHOD( VolumeSet )
224 { /* set volume in percentage */
228 dbus_error_init( &error );
230 dbus_int32_t i_dbus_vol;
231 audio_volume_t i_vol;
233 dbus_message_get_args( p_from, &error,
234 DBUS_TYPE_INT32, &i_dbus_vol,
237 if( dbus_error_is_set( &error ) )
239 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
241 dbus_error_free( &error );
242 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
245 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
246 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
252 { /* next playlist item */
254 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
255 playlist_Next( p_playlist );
256 pl_Release( ((vlc_object_t*) p_this) );
261 { /* previous playlist item */
263 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
264 playlist_Prev( p_playlist );
265 pl_Release( ((vlc_object_t*) p_this) );
272 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
273 playlist_Stop( p_playlist );
274 pl_Release( ((vlc_object_t*) p_this) );
278 DBUS_METHOD( GetStatus )
279 { /* returns an int: 0=playing 1=paused 2=stopped */
283 dbus_int32_t i_status;
286 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
287 input_thread_t *p_input = p_playlist->p_input;
292 var_Get( p_input, "state", &val );
293 if( val.i_int == PAUSE_S )
295 else if( val.i_int == PLAYING_S )
299 pl_Release( p_playlist );
301 ADD_INT32( &i_status );
308 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
309 playlist_Pause( p_playlist );
310 pl_Release( p_playlist );
317 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
318 playlist_Play( p_playlist );
319 pl_Release( p_playlist );
323 DBUS_METHOD( Disconnect )
328 dbus_error_init( &error );
329 i = dbus_bus_release_name( p_conn, "org.mpris.vlc", &error );
330 if( ( i == -1 ) && ( dbus_error_is_set( &error ) ) )
332 msg_Err( (vlc_object_t*) p_this, "D-Bus disconnection failed : %s\n",
334 dbus_error_free( &error );
339 DBUS_METHOD( GetCurrentMetadata )
343 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
345 GetInputMeta( p_playlist->status.p_item->p_input, &args );
347 pl_Release( p_playlist );
351 /* Media Player information */
353 DBUS_METHOD( Identity )
357 char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 1 );
358 sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
359 ADD_STRING( &psz_identity );
360 free( psz_identity );
366 DBUS_METHOD( AddTrack )
367 { /* add the string to the playlist, and play it if the boolean is true */
371 dbus_error_init( &error );
372 playlist_t* p_playlist = NULL;
377 dbus_message_get_args( p_from, &error,
378 DBUS_TYPE_STRING, &psz_mrl,
379 DBUS_TYPE_BOOLEAN, &b_play,
382 if( dbus_error_is_set( &error ) )
384 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
386 dbus_error_free( &error );
387 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
390 p_playlist = pl_Yield( (vlc_object_t*) p_this );
391 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
392 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
393 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
394 pl_Release( p_playlist );
399 DBUS_METHOD( GetCurrentTrack )
403 dbus_int32_t i_position = 0;
404 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
405 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
407 while ( p_tested_item->p_input->i_id !=
408 p_playlist->status.p_item->p_input->i_id )
414 pl_Release( p_playlist );
416 ADD_INT32( &i_position );
420 DBUS_METHOD( GetMetadata )
425 dbus_error_init( &error );
427 dbus_int32_t i_position, i_count = 0;
429 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
430 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
432 dbus_message_get_args( p_from, &error,
433 DBUS_TYPE_INT32, &i_position,
436 if( dbus_error_is_set( &error ) )
438 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
440 dbus_error_free( &error );
441 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
444 while ( i_count < i_position )
450 GetInputMeta ( p_tested_item->p_input, &args );
452 pl_Release( p_playlist );
456 DBUS_METHOD( GetLength )
461 dbus_int32_t i_elements = 0;
462 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
463 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
464 playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist,
465 p_playlist->p_root_onelevel );
467 while ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id )
473 pl_Release( p_playlist );
475 ADD_INT32( &i_elements );
479 DBUS_METHOD( DelTrack )
484 dbus_error_init( &error );
486 dbus_int32_t i_position, i_count = 0;
487 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
488 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
490 dbus_message_get_args( p_from, &error,
491 DBUS_TYPE_INT32, &i_position,
494 if( dbus_error_is_set( &error ) )
496 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
498 dbus_error_free( &error );
499 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
502 while ( i_count < i_position )
509 playlist_DeleteFromInput( p_playlist,
510 p_tested_item->p_input->i_id,
514 pl_Release( p_playlist );
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 ) ? VLC_TRUE : VLC_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 ) ? VLC_TRUE : VLC_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( Random )
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 ) ? VLC_TRUE : VLC_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 */
621 ADD_STRING( &psz_introspection_xml_data_root );
625 DBUS_METHOD( handle_introspect_player )
629 ADD_STRING( &psz_introspection_xml_data_player );
633 DBUS_METHOD( handle_introspect_tracklist )
637 ADD_STRING( &psz_introspection_xml_data_tracklist );
641 /*****************************************************************************
642 * handle_*: answer to incoming messages
643 *****************************************************************************/
645 #define METHOD_FUNC( method, function ) \
646 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
647 return function( p_conn, p_from, p_this )
649 DBUS_METHOD( handle_root )
652 if( dbus_message_is_method_call( p_from,
653 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
654 return handle_introspect_root( p_conn, p_from, p_this );
656 /* here D-Bus method's names are associated to an handler */
658 METHOD_FUNC( "Identity", Identity );
660 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
664 DBUS_METHOD( handle_player )
666 if( dbus_message_is_method_call( p_from,
667 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
668 return handle_introspect_player( p_conn, p_from, p_this );
670 /* here D-Bus method's names are associated to an handler */
672 METHOD_FUNC( "Prev", Prev );
673 METHOD_FUNC( "Next", Next );
674 METHOD_FUNC( "Quit", Quit );
675 METHOD_FUNC( "Stop", Stop );
676 METHOD_FUNC( "Play", Play );
677 METHOD_FUNC( "Pause", Pause );
678 METHOD_FUNC( "Repeat", Repeat );
679 METHOD_FUNC( "Disconnect", Disconnect );
680 METHOD_FUNC( "VolumeSet", VolumeSet );
681 METHOD_FUNC( "VolumeGet", VolumeGet );
682 METHOD_FUNC( "PositionSet", PositionSet );
683 METHOD_FUNC( "PositionGet", PositionGet );
684 METHOD_FUNC( "GetStatus", GetStatus );
685 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
687 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
690 DBUS_METHOD( handle_tracklist )
692 if( dbus_message_is_method_call( p_from,
693 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
694 return handle_introspect_tracklist( p_conn, p_from, p_this );
696 /* here D-Bus method's names are associated to an handler */
698 METHOD_FUNC( "GetMetadata", GetMetadata );
699 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
700 METHOD_FUNC( "GetLength", GetLength );
701 METHOD_FUNC( "AddTrack", AddTrack );
702 METHOD_FUNC( "DelTrack", DelTrack );
703 METHOD_FUNC( "Loop", Loop );
704 METHOD_FUNC( "Random", Random );
706 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
709 /*****************************************************************************
710 * Open: initialize interface
711 *****************************************************************************/
713 static int Open( vlc_object_t *p_this )
714 { /* initialisation of the connection */
715 intf_thread_t *p_intf = (intf_thread_t*)p_this;
716 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
717 playlist_t *p_playlist;
718 DBusConnection *p_conn;
724 dbus_threads_init_default();
726 dbus_error_init( &error );
728 /* connect to the session bus */
729 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
732 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
734 dbus_error_free( &error );
739 /* register a well-known name on the bus */
740 dbus_bus_request_name( p_conn, "org.mpris.vlc", 0, &error );
741 if( dbus_error_is_set( &error ) )
743 msg_Err( p_this, "Error requesting org.mpris.vlc service:" " %s\n", error.message );
744 dbus_error_free( &error );
749 /* we register the objects */
750 dbus_connection_register_object_path( p_conn, VLC_DBUS_ROOT_PATH,
751 &vlc_dbus_root_vtable, p_this );
752 dbus_connection_register_object_path( p_conn, VLC_DBUS_PLAYER_PATH,
753 &vlc_dbus_player_vtable, p_this );
754 dbus_connection_register_object_path( p_conn, VLC_DBUS_TRACKLIST_PATH,
755 &vlc_dbus_tracklist_vtable, p_this );
757 dbus_connection_flush( p_conn );
759 p_playlist = pl_Yield( p_intf );
761 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
763 pl_Release( p_playlist );
765 p_intf->pf_run = Run;
766 p_intf->p_sys = p_sys;
767 p_sys->p_conn = p_conn;
772 /*****************************************************************************
773 * Close: destroy interface
774 *****************************************************************************/
776 static void Close ( vlc_object_t *p_this )
778 intf_thread_t *p_intf = (intf_thread_t*) p_this;
779 playlist_t *p_playlist = pl_Yield( p_intf );;
782 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
784 pl_Release( p_playlist );
786 dbus_connection_unref( p_intf->p_sys->p_conn );
788 free( p_intf->p_sys );
791 /*****************************************************************************
793 *****************************************************************************/
795 static void Run ( intf_thread_t *p_intf )
797 while( !p_intf->b_die )
799 msleep( INTF_IDLE_SLEEP );
800 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
804 /*****************************************************************************
805 * TrackChange: Playlist item change callback
806 *****************************************************************************/
808 DBUS_SIGNAL( TrackChangeSignal )
809 { /* emit the metadata of the new item */
810 SIGNAL_INIT( "TrackChange" );
813 input_thread_t *p_input = (input_thread_t*) p_data;
814 GetInputMeta ( input_GetItem(p_input), &args );
819 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
820 vlc_value_t oldval, vlc_value_t newval, void *p_data )
822 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
823 intf_sys_t *p_sys = p_intf->p_sys;
824 playlist_t *p_playlist;
825 input_thread_t *p_input = NULL;
826 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
828 p_playlist = pl_Yield( p_intf );
830 p_input = p_playlist->p_input;
835 pl_Release( p_playlist );
839 vlc_object_yield( p_input );
841 pl_Release( p_playlist );
843 TrackChangeSignal( p_sys->p_conn, p_input );
845 vlc_object_release( p_input );
849 /*****************************************************************************
850 * GetInputMeta: Fill a DBusMessage with the given input item metadata
851 *****************************************************************************/
853 #define ADD_META( entry, type, data ) \
855 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
856 NULL, &dict_entry ); \
857 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
858 &ppsz_meta_items[entry] ); \
859 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
860 type##_AS_STRING, &variant ); \
861 dbus_message_iter_append_basic( &variant, \
864 dbus_message_iter_close_container( &dict_entry, &variant ); \
865 dbus_message_iter_close_container( &dict, &dict_entry ); }\
867 #define ADD_VLC_META_STRING( entry, item ) \
868 ADD_META( entry, DBUS_TYPE_STRING, \
869 p_input->p_meta->psz_##item );
871 static int GetInputMeta( input_item_t* p_input,
872 DBusMessageIter *args )
873 { /*FIXME: Works only for already read metadata. */
875 DBusMessageIter dict, dict_entry, variant;
876 /* We need the track length to be expressed in seconds
877 * instead of milliseconds */
878 dbus_int64_t i_length = (p_input->i_duration / 1000);
881 const char* ppsz_meta_items[] =
883 "title", "artist", "genre", "copyright", "album", "tracknum",
884 "description", "rating", "date", "setting", "url", "language",
885 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
886 "status", "URI", "length", "video-codec", "audio-codec",
887 "video-bitrate", "audio-bitrate", "audio-samplerate"
890 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
892 ADD_VLC_META_STRING( 0, title );
893 ADD_VLC_META_STRING( 1, artist );
894 ADD_VLC_META_STRING( 2, genre );
895 ADD_VLC_META_STRING( 3, copyright );
896 ADD_VLC_META_STRING( 4, album );
897 ADD_VLC_META_STRING( 5, tracknum );
898 ADD_VLC_META_STRING( 6, description );
899 ADD_VLC_META_STRING( 7, rating );
900 ADD_VLC_META_STRING( 8, date );
901 ADD_VLC_META_STRING( 9, setting );
902 ADD_VLC_META_STRING( 10, url );
903 ADD_VLC_META_STRING( 11, language );
904 ADD_VLC_META_STRING( 12, nowplaying );
905 ADD_VLC_META_STRING( 13, publisher );
906 ADD_VLC_META_STRING( 14, encodedby );
907 ADD_VLC_META_STRING( 15, arturl );
908 ADD_VLC_META_STRING( 16, trackid );
910 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
911 ADD_META( 18, DBUS_TYPE_STRING, p_input->psz_uri );
912 ADD_META( 19, DBUS_TYPE_INT64, i_length );
914 dbus_message_iter_close_container( args, &dict );
919 #undef ADD_VLC_META_STRING