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->current.i_size )
440 GetInputMeta( p_playlist->current.p_elems[i_position]->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->current.i_size;
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;
484 if( i_position < p_playlist->current.i_size )
486 playlist_DeleteFromInput( p_playlist,
487 p_playlist->current.p_elems[i_position]->p_input->i_id,
492 pl_Release( p_playlist );
497 DBUS_METHOD( SetLoop )
505 playlist_t* p_playlist = NULL;
507 dbus_error_init( &error );
508 dbus_message_get_args( p_from, &error,
509 DBUS_TYPE_BOOLEAN, &b_loop,
512 if( dbus_error_is_set( &error ) )
514 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
516 dbus_error_free( &error );
517 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
520 val.b_bool = ( b_loop == TRUE ) ? true : false ;
521 p_playlist = pl_Yield( (vlc_object_t*) p_this );
522 var_Set ( p_playlist, "loop", val );
523 pl_Release( ((vlc_object_t*) p_this) );
528 DBUS_METHOD( Repeat )
534 dbus_bool_t b_repeat;
536 playlist_t* p_playlist = NULL;
538 dbus_error_init( &error );
539 dbus_message_get_args( p_from, &error,
540 DBUS_TYPE_BOOLEAN, &b_repeat,
543 if( dbus_error_is_set( &error ) )
545 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
547 dbus_error_free( &error );
548 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
551 val.b_bool = ( b_repeat == TRUE ) ? true : false ;
553 p_playlist = pl_Yield( (vlc_object_t*) p_this );
554 var_Set ( p_playlist, "repeat", val );
555 pl_Release( ((vlc_object_t*) p_this) );
560 DBUS_METHOD( SetRandom )
566 dbus_bool_t b_random;
568 playlist_t* p_playlist = NULL;
570 dbus_error_init( &error );
571 dbus_message_get_args( p_from, &error,
572 DBUS_TYPE_BOOLEAN, &b_random,
575 if( dbus_error_is_set( &error ) )
577 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
579 dbus_error_free( &error );
580 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
583 val.b_bool = ( b_random == TRUE ) ? true : false ;
585 p_playlist = pl_Yield( (vlc_object_t*) p_this );
586 var_Set ( p_playlist, "random", val );
587 pl_Release( ((vlc_object_t*) p_this) );
591 /*****************************************************************************
592 * Introspection method
593 *****************************************************************************/
595 DBUS_METHOD( handle_introspect_root )
596 { /* handles introspection of root object */
600 ADD_STRING( &psz_introspection_xml_data_root );
604 DBUS_METHOD( handle_introspect_player )
609 ADD_STRING( &psz_introspection_xml_data_player );
613 DBUS_METHOD( handle_introspect_tracklist )
618 ADD_STRING( &psz_introspection_xml_data_tracklist );
622 /*****************************************************************************
623 * handle_*: answer to incoming messages
624 *****************************************************************************/
626 #define METHOD_FUNC( method, function ) \
627 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
628 return function( p_conn, p_from, p_this )
630 DBUS_METHOD( handle_root )
633 if( dbus_message_is_method_call( p_from,
634 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
635 return handle_introspect_root( p_conn, p_from, p_this );
637 /* here D-Bus method's names are associated to an handler */
639 METHOD_FUNC( "Identity", Identity );
640 METHOD_FUNC( "MprisVersion", MprisVersion );
641 METHOD_FUNC( "Quit", Quit );
643 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
647 DBUS_METHOD( handle_player )
649 if( dbus_message_is_method_call( p_from,
650 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
651 return handle_introspect_player( p_conn, p_from, p_this );
653 /* here D-Bus method's names are associated to an handler */
655 METHOD_FUNC( "Prev", Prev );
656 METHOD_FUNC( "Next", Next );
657 METHOD_FUNC( "Stop", Stop );
658 METHOD_FUNC( "Play", Play );
659 METHOD_FUNC( "Pause", Pause );
660 METHOD_FUNC( "Repeat", Repeat );
661 METHOD_FUNC( "VolumeSet", VolumeSet );
662 METHOD_FUNC( "VolumeGet", VolumeGet );
663 METHOD_FUNC( "PositionSet", PositionSet );
664 METHOD_FUNC( "PositionGet", PositionGet );
665 METHOD_FUNC( "GetStatus", GetStatus );
666 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
667 METHOD_FUNC( "GetCaps", GetCaps );
669 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
672 DBUS_METHOD( handle_tracklist )
674 if( dbus_message_is_method_call( p_from,
675 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
676 return handle_introspect_tracklist( p_conn, p_from, p_this );
678 /* here D-Bus method's names are associated to an handler */
680 METHOD_FUNC( "GetMetadata", GetMetadata );
681 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
682 METHOD_FUNC( "GetLength", GetLength );
683 METHOD_FUNC( "AddTrack", AddTrack );
684 METHOD_FUNC( "DelTrack", DelTrack );
685 METHOD_FUNC( "SetLoop", SetLoop );
686 METHOD_FUNC( "SetRandom", SetRandom );
688 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
691 /*****************************************************************************
692 * Open: initialize interface
693 *****************************************************************************/
695 static int Open( vlc_object_t *p_this )
696 { /* initialisation of the connection */
697 intf_thread_t *p_intf = (intf_thread_t*)p_this;
698 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
699 playlist_t *p_playlist;
700 DBusConnection *p_conn;
706 p_sys->b_meta_read = false;
707 p_sys->i_caps = CAPS_NONE;
709 dbus_error_init( &error );
711 /* connect to the session bus */
712 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
715 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
717 dbus_error_free( &error );
722 /* register a well-known name on the bus */
723 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
724 if( dbus_error_is_set( &error ) )
726 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
727 ": %s", error.message );
728 dbus_error_free( &error );
733 /* we register the objects */
734 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
735 &vlc_dbus_root_vtable, p_this );
736 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
737 &vlc_dbus_player_vtable, p_this );
738 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
739 &vlc_dbus_tracklist_vtable, p_this );
741 dbus_connection_flush( p_conn );
743 p_playlist = pl_Yield( p_intf );
745 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
746 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
747 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
748 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
749 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
750 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
751 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
753 pl_Release( p_playlist );
755 p_intf->pf_run = Run;
756 p_intf->p_sys = p_sys;
757 p_sys->p_conn = p_conn;
759 UpdateCaps( p_intf, false );
764 /*****************************************************************************
765 * Close: destroy interface
766 *****************************************************************************/
768 static void Close ( vlc_object_t *p_this )
770 intf_thread_t *p_intf = (intf_thread_t*) p_this;
771 playlist_t *p_playlist = pl_Yield( p_intf );;
772 input_thread_t *p_input;
774 p_this->b_dead = true;
777 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
778 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
779 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
780 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
781 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
782 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
783 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
785 p_input = p_playlist->p_input;
788 vlc_object_yield( p_input );
789 var_DelCallback( p_input, "state", StateChange, p_intf );
790 vlc_object_release( p_input );
794 pl_Release( p_playlist );
796 dbus_connection_unref( p_intf->p_sys->p_conn );
798 free( p_intf->p_sys );
801 /*****************************************************************************
803 *****************************************************************************/
805 static void Run ( intf_thread_t *p_intf )
807 while( !intf_ShouldDie( p_intf ) )
809 msleep( INTF_IDLE_SLEEP );
810 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
814 /******************************************************************************
815 * CapsChange: player capabilities change signal
816 *****************************************************************************/
817 DBUS_SIGNAL( CapsChangeSignal )
819 SIGNAL_INIT( "CapsChange" );
822 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
826 /******************************************************************************
827 * TrackListChange: tracklist order / length change signal
828 *****************************************************************************/
829 DBUS_SIGNAL( TrackListChangeSignal )
830 { /* emit the new tracklist lengh */
831 SIGNAL_INIT("TrackListChange");
834 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
835 dbus_int32_t i_elements = p_playlist->current.i_size;
836 pl_Release( p_playlist );
838 ADD_INT32( &i_elements );
842 /*****************************************************************************
843 * TrackListChangeEmit: Emits the TrackListChange signal
844 *****************************************************************************/
845 /* FIXME: It is not called on tracklist reordering */
846 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
847 vlc_value_t oldval, vlc_value_t newval, void *p_data )
850 intf_thread_t *p_intf = p_data;
852 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
854 /* don't signal when items are added/removed in p_category */
855 playlist_t *p_playlist = (playlist_t*)p_this;
856 playlist_add_t *p_add = newval.p_address;
857 playlist_item_t *p_item;
858 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, true );
860 while( p_item->p_parent )
861 p_item = p_item->p_parent;
862 if( p_item == p_playlist->p_root_category )
869 UpdateCaps( p_intf, true );
870 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
873 /*****************************************************************************
874 * TrackChange: Playlist item change callback
875 *****************************************************************************/
877 DBUS_SIGNAL( TrackChangeSignal )
878 { /* emit the metadata of the new item */
879 SIGNAL_INIT( "TrackChange" );
882 input_item_t *p_item = (input_item_t*) p_data;
883 GetInputMeta ( p_item, &args );
888 /*****************************************************************************
889 * StatusChange: Player status change signal
890 *****************************************************************************/
892 DBUS_SIGNAL( StatusChangeSignal )
893 { /* send the updated status info on the bus */
894 SIGNAL_INIT( "StatusChange" );
897 /* we're called from a callback of input_thread_t, so it can not be
898 * destroyed before we return */
899 MarshalStatus( (intf_thread_t*) p_data, &args, false );
904 /*****************************************************************************
905 * StateChange: callback on input "state"
906 *****************************************************************************/
907 static int StateChange( vlc_object_t *p_this, const char* psz_var,
908 vlc_value_t oldval, vlc_value_t newval, void *p_data )
910 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
911 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
912 intf_sys_t *p_sys = p_intf->p_sys;
917 UpdateCaps( p_intf, true );
919 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
921 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
924 p_sys->b_meta_read = true;
925 TrackChangeSignal( p_sys->p_conn, p_item );
929 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
930 newval.i_int == END_S )
932 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
938 /*****************************************************************************
939 * StatusChangeEmit: Emits the StatusChange signal
940 *****************************************************************************/
941 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
942 vlc_value_t oldval, vlc_value_t newval, void *p_data )
944 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
945 VLC_UNUSED(oldval); VLC_UNUSED(newval);
946 intf_thread_t *p_intf = p_data;
951 UpdateCaps( p_intf, false );
952 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
956 /*****************************************************************************
957 * TrackChange: callback on playlist "playlist-current"
958 *****************************************************************************/
959 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
960 vlc_value_t oldval, vlc_value_t newval, void *p_data )
962 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
963 intf_sys_t *p_sys = p_intf->p_sys;
964 playlist_t *p_playlist;
965 input_thread_t *p_input = NULL;
966 input_item_t *p_item = NULL;
967 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
968 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
973 p_sys->b_meta_read = false;
975 p_playlist = pl_Yield( p_intf );
976 p_input = p_playlist->p_input;
981 pl_Release( p_playlist );
985 vlc_object_yield( p_input );
986 pl_Release( p_playlist );
988 p_item = input_GetItem( p_input );
991 vlc_object_release( p_input );
995 if( input_item_IsPreparsed( p_item ) )
997 p_sys->b_meta_read = true;
998 TrackChangeSignal( p_sys->p_conn, p_item );
1001 var_AddCallback( p_input, "state", StateChange, p_intf );
1003 vlc_object_release( p_input );
1007 /*****************************************************************************
1008 * UpdateCaps: update p_sys->i_caps
1009 ****************************************************************************/
1010 static int UpdateCaps( intf_thread_t* p_intf, bool b_playlist_locked )
1012 intf_sys_t* p_sys = p_intf->p_sys;
1013 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1014 playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
1015 if( !b_playlist_locked ) PL_LOCK;
1017 if( p_playlist->current.i_size > 0 )
1018 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1020 if( p_playlist->p_input )
1022 /* XXX: if UpdateCaps() is called too early, these are
1023 * unconditionnaly true */
1024 if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1025 i_caps |= CAPS_CAN_PAUSE;
1026 if( var_GetBool( p_playlist->p_input, "seekable" ) )
1027 i_caps |= CAPS_CAN_SEEK;
1030 if( !b_playlist_locked ) PL_UNLOCK;
1031 pl_Release( p_playlist );
1033 if( p_sys->b_meta_read )
1034 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1036 if( i_caps != p_intf->p_sys->i_caps )
1038 p_sys->i_caps = i_caps;
1039 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1045 /*****************************************************************************
1046 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1047 *****************************************************************************/
1049 #define ADD_META( entry, type, data ) \
1051 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1052 NULL, &dict_entry ); \
1053 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1054 &ppsz_meta_items[entry] ); \
1055 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1056 type##_AS_STRING, &variant ); \
1057 dbus_message_iter_append_basic( &variant, \
1060 dbus_message_iter_close_container( &dict_entry, &variant ); \
1061 dbus_message_iter_close_container( &dict, &dict_entry ); }
1063 #define ADD_VLC_META_STRING( entry, item ) \
1065 char * psz = input_item_Get##item( p_input );\
1066 ADD_META( entry, DBUS_TYPE_STRING, \
1071 static int GetInputMeta( input_item_t* p_input,
1072 DBusMessageIter *args )
1074 DBusMessageIter dict, dict_entry, variant;
1075 /* We need the track length to be expressed in milli-seconds
1076 * instead of µ-seconds */
1077 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1079 const char* ppsz_meta_items[] =
1081 "title", "artist", "genre", "copyright", "album", "tracknum",
1082 "description", "rating", "date", "setting", "url", "language",
1083 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1084 "status", "location", "length", "video-codec", "audio-codec",
1085 "video-bitrate", "audio-bitrate", "audio-samplerate"
1088 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1090 ADD_VLC_META_STRING( 0, Title );
1091 ADD_VLC_META_STRING( 1, Artist );
1092 ADD_VLC_META_STRING( 2, Genre );
1093 ADD_VLC_META_STRING( 3, Copyright );
1094 ADD_VLC_META_STRING( 4, Album );
1095 ADD_VLC_META_STRING( 5, TrackNum );
1096 ADD_VLC_META_STRING( 6, Description );
1097 ADD_VLC_META_STRING( 7, Rating );
1098 ADD_VLC_META_STRING( 8, Date );
1099 ADD_VLC_META_STRING( 9, Setting );
1100 ADD_VLC_META_STRING( 10, URL );
1101 ADD_VLC_META_STRING( 11, Language );
1102 ADD_VLC_META_STRING( 12, NowPlaying );
1103 ADD_VLC_META_STRING( 13, Publisher );
1104 ADD_VLC_META_STRING( 14, EncodedBy );
1105 ADD_VLC_META_STRING( 15, ArtURL );
1106 ADD_VLC_META_STRING( 16, TrackID );
1108 vlc_mutex_lock( &p_input->lock );
1109 if( p_input->p_meta )
1110 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1111 vlc_mutex_unlock( &p_input->lock );
1113 ADD_VLC_META_STRING( 18, URI );
1114 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1116 dbus_message_iter_close_container( args, &dict );
1121 #undef ADD_VLC_META_STRING
1123 /*****************************************************************************
1124 * MarshalStatus: Fill a DBusMessage with the current player status
1125 *****************************************************************************/
1127 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1129 { /* This is NOT the right way to do that, it would be better to sore
1130 the status information in p_sys and update it on change, thus
1131 avoiding a long lock */
1133 DBusMessageIter status;
1134 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1136 playlist_t* p_playlist = NULL;
1137 input_thread_t* p_input = NULL;
1139 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1145 p_input = p_playlist->p_input;
1148 var_Get( p_input, "state", &val );
1149 if( val.i_int >= END_S )
1151 else if( val.i_int == PAUSE_S )
1153 else if( val.i_int <= PLAYING_S )
1157 i_random = var_CreateGetBool( p_playlist, "random" );
1159 i_repeat = var_CreateGetBool( p_playlist, "repeat" );
1161 i_loop = var_CreateGetBool( p_playlist, "loop" );
1165 pl_Release( p_playlist );
1167 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1168 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1169 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1170 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1171 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1172 dbus_message_iter_close_container( args, &status );