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>
50 #include <vlc_common.h>
51 #include <vlc_plugin.h>
53 #include <vlc_interface.h>
55 #include <vlc_input.h>
56 #include <vlc_playlist.h>
62 /*****************************************************************************
64 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
68 static void Run ( intf_thread_t * );
70 static int StateChange( vlc_object_t *, const char *, vlc_value_t,
71 vlc_value_t, void * );
73 static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
74 vlc_value_t, void * );
76 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
77 vlc_value_t, void * );
79 static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
80 vlc_value_t, void * );
82 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
83 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, bool );
84 static int UpdateCaps( intf_thread_t*, bool );
86 /* GetCaps() capabilities */
90 CAPS_CAN_GO_NEXT = 1 << 0,
91 CAPS_CAN_GO_PREV = 1 << 1,
92 CAPS_CAN_PAUSE = 1 << 2,
93 CAPS_CAN_PLAY = 1 << 3,
94 CAPS_CAN_SEEK = 1 << 4,
95 CAPS_CAN_PROVIDE_METADATA = 1 << 5,
96 CAPS_CAN_HAS_TRACKLIST = 1 << 6
101 DBusConnection *p_conn;
107 /*****************************************************************************
109 *****************************************************************************/
112 set_shortname( N_("dbus"));
113 set_category( CAT_INTERFACE );
114 set_subcategory( SUBCAT_INTERFACE_CONTROL );
115 set_description( N_("D-Bus control interface") );
116 set_capability( "interface", 0 );
117 set_callbacks( Open, Close );
120 /*****************************************************************************
122 *****************************************************************************/
129 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
130 playlist_Stop( p_playlist );
131 pl_Release( ((vlc_object_t*) p_this) );
132 vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
136 DBUS_METHOD( MprisVersion )
137 { /*implemented version of the mpris spec */
140 VLC_UNUSED( p_this );
141 dbus_uint16_t i_major = VLC_MPRIS_VERSION_MAJOR;
142 dbus_uint16_t i_minor = VLC_MPRIS_VERSION_MINOR;
143 DBusMessageIter version;
145 if( !dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL,
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_append_basic( &version, DBUS_TYPE_UINT16,
155 return DBUS_HANDLER_RESULT_NEED_MEMORY;
157 if( !dbus_message_iter_close_container( &args, &version ) )
158 return DBUS_HANDLER_RESULT_NEED_MEMORY;
162 DBUS_METHOD( PositionGet )
163 { /* returns position in milliseconds */
166 vlc_value_t position;
169 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
171 input_thread_t *p_input = p_playlist->p_input;
177 var_Get( p_input, "time", &position );
178 i_pos = position.i_time / 1000;
181 pl_Release( ((vlc_object_t*) p_this) );
186 DBUS_METHOD( PositionSet )
187 { /* set position in milliseconds */
190 vlc_value_t position;
191 playlist_t* p_playlist = NULL;
195 dbus_error_init( &error );
197 dbus_message_get_args( p_from, &error,
198 DBUS_TYPE_INT32, &i_pos,
201 if( dbus_error_is_set( &error ) )
203 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
205 dbus_error_free( &error );
206 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
208 p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
210 input_thread_t *p_input = p_playlist->p_input;
214 position.i_time = i_pos * 1000;
215 var_Set( p_input, "time", position );
218 pl_Release( ((vlc_object_t*) p_this) );
222 DBUS_METHOD( VolumeGet )
223 { /* returns volume in percentage */
226 dbus_int32_t i_dbus_vol;
227 audio_volume_t i_vol;
228 /* 2nd argument of aout_VolumeGet is int32 */
229 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
230 double f_vol = 100. * i_vol / AOUT_VOLUME_MAX;
231 i_dbus_vol = round( f_vol );
232 ADD_INT32( &i_dbus_vol );
236 DBUS_METHOD( VolumeSet )
237 { /* set volume in percentage */
241 dbus_error_init( &error );
243 dbus_int32_t i_dbus_vol;
244 audio_volume_t i_vol;
246 dbus_message_get_args( p_from, &error,
247 DBUS_TYPE_INT32, &i_dbus_vol,
250 if( dbus_error_is_set( &error ) )
252 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
254 dbus_error_free( &error );
255 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
258 double f_vol = AOUT_VOLUME_MAX * i_dbus_vol / 100.;
259 i_vol = round( f_vol );
260 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
266 { /* next playlist item */
268 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
269 playlist_Next( p_playlist );
270 pl_Release( ((vlc_object_t*) p_this) );
275 { /* previous playlist item */
277 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
278 playlist_Prev( p_playlist );
279 pl_Release( ((vlc_object_t*) p_this) );
286 playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p_this) );
287 playlist_Stop( p_playlist );
288 pl_Release( ((vlc_object_t*) p_this) );
292 DBUS_METHOD( GetStatus )
293 { /* returns the current status as a struct of 4 ints */
295 First 0 = Playing, 1 = Paused, 2 = Stopped.
296 Second 0 = Playing linearly , 1 = Playing randomly.
297 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
298 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
303 MarshalStatus( p_this, &args, true );
311 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
312 playlist_Pause( p_playlist );
313 pl_Release( (vlc_object_t*) p_this );
320 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
323 input_thread_t *p_input = p_playlist->p_input;
325 vlc_object_hold( p_input );
331 input_Control( p_input, INPUT_SET_POSITION, i_pos );
332 vlc_object_release( p_input );
335 playlist_Play( p_playlist );
337 pl_Release( (vlc_object_t*) p_this );
341 DBUS_METHOD( GetCurrentMetadata )
345 playlist_t* p_playlist = pl_Hold( (vlc_object_t*) p_this );
347 if( p_playlist->status.p_item )
348 GetInputMeta( p_playlist->status.p_item->p_input, &args );
350 pl_Release( (vlc_object_t*) p_this );
354 DBUS_METHOD( GetCaps )
359 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
364 /* Media Player information */
366 DBUS_METHOD( Identity )
372 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
374 ADD_STRING( &psz_identity );
375 free( psz_identity );
378 return DBUS_HANDLER_RESULT_NEED_MEMORY;
385 DBUS_METHOD( AddTrack )
386 { /* add the string to the playlist, and play it if the boolean is true */
391 dbus_error_init( &error );
392 playlist_t* p_playlist = NULL;
397 dbus_message_get_args( p_from, &error,
398 DBUS_TYPE_STRING, &psz_mrl,
399 DBUS_TYPE_BOOLEAN, &b_play,
402 if( dbus_error_is_set( &error ) )
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 p_playlist = pl_Hold( (vlc_object_t*) p_this );
411 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
412 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
413 PLAYLIST_END, true, false );
414 pl_Release( (vlc_object_t*) p_this );
416 dbus_int32_t i_success = 0;
417 ADD_INT32( &i_success );
422 DBUS_METHOD( GetCurrentTrack )
427 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
428 dbus_int32_t i_position = p_playlist->i_current_index;
429 pl_Release( (vlc_object_t*) p_this );
431 ADD_INT32( &i_position );
435 DBUS_METHOD( GetMetadata )
440 dbus_error_init( &error );
442 dbus_int32_t i_position;
444 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
447 dbus_message_get_args( p_from, &error,
448 DBUS_TYPE_INT32, &i_position,
451 if( dbus_error_is_set( &error ) )
454 pl_Release( (vlc_object_t*) p_this );
455 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
457 dbus_error_free( &error );
458 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
461 if( i_position < p_playlist->current.i_size )
463 GetInputMeta( p_playlist->current.p_elems[i_position]->p_input, &args );
467 pl_Release( (vlc_object_t*) p_this );
471 DBUS_METHOD( GetLength )
476 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
477 dbus_int32_t i_elements = p_playlist->current.i_size;
478 pl_Release( (vlc_object_t*) p_this );
480 ADD_INT32( &i_elements );
484 DBUS_METHOD( DelTrack )
489 dbus_error_init( &error );
491 dbus_int32_t i_position;
492 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_this );
494 dbus_message_get_args( p_from, &error,
495 DBUS_TYPE_INT32, &i_position,
498 if( dbus_error_is_set( &error ) )
500 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
502 dbus_error_free( &error );
503 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
507 if( i_position < p_playlist->current.i_size )
509 playlist_DeleteFromInput( p_playlist,
510 p_playlist->current.p_elems[i_position]->p_input->i_id,
515 pl_Release( (vlc_object_t*) p_this );
520 DBUS_METHOD( SetLoop )
528 playlist_t* p_playlist = NULL;
530 dbus_error_init( &error );
531 dbus_message_get_args( p_from, &error,
532 DBUS_TYPE_BOOLEAN, &b_loop,
535 if( dbus_error_is_set( &error ) )
537 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
539 dbus_error_free( &error );
540 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
543 val.b_bool = ( b_loop == TRUE ) ? true : false ;
544 p_playlist = pl_Hold( (vlc_object_t*) p_this );
545 var_Set ( p_playlist, "loop", val );
546 pl_Release( ((vlc_object_t*) p_this) );
551 DBUS_METHOD( Repeat )
557 dbus_bool_t b_repeat;
559 playlist_t* p_playlist = NULL;
561 dbus_error_init( &error );
562 dbus_message_get_args( p_from, &error,
563 DBUS_TYPE_BOOLEAN, &b_repeat,
566 if( dbus_error_is_set( &error ) )
568 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
570 dbus_error_free( &error );
571 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
574 val.b_bool = ( b_repeat == TRUE ) ? true : false ;
576 p_playlist = pl_Hold( (vlc_object_t*) p_this );
577 var_Set ( p_playlist, "repeat", val );
578 pl_Release( ((vlc_object_t*) p_this) );
583 DBUS_METHOD( SetRandom )
589 dbus_bool_t b_random;
591 playlist_t* p_playlist = NULL;
593 dbus_error_init( &error );
594 dbus_message_get_args( p_from, &error,
595 DBUS_TYPE_BOOLEAN, &b_random,
598 if( dbus_error_is_set( &error ) )
600 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
602 dbus_error_free( &error );
603 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
606 val.b_bool = ( b_random == TRUE ) ? true : false ;
608 p_playlist = pl_Hold( (vlc_object_t*) p_this );
609 var_Set ( p_playlist, "random", val );
610 pl_Release( ((vlc_object_t*) p_this) );
614 /*****************************************************************************
615 * Introspection method
616 *****************************************************************************/
618 DBUS_METHOD( handle_introspect_root )
619 { /* handles introspection of root object */
623 ADD_STRING( &psz_introspection_xml_data_root );
627 DBUS_METHOD( handle_introspect_player )
632 ADD_STRING( &psz_introspection_xml_data_player );
636 DBUS_METHOD( handle_introspect_tracklist )
641 ADD_STRING( &psz_introspection_xml_data_tracklist );
645 /*****************************************************************************
646 * handle_*: answer to incoming messages
647 *****************************************************************************/
649 #define METHOD_FUNC( method, function ) \
650 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
651 return function( p_conn, p_from, p_this )
653 DBUS_METHOD( handle_root )
656 if( dbus_message_is_method_call( p_from,
657 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
658 return handle_introspect_root( p_conn, p_from, p_this );
660 /* here D-Bus method's names are associated to an handler */
662 METHOD_FUNC( "Identity", Identity );
663 METHOD_FUNC( "MprisVersion", MprisVersion );
664 METHOD_FUNC( "Quit", Quit );
666 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
670 DBUS_METHOD( handle_player )
672 if( dbus_message_is_method_call( p_from,
673 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
674 return handle_introspect_player( p_conn, p_from, p_this );
676 /* here D-Bus method's names are associated to an handler */
678 METHOD_FUNC( "Prev", Prev );
679 METHOD_FUNC( "Next", Next );
680 METHOD_FUNC( "Stop", Stop );
681 METHOD_FUNC( "Play", Play );
682 METHOD_FUNC( "Pause", Pause );
683 METHOD_FUNC( "Repeat", Repeat );
684 METHOD_FUNC( "VolumeSet", VolumeSet );
685 METHOD_FUNC( "VolumeGet", VolumeGet );
686 METHOD_FUNC( "PositionSet", PositionSet );
687 METHOD_FUNC( "PositionGet", PositionGet );
688 METHOD_FUNC( "GetStatus", GetStatus );
689 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
690 METHOD_FUNC( "GetCaps", GetCaps );
692 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
695 DBUS_METHOD( handle_tracklist )
697 if( dbus_message_is_method_call( p_from,
698 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
699 return handle_introspect_tracklist( p_conn, p_from, p_this );
701 /* here D-Bus method's names are associated to an handler */
703 METHOD_FUNC( "GetMetadata", GetMetadata );
704 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
705 METHOD_FUNC( "GetLength", GetLength );
706 METHOD_FUNC( "AddTrack", AddTrack );
707 METHOD_FUNC( "DelTrack", DelTrack );
708 METHOD_FUNC( "SetLoop", SetLoop );
709 METHOD_FUNC( "SetRandom", SetRandom );
711 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
714 /*****************************************************************************
715 * Open: initialize interface
716 *****************************************************************************/
718 static int Open( vlc_object_t *p_this )
719 { /* initialisation of the connection */
720 intf_thread_t *p_intf = (intf_thread_t*)p_this;
721 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
722 playlist_t *p_playlist;
723 DBusConnection *p_conn;
729 p_sys->b_meta_read = false;
730 p_sys->i_caps = CAPS_NONE;
731 p_sys->b_dead = false;
733 dbus_error_init( &error );
735 /* connect to the session bus */
736 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
739 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
741 dbus_error_free( &error );
746 /* register a well-known name on the bus */
747 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
748 if( dbus_error_is_set( &error ) )
750 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
751 ": %s", error.message );
752 dbus_error_free( &error );
757 /* we register the objects */
758 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
759 &vlc_dbus_root_vtable, p_this );
760 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
761 &vlc_dbus_player_vtable, p_this );
762 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
763 &vlc_dbus_tracklist_vtable, p_this );
765 dbus_connection_flush( p_conn );
767 p_playlist = pl_Hold( p_intf );
769 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
770 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
771 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
772 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
773 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
774 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
775 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
777 pl_Release( p_intf );
779 p_intf->pf_run = Run;
780 p_intf->p_sys = p_sys;
781 p_sys->p_conn = p_conn;
783 UpdateCaps( p_intf, false );
788 /*****************************************************************************
789 * Close: destroy interface
790 *****************************************************************************/
792 static void Close ( vlc_object_t *p_this )
794 intf_thread_t *p_intf = (intf_thread_t*) p_this;
795 playlist_t *p_playlist = pl_Hold( p_intf );;
796 input_thread_t *p_input;
799 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
800 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
801 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
802 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
803 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
804 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
805 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
807 p_input = p_playlist->p_input;
810 vlc_object_hold( p_input );
811 var_DelCallback( p_input, "state", StateChange, p_intf );
812 vlc_object_release( p_input );
816 pl_Release( p_intf );
818 dbus_connection_unref( p_intf->p_sys->p_conn );
820 free( p_intf->p_sys );
823 /*****************************************************************************
825 *****************************************************************************/
827 static void Run ( intf_thread_t *p_intf )
831 msleep( INTF_IDLE_SLEEP );
832 int canc = vlc_savecancel();
833 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
834 vlc_restorecancel( canc );
838 /******************************************************************************
839 * CapsChange: player capabilities change signal
840 *****************************************************************************/
841 DBUS_SIGNAL( CapsChangeSignal )
843 SIGNAL_INIT( "CapsChange" );
846 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
850 /******************************************************************************
851 * TrackListChange: tracklist order / length change signal
852 *****************************************************************************/
853 DBUS_SIGNAL( TrackListChangeSignal )
854 { /* emit the new tracklist lengh */
855 SIGNAL_INIT("TrackListChange");
858 playlist_t *p_playlist = pl_Hold( (vlc_object_t*) p_data );
859 dbus_int32_t i_elements = p_playlist->current.i_size;
860 pl_Release( (vlc_object_t*) p_data );
862 ADD_INT32( &i_elements );
866 /*****************************************************************************
867 * TrackListChangeEmit: Emits the TrackListChange signal
868 *****************************************************************************/
869 /* FIXME: It is not called on tracklist reordering */
870 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
871 vlc_value_t oldval, vlc_value_t newval, void *p_data )
874 intf_thread_t *p_intf = p_data;
876 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
878 /* don't signal when items are added/removed in p_category */
879 playlist_t *p_playlist = (playlist_t*)p_this;
880 playlist_add_t *p_add = newval.p_address;
881 playlist_item_t *p_item;
882 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, pl_Locked );
884 while( p_item->p_parent )
885 p_item = p_item->p_parent;
886 if( p_item == p_playlist->p_root_category )
890 if( p_intf->p_sys->b_dead )
893 UpdateCaps( p_intf, true );
894 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
897 /*****************************************************************************
898 * TrackChange: Playlist item change callback
899 *****************************************************************************/
901 DBUS_SIGNAL( TrackChangeSignal )
902 { /* emit the metadata of the new item */
903 SIGNAL_INIT( "TrackChange" );
906 input_item_t *p_item = (input_item_t*) p_data;
907 GetInputMeta ( p_item, &args );
912 /*****************************************************************************
913 * StatusChange: Player status change signal
914 *****************************************************************************/
916 DBUS_SIGNAL( StatusChangeSignal )
917 { /* send the updated status info on the bus */
918 SIGNAL_INIT( "StatusChange" );
921 /* we're called from a callback of input_thread_t, so it can not be
922 * destroyed before we return */
923 MarshalStatus( (intf_thread_t*) p_data, &args, false );
928 /*****************************************************************************
929 * StateChange: callback on input "state"
930 *****************************************************************************/
931 static int StateChange( vlc_object_t *p_this, const char* psz_var,
932 vlc_value_t oldval, vlc_value_t newval, void *p_data )
934 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
935 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
936 intf_sys_t *p_sys = p_intf->p_sys;
938 if( p_intf->p_sys->b_dead )
941 UpdateCaps( p_intf, true );
943 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
945 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
948 p_sys->b_meta_read = true;
949 TrackChangeSignal( p_sys->p_conn, p_item );
953 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
954 newval.i_int == STOP_S || newval.i_int == END_S )
956 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
962 /*****************************************************************************
963 * StatusChangeEmit: Emits the StatusChange signal
964 *****************************************************************************/
965 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
966 vlc_value_t oldval, vlc_value_t newval, void *p_data )
968 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
969 VLC_UNUSED(oldval); VLC_UNUSED(newval);
970 intf_thread_t *p_intf = p_data;
972 if( p_intf->p_sys->b_dead )
975 UpdateCaps( p_intf, false );
976 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
980 /*****************************************************************************
981 * TrackChange: callback on playlist "playlist-current"
982 *****************************************************************************/
983 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
984 vlc_value_t oldval, vlc_value_t newval, void *p_data )
986 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
987 intf_sys_t *p_sys = p_intf->p_sys;
988 playlist_t *p_playlist;
989 input_thread_t *p_input = NULL;
990 input_item_t *p_item = NULL;
991 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
992 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
994 if( p_intf->p_sys->b_dead )
997 p_sys->b_meta_read = false;
999 p_playlist = pl_Hold( p_intf );
1000 p_input = p_playlist->p_input;
1005 pl_Release( p_intf );
1009 vlc_object_hold( p_input );
1010 pl_Release( p_intf );
1012 p_item = input_GetItem( p_input );
1015 vlc_object_release( p_input );
1016 return VLC_EGENERIC;
1019 if( input_item_IsPreparsed( p_item ) )
1021 p_sys->b_meta_read = true;
1022 TrackChangeSignal( p_sys->p_conn, p_item );
1025 var_AddCallback( p_input, "state", StateChange, p_intf );
1027 vlc_object_release( p_input );
1031 /*****************************************************************************
1032 * UpdateCaps: update p_sys->i_caps
1033 ****************************************************************************/
1034 static int UpdateCaps( intf_thread_t* p_intf, bool b_playlist_locked )
1036 intf_sys_t* p_sys = p_intf->p_sys;
1037 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1038 playlist_t* p_playlist = pl_Hold( p_intf );
1039 if( !b_playlist_locked ) PL_LOCK;
1041 if( p_playlist->current.i_size > 0 )
1042 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1044 if( p_playlist->p_input )
1046 /* XXX: if UpdateCaps() is called too early, these are
1047 * unconditionnaly true */
1048 if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1049 i_caps |= CAPS_CAN_PAUSE;
1050 if( var_GetBool( p_playlist->p_input, "seekable" ) )
1051 i_caps |= CAPS_CAN_SEEK;
1054 if( !b_playlist_locked ) PL_UNLOCK;
1055 pl_Release( p_intf );
1057 if( p_sys->b_meta_read )
1058 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1060 if( i_caps != p_intf->p_sys->i_caps )
1062 p_sys->i_caps = i_caps;
1063 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1069 /*****************************************************************************
1070 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1071 *****************************************************************************/
1073 #define ADD_META( entry, type, data ) \
1075 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1076 NULL, &dict_entry ); \
1077 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1078 &ppsz_meta_items[entry] ); \
1079 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1080 type##_AS_STRING, &variant ); \
1081 dbus_message_iter_append_basic( &variant, \
1084 dbus_message_iter_close_container( &dict_entry, &variant ); \
1085 dbus_message_iter_close_container( &dict, &dict_entry ); }
1087 #define ADD_VLC_META_STRING( entry, item ) \
1089 char * psz = input_item_Get##item( p_input );\
1090 ADD_META( entry, DBUS_TYPE_STRING, \
1095 static int GetInputMeta( input_item_t* p_input,
1096 DBusMessageIter *args )
1098 DBusMessageIter dict, dict_entry, variant;
1099 /* We need the track length to be expressed in milli-seconds
1100 * instead of µ-seconds */
1101 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1103 const char* ppsz_meta_items[] =
1105 "title", "artist", "genre", "copyright", "album", "tracknum",
1106 "description", "rating", "date", "setting", "url", "language",
1107 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1108 "status", "location", "length", "video-codec", "audio-codec",
1109 "video-bitrate", "audio-bitrate", "audio-samplerate"
1112 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1114 ADD_VLC_META_STRING( 0, Title );
1115 ADD_VLC_META_STRING( 1, Artist );
1116 ADD_VLC_META_STRING( 2, Genre );
1117 ADD_VLC_META_STRING( 3, Copyright );
1118 ADD_VLC_META_STRING( 4, Album );
1119 ADD_VLC_META_STRING( 5, TrackNum );
1120 ADD_VLC_META_STRING( 6, Description );
1121 ADD_VLC_META_STRING( 7, Rating );
1122 ADD_VLC_META_STRING( 8, Date );
1123 ADD_VLC_META_STRING( 9, Setting );
1124 ADD_VLC_META_STRING( 10, URL );
1125 ADD_VLC_META_STRING( 11, Language );
1126 ADD_VLC_META_STRING( 12, NowPlaying );
1127 ADD_VLC_META_STRING( 13, Publisher );
1128 ADD_VLC_META_STRING( 14, EncodedBy );
1129 ADD_VLC_META_STRING( 15, ArtURL );
1130 ADD_VLC_META_STRING( 16, TrackID );
1132 vlc_mutex_lock( &p_input->lock );
1133 if( p_input->p_meta )
1134 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1135 vlc_mutex_unlock( &p_input->lock );
1137 ADD_VLC_META_STRING( 18, URI );
1138 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1140 dbus_message_iter_close_container( args, &dict );
1145 #undef ADD_VLC_META_STRING
1147 /*****************************************************************************
1148 * MarshalStatus: Fill a DBusMessage with the current player status
1149 *****************************************************************************/
1151 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1153 { /* This is NOT the right way to do that, it would be better to sore
1154 the status information in p_sys and update it on change, thus
1155 avoiding a long lock */
1157 DBusMessageIter status;
1158 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1160 playlist_t* p_playlist = NULL;
1161 input_thread_t* p_input = NULL;
1163 p_playlist = pl_Hold( p_intf );
1169 p_input = p_playlist->p_input;
1172 var_Get( p_input, "state", &val );
1173 if( val.i_int >= END_S )
1175 else if( val.i_int == PAUSE_S )
1177 else if( val.i_int <= PLAYING_S )
1181 i_random = var_CreateGetBool( p_playlist, "random" );
1183 i_repeat = var_CreateGetBool( p_playlist, "repeat" );
1185 i_loop = var_CreateGetBool( p_playlist, "loop" );
1189 pl_Release( p_intf );
1191 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1192 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1193 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1194 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1195 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1196 dbus_message_iter_close_container( args, &status );