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>
56 #include <vlc_demux.h>
57 #include <vlc_access.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 *, vlc_bool_t );
81 static int UpdateCaps( intf_thread_t* );
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;
99 vlc_bool_t b_meta_read;
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( PositionGet )
133 { /* returns position in milliseconds */
136 vlc_value_t position;
139 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
141 input_thread_t *p_input = p_playlist->p_input;
147 var_Get( p_input, "time", &position );
148 i_pos = position.i_time / 1000;
151 pl_Release( ((vlc_object_t*) p_this) );
156 DBUS_METHOD( PositionSet )
157 { /* set position in milliseconds */
160 vlc_value_t position;
161 playlist_t* p_playlist = NULL;
165 dbus_error_init( &error );
167 dbus_message_get_args( p_from, &error,
168 DBUS_TYPE_INT32, &i_pos,
171 if( dbus_error_is_set( &error ) )
173 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
175 dbus_error_free( &error );
176 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
178 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
180 input_thread_t *p_input = p_playlist->p_input;
184 position.i_time = i_pos * 1000;
185 var_Set( p_input, "time", position );
188 pl_Release( ((vlc_object_t*) p_this) );
192 DBUS_METHOD( VolumeGet )
193 { /* returns volume in percentage */
196 dbus_int32_t i_dbus_vol;
197 audio_volume_t i_vol;
198 /* 2nd argument of aout_VolumeGet is int32 */
199 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
200 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
201 ADD_INT32( &i_dbus_vol );
205 DBUS_METHOD( VolumeSet )
206 { /* set volume in percentage */
210 dbus_error_init( &error );
212 dbus_int32_t i_dbus_vol;
213 audio_volume_t i_vol;
215 dbus_message_get_args( p_from, &error,
216 DBUS_TYPE_INT32, &i_dbus_vol,
219 if( dbus_error_is_set( &error ) )
221 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
223 dbus_error_free( &error );
224 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
227 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
228 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
234 { /* next playlist item */
236 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
237 playlist_Next( p_playlist );
238 pl_Release( ((vlc_object_t*) p_this) );
243 { /* previous playlist item */
245 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
246 playlist_Prev( p_playlist );
247 pl_Release( ((vlc_object_t*) p_this) );
254 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
255 playlist_Stop( p_playlist );
256 pl_Release( ((vlc_object_t*) p_this) );
260 DBUS_METHOD( GetStatus )
261 { /* returns the current status as a struct of 4 ints */
263 First 0 = Playing, 1 = Paused, 2 = Stopped.
264 Second 0 = Playing linearly , 1 = Playing randomly.
265 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
266 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
271 MarshalStatus( p_this, &args, VLC_TRUE );
279 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
280 playlist_Pause( p_playlist );
281 pl_Release( p_playlist );
288 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
289 playlist_Play( p_playlist );
290 pl_Release( p_playlist );
294 DBUS_METHOD( GetCurrentMetadata )
298 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
300 if( p_playlist->status.p_item )
301 GetInputMeta( p_playlist->status.p_item->p_input, &args );
303 pl_Release( p_playlist );
307 DBUS_METHOD( GetCaps )
312 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
317 /* Media Player information */
319 DBUS_METHOD( Identity )
325 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
327 ADD_STRING( &psz_identity );
328 free( psz_identity );
331 return DBUS_HANDLER_RESULT_NEED_MEMORY;
338 DBUS_METHOD( AddTrack )
339 { /* add the string to the playlist, and play it if the boolean is true */
343 dbus_error_init( &error );
344 playlist_t* p_playlist = NULL;
349 dbus_message_get_args( p_from, &error,
350 DBUS_TYPE_STRING, &psz_mrl,
351 DBUS_TYPE_BOOLEAN, &b_play,
354 if( dbus_error_is_set( &error ) )
356 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
358 dbus_error_free( &error );
359 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
362 p_playlist = pl_Yield( (vlc_object_t*) p_this );
363 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
364 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
365 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
366 pl_Release( p_playlist );
371 DBUS_METHOD( GetCurrentTrack )
376 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
377 dbus_int32_t i_position = p_playlist->i_current_index;
378 pl_Release( p_playlist );
380 ADD_INT32( &i_position );
384 DBUS_METHOD( GetMetadata )
389 dbus_error_init( &error );
391 dbus_int32_t i_position;
393 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
396 dbus_message_get_args( p_from, &error,
397 DBUS_TYPE_INT32, &i_position,
400 if( dbus_error_is_set( &error ) )
403 pl_Release( p_playlist );
404 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
406 dbus_error_free( &error );
407 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
410 if( i_position <= p_playlist->items.i_size / 2 )
412 GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
416 pl_Release( p_playlist );
420 DBUS_METHOD( GetLength )
425 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
426 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
427 pl_Release( p_playlist );
429 ADD_INT32( &i_elements );
433 DBUS_METHOD( DelTrack )
438 dbus_error_init( &error );
440 dbus_int32_t i_position;
441 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
443 dbus_message_get_args( p_from, &error,
444 DBUS_TYPE_INT32, &i_position,
447 if( dbus_error_is_set( &error ) )
449 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
451 dbus_error_free( &error );
452 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
455 if( i_position <= p_playlist->items.i_size / 2 )
457 playlist_DeleteFromInput( p_playlist,
458 p_playlist->items.p_elems[i_position*2-1]->i_id,
462 pl_Release( p_playlist );
475 playlist_t* p_playlist = NULL;
477 dbus_error_init( &error );
478 dbus_message_get_args( p_from, &error,
479 DBUS_TYPE_BOOLEAN, &b_loop,
482 if( dbus_error_is_set( &error ) )
484 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
486 dbus_error_free( &error );
487 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
490 val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
491 p_playlist = pl_Yield( (vlc_object_t*) p_this );
492 var_Set ( p_playlist, "loop", val );
493 pl_Release( ((vlc_object_t*) p_this) );
498 DBUS_METHOD( Repeat )
504 dbus_bool_t b_repeat;
506 playlist_t* p_playlist = NULL;
508 dbus_error_init( &error );
509 dbus_message_get_args( p_from, &error,
510 DBUS_TYPE_BOOLEAN, &b_repeat,
513 if( dbus_error_is_set( &error ) )
515 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
517 dbus_error_free( &error );
518 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
521 val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
523 p_playlist = pl_Yield( (vlc_object_t*) p_this );
524 var_Set ( p_playlist, "repeat", val );
525 pl_Release( ((vlc_object_t*) p_this) );
530 DBUS_METHOD( Random )
536 dbus_bool_t b_random;
538 playlist_t* p_playlist = NULL;
540 dbus_error_init( &error );
541 dbus_message_get_args( p_from, &error,
542 DBUS_TYPE_BOOLEAN, &b_random,
545 if( dbus_error_is_set( &error ) )
547 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
549 dbus_error_free( &error );
550 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
553 val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
555 p_playlist = pl_Yield( (vlc_object_t*) p_this );
556 var_Set ( p_playlist, "random", val );
557 pl_Release( ((vlc_object_t*) p_this) );
561 /*****************************************************************************
562 * Introspection method
563 *****************************************************************************/
565 DBUS_METHOD( handle_introspect_root )
566 { /* handles introspection of root object */
570 ADD_STRING( &psz_introspection_xml_data_root );
574 DBUS_METHOD( handle_introspect_player )
579 ADD_STRING( &psz_introspection_xml_data_player );
583 DBUS_METHOD( handle_introspect_tracklist )
588 ADD_STRING( &psz_introspection_xml_data_tracklist );
592 /*****************************************************************************
593 * handle_*: answer to incoming messages
594 *****************************************************************************/
596 #define METHOD_FUNC( method, function ) \
597 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
598 return function( p_conn, p_from, p_this )
600 DBUS_METHOD( handle_root )
603 if( dbus_message_is_method_call( p_from,
604 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
605 return handle_introspect_root( p_conn, p_from, p_this );
607 /* here D-Bus method's names are associated to an handler */
609 METHOD_FUNC( "Identity", Identity );
611 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
615 DBUS_METHOD( handle_player )
617 if( dbus_message_is_method_call( p_from,
618 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
619 return handle_introspect_player( p_conn, p_from, p_this );
621 /* here D-Bus method's names are associated to an handler */
623 METHOD_FUNC( "Prev", Prev );
624 METHOD_FUNC( "Next", Next );
625 METHOD_FUNC( "Quit", Quit );
626 METHOD_FUNC( "Stop", Stop );
627 METHOD_FUNC( "Play", Play );
628 METHOD_FUNC( "Pause", Pause );
629 METHOD_FUNC( "Repeat", Repeat );
630 METHOD_FUNC( "VolumeSet", VolumeSet );
631 METHOD_FUNC( "VolumeGet", VolumeGet );
632 METHOD_FUNC( "PositionSet", PositionSet );
633 METHOD_FUNC( "PositionGet", PositionGet );
634 METHOD_FUNC( "GetStatus", GetStatus );
635 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
636 METHOD_FUNC( "GetCaps", GetCaps );
638 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
641 DBUS_METHOD( handle_tracklist )
643 if( dbus_message_is_method_call( p_from,
644 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
645 return handle_introspect_tracklist( p_conn, p_from, p_this );
647 /* here D-Bus method's names are associated to an handler */
649 METHOD_FUNC( "GetMetadata", GetMetadata );
650 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
651 METHOD_FUNC( "GetLength", GetLength );
652 METHOD_FUNC( "AddTrack", AddTrack );
653 METHOD_FUNC( "DelTrack", DelTrack );
654 METHOD_FUNC( "Loop", Loop );
655 METHOD_FUNC( "Random", Random );
657 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
660 /*****************************************************************************
661 * Open: initialize interface
662 *****************************************************************************/
664 static int Open( vlc_object_t *p_this )
665 { /* initialisation of the connection */
666 intf_thread_t *p_intf = (intf_thread_t*)p_this;
667 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
668 playlist_t *p_playlist;
669 DBusConnection *p_conn;
675 p_sys->b_meta_read = VLC_FALSE;
676 p_sys->i_caps = CAPS_NONE;
678 dbus_error_init( &error );
680 /* connect to the session bus */
681 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
684 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
686 dbus_error_free( &error );
691 /* register a well-known name on the bus */
692 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
693 if( dbus_error_is_set( &error ) )
695 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
696 ": %s", error.message );
697 dbus_error_free( &error );
702 /* we register the objects */
703 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
704 &vlc_dbus_root_vtable, p_this );
705 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
706 &vlc_dbus_player_vtable, p_this );
707 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
708 &vlc_dbus_tracklist_vtable, p_this );
710 dbus_connection_flush( p_conn );
712 p_playlist = pl_Yield( p_intf );
714 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
715 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
716 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
717 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
718 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
719 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
720 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
722 pl_Release( p_playlist );
724 p_intf->pf_run = Run;
725 p_intf->p_sys = p_sys;
726 p_sys->p_conn = p_conn;
728 UpdateCaps( p_intf );
733 /*****************************************************************************
734 * Close: destroy interface
735 *****************************************************************************/
737 static void Close ( vlc_object_t *p_this )
739 intf_thread_t *p_intf = (intf_thread_t*) p_this;
740 playlist_t *p_playlist = pl_Yield( p_intf );;
741 input_thread_t *p_input;
743 p_this->b_dead = VLC_TRUE;
746 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
747 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
748 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
749 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
750 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
751 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
752 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
754 p_input = p_playlist->p_input;
757 vlc_object_yield( p_input );
758 var_DelCallback( p_input, "state", StateChange, p_intf );
759 vlc_object_release( p_input );
763 pl_Release( p_playlist );
765 dbus_connection_unref( p_intf->p_sys->p_conn );
767 free( p_intf->p_sys );
770 /*****************************************************************************
772 *****************************************************************************/
774 static void Run ( intf_thread_t *p_intf )
776 while( !intf_ShouldDie( p_intf ) )
778 msleep( INTF_IDLE_SLEEP );
779 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
783 /******************************************************************************
784 * CapsChange: player capabilities change signal
785 *****************************************************************************/
786 DBUS_SIGNAL( CapsChangeSignal )
788 SIGNAL_INIT( "CapsChange" );
791 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
795 /******************************************************************************
796 * TrackListChange: tracklist order / length change signal
797 *****************************************************************************/
798 DBUS_SIGNAL( TrackListChangeSignal )
799 { /* emit the new tracklist lengh */
800 SIGNAL_INIT("TrackListChange");
803 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
804 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
805 pl_Release( p_playlist );
807 ADD_INT32( &i_elements );
811 /*****************************************************************************
812 * TrackListChangeEmit: Emits the TrackListChange signal
813 *****************************************************************************/
814 /* FIXME: It is not called on tracklist reordering */
815 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
816 vlc_value_t oldval, vlc_value_t newval, void *p_data )
819 intf_thread_t *p_intf = p_data;
821 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
823 /* don't signal when items are added/removed in p_category */
824 playlist_t *p_playlist = (playlist_t*)p_this;
825 playlist_add_t *p_add = newval.p_address;
826 playlist_item_t *p_item;
827 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, VLC_TRUE );
829 while( p_item->p_parent )
830 p_item = p_item->p_parent;
831 if( p_item == p_playlist->p_root_category )
838 /* We're called from the playlist, so that would cause locking issues */
839 /* UpdateCaps( p_intf ); */
840 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
843 /*****************************************************************************
844 * TrackChange: Playlist item change callback
845 *****************************************************************************/
847 DBUS_SIGNAL( TrackChangeSignal )
848 { /* emit the metadata of the new item */
849 SIGNAL_INIT( "TrackChange" );
852 input_item_t *p_item = (input_item_t*) p_data;
853 GetInputMeta ( p_item, &args );
858 /*****************************************************************************
859 * StatusChange: Player status change signal
860 *****************************************************************************/
862 DBUS_SIGNAL( StatusChangeSignal )
863 { /* send the updated status info on the bus */
864 SIGNAL_INIT( "StatusChange" );
867 /* we're called from a callback of input_thread_t, so it can not be
868 * destroyed before we return */
869 MarshalStatus( (intf_thread_t*) p_data, &args, VLC_FALSE );
874 /*****************************************************************************
875 * StateChange: callback on input "state"
876 *****************************************************************************/
877 static int StateChange( vlc_object_t *p_this, const char* psz_var,
878 vlc_value_t oldval, vlc_value_t newval, void *p_data )
880 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
881 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
882 intf_sys_t *p_sys = p_intf->p_sys;
887 UpdateCaps( p_intf );
889 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
891 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
894 p_sys->b_meta_read = VLC_TRUE;
895 TrackChangeSignal( p_sys->p_conn, p_item );
899 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
900 newval.i_int == END_S )
902 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
908 /*****************************************************************************
909 * StatusChangeEmit: Emits the StatusChange signal
910 *****************************************************************************/
911 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
912 vlc_value_t oldval, vlc_value_t newval, void *p_data )
914 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
915 VLC_UNUSED(oldval); VLC_UNUSED(newval);
916 intf_thread_t *p_intf = p_data;
921 UpdateCaps( p_intf );
922 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
926 /*****************************************************************************
927 * TrackChange: callback on playlist "playlist-current"
928 *****************************************************************************/
929 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
930 vlc_value_t oldval, vlc_value_t newval, void *p_data )
932 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
933 intf_sys_t *p_sys = p_intf->p_sys;
934 playlist_t *p_playlist;
935 input_thread_t *p_input = NULL;
936 input_item_t *p_item = NULL;
937 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
938 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
943 p_sys->b_meta_read = VLC_FALSE;
945 p_playlist = pl_Yield( p_intf );
947 p_input = p_playlist->p_input;
952 pl_Release( p_playlist );
956 vlc_object_yield( p_input );
958 pl_Release( p_playlist );
960 p_item = input_GetItem( p_input );
963 vlc_object_release( p_input );
967 if( input_item_IsPreparsed( p_item ) )
969 p_sys->b_meta_read = VLC_TRUE;
970 TrackChangeSignal( p_sys->p_conn, p_item );
973 var_AddCallback( p_input, "state", StateChange, p_intf );
975 vlc_object_release( p_input );
979 /*****************************************************************************
980 * UpdateCaps: update p_sys->i_caps
981 ****************************************************************************/
982 static int UpdateCaps( intf_thread_t* p_intf )
984 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
985 playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
988 if( p_playlist->items.i_size > 0 )
989 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
991 if( p_playlist->p_input )
993 access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input,
994 VLC_OBJECT_ACCESS, FIND_CHILD );
995 if( p_access && p_access->pf_control )
997 vlc_bool_t b_can_pause;
998 if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
1000 i_caps |= CAPS_CAN_PAUSE;
1001 vlc_object_release( p_access );
1003 demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
1004 VLC_OBJECT_DEMUX, FIND_CHILD );
1006 { /* XXX: is: demux can seek and access can not a possibility ? */
1007 vlc_bool_t b_can_seek;
1008 if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
1010 i_caps |= CAPS_CAN_SEEK;
1011 vlc_object_release( p_demux );
1016 pl_Release( p_playlist );
1018 if( p_intf->p_sys->b_meta_read )
1019 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1021 if( i_caps != p_intf->p_sys->i_caps )
1023 p_intf->p_sys->i_caps = i_caps;
1024 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1030 /*****************************************************************************
1031 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1032 *****************************************************************************/
1034 #define ADD_META( entry, type, data ) \
1036 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1037 NULL, &dict_entry ); \
1038 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1039 &ppsz_meta_items[entry] ); \
1040 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1041 type##_AS_STRING, &variant ); \
1042 dbus_message_iter_append_basic( &variant, \
1045 dbus_message_iter_close_container( &dict_entry, &variant ); \
1046 dbus_message_iter_close_container( &dict, &dict_entry ); }
1048 #define ADD_VLC_META_STRING( entry, item ) \
1050 char * psz = input_item_Get##item( p_input );\
1051 ADD_META( entry, DBUS_TYPE_STRING, \
1056 static int GetInputMeta( input_item_t* p_input,
1057 DBusMessageIter *args )
1059 DBusMessageIter dict, dict_entry, variant;
1060 /* We need the track length to be expressed in milli-seconds
1061 * instead of µ-seconds */
1062 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1064 const char* ppsz_meta_items[] =
1066 "title", "artist", "genre", "copyright", "album", "tracknum",
1067 "description", "rating", "date", "setting", "url", "language",
1068 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1069 "status", "URI", "length", "video-codec", "audio-codec",
1070 "video-bitrate", "audio-bitrate", "audio-samplerate"
1073 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1075 ADD_VLC_META_STRING( 0, Title );
1076 ADD_VLC_META_STRING( 1, Artist );
1077 ADD_VLC_META_STRING( 2, Genre );
1078 ADD_VLC_META_STRING( 3, Copyright );
1079 ADD_VLC_META_STRING( 4, Album );
1080 ADD_VLC_META_STRING( 5, TrackNum );
1081 ADD_VLC_META_STRING( 6, Description );
1082 ADD_VLC_META_STRING( 7, Rating );
1083 ADD_VLC_META_STRING( 8, Date );
1084 ADD_VLC_META_STRING( 9, Setting );
1085 ADD_VLC_META_STRING( 10, URL );
1086 ADD_VLC_META_STRING( 11, Language );
1087 ADD_VLC_META_STRING( 12, NowPlaying );
1088 ADD_VLC_META_STRING( 13, Publisher );
1089 ADD_VLC_META_STRING( 14, EncodedBy );
1090 ADD_VLC_META_STRING( 15, ArtURL );
1091 ADD_VLC_META_STRING( 16, TrackID );
1093 vlc_mutex_lock( &p_input->lock );
1094 if( p_input->p_meta )
1095 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1096 vlc_mutex_unlock( &p_input->lock );
1098 ADD_VLC_META_STRING( 18, URI );
1099 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1101 dbus_message_iter_close_container( args, &dict );
1106 #undef ADD_VLC_META_STRING
1108 /*****************************************************************************
1109 * MarshalStatus: Fill a DBusMessage with the current player status
1110 *****************************************************************************/
1112 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1114 { /* This is NOT the right way to do that, it would be better to sore
1115 the status information in p_sys and update it on change, thus
1116 avoiding a long lock */
1118 DBusMessageIter status;
1119 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1121 playlist_t* p_playlist = NULL;
1122 input_thread_t* p_input = NULL;
1124 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1130 p_input = p_playlist->p_input;
1133 var_Get( p_input, "state", &val );
1134 if( val.i_int >= END_S )
1136 else if( val.i_int == PAUSE_S )
1138 else if( val.i_int <= PLAYING_S )
1142 var_Get( p_playlist, "random", &val );
1143 i_random = val.i_int;
1145 var_Get( p_playlist, "repeat", &val );
1146 i_repeat = val.i_int;
1148 var_Get( p_playlist, "loop", &val );
1153 pl_Release( p_playlist );
1155 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1156 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1157 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1158 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1159 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1160 dbus_message_iter_close_container( args, &status );