vlc_arrays: add vlc_dictionary_is_empty
[vlc.git] / modules / control / dbus / dbus.c
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
7  * Copyright © 2013      Alex Merry
8  * $Id$
9  *
10  * Authors:    Rafaël Carré <funman at videolanorg>
11  *             Mirsal Ennaime <mirsal at mirsal fr>
12  *             Alex Merry <dev at randomguy3 me uk>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 /*
30  * D-Bus Specification:
31  *      http://dbus.freedesktop.org/doc/dbus-specification.html
32  * D-Bus low-level C API (libdbus)
33  *      http://dbus.freedesktop.org/doc/dbus/api/html/index.html
34  *  extract:
35  *   "If you use this low-level API directly, you're signing up for some pain."
36  *
37  * MPRIS Specification version 1.0
38  *      http://wiki.xmms2.xmms.se/index.php/MPRIS
39  */
40
41 /*****************************************************************************
42  * Preamble
43  *****************************************************************************/
44
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48
49 #include <dbus/dbus.h>
50 #include "dbus_common.h"
51 #include "dbus_root.h"
52 #include "dbus_player.h"
53 #include "dbus_tracklist.h"
54 #include "dbus_introspect.h"
55
56 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
57 #include <vlc_common.h>
58 #include <vlc_plugin.h>
59 #include <vlc_interface.h>
60 #include <vlc_playlist.h>
61 #include <vlc_input.h>
62 #include <vlc_meta.h>
63 #include <vlc_mtime.h>
64 #include <vlc_fs.h>
65
66 #include <assert.h>
67 #include <limits.h>
68 #include <string.h>
69
70 #include <poll.h>
71 #include <errno.h>
72 #include <unistd.h>
73
74 #define DBUS_MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
75 #define DBUS_INSTANCE_ID_PREFIX "instance"
76
77 #define SEEK_THRESHOLD 1000 /* µsec */
78
79 /*****************************************************************************
80  * Local prototypes.
81  *****************************************************************************/
82
83 static DBusHandlerResult
84 MPRISEntryPoint ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this );
85
86 static const DBusObjectPathVTable dbus_mpris_vtable = {
87         NULL, MPRISEntryPoint, /* handler function */
88         NULL, NULL, NULL, NULL
89 };
90
91 typedef struct
92 {
93     int signal;
94 } callback_info_t;
95
96 enum
97 {
98     PIPE_OUT = 0,
99     PIPE_IN  = 1
100 };
101
102 static int  Open    ( vlc_object_t * );
103 static void Close   ( vlc_object_t * );
104 static void *Run    ( void * );
105
106 static int TrackChange( intf_thread_t * );
107 static int AllCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
108 static int InputCallback( vlc_object_t*, const char*, vlc_value_t, vlc_value_t, void* );
109
110 static dbus_bool_t add_timeout(DBusTimeout *, void *);
111 static void remove_timeout(DBusTimeout *, void *);
112 static void toggle_timeout(DBusTimeout *, void *);
113
114 static dbus_bool_t add_watch   ( DBusWatch *p_watch, void *p_data );
115 static void remove_watch    ( DBusWatch *p_watch, void *p_data );
116 static void watch_toggled   ( DBusWatch *p_watch, void *p_data );
117
118 static void wakeup_main_loop( void *p_data );
119
120 static void ProcessEvents  ( intf_thread_t    *p_intf,
121                              callback_info_t **p_events,
122                              int               i_events );
123
124 static void ProcessWatches ( intf_thread_t    *p_intf,
125                              DBusWatch       **p_watches,
126                              int               i_watches,
127                              struct pollfd    *p_fds,
128                              int               i_fds );
129
130 static void DispatchDBusMessages( intf_thread_t *p_intf );
131
132 /*****************************************************************************
133  * Module descriptor
134  *****************************************************************************/
135 vlc_module_begin ()
136     set_shortname( N_("DBus"))
137     set_category( CAT_INTERFACE )
138     set_description( N_("D-Bus control interface") )
139     set_capability( "interface", 0 )
140     set_callbacks( Open, Close )
141 vlc_module_end ()
142
143 /*****************************************************************************
144  * Open: initialize interface
145  *****************************************************************************/
146
147 static int Open( vlc_object_t *p_this )
148 {
149     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
150
151     /* initialisation of the connection */
152     if( !dbus_threads_init_default() )
153         return VLC_EGENERIC;
154
155     intf_sys_t *p_sys  = calloc( 1, sizeof( intf_sys_t ) );
156     if( unlikely(!p_sys) )
157         return VLC_ENOMEM;
158
159     playlist_t      *p_playlist;
160     DBusConnection  *p_conn;
161     p_sys->i_player_caps   = PLAYER_CAPS_NONE;
162     p_sys->i_playing_state = PLAYBACK_STATE_INVALID;
163
164     if( vlc_pipe( p_sys->p_pipe_fds ) )
165     {
166         free( p_sys );
167         msg_Err( p_intf, "Could not create pipe" );
168         return VLC_EGENERIC;
169     }
170
171     DBusError error;
172     dbus_error_init( &error );
173
174     /* connect privately to the session bus
175      * the connection will not be shared with other vlc modules which use dbus,
176      * thus avoiding a whole class of concurrency issues */
177     p_conn = dbus_bus_get_private( DBUS_BUS_SESSION, &error );
178     if( !p_conn )
179     {
180         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
181                 error.message );
182         dbus_error_free( &error );
183         vlc_close( p_sys->p_pipe_fds[1] );
184         vlc_close( p_sys->p_pipe_fds[0] );
185         free( p_sys );
186         return VLC_EGENERIC;
187     }
188
189     dbus_connection_set_exit_on_disconnect( p_conn, FALSE );
190
191     /* Register the entry point object path */
192     dbus_connection_register_object_path( p_conn, DBUS_MPRIS_OBJECT_PATH,
193             &dbus_mpris_vtable, p_this );
194
195     /* Try to register org.mpris.MediaPlayer2.vlc */
196     const unsigned bus_flags = DBUS_NAME_FLAG_DO_NOT_QUEUE;
197     var_Create(p_intf->obj.libvlc, "dbus-mpris-name", VLC_VAR_STRING);
198     if( dbus_bus_request_name( p_conn, DBUS_MPRIS_BUS_NAME, bus_flags, NULL )
199                                      != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER )
200     {
201         /* Register an instance-specific well known name of the form
202          * org.mpris.MediaPlayer2.vlc.instanceXXXX where XXXX is the
203          * current Process ID */
204         char unique_service[sizeof( DBUS_MPRIS_BUS_NAME ) +
205                             sizeof( DBUS_INSTANCE_ID_PREFIX ) + 10];
206
207         snprintf( unique_service, sizeof (unique_service),
208                   DBUS_MPRIS_BUS_NAME"."DBUS_INSTANCE_ID_PREFIX"%"PRIu32,
209                   (uint32_t)getpid() );
210
211         if( dbus_bus_request_name( p_conn, unique_service, bus_flags, NULL )
212                                      == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER )
213         {
214             msg_Dbg( p_intf, "listening on dbus as: %s", unique_service );
215             var_SetString(p_intf->obj.libvlc, "dbus-mpris-name",
216                           unique_service);
217         }
218     }
219     else
220     {
221         msg_Dbg( p_intf, "listening on dbus as: %s", DBUS_MPRIS_BUS_NAME );
222         var_SetString(p_intf->obj.libvlc, "dbus-mpris-name",
223                       DBUS_MPRIS_BUS_NAME);
224     }
225     dbus_connection_flush( p_conn );
226
227     p_intf->p_sys = p_sys;
228     p_sys->p_conn = p_conn;
229     vlc_array_init( &p_sys->events );
230     vlc_array_init( &p_sys->timeouts );
231     vlc_array_init( &p_sys->watches );
232     vlc_mutex_init( &p_sys->lock );
233
234     p_playlist = pl_Get( p_intf );
235     p_sys->p_playlist = p_playlist;
236
237     var_AddCallback( p_playlist, "input-current", 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 );
245     var_AddCallback( p_playlist, "fullscreen", AllCallback, p_intf );
246
247     if( !dbus_connection_set_timeout_functions( p_conn,
248                                                 add_timeout,
249                                                 remove_timeout,
250                                                 toggle_timeout,
251                                                 p_intf, NULL ) )
252         goto error;
253
254     if( !dbus_connection_set_watch_functions( p_conn,
255                                               add_watch,
256                                               remove_watch,
257                                               watch_toggled,
258                                               p_intf, NULL ) )
259         goto error;
260
261     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
262         goto error;
263
264     return VLC_SUCCESS;
265
266 error:
267     var_Destroy(p_intf->obj.libvlc, "dbus-mpris-name");
268     /* The dbus connection is private,
269      * so we are responsible for closing it
270      * XXX: Does this make sense when OOM ? */
271     dbus_connection_close( p_sys->p_conn );
272     dbus_connection_unref( p_conn );
273
274     vlc_mutex_destroy( &p_sys->lock );
275
276     vlc_close( p_sys->p_pipe_fds[1] );
277     vlc_close( p_sys->p_pipe_fds[0] );
278     free( p_sys );
279     return VLC_ENOMEM;
280 }
281
282 /*****************************************************************************
283  * Close: destroy interface
284  *****************************************************************************/
285
286 static void Close   ( vlc_object_t *p_this )
287 {
288     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
289     intf_sys_t      *p_sys      = p_intf->p_sys;
290     playlist_t      *p_playlist = p_sys->p_playlist;
291
292     vlc_cancel( p_sys->thread );
293     vlc_join( p_sys->thread, NULL );
294
295     var_DelCallback( p_playlist, "input-current", AllCallback, p_intf );
296     var_DelCallback( p_playlist, "volume", AllCallback, p_intf );
297     var_DelCallback( p_playlist, "mute", AllCallback, p_intf );
298     var_DelCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
299     var_DelCallback( p_playlist, "playlist-item-deleted", AllCallback, p_intf );
300     var_DelCallback( p_playlist, "random", AllCallback, p_intf );
301     var_DelCallback( p_playlist, "repeat", AllCallback, p_intf );
302     var_DelCallback( p_playlist, "loop", AllCallback, p_intf );
303     var_DelCallback( p_playlist, "fullscreen", AllCallback, p_intf );
304
305     if( p_sys->p_input )
306     {
307         var_DelCallback( p_sys->p_input, "intf-event", InputCallback, p_intf );
308         var_DelCallback( p_sys->p_input, "can-pause", AllCallback, p_intf );
309         var_DelCallback( p_sys->p_input, "can-seek", AllCallback, p_intf );
310         vlc_object_release( p_sys->p_input );
311     }
312
313     /* The dbus connection is private, so we are responsible
314      * for closing it */
315     dbus_connection_close( p_sys->p_conn );
316     dbus_connection_unref( p_sys->p_conn );
317
318     // Free the events array
319     for( size_t i = 0; i < vlc_array_count( &p_sys->events ); i++ )
320     {
321         callback_info_t* info = vlc_array_item_at_index( &p_sys->events, i );
322         free( info );
323     }
324     vlc_mutex_destroy( &p_sys->lock );
325     vlc_array_clear( &p_sys->events );
326     vlc_array_clear( &p_sys->timeouts );
327     vlc_array_clear( &p_sys->watches );
328     vlc_close( p_sys->p_pipe_fds[1] );
329     vlc_close( p_sys->p_pipe_fds[0] );
330     free( p_sys );
331 }
332
333 static dbus_bool_t add_timeout(DBusTimeout *to, void *data)
334 {
335     intf_thread_t *intf = data;
336     intf_sys_t *sys = intf->p_sys;
337
338     mtime_t *expiry = malloc(sizeof (*expiry));
339     if (unlikely(expiry == NULL))
340         return FALSE;
341
342     dbus_timeout_set_data(to, expiry, free);
343
344     vlc_mutex_lock(&sys->lock);
345     vlc_array_append(&sys->timeouts, to);
346     vlc_mutex_unlock(&sys->lock);
347
348     return TRUE;
349 }
350
351 static void remove_timeout(DBusTimeout *to, void *data)
352 {
353     intf_thread_t *intf = data;
354     intf_sys_t *sys = intf->p_sys;
355     size_t idx;
356
357     vlc_mutex_lock(&sys->lock);
358     idx = vlc_array_index_of_item(&sys->timeouts, to);
359     vlc_array_remove(&sys->timeouts, idx);
360     vlc_mutex_unlock(&sys->lock);
361 }
362
363 static void toggle_timeout(DBusTimeout *to, void *data)
364 {
365     intf_thread_t *intf = data;
366     intf_sys_t *sys = intf->p_sys;
367     mtime_t *expiry = dbus_timeout_get_data(to);
368
369     vlc_mutex_lock(&sys->lock);
370     if (dbus_timeout_get_enabled(to))
371         *expiry = mdate() + UINT64_C(1000) * dbus_timeout_get_interval(to);
372     vlc_mutex_unlock(&sys->lock);
373
374     wakeup_main_loop(intf);
375 }
376
377 /**
378  * Computes the time until the next timeout expiration.
379  * @note Interface lock must be held.
380  * @return The time in milliseconds until the next expiration,
381  *         or -1 if there are no pending timeouts.
382  */
383 static int next_timeout(intf_thread_t *intf)
384 {
385     intf_sys_t *sys = intf->p_sys;
386     mtime_t next_timeout = LAST_MDATE;
387     unsigned count = vlc_array_count(&sys->timeouts);
388
389     for (unsigned i = 0; i < count; i++)
390     {
391         DBusTimeout *to = vlc_array_item_at_index(&sys->timeouts, i);
392
393         if (!dbus_timeout_get_enabled(to))
394             continue;
395
396         mtime_t *expiry = dbus_timeout_get_data(to);
397
398         if (next_timeout > *expiry)
399             next_timeout = *expiry;
400     }
401
402     if (next_timeout >= LAST_MDATE)
403         return -1;
404
405     next_timeout /= 1000;
406
407     if (next_timeout > INT_MAX)
408         return INT_MAX;
409
410     return (int)next_timeout;
411 }
412
413 /**
414  * Process pending D-Bus timeouts.
415  *
416  * @note Interface lock must be held.
417  */
418 static void process_timeouts(intf_thread_t *intf)
419 {
420     intf_sys_t *sys = intf->p_sys;
421
422     for (size_t i = 0; i < vlc_array_count(&sys->timeouts); i++)
423     {
424         DBusTimeout *to = vlc_array_item_at_index(&sys->timeouts, i);
425
426         if (!dbus_timeout_get_enabled(to))
427             continue;
428
429         mtime_t *expiry = dbus_timeout_get_data(to);
430         if (*expiry > mdate())
431             continue;
432
433         expiry += UINT64_C(1000) * dbus_timeout_get_interval(to);
434         vlc_mutex_unlock(&sys->lock);
435
436         dbus_timeout_handle(to);
437
438         vlc_mutex_lock(&sys->lock);
439         i = -1; /* lost track of state, restart from beginning */
440     }
441 }
442
443
444 static dbus_bool_t add_watch( DBusWatch *p_watch, void *p_data )
445 {
446     intf_thread_t *p_intf = (intf_thread_t*) p_data;
447     intf_sys_t    *p_sys  = (intf_sys_t*) p_intf->p_sys;
448
449     vlc_mutex_lock( &p_sys->lock );
450     vlc_array_append( &p_sys->watches, p_watch );
451     vlc_mutex_unlock( &p_sys->lock );
452
453     return TRUE;
454 }
455
456 static void remove_watch( DBusWatch *p_watch, void *p_data )
457 {
458     intf_thread_t *p_intf = (intf_thread_t*) p_data;
459     intf_sys_t    *p_sys  = (intf_sys_t*) p_intf->p_sys;
460     size_t idx;
461
462     vlc_mutex_lock( &p_sys->lock );
463     idx = vlc_array_index_of_item( &p_sys->watches, p_watch );
464     vlc_array_remove( &p_sys->watches, idx );
465     vlc_mutex_unlock( &p_sys->lock );
466 }
467
468 static void watch_toggled( DBusWatch *p_watch, void *p_data )
469 {
470     intf_thread_t *p_intf = (intf_thread_t*) p_data;
471
472     if( dbus_watch_get_enabled( p_watch ) )
473         wakeup_main_loop( p_intf );
474 }
475
476 /**
477  * GetPollFds() fills an array of pollfd data structures with :
478  *  - the set of enabled dbus watches
479  *  - the unix pipe which we use to manually wake up the main loop
480  *
481  * This function must be called with p_sys->lock locked
482  *
483  * @return The number of file descriptors
484  *
485  * @param intf_thread_t *p_intf this interface thread's state
486  * @param struct pollfd *p_fds a pointer to a pollfd array large enough to
487  * contain all the returned data (number of enabled dbus watches + 1)
488  */
489 static int GetPollFds( intf_thread_t *p_intf, struct pollfd *p_fds )
490 {
491     intf_sys_t *p_sys = p_intf->p_sys;
492     size_t i_watches = vlc_array_count( &p_sys->watches );
493     int i_fds = 1;
494
495     p_fds[0].fd = p_sys->p_pipe_fds[PIPE_OUT];
496     p_fds[0].events = POLLIN | POLLPRI;
497
498     for( size_t i = 0; i < i_watches; i++ )
499     {
500         DBusWatch *p_watch = NULL;
501         p_watch = vlc_array_item_at_index( &p_sys->watches, i );
502         if( !dbus_watch_get_enabled( p_watch ) )
503             continue;
504
505         p_fds[i_fds].fd = dbus_watch_get_unix_fd( p_watch );
506         int i_flags = dbus_watch_get_flags( p_watch );
507
508         if( i_flags & DBUS_WATCH_READABLE )
509             p_fds[i_fds].events |= POLLIN | POLLPRI;
510
511         if( i_flags & DBUS_WATCH_WRITABLE )
512             p_fds[i_fds].events |= POLLOUT;
513
514         i_fds++;
515     }
516
517     return i_fds;
518 }
519
520 /**
521  * ProcessEvents() reacts to a list of events originating from other VLC threads
522  *
523  * This function must be called with p_sys->lock unlocked
524  *
525  * @param intf_thread_t *p_intf This interface thread state
526  * @param callback_info_t *p_events the list of events to process
527  */
528 static void ProcessEvents( intf_thread_t *p_intf,
529                            callback_info_t **p_events, int i_events )
530 {
531     bool b_can_play = p_intf->p_sys->b_can_play;
532
533     vlc_dictionary_t player_properties, tracklist_properties, root_properties;
534     vlc_dictionary_init( &player_properties,    0 );
535     vlc_dictionary_init( &tracklist_properties, 0 );
536     vlc_dictionary_init( &root_properties,      0 );
537
538     for( int i = 0; i < i_events; i++ )
539     {
540         switch( p_events[i]->signal )
541         {
542         case SIGNAL_ITEM_CURRENT:
543             TrackChange( p_intf );
544
545             // rate depends on current item
546             if( !vlc_dictionary_has_key( &player_properties, "Rate" ) )
547                 vlc_dictionary_insert( &player_properties, "Rate", NULL );
548
549             vlc_dictionary_insert( &player_properties, "Metadata", NULL );
550             break;
551         case SIGNAL_PLAYLIST_ITEM_APPEND:
552         case SIGNAL_PLAYLIST_ITEM_DELETED:
553         {
554             playlist_t *p_playlist = p_intf->p_sys->p_playlist;
555             PL_LOCK;
556             b_can_play = playlist_CurrentSize( p_playlist ) > 0;
557             PL_UNLOCK;
558
559             if( b_can_play != p_intf->p_sys->b_can_play )
560             {
561                 p_intf->p_sys->b_can_play = b_can_play;
562                 vlc_dictionary_insert( &player_properties, "CanPlay", NULL );
563             }
564
565             if( !vlc_dictionary_has_key( &tracklist_properties, "Tracks" ) )
566                 vlc_dictionary_insert( &tracklist_properties, "Tracks", NULL );
567             break;
568         }
569         case SIGNAL_VOLUME_MUTED:
570         case SIGNAL_VOLUME_CHANGE:
571             vlc_dictionary_insert( &player_properties, "Volume", NULL );
572             break;
573         case SIGNAL_RANDOM:
574             vlc_dictionary_insert( &player_properties, "Shuffle", NULL );
575             break;
576         case SIGNAL_FULLSCREEN:
577             vlc_dictionary_insert( &root_properties, "Fullscreen", NULL );
578             break;
579         case SIGNAL_REPEAT:
580         case SIGNAL_LOOP:
581             vlc_dictionary_insert( &player_properties, "LoopStatus", NULL );
582             break;
583         case SIGNAL_STATE:
584             vlc_dictionary_insert( &player_properties, "PlaybackStatus", NULL );
585             break;
586         case SIGNAL_RATE:
587             vlc_dictionary_insert( &player_properties, "Rate", NULL );
588             break;
589         case SIGNAL_INPUT_METADATA:
590         {
591             input_thread_t *p_input = pl_CurrentInput( p_intf );
592             input_item_t   *p_item;
593             if( p_input )
594             {
595                 p_item = input_GetItem( p_input );
596                 vlc_object_release( p_input );
597
598                 if( p_item )
599                     vlc_dictionary_insert( &player_properties,
600                                            "Metadata", NULL );
601             }
602             break;
603         }
604         case SIGNAL_CAN_SEEK:
605             vlc_dictionary_insert( &player_properties, "CanSeek", NULL );
606             break;
607         case SIGNAL_CAN_PAUSE:
608             vlc_dictionary_insert( &player_properties, "CanPause", NULL );
609             break;
610         case SIGNAL_SEEK:
611             SeekedEmit( p_intf );
612             break;
613         default:
614             vlc_assert_unreachable();
615         }
616         free( p_events[i] );
617     }
618
619     if( !vlc_dictionary_is_empty( &player_properties ) )
620         PlayerPropertiesChangedEmit( p_intf, &player_properties );
621
622     if( !vlc_dictionary_is_empty( &tracklist_properties ) )
623         TrackListPropertiesChangedEmit( p_intf, &tracklist_properties );
624
625     if( !vlc_dictionary_is_empty( &root_properties ) )
626         RootPropertiesChangedEmit( p_intf, &root_properties );
627
628     vlc_dictionary_clear( &player_properties,    NULL, NULL );
629     vlc_dictionary_clear( &tracklist_properties, NULL, NULL );
630     vlc_dictionary_clear( &root_properties,      NULL, NULL );
631 }
632
633 /**
634  * ProcessWatches() handles a list of dbus watches after poll() has returned
635  *
636  * This function must be called with p_sys->lock unlocked
637  *
638  * @param intf_thread_t *p_intf This interface thread state
639  * @param DBusWatch **p_watches The list of dbus watches to process
640  * @param int i_watches The size of the p_watches array
641  * @param struct pollfd *p_fds The result of a poll() call
642  * @param int i_fds The number of file descriptors processed by poll()
643  */
644 static void ProcessWatches( intf_thread_t *p_intf,
645                             DBusWatch **p_watches, int i_watches,
646                             struct pollfd *p_fds,  int i_fds )
647 {
648     VLC_UNUSED(p_intf);
649
650     /* Process watches */
651     for( int i = 0; i < i_watches; i++ )
652     {
653         DBusWatch *p_watch = p_watches[i];
654         if( !dbus_watch_get_enabled( p_watch ) )
655             continue;
656
657         for( int j = 0; j < i_fds; j++ )
658         {
659             if( p_fds[j].fd != dbus_watch_get_unix_fd( p_watch ) )
660                 continue;
661
662             int i_flags   = 0;
663             int i_revents = p_fds[j].revents;
664
665             if( i_revents & POLLIN )
666                 i_flags |= DBUS_WATCH_READABLE;
667
668             if( i_revents & POLLOUT )
669                 i_flags |= DBUS_WATCH_WRITABLE;
670
671             if( i_revents & POLLERR )
672                 i_flags |= DBUS_WATCH_ERROR;
673
674             if( i_revents & POLLHUP )
675                 i_flags |= DBUS_WATCH_HANGUP;
676
677             if( i_flags )
678                 dbus_watch_handle( p_watch, i_flags );
679         }
680     }
681 }
682
683 /**
684  * DispatchDBusMessages() dispatches incoming dbus messages
685  * (indirectly invoking the callbacks), then it sends outgoing
686  * messages which needs to be sent on the bus (method replies and signals)
687  *
688  * This function must be called with p_sys->lock unlocked
689  *
690  * @param intf_thread_t *p_intf This interface thread state
691  */
692 static void DispatchDBusMessages( intf_thread_t *p_intf )
693 {
694     DBusDispatchStatus status;
695     intf_sys_t *p_sys = p_intf->p_sys;
696
697     /* Dispatch incoming messages */
698     status = dbus_connection_get_dispatch_status( p_sys->p_conn );
699     while( status != DBUS_DISPATCH_COMPLETE )
700     {
701         dbus_connection_dispatch( p_sys->p_conn );
702         status = dbus_connection_get_dispatch_status( p_sys->p_conn );
703     }
704
705     /* Send outgoing data */
706     if( dbus_connection_has_messages_to_send( p_sys->p_conn ) )
707         dbus_connection_flush( p_sys->p_conn );
708 }
709
710 /**
711  * MPRISEntryPoint() routes incoming messages to their respective interface
712  * implementation.
713  *
714  * This function is called during dbus_connection_dispatch()
715  */
716 static DBusHandlerResult
717 MPRISEntryPoint ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this )
718 {
719     const char *psz_target_interface;
720     const char *psz_interface = dbus_message_get_interface( p_from );
721     const char *psz_method    = dbus_message_get_member( p_from );
722
723     DBusError error;
724
725     if( psz_interface && strcmp( psz_interface, DBUS_INTERFACE_PROPERTIES ) )
726         psz_target_interface = psz_interface;
727
728     else
729     {
730         dbus_error_init( &error );
731         dbus_message_get_args( p_from, &error,
732                                DBUS_TYPE_STRING, &psz_target_interface,
733                                DBUS_TYPE_INVALID );
734
735         if( dbus_error_is_set( &error ) )
736         {
737             msg_Err( (vlc_object_t*) p_this, "D-Bus error on %s.%s: %s",
738                                              psz_interface, psz_method,
739                                              error.message );
740             dbus_error_free( &error );
741             return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
742         }
743     }
744
745     if( !strcmp( psz_target_interface, DBUS_INTERFACE_INTROSPECTABLE ) )
746         return handle_introspect( p_conn, p_from, p_this );
747
748     if( !strcmp( psz_target_interface, DBUS_MPRIS_ROOT_INTERFACE ) )
749         return handle_root( p_conn, p_from, p_this );
750
751     if( !strcmp( psz_target_interface, DBUS_MPRIS_PLAYER_INTERFACE ) )
752         return handle_player( p_conn, p_from, p_this );
753
754     if( !strcmp( psz_target_interface, DBUS_MPRIS_TRACKLIST_INTERFACE ) )
755         return handle_tracklist( p_conn, p_from, p_this );
756
757     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
758 }
759
760 /*****************************************************************************
761  * Run: main loop
762  *****************************************************************************/
763
764 static void *Run( void *data )
765 {
766     intf_thread_t *p_intf = data;
767     intf_sys_t    *p_sys = p_intf->p_sys;
768
769     int canc = vlc_savecancel();
770
771     for( ;; )
772     {
773         vlc_mutex_lock( &p_sys->lock );
774
775         size_t i_watches = vlc_array_count( &p_sys->watches );
776         struct pollfd fds[i_watches];
777         memset(fds, 0, sizeof fds);
778
779         int i_fds = GetPollFds( p_intf, fds );
780         int timeout = next_timeout(p_intf);
781
782         vlc_mutex_unlock( &p_sys->lock );
783
784         /* thread cancellation is allowed while the main loop sleeps */
785         vlc_restorecancel( canc );
786
787         while (poll(fds, i_fds, timeout) == -1)
788         {
789             if (errno != EINTR)
790                 goto error;
791         }
792
793         canc = vlc_savecancel();
794
795         /* Was the main loop woken up manually ? */
796         if (fds[0].revents & POLLIN)
797         {
798             char buf;
799             (void)read( fds[0].fd, &buf, 1 );
800         }
801
802         /* We need to lock the mutex while building lists of events,
803          * timeouts and watches to process but we can't keep the lock while
804          * processing them, or else we risk a deadlock:
805          *
806          * The signal functions could lock mutex X while p_events is locked;
807          * While some other function in vlc (playlist) might lock mutex X
808          * and then set a variable which would call AllCallback(), which itself
809          * needs to lock p_events to add a new event.
810          */
811         vlc_mutex_lock( &p_intf->p_sys->lock );
812
813         process_timeouts(p_intf);
814
815         /* Get the list of watches to process */
816         i_watches = vlc_array_count( &p_sys->watches );
817         DBusWatch *p_watches[i_watches ? i_watches : 1];
818         for( size_t i = 0; i < i_watches; i++ )
819         {
820             p_watches[i] = vlc_array_item_at_index( &p_sys->watches, i );
821         }
822
823         /* Get the list of events to process */
824         size_t i_events = vlc_array_count( &p_sys->events );
825         callback_info_t* p_info[i_events ? i_events : 1];
826         for( size_t i = 0; i < i_events; i++ )
827         {
828             p_info[i] = vlc_array_item_at_index( &p_sys->events, i );
829         }
830
831         vlc_array_clear( &p_sys->events );
832
833         /* now we can release the lock and process what's pending */
834         vlc_mutex_unlock( &p_intf->p_sys->lock );
835
836         ProcessEvents( p_intf, p_info, i_events );
837         ProcessWatches( p_intf, p_watches, i_watches, fds, i_fds );
838
839         DispatchDBusMessages( p_intf );
840     }
841 error:
842     vlc_restorecancel(canc);
843     return NULL;
844 }
845
846 static void   wakeup_main_loop( void *p_data )
847 {
848     intf_thread_t *p_intf = (intf_thread_t*) p_data;
849
850     if( !write( p_intf->p_sys->p_pipe_fds[PIPE_IN], "\0", 1 ) )
851         msg_Err( p_intf, "Could not wake up the main loop: %s",
852                  vlc_strerror_c(errno) );
853 }
854
855 /* Flls a callback_info_t data structure in response
856  * to an "intf-event" input event.
857  *
858  * @warning This function executes in the input thread.
859  *
860  * @return VLC_SUCCESS on success, VLC_E* on error.
861  */
862 static int InputCallback( vlc_object_t *p_this, const char *psz_var,
863                           vlc_value_t oldval, vlc_value_t newval, void *data )
864 {
865     input_thread_t *p_input = (input_thread_t *)p_this;
866     intf_thread_t *p_intf = data;
867     intf_sys_t *p_sys = p_intf->p_sys;
868
869     dbus_int32_t i_state = PLAYBACK_STATE_INVALID;
870
871     callback_info_t *p_info = calloc( 1, sizeof( callback_info_t ) );
872     if( unlikely(p_info == NULL) )
873         return VLC_ENOMEM;
874
875     switch( newval.i_int )
876     {
877         case INPUT_EVENT_DEAD:
878             i_state = PLAYBACK_STATE_STOPPED;
879             break;
880         case INPUT_EVENT_STATE:
881             switch( var_GetInteger( p_input, "state" ) )
882             {
883                 case OPENING_S:
884                 case PLAYING_S:
885                     i_state = PLAYBACK_STATE_PLAYING;
886                     break;
887                 case PAUSE_S:
888                     i_state = PLAYBACK_STATE_PAUSED;
889                     break;
890                 default:
891                     i_state = PLAYBACK_STATE_STOPPED;
892             }
893             break;
894         case INPUT_EVENT_ITEM_META:
895             p_info->signal = SIGNAL_INPUT_METADATA;
896             break;
897         case INPUT_EVENT_RATE:
898             p_info->signal = SIGNAL_RATE;
899             break;
900         case INPUT_EVENT_POSITION:
901         {
902             mtime_t i_now = mdate(), i_pos, i_projected_pos, i_interval;
903             float f_current_rate;
904
905             /* Detect seeks
906              * XXX: This is way more convoluted than it should be... */
907             i_pos = var_GetInteger( p_input, "time" );
908
909             if( !p_intf->p_sys->i_last_input_pos_event ||
910                 !( var_GetInteger( p_input, "state" ) == PLAYING_S ) )
911             {
912                 p_intf->p_sys->i_last_input_pos_event = i_now;
913                 p_intf->p_sys->i_last_input_pos = i_pos;
914                 break;
915             }
916
917             f_current_rate = var_GetFloat( p_input, "rate" );
918             i_interval = ( i_now - p_intf->p_sys->i_last_input_pos_event );
919
920             i_projected_pos = p_intf->p_sys->i_last_input_pos +
921                 ( i_interval * f_current_rate );
922
923             p_intf->p_sys->i_last_input_pos_event = i_now;
924             p_intf->p_sys->i_last_input_pos = i_pos;
925
926             if( llabs( i_pos - i_projected_pos ) < SEEK_THRESHOLD )
927                 break;
928
929             p_info->signal = SIGNAL_SEEK;
930             break;
931         }
932         default:
933             free( p_info );
934             return VLC_SUCCESS; /* don't care */
935     }
936
937     vlc_mutex_lock( &p_sys->lock );
938     if( i_state != PLAYBACK_STATE_INVALID &&
939         i_state != p_sys->i_playing_state )
940     {
941         p_sys->i_playing_state = i_state;
942         p_info->signal = SIGNAL_STATE;
943     }
944     if( p_info->signal )
945         vlc_array_append( &p_sys->events, p_info );
946     else
947         free( p_info );
948     vlc_mutex_unlock( &p_intf->p_sys->lock );
949
950     wakeup_main_loop( p_intf );
951
952     (void)psz_var;
953     (void)oldval;
954     return VLC_SUCCESS;
955 }
956
957 // Get all the callbacks
958 static int AllCallback( vlc_object_t *p_this, const char *psz_var,
959                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
960 {
961     intf_thread_t *p_intf = p_data;
962     callback_info_t info = { .signal = SIGNAL_NONE };
963
964     // Wich event is it ?
965     if( !strcmp( "input-current", psz_var ) )
966         info.signal = SIGNAL_ITEM_CURRENT;
967     else if( !strcmp( "volume", psz_var ) )
968     {
969         if( oldval.f_float != newval.f_float )
970             info.signal = SIGNAL_VOLUME_CHANGE;
971     }
972     else if( !strcmp( "mute", psz_var ) )
973     {
974         if( oldval.b_bool != newval.b_bool )
975             info.signal = SIGNAL_VOLUME_MUTED;
976     }
977     else if( !strcmp( "playlist-item-append", psz_var ) )
978         info.signal = SIGNAL_PLAYLIST_ITEM_APPEND;
979     else if( !strcmp( "playlist-item-deleted", psz_var ) )
980         info.signal = SIGNAL_PLAYLIST_ITEM_DELETED;
981     else if( !strcmp( "random", psz_var ) )
982         info.signal = SIGNAL_RANDOM;
983     else if( !strcmp( "fullscreen", psz_var ) )
984         info.signal = SIGNAL_FULLSCREEN;
985     else if( !strcmp( "repeat", psz_var ) )
986         info.signal = SIGNAL_REPEAT;
987     else if( !strcmp( "loop", psz_var ) )
988         info.signal = SIGNAL_LOOP;
989     else if( !strcmp( "can-seek", psz_var ) )
990         info.signal = SIGNAL_CAN_SEEK;
991     else if( !strcmp( "can-pause", psz_var ) )
992         info.signal = SIGNAL_CAN_PAUSE;
993     else
994         vlc_assert_unreachable();
995
996     if( info.signal == SIGNAL_NONE )
997         return VLC_SUCCESS;
998
999     callback_info_t *p_info = malloc( sizeof( *p_info ) );
1000     if( unlikely(p_info == NULL) )
1001         return VLC_ENOMEM;
1002
1003     // Append the event
1004     *p_info = info;
1005     vlc_mutex_lock( &p_intf->p_sys->lock );
1006     vlc_array_append( &p_intf->p_sys->events, p_info );
1007     vlc_mutex_unlock( &p_intf->p_sys->lock );
1008
1009     wakeup_main_loop( p_intf );
1010     (void) p_this;
1011     return VLC_SUCCESS;
1012 }
1013
1014 /*****************************************************************************
1015  * TrackChange: callback on playlist "input-current"
1016  *****************************************************************************/
1017 static int TrackChange( intf_thread_t *p_intf )
1018 {
1019     intf_sys_t          *p_sys      = p_intf->p_sys;
1020     input_thread_t      *p_input    = NULL;
1021     input_item_t        *p_item     = NULL;
1022
1023     if( p_intf->p_sys->b_dead )
1024         return VLC_SUCCESS;
1025
1026     if( p_sys->p_input )
1027     {
1028         var_DelCallback( p_sys->p_input, "intf-event", InputCallback, p_intf );
1029         var_DelCallback( p_sys->p_input, "can-pause", AllCallback, p_intf );
1030         var_DelCallback( p_sys->p_input, "can-seek", AllCallback, p_intf );
1031         vlc_object_release( p_sys->p_input );
1032         p_sys->p_input = NULL;
1033     }
1034
1035     p_sys->b_meta_read = false;
1036
1037     p_input = pl_CurrentInput( p_intf );
1038     if( !p_input )
1039     {
1040         return VLC_SUCCESS;
1041     }
1042
1043     p_item = input_GetItem( p_input );
1044     if( !p_item )
1045     {
1046         vlc_object_release( p_input );
1047         return VLC_EGENERIC;
1048     }
1049
1050     if( input_item_IsPreparsed( p_item ) )
1051         p_sys->b_meta_read = true;
1052
1053     p_sys->p_input = p_input;
1054     var_AddCallback( p_input, "intf-event", InputCallback, p_intf );
1055     var_AddCallback( p_input, "can-pause", AllCallback, p_intf );
1056     var_AddCallback( p_input, "can-seek", AllCallback, p_intf );
1057
1058     return VLC_SUCCESS;
1059 }
1060
1061 /**
1062  * DemarshalSetPropertyValue() extracts the new property value from a
1063  * org.freedesktop.DBus.Properties.Set method call message.
1064  *
1065  * @return int VLC_SUCCESS on success
1066  * @param DBusMessage *p_msg a org.freedesktop.DBus.Properties.Set method call
1067  * @param void *p_arg placeholder for the demarshalled value
1068  */
1069 int DemarshalSetPropertyValue( DBusMessage *p_msg, void *p_arg )
1070 {
1071     int  i_type;
1072     bool b_valid_input = FALSE;
1073     DBusMessageIter in_args, variant;
1074     dbus_message_iter_init( p_msg, &in_args );
1075
1076     do
1077     {
1078         i_type = dbus_message_iter_get_arg_type( &in_args );
1079         if( DBUS_TYPE_VARIANT == i_type )
1080         {
1081             dbus_message_iter_recurse( &in_args, &variant );
1082             dbus_message_iter_get_basic( &variant, p_arg );
1083             b_valid_input = TRUE;
1084         }
1085     } while( dbus_message_iter_next( &in_args ) );
1086
1087     return b_valid_input ? VLC_SUCCESS : VLC_EGENERIC;
1088 }
1089
1090 /*****************************************************************************
1091  * GetInputMeta: Fill a DBusMessage with the given input item metadata
1092  *****************************************************************************/
1093
1094 #define ADD_META( entry, type, data ) \
1095     if( data ) { \
1096         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1097                 NULL, &dict_entry ); \
1098         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1099                 &ppsz_meta_items[entry] ); \
1100         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1101                 type##_AS_STRING, &variant ); \
1102         dbus_message_iter_append_basic( &variant, \
1103                 type, \
1104                 & data ); \
1105         dbus_message_iter_close_container( &dict_entry, &variant ); \
1106         dbus_message_iter_close_container( &dict, &dict_entry ); }
1107
1108 #define ADD_VLC_META_STRING( entry, item ) \
1109     { \
1110         char * psz = input_item_Get##item( p_input );\
1111         ADD_META( entry, DBUS_TYPE_STRING, \
1112                   psz ); \
1113         free( psz ); \
1114     }
1115
1116 #define ADD_META_SINGLETON_STRING_LIST( entry, item ) \
1117     { \
1118         char * psz = input_item_Get##item( p_input );\
1119         if( psz ) { \
1120             dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
1121                     NULL, &dict_entry ); \
1122             dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
1123                     &ppsz_meta_items[entry] ); \
1124             dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
1125                     "as", &variant ); \
1126             dbus_message_iter_open_container( &variant, DBUS_TYPE_ARRAY, "s", \
1127                                               &list ); \
1128             dbus_message_iter_append_basic( &list, \
1129                     DBUS_TYPE_STRING, \
1130                     &psz ); \
1131             dbus_message_iter_close_container( &variant, &list ); \
1132             dbus_message_iter_close_container( &dict_entry, &variant ); \
1133             dbus_message_iter_close_container( &dict, &dict_entry ); \
1134         } \
1135         free( psz ); \
1136     }
1137
1138 int GetInputMeta( playlist_item_t *item, DBusMessageIter *args )
1139 {
1140     input_item_t *p_input = item->p_input;
1141     DBusMessageIter dict, dict_entry, variant, list;
1142     /** The duration of the track can be expressed in second, milli-seconds and
1143         µ-seconds */
1144     dbus_int64_t i_mtime = input_item_GetDuration( p_input );
1145     dbus_uint32_t i_time = i_mtime / 1000000;
1146     dbus_int64_t i_length = i_mtime / 1000;
1147     char *psz_trackid;
1148
1149     if( -1 == asprintf( &psz_trackid, MPRIS_TRACKID_FORMAT, item->i_id ) )
1150         return VLC_ENOMEM;
1151
1152     const char* ppsz_meta_items[] =
1153     {
1154         "mpris:trackid", "xesam:url", "xesam:title", "xesam:artist",
1155         "xesam:album", "xesam:tracknumber", "vlc:time", "mpris:length",
1156         "xesam:genre", "xesam:userRating", "xesam:contentCreated",
1157         "mpris:artUrl", "mb:trackId", "vlc:audio-bitrate",
1158         "vlc:audio-samplerate", "vlc:video-bitrate", "vlc:audio-codec",
1159         "vlc:copyright", "xesam:comment", "vlc:encodedby", "language",
1160         "vlc:length", "vlc:nowplaying", "vlc:publisher", "vlc:setting",
1161         "status", "vlc:url", "vlc:video-codec"
1162     };
1163
1164     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
1165
1166     ADD_META( 0, DBUS_TYPE_OBJECT_PATH, psz_trackid );
1167     ADD_VLC_META_STRING( 1,  URI );
1168     ADD_VLC_META_STRING( 2,  Title );
1169     ADD_META_SINGLETON_STRING_LIST( 3,  Artist );
1170     ADD_VLC_META_STRING( 4,  Album );
1171     ADD_VLC_META_STRING( 5,  TrackNum );
1172     ADD_META( 6, DBUS_TYPE_UINT32, i_time );
1173     ADD_META( 7, DBUS_TYPE_INT64,  i_mtime );
1174     ADD_META_SINGLETON_STRING_LIST( 8,  Genre );
1175     //ADD_META( 9, DBUS_TYPE_DOUBLE, rating );
1176     ADD_VLC_META_STRING( 10, Date ); // this is supposed to be in ISO 8601 extended format
1177     ADD_VLC_META_STRING( 11, ArtURL );
1178     ADD_VLC_META_STRING( 12, TrackID );
1179
1180     ADD_VLC_META_STRING( 17, Copyright );
1181     ADD_META_SINGLETON_STRING_LIST( 18, Description );
1182     ADD_VLC_META_STRING( 19, EncodedBy );
1183     ADD_VLC_META_STRING( 20, Language );
1184     ADD_META( 21, DBUS_TYPE_INT64, i_length );
1185     ADD_VLC_META_STRING( 22, NowPlaying );
1186     ADD_VLC_META_STRING( 23, Publisher );
1187     ADD_VLC_META_STRING( 24, Setting );
1188     ADD_VLC_META_STRING( 25, URL );
1189
1190     free( psz_trackid );
1191
1192     vlc_mutex_lock( &p_input->lock );
1193     if( p_input->p_meta )
1194     {
1195         int i_status = vlc_meta_GetStatus( p_input->p_meta );
1196         ADD_META( 23, DBUS_TYPE_INT32, i_status );
1197     }
1198     vlc_mutex_unlock( &p_input->lock );
1199
1200     dbus_message_iter_close_container( args, &dict );
1201     return VLC_SUCCESS;
1202 }
1203
1204 int AddProperty( intf_thread_t *p_intf,
1205                  DBusMessageIter *p_container,
1206                  const char* psz_property_name,
1207                  const char* psz_signature,
1208                  int (*pf_marshaller) (intf_thread_t*, DBusMessageIter*) )
1209 {
1210     DBusMessageIter entry, v;
1211
1212     if( !dbus_message_iter_open_container( p_container,
1213                                            DBUS_TYPE_DICT_ENTRY, NULL,
1214                                            &entry ) )
1215         return VLC_ENOMEM;
1216
1217     if( !dbus_message_iter_append_basic( &entry,
1218                                          DBUS_TYPE_STRING,
1219                                          &psz_property_name ) )
1220         return VLC_ENOMEM;
1221
1222     if( !dbus_message_iter_open_container( &entry,
1223                                            DBUS_TYPE_VARIANT, psz_signature,
1224                                            &v ) )
1225         return VLC_ENOMEM;
1226
1227     if( VLC_SUCCESS != pf_marshaller( p_intf, &v ) )
1228         return VLC_ENOMEM;
1229
1230     if( !dbus_message_iter_close_container( &entry, &v) )
1231         return VLC_ENOMEM;
1232
1233     if( !dbus_message_iter_close_container( p_container, &entry ) )
1234         return VLC_ENOMEM;
1235
1236     return VLC_SUCCESS;
1237 }
1238
1239 #undef ADD_META
1240 #undef ADD_VLC_META_STRING
1241
1242