1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright © 2006-2008 Rafaël Carré
5 * Copyright © 2007-2010 Mirsal Ennaime
6 * Copyright © 2009-2010 The VideoLAN team
9 * Authors: Rafaël Carré <funman at videolanorg>
10 * Mirsal Ennaime <mirsal at mirsal fr>
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>
49 #include "dbus_common.h"
50 #include "dbus_root.h"
51 #include "dbus_player.h"
52 #include "dbus_tracklist.h"
53 #include "dbus_introspect.h"
55 #include <vlc_common.h>
56 #include <vlc_plugin.h>
57 #include <vlc_interface.h>
58 #include <vlc_playlist.h>
60 #include <vlc_mtime.h>
71 /*****************************************************************************
73 *****************************************************************************/
85 DBusTimeout *p_timeout;
94 static int Open ( vlc_object_t * );
95 static void Close ( vlc_object_t * );
96 static void Run ( intf_thread_t * );
98 static int TrackChange( intf_thread_t * );
99 static int AllCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
101 static void dispatch_status_cb( DBusConnection *p_conn,
102 DBusDispatchStatus i_status,
105 static dbus_bool_t add_timeout ( DBusTimeout *p_timeout, void *p_data );
106 static dbus_bool_t add_watch ( DBusWatch *p_watch, void *p_data );
108 static void remove_timeout ( DBusTimeout *p_timeout, void *p_data );
109 static void remove_watch ( DBusWatch *p_watch, void *p_data );
111 static void timeout_toggled ( DBusTimeout *p_timeout, void *p_data );
112 static void watch_toggled ( DBusWatch *p_watch, void *p_data );
114 static void wakeup_main_loop( void *p_data );
116 static int UpdateTimeouts( intf_thread_t *p_intf, mtime_t i_lastrun );
118 static void ProcessEvents ( intf_thread_t *p_intf,
119 callback_info_t **p_events,
122 static void ProcessWatches ( intf_thread_t *p_intf,
123 DBusWatch **p_watches,
125 struct pollfd *p_fds,
128 static void ProcessTimeouts( intf_thread_t *p_intf,
129 DBusTimeout **p_timeouts,
132 static void DispatchDBusMessages( intf_thread_t *p_intf );
134 /*****************************************************************************
136 *****************************************************************************/
137 #define DBUS_UNIQUE_TEXT N_("Unique DBUS service id (org.mpris.vlc-<pid>)")
138 #define DBUS_UNIQUE_LONGTEXT N_( \
139 "Use a unique dbus service id to identify this VLC instance on the DBUS bus. " \
140 "The process identifier (PID) is added to the service name: org.mpris.vlc-<pid>" )
143 set_shortname( N_("dbus"))
144 set_category( CAT_INTERFACE )
145 set_subcategory( SUBCAT_INTERFACE_CONTROL )
146 set_description( N_("D-Bus control interface") )
147 set_capability( "interface", 0 )
148 set_callbacks( Open, Close )
149 add_bool( "dbus-unique-service-id", false,
150 DBUS_UNIQUE_TEXT, DBUS_UNIQUE_LONGTEXT, true )
153 /*****************************************************************************
154 * Open: initialize interface
155 *****************************************************************************/
157 static int Open( vlc_object_t *p_this )
159 intf_thread_t *p_intf = (intf_thread_t*)p_this;
161 /* initialisation of the connection */
162 if( !dbus_threads_init_default() )
165 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
166 if( unlikely(!p_sys) )
169 playlist_t *p_playlist;
170 DBusConnection *p_conn;
171 p_sys->i_player_caps = PLAYER_CAPS_NONE;
172 p_sys->i_playing_state = PLAYBACK_STATE_INVALID;
174 if( vlc_pipe( p_sys->p_pipe_fds ) )
177 msg_Err( p_intf, "Could not create pipe" );
181 char psz_service_name[sizeof(DBUS_MPRIS_BUS_NAME) + 12];
182 p_sys->b_unique = var_CreateGetBool( p_intf, "dbus-unique-service-id" );
183 if( p_sys->b_unique )
184 snprintf( psz_service_name, sizeof( psz_service_name ),
185 DBUS_MPRIS_BUS_NAME"-%d", getpid() );
187 strcpy( psz_service_name, DBUS_MPRIS_BUS_NAME );
190 dbus_error_init( &error );
192 /* connect privately to the session bus
193 * the connection will not be shared with other vlc modules which use dbus,
194 * thus avoiding a whole class of concurrency issues */
195 p_conn = dbus_bus_get_private( DBUS_BUS_SESSION, &error );
198 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
200 dbus_error_free( &error );
205 dbus_connection_set_exit_on_disconnect( p_conn, FALSE );
207 /* register a well-known name on the bus */
208 dbus_bus_request_name( p_conn, psz_service_name, 0, &error );
209 if( dbus_error_is_set( &error ) )
211 msg_Err( p_this, "Error requesting service %s: %s",
212 psz_service_name, error.message );
213 dbus_error_free( &error );
217 msg_Info( p_intf, "listening on dbus as: %s", psz_service_name );
219 /* Register the entry point object path */
220 dbus_connection_register_object_path( p_conn, DBUS_MPRIS_OBJECT_PATH,
221 &dbus_mpris_vtable, p_this );
223 dbus_connection_flush( p_conn );
225 p_intf->pf_run = Run;
226 p_intf->p_sys = p_sys;
227 p_sys->p_conn = p_conn;
228 p_sys->p_events = vlc_array_new();
229 p_sys->p_timeouts = vlc_array_new();
230 p_sys->p_watches = vlc_array_new();
231 vlc_mutex_init( &p_sys->lock );
233 p_playlist = pl_Get( p_intf );
234 p_sys->p_playlist = p_playlist;
236 var_AddCallback( p_playlist, "item-current", AllCallback, p_intf );
237 var_AddCallback( p_playlist, "intf-change", AllCallback, p_intf );
238 var_AddCallback( p_playlist, "volume", AllCallback, p_intf );
239 var_AddCallback( p_playlist, "mute", AllCallback, p_intf );
240 var_AddCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
241 var_AddCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
242 var_AddCallback( p_playlist, "random", AllCallback, p_intf );
243 var_AddCallback( p_playlist, "repeat", AllCallback, p_intf );
244 var_AddCallback( p_playlist, "loop", AllCallback, p_intf );
246 dbus_connection_set_dispatch_status_function( p_conn,
250 if( !dbus_connection_set_timeout_functions( p_conn,
256 dbus_connection_unref( p_conn );
261 if( !dbus_connection_set_watch_functions( p_conn,
267 dbus_connection_unref( p_conn );
272 /* dbus_connection_set_wakeup_main_function( p_conn,
279 /*****************************************************************************
280 * Close: destroy interface
281 *****************************************************************************/
283 static void Close ( vlc_object_t *p_this )
285 intf_thread_t *p_intf = (intf_thread_t*) p_this;
286 intf_sys_t *p_sys = p_intf->p_sys;
287 playlist_t *p_playlist = p_sys->p_playlist;
289 var_DelCallback( p_playlist, "item-current", AllCallback, p_intf );
290 var_DelCallback( p_playlist, "intf-change", AllCallback, p_intf );
291 var_DelCallback( p_playlist, "volume", AllCallback, p_intf );
292 var_DelCallback( p_playlist, "mute", AllCallback, p_intf );
293 var_DelCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
294 var_DelCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
295 var_DelCallback( p_playlist, "random", AllCallback, p_intf );
296 var_DelCallback( p_playlist, "repeat", AllCallback, p_intf );
297 var_DelCallback( p_playlist, "loop", AllCallback, p_intf );
301 var_DelCallback( p_sys->p_input, "intf-event", AllCallback, p_intf );
302 var_DelCallback( p_sys->p_input, "can-pause", AllCallback, p_intf );
303 var_DelCallback( p_sys->p_input, "can-seek", AllCallback, p_intf );
304 vlc_object_release( p_sys->p_input );
307 /* The dbus connection is private, so we are responsible
309 dbus_connection_close( p_sys->p_conn );
310 dbus_connection_unref( p_sys->p_conn );
312 // Free the events array
313 for( int i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
315 callback_info_t* info = vlc_array_item_at_index( p_sys->p_events, i );
318 vlc_mutex_destroy( &p_sys->lock );
319 vlc_array_destroy( p_sys->p_events );
320 vlc_array_destroy( p_sys->p_timeouts );
321 vlc_array_destroy( p_sys->p_watches );
325 static void dispatch_status_cb( DBusConnection *p_conn,
326 DBusDispatchStatus i_status,
330 intf_thread_t *p_intf = (intf_thread_t*) p_data;
332 static const char *p_statuses[] = { "DATA_REMAINS",
337 "DBus dispatch status changed to %s.",
338 p_statuses[i_status]);
341 static dbus_bool_t add_timeout( DBusTimeout *p_timeout, void *p_data )
343 intf_thread_t *p_intf = (intf_thread_t*) p_data;
344 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
346 timeout_info_t *p_info = calloc( 1, sizeof( timeout_info_t ) );
347 p_info->i_remaining = dbus_timeout_get_interval( p_timeout ) * 1000;/* µs */
348 p_info->p_timeout = p_timeout;
350 dbus_timeout_set_data( p_timeout, p_info, free );
352 vlc_mutex_lock( &p_sys->lock );
353 vlc_array_append( p_sys->p_timeouts, p_timeout );
354 vlc_mutex_unlock( &p_sys->lock );
359 static void remove_timeout( DBusTimeout *p_timeout, void *p_data )
361 intf_thread_t *p_intf = (intf_thread_t*) p_data;
362 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
364 vlc_mutex_lock( &p_sys->lock );
366 vlc_array_remove( p_sys->p_timeouts,
367 vlc_array_index_of_item( p_sys->p_timeouts, p_timeout ) );
369 vlc_mutex_unlock( &p_sys->lock );
372 static void timeout_toggled( DBusTimeout *p_timeout, void *p_data )
374 intf_thread_t *p_intf = (intf_thread_t*) p_data;
376 msg_Dbg( p_intf, "Toggling dbus timeout" );
378 if( dbus_timeout_get_enabled( p_timeout ) )
380 msg_Dbg( p_intf, "Timeout is enabled, main loop needs to wake up" );
381 wakeup_main_loop( p_intf );
385 static dbus_bool_t add_watch( DBusWatch *p_watch, void *p_data )
387 intf_thread_t *p_intf = (intf_thread_t*) p_data;
388 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
389 int i_fd = dbus_watch_get_unix_fd( p_watch );
391 msg_Dbg( p_intf, "Adding dbus watch on fd %d", i_fd );
393 if( dbus_watch_get_flags( p_watch ) & DBUS_WATCH_READABLE )
394 msg_Dbg( p_intf, "Watching fd %d for readability", i_fd );
396 if( dbus_watch_get_flags( p_watch ) & DBUS_WATCH_WRITABLE )
397 msg_Dbg( p_intf, "Watching fd %d for writeability", i_fd );
399 vlc_mutex_lock( &p_sys->lock );
400 vlc_array_append( p_sys->p_watches, p_watch );
401 vlc_mutex_unlock( &p_sys->lock );
406 static void remove_watch( DBusWatch *p_watch, void *p_data )
408 intf_thread_t *p_intf = (intf_thread_t*) p_data;
409 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
411 msg_Dbg( p_intf, "Removing dbus watch on fd %d",
412 dbus_watch_get_unix_fd( p_watch ) );
414 vlc_mutex_lock( &p_sys->lock );
416 vlc_array_remove( p_sys->p_watches,
417 vlc_array_index_of_item( p_sys->p_watches, p_watch ) );
419 vlc_mutex_unlock( &p_sys->lock );
422 static void watch_toggled( DBusWatch *p_watch, void *p_data )
424 intf_thread_t *p_intf = (intf_thread_t*) p_data;
426 msg_Dbg( p_intf, "Toggling dbus watch on fd %d",
427 dbus_watch_get_unix_fd( p_watch ) );
429 if( dbus_watch_get_enabled( p_watch ) )
432 "Watch on fd %d has been enabled, "
433 "the main loops needs to wake up",
434 dbus_watch_get_unix_fd( p_watch ) );
436 wakeup_main_loop( p_intf );
441 * GetPollFds() fills an array of pollfd data structures with :
442 * - the set of enabled dbus watches
443 * - the unix pipe which we use to manually wake up the main loop
445 * This function must be called with p_sys->lock locked
447 * @return The number of file descriptors
449 * @param intf_thread_t *p_intf this interface thread's state
450 * @param struct pollfd *p_fds a pointer to a pollfd array large enough to
451 * contain all the returned data (number of enabled dbus watches + 1)
453 static int GetPollFds( intf_thread_t *p_intf, struct pollfd *p_fds )
455 intf_sys_t *p_sys = p_intf->p_sys;
456 int i_fds = 1, i_watches = vlc_array_count( p_sys->p_watches );
458 p_fds[0].fd = p_sys->p_pipe_fds[PIPE_OUT];
459 p_fds[0].events = POLLIN | POLLPRI;
461 for( int i = 0; i < i_watches; i++ )
463 DBusWatch *p_watch = NULL;
464 p_watch = vlc_array_item_at_index( p_sys->p_watches, i );
465 if( !dbus_watch_get_enabled( p_watch ) )
468 p_fds[i_fds].fd = dbus_watch_get_unix_fd( p_watch );
469 int i_flags = dbus_watch_get_flags( p_watch );
471 if( i_flags & DBUS_WATCH_READABLE )
472 p_fds[i_fds].events |= POLLIN | POLLPRI;
474 if( i_flags & DBUS_WATCH_WRITABLE )
475 p_fds[i_fds].events |= POLLOUT;
484 * UpdateTimeouts() updates the remaining time for each timeout and
485 * returns how much time is left until the next timeout.
487 * This function must be called with p_sys->lock locked
489 * @return int The time remaining until the next timeout, in milliseconds
490 * or -1 if there are no timeouts
492 * @param intf_thread_t *p_intf This interface thread's state
493 * @param mtime_t i_loop_interval The time which has elapsed since the last
494 * call to this function
496 static int UpdateTimeouts( intf_thread_t *p_intf, mtime_t i_loop_interval )
498 intf_sys_t *p_sys = p_intf->p_sys;
499 mtime_t i_next_timeout = LAST_MDATE;
500 unsigned int i_timeouts = vlc_array_count( p_sys->p_timeouts );
502 if( 0 == i_timeouts )
505 for( unsigned int i = 0; i < i_timeouts; i++ )
507 timeout_info_t *p_info = NULL;
508 DBusTimeout *p_timeout = NULL;
509 mtime_t i_interval = 0;
511 p_timeout = vlc_array_item_at_index( p_sys->p_timeouts, i );
512 i_interval = dbus_timeout_get_interval( p_timeout ) * 1000; /* µs */
513 p_info = (timeout_info_t*) dbus_timeout_get_data( p_timeout );
515 p_info->i_remaining -= __MAX( 0, i_loop_interval ) % i_interval;
517 if( !dbus_timeout_get_enabled( p_timeout ) )
520 /* The correct poll timeout value is the shortest one
521 * in the dbus timeouts list */
522 i_next_timeout = __MIN( i_next_timeout,
523 __MAX( 0, p_info->i_remaining ) );
526 /* next timeout in milliseconds */
527 return i_next_timeout / 1000;
531 * ProcessEvents() reacts to a list of events originating from other VLC threads
533 * This function must be called with p_sys->lock unlocked
535 * @param intf_thread_t *p_intf This interface thread state
536 * @param callback_info_t *p_events the list of events to process
538 static void ProcessEvents( intf_thread_t *p_intf,
539 callback_info_t **p_events, int i_events )
541 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
542 bool b_can_play = p_intf->p_sys->b_can_play;
544 vlc_dictionary_t player_properties, tracklist_properties;
545 vlc_dictionary_init( &player_properties, 0 );
546 vlc_dictionary_init( &tracklist_properties, 0 );
548 for( int i = 0; i < i_events; i++ )
550 switch( p_events[i]->signal )
552 case SIGNAL_ITEM_CURRENT:
553 TrackChange( p_intf );
554 vlc_dictionary_insert( &player_properties, "Metadata", NULL );
556 case SIGNAL_INTF_CHANGE:
557 case SIGNAL_PLAYLIST_ITEM_APPEND:
558 case SIGNAL_PLAYLIST_ITEM_DELETED:
560 b_can_play = playlist_CurrentSize( p_playlist ) > 0;
563 if( b_can_play != p_intf->p_sys->b_can_play )
565 p_intf->p_sys->b_can_play = b_can_play;
566 vlc_dictionary_insert( &player_properties, "CanPlay", NULL );
569 vlc_dictionary_insert( &tracklist_properties, "Tracks", NULL );
571 case SIGNAL_VOLUME_MUTED:
572 case SIGNAL_VOLUME_CHANGE:
573 vlc_dictionary_insert( &player_properties, "Volume", NULL );
576 vlc_dictionary_insert( &player_properties, "Shuffle", NULL );
580 vlc_dictionary_insert( &player_properties, "LoopStatus", NULL );
583 vlc_dictionary_insert( &player_properties, "PlaybackStatus", NULL );
586 vlc_dictionary_insert( &player_properties, "Rate", NULL );
588 case SIGNAL_INPUT_METADATA:
590 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
591 input_item_t *p_item;
594 p_item = input_GetItem( p_input );
595 vlc_object_release( p_input );
598 vlc_dictionary_insert( &player_properties,
603 case SIGNAL_CAN_SEEK:
604 vlc_dictionary_insert( &player_properties, "CanSeek", NULL );
606 case SIGNAL_CAN_PAUSE:
607 vlc_dictionary_insert( &player_properties, "CanPause", NULL );
611 input_thread_t *p_input;
612 input_item_t *p_item;
613 p_input = playlist_CurrentInput( p_intf->p_sys->p_playlist );
616 p_item = input_GetItem( p_input );
617 vlc_object_release( p_input );
619 if( p_item && ( p_item->i_id == p_events[i]->i_item ) )
620 SeekedEmit( p_intf );
630 if( vlc_dictionary_keys_count( &player_properties ) )
631 PlayerPropertiesChangedEmit( p_intf, &player_properties );
633 if( vlc_dictionary_keys_count( &tracklist_properties ) )
634 TrackListPropertiesChangedEmit( p_intf, &tracklist_properties );
636 vlc_dictionary_clear( &player_properties, NULL, NULL );
637 vlc_dictionary_clear( &tracklist_properties, NULL, NULL );
641 * ProcessWatches() handles a list of dbus watches after poll() has returned
643 * This function must be called with p_sys->lock unlocked
645 * @param intf_thread_t *p_intf This interface thread state
646 * @param DBusWatch **p_watches The list of dbus watches to process
647 * @param int i_watches The size of the p_watches array
648 * @param struct pollfd *p_fds The result of a poll() call
649 * @param int i_fds The number of file descriptors processed by poll()
651 static void ProcessWatches( intf_thread_t *p_intf,
652 DBusWatch **p_watches, int i_watches,
653 struct pollfd *p_fds, int i_fds )
655 /* Process watches */
656 for( int i = 0; i < i_watches; i++ )
658 DBusWatch *p_watch = p_watches[i];
659 if( !dbus_watch_get_enabled( p_watch ) )
662 for( int j = 0; j < i_fds; j++ )
664 if( p_fds[j].fd != dbus_watch_get_unix_fd( p_watch ) )
668 int i_revents = p_fds[j].revents;
669 int i_fd = p_fds[j].fd;
671 if( i_revents & POLLIN )
673 msg_Dbg( p_intf, "fd %d is ready for reading", i_fd );
674 i_flags |= DBUS_WATCH_READABLE;
677 if( i_revents & POLLOUT )
679 msg_Dbg( p_intf, "fd %d is ready for writing", i_fd );
680 i_flags |= DBUS_WATCH_WRITABLE;
683 if( i_revents & POLLERR )
685 msg_Dbg( p_intf, "error when polling fd %d", i_fd );
686 i_flags |= DBUS_WATCH_ERROR;
689 if( i_revents & POLLHUP )
691 msg_Dbg( p_intf, "Hangup signal on fd %d", i_fd );
692 i_flags |= DBUS_WATCH_HANGUP;
697 msg_Dbg( p_intf, "Handling dbus watch on fd %d", i_fd );
698 dbus_watch_handle( p_watch, i_flags );
701 msg_Dbg( p_intf, "Nothing happened on fd %d", i_fd );
707 * ProcessTimeouts() handles DBus timeouts
709 * This function must be called with p_sys->lock locked
711 * @param intf_thread_t *p_intf This interface thread state
712 * @param DBusTimeout **p_timeouts List of timeouts to process
713 * @param int i_timeouts Size of p_timeouts
715 static void ProcessTimeouts( intf_thread_t *p_intf,
716 DBusTimeout **p_timeouts, int i_timeouts )
718 VLC_UNUSED( p_intf );
720 for( int i = 0; i < i_timeouts; i++ )
722 timeout_info_t *p_info = NULL;
724 p_info = (timeout_info_t*) dbus_timeout_get_data( p_timeouts[i] );
726 if( !dbus_timeout_get_enabled( p_info->p_timeout ) )
729 if( p_info->i_remaining > 0 )
732 dbus_timeout_handle( p_info->p_timeout );
733 p_info->i_remaining = dbus_timeout_get_interval( p_info->p_timeout );
738 * DispatchDBusMessages() dispatches incoming dbus messages
739 * (indirectly invoking the callbacks), then it sends outgoing
740 * messages which needs to be sent on the bus (method replies and signals)
742 * This function must be called with p_sys->lock unlocked
744 * @param intf_thread_t *p_intf This interface thread state
746 static void DispatchDBusMessages( intf_thread_t *p_intf )
748 DBusDispatchStatus status;
749 intf_sys_t *p_sys = p_intf->p_sys;
751 /* Dispatch incoming messages */
752 status = dbus_connection_get_dispatch_status( p_sys->p_conn );
753 while( status != DBUS_DISPATCH_COMPLETE )
755 msg_Dbg( p_intf, "Dispatching incoming dbus message" );
756 dbus_connection_dispatch( p_sys->p_conn );
757 status = dbus_connection_get_dispatch_status( p_sys->p_conn );
760 /* Send outgoing data */
761 if( dbus_connection_has_messages_to_send( p_sys->p_conn ) )
763 msg_Dbg( p_intf, "Sending outgoing data" );
764 dbus_connection_flush( p_sys->p_conn );
769 * MPRISEntryPoint() routes incoming messages to their respective interface
772 * This function is called during dbus_connection_dispatch()
774 static DBusHandlerResult
775 MPRISEntryPoint ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this )
777 const char *psz_target_interface;
778 const char *psz_interface = dbus_message_get_interface( p_from );
779 const char *psz_method = dbus_message_get_member( p_from );
783 if( strcmp( psz_interface, DBUS_INTERFACE_PROPERTIES ) )
784 psz_target_interface = psz_interface;
788 dbus_error_init( &error );
789 dbus_message_get_args( p_from, &error,
790 DBUS_TYPE_STRING, &psz_target_interface,
793 if( dbus_error_is_set( &error ) )
795 msg_Err( (vlc_object_t*) p_this, "D-Bus error on %s.%s: %s",
796 psz_interface, psz_method,
798 dbus_error_free( &error );
799 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
803 msg_Dbg( (vlc_object_t*) p_this, "Routing %s.%s D-Bus method call to %s",
804 psz_interface, psz_method,
805 psz_target_interface );
807 if( !strcmp( psz_target_interface, DBUS_INTERFACE_INTROSPECTABLE ) )
808 return handle_introspect( p_conn, p_from, p_this );
810 if( !strcmp( psz_target_interface, DBUS_MPRIS_ROOT_INTERFACE ) )
811 return handle_root( p_conn, p_from, p_this );
813 if( !strcmp( psz_target_interface, DBUS_MPRIS_PLAYER_INTERFACE ) )
814 return handle_player( p_conn, p_from, p_this );
816 if( !strcmp( psz_target_interface, DBUS_MPRIS_TRACKLIST_INTERFACE ) )
817 return handle_tracklist( p_conn, p_from, p_this );
819 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
822 /*****************************************************************************
824 *****************************************************************************/
826 static void Run ( intf_thread_t *p_intf )
828 intf_sys_t *p_sys = p_intf->p_sys;
829 mtime_t i_last_run = mdate();
833 int canc = vlc_savecancel();
834 vlc_mutex_lock( &p_sys->lock );
836 int i_watches = vlc_array_count( p_sys->p_watches );
837 struct pollfd fds[i_watches];
838 memset(fds, 0, sizeof fds);
840 int i_fds = GetPollFds( p_intf, fds );
842 mtime_t i_now = mdate(), i_loop_interval = i_now - i_last_run;
845 "%lld µs elapsed since last wakeup",
846 (long long) i_loop_interval );
848 int i_next_timeout = UpdateTimeouts( p_intf, i_loop_interval );
851 vlc_mutex_unlock( &p_sys->lock );
853 if( -1 != i_next_timeout )
854 msg_Dbg( p_intf, "next timeout is in %d ms", i_next_timeout );
855 msg_Dbg( p_intf, "Sleeping until something happens" );
857 /* thread cancellation is allowed while the main loop sleeps */
858 vlc_restorecancel( canc );
860 int i_pollres = poll( fds, i_fds, i_next_timeout );
862 canc = vlc_savecancel();
864 msg_Dbg( p_intf, "the main loop has been woken up" );
866 if( -1 == i_pollres )
867 { /* XXX: What should we do when poll() fails ? */
868 msg_Err( p_intf, "poll() failed: %m" );
869 vlc_restorecancel( canc );
873 /* Was the main loop woken up manually ? */
874 if( 0 < i_pollres && ( fds[0].revents & POLLIN ) )
877 msg_Dbg( p_intf, "Removing a byte from the self-pipe" );
878 (void)read( fds[0].fd, &buf, 1 );
881 /* We need to lock the mutex while building lists of events,
882 * timeouts and watches to process but we can't keep the lock while
883 * processing them, or else we risk a deadlock:
885 * The signal functions could lock mutex X while p_events is locked;
886 * While some other function in vlc (playlist) might lock mutex X
887 * and then set a variable which would call AllCallback(), which itself
888 * needs to lock p_events to add a new event.
890 vlc_mutex_lock( &p_intf->p_sys->lock );
892 /* Get the list of timeouts to process */
893 unsigned int i_timeouts = vlc_array_count( p_sys->p_timeouts );
894 DBusTimeout *p_timeouts[i_timeouts];
895 for( unsigned int i = 0; i < i_timeouts; i++ )
897 p_timeouts[i] = vlc_array_item_at_index( p_sys->p_timeouts, i );
900 /* Get the list of watches to process */
901 i_watches = vlc_array_count( p_sys->p_watches );
902 DBusWatch *p_watches[i_watches];
903 for( int i = 0; i < i_watches; i++ )
905 p_watches[i] = vlc_array_item_at_index( p_sys->p_watches, i );
908 /* Get the list of events to process */
909 int i_events = vlc_array_count( p_intf->p_sys->p_events );
910 callback_info_t* p_info[i_events];
911 for( int i = i_events - 1; i >= 0; i-- )
913 p_info[i] = vlc_array_item_at_index( p_intf->p_sys->p_events, i );
914 vlc_array_remove( p_intf->p_sys->p_events, i );
917 /* now we can release the lock and process what's pending */
918 vlc_mutex_unlock( &p_intf->p_sys->lock );
920 ProcessEvents( p_intf, p_info, i_events );
921 ProcessWatches( p_intf, p_watches, i_watches, fds, i_fds );
923 ProcessTimeouts( p_intf, p_timeouts, i_timeouts );
924 DispatchDBusMessages( p_intf );
926 vlc_restorecancel( canc );
930 static void wakeup_main_loop( void *p_data )
932 intf_thread_t *p_intf = (intf_thread_t*) p_data;
934 msg_Dbg( p_intf, "Sending wakeup signal to the main loop" );
936 if( !write( p_intf->p_sys->p_pipe_fds[PIPE_IN], "\0", 1 ) )
938 msg_Err( p_intf, "Could not wake up the main loop: %m" );
942 /* InputIntfEventCallback() fills a callback_info_t data structure in response
943 * to an "intf-event" input event.
945 * Caution: This function executes in the input thread
947 * This function must be called with p_sys->lock locked
949 * @return int VLC_SUCCESS on success, VLC_E* on error
950 * @param intf_thread_t *p_intf the interface thread
951 * @param input_thread_t *p_input This input thread
952 * @param const int i_event input event type
953 * @param callback_info_t *p_info Location of the callback info to fill
955 static int InputIntfEventCallback( intf_thread_t *p_intf,
956 input_thread_t *p_input,
958 callback_info_t *p_info )
960 dbus_int32_t i_state = PLAYBACK_STATE_INVALID;
961 assert(!p_info->signal);
962 mtime_t i_now = mdate(), i_pos, i_projected_pos, i_interval;
963 float f_current_rate;
967 case INPUT_EVENT_DEAD:
968 case INPUT_EVENT_ABORT:
969 i_state = PLAYBACK_STATE_STOPPED;
971 case INPUT_EVENT_STATE:
972 switch( var_GetInteger( p_input, "state" ) )
976 i_state = PLAYBACK_STATE_PLAYING;
979 i_state = PLAYBACK_STATE_PAUSED;
982 i_state = PLAYBACK_STATE_STOPPED;
985 case INPUT_EVENT_ITEM_META:
986 p_info->signal = SIGNAL_INPUT_METADATA;
988 case INPUT_EVENT_RATE:
989 p_info->signal = SIGNAL_RATE;
991 case INPUT_EVENT_POSITION:
993 * XXX: This is way more convoluted than it should be... */
994 if( !p_intf->p_sys->i_last_input_pos_event ||
995 !( var_GetInteger( p_input, "state" ) == PLAYING_S ) )
997 i_pos = var_GetTime( p_input, "time" );
998 f_current_rate = var_GetFloat( p_input, "rate" );
999 i_interval = ( i_now - p_intf->p_sys->i_last_input_pos_event );
1000 i_projected_pos = p_intf->p_sys->i_last_input_pos + ( i_interval * f_current_rate );
1001 p_intf->p_sys->i_last_input_pos_event = i_now;
1002 p_intf->p_sys->i_last_input_pos = i_pos;
1003 if( ABS( i_pos - i_projected_pos ) < SEEK_THRESHOLD )
1005 p_info->signal = SIGNAL_SEEK;
1006 p_info->i_item = input_GetItem( p_input )->i_id;
1009 return VLC_EGENERIC;
1012 if( i_state != PLAYBACK_STATE_INVALID &&
1013 i_state != p_intf->p_sys->i_playing_state )
1015 p_intf->p_sys->i_playing_state = i_state;
1016 p_info->signal = SIGNAL_STATE;
1019 return p_info->signal ? VLC_SUCCESS : VLC_EGENERIC;
1022 // Get all the callbacks
1023 static int AllCallback( vlc_object_t *p_this, const char *psz_var,
1024 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1029 intf_thread_t *p_intf = (intf_thread_t*)p_data;
1030 callback_info_t *info = calloc( 1, sizeof( callback_info_t ) );
1035 vlc_mutex_lock( &p_intf->p_sys->lock );
1037 // Wich event is it ?
1038 if( !strcmp( "item-current", psz_var ) )
1039 info->signal = SIGNAL_ITEM_CURRENT;
1041 else if( !strcmp( "volume", psz_var ) )
1042 info->signal = SIGNAL_VOLUME_CHANGE;
1044 else if( !strcmp( "mute", psz_var ) )
1045 info->signal = SIGNAL_VOLUME_MUTED;
1047 else if( !strcmp( "intf-change", psz_var ) )
1048 info->signal = SIGNAL_INTF_CHANGE;
1050 else if( !strcmp( "playlist-item-append", psz_var ) )
1052 info->signal = SIGNAL_PLAYLIST_ITEM_APPEND;
1053 info->i_node = ((playlist_add_t*)newval.p_address)->i_node;
1056 else if( !strcmp( "playlist-item-deleted", psz_var ) )
1057 info->signal = SIGNAL_PLAYLIST_ITEM_DELETED;
1059 else if( !strcmp( "random", psz_var ) )
1060 info->signal = SIGNAL_RANDOM;
1062 else if( !strcmp( "repeat", psz_var ) )
1063 info->signal = SIGNAL_REPEAT;
1065 else if( !strcmp( "loop", psz_var ) )
1066 info->signal = SIGNAL_LOOP;
1068 else if( !strcmp( "intf-event", psz_var ) )
1070 int i_res = InputIntfEventCallback( p_intf,
1071 (input_thread_t*) p_this,
1072 newval.i_int, info );
1073 if( VLC_SUCCESS != i_res )
1075 vlc_mutex_unlock( &p_intf->p_sys->lock );
1082 else if( !strcmp( "can-seek", psz_var ) )
1083 info->signal = SIGNAL_CAN_SEEK;
1085 else if( !strcmp( "can-pause", psz_var ) )
1086 info->signal = SIGNAL_CAN_PAUSE;
1092 vlc_array_append( p_intf->p_sys->p_events, info );
1093 vlc_mutex_unlock( &p_intf->p_sys->lock );
1096 "Got a VLC event on %s. The main loop needs to wake up "
1097 "in order to process it", psz_var );
1099 wakeup_main_loop( p_intf );
1104 /*****************************************************************************
1105 * TrackChange: callback on playlist "item-current"
1106 *****************************************************************************/
1107 static int TrackChange( intf_thread_t *p_intf )
1109 intf_sys_t *p_sys = p_intf->p_sys;
1110 playlist_t *p_playlist = p_sys->p_playlist;
1111 input_thread_t *p_input = NULL;
1112 input_item_t *p_item = NULL;
1114 if( p_intf->p_sys->b_dead )
1117 if( p_sys->p_input )
1119 var_DelCallback( p_sys->p_input, "intf-event", AllCallback, p_intf );
1120 var_DelCallback( p_sys->p_input, "can-pause", AllCallback, p_intf );
1121 var_DelCallback( p_sys->p_input, "can-seek", AllCallback, p_intf );
1122 vlc_object_release( p_sys->p_input );
1123 p_sys->p_input = NULL;
1126 p_sys->b_meta_read = false;
1128 p_input = playlist_CurrentInput( p_playlist );
1134 p_item = input_GetItem( p_input );
1137 vlc_object_release( p_input );
1138 return VLC_EGENERIC;
1141 if( input_item_IsPreparsed( p_item ) )
1142 p_sys->b_meta_read = true;
1144 p_sys->p_input = p_input;
1145 var_AddCallback( p_input, "intf-event", AllCallback, p_intf );
1146 var_AddCallback( p_input, "can-pause", AllCallback, p_intf );
1147 var_AddCallback( p_input, "can-seek", AllCallback, p_intf );
1153 * DemarshalSetPropertyValue() extracts the new property value from a
1154 * org.freedesktop.DBus.Properties.Set method call message.
1156 * @return int VLC_SUCCESS on success
1157 * @param DBusMessage *p_msg a org.freedesktop.DBus.Properties.Set method call
1158 * @param void *p_arg placeholder for the demarshalled value
1160 int DemarshalSetPropertyValue( DBusMessage *p_msg, void *p_arg )
1163 bool b_valid_input = FALSE;
1164 DBusMessageIter in_args, variant;
1165 dbus_message_iter_init( p_msg, &in_args );
1169 i_type = dbus_message_iter_get_arg_type( &in_args );
1170 if( DBUS_TYPE_VARIANT == i_type )
1172 dbus_message_iter_recurse( &in_args, &variant );
1173 dbus_message_iter_get_basic( &variant, p_arg );
1174 b_valid_input = TRUE;
1176 } while( dbus_message_iter_next( &in_args ) );
1178 return b_valid_input ? VLC_SUCCESS : VLC_EGENERIC;
1181 /*****************************************************************************
1182 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1183 *****************************************************************************/
1185 #define ADD_META( entry, type, data ) \
1187 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1188 NULL, &dict_entry ); \
1189 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1190 &ppsz_meta_items[entry] ); \
1191 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1192 type##_AS_STRING, &variant ); \
1193 dbus_message_iter_append_basic( &variant, \
1196 dbus_message_iter_close_container( &dict_entry, &variant ); \
1197 dbus_message_iter_close_container( &dict, &dict_entry ); }
1199 #define ADD_VLC_META_STRING( entry, item ) \
1201 char * psz = input_item_Get##item( p_input );\
1202 ADD_META( entry, DBUS_TYPE_STRING, \
1207 int GetInputMeta( input_item_t* p_input,
1208 DBusMessageIter *args )
1210 DBusMessageIter dict, dict_entry, variant;
1211 /** The duration of the track can be expressed in second, milli-seconds and
1213 dbus_int64_t i_mtime = input_item_GetDuration( p_input );
1214 dbus_uint32_t i_time = i_mtime / 1000000;
1215 dbus_int64_t i_length = i_mtime / 1000;
1218 if( -1 == asprintf( &psz_trackid, MPRIS_TRACKID_FORMAT, p_input->i_id ) )
1221 const char* ppsz_meta_items[] =
1223 "mpris:trackid", "xesam:url", "xesam:title", "xesam:artist", "xesam:album",
1224 "xesam:tracknumber", "vlc:time", "mpris:length", "xesam:genre",
1225 "xesam:userRating", "xesam:contentCreated", "mpris:artUrl", "mb:trackId",
1226 "vlc:audio-bitrate", "vlc:audio-samplerate", "vlc:video-bitrate",
1227 "vlc:audio-codec", "vlc:copyright", "xesam:comment", "vlc:encodedby",
1228 "language", "vlc:length", "vlc:nowplaying", "vlc:publisher", "vlc:setting",
1229 "status", "vlc:url", "vlc:video-codec"
1232 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1234 ADD_META( 0, DBUS_TYPE_OBJECT_PATH, psz_trackid );
1235 ADD_VLC_META_STRING( 1, URI );
1236 ADD_VLC_META_STRING( 2, Title );
1237 ADD_VLC_META_STRING( 3, Artist );
1238 ADD_VLC_META_STRING( 4, Album );
1239 ADD_VLC_META_STRING( 5, TrackNum );
1240 ADD_META( 6, DBUS_TYPE_UINT32, i_time );
1241 ADD_META( 7, DBUS_TYPE_INT64, i_mtime );
1242 ADD_VLC_META_STRING( 8, Genre );
1243 ADD_VLC_META_STRING( 9, Rating );
1244 ADD_VLC_META_STRING( 10, Date );
1245 ADD_VLC_META_STRING( 11, ArtURL );
1246 ADD_VLC_META_STRING( 12, TrackID );
1248 ADD_VLC_META_STRING( 17, Copyright );
1249 ADD_VLC_META_STRING( 18, Description );
1250 ADD_VLC_META_STRING( 19, EncodedBy );
1251 ADD_VLC_META_STRING( 20, Language );
1252 ADD_META( 21, DBUS_TYPE_INT64, i_length );
1253 ADD_VLC_META_STRING( 22, NowPlaying );
1254 ADD_VLC_META_STRING( 23, Publisher );
1255 ADD_VLC_META_STRING( 24, Setting );
1256 ADD_VLC_META_STRING( 25, URL );
1258 free( psz_trackid );
1260 vlc_mutex_lock( &p_input->lock );
1261 if( p_input->p_meta )
1263 int i_status = vlc_meta_GetStatus( p_input->p_meta );
1264 ADD_META( 23, DBUS_TYPE_INT32, i_status );
1266 vlc_mutex_unlock( &p_input->lock );
1268 dbus_message_iter_close_container( args, &dict );
1273 #undef ADD_VLC_META_STRING