1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright © 2006-2008 Rafaël Carré
5 * Copyright © 2007-2008 Mirsal Ennaime
6 * Copyright © 2009 The VideoLAN team
9 * Authors: Rafaël Carré <funman at videolanorg>
10 * Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * D-Bus Specification:
29 * http://dbus.freedesktop.org/doc/dbus-specification.html
30 * D-Bus low-level C API (libdbus)
31 * http://dbus.freedesktop.org/doc/dbus/api/html/index.html
33 * "If you use this low-level API directly, you're signing up for some pain."
35 * MPRIS Specification version 1.0
36 * http://wiki.xmms2.xmms.se/index.php/MPRIS
39 /*****************************************************************************
41 *****************************************************************************/
47 #include <dbus/dbus.h>
50 #include <vlc_common.h>
51 #include <vlc_plugin.h>
53 #include <vlc_interface.h>
54 #include <vlc_playlist.h>
61 /*****************************************************************************
63 *****************************************************************************/
65 static int Open ( vlc_object_t
* );
66 static void Close ( vlc_object_t
* );
67 static void Run ( intf_thread_t
* );
69 static int StateChange( intf_thread_t
*, int );
70 static int TrackChange( intf_thread_t
* );
71 static int StatusChangeEmit( intf_thread_t
*);
72 static int TrackListChangeEmit( intf_thread_t
*, int, int );
74 static int AllCallback( vlc_object_t
*, const char*, vlc_value_t
, vlc_value_t
, void* );
76 static int GetInputMeta ( input_item_t
*, DBusMessageIter
* );
77 static int MarshalStatus ( intf_thread_t
*, DBusMessageIter
* );
78 static int UpdateCaps( intf_thread_t
* );
80 /* GetCaps() capabilities */
84 CAPS_CAN_GO_NEXT
= 1 << 0,
85 CAPS_CAN_GO_PREV
= 1 << 1,
86 CAPS_CAN_PAUSE
= 1 << 2,
87 CAPS_CAN_PLAY
= 1 << 3,
88 CAPS_CAN_SEEK
= 1 << 4,
89 CAPS_CAN_PROVIDE_METADATA
= 1 << 5,
90 CAPS_CAN_HAS_TRACKLIST
= 1 << 6
93 // The signal that can be get from the callbacks
98 SIGNAL_PLAYLIST_ITEM_APPEND
,
99 SIGNAL_PLAYLIST_ITEM_DELETED
,
108 DBusConnection
*p_conn
;
109 playlist_t
*p_playlist
;
113 vlc_array_t
*p_events
;
115 input_thread_t
*p_input
;
126 #define INTF ((intf_thread_t *)p_this)
127 #define PL (INTF->p_sys->p_playlist)
130 /*****************************************************************************
132 *****************************************************************************/
133 #define DBUS_UNIQUE_TEXT N_("Unique DBUS service id (org.mpris.vlc-<pid>)")
134 #define DBUS_UNIQUE_LONGTEXT N_( \
135 "Use a unique dbus service id to identify this VLC instance on the DBUS bus. " \
136 "The process identifier (PID) is added to the service name: org.mpris.vlc-<pid>" )
139 set_shortname( N_("dbus"))
140 set_category( CAT_INTERFACE
)
141 set_subcategory( SUBCAT_INTERFACE_CONTROL
)
142 set_description( N_("D-Bus control interface") )
143 set_capability( "interface", 0 )
144 set_callbacks( Open
, Close
)
145 add_bool( "dbus-unique-service-id", false, NULL
,
146 DBUS_UNIQUE_TEXT
, DBUS_UNIQUE_LONGTEXT
, true )
149 /*****************************************************************************
151 *****************************************************************************/
158 libvlc_Quit(INTF
->p_libvlc
);
162 DBUS_METHOD( MprisVersion
)
163 { /*implemented version of the mpris spec */
166 VLC_UNUSED( p_this
);
167 dbus_uint16_t i_major
= VLC_MPRIS_VERSION_MAJOR
;
168 dbus_uint16_t i_minor
= VLC_MPRIS_VERSION_MINOR
;
169 DBusMessageIter version
;
171 if( !dbus_message_iter_open_container( &args
, DBUS_TYPE_STRUCT
, NULL
,
173 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
175 if( !dbus_message_iter_append_basic( &version
, DBUS_TYPE_UINT16
,
177 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
179 if( !dbus_message_iter_append_basic( &version
, DBUS_TYPE_UINT16
,
181 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
183 if( !dbus_message_iter_close_container( &args
, &version
) )
184 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
188 DBUS_METHOD( PositionGet
)
189 { /* returns position in milliseconds */
194 input_thread_t
*p_input
= playlist_CurrentInput( PL
);
200 i_pos
= var_GetTime( p_input
, "time" ) / 1000;
201 vlc_object_release( p_input
);
207 DBUS_METHOD( PositionSet
)
208 { /* set position in milliseconds */
211 vlc_value_t position
;
215 dbus_error_init( &error
);
217 dbus_message_get_args( p_from
, &error
,
218 DBUS_TYPE_INT32
, &i_pos
,
221 if( dbus_error_is_set( &error
) )
223 msg_Err( (vlc_object_t
*) p_this
, "D-Bus message reading : %s",
225 dbus_error_free( &error
);
226 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
228 input_thread_t
*p_input
= playlist_CurrentInput( PL
);
232 position
.i_time
= ((mtime_t
)i_pos
) * 1000;
233 var_Set( p_input
, "time", position
);
234 vlc_object_release( p_input
);
239 DBUS_METHOD( VolumeGet
)
240 { /* returns volume in percentage */
243 dbus_int32_t i_dbus_vol
;
244 audio_volume_t i_vol
;
246 /* 2nd argument of aout_VolumeGet is int32 */
247 aout_VolumeGet( PL
, &i_vol
);
249 double f_vol
= 100. * i_vol
/ AOUT_VOLUME_MAX
;
250 i_dbus_vol
= round( f_vol
);
251 ADD_INT32( &i_dbus_vol
);
255 DBUS_METHOD( VolumeSet
)
256 { /* set volume in percentage */
260 dbus_error_init( &error
);
262 dbus_int32_t i_dbus_vol
;
263 audio_volume_t i_vol
;
265 dbus_message_get_args( p_from
, &error
,
266 DBUS_TYPE_INT32
, &i_dbus_vol
,
269 if( dbus_error_is_set( &error
) )
271 msg_Err( (vlc_object_t
*) p_this
, "D-Bus message reading : %s",
273 dbus_error_free( &error
);
274 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
277 double f_vol
= AOUT_VOLUME_MAX
* i_dbus_vol
/ 100.;
278 i_vol
= round( f_vol
);
279 aout_VolumeSet( PL
, i_vol
);
284 { /* next playlist item */
291 { /* previous playlist item */
304 DBUS_METHOD( GetStatus
)
305 { /* returns the current status as a struct of 4 ints */
307 First 0 = Playing, 1 = Paused, 2 = Stopped.
308 Second 0 = Playing linearly , 1 = Playing randomly.
309 Third 0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
310 Fourth 0 = Stop playing once the last element has been played, 1 = Never give up playing *
315 MarshalStatus( p_this
, &args
);
323 playlist_Pause( PL
);
331 input_thread_t
*p_input
= playlist_CurrentInput( PL
);
336 input_Control( p_input
, INPUT_SET_POSITION
, i_pos
);
337 vlc_object_release( p_input
);
345 DBUS_METHOD( GetCurrentMetadata
)
349 playlist_t
*p_playlist
= PL
;
352 playlist_item_t
* p_item
= playlist_CurrentPlayingItem( p_playlist
);
354 GetInputMeta( p_item
->p_input
, &args
);
359 DBUS_METHOD( GetCaps
)
364 ADD_INT32( &INTF
->p_sys
->i_caps
);
369 /* Media Player information */
371 DBUS_METHOD( Identity
)
378 if( asprintf( &psz_identity
, "%s %s", PACKAGE
, VERSION
) != -1 )
380 ADD_STRING( &psz_identity
);
381 free( psz_identity
);
384 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
391 DBUS_METHOD( AddTrack
)
392 { /* add the string to the playlist, and play it if the boolean is true */
397 dbus_error_init( &error
);
402 dbus_message_get_args( p_from
, &error
,
403 DBUS_TYPE_STRING
, &psz_mrl
,
404 DBUS_TYPE_BOOLEAN
, &b_play
,
407 if( dbus_error_is_set( &error
) )
409 msg_Err( (vlc_object_t
*) p_this
, "D-Bus message reading : %s",
411 dbus_error_free( &error
);
412 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
415 playlist_Add( PL
, psz_mrl
, NULL
, PLAYLIST_APPEND
|
416 ( ( b_play
== TRUE
) ? PLAYLIST_GO
: 0 ) ,
417 PLAYLIST_END
, true, false );
419 dbus_int32_t i_success
= 0;
420 ADD_INT32( &i_success
);
425 DBUS_METHOD( GetCurrentTrack
)
430 playlist_t
*p_playlist
= PL
;
433 dbus_int32_t i_position
= PL
->i_current_index
;
436 ADD_INT32( &i_position
);
440 DBUS_METHOD( GetMetadata
)
445 dbus_error_init( &error
);
447 dbus_int32_t i_position
;
448 playlist_t
*p_playlist
= PL
;
450 dbus_message_get_args( p_from
, &error
,
451 DBUS_TYPE_INT32
, &i_position
,
454 if( dbus_error_is_set( &error
) )
456 msg_Err( (vlc_object_t
*) p_this
, "D-Bus message reading : %s",
458 dbus_error_free( &error
);
459 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
463 if( i_position
< p_playlist
->current
.i_size
)
465 GetInputMeta( p_playlist
->current
.p_elems
[i_position
]->p_input
, &args
);
472 DBUS_METHOD( GetLength
)
476 playlist_t
*p_playlist
= PL
;
479 dbus_int32_t i_elements
= PL
->current
.i_size
;
482 ADD_INT32( &i_elements
);
486 DBUS_METHOD( DelTrack
)
491 dbus_error_init( &error
);
493 dbus_int32_t i_position
;
494 playlist_t
*p_playlist
= PL
;
496 dbus_message_get_args( p_from
, &error
,
497 DBUS_TYPE_INT32
, &i_position
,
500 if( dbus_error_is_set( &error
) )
502 msg_Err( (vlc_object_t
*) p_this
, "D-Bus message reading : %s",
504 dbus_error_free( &error
);
505 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
509 if( i_position
< p_playlist
->current
.i_size
)
511 playlist_DeleteFromInput( p_playlist
,
512 p_playlist
->current
.p_elems
[i_position
]->p_input
,
520 DBUS_METHOD( SetLoop
)
528 dbus_error_init( &error
);
529 dbus_message_get_args( p_from
, &error
,
530 DBUS_TYPE_BOOLEAN
, &b_loop
,
533 if( dbus_error_is_set( &error
) )
535 msg_Err( (vlc_object_t
*) p_this
, "D-Bus message reading : %s",
537 dbus_error_free( &error
);
538 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
541 var_SetBool( PL
, "loop", ( b_loop
== TRUE
) );
546 DBUS_METHOD( Repeat
)
552 dbus_bool_t b_repeat
;
554 dbus_error_init( &error
);
555 dbus_message_get_args( p_from
, &error
,
556 DBUS_TYPE_BOOLEAN
, &b_repeat
,
559 if( dbus_error_is_set( &error
) )
561 msg_Err( (vlc_object_t
*) p_this
, "D-Bus message reading : %s",
563 dbus_error_free( &error
);
564 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
567 var_SetBool( PL
, "repeat", ( b_repeat
== TRUE
) );
572 DBUS_METHOD( SetRandom
)
578 dbus_bool_t b_random
;
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",
589 dbus_error_free( &error
);
590 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
593 var_SetBool( PL
, "random", ( b_random
== TRUE
) );
597 /*****************************************************************************
598 * Introspection method
599 *****************************************************************************/
601 DBUS_METHOD( handle_introspect_root
)
602 { /* handles introspection of root object */
606 ADD_STRING( &psz_introspection_xml_data_root
);
610 DBUS_METHOD( handle_introspect_player
)
615 ADD_STRING( &psz_introspection_xml_data_player
);
619 DBUS_METHOD( handle_introspect_tracklist
)
624 ADD_STRING( &psz_introspection_xml_data_tracklist
);
628 /*****************************************************************************
629 * handle_*: answer to incoming messages
630 *****************************************************************************/
632 #define METHOD_FUNC( interface, method, function ) \
633 else if( dbus_message_is_method_call( p_from, interface, method ) )\
634 return function( p_conn, p_from, p_this )
636 DBUS_METHOD( handle_root
)
639 if( dbus_message_is_method_call( p_from
,
640 DBUS_INTERFACE_INTROSPECTABLE
, "Introspect" ) )
641 return handle_introspect_root( p_conn
, p_from
, p_this
);
643 /* here D-Bus method's names are associated to an handler */
645 METHOD_FUNC( MPRIS_DBUS_ROOT_INTERFACE
, "Identity", Identity
);
646 METHOD_FUNC( MPRIS_DBUS_ROOT_INTERFACE
, "MprisVersion", MprisVersion
);
647 METHOD_FUNC( MPRIS_DBUS_ROOT_INTERFACE
, "Quit", Quit
);
649 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
653 DBUS_METHOD( handle_player
)
655 if( dbus_message_is_method_call( p_from
,
656 DBUS_INTERFACE_INTROSPECTABLE
, "Introspect" ) )
657 return handle_introspect_player( p_conn
, p_from
, p_this
);
659 /* here D-Bus method's names are associated to an handler */
661 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "Prev", Prev
);
662 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "Next", Next
);
663 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "Stop", Stop
);
664 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "Play", Play
);
665 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "Pause", Pause
);
666 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "Repeat", Repeat
);
667 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "VolumeSet", VolumeSet
);
668 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "VolumeGet", VolumeGet
);
669 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "PositionSet", PositionSet
);
670 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "PositionGet", PositionGet
);
671 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "GetStatus", GetStatus
);
672 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "GetMetadata", GetCurrentMetadata
);
673 METHOD_FUNC( MPRIS_DBUS_PLAYER_INTERFACE
, "GetCaps", GetCaps
);
675 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
678 DBUS_METHOD( handle_tracklist
)
680 if( dbus_message_is_method_call( p_from
,
681 DBUS_INTERFACE_INTROSPECTABLE
, "Introspect" ) )
682 return handle_introspect_tracklist( p_conn
, p_from
, p_this
);
684 /* here D-Bus method's names are associated to an handler */
686 METHOD_FUNC( MPRIS_DBUS_TRACKLIST_INTERFACE
, "GetMetadata", GetMetadata
);
687 METHOD_FUNC( MPRIS_DBUS_TRACKLIST_INTERFACE
, "GetCurrentTrack", GetCurrentTrack
);
688 METHOD_FUNC( MPRIS_DBUS_TRACKLIST_INTERFACE
, "GetLength", GetLength
);
689 METHOD_FUNC( MPRIS_DBUS_TRACKLIST_INTERFACE
, "AddTrack", AddTrack
);
690 METHOD_FUNC( MPRIS_DBUS_TRACKLIST_INTERFACE
, "DelTrack", DelTrack
);
691 METHOD_FUNC( MPRIS_DBUS_TRACKLIST_INTERFACE
, "SetLoop", SetLoop
);
692 METHOD_FUNC( MPRIS_DBUS_TRACKLIST_INTERFACE
, "SetRandom", SetRandom
);
694 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
697 /*****************************************************************************
698 * Open: initialize interface
699 *****************************************************************************/
701 static int Open( vlc_object_t
*p_this
)
702 { /* initialisation of the connection */
703 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
704 intf_sys_t
*p_sys
= malloc( sizeof( intf_sys_t
) );
705 playlist_t
*p_playlist
;
706 DBusConnection
*p_conn
;
708 char *psz_service_name
= NULL
;
713 p_sys
->b_meta_read
= false;
714 p_sys
->i_caps
= CAPS_NONE
;
715 p_sys
->b_dead
= false;
716 p_sys
->p_input
= NULL
;
718 p_sys
->b_unique
= var_CreateGetBool( p_intf
, "dbus-unique-service-id" );
719 if( p_sys
->b_unique
)
721 if( asprintf( &psz_service_name
, "%s-%d",
722 VLC_MPRIS_DBUS_SERVICE
, getpid() ) < 0 )
730 psz_service_name
= strdup(VLC_MPRIS_DBUS_SERVICE
);
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
, psz_service_name
, 0, &error
);
748 if( dbus_error_is_set( &error
) )
750 msg_Err( p_this
, "Error requesting service %s: %s",
751 psz_service_name
, error
.message
);
752 dbus_error_free( &error
);
753 free( psz_service_name
);
757 msg_Info( p_intf
, "listening on dbus as: %s", psz_service_name
);
758 free( psz_service_name
);
760 /* we register the objects */
761 dbus_connection_register_object_path( p_conn
, MPRIS_DBUS_ROOT_PATH
,
762 &vlc_dbus_root_vtable
, p_this
);
763 dbus_connection_register_object_path( p_conn
, MPRIS_DBUS_PLAYER_PATH
,
764 &vlc_dbus_player_vtable
, p_this
);
765 dbus_connection_register_object_path( p_conn
, MPRIS_DBUS_TRACKLIST_PATH
,
766 &vlc_dbus_tracklist_vtable
, p_this
);
768 dbus_connection_flush( p_conn
);
770 p_intf
->pf_run
= Run
;
771 p_intf
->p_sys
= p_sys
;
772 p_sys
->p_conn
= p_conn
;
773 p_sys
->p_events
= vlc_array_new();
774 vlc_mutex_init( &p_sys
->lock
);
776 p_playlist
= pl_Get( p_intf
);
777 p_sys
->p_playlist
= p_playlist
;
779 var_AddCallback( p_playlist
, "item-current", AllCallback
, p_intf
);
780 var_AddCallback( p_playlist
, "intf-change", AllCallback
, p_intf
);
781 var_AddCallback( p_playlist
, "playlist-item-append", AllCallback
, p_intf
);
782 var_AddCallback( p_playlist
, "playlist-item-deleted", AllCallback
, p_intf
);
783 var_AddCallback( p_playlist
, "random", AllCallback
, p_intf
);
784 var_AddCallback( p_playlist
, "repeat", AllCallback
, p_intf
);
785 var_AddCallback( p_playlist
, "loop", AllCallback
, p_intf
);
787 UpdateCaps( p_intf
);
792 /*****************************************************************************
793 * Close: destroy interface
794 *****************************************************************************/
796 static void Close ( vlc_object_t
*p_this
)
798 intf_thread_t
*p_intf
= (intf_thread_t
*) p_this
;
799 intf_sys_t
*p_sys
= p_intf
->p_sys
;
800 playlist_t
*p_playlist
= p_sys
->p_playlist
;
802 var_DelCallback( p_playlist
, "item-current", AllCallback
, p_intf
);
803 var_DelCallback( p_playlist
, "intf-change", AllCallback
, p_intf
);
804 var_DelCallback( p_playlist
, "playlist-item-append", AllCallback
, p_intf
);
805 var_DelCallback( p_playlist
, "playlist-item-deleted", AllCallback
, p_intf
);
806 var_DelCallback( p_playlist
, "random", AllCallback
, p_intf
);
807 var_DelCallback( p_playlist
, "repeat", AllCallback
, p_intf
);
808 var_DelCallback( p_playlist
, "loop", AllCallback
, p_intf
);
812 var_DelCallback( p_sys
->p_input
, "state", AllCallback
, p_intf
);
813 vlc_object_release( p_sys
->p_input
);
816 dbus_connection_unref( p_sys
->p_conn
);
818 // Free the events array
819 for( int i
= 0; i
< vlc_array_count( p_sys
->p_events
); i
++ )
821 callback_info_t
* info
= vlc_array_item_at_index( p_sys
->p_events
, i
);
824 vlc_mutex_destroy( &p_sys
->lock
);
825 vlc_array_destroy( p_sys
->p_events
);
829 /*****************************************************************************
831 *****************************************************************************/
833 static void Run ( intf_thread_t
*p_intf
)
837 if( dbus_connection_get_dispatch_status(p_intf
->p_sys
->p_conn
)
838 == DBUS_DISPATCH_COMPLETE
)
839 msleep( INTF_IDLE_SLEEP
);
840 int canc
= vlc_savecancel();
841 dbus_connection_read_write_dispatch( p_intf
->p_sys
->p_conn
, 0 );
843 /* Get the list of events to process
845 * We can't keep the lock on p_intf->p_sys->p_events, else we risk a
847 * The signal functions could lock mutex X while p_events is locked;
848 * While some other function in vlc (playlist) might lock mutex X
849 * and then set a variable which would call AllCallback(), which itself
850 * needs to lock p_events to add a new event.
852 vlc_mutex_lock( &p_intf
->p_sys
->lock
);
853 int i_events
= vlc_array_count( p_intf
->p_sys
->p_events
);
854 callback_info_t
* info
[i_events
];
855 for( int i
= i_events
- 1; i
>= 0; i
-- )
857 info
[i
] = vlc_array_item_at_index( p_intf
->p_sys
->p_events
, i
);
858 vlc_array_remove( p_intf
->p_sys
->p_events
, i
);
860 vlc_mutex_unlock( &p_intf
->p_sys
->lock
);
862 for( int i
= 0; i
< i_events
; i
++ )
864 switch( info
[i
]->signal
)
866 case SIGNAL_ITEM_CURRENT
:
867 TrackChange( p_intf
);
869 case SIGNAL_INTF_CHANGE
:
870 case SIGNAL_PLAYLIST_ITEM_APPEND
:
871 case SIGNAL_PLAYLIST_ITEM_DELETED
:
872 TrackListChangeEmit( p_intf
, info
[i
]->signal
, info
[i
]->i_node
);
877 StatusChangeEmit( p_intf
);
880 StateChange( p_intf
, info
[i
]->i_input_state
);
887 vlc_restorecancel( canc
);
892 // Get all the callbacks
893 static int AllCallback( vlc_object_t
*p_this
, const char *psz_var
,
894 vlc_value_t oldval
, vlc_value_t newval
, void *p_data
)
898 intf_thread_t
*p_intf
= (intf_thread_t
*)p_data
;
900 callback_info_t
*info
= malloc( sizeof( callback_info_t
) );
904 // Wich event is it ?
905 if( !strcmp( "item-current", psz_var
) )
906 info
->signal
= SIGNAL_ITEM_CURRENT
;
907 else if( !strcmp( "intf-change", psz_var
) )
908 info
->signal
= SIGNAL_INTF_CHANGE
;
909 else if( !strcmp( "playlist-item-append", psz_var
) )
911 info
->signal
= SIGNAL_PLAYLIST_ITEM_APPEND
;
912 info
->i_node
= ((playlist_add_t
*)newval
.p_address
)->i_node
;
914 else if( !strcmp( "playlist-item-deleted", psz_var
) )
915 info
->signal
= SIGNAL_PLAYLIST_ITEM_DELETED
;
916 else if( !strcmp( "random", psz_var
) )
917 info
->signal
= SIGNAL_RANDOM
;
918 else if( !strcmp( "repeat", psz_var
) )
919 info
->signal
= SIGNAL_REPEAT
;
920 else if( !strcmp( "loop", psz_var
) )
921 info
->signal
= SIGNAL_LOOP
;
922 else if( !strcmp( "state", psz_var
) )
924 info
->signal
= SIGNAL_STATE
;
925 info
->i_input_state
= newval
.i_int
;
931 vlc_mutex_lock( &p_intf
->p_sys
->lock
);
932 vlc_array_append( p_intf
->p_sys
->p_events
, info
);
933 vlc_mutex_unlock( &p_intf
->p_sys
->lock
);
937 /******************************************************************************
938 * CapsChange: player capabilities change signal
939 *****************************************************************************/
940 DBUS_SIGNAL( CapsChangeSignal
)
942 SIGNAL_INIT( MPRIS_DBUS_PLAYER_INTERFACE
,
943 MPRIS_DBUS_PLAYER_PATH
,
948 ADD_INT32( &((intf_thread_t
*)p_data
)->p_sys
->i_caps
);
952 /******************************************************************************
953 * TrackListChange: tracklist order / length change signal
954 *****************************************************************************/
955 DBUS_SIGNAL( TrackListChangeSignal
)
956 { /* emit the new tracklist lengh */
957 SIGNAL_INIT( MPRIS_DBUS_TRACKLIST_INTERFACE
,
958 MPRIS_DBUS_TRACKLIST_PATH
,
963 playlist_t
*p_playlist
= ((intf_thread_t
*)p_data
)->p_sys
->p_playlist
;
965 dbus_int32_t i_elements
= p_playlist
->current
.i_size
;
968 ADD_INT32( &i_elements
);
972 /*****************************************************************************
973 * TrackListChangeEmit: Emits the TrackListChange signal
974 *****************************************************************************/
975 /* FIXME: It is not called on tracklist reordering */
976 static int TrackListChangeEmit( intf_thread_t
*p_intf
, int signal
, int i_node
)
978 // "playlist-item-append"
979 if( signal
== SIGNAL_PLAYLIST_ITEM_APPEND
)
981 /* don't signal when items are added/removed in p_category */
982 playlist_t
*p_playlist
= p_intf
->p_sys
->p_playlist
;
984 playlist_item_t
*p_item
= playlist_ItemGetById( p_playlist
, i_node
);
986 while( p_item
->p_parent
)
987 p_item
= p_item
->p_parent
;
988 if( p_item
== p_playlist
->p_root_category
)
996 if( p_intf
->p_sys
->b_dead
)
999 UpdateCaps( p_intf
);
1000 TrackListChangeSignal( p_intf
->p_sys
->p_conn
, p_intf
);
1003 /*****************************************************************************
1004 * TrackChange: Playlist item change callback
1005 *****************************************************************************/
1007 DBUS_SIGNAL( TrackChangeSignal
)
1008 { /* emit the metadata of the new item */
1009 SIGNAL_INIT( MPRIS_DBUS_PLAYER_INTERFACE
,
1010 MPRIS_DBUS_PLAYER_PATH
,
1015 input_item_t
*p_item
= (input_item_t
*) p_data
;
1016 GetInputMeta ( p_item
, &args
);
1021 /*****************************************************************************
1022 * StatusChange: Player status change signal
1023 *****************************************************************************/
1025 DBUS_SIGNAL( StatusChangeSignal
)
1026 { /* send the updated status info on the bus */
1027 SIGNAL_INIT( MPRIS_DBUS_PLAYER_INTERFACE
,
1028 MPRIS_DBUS_PLAYER_PATH
,
1033 /* we're called from a callback of input_thread_t, so it can not be
1034 * destroyed before we return */
1035 MarshalStatus( (intf_thread_t
*) p_data
, &args
);
1040 /*****************************************************************************
1041 * StateChange: callback on input "state"
1042 *****************************************************************************/
1043 //static int StateChange( vlc_object_t *p_this, const char* psz_var,
1044 // vlc_value_t oldval, vlc_value_t newval, void *p_data )
1045 static int StateChange( intf_thread_t
*p_intf
, int i_input_state
)
1047 intf_sys_t
*p_sys
= p_intf
->p_sys
;
1048 playlist_t
*p_playlist
= p_sys
->p_playlist
;
1049 input_thread_t
*p_input
;
1050 input_item_t
*p_item
;
1052 if( p_intf
->p_sys
->b_dead
)
1055 UpdateCaps( p_intf
);
1057 if( !p_sys
->b_meta_read
&& i_input_state
== PLAYING_S
)
1059 p_input
= playlist_CurrentInput( p_playlist
);
1062 p_item
= input_GetItem( p_input
);
1065 p_sys
->b_meta_read
= true;
1066 TrackChangeSignal( p_sys
->p_conn
, p_item
);
1068 vlc_object_release( p_input
);
1072 if( i_input_state
== PLAYING_S
|| i_input_state
== PAUSE_S
||
1073 i_input_state
== END_S
)
1075 StatusChangeSignal( p_sys
->p_conn
, p_intf
);
1081 /*****************************************************************************
1082 * StatusChangeEmit: Emits the StatusChange signal
1083 *****************************************************************************/
1084 static int StatusChangeEmit( intf_thread_t
* p_intf
)
1086 if( p_intf
->p_sys
->b_dead
)
1089 UpdateCaps( p_intf
);
1090 StatusChangeSignal( p_intf
->p_sys
->p_conn
, p_intf
);
1094 /*****************************************************************************
1095 * TrackChange: callback on playlist "item-current"
1096 *****************************************************************************/
1097 static int TrackChange( intf_thread_t
*p_intf
)
1099 intf_sys_t
*p_sys
= p_intf
->p_sys
;
1100 playlist_t
*p_playlist
= p_sys
->p_playlist
;
1101 input_thread_t
*p_input
= NULL
;
1102 input_item_t
*p_item
= NULL
;
1104 if( p_intf
->p_sys
->b_dead
)
1107 if( p_sys
->p_input
)
1109 var_DelCallback( p_sys
->p_input
, "state", AllCallback
, p_intf
);
1110 vlc_object_release( p_sys
->p_input
);
1111 p_sys
->p_input
= NULL
;
1114 p_sys
->b_meta_read
= false;
1116 p_input
= playlist_CurrentInput( p_playlist
);
1122 p_item
= input_GetItem( p_input
);
1125 vlc_object_release( p_input
);
1126 return VLC_EGENERIC
;
1129 if( input_item_IsPreparsed( p_item
) )
1131 p_sys
->b_meta_read
= true;
1132 TrackChangeSignal( p_sys
->p_conn
, p_item
);
1135 p_sys
->p_input
= p_input
;
1136 var_AddCallback( p_input
, "state", AllCallback
, p_intf
);
1141 /*****************************************************************************
1142 * UpdateCaps: update p_sys->i_caps
1143 * This function have to be called with the playlist unlocked
1144 ****************************************************************************/
1145 static int UpdateCaps( intf_thread_t
* p_intf
)
1147 intf_sys_t
* p_sys
= p_intf
->p_sys
;
1148 dbus_int32_t i_caps
= CAPS_CAN_HAS_TRACKLIST
;
1149 playlist_t
* p_playlist
= p_sys
->p_playlist
;
1152 if( p_playlist
->current
.i_size
> 0 )
1153 i_caps
|= CAPS_CAN_PLAY
| CAPS_CAN_GO_PREV
| CAPS_CAN_GO_NEXT
;
1156 input_thread_t
* p_input
= playlist_CurrentInput( p_playlist
);
1159 /* XXX: if UpdateCaps() is called too early, these are
1160 * unconditionnaly true */
1161 if( var_GetBool( p_input
, "can-pause" ) )
1162 i_caps
|= CAPS_CAN_PAUSE
;
1163 if( var_GetBool( p_input
, "can-seek" ) )
1164 i_caps
|= CAPS_CAN_SEEK
;
1165 vlc_object_release( p_input
);
1168 if( p_sys
->b_meta_read
)
1169 i_caps
|= CAPS_CAN_PROVIDE_METADATA
;
1171 if( i_caps
!= p_intf
->p_sys
->i_caps
)
1173 p_sys
->i_caps
= i_caps
;
1174 CapsChangeSignal( p_intf
->p_sys
->p_conn
, (vlc_object_t
*)p_intf
);
1180 /*****************************************************************************
1181 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1182 *****************************************************************************/
1184 #define ADD_META( entry, type, data ) \
1186 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1187 NULL, &dict_entry ); \
1188 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1189 &ppsz_meta_items[entry] ); \
1190 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1191 type##_AS_STRING, &variant ); \
1192 dbus_message_iter_append_basic( &variant, \
1195 dbus_message_iter_close_container( &dict_entry, &variant ); \
1196 dbus_message_iter_close_container( &dict, &dict_entry ); }
1198 #define ADD_VLC_META_STRING( entry, item ) \
1200 char * psz = input_item_Get##item( p_input );\
1201 ADD_META( entry, DBUS_TYPE_STRING, \
1206 static int GetInputMeta( input_item_t
* p_input
,
1207 DBusMessageIter
*args
)
1209 DBusMessageIter dict
, dict_entry
, variant
;
1210 /** The duration of the track can be expressed in second, milli-seconds and
1212 dbus_int64_t i_mtime
= input_item_GetDuration( p_input
);
1213 dbus_uint32_t i_time
= i_mtime
/ 1000000;
1214 dbus_int64_t i_length
= i_mtime
/ 1000;
1216 const char* ppsz_meta_items
[] =
1218 /* Official MPRIS metas */
1219 "location", "title", "artist", "album", "tracknumber", "time", "mtime",
1220 "genre", "rating", "date", "arturl",
1221 "audio-bitrate", "audio-samplerate", "video-bitrate",
1222 /* VLC specifics metas */
1223 "audio-codec", "copyright", "description", "encodedby", "language", "length",
1224 "nowplaying", "publisher", "setting", "status", "trackid", "url",
1228 dbus_message_iter_open_container( args
, DBUS_TYPE_ARRAY
, "{sv}", &dict
);
1230 ADD_VLC_META_STRING( 0, URI
);
1231 ADD_VLC_META_STRING( 1, Title
);
1232 ADD_VLC_META_STRING( 2, Artist
);
1233 ADD_VLC_META_STRING( 3, Album
);
1234 ADD_VLC_META_STRING( 4, TrackNum
);
1235 ADD_META( 5, DBUS_TYPE_UINT32
, i_time
);
1236 ADD_META( 6, DBUS_TYPE_UINT32
, i_mtime
);
1237 ADD_VLC_META_STRING( 7, Genre
);
1238 ADD_VLC_META_STRING( 8, Rating
);
1239 ADD_VLC_META_STRING( 9, Date
);
1240 ADD_VLC_META_STRING( 10, ArtURL
);
1242 ADD_VLC_META_STRING( 15, Copyright
);
1243 ADD_VLC_META_STRING( 16, Description
);
1244 ADD_VLC_META_STRING( 17, EncodedBy
);
1245 ADD_VLC_META_STRING( 18, Language
);
1246 ADD_META( 19, DBUS_TYPE_INT64
, i_length
);
1247 ADD_VLC_META_STRING( 20, NowPlaying
);
1248 ADD_VLC_META_STRING( 21, Publisher
);
1249 ADD_VLC_META_STRING( 22, Setting
);
1250 ADD_VLC_META_STRING( 24, TrackID
);
1251 ADD_VLC_META_STRING( 25, URL
);
1253 vlc_mutex_lock( &p_input
->lock
);
1254 if( p_input
->p_meta
)
1256 int i_status
= vlc_meta_GetStatus( p_input
->p_meta
);
1257 ADD_META( 23, DBUS_TYPE_INT32
, i_status
);
1259 vlc_mutex_unlock( &p_input
->lock
);
1261 dbus_message_iter_close_container( args
, &dict
);
1266 #undef ADD_VLC_META_STRING
1268 /*****************************************************************************
1269 * MarshalStatus: Fill a DBusMessage with the current player status
1270 *****************************************************************************/
1272 static int MarshalStatus( intf_thread_t
* p_intf
, DBusMessageIter
* args
)
1273 { /* This is NOT the right way to do that, it would be better to sore
1274 the status information in p_sys and update it on change, thus
1275 avoiding a long lock */
1277 DBusMessageIter status
;
1278 dbus_int32_t i_state
, i_random
, i_repeat
, i_loop
;
1280 playlist_t
* p_playlist
= p_intf
->p_sys
->p_playlist
;
1281 input_thread_t
* p_input
= NULL
;
1285 p_input
= playlist_CurrentInput( p_playlist
);
1288 i_val
= var_GetInteger( p_input
, "state" );
1289 if( i_val
>= END_S
)
1291 else if( i_val
== PAUSE_S
)
1293 else if( i_val
<= PLAYING_S
)
1295 vlc_object_release( p_input
);
1298 i_random
= var_CreateGetBool( p_playlist
, "random" );
1300 i_repeat
= var_CreateGetBool( p_playlist
, "repeat" );
1302 i_loop
= var_CreateGetBool( p_playlist
, "loop" );
1304 dbus_message_iter_open_container( args
, DBUS_TYPE_STRUCT
, NULL
, &status
);
1305 dbus_message_iter_append_basic( &status
, DBUS_TYPE_INT32
, &i_state
);
1306 dbus_message_iter_append_basic( &status
, DBUS_TYPE_INT32
, &i_random
);
1307 dbus_message_iter_append_basic( &status
, DBUS_TYPE_INT32
, &i_repeat
);
1308 dbus_message_iter_append_basic( &status
, DBUS_TYPE_INT32
, &i_loop
);
1309 dbus_message_iter_close_container( args
, &status
);