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 *, int );
71 static int TrackChange( intf_thread_t * );
72 static int AllCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
81 /*****************************************************************************
83 *****************************************************************************/
84 #define DBUS_UNIQUE_TEXT N_("Unique DBUS service id (org.mpris.vlc-<pid>)")
85 #define DBUS_UNIQUE_LONGTEXT N_( \
86 "Use a unique dbus service id to identify this VLC instance on the DBUS bus. " \
87 "The process identifier (PID) is added to the service name: org.mpris.vlc-<pid>" )
90 set_shortname( N_("dbus"))
91 set_category( CAT_INTERFACE )
92 set_subcategory( SUBCAT_INTERFACE_CONTROL )
93 set_description( N_("D-Bus control interface") )
94 set_capability( "interface", 0 )
95 set_callbacks( Open, Close )
96 add_bool( "dbus-unique-service-id", false, NULL,
97 DBUS_UNIQUE_TEXT, DBUS_UNIQUE_LONGTEXT, true )
100 /*****************************************************************************
101 * Open: initialize interface
102 *****************************************************************************/
104 static int Open( vlc_object_t *p_this )
105 { /* initialisation of the connection */
106 intf_thread_t *p_intf = (intf_thread_t*)p_this;
107 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
108 playlist_t *p_playlist;
109 DBusConnection *p_conn;
111 char *psz_service_name = NULL;
116 p_sys->b_meta_read = false;
117 p_sys->i_caps = CAPS_NONE;
118 p_sys->b_dead = false;
119 p_sys->p_input = NULL;
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, "state", 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, info[i]->i_input_state );
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 // Wich event is it ?
346 if( !strcmp( "item-current", psz_var ) )
347 info->signal = SIGNAL_ITEM_CURRENT;
348 else if( !strcmp( "intf-change", psz_var ) )
349 info->signal = SIGNAL_INTF_CHANGE;
350 else if( !strcmp( "playlist-item-append", psz_var ) )
352 info->signal = SIGNAL_PLAYLIST_ITEM_APPEND;
353 info->i_node = ((playlist_add_t*)newval.p_address)->i_node;
355 else if( !strcmp( "playlist-item-deleted", psz_var ) )
356 info->signal = SIGNAL_PLAYLIST_ITEM_DELETED;
357 else if( !strcmp( "random", psz_var ) )
358 info->signal = SIGNAL_RANDOM;
359 else if( !strcmp( "repeat", psz_var ) )
360 info->signal = SIGNAL_REPEAT;
361 else if( !strcmp( "loop", psz_var ) )
362 info->signal = SIGNAL_LOOP;
363 else if( !strcmp( "state", psz_var ) )
365 info->signal = SIGNAL_STATE;
366 info->i_input_state = newval.i_int;
372 vlc_mutex_lock( &p_intf->p_sys->lock );
373 vlc_array_append( p_intf->p_sys->p_events, info );
374 vlc_mutex_unlock( &p_intf->p_sys->lock );
378 /*****************************************************************************
379 * StateChange: callback on input "state"
380 *****************************************************************************/
381 //static int StateChange( vlc_object_t *p_this, const char* psz_var,
382 // vlc_value_t oldval, vlc_value_t newval, void *p_data )
383 static int StateChange( intf_thread_t *p_intf, int i_input_state )
385 intf_sys_t *p_sys = p_intf->p_sys;
386 playlist_t *p_playlist = p_sys->p_playlist;
387 input_thread_t *p_input;
388 input_item_t *p_item;
390 if( p_intf->p_sys->b_dead )
393 UpdateCaps( p_intf );
395 if( !p_sys->b_meta_read && i_input_state == PLAYING_S )
397 p_input = playlist_CurrentInput( p_playlist );
400 p_item = input_GetItem( p_input );
403 p_sys->b_meta_read = true;
404 TrackChangeEmit( p_intf, p_item );
406 vlc_object_release( p_input );
410 if( i_input_state == PLAYING_S || i_input_state == PAUSE_S ||
411 i_input_state == END_S )
413 StatusChangeEmit( p_intf );
419 /*****************************************************************************
420 * TrackChange: callback on playlist "item-current"
421 *****************************************************************************/
422 static int TrackChange( intf_thread_t *p_intf )
424 intf_sys_t *p_sys = p_intf->p_sys;
425 playlist_t *p_playlist = p_sys->p_playlist;
426 input_thread_t *p_input = NULL;
427 input_item_t *p_item = NULL;
429 if( p_intf->p_sys->b_dead )
434 var_DelCallback( p_sys->p_input, "state", AllCallback, p_intf );
435 vlc_object_release( p_sys->p_input );
436 p_sys->p_input = NULL;
439 p_sys->b_meta_read = false;
441 p_input = playlist_CurrentInput( p_playlist );
447 p_item = input_GetItem( p_input );
450 vlc_object_release( p_input );
454 if( input_item_IsPreparsed( p_item ) )
456 p_sys->b_meta_read = true;
457 TrackChangeEmit( p_intf, p_item );
460 p_sys->p_input = p_input;
461 var_AddCallback( p_input, "state", AllCallback, p_intf );
466 /*****************************************************************************
467 * GetInputMeta: Fill a DBusMessage with the given input item metadata
468 *****************************************************************************/
470 #define ADD_META( entry, type, data ) \
472 dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
473 NULL, &dict_entry ); \
474 dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
475 &ppsz_meta_items[entry] ); \
476 dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
477 type##_AS_STRING, &variant ); \
478 dbus_message_iter_append_basic( &variant, \
481 dbus_message_iter_close_container( &dict_entry, &variant ); \
482 dbus_message_iter_close_container( &dict, &dict_entry ); }
484 #define ADD_VLC_META_STRING( entry, item ) \
486 char * psz = input_item_Get##item( p_input );\
487 ADD_META( entry, DBUS_TYPE_STRING, \
492 int GetInputMeta( input_item_t* p_input,
493 DBusMessageIter *args )
495 DBusMessageIter dict, dict_entry, variant;
496 /** The duration of the track can be expressed in second, milli-seconds and
498 dbus_int64_t i_mtime = input_item_GetDuration( p_input );
499 dbus_uint32_t i_time = i_mtime / 1000000;
500 dbus_int64_t i_length = i_mtime / 1000;
502 const char* ppsz_meta_items[] =
504 /* Official MPRIS metas */
505 "location", "title", "artist", "album", "tracknumber", "time", "mtime",
506 "genre", "rating", "date", "arturl",
507 "audio-bitrate", "audio-samplerate", "video-bitrate",
508 /* VLC specifics metas */
509 "audio-codec", "copyright", "description", "encodedby", "language", "length",
510 "nowplaying", "publisher", "setting", "status", "trackid", "url",
514 dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
516 ADD_VLC_META_STRING( 0, URI );
517 ADD_VLC_META_STRING( 1, Title );
518 ADD_VLC_META_STRING( 2, Artist );
519 ADD_VLC_META_STRING( 3, Album );
520 ADD_VLC_META_STRING( 4, TrackNum );
521 ADD_META( 5, DBUS_TYPE_UINT32, i_time );
522 ADD_META( 6, DBUS_TYPE_UINT32, i_mtime );
523 ADD_VLC_META_STRING( 7, Genre );
524 ADD_VLC_META_STRING( 8, Rating );
525 ADD_VLC_META_STRING( 9, Date );
526 ADD_VLC_META_STRING( 10, ArtURL );
528 ADD_VLC_META_STRING( 15, Copyright );
529 ADD_VLC_META_STRING( 16, Description );
530 ADD_VLC_META_STRING( 17, EncodedBy );
531 ADD_VLC_META_STRING( 18, Language );
532 ADD_META( 19, DBUS_TYPE_INT64, i_length );
533 ADD_VLC_META_STRING( 20, NowPlaying );
534 ADD_VLC_META_STRING( 21, Publisher );
535 ADD_VLC_META_STRING( 22, Setting );
536 ADD_VLC_META_STRING( 24, TrackID );
537 ADD_VLC_META_STRING( 25, URL );
539 vlc_mutex_lock( &p_input->lock );
540 if( p_input->p_meta )
542 int i_status = vlc_meta_GetStatus( p_input->p_meta );
543 ADD_META( 23, DBUS_TYPE_INT32, i_status );
545 vlc_mutex_unlock( &p_input->lock );
547 dbus_message_iter_close_container( args, &dict );
552 #undef ADD_VLC_META_STRING