]> git.sesse.net Git - vlc/blob - modules/control/dbus/dbus.c
dbus: monitor input state change through "intf-event"
[vlc] / modules / control / dbus / dbus.c
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
7  * $Id$
8  *
9  * Authors:    Rafaël Carré <funman at videolanorg>
10  *             Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*
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
32  *  extract:
33  *   "If you use this low-level API directly, you're signing up for some pain."
34  *
35  * MPRIS Specification version 1.0
36  *      http://wiki.xmms2.xmms.se/index.php/MPRIS
37  */
38
39 /*****************************************************************************
40  * Preamble
41  *****************************************************************************/
42
43 #ifdef HAVE_CONFIG_H
44 # include "config.h"
45 #endif
46
47 #include <dbus/dbus.h>
48 #include "dbus.h"
49 #include "dbus_common.h"
50 #include "dbus_root.h"
51 #include "dbus_player.h"
52 #include "dbus_tracklist.h"
53
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
56 #include <vlc_interface.h>
57 #include <vlc_playlist.h>
58 #include <vlc_meta.h>
59
60 #include <assert.h>
61
62 /*****************************************************************************
63  * Local prototypes.
64  *****************************************************************************/
65
66 static int  Open    ( vlc_object_t * );
67 static void Close   ( vlc_object_t * );
68 static void Run     ( intf_thread_t * );
69
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* );
73
74 typedef struct
75 {
76     int signal;
77     int i_node;
78 } callback_info_t;
79
80 /*****************************************************************************
81  * Module descriptor
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>" )
87
88 vlc_module_begin ()
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 )
97 vlc_module_end ()
98
99 /*****************************************************************************
100  * Open: initialize interface
101  *****************************************************************************/
102
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;
109     DBusError       error;
110     char            *psz_service_name = NULL;
111
112     if( !p_sys )
113         return VLC_ENOMEM;
114
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;
120
121     p_sys->b_unique = var_CreateGetBool( p_intf, "dbus-unique-service-id" );
122     if( p_sys->b_unique )
123     {
124         if( asprintf( &psz_service_name, "%s-%d",
125             DBUS_MPRIS_BUS_NAME, getpid() ) < 0 )
126         {
127             free( p_sys );
128             return VLC_ENOMEM;
129         }
130     }
131     else
132     {
133         psz_service_name = strdup(DBUS_MPRIS_BUS_NAME);
134     }
135
136     dbus_error_init( &error );
137
138     /* connect to the session bus */
139     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
140     if( !p_conn )
141     {
142         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
143                 error.message );
144         dbus_error_free( &error );
145         free( p_sys );
146         return VLC_EGENERIC;
147     }
148
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 ) )
152     {
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 );
157         free( p_sys );
158         return VLC_EGENERIC;
159     }
160     msg_Info( p_intf, "listening on dbus as: %s", psz_service_name );
161     free( psz_service_name );
162
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 );
170
171     dbus_connection_flush( p_conn );
172
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 );
178
179     p_playlist = pl_Get( p_intf );
180     p_sys->p_playlist = p_playlist;
181
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 );
189
190     UpdateCaps( p_intf );
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Close: destroy interface
197  *****************************************************************************/
198
199 static void Close   ( vlc_object_t *p_this )
200 {
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;
204
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 );
212
213     if( p_sys->p_input )
214     {
215         var_DelCallback( p_sys->p_input, "intf-event", AllCallback, p_intf );
216         vlc_object_release( p_sys->p_input );
217     }
218
219     dbus_connection_unref( p_sys->p_conn );
220
221     // Free the events array
222     for( int i = 0; i < vlc_array_count( p_sys->p_events ); i++ )
223     {
224         callback_info_t* info = vlc_array_item_at_index( p_sys->p_events, i );
225         free( info );
226     }
227     vlc_mutex_destroy( &p_sys->lock );
228     vlc_array_destroy( p_sys->p_events );
229     free( p_sys );
230 }
231
232 /*****************************************************************************
233  * Run: main loop
234  *****************************************************************************/
235
236 static void Run          ( intf_thread_t *p_intf )
237 {
238     for( ;; )
239     {
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 );
245
246         /* Get the list of events to process
247          *
248          * We can't keep the lock on p_intf->p_sys->p_events, else we risk a
249          * deadlock:
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.
254          */
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-- )
259         {
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 );
262         }
263         vlc_mutex_unlock( &p_intf->p_sys->lock );
264
265         for( int i = 0; i < i_events; i++ )
266         {
267             switch( info[i]->signal )
268             {
269             case SIGNAL_ITEM_CURRENT:
270                 TrackChange( p_intf );
271                 break;
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 );
276                 break;
277             case SIGNAL_RANDOM:
278             case SIGNAL_REPEAT:
279             case SIGNAL_LOOP:
280                 StatusChangeEmit( p_intf );
281                 break;
282             case SIGNAL_STATE:
283                 StateChange( p_intf );
284                 break;
285             default:
286                 assert(0);
287             }
288             free( info[i] );
289         }
290         vlc_restorecancel( canc );
291     }
292 }
293
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 )
299 {
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;
303
304     PL_LOCK;
305     if( p_playlist->current.i_size > 0 )
306         i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
307     PL_UNLOCK;
308
309     input_thread_t* p_input = playlist_CurrentInput( p_playlist );
310     if( p_input )
311     {
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 );
319     }
320
321     if( p_sys->b_meta_read )
322         i_caps |= CAPS_CAN_PROVIDE_METADATA;
323
324     if( i_caps != p_intf->p_sys->i_caps )
325     {
326         p_sys->i_caps = i_caps;
327         CapsChangeEmit( p_intf );
328     }
329
330     return VLC_SUCCESS;
331 }
332
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 )
336 {
337     (void)p_this;
338     (void)oldval;
339     intf_thread_t *p_intf = (intf_thread_t*)p_data;
340
341     callback_info_t *info = malloc( sizeof( callback_info_t ) );
342     if( !info )
343         return VLC_ENOMEM;
344
345     vlc_mutex_lock( &p_intf->p_sys->lock );
346
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 ) )
353     {
354         info->signal = SIGNAL_PLAYLIST_ITEM_APPEND;
355         info->i_node = ((playlist_add_t*)newval.p_address)->i_node;
356     }
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 ) )
366     {
367         dbus_int32_t state;
368         state = (var_GetInteger(p_this, "state") == PAUSE_S) ? 1 : 0;
369
370         if( state == p_intf->p_sys->i_playing_state )
371             goto end;
372
373         p_intf->p_sys->i_playing_state = state;
374         info->signal = SIGNAL_STATE;
375     }
376     else
377         assert(0);
378
379     // Append the event
380     vlc_array_append( p_intf->p_sys->p_events, info );
381
382 end:
383     vlc_mutex_unlock( &p_intf->p_sys->lock );
384     return VLC_SUCCESS;
385 }
386
387 /*****************************************************************************
388  * StateChange: callback on input "state"
389  *****************************************************************************/
390 static int StateChange( intf_thread_t *p_intf )
391 {
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;
396
397     if( p_intf->p_sys->b_dead )
398         return VLC_SUCCESS;
399
400     UpdateCaps( p_intf );
401
402     if( !p_sys->b_meta_read && p_sys->i_playing_state == 0)
403     {
404         p_input = playlist_CurrentInput( p_playlist );
405         if( p_input )
406         {
407             p_item = input_GetItem( p_input );
408             if( p_item )
409             {
410                 p_sys->b_meta_read = true;
411                 TrackChangeEmit( p_intf, p_item );
412             }
413             vlc_object_release( p_input );
414         }
415     }
416
417     StatusChangeEmit( p_intf );
418
419     return VLC_SUCCESS;
420 }
421
422 /*****************************************************************************
423  * TrackChange: callback on playlist "item-current"
424  *****************************************************************************/
425 static int TrackChange( intf_thread_t *p_intf )
426 {
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;
431
432     if( p_intf->p_sys->b_dead )
433         return VLC_SUCCESS;
434
435     if( p_sys->p_input )
436     {
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;
440     }
441
442     p_sys->b_meta_read = false;
443
444     p_input = playlist_CurrentInput( p_playlist );
445     if( !p_input )
446     {
447         return VLC_SUCCESS;
448     }
449
450     p_item = input_GetItem( p_input );
451     if( !p_item )
452     {
453         vlc_object_release( p_input );
454         return VLC_EGENERIC;
455     }
456
457     if( input_item_IsPreparsed( p_item ) )
458     {
459         p_sys->b_meta_read = true;
460         TrackChangeEmit( p_intf, p_item );
461     }
462
463     p_sys->p_input = p_input;
464     var_AddCallback( p_input, "intf-event", AllCallback, p_intf );
465
466     return VLC_SUCCESS;
467 }
468
469 /*****************************************************************************
470  * GetInputMeta: Fill a DBusMessage with the given input item metadata
471  *****************************************************************************/
472
473 #define ADD_META( entry, type, data ) \
474     if( 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, \
482                 type, \
483                 & data ); \
484         dbus_message_iter_close_container( &dict_entry, &variant ); \
485         dbus_message_iter_close_container( &dict, &dict_entry ); }
486
487 #define ADD_VLC_META_STRING( entry, item ) \
488     { \
489         char * psz = input_item_Get##item( p_input );\
490         ADD_META( entry, DBUS_TYPE_STRING, \
491                   psz ); \
492         free( psz ); \
493     }
494
495 int GetInputMeta( input_item_t* p_input,
496                         DBusMessageIter *args )
497 {
498     DBusMessageIter dict, dict_entry, variant;
499     /** The duration of the track can be expressed in second, milli-seconds and
500         µ-seconds */
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;
504
505     const char* ppsz_meta_items[] =
506     {
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",
514     "video-codec"
515     };
516
517     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
518
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 );
530
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 );
541
542     vlc_mutex_lock( &p_input->lock );
543     if( p_input->p_meta )
544     {
545         int i_status = vlc_meta_GetStatus( p_input->p_meta );
546         ADD_META( 23, DBUS_TYPE_INT32, i_status );
547     }
548     vlc_mutex_unlock( &p_input->lock );
549
550     dbus_message_iter_close_container( args, &dict );
551     return VLC_SUCCESS;
552 }
553
554 #undef ADD_META
555 #undef ADD_VLC_META_STRING
556
557