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>
52 #include <vlc_interface.h>
54 #include <vlc_input.h>
55 #include <vlc_playlist.h>
59 /*****************************************************************************
61 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
65 static void Run ( intf_thread_t * );
67 static int StateChange( vlc_object_t *, const char *, vlc_value_t,
68 vlc_value_t, void * );
70 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
71 vlc_value_t, void * );
73 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
74 vlc_value_t, void * );
76 static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
77 vlc_value_t, void * );
79 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
80 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, bool );
81 static int UpdateCaps( intf_thread_t*, bool );
83 /* GetCaps() capabilities */
87 CAPS_CAN_GO_NEXT = 1 << 0,
88 CAPS_CAN_GO_PREV = 1 << 1,
89 CAPS_CAN_PAUSE = 1 << 2,
90 CAPS_CAN_PLAY = 1 << 3,
91 CAPS_CAN_SEEK = 1 << 4,
92 CAPS_CAN_PROVIDE_METADATA = 1 << 5,
93 CAPS_CAN_HAS_TRACKLIST = 1 << 6
98 DBusConnection *p_conn;
103 /*****************************************************************************
105 *****************************************************************************/
108 set_shortname( _("dbus"));
109 set_category( CAT_INTERFACE );
110 set_subcategory( SUBCAT_INTERFACE_CONTROL );
111 set_description( _("D-Bus control interface") );
112 set_capability( "interface", 0 );
113 set_callbacks( Open, Close );
116 /*****************************************************************************
118 *****************************************************************************/
125 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
126 playlist_Stop( p_playlist );
127 pl_Release( ((vlc_object_t*) p_this) );
128 vlc_object_kill(((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_Yield( ((vlc_object_t*) p_this) );
167 input_thread_t *p_input = p_playlist->p_input;
173 var_Get( p_input, "time", &position );
174 i_pos = position.i_time / 1000;
177 pl_Release( ((vlc_object_t*) p_this) );
182 DBUS_METHOD( PositionSet )
183 { /* set position in milliseconds */
186 vlc_value_t position;
187 playlist_t* p_playlist = NULL;
191 dbus_error_init( &error );
193 dbus_message_get_args( p_from, &error,
194 DBUS_TYPE_INT32, &i_pos,
197 if( dbus_error_is_set( &error ) )
199 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
201 dbus_error_free( &error );
202 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
204 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
206 input_thread_t *p_input = p_playlist->p_input;
210 position.i_time = i_pos * 1000;
211 var_Set( p_input, "time", position );
214 pl_Release( ((vlc_object_t*) p_this) );
218 DBUS_METHOD( VolumeGet )
219 { /* returns volume in percentage */
222 dbus_int32_t i_dbus_vol;
223 audio_volume_t i_vol;
224 /* 2nd argument of aout_VolumeGet is int32 */
225 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
226 double f_vol = 100. * i_vol / AOUT_VOLUME_MAX;
227 i_dbus_vol = round( f_vol );
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 double f_vol = AOUT_VOLUME_MAX * i_dbus_vol / 100.;
255 i_vol = round( f_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 the current status as a struct of 4 ints */
291 First 0 = Playing, 1 = Paused, 2 = Stopped.
292 Second 0 = Playing linearly , 1 = Playing randomly.
293 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
294 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
299 MarshalStatus( p_this, &args, true );
307 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
308 playlist_Pause( p_playlist );
309 pl_Release( p_playlist );
316 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
317 playlist_Play( p_playlist );
318 pl_Release( p_playlist );
322 DBUS_METHOD( GetCurrentMetadata )
326 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
328 if( p_playlist->status.p_item )
329 GetInputMeta( p_playlist->status.p_item->p_input, &args );
331 pl_Release( p_playlist );
335 DBUS_METHOD( GetCaps )
340 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
345 /* Media Player information */
347 DBUS_METHOD( Identity )
353 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
355 ADD_STRING( &psz_identity );
356 free( psz_identity );
359 return DBUS_HANDLER_RESULT_NEED_MEMORY;
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, true, false );
394 pl_Release( p_playlist );
399 DBUS_METHOD( GetCurrentTrack )
404 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
405 dbus_int32_t i_position = p_playlist->i_current_index;
406 pl_Release( p_playlist );
408 ADD_INT32( &i_position );
412 DBUS_METHOD( GetMetadata )
417 dbus_error_init( &error );
419 dbus_int32_t i_position;
421 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
424 dbus_message_get_args( p_from, &error,
425 DBUS_TYPE_INT32, &i_position,
428 if( dbus_error_is_set( &error ) )
431 pl_Release( p_playlist );
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 if( i_position <= p_playlist->items.i_size / 2 )
440 GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
444 pl_Release( p_playlist );
448 DBUS_METHOD( GetLength )
453 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
454 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
455 pl_Release( p_playlist );
457 ADD_INT32( &i_elements );
461 DBUS_METHOD( DelTrack )
466 dbus_error_init( &error );
468 dbus_int32_t i_position;
469 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
471 dbus_message_get_args( p_from, &error,
472 DBUS_TYPE_INT32, &i_position,
475 if( dbus_error_is_set( &error ) )
477 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
479 dbus_error_free( &error );
480 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
483 if( i_position <= p_playlist->items.i_size / 2 )
485 playlist_DeleteFromInput( p_playlist,
486 p_playlist->items.p_elems[i_position*2-1]->i_id,
490 pl_Release( p_playlist );
495 DBUS_METHOD( SetLoop )
503 playlist_t* p_playlist = NULL;
505 dbus_error_init( &error );
506 dbus_message_get_args( p_from, &error,
507 DBUS_TYPE_BOOLEAN, &b_loop,
510 if( dbus_error_is_set( &error ) )
512 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
514 dbus_error_free( &error );
515 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
518 val.b_bool = ( b_loop == TRUE ) ? true : false ;
519 p_playlist = pl_Yield( (vlc_object_t*) p_this );
520 var_Set ( p_playlist, "loop", val );
521 pl_Release( ((vlc_object_t*) p_this) );
526 DBUS_METHOD( Repeat )
532 dbus_bool_t b_repeat;
534 playlist_t* p_playlist = NULL;
536 dbus_error_init( &error );
537 dbus_message_get_args( p_from, &error,
538 DBUS_TYPE_BOOLEAN, &b_repeat,
541 if( dbus_error_is_set( &error ) )
543 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
545 dbus_error_free( &error );
546 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
549 val.b_bool = ( b_repeat == TRUE ) ? true : false ;
551 p_playlist = pl_Yield( (vlc_object_t*) p_this );
552 var_Set ( p_playlist, "repeat", val );
553 pl_Release( ((vlc_object_t*) p_this) );
558 DBUS_METHOD( SetRandom )
564 dbus_bool_t b_random;
566 playlist_t* p_playlist = NULL;
568 dbus_error_init( &error );
569 dbus_message_get_args( p_from, &error,
570 DBUS_TYPE_BOOLEAN, &b_random,
573 if( dbus_error_is_set( &error ) )
575 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
577 dbus_error_free( &error );
578 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
581 val.b_bool = ( b_random == TRUE ) ? true : false ;
583 p_playlist = pl_Yield( (vlc_object_t*) p_this );
584 var_Set ( p_playlist, "random", val );
585 pl_Release( ((vlc_object_t*) p_this) );
589 /*****************************************************************************
590 * Introspection method
591 *****************************************************************************/
593 DBUS_METHOD( handle_introspect_root )
594 { /* handles introspection of root object */
598 ADD_STRING( &psz_introspection_xml_data_root );
602 DBUS_METHOD( handle_introspect_player )
607 ADD_STRING( &psz_introspection_xml_data_player );
611 DBUS_METHOD( handle_introspect_tracklist )
616 ADD_STRING( &psz_introspection_xml_data_tracklist );
620 /*****************************************************************************
621 * handle_*: answer to incoming messages
622 *****************************************************************************/
624 #define METHOD_FUNC( method, function ) \
625 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
626 return function( p_conn, p_from, p_this )
628 DBUS_METHOD( handle_root )
631 if( dbus_message_is_method_call( p_from,
632 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
633 return handle_introspect_root( p_conn, p_from, p_this );
635 /* here D-Bus method's names are associated to an handler */
637 METHOD_FUNC( "Identity", Identity );
638 METHOD_FUNC( "MprisVersion", MprisVersion );
639 METHOD_FUNC( "Quit", Quit );
641 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
645 DBUS_METHOD( handle_player )
647 if( dbus_message_is_method_call( p_from,
648 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
649 return handle_introspect_player( p_conn, p_from, p_this );
651 /* here D-Bus method's names are associated to an handler */
653 METHOD_FUNC( "Prev", Prev );
654 METHOD_FUNC( "Next", Next );
655 METHOD_FUNC( "Stop", Stop );
656 METHOD_FUNC( "Play", Play );
657 METHOD_FUNC( "Pause", Pause );
658 METHOD_FUNC( "Repeat", Repeat );
659 METHOD_FUNC( "VolumeSet", VolumeSet );
660 METHOD_FUNC( "VolumeGet", VolumeGet );
661 METHOD_FUNC( "PositionSet", PositionSet );
662 METHOD_FUNC( "PositionGet", PositionGet );
663 METHOD_FUNC( "GetStatus", GetStatus );
664 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
665 METHOD_FUNC( "GetCaps", GetCaps );
667 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
670 DBUS_METHOD( handle_tracklist )
672 if( dbus_message_is_method_call( p_from,
673 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
674 return handle_introspect_tracklist( p_conn, p_from, p_this );
676 /* here D-Bus method's names are associated to an handler */
678 METHOD_FUNC( "GetMetadata", GetMetadata );
679 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
680 METHOD_FUNC( "GetLength", GetLength );
681 METHOD_FUNC( "AddTrack", AddTrack );
682 METHOD_FUNC( "DelTrack", DelTrack );
683 METHOD_FUNC( "SetLoop", SetLoop );
684 METHOD_FUNC( "SetRandom", SetRandom );
686 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
689 /*****************************************************************************
690 * Open: initialize interface
691 *****************************************************************************/
693 static int Open( vlc_object_t *p_this )
694 { /* initialisation of the connection */
695 intf_thread_t *p_intf = (intf_thread_t*)p_this;
696 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
697 playlist_t *p_playlist;
698 DBusConnection *p_conn;
704 p_sys->b_meta_read = false;
705 p_sys->i_caps = CAPS_NONE;
707 dbus_error_init( &error );
709 /* connect to the session bus */
710 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
713 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
715 dbus_error_free( &error );
720 /* register a well-known name on the bus */
721 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
722 if( dbus_error_is_set( &error ) )
724 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
725 ": %s", error.message );
726 dbus_error_free( &error );
731 /* we register the objects */
732 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
733 &vlc_dbus_root_vtable, p_this );
734 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
735 &vlc_dbus_player_vtable, p_this );
736 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
737 &vlc_dbus_tracklist_vtable, p_this );
739 dbus_connection_flush( p_conn );
741 p_playlist = pl_Yield( p_intf );
743 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
744 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
745 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
746 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
747 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
748 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
749 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
751 pl_Release( p_playlist );
753 p_intf->pf_run = Run;
754 p_intf->p_sys = p_sys;
755 p_sys->p_conn = p_conn;
757 UpdateCaps( p_intf, false );
762 /*****************************************************************************
763 * Close: destroy interface
764 *****************************************************************************/
766 static void Close ( vlc_object_t *p_this )
768 intf_thread_t *p_intf = (intf_thread_t*) p_this;
769 playlist_t *p_playlist = pl_Yield( p_intf );;
770 input_thread_t *p_input;
772 p_this->b_dead = true;
775 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
776 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
777 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
778 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
779 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
780 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
781 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
783 p_input = p_playlist->p_input;
786 vlc_object_yield( p_input );
787 var_DelCallback( p_input, "state", StateChange, p_intf );
788 vlc_object_release( p_input );
792 pl_Release( p_playlist );
794 dbus_connection_unref( p_intf->p_sys->p_conn );
796 free( p_intf->p_sys );
799 /*****************************************************************************
801 *****************************************************************************/
803 static void Run ( intf_thread_t *p_intf )
805 while( !intf_ShouldDie( p_intf ) )
807 msleep( INTF_IDLE_SLEEP );
808 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
812 /******************************************************************************
813 * CapsChange: player capabilities change signal
814 *****************************************************************************/
815 DBUS_SIGNAL( CapsChangeSignal )
817 SIGNAL_INIT( "CapsChange" );
820 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
824 /******************************************************************************
825 * TrackListChange: tracklist order / length change signal
826 *****************************************************************************/
827 DBUS_SIGNAL( TrackListChangeSignal )
828 { /* emit the new tracklist lengh */
829 SIGNAL_INIT("TrackListChange");
832 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
833 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
834 pl_Release( p_playlist );
836 ADD_INT32( &i_elements );
840 /*****************************************************************************
841 * TrackListChangeEmit: Emits the TrackListChange signal
842 *****************************************************************************/
843 /* FIXME: It is not called on tracklist reordering */
844 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
845 vlc_value_t oldval, vlc_value_t newval, void *p_data )
848 intf_thread_t *p_intf = p_data;
850 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
852 /* don't signal when items are added/removed in p_category */
853 playlist_t *p_playlist = (playlist_t*)p_this;
854 playlist_add_t *p_add = newval.p_address;
855 playlist_item_t *p_item;
856 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, true );
858 while( p_item->p_parent )
859 p_item = p_item->p_parent;
860 if( p_item == p_playlist->p_root_category )
867 UpdateCaps( p_intf, true );
868 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
871 /*****************************************************************************
872 * TrackChange: Playlist item change callback
873 *****************************************************************************/
875 DBUS_SIGNAL( TrackChangeSignal )
876 { /* emit the metadata of the new item */
877 SIGNAL_INIT( "TrackChange" );
880 input_item_t *p_item = (input_item_t*) p_data;
881 GetInputMeta ( p_item, &args );
886 /*****************************************************************************
887 * StatusChange: Player status change signal
888 *****************************************************************************/
890 DBUS_SIGNAL( StatusChangeSignal )
891 { /* send the updated status info on the bus */
892 SIGNAL_INIT( "StatusChange" );
895 /* we're called from a callback of input_thread_t, so it can not be
896 * destroyed before we return */
897 MarshalStatus( (intf_thread_t*) p_data, &args, false );
902 /*****************************************************************************
903 * StateChange: callback on input "state"
904 *****************************************************************************/
905 static int StateChange( vlc_object_t *p_this, const char* psz_var,
906 vlc_value_t oldval, vlc_value_t newval, void *p_data )
908 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
909 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
910 intf_sys_t *p_sys = p_intf->p_sys;
915 UpdateCaps( p_intf, true );
917 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
919 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
922 p_sys->b_meta_read = true;
923 TrackChangeSignal( p_sys->p_conn, p_item );
927 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
928 newval.i_int == END_S )
930 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
936 /*****************************************************************************
937 * StatusChangeEmit: Emits the StatusChange signal
938 *****************************************************************************/
939 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
940 vlc_value_t oldval, vlc_value_t newval, void *p_data )
942 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
943 VLC_UNUSED(oldval); VLC_UNUSED(newval);
944 intf_thread_t *p_intf = p_data;
949 UpdateCaps( p_intf, false );
950 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
954 /*****************************************************************************
955 * TrackChange: callback on playlist "playlist-current"
956 *****************************************************************************/
957 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
958 vlc_value_t oldval, vlc_value_t newval, void *p_data )
960 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
961 intf_sys_t *p_sys = p_intf->p_sys;
962 playlist_t *p_playlist;
963 input_thread_t *p_input = NULL;
964 input_item_t *p_item = NULL;
965 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
966 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
971 p_sys->b_meta_read = false;
973 p_playlist = pl_Yield( p_intf );
974 p_input = p_playlist->p_input;
979 pl_Release( p_playlist );
983 vlc_object_yield( p_input );
984 pl_Release( p_playlist );
986 p_item = input_GetItem( p_input );
989 vlc_object_release( p_input );
993 if( input_item_IsPreparsed( p_item ) )
995 p_sys->b_meta_read = true;
996 TrackChangeSignal( p_sys->p_conn, p_item );
999 var_AddCallback( p_input, "state", StateChange, p_intf );
1001 vlc_object_release( p_input );
1005 /*****************************************************************************
1006 * UpdateCaps: update p_sys->i_caps
1007 ****************************************************************************/
1008 static int UpdateCaps( intf_thread_t* p_intf, bool b_playlist_locked )
1010 intf_sys_t* p_sys = p_intf->p_sys;
1011 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1012 playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
1013 if( !b_playlist_locked ) PL_LOCK;
1015 if( p_playlist->items.i_size > 0 )
1016 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1018 if( p_playlist->p_input )
1020 /* XXX: if UpdateCaps() is called too early, these are
1021 * unconditionnaly true */
1022 if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1023 i_caps |= CAPS_CAN_PAUSE;
1024 if( var_GetBool( p_playlist->p_input, "seekable" ) )
1025 i_caps |= CAPS_CAN_SEEK;
1028 if( !b_playlist_locked ) PL_UNLOCK;
1029 pl_Release( p_playlist );
1031 if( p_sys->b_meta_read )
1032 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1034 if( i_caps != p_intf->p_sys->i_caps )
1036 p_sys->i_caps = i_caps;
1037 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1043 /*****************************************************************************
1044 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1045 *****************************************************************************/
1047 #define ADD_META( entry, type, data ) \
1049 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1050 NULL, &dict_entry ); \
1051 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1052 &ppsz_meta_items[entry] ); \
1053 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1054 type##_AS_STRING, &variant ); \
1055 dbus_message_iter_append_basic( &variant, \
1058 dbus_message_iter_close_container( &dict_entry, &variant ); \
1059 dbus_message_iter_close_container( &dict, &dict_entry ); }
1061 #define ADD_VLC_META_STRING( entry, item ) \
1063 char * psz = input_item_Get##item( p_input );\
1064 ADD_META( entry, DBUS_TYPE_STRING, \
1069 static int GetInputMeta( input_item_t* p_input,
1070 DBusMessageIter *args )
1072 DBusMessageIter dict, dict_entry, variant;
1073 /* We need the track length to be expressed in milli-seconds
1074 * instead of µ-seconds */
1075 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1077 const char* ppsz_meta_items[] =
1079 "title", "artist", "genre", "copyright", "album", "tracknum",
1080 "description", "rating", "date", "setting", "url", "language",
1081 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1082 "status", "URI", "length", "video-codec", "audio-codec",
1083 "video-bitrate", "audio-bitrate", "audio-samplerate"
1086 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1088 ADD_VLC_META_STRING( 0, Title );
1089 ADD_VLC_META_STRING( 1, Artist );
1090 ADD_VLC_META_STRING( 2, Genre );
1091 ADD_VLC_META_STRING( 3, Copyright );
1092 ADD_VLC_META_STRING( 4, Album );
1093 ADD_VLC_META_STRING( 5, TrackNum );
1094 ADD_VLC_META_STRING( 6, Description );
1095 ADD_VLC_META_STRING( 7, Rating );
1096 ADD_VLC_META_STRING( 8, Date );
1097 ADD_VLC_META_STRING( 9, Setting );
1098 ADD_VLC_META_STRING( 10, URL );
1099 ADD_VLC_META_STRING( 11, Language );
1100 ADD_VLC_META_STRING( 12, NowPlaying );
1101 ADD_VLC_META_STRING( 13, Publisher );
1102 ADD_VLC_META_STRING( 14, EncodedBy );
1103 ADD_VLC_META_STRING( 15, ArtURL );
1104 ADD_VLC_META_STRING( 16, TrackID );
1106 vlc_mutex_lock( &p_input->lock );
1107 if( p_input->p_meta )
1108 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1109 vlc_mutex_unlock( &p_input->lock );
1111 ADD_VLC_META_STRING( 18, URI );
1112 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1114 dbus_message_iter_close_container( args, &dict );
1119 #undef ADD_VLC_META_STRING
1121 /*****************************************************************************
1122 * MarshalStatus: Fill a DBusMessage with the current player status
1123 *****************************************************************************/
1125 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1127 { /* This is NOT the right way to do that, it would be better to sore
1128 the status information in p_sys and update it on change, thus
1129 avoiding a long lock */
1131 DBusMessageIter status;
1132 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1134 playlist_t* p_playlist = NULL;
1135 input_thread_t* p_input = NULL;
1137 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1143 p_input = p_playlist->p_input;
1146 var_Get( p_input, "state", &val );
1147 if( val.i_int >= END_S )
1149 else if( val.i_int == PAUSE_S )
1151 else if( val.i_int <= PLAYING_S )
1155 i_random = var_CreateGetBool( p_playlist, "random" );
1157 i_repeat = var_CreateGetBool( p_playlist, "repeat" );
1159 i_loop = var_CreateGetBool( p_playlist, "loop" );
1163 pl_Release( p_playlist );
1165 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1166 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1167 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1168 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1169 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1170 dbus_message_iter_close_container( args, &status );