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_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
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 ;
182 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;
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 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
208 input_thread_t *p_input = p_playlist->p_input;
212 position.f_float = ((float)i_pos) / 1000;
213 var_Set( p_input, "position", position );
215 pl_Release( ((vlc_object_t*) p_this) );
219 DBUS_METHOD( VolumeGet )
220 { /* returns volume in percentage */
223 dbus_int32_t i_dbus_vol;
224 audio_volume_t i_vol;
225 /* 2nd argument of aout_VolumeGet is int32 */
226 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
227 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
228 ADD_INT32( &i_dbus_vol );
232 DBUS_METHOD( VolumeSet )
233 { /* set volume in percentage */
237 dbus_error_init( &error );
239 dbus_int32_t i_dbus_vol;
240 audio_volume_t i_vol;
242 dbus_message_get_args( p_from, &error,
243 DBUS_TYPE_INT32, &i_dbus_vol,
246 if( dbus_error_is_set( &error ) )
248 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
250 dbus_error_free( &error );
251 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
254 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
255 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
261 { /* next playlist item */
263 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
264 playlist_Next( p_playlist );
265 pl_Release( ((vlc_object_t*) p_this) );
270 { /* previous playlist item */
272 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
273 playlist_Prev( p_playlist );
274 pl_Release( ((vlc_object_t*) p_this) );
281 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
282 playlist_Stop( p_playlist );
283 pl_Release( ((vlc_object_t*) p_this) );
287 DBUS_METHOD( GetStatus )
288 { /* returns an int: 0=playing 1=paused 2=stopped */
292 dbus_int32_t i_status;
295 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
296 input_thread_t *p_input = p_playlist->p_input;
301 var_Get( p_input, "state", &val );
302 if( val.i_int == PAUSE_S )
304 else if( val.i_int == PLAYING_S )
308 pl_Release( p_playlist );
310 ADD_INT32( &i_status );
317 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
318 playlist_Pause( p_playlist );
319 pl_Release( p_playlist );
326 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
327 playlist_Play( p_playlist );
328 pl_Release( p_playlist );
332 /* Media Player information */
334 DBUS_METHOD( Identity )
338 char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 1 );
339 sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
340 ADD_STRING( &psz_identity );
341 free( psz_identity );
347 DBUS_METHOD( AddTrack )
348 { /* add the string to the playlist, and play it if the boolean is true */
352 dbus_error_init( &error );
357 dbus_message_get_args( p_from, &error,
358 DBUS_TYPE_STRING, &psz_mrl,
359 DBUS_TYPE_BOOLEAN, &b_play,
362 if( dbus_error_is_set( &error ) )
364 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
366 dbus_error_free( &error );
367 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
370 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
371 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
372 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
373 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
374 pl_Release( p_playlist );
379 DBUS_METHOD( GetCurrentTrack )
383 dbus_int32_t i_position = 0;
384 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
385 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
387 while ( p_tested_item->p_input->i_id !=
388 p_playlist->status.p_item->p_input->i_id )
394 pl_Release( p_playlist );
396 ADD_INT32( &i_position );
400 DBUS_METHOD( GetMetadata )
405 dbus_error_init( &error );
407 dbus_int32_t i_position, i_count = 0;
409 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
410 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
412 dbus_message_get_args( p_from, &error,
413 DBUS_TYPE_INT32, &i_position,
416 if( dbus_error_is_set( &error ) )
418 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
420 dbus_error_free( &error );
421 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
424 while ( i_count < i_position )
430 GetInputMeta ( p_tested_item->p_input, &args );
432 pl_Release( p_playlist );
436 DBUS_METHOD( GetLength )
441 dbus_int32_t i_elements = 0;
442 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
443 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
444 playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist,
445 p_playlist->p_root_onelevel );
447 while ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id )
453 pl_Release( p_playlist );
455 ADD_INT32( &i_elements );
459 DBUS_METHOD( DelTrack )
464 dbus_error_init( &error );
466 dbus_int32_t i_position, i_count = 0;
467 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
468 playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
470 dbus_message_get_args( p_from, &error,
471 DBUS_TYPE_INT32, &i_position,
474 if( dbus_error_is_set( &error ) )
476 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
478 dbus_error_free( &error );
479 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
482 while ( i_count < i_position )
489 playlist_DeleteFromInput( p_playlist,
490 p_tested_item->p_input->i_id,
494 pl_Release( p_playlist );
499 /*****************************************************************************
500 * Introspection method
501 *****************************************************************************/
503 DBUS_METHOD( handle_introspect_root )
504 { /* handles introspection of /org/videolan/vlc */
507 ADD_STRING( &psz_introspection_xml_data_root );
511 DBUS_METHOD( handle_introspect_player )
515 ADD_STRING( &psz_introspection_xml_data_player );
519 DBUS_METHOD( handle_introspect_tracklist )
523 ADD_STRING( &psz_introspection_xml_data_tracklist );
527 /*****************************************************************************
528 * handle_*: answer to incoming messages
529 *****************************************************************************/
531 #define METHOD_FUNC( method, function ) \
532 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
533 return function( p_conn, p_from, p_this )
535 DBUS_METHOD( handle_root )
538 if( dbus_message_is_method_call( p_from,
539 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
540 return handle_introspect_root( p_conn, p_from, p_this );
542 /* here D-Bus method's names are associated to an handler */
544 METHOD_FUNC( "Identity", Identity );
546 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
550 DBUS_METHOD( handle_player )
552 if( dbus_message_is_method_call( p_from,
553 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
554 return handle_introspect_player( p_conn, p_from, p_this );
556 /* here D-Bus method's names are associated to an handler */
558 METHOD_FUNC( "Prev", Prev );
559 METHOD_FUNC( "Next", Next );
560 METHOD_FUNC( "Quit", Quit );
561 METHOD_FUNC( "Stop", Stop );
562 METHOD_FUNC( "Play", Play );
563 METHOD_FUNC( "Pause", Pause );
564 METHOD_FUNC( "VolumeSet", VolumeSet );
565 METHOD_FUNC( "VolumeGet", VolumeGet );
566 METHOD_FUNC( "PositionSet", PositionSet );
567 METHOD_FUNC( "PositionGet", PositionGet );
568 METHOD_FUNC( "GetStatus", GetStatus );
570 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
573 DBUS_METHOD( handle_tracklist )
575 if( dbus_message_is_method_call( p_from,
576 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
577 return handle_introspect_tracklist( p_conn, p_from, p_this );
579 /* here D-Bus method's names are associated to an handler */
581 METHOD_FUNC( "GetMetadata", GetMetadata );
582 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
583 METHOD_FUNC( "GetLength", GetLength );
584 METHOD_FUNC( "AddTrack", AddTrack );
585 METHOD_FUNC( "DelTrack", DelTrack );
587 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
590 /*****************************************************************************
591 * Open: initialize interface
592 *****************************************************************************/
594 static int Open( vlc_object_t *p_this )
595 { /* initialisation of the connection */
596 intf_thread_t *p_intf = (intf_thread_t*)p_this;
597 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
598 playlist_t *p_playlist;
599 DBusConnection *p_conn;
605 dbus_threads_init_default();
607 dbus_error_init( &error );
609 /* connect to the session bus */
610 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
613 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
615 dbus_error_free( &error );
620 /* register a well-known name on the bus */
621 dbus_bus_request_name( p_conn, "org.freedesktop.MediaPlayer", 0, &error );
622 if( dbus_error_is_set( &error ) )
624 msg_Err( p_this, "Error requesting org.freedesktop.MediaPlayer service:" " %s\n", error.message );
625 dbus_error_free( &error );
630 /* we register the objects */
631 dbus_connection_register_object_path( p_conn, VLC_DBUS_ROOT_PATH,
632 &vlc_dbus_root_vtable, p_this );
633 dbus_connection_register_object_path( p_conn, VLC_DBUS_PLAYER_PATH,
634 &vlc_dbus_player_vtable, p_this );
635 dbus_connection_register_object_path( p_conn, VLC_DBUS_TRACKLIST_PATH,
636 &vlc_dbus_tracklist_vtable, p_this );
638 dbus_connection_flush( p_conn );
640 p_playlist = pl_Yield( p_intf );
642 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
644 pl_Release( p_playlist );
646 p_intf->pf_run = Run;
647 p_intf->p_sys = p_sys;
648 p_sys->p_conn = p_conn;
653 /*****************************************************************************
654 * Close: destroy interface
655 *****************************************************************************/
657 static void Close ( vlc_object_t *p_this )
659 intf_thread_t *p_intf = (intf_thread_t*) p_this;
660 playlist_t *p_playlist = pl_Yield( p_intf );;
663 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
665 pl_Release( p_playlist );
667 dbus_connection_unref( p_intf->p_sys->p_conn );
669 free( p_intf->p_sys );
672 /*****************************************************************************
674 *****************************************************************************/
676 static void Run ( intf_thread_t *p_intf )
678 while( !p_intf->b_die )
680 msleep( INTF_IDLE_SLEEP );
681 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
685 /*****************************************************************************
686 * TrackChange: Playlist item change callback
687 *****************************************************************************/
689 DBUS_SIGNAL( TrackChangeSignal )
690 { /* emit the metadata of the new item */
691 SIGNAL_INIT( "TrackChange" );
694 input_thread_t *p_input = (input_thread_t*) p_data;
695 GetInputMeta ( input_GetItem(p_input), &args );
700 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
701 vlc_value_t oldval, vlc_value_t newval, void *p_data )
703 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
704 intf_sys_t *p_sys = p_intf->p_sys;
705 playlist_t *p_playlist;
706 input_thread_t *p_input = NULL;
707 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
709 p_playlist = pl_Yield( p_intf );
711 p_input = p_playlist->p_input;
716 pl_Release( p_playlist );
720 vlc_object_yield( p_input );
722 pl_Release( p_playlist );
724 TrackChangeSignal( p_sys->p_conn, p_input );
726 vlc_object_release( p_input );
730 /*****************************************************************************
731 * GetInputMeta: Fill a DBusMessage with the given input item metadata
732 *****************************************************************************/
734 #define ADD_META( entry, type, data ) \
736 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
737 NULL, &dict_entry ); \
738 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
739 &ppsz_meta_items[entry] ); \
740 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
741 type##_AS_STRING, &variant ); \
742 dbus_message_iter_append_basic( &variant, \
745 dbus_message_iter_close_container( &dict_entry, &variant ); \
746 dbus_message_iter_close_container( &dict, &dict_entry ); }\
748 #define ADD_VLC_META_STRING( entry, item ) \
749 ADD_META( entry, DBUS_TYPE_STRING, \
750 p_input->p_meta->psz_##item );
752 static int GetInputMeta( input_item_t* p_input,
753 DBusMessageIter *args )
754 { /*FIXME: Works only for already read metadata. */
755 /*FIXME: Should return the length in seconds rather than milliseconds */
757 DBusMessageIter dict, dict_entry, variant;
759 const char* ppsz_meta_items[] =
761 "title", "artist", "genre", "copyright", "album", "tracknum",
762 "description", "rating", "date", "setting", "url", "language",
763 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
764 "status", "URI", "length", "video-codec", "audio-codec",
765 "video-bitrate", "audio-bitrate", "audio-samplerate"
768 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
770 ADD_VLC_META_STRING( 0, title );
771 ADD_VLC_META_STRING( 1, artist );
772 ADD_VLC_META_STRING( 2, genre );
773 ADD_VLC_META_STRING( 3, copyright );
774 ADD_VLC_META_STRING( 4, album );
775 ADD_VLC_META_STRING( 5, tracknum );
776 ADD_VLC_META_STRING( 6, description );
777 ADD_VLC_META_STRING( 7, rating );
778 ADD_VLC_META_STRING( 8, date );
779 ADD_VLC_META_STRING( 9, setting );
780 ADD_VLC_META_STRING( 10, url );
781 ADD_VLC_META_STRING( 11, language );
782 ADD_VLC_META_STRING( 12, nowplaying );
783 ADD_VLC_META_STRING( 13, publisher );
784 ADD_VLC_META_STRING( 14, encodedby );
785 ADD_VLC_META_STRING( 15, arturl );
786 ADD_VLC_META_STRING( 16, trackid );
788 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
789 ADD_META( 18, DBUS_TYPE_STRING, p_input->psz_uri );
790 ADD_META( 19, DBUS_TYPE_INT64, p_input->i_duration );
792 dbus_message_iter_close_container( args, &dict );
797 #undef ADD_VLC_META_STRING