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 );
82 /* GetCaps() capabilities */
86 CAPS_CAN_GO_NEXT = 1 << 0,
87 CAPS_CAN_GO_PREV = 1 << 1,
88 CAPS_CAN_PAUSE = 1 << 2,
89 CAPS_CAN_PLAY = 1 << 3,
90 CAPS_CAN_SEEK = 1 << 4,
91 CAPS_CAN_PROVIDE_METADATA = 1 << 5
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( PositionGet )
131 { /* returns position in milliseconds */
134 vlc_value_t position;
137 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
139 input_thread_t *p_input = p_playlist->p_input;
145 var_Get( p_input, "time", &position );
146 i_pos = position.i_time / 1000;
149 pl_Release( ((vlc_object_t*) p_this) );
154 DBUS_METHOD( PositionSet )
155 { /* set position in milliseconds */
158 vlc_value_t position;
159 playlist_t* p_playlist = NULL;
163 dbus_error_init( &error );
165 dbus_message_get_args( p_from, &error,
166 DBUS_TYPE_INT32, &i_pos,
169 if( dbus_error_is_set( &error ) )
171 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
173 dbus_error_free( &error );
174 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
176 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
178 input_thread_t *p_input = p_playlist->p_input;
182 position.i_time = i_pos * 1000;
183 var_Set( p_input, "time", position );
186 pl_Release( ((vlc_object_t*) p_this) );
190 DBUS_METHOD( VolumeGet )
191 { /* returns volume in percentage */
194 dbus_int32_t i_dbus_vol;
195 audio_volume_t i_vol;
196 /* 2nd argument of aout_VolumeGet is int32 */
197 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
198 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
199 ADD_INT32( &i_dbus_vol );
203 DBUS_METHOD( VolumeSet )
204 { /* set volume in percentage */
208 dbus_error_init( &error );
210 dbus_int32_t i_dbus_vol;
211 audio_volume_t i_vol;
213 dbus_message_get_args( p_from, &error,
214 DBUS_TYPE_INT32, &i_dbus_vol,
217 if( dbus_error_is_set( &error ) )
219 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
221 dbus_error_free( &error );
222 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
225 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
226 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
232 { /* next playlist item */
234 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
235 playlist_Next( p_playlist );
236 pl_Release( ((vlc_object_t*) p_this) );
241 { /* previous playlist item */
243 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
244 playlist_Prev( p_playlist );
245 pl_Release( ((vlc_object_t*) p_this) );
252 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
253 playlist_Stop( p_playlist );
254 pl_Release( ((vlc_object_t*) p_this) );
258 DBUS_METHOD( GetStatus )
259 { /* returns the current status as a struct of 4 ints */
261 First 0 = Playing, 1 = Paused, 2 = Stopped.
262 Second 0 = Playing linearly , 1 = Playing randomly.
263 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
264 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
269 MarshalStatus( p_this, &args, VLC_TRUE );
277 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
278 playlist_Pause( p_playlist );
279 pl_Release( p_playlist );
286 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
287 playlist_Play( p_playlist );
288 pl_Release( p_playlist );
292 DBUS_METHOD( GetCurrentMetadata )
296 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
298 if( p_playlist->status.p_item )
299 GetInputMeta( p_playlist->status.p_item->p_input, &args );
301 pl_Release( p_playlist );
305 DBUS_METHOD( GetCaps )
309 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
312 dbus_int32_t i_caps = CAPS_NONE;
315 * Every capability should be checked in a callback, modifying p_sys->i_caps
316 * so we can send a signal whenever it changes.
317 * When it is done, GetCaps method will just return p_sys->i_caps
320 if( p_playlist->items.i_size > 0 )
321 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
323 if( p_playlist->p_input )
325 access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input,
326 VLC_OBJECT_ACCESS, FIND_CHILD );
329 vlc_bool_t b_can_pause;
330 if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
332 i_caps |= CAPS_CAN_PAUSE;
333 vlc_object_release( p_access );
335 demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
336 VLC_OBJECT_DEMUX, FIND_CHILD );
338 { /* XXX: is: demux can seek and access can not a possibility ? */
339 vlc_bool_t b_can_seek;
340 if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
342 i_caps |= CAPS_CAN_SEEK;
343 vlc_object_release( p_demux );
347 if( ((intf_thread_t*)p_this)->p_sys->b_meta_read )
348 i_caps |= CAPS_CAN_PROVIDE_METADATA;
352 ADD_INT32( &i_caps );
357 /* Media Player information */
359 DBUS_METHOD( Identity )
365 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
367 ADD_STRING( &psz_identity );
368 free( psz_identity );
371 return DBUS_HANDLER_RESULT_NEED_MEMORY;
378 DBUS_METHOD( AddTrack )
379 { /* add the string to the playlist, and play it if the boolean is true */
383 dbus_error_init( &error );
384 playlist_t* p_playlist = NULL;
389 dbus_message_get_args( p_from, &error,
390 DBUS_TYPE_STRING, &psz_mrl,
391 DBUS_TYPE_BOOLEAN, &b_play,
394 if( dbus_error_is_set( &error ) )
396 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
398 dbus_error_free( &error );
399 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
402 p_playlist = pl_Yield( (vlc_object_t*) p_this );
403 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
404 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
405 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
406 pl_Release( p_playlist );
411 DBUS_METHOD( GetCurrentTrack )
416 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
417 dbus_int32_t i_position = p_playlist->i_current_index;
418 pl_Release( p_playlist );
420 ADD_INT32( &i_position );
424 DBUS_METHOD( GetMetadata )
429 dbus_error_init( &error );
431 dbus_int32_t i_position;
433 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
436 dbus_message_get_args( p_from, &error,
437 DBUS_TYPE_INT32, &i_position,
440 if( dbus_error_is_set( &error ) )
443 pl_Release( p_playlist );
444 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
446 dbus_error_free( &error );
447 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
450 if( i_position <= p_playlist->items.i_size / 2 )
452 GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
456 pl_Release( p_playlist );
460 DBUS_METHOD( GetLength )
465 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
466 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
467 pl_Release( p_playlist );
469 ADD_INT32( &i_elements );
473 DBUS_METHOD( DelTrack )
478 dbus_error_init( &error );
480 dbus_int32_t i_position;
481 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
483 dbus_message_get_args( p_from, &error,
484 DBUS_TYPE_INT32, &i_position,
487 if( dbus_error_is_set( &error ) )
489 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
491 dbus_error_free( &error );
492 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
495 if( i_position <= p_playlist->items.i_size / 2 )
497 playlist_DeleteFromInput( p_playlist,
498 p_playlist->items.p_elems[i_position*2-1]->i_id,
502 pl_Release( p_playlist );
515 playlist_t* p_playlist = NULL;
517 dbus_error_init( &error );
518 dbus_message_get_args( p_from, &error,
519 DBUS_TYPE_BOOLEAN, &b_loop,
522 if( dbus_error_is_set( &error ) )
524 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
526 dbus_error_free( &error );
527 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
530 val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
531 p_playlist = pl_Yield( (vlc_object_t*) p_this );
532 var_Set ( p_playlist, "loop", val );
533 pl_Release( ((vlc_object_t*) p_this) );
538 DBUS_METHOD( Repeat )
544 dbus_bool_t b_repeat;
546 playlist_t* p_playlist = NULL;
548 dbus_error_init( &error );
549 dbus_message_get_args( p_from, &error,
550 DBUS_TYPE_BOOLEAN, &b_repeat,
553 if( dbus_error_is_set( &error ) )
555 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
557 dbus_error_free( &error );
558 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
561 val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
563 p_playlist = pl_Yield( (vlc_object_t*) p_this );
564 var_Set ( p_playlist, "repeat", val );
565 pl_Release( ((vlc_object_t*) p_this) );
570 DBUS_METHOD( Random )
576 dbus_bool_t b_random;
578 playlist_t* p_playlist = NULL;
580 dbus_error_init( &error );
581 dbus_message_get_args( p_from, &error,
582 DBUS_TYPE_BOOLEAN, &b_random,
585 if( dbus_error_is_set( &error ) )
587 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
589 dbus_error_free( &error );
590 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
593 val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
595 p_playlist = pl_Yield( (vlc_object_t*) p_this );
596 var_Set ( p_playlist, "random", val );
597 pl_Release( ((vlc_object_t*) p_this) );
601 /*****************************************************************************
602 * Introspection method
603 *****************************************************************************/
605 DBUS_METHOD( handle_introspect_root )
606 { /* handles introspection of root object */
610 ADD_STRING( &psz_introspection_xml_data_root );
614 DBUS_METHOD( handle_introspect_player )
619 ADD_STRING( &psz_introspection_xml_data_player );
623 DBUS_METHOD( handle_introspect_tracklist )
628 ADD_STRING( &psz_introspection_xml_data_tracklist );
632 /*****************************************************************************
633 * handle_*: answer to incoming messages
634 *****************************************************************************/
636 #define METHOD_FUNC( method, function ) \
637 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
638 return function( p_conn, p_from, p_this )
640 DBUS_METHOD( handle_root )
643 if( dbus_message_is_method_call( p_from,
644 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
645 return handle_introspect_root( p_conn, p_from, p_this );
647 /* here D-Bus method's names are associated to an handler */
649 METHOD_FUNC( "Identity", Identity );
651 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
655 DBUS_METHOD( handle_player )
657 if( dbus_message_is_method_call( p_from,
658 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
659 return handle_introspect_player( p_conn, p_from, p_this );
661 /* here D-Bus method's names are associated to an handler */
663 METHOD_FUNC( "Prev", Prev );
664 METHOD_FUNC( "Next", Next );
665 METHOD_FUNC( "Quit", Quit );
666 METHOD_FUNC( "Stop", Stop );
667 METHOD_FUNC( "Play", Play );
668 METHOD_FUNC( "Pause", Pause );
669 METHOD_FUNC( "Repeat", Repeat );
670 METHOD_FUNC( "VolumeSet", VolumeSet );
671 METHOD_FUNC( "VolumeGet", VolumeGet );
672 METHOD_FUNC( "PositionSet", PositionSet );
673 METHOD_FUNC( "PositionGet", PositionGet );
674 METHOD_FUNC( "GetStatus", GetStatus );
675 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
676 METHOD_FUNC( "GetCaps", GetCaps );
678 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
681 DBUS_METHOD( handle_tracklist )
683 if( dbus_message_is_method_call( p_from,
684 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
685 return handle_introspect_tracklist( p_conn, p_from, p_this );
687 /* here D-Bus method's names are associated to an handler */
689 METHOD_FUNC( "GetMetadata", GetMetadata );
690 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
691 METHOD_FUNC( "GetLength", GetLength );
692 METHOD_FUNC( "AddTrack", AddTrack );
693 METHOD_FUNC( "DelTrack", DelTrack );
694 METHOD_FUNC( "Loop", Loop );
695 METHOD_FUNC( "Random", Random );
697 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
700 /*****************************************************************************
701 * Open: initialize interface
702 *****************************************************************************/
704 static int Open( vlc_object_t *p_this )
705 { /* initialisation of the connection */
706 intf_thread_t *p_intf = (intf_thread_t*)p_this;
707 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
708 playlist_t *p_playlist;
709 DBusConnection *p_conn;
715 p_sys->b_meta_read = VLC_FALSE;
716 p_sys->i_caps = CAPS_NONE;
718 dbus_error_init( &error );
720 /* connect to the session bus */
721 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
724 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
726 dbus_error_free( &error );
731 /* register a well-known name on the bus */
732 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
733 if( dbus_error_is_set( &error ) )
735 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
736 ": %s", error.message );
737 dbus_error_free( &error );
742 /* we register the objects */
743 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
744 &vlc_dbus_root_vtable, p_this );
745 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
746 &vlc_dbus_player_vtable, p_this );
747 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
748 &vlc_dbus_tracklist_vtable, p_this );
750 dbus_connection_flush( p_conn );
752 p_playlist = pl_Yield( p_intf );
754 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
755 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
756 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
757 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
758 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
759 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
760 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
762 pl_Release( p_playlist );
764 p_intf->pf_run = Run;
765 p_intf->p_sys = p_sys;
766 p_sys->p_conn = p_conn;
771 /*****************************************************************************
772 * Close: destroy interface
773 *****************************************************************************/
775 static void Close ( vlc_object_t *p_this )
777 intf_thread_t *p_intf = (intf_thread_t*) p_this;
778 playlist_t *p_playlist = pl_Yield( p_intf );;
779 input_thread_t *p_input;
781 p_this->b_dead = VLC_TRUE;
784 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
785 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
786 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
787 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
788 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
789 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
790 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
792 p_input = p_playlist->p_input;
795 vlc_object_yield( p_input );
796 var_DelCallback( p_input, "state", StateChange, p_intf );
797 vlc_object_release( p_input );
801 pl_Release( p_playlist );
803 dbus_connection_unref( p_intf->p_sys->p_conn );
805 free( p_intf->p_sys );
808 /*****************************************************************************
810 *****************************************************************************/
812 static void Run ( intf_thread_t *p_intf )
814 while( !intf_ShouldDie( p_intf ) )
816 msleep( INTF_IDLE_SLEEP );
817 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
821 /******************************************************************************
822 * TrackListChange: tracklist order / length change signal
823 *****************************************************************************/
824 DBUS_SIGNAL( TrackListChangeSignal )
825 { /* emit the new tracklist lengh */
826 SIGNAL_INIT("TrackListChange");
829 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
830 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
831 pl_Release( p_playlist );
833 ADD_INT32( &i_elements );
837 /*****************************************************************************
838 * TrackListChangeEmit: Emits the TrackListChange signal
839 *****************************************************************************/
840 /* FIXME: It is not called on tracklist reordering */
841 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
842 vlc_value_t oldval, vlc_value_t newval, void *p_data )
845 intf_thread_t *p_intf = p_data;
847 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
849 /* don't signal when items are added/removed in p_category */
850 playlist_t *p_playlist = (playlist_t*)p_this;
851 playlist_add_t *p_add = newval.p_address;
852 playlist_item_t *p_item;
853 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, VLC_TRUE );
855 while( p_item->p_parent )
856 p_item = p_item->p_parent;
857 if( p_item == p_playlist->p_root_category )
864 TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
867 /*****************************************************************************
868 * TrackChange: Playlist item change callback
869 *****************************************************************************/
871 DBUS_SIGNAL( TrackChangeSignal )
872 { /* emit the metadata of the new item */
873 SIGNAL_INIT( "TrackChange" );
876 input_item_t *p_item = (input_item_t*) p_data;
877 GetInputMeta ( p_item, &args );
882 /*****************************************************************************
883 * StatusChange: Player status change signal
884 *****************************************************************************/
886 DBUS_SIGNAL( StatusChangeSignal )
887 { /* send the updated status info on the bus */
888 SIGNAL_INIT( "StatusChange" );
891 /* we're called from a callback of input_thread_t, so it can not be
892 * destroyed before we return */
893 MarshalStatus( (intf_thread_t*) p_data, &args, VLC_FALSE );
898 /*****************************************************************************
899 * StateChange: callback on input "state"
900 *****************************************************************************/
901 static int StateChange( vlc_object_t *p_this, const char* psz_var,
902 vlc_value_t oldval, vlc_value_t newval, void *p_data )
904 VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
905 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
906 intf_sys_t *p_sys = p_intf->p_sys;
911 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
913 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
916 p_sys->b_meta_read = VLC_TRUE;
917 TrackChangeSignal( p_sys->p_conn, p_item );
921 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
922 newval.i_int == END_S )
924 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
930 /*****************************************************************************
931 * StatusChangeEmit: Emits the StatusChange signal
932 *****************************************************************************/
933 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
934 vlc_value_t oldval, vlc_value_t newval, void *p_data )
936 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
937 VLC_UNUSED(oldval); VLC_UNUSED(newval);
938 intf_thread_t *p_intf = p_data;
943 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
947 /*****************************************************************************
948 * TrackChange: callback on playlist "playlist-current"
949 *****************************************************************************/
950 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
951 vlc_value_t oldval, vlc_value_t newval, void *p_data )
953 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
954 intf_sys_t *p_sys = p_intf->p_sys;
955 playlist_t *p_playlist;
956 input_thread_t *p_input = NULL;
957 input_item_t *p_item = NULL;
958 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
959 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
964 p_sys->b_meta_read = VLC_FALSE;
966 p_playlist = pl_Yield( p_intf );
968 p_input = p_playlist->p_input;
973 pl_Release( p_playlist );
977 vlc_object_yield( p_input );
979 pl_Release( p_playlist );
981 p_item = input_GetItem( p_input );
984 vlc_object_release( p_input );
988 if( input_item_IsPreparsed( p_item ) )
990 p_sys->b_meta_read = VLC_TRUE;
991 TrackChangeSignal( p_sys->p_conn, p_item );
994 dbus_int32_t i_caps = CAPS_NONE;
996 if( p_playlist->items.i_size > 0 )
997 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
999 if( p_playlist->p_input )
1001 access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input,
1002 VLC_OBJECT_ACCESS, FIND_CHILD );
1005 vlc_bool_t b_can_pause;
1006 if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
1008 i_caps |= CAPS_CAN_PAUSE;
1009 vlc_object_release( p_access );
1011 demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
1012 VLC_OBJECT_DEMUX, FIND_CHILD );
1014 { /* XXX: is: demux can seek and access can not a possibility ? */
1015 vlc_bool_t b_can_seek;
1016 if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
1018 i_caps |= CAPS_CAN_SEEK;
1019 vlc_object_release( p_demux );
1023 if( ((intf_thread_t*)p_this)->p_sys->b_meta_read )
1024 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1027 var_AddCallback( p_input, "state", StateChange, p_intf );
1029 vlc_object_release( p_input );
1033 /*****************************************************************************
1034 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1035 *****************************************************************************/
1037 #define ADD_META( entry, type, data ) \
1039 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1040 NULL, &dict_entry ); \
1041 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1042 &ppsz_meta_items[entry] ); \
1043 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1044 type##_AS_STRING, &variant ); \
1045 dbus_message_iter_append_basic( &variant, \
1048 dbus_message_iter_close_container( &dict_entry, &variant ); \
1049 dbus_message_iter_close_container( &dict, &dict_entry ); }
1051 #define ADD_VLC_META_STRING( entry, item ) \
1053 char * psz = input_item_Get##item( p_input );\
1054 ADD_META( entry, DBUS_TYPE_STRING, \
1059 static int GetInputMeta( input_item_t* p_input,
1060 DBusMessageIter *args )
1062 DBusMessageIter dict, dict_entry, variant;
1063 /* We need the track length to be expressed in milli-seconds
1064 * instead of µ-seconds */
1065 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1067 const char* ppsz_meta_items[] =
1069 "title", "artist", "genre", "copyright", "album", "tracknum",
1070 "description", "rating", "date", "setting", "url", "language",
1071 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1072 "status", "URI", "length", "video-codec", "audio-codec",
1073 "video-bitrate", "audio-bitrate", "audio-samplerate"
1076 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1078 ADD_VLC_META_STRING( 0, Title );
1079 ADD_VLC_META_STRING( 1, Artist );
1080 ADD_VLC_META_STRING( 2, Genre );
1081 ADD_VLC_META_STRING( 3, Copyright );
1082 ADD_VLC_META_STRING( 4, Album );
1083 ADD_VLC_META_STRING( 5, TrackNum );
1084 ADD_VLC_META_STRING( 6, Description );
1085 ADD_VLC_META_STRING( 7, Rating );
1086 ADD_VLC_META_STRING( 8, Date );
1087 ADD_VLC_META_STRING( 9, Setting );
1088 ADD_VLC_META_STRING( 10, URL );
1089 ADD_VLC_META_STRING( 11, Language );
1090 ADD_VLC_META_STRING( 12, NowPlaying );
1091 ADD_VLC_META_STRING( 13, Publisher );
1092 ADD_VLC_META_STRING( 14, EncodedBy );
1093 ADD_VLC_META_STRING( 15, ArtURL );
1094 ADD_VLC_META_STRING( 16, TrackID );
1096 vlc_mutex_lock( &p_input->lock );
1097 if( p_input->p_meta )
1098 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1099 vlc_mutex_unlock( &p_input->lock );
1101 ADD_VLC_META_STRING( 18, URI );
1102 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1104 dbus_message_iter_close_container( args, &dict );
1109 #undef ADD_VLC_META_STRING
1111 /*****************************************************************************
1112 * MarshalStatus: Fill a DBusMessage with the current player status
1113 *****************************************************************************/
1115 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1117 { /* This is NOT the right way to do that, it would be better to sore
1118 the status information in p_sys and update it on change, thus
1119 avoiding a long lock */
1121 DBusMessageIter status;
1122 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1124 playlist_t* p_playlist = NULL;
1125 input_thread_t* p_input = NULL;
1127 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1133 p_input = p_playlist->p_input;
1136 var_Get( p_input, "state", &val );
1137 if( val.i_int >= END_S )
1139 else if( val.i_int == PAUSE_S )
1141 else if( val.i_int <= PLAYING_S )
1145 var_Get( p_playlist, "random", &val );
1146 i_random = val.i_int;
1148 var_Get( p_playlist, "repeat", &val );
1149 i_repeat = val.i_int;
1151 var_Get( p_playlist, "loop", &val );
1156 pl_Release( p_playlist );
1158 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1159 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1160 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1161 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1162 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1163 dbus_message_iter_close_container( args, &status );