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>
57 /*****************************************************************************
59 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close ( vlc_object_t * );
63 static void Run ( intf_thread_t * );
65 static int StateChange( vlc_object_t *, const char *, vlc_value_t,
66 vlc_value_t, void * );
68 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
69 vlc_value_t, void * );
71 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
72 vlc_value_t, void * );
74 static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
75 vlc_value_t, void * );
77 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
78 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, vlc_bool_t );
79 static int UpdateCaps( intf_thread_t*, vlc_bool_t );
81 /* GetCaps() capabilities */
85 CAPS_CAN_GO_NEXT = 1 << 0,
86 CAPS_CAN_GO_PREV = 1 << 1,
87 CAPS_CAN_PAUSE = 1 << 2,
88 CAPS_CAN_PLAY = 1 << 3,
89 CAPS_CAN_SEEK = 1 << 4,
90 CAPS_CAN_PROVIDE_METADATA = 1 << 5,
91 CAPS_CAN_HAS_TRACKLIST = 1 << 6
96 DBusConnection *p_conn;
97 vlc_bool_t b_meta_read;
101 /*****************************************************************************
103 *****************************************************************************/
106 set_shortname( _("dbus"));
107 set_category( CAT_INTERFACE );
108 set_subcategory( SUBCAT_INTERFACE_CONTROL );
109 set_description( _("D-Bus control interface") );
110 set_capability( "interface", 0 );
111 set_callbacks( Open, Close );
114 /*****************************************************************************
116 *****************************************************************************/
123 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
124 playlist_Stop( p_playlist );
125 pl_Release( ((vlc_object_t*) p_this) );
126 vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
130 DBUS_METHOD( MprisVersion )
131 { /*implemented version of the mpris spec */
134 VLC_UNUSED( p_this );
135 dbus_uint16_t i_major = VLC_MPRIS_VERSION_MAJOR;
136 dbus_uint16_t i_minor = VLC_MPRIS_VERSION_MINOR;
137 DBusMessageIter version;
139 dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL, &version );
140 dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16, &i_major );
141 dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16, &i_minor );
142 dbus_message_iter_close_container( &args, &version );
146 DBUS_METHOD( PositionGet )
147 { /* returns position in milliseconds */
150 vlc_value_t position;
153 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
155 input_thread_t *p_input = p_playlist->p_input;
161 var_Get( p_input, "time", &position );
162 i_pos = position.i_time / 1000;
165 pl_Release( ((vlc_object_t*) p_this) );
170 DBUS_METHOD( PositionSet )
171 { /* set position in milliseconds */
174 vlc_value_t position;
175 playlist_t* p_playlist = NULL;
179 dbus_error_init( &error );
181 dbus_message_get_args( p_from, &error,
182 DBUS_TYPE_INT32, &i_pos,
185 if( dbus_error_is_set( &error ) )
187 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
189 dbus_error_free( &error );
190 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
192 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
194 input_thread_t *p_input = p_playlist->p_input;
198 position.i_time = i_pos * 1000;
199 var_Set( p_input, "time", position );
202 pl_Release( ((vlc_object_t*) p_this) );
206 DBUS_METHOD( VolumeGet )
207 { /* returns volume in percentage */
210 dbus_int32_t i_dbus_vol;
211 audio_volume_t i_vol;
212 /* 2nd argument of aout_VolumeGet is int32 */
213 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
214 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
215 ADD_INT32( &i_dbus_vol );
219 DBUS_METHOD( VolumeSet )
220 { /* set volume in percentage */
224 dbus_error_init( &error );
226 dbus_int32_t i_dbus_vol;
227 audio_volume_t i_vol;
229 dbus_message_get_args( p_from, &error,
230 DBUS_TYPE_INT32, &i_dbus_vol,
233 if( dbus_error_is_set( &error ) )
235 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
237 dbus_error_free( &error );
238 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
241 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
242 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
248 { /* next playlist item */
250 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
251 playlist_Next( p_playlist );
252 pl_Release( ((vlc_object_t*) p_this) );
257 { /* previous playlist item */
259 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
260 playlist_Prev( p_playlist );
261 pl_Release( ((vlc_object_t*) p_this) );
268 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
269 playlist_Stop( p_playlist );
270 pl_Release( ((vlc_object_t*) p_this) );
274 DBUS_METHOD( GetStatus )
275 { /* returns the current status as a struct of 4 ints */
277 First 0 = Playing, 1 = Paused, 2 = Stopped.
278 Second 0 = Playing linearly , 1 = Playing randomly.
279 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
280 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
285 MarshalStatus( p_this, &args, VLC_TRUE );
293 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
294 playlist_Pause( p_playlist );
295 pl_Release( p_playlist );
302 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
303 playlist_Play( p_playlist );
304 pl_Release( p_playlist );
308 DBUS_METHOD( GetCurrentMetadata )
312 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
314 if( p_playlist->status.p_item )
315 GetInputMeta( p_playlist->status.p_item->p_input, &args );
317 pl_Release( p_playlist );
321 DBUS_METHOD( GetCaps )
326 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
331 /* Media Player information */
333 DBUS_METHOD( Identity )
339 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
341 ADD_STRING( &psz_identity );
342 free( psz_identity );
345 return DBUS_HANDLER_RESULT_NEED_MEMORY;
352 DBUS_METHOD( AddTrack )
353 { /* add the string to the playlist, and play it if the boolean is true */
357 dbus_error_init( &error );
358 playlist_t* p_playlist = NULL;
363 dbus_message_get_args( p_from, &error,
364 DBUS_TYPE_STRING, &psz_mrl,
365 DBUS_TYPE_BOOLEAN, &b_play,
368 if( dbus_error_is_set( &error ) )
370 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
372 dbus_error_free( &error );
373 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
376 p_playlist = pl_Yield( (vlc_object_t*) p_this );
377 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
378 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
379 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
380 pl_Release( p_playlist );
385 DBUS_METHOD( GetCurrentTrack )
390 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
391 dbus_int32_t i_position = p_playlist->i_current_index;
392 pl_Release( p_playlist );
394 ADD_INT32( &i_position );
398 DBUS_METHOD( GetMetadata )
403 dbus_error_init( &error );
405 dbus_int32_t i_position;
407 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
410 dbus_message_get_args( p_from, &error,
411 DBUS_TYPE_INT32, &i_position,
414 if( dbus_error_is_set( &error ) )
417 pl_Release( p_playlist );
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 if( i_position <= p_playlist->items.i_size / 2 )
426 GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
430 pl_Release( p_playlist );
434 DBUS_METHOD( GetLength )
439 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
440 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
441 pl_Release( p_playlist );
443 ADD_INT32( &i_elements );
447 DBUS_METHOD( DelTrack )
452 dbus_error_init( &error );
454 dbus_int32_t i_position;
455 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
457 dbus_message_get_args( p_from, &error,
458 DBUS_TYPE_INT32, &i_position,
461 if( dbus_error_is_set( &error ) )
463 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
465 dbus_error_free( &error );
466 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
469 if( i_position <= p_playlist->items.i_size / 2 )
471 playlist_DeleteFromInput( p_playlist,
472 p_playlist->items.p_elems[i_position*2-1]->i_id,
476 pl_Release( p_playlist );
489 playlist_t* p_playlist = NULL;
491 dbus_error_init( &error );
492 dbus_message_get_args( p_from, &error,
493 DBUS_TYPE_BOOLEAN, &b_loop,
496 if( dbus_error_is_set( &error ) )
498 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
500 dbus_error_free( &error );
501 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
504 val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
505 p_playlist = pl_Yield( (vlc_object_t*) p_this );
506 var_Set ( p_playlist, "loop", val );
507 pl_Release( ((vlc_object_t*) p_this) );
512 DBUS_METHOD( Repeat )
518 dbus_bool_t b_repeat;
520 playlist_t* p_playlist = NULL;
522 dbus_error_init( &error );
523 dbus_message_get_args( p_from, &error,
524 DBUS_TYPE_BOOLEAN, &b_repeat,
527 if( dbus_error_is_set( &error ) )
529 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
531 dbus_error_free( &error );
532 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
535 val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
537 p_playlist = pl_Yield( (vlc_object_t*) p_this );
538 var_Set ( p_playlist, "repeat", val );
539 pl_Release( ((vlc_object_t*) p_this) );
544 DBUS_METHOD( Random )
550 dbus_bool_t b_random;
552 playlist_t* p_playlist = NULL;
554 dbus_error_init( &error );
555 dbus_message_get_args( p_from, &error,
556 DBUS_TYPE_BOOLEAN, &b_random,
559 if( dbus_error_is_set( &error ) )
561 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
563 dbus_error_free( &error );
564 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
567 val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
569 p_playlist = pl_Yield( (vlc_object_t*) p_this );
570 var_Set ( p_playlist, "random", val );
571 pl_Release( ((vlc_object_t*) p_this) );
575 /*****************************************************************************
576 * Introspection method
577 *****************************************************************************/
579 DBUS_METHOD( handle_introspect_root )
580 { /* handles introspection of root object */
584 ADD_STRING( &psz_introspection_xml_data_root );
588 DBUS_METHOD( handle_introspect_player )
593 ADD_STRING( &psz_introspection_xml_data_player );
597 DBUS_METHOD( handle_introspect_tracklist )
602 ADD_STRING( &psz_introspection_xml_data_tracklist );
606 /*****************************************************************************
607 * handle_*: answer to incoming messages
608 *****************************************************************************/
610 #define METHOD_FUNC( method, function ) \
611 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
612 return function( p_conn, p_from, p_this )
614 DBUS_METHOD( handle_root )
617 if( dbus_message_is_method_call( p_from,
618 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
619 return handle_introspect_root( p_conn, p_from, p_this );
621 /* here D-Bus method's names are associated to an handler */
623 METHOD_FUNC( "Identity", Identity );
624 METHOD_FUNC( "MprisVersion", MprisVersion );
626 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
630 DBUS_METHOD( handle_player )
632 if( dbus_message_is_method_call( p_from,
633 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
634 return handle_introspect_player( p_conn, p_from, p_this );
636 /* here D-Bus method's names are associated to an handler */
638 METHOD_FUNC( "Prev", Prev );
639 METHOD_FUNC( "Next", Next );
640 METHOD_FUNC( "Quit", Quit );
641 METHOD_FUNC( "Stop", Stop );
642 METHOD_FUNC( "Play", Play );
643 METHOD_FUNC( "Pause", Pause );
644 METHOD_FUNC( "Repeat", Repeat );
645 METHOD_FUNC( "VolumeSet", VolumeSet );
646 METHOD_FUNC( "VolumeGet", VolumeGet );
647 METHOD_FUNC( "PositionSet", PositionSet );
648 METHOD_FUNC( "PositionGet", PositionGet );
649 METHOD_FUNC( "GetStatus", GetStatus );
650 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
651 METHOD_FUNC( "GetCaps", GetCaps );
653 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
656 DBUS_METHOD( handle_tracklist )
658 if( dbus_message_is_method_call( p_from,
659 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
660 return handle_introspect_tracklist( p_conn, p_from, p_this );
662 /* here D-Bus method's names are associated to an handler */
664 METHOD_FUNC( "GetMetadata", GetMetadata );
665 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
666 METHOD_FUNC( "GetLength", GetLength );
667 METHOD_FUNC( "AddTrack", AddTrack );
668 METHOD_FUNC( "DelTrack", DelTrack );
669 METHOD_FUNC( "Loop", Loop );
670 METHOD_FUNC( "Random", Random );
672 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
675 /*****************************************************************************
676 * Open: initialize interface
677 *****************************************************************************/
679 static int Open( vlc_object_t *p_this )
680 { /* initialisation of the connection */
681 intf_thread_t *p_intf = (intf_thread_t*)p_this;
682 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
683 playlist_t *p_playlist;
684 DBusConnection *p_conn;
690 p_sys->b_meta_read = VLC_FALSE;
691 p_sys->i_caps = CAPS_NONE;
693 dbus_error_init( &error );
695 /* connect to the session bus */
696 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
699 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
701 dbus_error_free( &error );
706 /* register a well-known name on the bus */
707 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
708 if( dbus_error_is_set( &error ) )
710 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
711 ": %s", error.message );
712 dbus_error_free( &error );
717 /* we register the objects */
718 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
719 &vlc_dbus_root_vtable, p_this );
720 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
721 &vlc_dbus_player_vtable, p_this );
722 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
723 &vlc_dbus_tracklist_vtable, p_this );
725 dbus_connection_flush( p_conn );
727 p_playlist = pl_Yield( p_intf );
729 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
730 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
731 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
732 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
733 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
734 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
735 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
737 pl_Release( p_playlist );
739 p_intf->pf_run = Run;
740 p_intf->p_sys = p_sys;
741 p_sys->p_conn = p_conn;
743 UpdateCaps( p_intf, VLC_FALSE );
748 /*****************************************************************************
749 * Close: destroy interface
750 *****************************************************************************/
752 static void Close ( vlc_object_t *p_this )
754 intf_thread_t *p_intf = (intf_thread_t*) p_this;
755 playlist_t *p_playlist = pl_Yield( p_intf );;
756 input_thread_t *p_input;
758 p_this->b_dead = VLC_TRUE;
761 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
762 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
763 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
764 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
765 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
766 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
767 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
769 p_input = p_playlist->p_input;
772 vlc_object_yield( p_input );
773 var_DelCallback( p_input, "state", StateChange, p_intf );
774 vlc_object_release( p_input );
778 pl_Release( p_playlist );
780 dbus_connection_unref( p_intf->p_sys->p_conn );
782 free( p_intf->p_sys );
785 /*****************************************************************************
787 *****************************************************************************/
789 static void Run ( intf_thread_t *p_intf )
791 while( !intf_ShouldDie( p_intf ) )
793 msleep( INTF_IDLE_SLEEP );
794 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
798 /******************************************************************************
799 * CapsChange: player capabilities change signal
800 *****************************************************************************/
801 DBUS_SIGNAL( CapsChangeSignal )
803 SIGNAL_INIT( "CapsChange" );
806 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
810 /******************************************************************************
811 * TrackListChange: tracklist order / length change signal
812 *****************************************************************************/
813 DBUS_SIGNAL( TrackListChangeSignal )
814 { /* emit the new tracklist lengh */
815 SIGNAL_INIT("TrackListChange");
818 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
819 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
820 pl_Release( p_playlist );
822 ADD_INT32( &i_elements );
826 /*****************************************************************************
827 * TrackListChangeEmit: Emits the TrackListChange signal
828 *****************************************************************************/
829 /* FIXME: It is not called on tracklist reordering */
830 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
831 vlc_value_t oldval, vlc_value_t newval, void *p_data )
834 intf_thread_t *p_intf = p_data;
836 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
838 /* don't signal when items are added/removed in p_category */
839 playlist_t *p_playlist = (playlist_t*)p_this;
840 playlist_add_t *p_add = newval.p_address;
841 playlist_item_t *p_item;
842 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, VLC_TRUE );
844 while( p_item->p_parent )
845 p_item = p_item->p_parent;
846 if( p_item == p_playlist->p_root_category )
853 UpdateCaps( p_intf, VLC_TRUE );
854 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
857 /*****************************************************************************
858 * TrackChange: Playlist item change callback
859 *****************************************************************************/
861 DBUS_SIGNAL( TrackChangeSignal )
862 { /* emit the metadata of the new item */
863 SIGNAL_INIT( "TrackChange" );
866 input_item_t *p_item = (input_item_t*) p_data;
867 GetInputMeta ( p_item, &args );
872 /*****************************************************************************
873 * StatusChange: Player status change signal
874 *****************************************************************************/
876 DBUS_SIGNAL( StatusChangeSignal )
877 { /* send the updated status info on the bus */
878 SIGNAL_INIT( "StatusChange" );
881 /* we're called from a callback of input_thread_t, so it can not be
882 * destroyed before we return */
883 MarshalStatus( (intf_thread_t*) p_data, &args, VLC_FALSE );
888 /*****************************************************************************
889 * StateChange: callback on input "state"
890 *****************************************************************************/
891 static int StateChange( vlc_object_t *p_this, const char* psz_var,
892 vlc_value_t oldval, vlc_value_t newval, void *p_data )
894 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
895 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
896 intf_sys_t *p_sys = p_intf->p_sys;
901 UpdateCaps( p_intf, VLC_FALSE );
903 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
905 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
908 p_sys->b_meta_read = VLC_TRUE;
909 TrackChangeSignal( p_sys->p_conn, p_item );
913 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
914 newval.i_int == END_S )
916 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
922 /*****************************************************************************
923 * StatusChangeEmit: Emits the StatusChange signal
924 *****************************************************************************/
925 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
926 vlc_value_t oldval, vlc_value_t newval, void *p_data )
928 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
929 VLC_UNUSED(oldval); VLC_UNUSED(newval);
930 intf_thread_t *p_intf = p_data;
935 UpdateCaps( p_intf, VLC_FALSE );
936 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
940 /*****************************************************************************
941 * TrackChange: callback on playlist "playlist-current"
942 *****************************************************************************/
943 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
944 vlc_value_t oldval, vlc_value_t newval, void *p_data )
946 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
947 intf_sys_t *p_sys = p_intf->p_sys;
948 playlist_t *p_playlist;
949 input_thread_t *p_input = NULL;
950 input_item_t *p_item = NULL;
951 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
952 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
957 p_sys->b_meta_read = VLC_FALSE;
959 p_playlist = pl_Yield( p_intf );
961 p_input = p_playlist->p_input;
966 pl_Release( p_playlist );
970 vlc_object_yield( p_input );
972 pl_Release( p_playlist );
974 p_item = input_GetItem( p_input );
977 vlc_object_release( p_input );
981 if( input_item_IsPreparsed( p_item ) )
983 p_sys->b_meta_read = VLC_TRUE;
984 TrackChangeSignal( p_sys->p_conn, p_item );
987 var_AddCallback( p_input, "state", StateChange, p_intf );
989 vlc_object_release( p_input );
993 /*****************************************************************************
994 * UpdateCaps: update p_sys->i_caps
995 ****************************************************************************/
996 static int UpdateCaps( intf_thread_t* p_intf, vlc_bool_t b_playlist_locked )
998 intf_sys_t* p_sys = p_intf->p_sys;
999 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1000 playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
1001 if( !b_playlist_locked ) PL_LOCK;
1003 if( p_playlist->items.i_size > 0 )
1004 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1006 if( p_playlist->p_input )
1008 /* XXX: if UpdateCaps() is called too early, these are
1009 * unconditionnaly true */
1010 if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1011 i_caps |= CAPS_CAN_PAUSE;
1012 if( var_GetBool( p_playlist->p_input, "seekable" ) )
1013 i_caps |= CAPS_CAN_SEEK;
1016 if( !b_playlist_locked ) PL_UNLOCK;
1017 pl_Release( p_playlist );
1019 if( p_sys->b_meta_read )
1020 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1022 if( i_caps != p_intf->p_sys->i_caps )
1024 p_sys->i_caps = i_caps;
1025 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1031 /*****************************************************************************
1032 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1033 *****************************************************************************/
1035 #define ADD_META( entry, type, data ) \
1037 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1038 NULL, &dict_entry ); \
1039 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1040 &ppsz_meta_items[entry] ); \
1041 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1042 type##_AS_STRING, &variant ); \
1043 dbus_message_iter_append_basic( &variant, \
1046 dbus_message_iter_close_container( &dict_entry, &variant ); \
1047 dbus_message_iter_close_container( &dict, &dict_entry ); }
1049 #define ADD_VLC_META_STRING( entry, item ) \
1051 char * psz = input_item_Get##item( p_input );\
1052 ADD_META( entry, DBUS_TYPE_STRING, \
1057 static int GetInputMeta( input_item_t* p_input,
1058 DBusMessageIter *args )
1060 DBusMessageIter dict, dict_entry, variant;
1061 /* We need the track length to be expressed in milli-seconds
1062 * instead of µ-seconds */
1063 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1065 const char* ppsz_meta_items[] =
1067 "title", "artist", "genre", "copyright", "album", "tracknum",
1068 "description", "rating", "date", "setting", "url", "language",
1069 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1070 "status", "URI", "length", "video-codec", "audio-codec",
1071 "video-bitrate", "audio-bitrate", "audio-samplerate"
1074 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1076 ADD_VLC_META_STRING( 0, Title );
1077 ADD_VLC_META_STRING( 1, Artist );
1078 ADD_VLC_META_STRING( 2, Genre );
1079 ADD_VLC_META_STRING( 3, Copyright );
1080 ADD_VLC_META_STRING( 4, Album );
1081 ADD_VLC_META_STRING( 5, TrackNum );
1082 ADD_VLC_META_STRING( 6, Description );
1083 ADD_VLC_META_STRING( 7, Rating );
1084 ADD_VLC_META_STRING( 8, Date );
1085 ADD_VLC_META_STRING( 9, Setting );
1086 ADD_VLC_META_STRING( 10, URL );
1087 ADD_VLC_META_STRING( 11, Language );
1088 ADD_VLC_META_STRING( 12, NowPlaying );
1089 ADD_VLC_META_STRING( 13, Publisher );
1090 ADD_VLC_META_STRING( 14, EncodedBy );
1091 ADD_VLC_META_STRING( 15, ArtURL );
1092 ADD_VLC_META_STRING( 16, TrackID );
1094 vlc_mutex_lock( &p_input->lock );
1095 if( p_input->p_meta )
1096 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1097 vlc_mutex_unlock( &p_input->lock );
1099 ADD_VLC_META_STRING( 18, URI );
1100 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1102 dbus_message_iter_close_container( args, &dict );
1107 #undef ADD_VLC_META_STRING
1109 /*****************************************************************************
1110 * MarshalStatus: Fill a DBusMessage with the current player status
1111 *****************************************************************************/
1113 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1115 { /* This is NOT the right way to do that, it would be better to sore
1116 the status information in p_sys and update it on change, thus
1117 avoiding a long lock */
1119 DBusMessageIter status;
1120 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1122 playlist_t* p_playlist = NULL;
1123 input_thread_t* p_input = NULL;
1125 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1131 p_input = p_playlist->p_input;
1134 var_Get( p_input, "state", &val );
1135 if( val.i_int >= END_S )
1137 else if( val.i_int == PAUSE_S )
1139 else if( val.i_int <= PLAYING_S )
1143 var_Get( p_playlist, "random", &val );
1144 i_random = val.i_int;
1146 var_Get( p_playlist, "repeat", &val );
1147 i_repeat = val.i_int;
1149 var_Get( p_playlist, "loop", &val );
1154 pl_Release( p_playlist );
1156 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1157 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1158 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1159 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1160 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1161 dbus_message_iter_close_container( args, &status );