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 if( !dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL,
141 return DBUS_HANDLER_RESULT_NEED_MEMORY;
143 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
145 return DBUS_HANDLER_RESULT_NEED_MEMORY;
147 if( !dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16,
149 return DBUS_HANDLER_RESULT_NEED_MEMORY;
151 if( !dbus_message_iter_close_container( &args, &version ) )
152 return DBUS_HANDLER_RESULT_NEED_MEMORY;
156 DBUS_METHOD( PositionGet )
157 { /* returns position in milliseconds */
160 vlc_value_t position;
163 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
165 input_thread_t *p_input = p_playlist->p_input;
171 var_Get( p_input, "time", &position );
172 i_pos = position.i_time / 1000;
175 pl_Release( ((vlc_object_t*) p_this) );
180 DBUS_METHOD( PositionSet )
181 { /* set position in milliseconds */
184 vlc_value_t position;
185 playlist_t* p_playlist = NULL;
189 dbus_error_init( &error );
191 dbus_message_get_args( p_from, &error,
192 DBUS_TYPE_INT32, &i_pos,
195 if( dbus_error_is_set( &error ) )
197 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
199 dbus_error_free( &error );
200 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
202 p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
204 input_thread_t *p_input = p_playlist->p_input;
208 position.i_time = i_pos * 1000;
209 var_Set( p_input, "time", position );
212 pl_Release( ((vlc_object_t*) p_this) );
216 DBUS_METHOD( VolumeGet )
217 { /* returns volume in percentage */
220 dbus_int32_t i_dbus_vol;
221 audio_volume_t i_vol;
222 /* 2nd argument of aout_VolumeGet is int32 */
223 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
224 i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
225 ADD_INT32( &i_dbus_vol );
229 DBUS_METHOD( VolumeSet )
230 { /* set volume in percentage */
234 dbus_error_init( &error );
236 dbus_int32_t i_dbus_vol;
237 audio_volume_t i_vol;
239 dbus_message_get_args( p_from, &error,
240 DBUS_TYPE_INT32, &i_dbus_vol,
243 if( dbus_error_is_set( &error ) )
245 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
247 dbus_error_free( &error );
248 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
251 i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
252 aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
258 { /* next playlist item */
260 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
261 playlist_Next( p_playlist );
262 pl_Release( ((vlc_object_t*) p_this) );
267 { /* previous playlist item */
269 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
270 playlist_Prev( p_playlist );
271 pl_Release( ((vlc_object_t*) p_this) );
278 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
279 playlist_Stop( p_playlist );
280 pl_Release( ((vlc_object_t*) p_this) );
284 DBUS_METHOD( GetStatus )
285 { /* returns the current status as a struct of 4 ints */
287 First 0 = Playing, 1 = Paused, 2 = Stopped.
288 Second 0 = Playing linearly , 1 = Playing randomly.
289 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
290 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
295 MarshalStatus( p_this, &args, VLC_TRUE );
303 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
304 playlist_Pause( p_playlist );
305 pl_Release( p_playlist );
312 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
313 playlist_Play( p_playlist );
314 pl_Release( p_playlist );
318 DBUS_METHOD( GetCurrentMetadata )
322 playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
324 if( p_playlist->status.p_item )
325 GetInputMeta( p_playlist->status.p_item->p_input, &args );
327 pl_Release( p_playlist );
331 DBUS_METHOD( GetCaps )
336 ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
341 /* Media Player information */
343 DBUS_METHOD( Identity )
349 if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
351 ADD_STRING( &psz_identity );
352 free( psz_identity );
355 return DBUS_HANDLER_RESULT_NEED_MEMORY;
362 DBUS_METHOD( AddTrack )
363 { /* add the string to the playlist, and play it if the boolean is true */
367 dbus_error_init( &error );
368 playlist_t* p_playlist = NULL;
373 dbus_message_get_args( p_from, &error,
374 DBUS_TYPE_STRING, &psz_mrl,
375 DBUS_TYPE_BOOLEAN, &b_play,
378 if( dbus_error_is_set( &error ) )
380 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
382 dbus_error_free( &error );
383 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
386 p_playlist = pl_Yield( (vlc_object_t*) p_this );
387 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
388 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
389 PLAYLIST_END, VLC_TRUE, VLC_FALSE );
390 pl_Release( p_playlist );
395 DBUS_METHOD( GetCurrentTrack )
400 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
401 dbus_int32_t i_position = p_playlist->i_current_index;
402 pl_Release( p_playlist );
404 ADD_INT32( &i_position );
408 DBUS_METHOD( GetMetadata )
413 dbus_error_init( &error );
415 dbus_int32_t i_position;
417 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
420 dbus_message_get_args( p_from, &error,
421 DBUS_TYPE_INT32, &i_position,
424 if( dbus_error_is_set( &error ) )
427 pl_Release( p_playlist );
428 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
430 dbus_error_free( &error );
431 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
434 if( i_position <= p_playlist->items.i_size / 2 )
436 GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
440 pl_Release( p_playlist );
444 DBUS_METHOD( GetLength )
449 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
450 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
451 pl_Release( p_playlist );
453 ADD_INT32( &i_elements );
457 DBUS_METHOD( DelTrack )
462 dbus_error_init( &error );
464 dbus_int32_t i_position;
465 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
467 dbus_message_get_args( p_from, &error,
468 DBUS_TYPE_INT32, &i_position,
471 if( dbus_error_is_set( &error ) )
473 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
475 dbus_error_free( &error );
476 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
479 if( i_position <= p_playlist->items.i_size / 2 )
481 playlist_DeleteFromInput( p_playlist,
482 p_playlist->items.p_elems[i_position*2-1]->i_id,
486 pl_Release( p_playlist );
499 playlist_t* p_playlist = NULL;
501 dbus_error_init( &error );
502 dbus_message_get_args( p_from, &error,
503 DBUS_TYPE_BOOLEAN, &b_loop,
506 if( dbus_error_is_set( &error ) )
508 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
510 dbus_error_free( &error );
511 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
514 val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
515 p_playlist = pl_Yield( (vlc_object_t*) p_this );
516 var_Set ( p_playlist, "loop", val );
517 pl_Release( ((vlc_object_t*) p_this) );
522 DBUS_METHOD( Repeat )
528 dbus_bool_t b_repeat;
530 playlist_t* p_playlist = NULL;
532 dbus_error_init( &error );
533 dbus_message_get_args( p_from, &error,
534 DBUS_TYPE_BOOLEAN, &b_repeat,
537 if( dbus_error_is_set( &error ) )
539 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
541 dbus_error_free( &error );
542 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
545 val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
547 p_playlist = pl_Yield( (vlc_object_t*) p_this );
548 var_Set ( p_playlist, "repeat", val );
549 pl_Release( ((vlc_object_t*) p_this) );
554 DBUS_METHOD( Random )
560 dbus_bool_t b_random;
562 playlist_t* p_playlist = NULL;
564 dbus_error_init( &error );
565 dbus_message_get_args( p_from, &error,
566 DBUS_TYPE_BOOLEAN, &b_random,
569 if( dbus_error_is_set( &error ) )
571 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
573 dbus_error_free( &error );
574 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
577 val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
579 p_playlist = pl_Yield( (vlc_object_t*) p_this );
580 var_Set ( p_playlist, "random", val );
581 pl_Release( ((vlc_object_t*) p_this) );
585 /*****************************************************************************
586 * Introspection method
587 *****************************************************************************/
589 DBUS_METHOD( handle_introspect_root )
590 { /* handles introspection of root object */
594 ADD_STRING( &psz_introspection_xml_data_root );
598 DBUS_METHOD( handle_introspect_player )
603 ADD_STRING( &psz_introspection_xml_data_player );
607 DBUS_METHOD( handle_introspect_tracklist )
612 ADD_STRING( &psz_introspection_xml_data_tracklist );
616 /*****************************************************************************
617 * handle_*: answer to incoming messages
618 *****************************************************************************/
620 #define METHOD_FUNC( method, function ) \
621 else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
622 return function( p_conn, p_from, p_this )
624 DBUS_METHOD( handle_root )
627 if( dbus_message_is_method_call( p_from,
628 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
629 return handle_introspect_root( p_conn, p_from, p_this );
631 /* here D-Bus method's names are associated to an handler */
633 METHOD_FUNC( "Identity", Identity );
634 METHOD_FUNC( "MprisVersion", MprisVersion );
636 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
640 DBUS_METHOD( handle_player )
642 if( dbus_message_is_method_call( p_from,
643 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
644 return handle_introspect_player( p_conn, p_from, p_this );
646 /* here D-Bus method's names are associated to an handler */
648 METHOD_FUNC( "Prev", Prev );
649 METHOD_FUNC( "Next", Next );
650 METHOD_FUNC( "Quit", Quit );
651 METHOD_FUNC( "Stop", Stop );
652 METHOD_FUNC( "Play", Play );
653 METHOD_FUNC( "Pause", Pause );
654 METHOD_FUNC( "Repeat", Repeat );
655 METHOD_FUNC( "VolumeSet", VolumeSet );
656 METHOD_FUNC( "VolumeGet", VolumeGet );
657 METHOD_FUNC( "PositionSet", PositionSet );
658 METHOD_FUNC( "PositionGet", PositionGet );
659 METHOD_FUNC( "GetStatus", GetStatus );
660 METHOD_FUNC( "GetMetadata", GetCurrentMetadata );
661 METHOD_FUNC( "GetCaps", GetCaps );
663 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
666 DBUS_METHOD( handle_tracklist )
668 if( dbus_message_is_method_call( p_from,
669 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
670 return handle_introspect_tracklist( p_conn, p_from, p_this );
672 /* here D-Bus method's names are associated to an handler */
674 METHOD_FUNC( "GetMetadata", GetMetadata );
675 METHOD_FUNC( "GetCurrentTrack", GetCurrentTrack );
676 METHOD_FUNC( "GetLength", GetLength );
677 METHOD_FUNC( "AddTrack", AddTrack );
678 METHOD_FUNC( "DelTrack", DelTrack );
679 METHOD_FUNC( "Loop", Loop );
680 METHOD_FUNC( "Random", Random );
682 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
685 /*****************************************************************************
686 * Open: initialize interface
687 *****************************************************************************/
689 static int Open( vlc_object_t *p_this )
690 { /* initialisation of the connection */
691 intf_thread_t *p_intf = (intf_thread_t*)p_this;
692 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
693 playlist_t *p_playlist;
694 DBusConnection *p_conn;
700 p_sys->b_meta_read = VLC_FALSE;
701 p_sys->i_caps = CAPS_NONE;
703 dbus_error_init( &error );
705 /* connect to the session bus */
706 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
709 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
711 dbus_error_free( &error );
716 /* register a well-known name on the bus */
717 dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
718 if( dbus_error_is_set( &error ) )
720 msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
721 ": %s", error.message );
722 dbus_error_free( &error );
727 /* we register the objects */
728 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
729 &vlc_dbus_root_vtable, p_this );
730 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
731 &vlc_dbus_player_vtable, p_this );
732 dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
733 &vlc_dbus_tracklist_vtable, p_this );
735 dbus_connection_flush( p_conn );
737 p_playlist = pl_Yield( p_intf );
739 var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
740 var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
741 var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
742 var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
743 var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
744 var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
745 var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
747 pl_Release( p_playlist );
749 p_intf->pf_run = Run;
750 p_intf->p_sys = p_sys;
751 p_sys->p_conn = p_conn;
753 UpdateCaps( p_intf, VLC_FALSE );
758 /*****************************************************************************
759 * Close: destroy interface
760 *****************************************************************************/
762 static void Close ( vlc_object_t *p_this )
764 intf_thread_t *p_intf = (intf_thread_t*) p_this;
765 playlist_t *p_playlist = pl_Yield( p_intf );;
766 input_thread_t *p_input;
768 p_this->b_dead = VLC_TRUE;
771 var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
772 var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
773 var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
774 var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
775 var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
776 var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
777 var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
779 p_input = p_playlist->p_input;
782 vlc_object_yield( p_input );
783 var_DelCallback( p_input, "state", StateChange, p_intf );
784 vlc_object_release( p_input );
788 pl_Release( p_playlist );
790 dbus_connection_unref( p_intf->p_sys->p_conn );
792 free( p_intf->p_sys );
795 /*****************************************************************************
797 *****************************************************************************/
799 static void Run ( intf_thread_t *p_intf )
801 while( !intf_ShouldDie( p_intf ) )
803 msleep( INTF_IDLE_SLEEP );
804 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
808 /******************************************************************************
809 * CapsChange: player capabilities change signal
810 *****************************************************************************/
811 DBUS_SIGNAL( CapsChangeSignal )
813 SIGNAL_INIT( "CapsChange" );
816 ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
820 /******************************************************************************
821 * TrackListChange: tracklist order / length change signal
822 *****************************************************************************/
823 DBUS_SIGNAL( TrackListChangeSignal )
824 { /* emit the new tracklist lengh */
825 SIGNAL_INIT("TrackListChange");
828 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
829 dbus_int32_t i_elements = p_playlist->items.i_size / 2;
830 pl_Release( p_playlist );
832 ADD_INT32( &i_elements );
836 /*****************************************************************************
837 * TrackListChangeEmit: Emits the TrackListChange signal
838 *****************************************************************************/
839 /* FIXME: It is not called on tracklist reordering */
840 static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
841 vlc_value_t oldval, vlc_value_t newval, void *p_data )
844 intf_thread_t *p_intf = p_data;
846 if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
848 /* don't signal when items are added/removed in p_category */
849 playlist_t *p_playlist = (playlist_t*)p_this;
850 playlist_add_t *p_add = newval.p_address;
851 playlist_item_t *p_item;
852 p_item = playlist_ItemGetById( p_playlist, p_add->i_node, VLC_TRUE );
854 while( p_item->p_parent )
855 p_item = p_item->p_parent;
856 if( p_item == p_playlist->p_root_category )
863 UpdateCaps( p_intf, VLC_TRUE );
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 UpdateCaps( p_intf, VLC_FALSE );
913 if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
915 input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
918 p_sys->b_meta_read = VLC_TRUE;
919 TrackChangeSignal( p_sys->p_conn, p_item );
923 if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
924 newval.i_int == END_S )
926 StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
932 /*****************************************************************************
933 * StatusChangeEmit: Emits the StatusChange signal
934 *****************************************************************************/
935 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
936 vlc_value_t oldval, vlc_value_t newval, void *p_data )
938 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
939 VLC_UNUSED(oldval); VLC_UNUSED(newval);
940 intf_thread_t *p_intf = p_data;
945 UpdateCaps( p_intf, VLC_FALSE );
946 StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
950 /*****************************************************************************
951 * TrackChange: callback on playlist "playlist-current"
952 *****************************************************************************/
953 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
954 vlc_value_t oldval, vlc_value_t newval, void *p_data )
956 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
957 intf_sys_t *p_sys = p_intf->p_sys;
958 playlist_t *p_playlist;
959 input_thread_t *p_input = NULL;
960 input_item_t *p_item = NULL;
961 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
962 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
967 p_sys->b_meta_read = VLC_FALSE;
969 p_playlist = pl_Yield( p_intf );
971 p_input = p_playlist->p_input;
976 pl_Release( p_playlist );
980 vlc_object_yield( p_input );
982 pl_Release( p_playlist );
984 p_item = input_GetItem( p_input );
987 vlc_object_release( p_input );
991 if( input_item_IsPreparsed( p_item ) )
993 p_sys->b_meta_read = VLC_TRUE;
994 TrackChangeSignal( p_sys->p_conn, p_item );
997 var_AddCallback( p_input, "state", StateChange, p_intf );
999 vlc_object_release( p_input );
1003 /*****************************************************************************
1004 * UpdateCaps: update p_sys->i_caps
1005 ****************************************************************************/
1006 static int UpdateCaps( intf_thread_t* p_intf, vlc_bool_t b_playlist_locked )
1008 intf_sys_t* p_sys = p_intf->p_sys;
1009 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1010 playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
1011 if( !b_playlist_locked ) PL_LOCK;
1013 if( p_playlist->items.i_size > 0 )
1014 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
1016 if( p_playlist->p_input )
1018 /* XXX: if UpdateCaps() is called too early, these are
1019 * unconditionnaly true */
1020 if( var_GetBool( p_playlist->p_input, "can-pause" ) )
1021 i_caps |= CAPS_CAN_PAUSE;
1022 if( var_GetBool( p_playlist->p_input, "seekable" ) )
1023 i_caps |= CAPS_CAN_SEEK;
1026 if( !b_playlist_locked ) PL_UNLOCK;
1027 pl_Release( p_playlist );
1029 if( p_sys->b_meta_read )
1030 i_caps |= CAPS_CAN_PROVIDE_METADATA;
1032 if( i_caps != p_intf->p_sys->i_caps )
1034 p_sys->i_caps = i_caps;
1035 CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
1041 /*****************************************************************************
1042 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1043 *****************************************************************************/
1045 #define ADD_META( entry, type, data ) \
1047 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1048 NULL, &dict_entry ); \
1049 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1050 &ppsz_meta_items[entry] ); \
1051 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1052 type##_AS_STRING, &variant ); \
1053 dbus_message_iter_append_basic( &variant, \
1056 dbus_message_iter_close_container( &dict_entry, &variant ); \
1057 dbus_message_iter_close_container( &dict, &dict_entry ); }
1059 #define ADD_VLC_META_STRING( entry, item ) \
1061 char * psz = input_item_Get##item( p_input );\
1062 ADD_META( entry, DBUS_TYPE_STRING, \
1067 static int GetInputMeta( input_item_t* p_input,
1068 DBusMessageIter *args )
1070 DBusMessageIter dict, dict_entry, variant;
1071 /* We need the track length to be expressed in milli-seconds
1072 * instead of µ-seconds */
1073 dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
1075 const char* ppsz_meta_items[] =
1077 "title", "artist", "genre", "copyright", "album", "tracknum",
1078 "description", "rating", "date", "setting", "url", "language",
1079 "nowplaying", "publisher", "encodedby", "arturl", "trackid",
1080 "status", "URI", "length", "video-codec", "audio-codec",
1081 "video-bitrate", "audio-bitrate", "audio-samplerate"
1084 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1086 ADD_VLC_META_STRING( 0, Title );
1087 ADD_VLC_META_STRING( 1, Artist );
1088 ADD_VLC_META_STRING( 2, Genre );
1089 ADD_VLC_META_STRING( 3, Copyright );
1090 ADD_VLC_META_STRING( 4, Album );
1091 ADD_VLC_META_STRING( 5, TrackNum );
1092 ADD_VLC_META_STRING( 6, Description );
1093 ADD_VLC_META_STRING( 7, Rating );
1094 ADD_VLC_META_STRING( 8, Date );
1095 ADD_VLC_META_STRING( 9, Setting );
1096 ADD_VLC_META_STRING( 10, URL );
1097 ADD_VLC_META_STRING( 11, Language );
1098 ADD_VLC_META_STRING( 12, NowPlaying );
1099 ADD_VLC_META_STRING( 13, Publisher );
1100 ADD_VLC_META_STRING( 14, EncodedBy );
1101 ADD_VLC_META_STRING( 15, ArtURL );
1102 ADD_VLC_META_STRING( 16, TrackID );
1104 vlc_mutex_lock( &p_input->lock );
1105 if( p_input->p_meta )
1106 ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
1107 vlc_mutex_unlock( &p_input->lock );
1109 ADD_VLC_META_STRING( 18, URI );
1110 ADD_META( 19, DBUS_TYPE_INT64, i_length );
1112 dbus_message_iter_close_container( args, &dict );
1117 #undef ADD_VLC_META_STRING
1119 /*****************************************************************************
1120 * MarshalStatus: Fill a DBusMessage with the current player status
1121 *****************************************************************************/
1123 static int MarshalStatus( intf_thread_t* p_intf, DBusMessageIter* args,
1125 { /* This is NOT the right way to do that, it would be better to sore
1126 the status information in p_sys and update it on change, thus
1127 avoiding a long lock */
1129 DBusMessageIter status;
1130 dbus_int32_t i_state, i_random, i_repeat, i_loop;
1132 playlist_t* p_playlist = NULL;
1133 input_thread_t* p_input = NULL;
1135 p_playlist = pl_Yield( (vlc_object_t*) p_intf );
1141 p_input = p_playlist->p_input;
1144 var_Get( p_input, "state", &val );
1145 if( val.i_int >= END_S )
1147 else if( val.i_int == PAUSE_S )
1149 else if( val.i_int <= PLAYING_S )
1153 var_Get( p_playlist, "random", &val );
1154 i_random = val.i_int;
1156 var_Get( p_playlist, "repeat", &val );
1157 i_repeat = val.i_int;
1159 var_Get( p_playlist, "loop", &val );
1164 pl_Release( p_playlist );
1166 dbus_message_iter_open_container( args, DBUS_TYPE_STRUCT, NULL, &status );
1167 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_state );
1168 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_random );
1169 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_repeat );
1170 dbus_message_iter_append_basic( &status, DBUS_TYPE_INT32, &i_loop );
1171 dbus_message_iter_close_container( args, &status );