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 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>
49 #include "dbus_common.h"
50 #include "dbus_root.h"
51 #include "dbus_player.h"
52 #include "dbus_tracklist.h"
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
56 #include <vlc_interface.h>
57 #include <vlc_playlist.h>
62 /*****************************************************************************
64 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
68 static void Run ( intf_thread_t * );
70 static int StateChange( intf_thread_t * );
71 static int TrackChange( intf_thread_t * );
72 static int AllCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
80 /*****************************************************************************
82 *****************************************************************************/
83 #define DBUS_UNIQUE_TEXT N_("Unique DBUS service id (org.mpris.vlc-<pid>)")
84 #define DBUS_UNIQUE_LONGTEXT N_( \
85 "Use a unique dbus service id to identify this VLC instance on the DBUS bus. " \
86 "The process identifier (PID) is added to the service name: org.mpris.vlc-<pid>" )
89 set_shortname( N_("dbus"))
90 set_category( CAT_INTERFACE )
91 set_subcategory( SUBCAT_INTERFACE_CONTROL )
92 set_description( N_("D-Bus control interface") )
93 set_capability( "interface", 0 )
94 set_callbacks( Open, Close )
95 add_bool( "dbus-unique-service-id", false, NULL,
96 DBUS_UNIQUE_TEXT, DBUS_UNIQUE_LONGTEXT, true )
99 /*****************************************************************************
100 * Open: initialize interface
101 *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
104 { /* initialisation of the connection */
105 intf_thread_t *p_intf = (intf_thread_t*)p_this;
106 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
107 playlist_t *p_playlist;
108 DBusConnection *p_conn;
110 char *psz_service_name = NULL;
115 p_sys->b_meta_read = false;
116 p_sys->i_caps = CAPS_NONE;
117 p_sys->b_dead = false;
118 p_sys->p_input = NULL;
119 p_sys->i_playing_state = -1;
121 p_sys->b_unique = var_CreateGetBool( p_intf, "dbus-unique-service-id" );
122 if( p_sys->b_unique )
124 if( asprintf( &psz_service_name, "%s-%d",
125 DBUS_MPRIS_BUS_NAME, getpid() ) < 0 )
133 psz_service_name = strdup(DBUS_MPRIS_BUS_NAME);
136 dbus_error_init( &error );
138 /* connect to the session bus */
139 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
142 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
144 dbus_error_free( &error );
149 /* register a well-known name on the bus */
150 dbus_bus_request_name( p_conn, psz_service_name, 0, &error );
151 if( dbus_error_is_set( &error ) )
153 msg_Err( p_this, "Error requesting service %s: %s",
154 psz_service_name, error.message );
155 dbus_error_free( &error );
156 free( psz_service_name );
160 msg_Info( p_intf, "listening on dbus as: %s", psz_service_name );
161 free( psz_service_name );
163 /* we register the objects */
164 dbus_connection_register_object_path( p_conn, DBUS_MPRIS_ROOT_PATH,
165 &dbus_mpris_root_vtable, p_this );
166 dbus_connection_register_object_path( p_conn, DBUS_MPRIS_PLAYER_PATH,
167 &dbus_mpris_player_vtable, p_this );
168 dbus_connection_register_object_path( p_conn, DBUS_MPRIS_TRACKLIST_PATH,
169 &dbus_mpris_tracklist_vtable, p_this );
171 dbus_connection_flush( p_conn );
173 p_intf->pf_run = Run;
174 p_intf->p_sys = p_sys;
175 p_sys->p_conn = p_conn;
176 p_sys->p_events = vlc_array_new();
177 vlc_mutex_init( &p_sys->lock );
179 p_playlist = pl_Get( p_intf );
180 p_sys->p_playlist = p_playlist;
182 var_AddCallback( p_playlist, "item-current", AllCallback, p_intf );
183 var_AddCallback( p_playlist, "intf-change", AllCallback, p_intf );
184 var_AddCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
185 var_AddCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
186 var_AddCallback( p_playlist, "random", AllCallback, p_intf );
187 var_AddCallback( p_playlist, "repeat", AllCallback, p_intf );
188 var_AddCallback( p_playlist, "loop", AllCallback, p_intf );
190 UpdateCaps( p_intf );
195 /*****************************************************************************
196 * Close: destroy interface
197 *****************************************************************************/
199 static void Close ( vlc_object_t *p_this )
201 intf_thread_t *p_intf = (intf_thread_t*) p_this;
202 intf_sys_t *p_sys = p_intf->p_sys;
203 playlist_t *p_playlist = p_sys->p_playlist;
205 var_DelCallback( p_playlist, "item-current", AllCallback, p_intf );
206 var_DelCallback( p_playlist, "intf-change", AllCallback, p_intf );
207 var_DelCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
208 var_DelCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
209 var_DelCallback( p_playlist, "random", AllCallback, p_intf );
210 var_DelCallback( p_playlist, "repeat", AllCallback, p_intf );
211 var_DelCallback( p_playlist, "loop", AllCallback, p_intf );
215 var_DelCallback( p_sys->p_input, "intf-event", AllCallback, p_intf );
216 vlc_object_release( p_sys->p_input );
219 dbus_connection_unref( p_sys->p_conn );
221 // Free the events array
222 for( int i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
224 callback_info_t* info = vlc_array_item_at_index( p_sys->p_events, i );
227 vlc_mutex_destroy( &p_sys->lock );
228 vlc_array_destroy( p_sys->p_events );
232 /*****************************************************************************
234 *****************************************************************************/
236 static void Run ( intf_thread_t *p_intf )
240 if( dbus_connection_get_dispatch_status(p_intf->p_sys->p_conn)
241 == DBUS_DISPATCH_COMPLETE )
242 msleep( INTF_IDLE_SLEEP );
243 int canc = vlc_savecancel();
244 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
246 /* Get the list of events to process
248 * We can't keep the lock on p_intf->p_sys->p_events, else we risk a
250 * The signal functions could lock mutex X while p_events is locked;
251 * While some other function in vlc (playlist) might lock mutex X
252 * and then set a variable which would call AllCallback(), which itself
253 * needs to lock p_events to add a new event.
255 vlc_mutex_lock( &p_intf->p_sys->lock );
256 int i_events = vlc_array_count( p_intf->p_sys->p_events );
257 callback_info_t* info[i_events];
258 for( int i = i_events - 1; i >= 0; i-- )
260 info[i] = vlc_array_item_at_index( p_intf->p_sys->p_events, i );
261 vlc_array_remove( p_intf->p_sys->p_events, i );
263 vlc_mutex_unlock( &p_intf->p_sys->lock );
265 for( int i = 0; i < i_events; i++ )
267 switch( info[i]->signal )
269 case SIGNAL_ITEM_CURRENT:
270 TrackChange( p_intf );
272 case SIGNAL_INTF_CHANGE:
273 case SIGNAL_PLAYLIST_ITEM_APPEND:
274 case SIGNAL_PLAYLIST_ITEM_DELETED:
275 TrackListChangeEmit( p_intf, info[i]->signal, info[i]->i_node );
280 StatusChangeEmit( p_intf );
283 StateChange( p_intf );
290 vlc_restorecancel( canc );
294 /*****************************************************************************
295 * UpdateCaps: update p_sys->i_caps
296 * This function have to be called with the playlist unlocked
297 ****************************************************************************/
298 int UpdateCaps( intf_thread_t* p_intf )
300 intf_sys_t* p_sys = p_intf->p_sys;
301 dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
302 playlist_t* p_playlist = p_sys->p_playlist;
305 if( p_playlist->current.i_size > 0 )
306 i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
309 input_thread_t* p_input = playlist_CurrentInput( p_playlist );
312 /* XXX: if UpdateCaps() is called too early, these are
313 * unconditionnaly true */
314 if( var_GetBool( p_input, "can-pause" ) )
315 i_caps |= CAPS_CAN_PAUSE;
316 if( var_GetBool( p_input, "can-seek" ) )
317 i_caps |= CAPS_CAN_SEEK;
318 vlc_object_release( p_input );
321 if( p_sys->b_meta_read )
322 i_caps |= CAPS_CAN_PROVIDE_METADATA;
324 if( i_caps != p_intf->p_sys->i_caps )
326 p_sys->i_caps = i_caps;
327 CapsChangeEmit( p_intf );
333 // Get all the callbacks
334 static int AllCallback( vlc_object_t *p_this, const char *psz_var,
335 vlc_value_t oldval, vlc_value_t newval, void *p_data )
339 intf_thread_t *p_intf = (intf_thread_t*)p_data;
341 callback_info_t *info = malloc( sizeof( callback_info_t ) );
345 vlc_mutex_lock( &p_intf->p_sys->lock );
347 // Wich event is it ?
348 if( !strcmp( "item-current", psz_var ) )
349 info->signal = SIGNAL_ITEM_CURRENT;
350 else if( !strcmp( "intf-change", psz_var ) )
351 info->signal = SIGNAL_INTF_CHANGE;
352 else if( !strcmp( "playlist-item-append", psz_var ) )
354 info->signal = SIGNAL_PLAYLIST_ITEM_APPEND;
355 info->i_node = ((playlist_add_t*)newval.p_address)->i_node;
357 else if( !strcmp( "playlist-item-deleted", psz_var ) )
358 info->signal = SIGNAL_PLAYLIST_ITEM_DELETED;
359 else if( !strcmp( "random", psz_var ) )
360 info->signal = SIGNAL_RANDOM;
361 else if( !strcmp( "repeat", psz_var ) )
362 info->signal = SIGNAL_REPEAT;
363 else if( !strcmp( "loop", psz_var ) )
364 info->signal = SIGNAL_LOOP;
365 else if( !strcmp( "intf-event", psz_var ) )
368 state = (var_GetInteger(p_this, "state") == PAUSE_S) ? 1 : 0;
370 if( state == p_intf->p_sys->i_playing_state )
373 p_intf->p_sys->i_playing_state = state;
374 info->signal = SIGNAL_STATE;
380 vlc_array_append( p_intf->p_sys->p_events, info );
383 vlc_mutex_unlock( &p_intf->p_sys->lock );
387 /*****************************************************************************
388 * StateChange: callback on input "state"
389 *****************************************************************************/
390 static int StateChange( intf_thread_t *p_intf )
392 intf_sys_t *p_sys = p_intf->p_sys;
393 playlist_t *p_playlist = p_sys->p_playlist;
394 input_thread_t *p_input;
395 input_item_t *p_item;
397 if( p_intf->p_sys->b_dead )
400 UpdateCaps( p_intf );
402 if( !p_sys->b_meta_read && p_sys->i_playing_state == 0)
404 p_input = playlist_CurrentInput( p_playlist );
407 p_item = input_GetItem( p_input );
410 p_sys->b_meta_read = true;
411 TrackChangeEmit( p_intf, p_item );
413 vlc_object_release( p_input );
417 StatusChangeEmit( p_intf );
422 /*****************************************************************************
423 * TrackChange: callback on playlist "item-current"
424 *****************************************************************************/
425 static int TrackChange( intf_thread_t *p_intf )
427 intf_sys_t *p_sys = p_intf->p_sys;
428 playlist_t *p_playlist = p_sys->p_playlist;
429 input_thread_t *p_input = NULL;
430 input_item_t *p_item = NULL;
432 if( p_intf->p_sys->b_dead )
437 var_DelCallback( p_sys->p_input, "intf-event", AllCallback, p_intf );
438 vlc_object_release( p_sys->p_input );
439 p_sys->p_input = NULL;
442 p_sys->b_meta_read = false;
444 p_input = playlist_CurrentInput( p_playlist );
450 p_item = input_GetItem( p_input );
453 vlc_object_release( p_input );
457 if( input_item_IsPreparsed( p_item ) )
459 p_sys->b_meta_read = true;
460 TrackChangeEmit( p_intf, p_item );
463 p_sys->p_input = p_input;
464 var_AddCallback( p_input, "intf-event", AllCallback, p_intf );
469 /*****************************************************************************
470 * GetInputMeta: Fill a DBusMessage with the given input item metadata
471 *****************************************************************************/
473 #define ADD_META( entry, type, data ) \
475 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
476 NULL, &dict_entry ); \
477 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
478 &ppsz_meta_items[entry] ); \
479 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
480 type##_AS_STRING, &variant ); \
481 dbus_message_iter_append_basic( &variant, \
484 dbus_message_iter_close_container( &dict_entry, &variant ); \
485 dbus_message_iter_close_container( &dict, &dict_entry ); }
487 #define ADD_VLC_META_STRING( entry, item ) \
489 char * psz = input_item_Get##item( p_input );\
490 ADD_META( entry, DBUS_TYPE_STRING, \
495 int GetInputMeta( input_item_t* p_input,
496 DBusMessageIter *args )
498 DBusMessageIter dict, dict_entry, variant;
499 /** The duration of the track can be expressed in second, milli-seconds and
501 dbus_int64_t i_mtime = input_item_GetDuration( p_input );
502 dbus_uint32_t i_time = i_mtime / 1000000;
503 dbus_int64_t i_length = i_mtime / 1000;
505 const char* ppsz_meta_items[] =
507 /* Official MPRIS metas */
508 "location", "title", "artist", "album", "tracknumber", "time", "mtime",
509 "genre", "rating", "date", "arturl",
510 "audio-bitrate", "audio-samplerate", "video-bitrate",
511 /* VLC specifics metas */
512 "audio-codec", "copyright", "description", "encodedby", "language", "length",
513 "nowplaying", "publisher", "setting", "status", "trackid", "url",
517 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
519 ADD_VLC_META_STRING( 0, URI );
520 ADD_VLC_META_STRING( 1, Title );
521 ADD_VLC_META_STRING( 2, Artist );
522 ADD_VLC_META_STRING( 3, Album );
523 ADD_VLC_META_STRING( 4, TrackNum );
524 ADD_META( 5, DBUS_TYPE_UINT32, i_time );
525 ADD_META( 6, DBUS_TYPE_UINT32, i_mtime );
526 ADD_VLC_META_STRING( 7, Genre );
527 ADD_VLC_META_STRING( 8, Rating );
528 ADD_VLC_META_STRING( 9, Date );
529 ADD_VLC_META_STRING( 10, ArtURL );
531 ADD_VLC_META_STRING( 15, Copyright );
532 ADD_VLC_META_STRING( 16, Description );
533 ADD_VLC_META_STRING( 17, EncodedBy );
534 ADD_VLC_META_STRING( 18, Language );
535 ADD_META( 19, DBUS_TYPE_INT64, i_length );
536 ADD_VLC_META_STRING( 20, NowPlaying );
537 ADD_VLC_META_STRING( 21, Publisher );
538 ADD_VLC_META_STRING( 22, Setting );
539 ADD_VLC_META_STRING( 24, TrackID );
540 ADD_VLC_META_STRING( 25, URL );
542 vlc_mutex_lock( &p_input->lock );
543 if( p_input->p_meta )
545 int i_status = vlc_meta_GetStatus( p_input->p_meta );
546 ADD_META( 23, DBUS_TYPE_INT32, i_status );
548 vlc_mutex_unlock( &p_input->lock );
550 dbus_message_iter_close_container( args, &dict );
555 #undef ADD_VLC_META_STRING