1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright © 2006-2008 Rafaël Carré
5 * Copyright © 2007-2012 Mirsal Ennaime
6 * Copyright © 2009-2012 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>
48 #include "dbus_common.h"
49 #include "dbus_root.h"
50 #include "dbus_player.h"
51 #include "dbus_tracklist.h"
52 #include "dbus_introspect.h"
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
56 #include <vlc_interface.h>
57 #include <vlc_playlist.h>
59 #include <vlc_mtime.h>
69 #define DBUS_MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
70 #define DBUS_INSTANCE_ID_PREFIX "instance"
72 #define SEEK_THRESHOLD 1000 /* µsec */
74 /*****************************************************************************
76 *****************************************************************************/
78 static DBusHandlerResult
79 MPRISEntryPoint ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this );
81 static const DBusObjectPathVTable dbus_mpris_vtable = {
82 NULL, MPRISEntryPoint, /* handler function */
83 NULL, NULL, NULL, NULL
96 DBusTimeout *p_timeout;
105 static int Open ( vlc_object_t * );
106 static void Close ( vlc_object_t * );
107 static void Run ( intf_thread_t * );
109 static int TrackChange( intf_thread_t * );
110 static int AllCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
111 static int InputCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
113 static dbus_bool_t add_timeout ( DBusTimeout *p_timeout, void *p_data );
114 static dbus_bool_t add_watch ( DBusWatch *p_watch, void *p_data );
116 static void remove_timeout ( DBusTimeout *p_timeout, void *p_data );
117 static void remove_watch ( DBusWatch *p_watch, void *p_data );
119 static void timeout_toggled ( DBusTimeout *p_timeout, void *p_data );
120 static void watch_toggled ( DBusWatch *p_watch, void *p_data );
122 static void wakeup_main_loop( void *p_data );
124 static int UpdateTimeouts( intf_thread_t *p_intf, mtime_t i_lastrun );
126 static void ProcessEvents ( intf_thread_t *p_intf,
127 callback_info_t **p_events,
130 static void ProcessWatches ( intf_thread_t *p_intf,
131 DBusWatch **p_watches,
133 struct pollfd *p_fds,
136 static void ProcessTimeouts( intf_thread_t *p_intf,
137 DBusTimeout **p_timeouts,
140 static void DispatchDBusMessages( intf_thread_t *p_intf );
142 /*****************************************************************************
144 *****************************************************************************/
146 set_shortname( N_("DBus"))
147 set_category( CAT_INTERFACE )
148 set_subcategory( SUBCAT_INTERFACE_CONTROL )
149 set_description( N_("D-Bus control interface") )
150 set_capability( "interface", 0 )
151 set_callbacks( Open, Close )
154 /*****************************************************************************
155 * Open: initialize interface
156 *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
160 intf_thread_t *p_intf = (intf_thread_t*)p_this;
162 /* initialisation of the connection */
163 if( !dbus_threads_init_default() )
166 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
167 if( unlikely(!p_sys) )
170 playlist_t *p_playlist;
171 DBusConnection *p_conn;
172 p_sys->i_player_caps = PLAYER_CAPS_NONE;
173 p_sys->i_playing_state = PLAYBACK_STATE_INVALID;
175 if( vlc_pipe( p_sys->p_pipe_fds ) )
178 msg_Err( p_intf, "Could not create pipe" );
183 dbus_error_init( &error );
185 /* connect privately to the session bus
186 * the connection will not be shared with other vlc modules which use dbus,
187 * thus avoiding a whole class of concurrency issues */
188 p_conn = dbus_bus_get_private( DBUS_BUS_SESSION, &error );
191 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
193 dbus_error_free( &error );
198 dbus_connection_set_exit_on_disconnect( p_conn, FALSE );
200 /* register an instance-specific well known name of the form
201 * org.mpris.MediaPlayer2.vlc.instanceXXXX where XXXX is the
202 * current process's pid */
203 size_t i_length = sizeof( DBUS_MPRIS_BUS_NAME ) +
204 sizeof( DBUS_INSTANCE_ID_PREFIX ) + 10;
206 char unique_service[i_length];
208 snprintf( unique_service, sizeof (unique_service),
209 DBUS_MPRIS_BUS_NAME"."DBUS_INSTANCE_ID_PREFIX"%"PRIu32,
210 (uint32_t)getpid() );
212 dbus_bus_request_name( p_conn, unique_service, 0, &error );
214 if( dbus_error_is_set( &error ) )
216 msg_Err( p_this, "Error requesting service name %s: %s",
217 unique_service, error.message );
218 dbus_error_free( &error );
222 msg_Dbg( p_intf, "listening on dbus as: %s", unique_service );
224 /* Try to register org.mpris.MediaPlayer2.vlc as well in case we are
225 * the only VLC instance currently connected to the bus */
226 dbus_bus_request_name( p_conn, DBUS_MPRIS_BUS_NAME, 0, NULL );
228 /* Register the entry point object path */
229 dbus_connection_register_object_path( p_conn, DBUS_MPRIS_OBJECT_PATH,
230 &dbus_mpris_vtable, p_this );
232 dbus_connection_flush( p_conn );
234 p_intf->pf_run = Run;
235 p_intf->p_sys = p_sys;
236 p_sys->p_conn = p_conn;
237 p_sys->p_events = vlc_array_new();
238 p_sys->p_timeouts = vlc_array_new();
239 p_sys->p_watches = vlc_array_new();
240 vlc_mutex_init( &p_sys->lock );
242 p_playlist = pl_Get( p_intf );
243 p_sys->p_playlist = p_playlist;
245 var_AddCallback( p_playlist, "item-current", AllCallback, p_intf );
246 var_AddCallback( p_playlist, "intf-change", AllCallback, p_intf );
247 var_AddCallback( p_playlist, "volume", AllCallback, p_intf );
248 var_AddCallback( p_playlist, "mute", AllCallback, p_intf );
249 var_AddCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
250 var_AddCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
251 var_AddCallback( p_playlist, "random", AllCallback, p_intf );
252 var_AddCallback( p_playlist, "repeat", AllCallback, p_intf );
253 var_AddCallback( p_playlist, "loop", AllCallback, p_intf );
254 var_AddCallback( p_playlist, "fullscreen", AllCallback, p_intf );
256 if( !dbus_connection_set_timeout_functions( p_conn,
262 dbus_connection_unref( p_conn );
267 if( !dbus_connection_set_watch_functions( p_conn,
273 dbus_connection_unref( p_conn );
278 /* dbus_connection_set_wakeup_main_function( p_conn,
285 /*****************************************************************************
286 * Close: destroy interface
287 *****************************************************************************/
289 static void Close ( vlc_object_t *p_this )
291 intf_thread_t *p_intf = (intf_thread_t*) p_this;
292 intf_sys_t *p_sys = p_intf->p_sys;
293 playlist_t *p_playlist = p_sys->p_playlist;
295 var_DelCallback( p_playlist, "item-current", AllCallback, p_intf );
296 var_DelCallback( p_playlist, "intf-change", AllCallback, p_intf );
297 var_DelCallback( p_playlist, "volume", AllCallback, p_intf );
298 var_DelCallback( p_playlist, "mute", AllCallback, p_intf );
299 var_DelCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
300 var_DelCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
301 var_DelCallback( p_playlist, "random", AllCallback, p_intf );
302 var_DelCallback( p_playlist, "repeat", AllCallback, p_intf );
303 var_DelCallback( p_playlist, "loop", AllCallback, p_intf );
304 var_DelCallback( p_playlist, "fullscreen", AllCallback, p_intf );
308 var_DelCallback( p_sys->p_input, "intf-event", InputCallback, p_intf );
309 var_DelCallback( p_sys->p_input, "can-pause", AllCallback, p_intf );
310 var_DelCallback( p_sys->p_input, "can-seek", AllCallback, p_intf );
311 vlc_object_release( p_sys->p_input );
314 /* The dbus connection is private, so we are responsible
316 dbus_connection_close( p_sys->p_conn );
317 dbus_connection_unref( p_sys->p_conn );
319 // Free the events array
320 for( int i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
322 callback_info_t* info = vlc_array_item_at_index( p_sys->p_events, i );
325 vlc_mutex_destroy( &p_sys->lock );
326 vlc_array_destroy( p_sys->p_events );
327 vlc_array_destroy( p_sys->p_timeouts );
328 vlc_array_destroy( p_sys->p_watches );
332 static dbus_bool_t add_timeout( DBusTimeout *p_timeout, void *p_data )
334 intf_thread_t *p_intf = (intf_thread_t*) p_data;
335 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
337 timeout_info_t *p_info = calloc( 1, sizeof( timeout_info_t ) );
338 p_info->i_remaining = dbus_timeout_get_interval( p_timeout ) * 1000;/* µs */
339 p_info->p_timeout = p_timeout;
341 dbus_timeout_set_data( p_timeout, p_info, free );
343 vlc_mutex_lock( &p_sys->lock );
344 vlc_array_append( p_sys->p_timeouts, p_timeout );
345 vlc_mutex_unlock( &p_sys->lock );
350 static void remove_timeout( DBusTimeout *p_timeout, void *p_data )
352 intf_thread_t *p_intf = (intf_thread_t*) p_data;
353 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
355 vlc_mutex_lock( &p_sys->lock );
357 vlc_array_remove( p_sys->p_timeouts,
358 vlc_array_index_of_item( p_sys->p_timeouts, p_timeout ) );
360 vlc_mutex_unlock( &p_sys->lock );
363 static void timeout_toggled( DBusTimeout *p_timeout, void *p_data )
365 intf_thread_t *p_intf = (intf_thread_t*) p_data;
367 if( dbus_timeout_get_enabled( p_timeout ) )
368 wakeup_main_loop( p_intf );
371 static dbus_bool_t add_watch( DBusWatch *p_watch, void *p_data )
373 intf_thread_t *p_intf = (intf_thread_t*) p_data;
374 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
376 vlc_mutex_lock( &p_sys->lock );
377 vlc_array_append( p_sys->p_watches, p_watch );
378 vlc_mutex_unlock( &p_sys->lock );
383 static void remove_watch( DBusWatch *p_watch, void *p_data )
385 intf_thread_t *p_intf = (intf_thread_t*) p_data;
386 intf_sys_t *p_sys = (intf_sys_t*) p_intf->p_sys;
388 vlc_mutex_lock( &p_sys->lock );
390 vlc_array_remove( p_sys->p_watches,
391 vlc_array_index_of_item( p_sys->p_watches, p_watch ) );
393 vlc_mutex_unlock( &p_sys->lock );
396 static void watch_toggled( DBusWatch *p_watch, void *p_data )
398 intf_thread_t *p_intf = (intf_thread_t*) p_data;
400 if( dbus_watch_get_enabled( p_watch ) )
401 wakeup_main_loop( p_intf );
405 * GetPollFds() fills an array of pollfd data structures with :
406 * - the set of enabled dbus watches
407 * - the unix pipe which we use to manually wake up the main loop
409 * This function must be called with p_sys->lock locked
411 * @return The number of file descriptors
413 * @param intf_thread_t *p_intf this interface thread's state
414 * @param struct pollfd *p_fds a pointer to a pollfd array large enough to
415 * contain all the returned data (number of enabled dbus watches + 1)
417 static int GetPollFds( intf_thread_t *p_intf, struct pollfd *p_fds )
419 intf_sys_t *p_sys = p_intf->p_sys;
420 int i_fds = 1, i_watches = vlc_array_count( p_sys->p_watches );
422 p_fds[0].fd = p_sys->p_pipe_fds[PIPE_OUT];
423 p_fds[0].events = POLLIN | POLLPRI;
425 for( int i = 0; i < i_watches; i++ )
427 DBusWatch *p_watch = NULL;
428 p_watch = vlc_array_item_at_index( p_sys->p_watches, i );
429 if( !dbus_watch_get_enabled( p_watch ) )
432 p_fds[i_fds].fd = dbus_watch_get_unix_fd( p_watch );
433 int i_flags = dbus_watch_get_flags( p_watch );
435 if( i_flags & DBUS_WATCH_READABLE )
436 p_fds[i_fds].events |= POLLIN | POLLPRI;
438 if( i_flags & DBUS_WATCH_WRITABLE )
439 p_fds[i_fds].events |= POLLOUT;
448 * UpdateTimeouts() updates the remaining time for each timeout and
449 * returns how much time is left until the next timeout.
451 * This function must be called with p_sys->lock locked
453 * @return int The time remaining until the next timeout, in milliseconds
454 * or -1 if there are no timeouts
456 * @param intf_thread_t *p_intf This interface thread's state
457 * @param mtime_t i_loop_interval The time which has elapsed since the last
458 * call to this function
460 static int UpdateTimeouts( intf_thread_t *p_intf, mtime_t i_loop_interval )
462 intf_sys_t *p_sys = p_intf->p_sys;
463 mtime_t i_next_timeout = LAST_MDATE;
464 unsigned int i_timeouts = vlc_array_count( p_sys->p_timeouts );
466 if( 0 == i_timeouts )
469 for( unsigned int i = 0; i < i_timeouts; i++ )
471 timeout_info_t *p_info = NULL;
472 DBusTimeout *p_timeout = NULL;
473 mtime_t i_interval = 0;
475 p_timeout = vlc_array_item_at_index( p_sys->p_timeouts, i );
476 i_interval = dbus_timeout_get_interval( p_timeout ) * 1000; /* µs */
477 p_info = (timeout_info_t*) dbus_timeout_get_data( p_timeout );
479 p_info->i_remaining -= __MAX( 0, i_loop_interval ) % i_interval;
481 if( !dbus_timeout_get_enabled( p_timeout ) )
484 /* The correct poll timeout value is the shortest one
485 * in the dbus timeouts list */
486 i_next_timeout = __MIN( i_next_timeout,
487 __MAX( 0, p_info->i_remaining ) );
490 /* next timeout in milliseconds */
491 return i_next_timeout / 1000;
495 * ProcessEvents() reacts to a list of events originating from other VLC threads
497 * This function must be called with p_sys->lock unlocked
499 * @param intf_thread_t *p_intf This interface thread state
500 * @param callback_info_t *p_events the list of events to process
502 static void ProcessEvents( intf_thread_t *p_intf,
503 callback_info_t **p_events, int i_events )
505 playlist_t *p_playlist = p_intf->p_sys->p_playlist;
506 bool b_can_play = p_intf->p_sys->b_can_play;
508 vlc_dictionary_t player_properties, tracklist_properties, root_properties;
509 vlc_dictionary_init( &player_properties, 0 );
510 vlc_dictionary_init( &tracklist_properties, 0 );
511 vlc_dictionary_init( &root_properties, 0 );
513 for( int i = 0; i < i_events; i++ )
515 switch( p_events[i]->signal )
517 case SIGNAL_ITEM_CURRENT:
518 TrackChange( p_intf );
519 vlc_dictionary_insert( &player_properties, "Metadata", NULL );
521 case SIGNAL_INTF_CHANGE:
522 case SIGNAL_PLAYLIST_ITEM_APPEND:
523 case SIGNAL_PLAYLIST_ITEM_DELETED:
525 b_can_play = playlist_CurrentSize( p_playlist ) > 0;
528 if( b_can_play != p_intf->p_sys->b_can_play )
530 p_intf->p_sys->b_can_play = b_can_play;
531 vlc_dictionary_insert( &player_properties, "CanPlay", NULL );
534 if( !vlc_dictionary_has_key( &tracklist_properties, "Tracks" ) )
535 vlc_dictionary_insert( &tracklist_properties, "Tracks", NULL );
537 case SIGNAL_VOLUME_MUTED:
538 case SIGNAL_VOLUME_CHANGE:
539 vlc_dictionary_insert( &player_properties, "Volume", NULL );
542 vlc_dictionary_insert( &player_properties, "Shuffle", NULL );
544 case SIGNAL_FULLSCREEN:
545 vlc_dictionary_insert( &root_properties, "Fullscreen", NULL );
549 vlc_dictionary_insert( &player_properties, "LoopStatus", NULL );
552 vlc_dictionary_insert( &player_properties, "PlaybackStatus", NULL );
555 vlc_dictionary_insert( &player_properties, "Rate", NULL );
557 case SIGNAL_INPUT_METADATA:
559 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
560 input_item_t *p_item;
563 p_item = input_GetItem( p_input );
564 vlc_object_release( p_input );
567 vlc_dictionary_insert( &player_properties,
572 case SIGNAL_CAN_SEEK:
573 vlc_dictionary_insert( &player_properties, "CanSeek", NULL );
575 case SIGNAL_CAN_PAUSE:
576 vlc_dictionary_insert( &player_properties, "CanPause", NULL );
580 input_thread_t *p_input;
581 input_item_t *p_item;
582 p_input = playlist_CurrentInput( p_intf->p_sys->p_playlist );
585 p_item = input_GetItem( p_input );
586 vlc_object_release( p_input );
588 if( p_item && ( p_item->i_id == p_events[i]->i_item ) )
589 SeekedEmit( p_intf );
599 if( vlc_dictionary_keys_count( &player_properties ) )
600 PlayerPropertiesChangedEmit( p_intf, &player_properties );
602 if( vlc_dictionary_keys_count( &tracklist_properties ) )
603 TrackListPropertiesChangedEmit( p_intf, &tracklist_properties );
605 if( vlc_dictionary_keys_count( &root_properties ) )
606 RootPropertiesChangedEmit( p_intf, &root_properties );
608 vlc_dictionary_clear( &player_properties, NULL, NULL );
609 vlc_dictionary_clear( &tracklist_properties, NULL, NULL );
610 vlc_dictionary_clear( &root_properties, NULL, NULL );
614 * ProcessWatches() handles a list of dbus watches after poll() has returned
616 * This function must be called with p_sys->lock unlocked
618 * @param intf_thread_t *p_intf This interface thread state
619 * @param DBusWatch **p_watches The list of dbus watches to process
620 * @param int i_watches The size of the p_watches array
621 * @param struct pollfd *p_fds The result of a poll() call
622 * @param int i_fds The number of file descriptors processed by poll()
624 static void ProcessWatches( intf_thread_t *p_intf,
625 DBusWatch **p_watches, int i_watches,
626 struct pollfd *p_fds, int i_fds )
630 /* Process watches */
631 for( int i = 0; i < i_watches; i++ )
633 DBusWatch *p_watch = p_watches[i];
634 if( !dbus_watch_get_enabled( p_watch ) )
637 for( int j = 0; j < i_fds; j++ )
639 if( p_fds[j].fd != dbus_watch_get_unix_fd( p_watch ) )
643 int i_revents = p_fds[j].revents;
645 if( i_revents & POLLIN )
646 i_flags |= DBUS_WATCH_READABLE;
648 if( i_revents & POLLOUT )
649 i_flags |= DBUS_WATCH_WRITABLE;
651 if( i_revents & POLLERR )
652 i_flags |= DBUS_WATCH_ERROR;
654 if( i_revents & POLLHUP )
655 i_flags |= DBUS_WATCH_HANGUP;
658 dbus_watch_handle( p_watch, i_flags );
664 * ProcessTimeouts() handles DBus timeouts
666 * This function must be called with p_sys->lock locked
668 * @param intf_thread_t *p_intf This interface thread state
669 * @param DBusTimeout **p_timeouts List of timeouts to process
670 * @param int i_timeouts Size of p_timeouts
672 static void ProcessTimeouts( intf_thread_t *p_intf,
673 DBusTimeout **p_timeouts, int i_timeouts )
675 VLC_UNUSED( p_intf );
677 for( int i = 0; i < i_timeouts; i++ )
679 timeout_info_t *p_info = NULL;
681 p_info = (timeout_info_t*) dbus_timeout_get_data( p_timeouts[i] );
683 if( !dbus_timeout_get_enabled( p_info->p_timeout ) )
686 if( p_info->i_remaining > 0 )
689 dbus_timeout_handle( p_info->p_timeout );
690 p_info->i_remaining = dbus_timeout_get_interval( p_info->p_timeout );
695 * DispatchDBusMessages() dispatches incoming dbus messages
696 * (indirectly invoking the callbacks), then it sends outgoing
697 * messages which needs to be sent on the bus (method replies and signals)
699 * This function must be called with p_sys->lock unlocked
701 * @param intf_thread_t *p_intf This interface thread state
703 static void DispatchDBusMessages( intf_thread_t *p_intf )
705 DBusDispatchStatus status;
706 intf_sys_t *p_sys = p_intf->p_sys;
708 /* Dispatch incoming messages */
709 status = dbus_connection_get_dispatch_status( p_sys->p_conn );
710 while( status != DBUS_DISPATCH_COMPLETE )
712 dbus_connection_dispatch( p_sys->p_conn );
713 status = dbus_connection_get_dispatch_status( p_sys->p_conn );
716 /* Send outgoing data */
717 if( dbus_connection_has_messages_to_send( p_sys->p_conn ) )
718 dbus_connection_flush( p_sys->p_conn );
722 * MPRISEntryPoint() routes incoming messages to their respective interface
725 * This function is called during dbus_connection_dispatch()
727 static DBusHandlerResult
728 MPRISEntryPoint ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this )
730 const char *psz_target_interface;
731 const char *psz_interface = dbus_message_get_interface( p_from );
732 const char *psz_method = dbus_message_get_member( p_from );
736 if( psz_interface && strcmp( psz_interface, DBUS_INTERFACE_PROPERTIES ) )
737 psz_target_interface = psz_interface;
741 dbus_error_init( &error );
742 dbus_message_get_args( p_from, &error,
743 DBUS_TYPE_STRING, &psz_target_interface,
746 if( dbus_error_is_set( &error ) )
748 msg_Err( (vlc_object_t*) p_this, "D-Bus error on %s.%s: %s",
749 psz_interface, psz_method,
751 dbus_error_free( &error );
752 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
756 if( !strcmp( psz_target_interface, DBUS_INTERFACE_INTROSPECTABLE ) )
757 return handle_introspect( p_conn, p_from, p_this );
759 if( !strcmp( psz_target_interface, DBUS_MPRIS_ROOT_INTERFACE ) )
760 return handle_root( p_conn, p_from, p_this );
762 if( !strcmp( psz_target_interface, DBUS_MPRIS_PLAYER_INTERFACE ) )
763 return handle_player( p_conn, p_from, p_this );
765 if( !strcmp( psz_target_interface, DBUS_MPRIS_TRACKLIST_INTERFACE ) )
766 return handle_tracklist( p_conn, p_from, p_this );
768 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
771 /*****************************************************************************
773 *****************************************************************************/
775 static void Run ( intf_thread_t *p_intf )
777 intf_sys_t *p_sys = p_intf->p_sys;
778 mtime_t i_last_run = mdate();
782 int canc = vlc_savecancel();
783 vlc_mutex_lock( &p_sys->lock );
785 int i_watches = vlc_array_count( p_sys->p_watches );
786 struct pollfd fds[i_watches];
787 memset(fds, 0, sizeof fds);
789 int i_fds = GetPollFds( p_intf, fds );
791 mtime_t i_now = mdate(), i_loop_interval = i_now - i_last_run;
793 int i_next_timeout = UpdateTimeouts( p_intf, i_loop_interval );
796 vlc_mutex_unlock( &p_sys->lock );
798 /* thread cancellation is allowed while the main loop sleeps */
799 vlc_restorecancel( canc );
801 int i_pollres = poll( fds, i_fds, i_next_timeout );
803 canc = vlc_savecancel();
805 if( -1 == i_pollres )
806 { /* XXX: What should we do when poll() fails ? */
807 msg_Err( p_intf, "poll() failed: %m" );
808 vlc_restorecancel( canc );
812 /* Was the main loop woken up manually ? */
813 if( 0 < i_pollres && ( fds[0].revents & POLLIN ) )
816 (void)read( fds[0].fd, &buf, 1 );
819 /* We need to lock the mutex while building lists of events,
820 * timeouts and watches to process but we can't keep the lock while
821 * processing them, or else we risk a deadlock:
823 * The signal functions could lock mutex X while p_events is locked;
824 * While some other function in vlc (playlist) might lock mutex X
825 * and then set a variable which would call AllCallback(), which itself
826 * needs to lock p_events to add a new event.
828 vlc_mutex_lock( &p_intf->p_sys->lock );
830 /* Get the list of timeouts to process */
831 unsigned int i_timeouts = vlc_array_count( p_sys->p_timeouts );
832 DBusTimeout *p_timeouts[i_timeouts];
833 for( unsigned int i = 0; i < i_timeouts; i++ )
835 p_timeouts[i] = vlc_array_item_at_index( p_sys->p_timeouts, i );
838 /* Get the list of watches to process */
839 i_watches = vlc_array_count( p_sys->p_watches );
840 DBusWatch *p_watches[i_watches];
841 for( int i = 0; i < i_watches; i++ )
843 p_watches[i] = vlc_array_item_at_index( p_sys->p_watches, i );
846 /* Get the list of events to process */
847 int i_events = vlc_array_count( p_intf->p_sys->p_events );
848 callback_info_t* p_info[i_events];
849 for( int i = i_events - 1; i >= 0; i-- )
851 p_info[i] = vlc_array_item_at_index( p_intf->p_sys->p_events, i );
852 vlc_array_remove( p_intf->p_sys->p_events, i );
855 /* now we can release the lock and process what's pending */
856 vlc_mutex_unlock( &p_intf->p_sys->lock );
858 ProcessEvents( p_intf, p_info, i_events );
859 ProcessWatches( p_intf, p_watches, i_watches, fds, i_fds );
861 ProcessTimeouts( p_intf, p_timeouts, i_timeouts );
862 DispatchDBusMessages( p_intf );
864 vlc_restorecancel( canc );
868 static void wakeup_main_loop( void *p_data )
870 intf_thread_t *p_intf = (intf_thread_t*) p_data;
872 if( !write( p_intf->p_sys->p_pipe_fds[PIPE_IN], "\0", 1 ) )
873 msg_Err( p_intf, "Could not wake up the main loop: %m" );
876 /* Flls a callback_info_t data structure in response
877 * to an "intf-event" input event.
879 * @warning This function executes in the input thread.
881 * @return VLC_SUCCESS on success, VLC_E* on error.
883 static int InputCallback( vlc_object_t *p_this, const char *psz_var,
884 vlc_value_t oldval, vlc_value_t newval, void *data )
886 input_thread_t *p_input = (input_thread_t *)p_this;
887 intf_thread_t *p_intf = data;
888 intf_sys_t *p_sys = p_intf->p_sys;
890 dbus_int32_t i_state = PLAYBACK_STATE_INVALID;
892 callback_info_t *p_info = calloc( 1, sizeof( callback_info_t ) );
893 if( unlikely(p_info == NULL) )
896 switch( newval.i_int )
898 case INPUT_EVENT_DEAD:
899 case INPUT_EVENT_ABORT:
900 i_state = PLAYBACK_STATE_STOPPED;
902 case INPUT_EVENT_STATE:
903 switch( var_GetInteger( p_input, "state" ) )
907 i_state = PLAYBACK_STATE_PLAYING;
910 i_state = PLAYBACK_STATE_PAUSED;
913 i_state = PLAYBACK_STATE_STOPPED;
916 case INPUT_EVENT_ITEM_META:
917 p_info->signal = SIGNAL_INPUT_METADATA;
919 case INPUT_EVENT_RATE:
920 p_info->signal = SIGNAL_RATE;
922 case INPUT_EVENT_POSITION:
924 mtime_t i_now = mdate(), i_pos, i_projected_pos, i_interval;
925 float f_current_rate;
928 * XXX: This is way more convoluted than it should be... */
929 i_pos = var_GetTime( p_input, "time" );
931 if( !p_intf->p_sys->i_last_input_pos_event ||
932 !( var_GetInteger( p_input, "state" ) == PLAYING_S ) )
934 p_intf->p_sys->i_last_input_pos_event = i_now;
935 p_intf->p_sys->i_last_input_pos = i_pos;
939 f_current_rate = var_GetFloat( p_input, "rate" );
940 i_interval = ( i_now - p_intf->p_sys->i_last_input_pos_event );
942 i_projected_pos = p_intf->p_sys->i_last_input_pos +
943 ( i_interval * f_current_rate );
945 p_intf->p_sys->i_last_input_pos_event = i_now;
946 p_intf->p_sys->i_last_input_pos = i_pos;
948 if( llabs( i_pos - i_projected_pos ) < SEEK_THRESHOLD )
951 p_info->signal = SIGNAL_SEEK;
952 p_info->i_item = input_GetItem( p_input )->i_id;
957 return VLC_SUCCESS; /* don't care */
960 vlc_mutex_lock( &p_sys->lock );
961 if( i_state != PLAYBACK_STATE_INVALID &&
962 i_state != p_sys->i_playing_state )
964 p_sys->i_playing_state = i_state;
965 p_info->signal = SIGNAL_STATE;
968 vlc_array_append( p_intf->p_sys->p_events, p_info );
971 vlc_mutex_unlock( &p_intf->p_sys->lock );
973 wakeup_main_loop( p_intf );
980 // Get all the callbacks
981 static int AllCallback( vlc_object_t *p_this, const char *psz_var,
982 vlc_value_t oldval, vlc_value_t newval, void *p_data )
984 intf_thread_t *p_intf = p_data;
985 callback_info_t info = { .signal = SIGNAL_NONE };
987 // Wich event is it ?
988 if( !strcmp( "item-current", psz_var ) )
989 info.signal = SIGNAL_ITEM_CURRENT;
990 else if( !strcmp( "volume", psz_var ) )
992 if( oldval.f_float != newval.f_float )
993 info.signal = SIGNAL_VOLUME_CHANGE;
995 else if( !strcmp( "mute", psz_var ) )
997 if( oldval.b_bool != newval.b_bool )
998 info.signal = SIGNAL_VOLUME_MUTED;
1000 else if( !strcmp( "intf-change", psz_var ) )
1001 info.signal = SIGNAL_INTF_CHANGE;
1002 else if( !strcmp( "playlist-item-append", psz_var ) )
1004 info.signal = SIGNAL_PLAYLIST_ITEM_APPEND;
1005 info.i_node = ((playlist_add_t*)newval.p_address)->i_node;
1007 else if( !strcmp( "playlist-item-deleted", psz_var ) )
1008 info.signal = SIGNAL_PLAYLIST_ITEM_DELETED;
1009 else if( !strcmp( "random", psz_var ) )
1010 info.signal = SIGNAL_RANDOM;
1011 else if( !strcmp( "fullscreen", psz_var ) )
1012 info.signal = SIGNAL_FULLSCREEN;
1013 else if( !strcmp( "repeat", psz_var ) )
1014 info.signal = SIGNAL_REPEAT;
1015 else if( !strcmp( "loop", psz_var ) )
1016 info.signal = SIGNAL_LOOP;
1017 else if( !strcmp( "can-seek", psz_var ) )
1018 info.signal = SIGNAL_CAN_SEEK;
1019 else if( !strcmp( "can-pause", psz_var ) )
1020 info.signal = SIGNAL_CAN_PAUSE;
1024 if( info.signal == SIGNAL_NONE )
1027 callback_info_t *p_info = malloc( sizeof( *p_info ) );
1028 if( unlikely(p_info == NULL) )
1033 vlc_mutex_lock( &p_intf->p_sys->lock );
1034 vlc_array_append( p_intf->p_sys->p_events, p_info );
1035 vlc_mutex_unlock( &p_intf->p_sys->lock );
1037 wakeup_main_loop( p_intf );
1042 /*****************************************************************************
1043 * TrackChange: callback on playlist "item-current"
1044 *****************************************************************************/
1045 static int TrackChange( intf_thread_t *p_intf )
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 = NULL;
1050 input_item_t *p_item = NULL;
1052 if( p_intf->p_sys->b_dead )
1055 if( p_sys->p_input )
1057 var_DelCallback( p_sys->p_input, "intf-event", InputCallback, p_intf );
1058 var_DelCallback( p_sys->p_input, "can-pause", AllCallback, p_intf );
1059 var_DelCallback( p_sys->p_input, "can-seek", AllCallback, p_intf );
1060 vlc_object_release( p_sys->p_input );
1061 p_sys->p_input = NULL;
1064 p_sys->b_meta_read = false;
1066 p_input = playlist_CurrentInput( p_playlist );
1072 p_item = input_GetItem( p_input );
1075 vlc_object_release( p_input );
1076 return VLC_EGENERIC;
1079 if( input_item_IsPreparsed( p_item ) )
1080 p_sys->b_meta_read = true;
1082 p_sys->p_input = p_input;
1083 var_AddCallback( p_input, "intf-event", InputCallback, p_intf );
1084 var_AddCallback( p_input, "can-pause", AllCallback, p_intf );
1085 var_AddCallback( p_input, "can-seek", AllCallback, p_intf );
1091 * DemarshalSetPropertyValue() extracts the new property value from a
1092 * org.freedesktop.DBus.Properties.Set method call message.
1094 * @return int VLC_SUCCESS on success
1095 * @param DBusMessage *p_msg a org.freedesktop.DBus.Properties.Set method call
1096 * @param void *p_arg placeholder for the demarshalled value
1098 int DemarshalSetPropertyValue( DBusMessage *p_msg, void *p_arg )
1101 bool b_valid_input = FALSE;
1102 DBusMessageIter in_args, variant;
1103 dbus_message_iter_init( p_msg, &in_args );
1107 i_type = dbus_message_iter_get_arg_type( &in_args );
1108 if( DBUS_TYPE_VARIANT == i_type )
1110 dbus_message_iter_recurse( &in_args, &variant );
1111 dbus_message_iter_get_basic( &variant, p_arg );
1112 b_valid_input = TRUE;
1114 } while( dbus_message_iter_next( &in_args ) );
1116 return b_valid_input ? VLC_SUCCESS : VLC_EGENERIC;
1119 /*****************************************************************************
1120 * GetInputMeta: Fill a DBusMessage with the given input item metadata
1121 *****************************************************************************/
1123 #define ADD_META( entry, type, data ) \
1125 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1126 NULL, &dict_entry ); \
1127 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1128 &ppsz_meta_items[entry] ); \
1129 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1130 type##_AS_STRING, &variant ); \
1131 dbus_message_iter_append_basic( &variant, \
1134 dbus_message_iter_close_container( &dict_entry, &variant ); \
1135 dbus_message_iter_close_container( &dict, &dict_entry ); }
1137 #define ADD_VLC_META_STRING( entry, item ) \
1139 char * psz = input_item_Get##item( p_input );\
1140 ADD_META( entry, DBUS_TYPE_STRING, \
1145 int GetInputMeta( input_item_t* p_input,
1146 DBusMessageIter *args )
1148 DBusMessageIter dict, dict_entry, variant;
1149 /** The duration of the track can be expressed in second, milli-seconds and
1151 dbus_int64_t i_mtime = input_item_GetDuration( p_input );
1152 dbus_uint32_t i_time = i_mtime / 1000000;
1153 dbus_int64_t i_length = i_mtime / 1000;
1156 if( -1 == asprintf( &psz_trackid, MPRIS_TRACKID_FORMAT, p_input->i_id ) )
1159 const char* ppsz_meta_items[] =
1161 "mpris:trackid", "xesam:url", "xesam:title", "xesam:artist", "xesam:album",
1162 "xesam:tracknumber", "vlc:time", "mpris:length", "xesam:genre",
1163 "xesam:userRating", "xesam:contentCreated", "mpris:artUrl", "mb:trackId",
1164 "vlc:audio-bitrate", "vlc:audio-samplerate", "vlc:video-bitrate",
1165 "vlc:audio-codec", "vlc:copyright", "xesam:comment", "vlc:encodedby",
1166 "language", "vlc:length", "vlc:nowplaying", "vlc:publisher", "vlc:setting",
1167 "status", "vlc:url", "vlc:video-codec"
1170 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1172 ADD_META( 0, DBUS_TYPE_OBJECT_PATH, psz_trackid );
1173 ADD_VLC_META_STRING( 1, URI );
1174 ADD_VLC_META_STRING( 2, Title );
1175 ADD_VLC_META_STRING( 3, Artist );
1176 ADD_VLC_META_STRING( 4, Album );
1177 ADD_VLC_META_STRING( 5, TrackNum );
1178 ADD_META( 6, DBUS_TYPE_UINT32, i_time );
1179 ADD_META( 7, DBUS_TYPE_INT64, i_mtime );
1180 ADD_VLC_META_STRING( 8, Genre );
1181 ADD_VLC_META_STRING( 9, Rating );
1182 ADD_VLC_META_STRING( 10, Date );
1183 ADD_VLC_META_STRING( 11, ArtURL );
1184 ADD_VLC_META_STRING( 12, TrackID );
1186 ADD_VLC_META_STRING( 17, Copyright );
1187 ADD_VLC_META_STRING( 18, Description );
1188 ADD_VLC_META_STRING( 19, EncodedBy );
1189 ADD_VLC_META_STRING( 20, Language );
1190 ADD_META( 21, DBUS_TYPE_INT64, i_length );
1191 ADD_VLC_META_STRING( 22, NowPlaying );
1192 ADD_VLC_META_STRING( 23, Publisher );
1193 ADD_VLC_META_STRING( 24, Setting );
1194 ADD_VLC_META_STRING( 25, URL );
1196 free( psz_trackid );
1198 vlc_mutex_lock( &p_input->lock );
1199 if( p_input->p_meta )
1201 int i_status = vlc_meta_GetStatus( p_input->p_meta );
1202 ADD_META( 23, DBUS_TYPE_INT32, i_status );
1204 vlc_mutex_unlock( &p_input->lock );
1206 dbus_message_iter_close_container( args, &dict );
1211 #undef ADD_VLC_META_STRING